colorwidget.h 8.45 KB
Newer Older
1
2
3
4
/**************************************************************************
**
** This file is part of Qt Creator
**
hjk's avatar
hjk committed
5
** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
**
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** Commercial Usage
**
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Nokia.
**
** GNU Lesser General Public License Usage
**
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** If you are unsure which license is appropriate for your use, please
** contact the sales department at http://qt.nokia.com/contact.
**
**************************************************************************/

#ifndef COLORWIDGET_H
#define COLORWIDGET_H

#include <QWeakPointer>
#include <QtGui/QWidget>
#include <QLabel>
36
37
#include <QToolButton>
#include <QMouseEvent>
38
#include <modelnode.h>
39
#include <qdeclarative.h>
40
#include <propertyeditorvalue.h>
41
#include <qmlitemnode.h>
42
#include <QDialog>
43
#include <QLinearGradient>
44

45
QT_BEGIN_NAMESPACE
46
47
class QtColorButton;
class QToolButton;
48
class QDoubleSpinBox;
49
QT_END_NAMESPACE
50
51
52

namespace QmlDesigner {

53
54
55
56
57
class ColorButton : public QToolButton {

Q_OBJECT

Q_PROPERTY(QString color READ color WRITE setColor NOTIFY colorChanged)
58
Q_PROPERTY(bool noColor READ noColor WRITE setNoColor)
59
Q_PROPERTY(bool showArrow READ showArrow WRITE setShowArrow)
60

61
public:
62
    ColorButton(QWidget *parent = 0) : QToolButton (parent), m_colorString("#ffffff"), m_noColor(false), m_showArrow(true) {}
63

64
65
    void setColor(const QString &colorStr);
    QString color() const { return m_colorString; }
66
    QColor convertedColor() const;
67
68
    bool noColor() const { return m_noColor; }
    void setNoColor(bool f) { m_noColor = f; update(); }
69
    bool showArrow() const { return m_showArrow; }
70
    void setShowArrow(bool b) { m_showArrow = b; }      
71

72
73
74
75
76
77
78
signals:
    void colorChanged();

protected:
    void paintEvent(QPaintEvent *event);
private:
    QString m_colorString;
79
    bool m_noColor;
80
    bool m_showArrow;
81
82
83
};

class ColorBox : public QWidget
84
{
85
86
Q_OBJECT

87
88
Q_PROPERTY(QString strColor READ strColor WRITE setStrColor NOTIFY colorChanged)
Q_PROPERTY(QColor color READ color WRITE setColor NOTIFY colorChanged)
89
90
91
Q_PROPERTY(int hue READ hue WRITE setHue NOTIFY hueChanged)
Q_PROPERTY(int saturation READ saturation WRITE setSaturation NOTIFY saturationChanged)
Q_PROPERTY(int value READ value WRITE setValue NOTIFY valueChanged)
92
Q_PROPERTY(int alpha READ alpha WRITE setAlpha NOTIFY alphaChanged)
93

94
public:
95
96
97
98
99
    ColorBox(QWidget *parent = 0) : QWidget(parent), m_color(Qt::white), m_saturatedColor(Qt::white), m_lastHue(0)
    {
        setFixedWidth(130);
        setFixedHeight(130);
    }
100

101
102
103
104
105
106
107
108
109
110
111
112
    void setHue(int newHue);
    int hue() const;
    void setAlpha(int newAlpha);
    int alpha() const { return m_color.alpha(); }
    void setStrColor(const QString &colorStr);
    void setColor(const QColor &color);
    QString strColor() const;
    QColor color() const { return m_color; }
    int saturation() const { return m_color.hsvSaturation(); }
    void setSaturation(int newsaturation);
    int value() const { return m_color.value(); }
    void setValue(int newvalue);
113

114
signals:
115
116
    void colorChanged();
    void hueChanged();
117
118
    void saturationChanged();
    void valueChanged();
119
    void alphaChanged();
120
121

protected:
122
123
    void paintEvent(QPaintEvent *event);

124
125
126
127
    void mousePressEvent(QMouseEvent *);
    void mouseReleaseEvent(QMouseEvent *);
    void mouseMoveEvent(QMouseEvent *);
    void setCurrent(int x, int y);
128
129

private:
130
131
    QColor m_color;
    QColor m_saturatedColor;
132
    bool m_mousePressed;
133
    int m_lastHue;
134
    QPixmap m_cache;
135
136
};

137
138
139
140
141
142
143
144
class HueControl : public QWidget
{
Q_OBJECT

Q_PROPERTY(qreal hue READ hue WRITE setHue NOTIFY hueChanged)

public:

145
146
    HueControl(QWidget *parent = 0) : QWidget(parent), m_color(Qt::white), m_mousePressed(false)
    {
147
        setFixedWidth(28);
148
149
        setFixedHeight(130);
    }
150

151
152
    void setHue(int newHue);
    int hue() const { return m_color.hsvHue(); }
153
154

signals:
155
    void hueChanged(int hue);
156
157

protected:
158
159
160
161
162
    void paintEvent(QPaintEvent *);
    void mousePressEvent(QMouseEvent *);
    void mouseReleaseEvent(QMouseEvent *);
    void mouseMoveEvent(QMouseEvent *);
    void setCurrent(int y);
163
164

private:
165
    QColor m_color;
166
167
168
169
    bool m_mousePressed;
    QPixmap m_cache;
};

170
171
172
173
174
175
176
class GradientLine : public QWidget {

