colorwidget.h 7.7 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

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

namespace QmlDesigner {

52
53
54
55
56
class ColorButton : public QToolButton {

Q_OBJECT

Q_PROPERTY(QString color READ color WRITE setColor NOTIFY colorChanged)
57
58
Q_PROPERTY(bool noColor READ noColor WRITE setNoColor)

59
public:
60
    ColorButton(QWidget *parent = 0) : QToolButton (parent), m_colorString("#ffffff"), m_noColor(false) {}
61

62
63
    void setColor(const QString &colorStr);
    QString color() const { return m_colorString; }
64
65
66
    bool noColor() const { return m_noColor; }
    void setNoColor(bool f) { m_noColor = f; update(); }

67
68
69
70
71
72
73
signals:
    void colorChanged();

protected:
    void paintEvent(QPaintEvent *event);
private:
    QString m_colorString;
74
    bool m_noColor;
75
76
77
};

class ColorBox : public QWidget
78
{
79
80
Q_OBJECT

81
82
Q_PROPERTY(QString strColor READ strColor WRITE setStrColor NOTIFY colorChanged)
Q_PROPERTY(QColor color READ color WRITE setColor NOTIFY colorChanged)
83
84
85
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)
86
Q_PROPERTY(int alpha READ alpha WRITE setAlpha NOTIFY alphaChanged)
87

88
public:
89
90
91
92
93
    ColorBox(QWidget *parent = 0) : QWidget(parent), m_color(Qt::white), m_saturatedColor(Qt::white), m_lastHue(0)
    {
        setFixedWidth(130);
        setFixedHeight(130);
    }
94

95
96
97
98
99
100
101
102
103
104
105
106
    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);
107

108
signals:
109
110
    void colorChanged();
    void hueChanged();
111
112
    void saturationChanged();
    void valueChanged();
113
    void alphaChanged();
114
115

protected:
116
117
    void paintEvent(QPaintEvent *event);

118
119
120
121
    void mousePressEvent(QMouseEvent *);
    void mouseReleaseEvent(QMouseEvent *);
    void mouseMoveEvent(QMouseEvent *);
    void setCurrent(int x, int y);
122
123

private:
124
125
    QColor m_color;
    QColor m_saturatedColor;
126
    bool m_mousePressed;
127
    int m_lastHue;
128
    QPixmap m_cache;
129
130
};

131
132
133
134
135
136
137
138
class HueControl : public QWidget
{
Q_OBJECT

Q_PROPERTY(qreal hue READ hue WRITE setHue NOTIFY hueChanged)

public:

139
140
141
142
143
    HueControl(QWidget *parent = 0) : QWidget(parent), m_color(Qt::white), m_mousePressed(false)
    {
        setFixedWidth(40);
        setFixedHeight(130);
    }
144

145
146
    void setHue(int newHue);
    int hue() const { return m_color.hsvHue(); }
147
148

signals:
149
    void hueChanged(int hue);
150
151

protected:
152
153
154
155
156
    void paintEvent(QPaintEvent *);
    void mousePressEvent(QMouseEvent *);
    void mouseReleaseEvent(QMouseEvent *);
    void mouseMoveEvent(QMouseEvent *);
    void setCurrent(int y);
157
158

private:
159
    QColor m_color;
160
161
162
163
    bool m_mousePressed;
    QPixmap m_cache;
};

164
165
166
167
168
169
170
171
172
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)

public:
173
    GradientLine(QWidget *parent = 0);
174
175
176
177

    QVariant itemNode() const { return QVariant::fromValue(m_itemNode.modelNode()); }
    void setItemNode(const QVariant &itemNode);
    QString gradientName() const { return m_gradientName; }
178
    void setGradientName(const QString &newName);
179
    QColor activeColor() const { return m_activeColor; }
180
    void setActiveColor(const QColor &newColor);
181
182
183
184
185
    bool active() const { return m_active; }
    void setActive(bool a) { m_active = a; }

public slots:
    void setupGradient();
186
    void deleteGradient();
187
188
189
190
191
192

signals:
    void activeColorChanged();
    void itemNodeChanged();
    void gradientNameChanged();
protected:
hjk's avatar
hjk committed
193
    bool event(QEvent *event);
194
195
196
197
198
199
200
201
202
203
    void keyPressEvent(QKeyEvent * event);
    void paintEvent(QPaintEvent *event);
    void mousePressEvent(QMouseEvent *);
    void mouseReleaseEvent(QMouseEvent *);
    void mouseMoveEvent(QMouseEvent *);    

private:
    void setup();
    void updateGradient();
    int currentColorIndex() const { return m_colorIndex; }
204
    void setCurrentIndex(int i);
205
206
207
208
209
210
211
212
213
214
215
216

    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;
    int m_yOffset;
    bool m_create;
    bool m_active;
217
    bool m_dragOff;
218
219
};

220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
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
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; }
    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();

private:    
    ColorBox *m_colorBox;
    HueControl *m_hueControl;

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

    QColor m_color;
    bool m_blockUpdate;

};

282

283
class ColorWidget {
284

285
286
public:
    static void registerDeclarativeTypes();
287
288


289
290
291
};


Kai Koehne's avatar
Kai Koehne committed
292
293
} //QmlDesigner

294
295
296
297
298
QML_DECLARE_TYPE(QmlDesigner::ColorButton);
QML_DECLARE_TYPE(QmlDesigner::HueControl);
QML_DECLARE_TYPE(QmlDesigner::ColorBox);

#endif //COLORWIDGET_H