OSDN Git Service

It's 2011 now.
[qt-creator-jp/qt-creator-jp.git] / src / libs / qmleditorwidgets / colorbox.cpp
1 /**************************************************************************
2 **
3 ** This file is part of Qt Creator
4 **
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
6 **
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
8 **
9 ** No Commercial Usage
10 **
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
14 ** this package.
15 **
16 ** GNU Lesser General Public License Usage
17 **
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file.  Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Nokia gives you certain additional
26 ** rights.  These rights are described in the Nokia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
31 **
32 **************************************************************************/
33
34 #include "colorbox.h"
35 #include <QPainter>
36 #include <QMouseEvent>
37
38 static inline QString properName(const QColor &color)
39 {
40     QString s;
41     if (color.alpha() == 255)
42         s.sprintf("#%02x%02x%02x", color.red(), color.green(), color.blue());
43     else
44         s.sprintf("#%02x%02x%02x%02x", color.alpha(), color.red(), color.green(), color.blue());
45     return s;
46 }
47
48 static inline QColor properColor(const QString &str)
49 {
50     if (str.isEmpty())
51         return QColor();
52     int lalpha = 255;
53     QString lcolorStr = str;
54     if (lcolorStr.at(0) == '#' && lcolorStr.length() == 9) {
55         QString alphaStr = lcolorStr;
56         alphaStr.truncate(3);
57         lcolorStr.remove(0, 3);
58         lcolorStr = "#" + lcolorStr;
59         alphaStr.remove(0,1);
60         bool v;
61         lalpha = alphaStr.toInt(&v, 16);
62         if (!v)
63             lalpha = 255;
64     }
65     QColor lcolor(lcolorStr);
66     if (lcolorStr.contains('#'))
67         lcolor.setAlpha(lalpha);
68     return lcolor;
69 }
70
71 static inline int clamp(int x, int lower, int upper)
72 {
73     if (x < lower)
74         x = lower;
75     if (x > upper)
76         x = upper;
77     return x;
78 }
79
80 namespace QmlEditorWidgets {
81
82 void ColorBox::setHue(int newHue)
83 {
84     if (m_color.hsvHue() == newHue)
85         return;
86
87     int oldAlpha = m_color.alpha();
88     m_color.setHsv(newHue,m_color.hsvSaturation(),m_color.value());
89     m_color.setAlpha(oldAlpha);
90     update();
91     emit hueChanged();
92     emit colorChanged();
93 }
94
95 int ColorBox::hue() const
96 {
97     int retval = m_color.hsvHue();
98     if (retval<0) retval=0;
99     if (retval>359) retval=359;
100     return retval;
101 }
102
103 void ColorBox::setAlpha(int newAlpha)
104 {
105     if (m_color.alpha() == newAlpha)
106         return;
107
108     m_color.setAlpha(newAlpha);
109     update();
110     emit alphaChanged();
111     emit colorChanged();
112 }
113
114 QString ColorBox::strColor() const
115 {
116     return properName(m_color);
117 }
118
119 void ColorBox::setStrColor(const QString &colorStr)
120 {
121     if (properName(m_color) == colorStr)
122         return;
123
124     setColor(properColor(colorStr));
125 }
126
127 void ColorBox::setColor(const QColor &color)
128 {
129     if (m_color == color)
130         return;
131
132     int oldsaturation = m_color.hsvSaturation();
133     int oldvalue = m_color.value();
134     int oldhue = m_color.hsvHue();
135     int oldAlpha = m_color.alpha();
136     m_color=color;
137     update();
138     if (oldhue != m_color.hsvHue()) emit hueChanged();
139     if (oldsaturation != saturation()) emit saturationChanged();
140     if (oldvalue != value()) emit valueChanged();
141     if (oldAlpha != alpha()) emit alphaChanged();
142 }
143
144 void ColorBox::setSaturation(int newsaturation)
145 {
146     if (m_color.hsvSaturation()==newsaturation) return;
147     int oldAlpha = m_color.alpha();
148     m_color.setHsv(m_color.hsvHue(),newsaturation,m_color.value());
149     m_color.setAlpha(oldAlpha);
150     update();
151     emit saturationChanged();
152     emit colorChanged();
153 }
154
155 void ColorBox::setCurrent(int x, int y)
156 {
157     QColor newColor;
158     x = clamp(x, 0, 120);
159     y = clamp(y, 0, 120);
160     int oldAlpha = m_color.alpha();
161     newColor.setHsv(hue(), (x*255) / 120, 255 - (y*255) / 120);
162     newColor.setAlpha(oldAlpha);
163     setColor(newColor);
164 }
165
166 void ColorBox::setValue(int newvalue)
167 {
168     if (m_color.value()==newvalue) return;
169     int oldAlpha = m_color.alpha();
170     m_color.setHsv(m_color.hsvHue(),m_color.hsvSaturation(),newvalue);
171     m_color.setAlpha(oldAlpha);
172     update();
173     emit valueChanged();
174     emit colorChanged();
175 }
176
177 void ColorBox::paintEvent(QPaintEvent *event)
178 {
179     QWidget::paintEvent(event);
180
181     QPainter p(this);
182
183     if ((m_color.saturation()>1) && (m_color.value()>1))
184         m_saturatedColor.setHsv(m_color.hsvHue(),255,255);
185
186     if ((hue() != m_lastHue) || (m_cache.isNull())) {
187         m_lastHue = hue();
188
189         int fixedHue = clamp(m_lastHue, 0, 359);
190
191         QImage cache = QImage(120, 120, QImage::Format_RGB32);
192
193         int height = 120;
194         int width = 120;
195
196         for (int y = 0; y < height; y++) {
197             for (int x = 0; x < width; x++) {
198                 QColor c;
199                 c.setHsv(fixedHue, (x*255) / width, 255 - (y*255) / height);
200                 cache.setPixel(x, y, c.rgb());
201             }
202         }
203         m_cache = QPixmap::fromImage(cache);
204     }
205     
206     p.drawPixmap(5, 5, m_cache);
207
208     int x = clamp(m_color.hsvSaturationF() * 120, 0, 119) + 5;
209     int y = clamp(120 - m_color.valueF() * 120, 0, 119) + 5;
210
211     p.setPen(QColor(255, 255, 255, 50));
212     p.drawLine(5, y, x-1, y);
213     p.drawLine(x+1, y, width()-7, y);
214     p.drawLine(x, 5, x, y-1);
215     p.drawLine(x, y+1, x, height()-7);
216
217 }
218
219 void ColorBox::mousePressEvent(QMouseEvent *e)
220 {
221     // The current cell marker is set to the cell the mouse is pressed in
222     QPoint pos = e->pos();
223     m_mousePressed = true;
224     setCurrent(pos.x() - 5, pos.y() - 5);
225 }
226
227 void ColorBox::mouseReleaseEvent(QMouseEvent * /* event */)
228 {
229     if (m_mousePressed)
230         emit colorChanged();
231     m_mousePressed = false;
232 }
233
234 void ColorBox::mouseMoveEvent(QMouseEvent *e)
235 {
236     if (!m_mousePressed)
237         return;
238     QPoint pos = e->pos();
239     setCurrent(pos.x() - 5, pos.y() - 5);
240 }
241
242
243 } //QmlEditorWidgets