1 /**************************************************************************
3 ** This file is part of Qt Creator
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
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
16 ** GNU Lesser General Public License Usage
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.
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.
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
32 **************************************************************************/
36 #include <QMouseEvent>
38 static inline QString properName(const QColor &color)
41 if (color.alpha() == 255)
42 s.sprintf("#%02x%02x%02x", color.red(), color.green(), color.blue());
44 s.sprintf("#%02x%02x%02x%02x", color.alpha(), color.red(), color.green(), color.blue());
48 static inline QColor properColor(const QString &str)
53 QString lcolorStr = str;
54 if (lcolorStr.at(0) == '#' && lcolorStr.length() == 9) {
55 QString alphaStr = lcolorStr;
57 lcolorStr.remove(0, 3);
58 lcolorStr = "#" + lcolorStr;
61 lalpha = alphaStr.toInt(&v, 16);
65 QColor lcolor(lcolorStr);
66 if (lcolorStr.contains('#'))
67 lcolor.setAlpha(lalpha);
71 static inline int clamp(int x, int lower, int upper)
80 namespace QmlEditorWidgets {
82 void ColorBox::setHue(int newHue)
84 if (m_color.hsvHue() == newHue)
87 int oldAlpha = m_color.alpha();
88 m_color.setHsv(newHue,m_color.hsvSaturation(),m_color.value());
89 m_color.setAlpha(oldAlpha);
95 int ColorBox::hue() const
97 int retval = m_color.hsvHue();
98 if (retval<0) retval=0;
99 if (retval>359) retval=359;
103 void ColorBox::setAlpha(int newAlpha)
105 if (m_color.alpha() == newAlpha)
108 m_color.setAlpha(newAlpha);
114 QString ColorBox::strColor() const
116 return properName(m_color);
119 void ColorBox::setStrColor(const QString &colorStr)
121 if (properName(m_color) == colorStr)
124 setColor(properColor(colorStr));
127 void ColorBox::setColor(const QColor &color)
129 if (m_color == color)
132 int oldsaturation = m_color.hsvSaturation();
133 int oldvalue = m_color.value();
134 int oldhue = m_color.hsvHue();
135 int oldAlpha = m_color.alpha();
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();
144 void ColorBox::setSaturation(int newsaturation)
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);
151 emit saturationChanged();
155 void ColorBox::setCurrent(int x, int y)
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);
166 void ColorBox::setValue(int newvalue)
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);
177 void ColorBox::paintEvent(QPaintEvent *event)
179 QWidget::paintEvent(event);
183 if ((m_color.saturation()>1) && (m_color.value()>1))
184 m_saturatedColor.setHsv(m_color.hsvHue(),255,255);
186 if ((hue() != m_lastHue) || (m_cache.isNull())) {
189 int fixedHue = clamp(m_lastHue, 0, 359);
191 QImage cache = QImage(120, 120, QImage::Format_RGB32);
196 for (int y = 0; y < height; y++) {
197 for (int x = 0; x < width; x++) {
199 c.setHsv(fixedHue, (x*255) / width, 255 - (y*255) / height);
200 cache.setPixel(x, y, c.rgb());
203 m_cache = QPixmap::fromImage(cache);
206 p.drawPixmap(5, 5, m_cache);
208 int x = clamp(m_color.hsvSaturationF() * 120, 0, 119) + 5;
209 int y = clamp(120 - m_color.valueF() * 120, 0, 119) + 5;
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);
219 void ColorBox::mousePressEvent(QMouseEvent *e)
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);
227 void ColorBox::mouseReleaseEvent(QMouseEvent * /* event */)
231 m_mousePressed = false;
234 void ColorBox::mouseMoveEvent(QMouseEvent *e)
238 QPoint pos = e->pos();
239 setCurrent(pos.x() - 5, pos.y() - 5);