OSDN Git Service

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