UBGraphicsTextItemDelegate.h 5.01 KB
Newer Older
Claudio Valerio's avatar
Claudio Valerio committed
1
/*
2
 * Copyright (C) 2015-2018 Département de l'Instruction Publique (DIP-SEM)
Craig Watson's avatar
Craig Watson committed
3
 *
Claudio Valerio's avatar
Claudio Valerio committed
4
 * Copyright (C) 2013 Open Education Foundation
Claudio Valerio's avatar
Claudio Valerio committed
5
 *
Claudio Valerio's avatar
Claudio Valerio committed
6 7
 * Copyright (C) 2010-2013 Groupement d'Intérêt Public pour
 * l'Education Numérique en Afrique (GIP ENA)
8
 *
Claudio Valerio's avatar
Claudio Valerio committed
9 10 11
 * This file is part of OpenBoard.
 *
 * OpenBoard is free software: you can redistribute it and/or modify
Claudio Valerio's avatar
Claudio Valerio committed
12 13
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 3 of the License,
14 15 16 17
 * with a specific linking exception for the OpenSSL project's
 * "OpenSSL" library (or with modified versions of it that use the
 * same license as the "OpenSSL" library).
 *
Claudio Valerio's avatar
Claudio Valerio committed
18
 * OpenBoard is distributed in the hope that it will be useful,
Claudio Valerio's avatar
Claudio Valerio committed
19
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Claudio Valerio's avatar
Claudio Valerio committed
20
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Claudio Valerio's avatar
Claudio Valerio committed
21
 * GNU General Public License for more details.
Claudio Valerio's avatar
Claudio Valerio committed
22
 *
Claudio Valerio's avatar
Claudio Valerio committed
23
 * You should have received a copy of the GNU General Public License
Claudio Valerio's avatar
Claudio Valerio committed
24
 * along with OpenBoard. If not, see <http://www.gnu.org/licenses/>.
Claudio Valerio's avatar
Claudio Valerio committed
25 26
 */

27

Claudio Valerio's avatar
Claudio Valerio committed
28

Claudio Valerio's avatar
Claudio Valerio committed
29

Claudio Valerio's avatar
Claudio Valerio committed
30 31 32 33 34 35 36 37 38 39 40 41
#ifndef UBGRAPHICSTEXTITEMDELEGATE_H_
#define UBGRAPHICSTEXTITEMDELEGATE_H_

#include <QtGui>

#include <QtSvg>

#include "core/UB.h"
#include "UBGraphicsItemDelegate.h"

class UBGraphicsTextItem;

42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
class AlignTextButton : public DelegateButton
{
    Q_OBJECT

public:
    static const int MAX_KIND = 3;
    enum kind_t{
        k_left = 0
        , k_center
        , k_right
        , k_mixed
    };

    AlignTextButton(const QString & fileName, QGraphicsItem* pDelegated, QGraphicsItem * parent = 0, Qt::WindowFrameSection section = Qt::TopLeftSection);
    virtual ~AlignTextButton();

    void setKind(int pKind);
    int kind() {return mKind;}

    void setNextKind();
    int nextKind() const;

    void setMixedButtonVisible(bool v = true) {mHideMixed = !v;}
    bool isMixedButtonVisible() {return !mHideMixed;}

private:

    QSvgRenderer *rndFromKind(int pknd)
    {
        switch (pknd) {
        case k_left:
            return lft;
            break;
        case k_center:
            return cntr;
            break;
        case k_right:
            return rght;
            break;
        case k_mixed:
            return mxd;
            break;
        }

        return 0;
    }

    QSvgRenderer *curRnd() {return rndFromKind(mKind);}

    QPointer<QSvgRenderer> lft;
    QPointer<QSvgRenderer> cntr;
    QPointer<QSvgRenderer> rght;
    QPointer<QSvgRenderer> mxd;

    int mKind;
    bool mHideMixed;
};

