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 **************************************************************************/
33 #include "huecontrol.h"
34 #include <QtGui/QPainter>
35 #include <QtGui/QMouseEvent>
37 static inline int clamp(int x, int lower, int upper)
46 namespace QmlEditorWidgets {
48 void HueControl::setCurrent(int y)
51 int oldAlpha = m_color.alpha();
52 m_color.setHsv((y * 359)/120, m_color.hsvSaturation(), m_color.value());
53 m_color.setAlpha(oldAlpha);
54 update(); // redraw pointer
55 emit hueChanged(m_color.hsvHue());
58 void HueControl::setHue(int newHue)
60 if (m_color.hsvHue() == newHue)
62 m_color.setHsv(newHue, m_color.hsvSaturation(), m_color.value());
64 emit hueChanged(m_color.hsvHue());
67 void HueControl::paintEvent(QPaintEvent *event)
69 QWidget::paintEvent(event);
73 int localHeight = 120;
75 if (m_cache.isNull()) {
76 m_cache = QPixmap(10, localHeight);
78 QPainter cacheP(&m_cache);
80 for (int i = 0; i < localHeight; i++)
83 c.setHsv( (i*359) / 120.0, 255,255);
84 cacheP.fillRect(0, i, 10, i + 1, c);
88 p.drawPixmap(0, 5, m_cache);
90 QVector<QPointF> points;
92 int y = m_color.hueF() * 120 + 5;
94 points.append(QPointF(5, y));
95 points.append(QPointF(15, y + 5));
96 points.append(QPointF(15, y - 5));
99 p.setRenderHint(QPainter::Antialiasing, true);
100 p.translate(0.5, 1.5);
101 p.setPen(QColor(0, 0, 0, 120));
102 p.drawPolygon(points);
105 p.setBrush(QColor(0x707070));
106 p.drawPolygon(points);
109 void HueControl::mousePressEvent(QMouseEvent *e)
111 // The current cell marker is set to the cell the mouse is pressed in
112 QPoint pos = e->pos();
113 m_mousePressed = true;
114 setCurrent(pos.y() - 5);
117 void HueControl::mouseReleaseEvent(QMouseEvent * /* event */)
119 m_mousePressed = false;
122 void HueControl::mouseMoveEvent(QMouseEvent *e)
126 QPoint pos = e->pos();
127 setCurrent(pos.y() - 5);