OSDN Git Service

Update license.
[qt-creator-jp/qt-creator-jp.git] / src / libs / qmleditorwidgets / huecontrol.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 "huecontrol.h"
34 #include <QtGui/QPainter>
35 #include <QtGui/QMouseEvent>
36
37 static inline int clamp(int x, int lower, int upper)
38 {
39     if (x < lower)
40         x = lower;
41     if (x > upper)
42         x = upper;
43     return x;
44 }
45
46 namespace QmlEditorWidgets {
47
48 void HueControl::setCurrent(int y)
49 {
50     y = clamp(y, 0, 120);
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());
56 }
57
58 void HueControl::setHue(int newHue)
59 {
60     if (m_color.hsvHue() == newHue)
61         return;
62     m_color.setHsv(newHue, m_color.hsvSaturation(), m_color.value());
63     update();
64     emit hueChanged(m_color.hsvHue());
65 }
66
67 void HueControl::paintEvent(QPaintEvent *event)
68 {
69     QWidget::paintEvent(event);
70
71     QPainter p(this);
72
73     int localHeight = 120;
74
75     if (m_cache.isNull()) {
76         m_cache = QPixmap(10, localHeight);
77
78         QPainter cacheP(&m_cache);
79
80         for (int i = 0; i < localHeight; i++)
81         {
82             QColor c;
83             c.setHsv( (i*359) / 120.0, 255,255);
84             cacheP.fillRect(0, i, 10, i + 1, c);
85         }
86     }
87
88     p.drawPixmap(0, 5, m_cache);
89
90     QVector<QPointF> points;
91
92     int y = m_color.hueF() * 120 + 5;
93
94     points.append(QPointF(5, y));
95     points.append(QPointF(15, y + 5));
96     points.append(QPointF(15, y - 5));
97
98
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);
103     p.translate(0, -1);
104     p.setPen(0x222222);
105     p.setBrush(QColor(0x707070));
106     p.drawPolygon(points);
107 }
108
109 void HueControl::mousePressEvent(QMouseEvent *e)
110 {
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);
115 }
116
117 void HueControl::mouseReleaseEvent(QMouseEvent * /* event */)
118 {
119     m_mousePressed = false;
120 }
121
122 void HueControl::mouseMoveEvent(QMouseEvent *e)
123 {
124     if (!m_mousePressed)
125         return;
126     QPoint pos = e->pos();
127     setCurrent(pos.y() - 5);
128 }
129
130 } //QmlEditorWidgets