Claudio Valerio's avatar
Claudio Valerio committed
100 101
class UBGraphicsTextItemDelegate : public UBGraphicsItemDelegate
{
102
    Q_OBJECT
Claudio Valerio's avatar
Claudio Valerio committed
103

Aleksei Kanash's avatar
Aleksei Kanash committed
104 105 106 107 108 109
    enum textChangeMode
    {
        changeSize = 0,
        scaleSize
    };

Claudio Valerio's avatar
Claudio Valerio committed
110 111 112
    public:
        UBGraphicsTextItemDelegate(UBGraphicsTextItem* pDelegated, QObject * parent = 0);
        virtual ~UBGraphicsTextItemDelegate();
113
        bool isEditable();
Aleksei Kanash's avatar
Aleksei Kanash committed
114
        void scaleTextSize(qreal multiplyer);
115
        void recolor();
Anatoly Mihalchenko's avatar
Anatoly Mihalchenko committed
116
        virtual QVariant itemChange(QGraphicsItem::GraphicsItemChange change, const QVariant &value);
117
        virtual void createControls();
118
        qreal titleBarWidth();
Claudio Valerio's avatar
Claudio Valerio committed
119 120 121

    public slots:
        void contentsChanged();
122
        virtual void setEditable(bool);
Ilia Ryabokon's avatar
Ilia Ryabokon committed
123
        virtual void remove(bool canUndo);
Claudio Valerio's avatar
Claudio Valerio committed
124 125

    protected:
126 127
        virtual void decorateMenu(QMenu *menu);
        virtual void updateMenuActionState();
Claudio Valerio's avatar
Claudio Valerio committed
128

129
        virtual void freeButtons();
Ivan Ilin's avatar
Ivan Ilin committed
130

131 132 133
        virtual bool mousePressEvent(QGraphicsSceneMouseEvent *event);
        virtual bool mouseMoveEvent(QGraphicsSceneMouseEvent *event);
        virtual bool mouseReleaseEvent(QGraphicsSceneMouseEvent *event);
Claudio Valerio's avatar
Claudio Valerio committed
134

135 136 137
        virtual bool keyPressEvent(QKeyEvent *event);
        virtual bool keyReleaseEvent(QKeyEvent *event);

138
    private:
Claudio Valerio's avatar
Claudio Valerio committed
139 140 141 142 143 144
        UBGraphicsTextItem* delegated();

        DelegateButton* mFontButton;
        DelegateButton* mColorButton;
        DelegateButton* mDecreaseSizeButton;
        DelegateButton* mIncreaseSizeButton;
145
        DelegateButton* mAlignButton;
146

Claudio Valerio's avatar
Claudio Valerio committed
147 148 149
        int mLastFontPixelSize;

        static const int sMinPixelSize;
150
        static const int sMinPointSize;
Claudio Valerio's avatar
Claudio Valerio committed
151 152 153

    private:
        void customize(QFontDialog &fontDialog);
Aleksei Kanash's avatar
Aleksei Kanash committed
154
        void ChangeTextSize(qreal factor, textChangeMode changeMode);
155 156 157 158 159
        void updateAlighButtonState();
        bool oneBlockSelection();
        void saveTextCursorFormats();
        void restoreTextCursorFormats();

160

Ivan Ilin's avatar
Ivan Ilin committed
161
        QFont createDefaultFont();
162
        QAction *mEditableAction;
163 164 165 166 167 168 169 170 171 172 173 174
        struct selectionData_t {
            selectionData_t()
                : mButtonIsPressed(false)
            {}
            bool mButtonIsPressed;
            int position;
            int anchor;
            QString html;
            QTextDocumentFragment selection;
            QList<QTextBlockFormat> fmts;

        } mSelectionData;
Claudio Valerio's avatar
Claudio Valerio committed
175 176 177 178 179 180 181 182 183

    private slots:

        void pickFont();
        void pickColor();

        void decreaseSize();
        void increaseSize();

184 185 186
        void alignButtonProcess();
        void onCursorPositionChanged(const QTextCursor& cursor);
        void onModificationChanged(bool ch);
187
        void onContentChanged();
188

189 190
private:
      const int delta;
Claudio Valerio's avatar
Claudio Valerio committed
191 192 193
};

#endif /* UBGRAPHICSTEXTITEMDELEGATE_H_ */