colorwidget.h 6.21 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
Q_OBJECT

Q_PROPERTY(QString color READ color WRITE setColor NOTIFY colorChanged)
90
91
92
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)
93

94
95
public:

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

102
void setHue(int newHue)
103
{
104
    if (m_color.hsvHue() == newHue)
105
        return;
106

107
    m_color.setHsv(newHue,m_color.hsvSaturation(),m_color.value());
108
109
    update();
    emit hueChanged();
110
    emit colorChanged();
111
}
112

113
int hue() const
114
{
115
116
117
118
    int retval = m_color.hsvHue();
    if (retval<0) retval=0;
    if (retval>359) retval=359;
    return retval;
119
120
121
122
}

void setColor(const QString &colorStr)
{
123
124
125
126
127
128
129
130
131
    if (m_color.name() == colorStr)
        return;

    setColor(QColor(colorStr));
}

void setColor(const QColor &color)
{
    if (m_color == color)
132
        return;
133

134
135
136
137
    int oldsaturation = m_color.hsvSaturation();
    int oldvalue = m_color.value();
    int oldhue = m_color.hsvHue();
    m_color=color;
138
    update();
139
    if (oldhue != m_color.hsvHue()) emit hueChanged();
140
141
    if (oldsaturation != saturation()) emit saturationChanged();
    if (oldvalue != value()) emit valueChanged();
142
143
    emit colorChanged();
}
144

145
146
QString color() const
{
147
    return m_color.name();
148
}
149

150

151
int saturation() const
152
{
153
    return m_color.hsvSaturation();
154
155
}

156
void setSaturation(int newsaturation)
157
{
158
159
    if (m_color.hsvSaturation()==newsaturation) return;
    m_color.setHsv(m_color.hsvHue(),newsaturation,m_color.value());
160
    update();
161
    emit saturationChanged();
162
    emit colorChanged();
163
164
}

165
int value() const
166
{
167
    return m_color.value();
168
169
}

170
void setValue(int newvalue)
171
{
172
173
    if (m_color.value()==newvalue) return;
    m_color.setHsv(m_color.hsvHue(),m_color.hsvSaturation(),newvalue);
174
    update();
175
    emit valueChanged();
176
    emit colorChanged();
177
178
}

179
signals:
180
181
    void colorChanged();
    void hueChanged();
182
183
    void saturationChanged();
    void valueChanged();
184
185

protected:
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
    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);

211
212

private:
213
214
    QColor m_color;
    QColor m_saturatedColor;
215
    bool m_mousePressed;
216
    int m_lastHue;
217
    QPixmap m_cache;
218
219
};

220
221
222
223
224
225
226
227
class HueControl : public QWidget
{
Q_OBJECT

Q_PROPERTY(qreal hue READ hue WRITE setHue NOTIFY hueChanged)

public:

228
HueControl(QWidget *parent = 0) : QWidget(parent), m_color(Qt::white), m_mousePressed(false)
229
230
231
232
233
{
    setFixedWidth(40);
    setFixedHeight(130);
}

234
void setHue(int newHue)
235
{
236
    if (m_color.hsvHue() == newHue)
237
        return;
238
    m_color.setHsv(newHue, m_color.hsvSaturation(), m_color.value());
239
240
241
242
    update();
    emit hueChanged();
}

243
int hue() const
244
{
245
    return m_color.hsvHue();
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
}

signals:
    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);
273
274
}

275
276
277
278
void setCurrent(int y);


private:
279
    QColor m_color;
280
281
282
283
284
    bool m_mousePressed;
    QPixmap m_cache;
};


Kai Koehne's avatar
Kai Koehne committed
285
286
} //QmlDesigner

287
288
289
290
291
QML_DECLARE_TYPE(QmlDesigner::ColorButton);
QML_DECLARE_TYPE(QmlDesigner::HueControl);
QML_DECLARE_TYPE(QmlDesigner::ColorBox);

#endif //COLORWIDGET_H