OSDN Git Service

b5f6cbf1eb410f04348240ee44cb977d140a79cd
[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 (qt-info@nokia.com)
8 **
9 ** No Commercial Usage
10 **
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
14 ** this package.
15 **
16 ** GNU Lesser General Public License Usage
17 **
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.
24 **
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.
28 **
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
31 **
32 **************************************************************************/
33
34 #include "huecontrol.h"
35 #include <QtGui/QPainter>
36 #include <QtGui/QMouseEvent>
37
38 static inline int clamp(int x, int lower, int upper)
39 {
40     if (x < lower)
41         x = lower;
42     if (x > upper)
43         x = upper;
44     return x;
45 }
46
47 namespace QmlEditorWidgets {
48
49 void HueControl::setCurrent(int y)
50 {
51     y = clamp(y, 0, 120);
52     int oldAlpha = m_color.alpha();
53     m_color.setHsv((y * 359)/120, m_color.hsvSaturation(), m_color.value());
54     m_color.setAlpha(oldAlpha);
55     update(); // redraw pointer
56     emit hueChanged(m_color.hsvHue());
57 }
58
59 void HueControl::setHue(int newHue)
60 {
61     if (m_color.hsvHue() == newHue)
62         return;
63     m_color.setHsv(newHue, m_color.hsvSaturation(), m_color.value());
64     update();
65     emit hueChanged(m_color.hsvHue());
66 }
67
68 void HueControl::paintEvent(QPaintEvent *event)
69 {
70     QWidget::paintEvent(event);
71
72     QPainter p(this);
73
74     int localHeight = 120;
75
76     if (m_cache.isNull()) {
77         m_cache = QPixmap(10, localHeight);
78
79         QPainter cacheP(&m_cache);
80
81         for (int i = 0; i < localHeight; i++)
82         {
83             QColor c;
84             c.setHsv( (i*359) / 120.0, 255,255);
85             cacheP.fillRect(0, i, 10, i + 1, c);
86         }
87     }
88
89     p.drawPixmap(0, 5, m_cache);
90
91     QVector<QPointF> points;
92
93     int y = m_color.hueF() * 120 + 5;
94
95     points.append(QPointF(5, y));
96     points.append(QPointF(15, y + 5));
97     points.append(QPointF(15, y - 5));
98
99
100     p.setRenderHint(QPainter::Antialiasing, true);
101     p.translate(0.5, 1.5);
102     p.setPen(QColor(0, 0, 0, 120));
103     p.drawPolygon(points);
104     p.translate(0, -1);
105     p.setPen(0x222222);
106     p.setBrush(QColor(0x707070));
107     p.drawPolygon(points);
108 }
109
110 void HueControl::mousePressEvent(QMouseEvent *e)
111 {
112     // The current cell marker is set to the cell the mouse is pressed in
113     QPoint pos = e->pos();
114     m_mousePressed = true;
115     setCurrent(pos.y() - 5);
116 }
117
118 void HueControl::mouseReleaseEvent(QMouseEvent * /* event */)
119 {
120     m_mousePressed = false;
121 }
122
123 void HueControl::mouseMoveEvent(QMouseEvent *e)
124 {
125     if (!m_mousePressed)
126         return;
127     QPoint pos = e->pos();
128     setCurrent(pos.y() - 5);
129 }
130
131 } //QmlEditorWidgets