OSDN Git Service

9d45d41b5c6e87e4da73065a7d48102886b861b1
[qt-creator-jp/qt-creator-jp.git] / src / libs / utils / qtcolorbutton.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 "qtcolorbutton.h"
35
36 #include <QtCore/QMimeData>
37 #include <QtGui/QApplication>
38 #include <QtGui/QColorDialog>
39 #include <QtGui/QDragEnterEvent>
40 #include <QtGui/QPainter>
41
42 namespace Utils {
43
44 class QtColorButtonPrivate
45 {
46     QtColorButton *q_ptr;
47     Q_DECLARE_PUBLIC(QtColorButton)
48 public:
49     QColor m_color;
50 #ifndef QT_NO_DRAGANDDROP
51     QColor m_dragColor;
52     QPoint m_dragStart;
53     bool m_dragging;
54 #endif
55     bool m_backgroundCheckered;
56     bool m_alphaAllowed;
57
58     void slotEditColor();
59     QColor shownColor() const;
60     QPixmap generatePixmap() const;
61 };
62
63 void QtColorButtonPrivate::slotEditColor()
64 {
65     QColor newColor;
66     if (m_alphaAllowed) {
67         bool ok;
68         const QRgb rgba = QColorDialog::getRgba(m_color.rgba(), &ok, q_ptr);
69         if (!ok)
70             return;
71         newColor = QColor::fromRgba(rgba);
72     } else {
73         newColor = QColorDialog::getColor(m_color, q_ptr);
74         if (!newColor.isValid())
75             return;
76     }
77     if (newColor == q_ptr->color())
78         return;
79     q_ptr->setColor(newColor);
80     emit q_ptr->colorChanged(m_color);
81 }
82
83 QColor QtColorButtonPrivate::shownColor() const
84 {
85 #ifndef QT_NO_DRAGANDDROP
86     if (m_dragging)
87         return m_dragColor;
88 #endif
89     return m_color;
90 }
91
92 QPixmap QtColorButtonPrivate::generatePixmap() const
93 {
94     QPixmap pix(24, 24);
95
96     int pixSize = 20;
97     QBrush br(shownColor());
98
99     QPixmap pm(2 * pixSize, 2 * pixSize);
100     QPainter pmp(&pm);
101     pmp.fillRect(0, 0, pixSize, pixSize, Qt::lightGray);
102     pmp.fillRect(pixSize, pixSize, pixSize, pixSize, Qt::lightGray);
103     pmp.fillRect(0, pixSize, pixSize, pixSize, Qt::darkGray);
104     pmp.fillRect(pixSize, 0, pixSize, pixSize, Qt::darkGray);
105     pmp.fillRect(0, 0, 2 * pixSize, 2 * pixSize, shownColor());
106     br = QBrush(pm);
107
108     QPainter p(&pix);
109     int corr = 1;
110     QRect r = pix.rect().adjusted(corr, corr, -corr, -corr);
111     p.setBrushOrigin((r.width() % pixSize + pixSize) / 2 + corr, (r.height() % pixSize + pixSize) / 2 + corr);
112     p.fillRect(r, br);
113
114     p.fillRect(r.width() / 4 + corr, r.height() / 4 + corr,
115                r.width() / 2, r.height() / 2,
116                QColor(shownColor().rgb()));
117     p.drawRect(pix.rect().adjusted(0, 0, -1, -1));
118
119     return pix;
120 }
121
122 ///////////////
123
124 QtColorButton::QtColorButton(QWidget *parent)
125     : QToolButton(parent)
126 {
127     d_ptr = new QtColorButtonPrivate;
128     d_ptr->q_ptr = this;
129     d_ptr->m_dragging = false;
130     d_ptr->m_backgroundCheckered = true;
131     d_ptr->m_alphaAllowed = true;
132
133     setAcceptDrops(true);
134
135     connect(this, SIGNAL(clicked()), this, SLOT(slotEditColor()));
136     setSizePolicy(QSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred));
137 }
138
139 QtColorButton::~QtColorButton()
140 {
141     delete d_ptr;
142 }
143
144 void QtColorButton::setColor(const QColor &color)
145 {
146     if (d_ptr->m_color == color)
147         return;
148     d_ptr->m_color = color;
149     update();
150 }
151
152 QColor QtColorButton::color() const
153 {
154     return d_ptr->m_color;
155 }
156
157 void QtColorButton::setBackgroundCheckered(bool checkered)
158 {
159     if (d_ptr->m_backgroundCheckered == checkered)
160         return;
161     d_ptr->m_backgroundCheckered = checkered;
162     update();
163 }
164
165 bool QtColorButton::isBackgroundCheckered() const
166 {
167     return d_ptr->m_backgroundCheckered;
168 }
169
170 void QtColorButton::setAlphaAllowed(bool allowed)
171 {
172     d_ptr->m_alphaAllowed = allowed;
173 }
174
175 bool QtColorButton::isAlphaAllowed() const
176 {
177     return d_ptr->m_alphaAllowed;
178 }
179
180 void QtColorButton::paintEvent(QPaintEvent *event)
181 {
182     QToolButton::paintEvent(event);
183     if (!isEnabled())
184         return;
185
186     const int pixSize = 10;
187     QBrush br(d_ptr->shownColor());
188     if (d_ptr->m_backgroundCheckered) {
189         QPixmap pm(2 * pixSize, 2 * pixSize);
190         QPainter pmp(&pm);
191         pmp.fillRect(0, 0, pixSize, pixSize, Qt::white);
192         pmp.fillRect(pixSize, pixSize, pixSize, pixSize, Qt::white);
193         pmp.fillRect(0, pixSize, pixSize, pixSize, Qt::black);
194         pmp.fillRect(pixSize, 0, pixSize, pixSize, Qt::black);
195         pmp.fillRect(0, 0, 2 * pixSize, 2 * pixSize, d_ptr->shownColor());
196         br = QBrush(pm);
197     }
198
199     QPainter p(this);
200     const int corr = 5;
201     QRect r = rect().adjusted(corr, corr, -corr, -corr);
202     p.setBrushOrigin((r.width() % pixSize + pixSize) / 2 + corr, (r.height() % pixSize + pixSize) / 2 + corr);
203     p.fillRect(r, br);
204
205     //const int adjX = qRound(r.width() / 4.0);
206     //const int adjY = qRound(r.height() / 4.0);
207     //p.fillRect(r.adjusted(adjX, adjY, -adjX, -adjY),
208     //           QColor(d_ptr->shownColor().rgb()));
209     /*
210     p.fillRect(r.adjusted(0, r.height() * 3 / 4, 0, 0),
211                QColor(d_ptr->shownColor().rgb()));
212     p.fillRect(r.adjusted(0, 0, 0, -r.height() * 3 / 4),
213                QColor(d_ptr->shownColor().rgb()));
214                */
215     /*
216     const QColor frameColor0(0, 0, 0, qRound(0.2 * (0xFF - d_ptr->shownColor().alpha())));
217     p.setPen(frameColor0);
218     p.drawRect(r.adjusted(adjX, adjY, -adjX - 1, -adjY - 1));
219     */
220
221     const QColor frameColor1(0, 0, 0, 26);
222     p.setPen(frameColor1);
223     p.drawRect(r.adjusted(1, 1, -2, -2));
224     const QColor frameColor2(0, 0, 0, 51);
225     p.setPen(frameColor2);
226     p.drawRect(r.adjusted(0, 0, -1, -1));
227 }
228
229 void QtColorButton::mousePressEvent(QMouseEvent *event)
230 {
231 #ifndef QT_NO_DRAGANDDROP
232     if (event->button() == Qt::LeftButton)
233         d_ptr->m_dragStart = event->pos();
234 #endif
235     QToolButton::mousePressEvent(event);
236 }
237
238 void QtColorButton::mouseMoveEvent(QMouseEvent *event)
239 {
240 #ifndef QT_NO_DRAGANDDROP
241     if (event->buttons() & Qt::LeftButton &&
242             (d_ptr->m_dragStart - event->pos()).manhattanLength() > QApplication::startDragDistance()) {
243         QMimeData *mime = new QMimeData;
244         mime->setColorData(color());
245         QDrag *drg = new QDrag(this);
246         drg->setMimeData(mime);
247         drg->setPixmap(d_ptr->generatePixmap());
248         setDown(false);
249         event->accept();
250         drg->start();
251         return;
252     }
253 #endif
254     QToolButton::mouseMoveEvent(event);
255 }
256
257 #ifndef QT_NO_DRAGANDDROP
258 void QtColorButton::dragEnterEvent(QDragEnterEvent *event)
259 {
260     const QMimeData *mime = event->mimeData();
261     if (!mime->hasColor())
262         return;
263
264     event->accept();
265     d_ptr->m_dragColor = qvariant_cast<QColor>(mime->colorData());
266     d_ptr->m_dragging = true;
267     update();
268 }
269
270 void QtColorButton::dragLeaveEvent(QDragLeaveEvent *event)
271 {
272     event->accept();
273     d_ptr->m_dragging = false;
274     update();
275 }
276
277 void QtColorButton::dropEvent(QDropEvent *event)
278 {
279     event->accept();
280     d_ptr->m_dragging = false;
281     if (d_ptr->m_dragColor == color())
282         return;
283     setColor(d_ptr->m_dragColor);
284     emit colorChanged(color());
285 }
286 #endif
287
288 } // namespace Utils
289
290 #include "moc_qtcolorbutton.cpp"