1 /**************************************************************************
3 ** This file is part of Qt Creator
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
7 ** Contact: Nokia Corporation (info@qt.nokia.com)
10 ** GNU Lesser General Public License Usage
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.
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.
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.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
31 **************************************************************************/
34 #include <QtGui/QPainter>
35 #include <QtGui/QMouseEvent>
37 static inline QString properName(const QColor &color)
40 if (color.alpha() == 255)
41 s.sprintf("#%02x%02x%02x", color.red(), color.green(), color.blue());
43 s.sprintf("#%02x%02x%02x%02x", color.alpha(), color.red(), color.green(), color.blue());
47 static inline QColor properColor(const QString &str)
52 QString lcolorStr = str;
53 if (lcolorStr.at(0) == '#' && lcolorStr.length() == 9) {
54 QString alphaStr = lcolorStr;
56 lcolorStr.remove(0, 3);
57 lcolorStr = "#" + lcolorStr;
60 lalpha = alphaStr.toInt(&v, 16);
64 QColor lcolor(lcolorStr);
65 if (lcolorStr.contains('#'))
66 lcolor.setAlpha(lalpha);
70 static inline int clamp(int x, int lower, int upper)
79 namespace QmlEditorWidgets {
81 void ColorBox::setHue(int newHue)
83 if (m_color.hsvHue() == newHue)
86 int oldAlpha = m_color.alpha();
87 m_color.setHsv(newHue,m_color.hsvSaturation(),m_color.value());
88 m_color.setAlpha(oldAlpha);
94 int ColorBox::hue() const
96 int retval = m_color.hsvHue();
97 if (retval<0) retval=0;
98 if (retval>359) retval=359;
102 void ColorBox::setAlpha(int newAlpha)
104 if (m_color.alpha() == newAlpha)
107 m_color.setAlpha(newAlpha);
113 QString ColorBox::strColor() const
115 return properName(m_color);
118 void ColorBox::setStrColor(const QString &colorStr)
120 if (properName(m_color) == colorStr)
123 setColor(properColor(colorStr));
126 void ColorBox::setColor(const QColor &color)
128 if (m_color == color)
131 int oldsaturation = m_color.hsvSaturation();
132 int oldvalue = m_color.value();
133 int oldhue = m_color.hsvHue();
134 int oldAlpha = m_color.alpha();
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();
143 void ColorBox::setSaturation(int newsaturation)
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);
150 emit saturationChanged();
154 void ColorBox::setCurrent(int x, int y)
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);
165 void ColorBox::setValue(int newvalue)
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);
176 void ColorBox::paintEvent(QPaintEvent *event)
178 QWidget::paintEvent(event);
182 if ((m_color.saturation()>1) && (m_color.value()>1))
183 m_saturatedColor.setHsv(m_color.hsvHue(),255,255);
185 if ((hue() != m_lastHue) || (m_cache.isNull())) {
188 int fixedHue = clamp(m_lastHue, 0, 359);
190 QImage cache = QImage(120, 120, QImage::Format_RGB32);
195 for (int y = 0; y < height; y++) {
196 for (int x = 0; x < width; x++) {
198 c.setHsv(fixedHue, (x*255) / width, 255 - (y*255) / height);
199 cache.setPixel(x, y, c.rgb());
202 m_cache = QPixmap::fromImage(cache);
205 p.drawPixmap(5, 5, m_cache);
207 int x = clamp(m_color.hsvSaturationF() * 120, 0, 119) + 5;
208 int y = clamp(120 - m_color.valueF() * 120, 0, 119) + 5;
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);
218 void ColorBox::mousePressEvent(QMouseEvent *e)
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);
226 void ColorBox::mouseReleaseEvent(QMouseEvent * /* event */)
230 m_mousePressed = false;
233 void ColorBox::mouseMoveEvent(QMouseEvent *e)
237 QPoint pos = e->pos();
238 setCurrent(pos.x() - 5, pos.y() - 5);