    Q_OBJECT
    Q_PROPERTY(QColor activeColor READ activeColor WRITE setActiveColor NOTIFY activeColorChanged)
    Q_PROPERTY(QVariant itemNode READ itemNode WRITE setItemNode NOTIFY itemNodeChanged)
    Q_PROPERTY(QString gradientName READ gradientName WRITE setGradientName NOTIFY gradientNameChanged)
    Q_PROPERTY(bool active READ active WRITE setActive)
177
    Q_PROPERTY(QLinearGradient gradient READ gradient WRITE setGradient NOTIFY gradientChanged)
178
179

public:
180
    GradientLine(QWidget *parent = 0);
181
182
183
184

    QVariant itemNode() const { return QVariant::fromValue(m_itemNode.modelNode()); }
    void setItemNode(const QVariant &itemNode);
    QString gradientName() const { return m_gradientName; }
185
    void setGradientName(const QString &newName);
186
    QColor activeColor() const { return m_activeColor; }
187
    void setActiveColor(const QColor &newColor);
188
189
    bool active() const { return m_active; }
    void setActive(bool a) { m_active = a; }
190
191
    QLinearGradient gradient() const { return m_gradient; }
    void setGradient(const QLinearGradient &);
192
193
194

public slots:
    void setupGradient();
195
    void deleteGradient();
196
197
198
199
200

signals:
    void activeColorChanged();
    void itemNodeChanged();
    void gradientNameChanged();
201
202
    void gradientChanged();
    void openColorDialog(const QPoint &pos);
203
protected:
hjk's avatar
hjk committed
204
    bool event(QEvent *event);
205
206
    void keyPressEvent(QKeyEvent * event);
    void paintEvent(QPaintEvent *event);
207
208
    void mousePressEvent(QMouseEvent *event);
    void mouseDoubleClickEvent(QMouseEvent *event);
209
210
211
212
213
214
215
    void mouseReleaseEvent(QMouseEvent *);
    void mouseMoveEvent(QMouseEvent *);    

private:
    void setup();
    void updateGradient();
    int currentColorIndex() const { return m_colorIndex; }
216
    void setCurrentIndex(int i);    
217
218
219
220
221
222
223
224
225

    QColor m_activeColor;
    QmlItemNode m_itemNode;
    QString m_gradientName;
    QList<QColor> m_colorList;
    QList<qreal> m_stops;
    int m_colorIndex;
    bool m_dragActive;
    QPoint m_dragStart;
226
    QLinearGradient m_gradient;
227
228
229
    int m_yOffset;
    bool m_create;
    bool m_active;
230
    bool m_dragOff;
231
232
    bool m_useGradient;

233
234
};

235
236
237
238
239
240
241
242
243
244
class BauhausColorDialog : public QFrame {

    Q_OBJECT

    Q_PROPERTY(QColor color READ color WRITE setColor NOTIFY colorChanged)               

public:
    BauhausColorDialog(QWidget *parent = 0);

    QColor color() const { return m_color; }
245
246
247

    void setupColor(const QColor &color);

248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
    void setColor(const QColor &color)
    {
        if (color == m_color)
            return;

        m_color = color;
        setupWidgets();
        emit colorChanged();
    }

public slots:
    void changeColor(const QColor &color) { setColor(color); }
    void spinBoxChanged();
    void onColorBoxChanged();
    void onHueChanged(int newHue)
    {
        if (m_blockUpdate)
            return;

        if (m_color.hsvHue() == newHue)
            return;
        m_color.setHsv(newHue, m_color.hsvSaturation(), m_color.value());
         setupWidgets();
        emit colorChanged();
    }
    void onAccept()
    {
        emit accepted(m_color);
    }

signals:
  void colorChanged();
  void accepted(const QColor &color);
  void rejected();

protected:
  void setupWidgets();

286
287
288
private:
    QFrame *m_beforeColorWidget;
    QFrame *m_currentColorWidget;
289
290
291
292
293
294
295
296
297
298
299
300
301
    ColorBox *m_colorBox;
    HueControl *m_hueControl;

    QDoubleSpinBox *m_rSpinBox;
    QDoubleSpinBox *m_gSpinBox;
    QDoubleSpinBox *m_bSpinBox;
    QDoubleSpinBox *m_alphaSpinBox;

    QColor m_color;
    bool m_blockUpdate;

};

302

303
class ColorWidget {
304

305
306
public:
    static void registerDeclarativeTypes();
307
308


309
310
311
};


Kai Koehne's avatar
Kai Koehne committed
312
313
} //QmlDesigner

314
315
316
317
318
QML_DECLARE_TYPE(QmlDesigner::ColorButton);
QML_DECLARE_TYPE(QmlDesigner::HueControl);
QML_DECLARE_TYPE(QmlDesigner::ColorBox);

#endif //COLORWIDGET_H