colorwidget.h 6.55 KB
Newer Older
1
2
3
4
5
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
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
**
** 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
39
40
41
#include <modelnode.h>
#include <qml.h>
#include <propertyeditorvalue.h>

42
QT_BEGIN_NAMESPACE
43
44
class QtColorButton;
class QToolButton;
45
QT_END_NAMESPACE
46
47
48

namespace QmlDesigner {

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
class ColorButton : public QToolButton {

Q_OBJECT

Q_PROPERTY(QString color READ color WRITE setColor NOTIFY colorChanged)

public:

    ColorButton(QWidget *parent = 0) : QToolButton (parent), m_colorString("#ffffff")
    {
    }

    void setColor(const QString &colorStr)
    {
        if (m_colorString == colorStr)
            return;

        m_colorString = colorStr;
        update();
        emit colorChanged();
    }

    QString color() const
    {
        return m_colorString;
    }

signals:
    void colorChanged();

protected:
    void paintEvent(QPaintEvent *event);
private:
    QString m_colorString;
};

class ColorBox : public QWidget
86
{
87
88
89
90
Q_OBJECT

Q_PROPERTY(QString color READ color WRITE setColor NOTIFY colorChanged)
Q_PROPERTY(qreal hue READ hue WRITE setHue NOTIFY hueChanged)
91
92
Q_PROPERTY(qreal saturation READ saturation WRITE setSaturation NOTIFY saturationChanged)
Q_PROPERTY(qreal value READ value WRITE setValue NOTIFY valueChanged)
93

94
95
public:

96
97
98
99
100
101
102
103
104
105
ColorBox(QWidget *parent = 0) : QWidget(parent), m_colorString("#ffffff"), m_hue(0), m_lastHue(0)
{
    setFixedWidth(130);
    setFixedHeight(130);
}

void setHue(qreal newHue)
{
    if (m_hue == newHue)
        return;
106

107
108
109
    m_hue = newHue;
    update();
    emit hueChanged();
110
    emit colorChanged();
111
}
112

113
114
115
116
117
118
119
120
121
qreal hue() const
{
    return m_hue;
}

void setColor(const QString &colorStr)
{
    if (m_colorString == colorStr)
        return;
122

123
124
    qreal oldsaturation = saturation();
    qreal oldvalue = value();
125
126
127
128
129
    m_colorString = colorStr;
    update();
    qreal newHue = QColor(m_colorString).hsvHueF();
    if (newHue >= 0)
        setHue(newHue);
130
131
    if (oldsaturation != saturation()) emit saturationChanged();
    if (oldvalue != value()) emit valueChanged();
132
133
    emit colorChanged();
}
134

135
136
137
138
QString color() const
{
    return m_colorString;
}
139

140
141
142
143
144
145

qreal saturation() const
{
  return QColor(m_colorString).hsvSaturationF();
}

146
void setSaturation(qreal newsaturation)
147
148
{
    QColor color(m_colorString);
149
    if (color.hsvSaturationF()==newsaturation) return;
150
151
    color.setHsvF(color.hsvHueF(),newsaturation,color.valueF());
    m_colorString=color.name();
152
    update();
153
    emit saturationChanged();
154
    emit colorChanged();
155
156
157
158
159
160
161
}

qreal value() const
{
  return QColor(m_colorString).valueF();
}

162
void setValue(qreal newvalue)
163
164
{
    QColor color(m_colorString);
165
    if (color.valueF()==newvalue) return;
166
167
    color.setHsvF(color.hsvHueF(),color.hsvSaturationF(),newvalue);
    m_colorString=color.name();
168
    update();
169
    emit valueChanged();
170
    emit colorChanged();
171
172
}

173
signals:
174
175
    void colorChanged();
    void hueChanged();
176
177
    void saturationChanged();
    void valueChanged();
178
179

protected:
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
    void paintEvent(QPaintEvent *event);

    void mousePressEvent(QMouseEvent *e)
    {
        // The current cell marker is set to the cell the mouse is pressed in
        QPoint pos = e->pos();
        m_mousePressed = true;
        setCurrent(pos.x() - 5, pos.y() - 5);
    }

    void mouseReleaseEvent(QMouseEvent * /* event */)
    {
        m_mousePressed = false;
    }

void mouseMoveEvent(QMouseEvent *e)
{
    if (!m_mousePressed)
        return;
    QPoint pos = e->pos();
    setCurrent(pos.x() - 5, pos.y() - 5);
}

void setCurrent(int x, int y);

205
206

private:
207
208
209
210
211
    QString m_colorString;
    bool m_mousePressed;
    qreal m_hue;
    qreal m_lastHue;
    QPixmap m_cache;
212
213
};

214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
class HueControl : public QWidget
{
Q_OBJECT

Q_PROPERTY(QString color READ color WRITE setColor NOTIFY colorChanged)
Q_PROPERTY(qreal hue READ hue WRITE setHue NOTIFY hueChanged)

public:

HueControl(QWidget *parent = 0) : QWidget(parent), m_colorString("#ffffff"), m_mousePressed(false), m_hue(0)
{
    setFixedWidth(40);
    setFixedHeight(130);
}

void setHue(qreal newHue)
{
    if (m_hue == newHue)
        return;

    m_hue = newHue;
235
236
237
    QColor color(m_colorString);
    color.setHsvF(newHue, color.hsvSaturationF(), color.valueF());
    m_colorString = color.name();
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
    update();
    emit hueChanged();
}

qreal hue() const
{
    return m_hue;
}

void setColor(const QString &colorStr)
{
    if (m_colorString == colorStr)
        return;

    m_colorString = colorStr;
253
    m_hue = QColor(m_colorString).hsvHueF();
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
286
287
288
    update();
    emit colorChanged();
}

QString color() const
{
    return m_colorString;
}

signals:
    void colorChanged();
    void hueChanged();

protected:
    void paintEvent(QPaintEvent *event);

    void mousePressEvent(QMouseEvent *e)
    {
        // The current cell marker is set to the cell the mouse is pressed in
        QPoint pos = e->pos();
        m_mousePressed = true;
        setCurrent(pos.y() - 5);
    }

    void mouseReleaseEvent(QMouseEvent * /* event */)
    {
        m_mousePressed = false;
    }

void mouseMoveEvent(QMouseEvent *e)
{
    if (!m_mousePressed)
        return;
    QPoint pos = e->pos();
    setCurrent(pos.y() - 5);
289
290
}

291
292
293
294
295
296
297
298
299
300
301
void setCurrent(int y);


private:
    QString m_colorString;
    bool m_mousePressed;
    qreal m_hue;
    QPixmap m_cache;
};


Kai Koehne's avatar
Kai Koehne committed
302
303
} //QmlDesigner

304
305
306
307
308
QML_DECLARE_TYPE(QmlDesigner::ColorButton);
QML_DECLARE_TYPE(QmlDesigner::HueControl);
QML_DECLARE_TYPE(QmlDesigner::ColorBox);

#endif //COLORWIDGET_H