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

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

92
93
public:

94
95
96
97
98
99
100
101
102
103
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;
104

105
106
107
108
    m_hue = newHue;
    update();
    emit hueChanged();
}
109

110
111
112
113
114
115
116
117
118
qreal hue() const
{
    return m_hue;
}

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

120
121
122
123
124
125
126
    m_colorString = colorStr;
    update();
    qreal newHue = QColor(m_colorString).hsvHueF();
    if (newHue >= 0)
        setHue(newHue);
    emit colorChanged();
}
127

128
129
130
131
QString color() const
{
    return m_colorString;
}
132
133

signals:
134
135
    void colorChanged();
    void hueChanged();
136
137

protected:
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
    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);

163
164

private:
165
166
167
168
169
    QString m_colorString;
    bool m_mousePressed;
    qreal m_hue;
    qreal m_lastHue;
    QPixmap m_cache;
170
171
};

172
173
174
175
176
177
178
179
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
205
206
207
208
209
210
211
212
213
214
215
216
217
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
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;
    update();
    emit hueChanged();
}

qreal hue() const
{
    return m_hue;
}

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();
    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);
243
244
}

245
246
247
248
249
250
251
252
253
254
255
void setCurrent(int y);


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


Kai Koehne's avatar
Kai Koehne committed
256
257
} //QmlDesigner

258
259
260
261
262
QML_DECLARE_TYPE(QmlDesigner::ColorButton);
QML_DECLARE_TYPE(QmlDesigner::HueControl);
QML_DECLARE_TYPE(QmlDesigner::ColorBox);

#endif //COLORWIDGET_H