1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the tools applications of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** 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 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#include "qtcolorbutton.h"
41#include <QtWidgets/QColorDialog>
42#include <QtGui/QPainter>
43#include <QtCore/QMimeData>
44#include <QtGui/QDragEnterEvent>
45#include <QtGui/QDrag>
46#include <QtWidgets/QApplication>
47
48QT_BEGIN_NAMESPACE
49
50class QtColorButtonPrivate
51{
52 QtColorButton *q_ptr;
53 Q_DECLARE_PUBLIC(QtColorButton)
54public:
55 QColor m_color;
56#ifndef QT_NO_DRAGANDDROP
57 QColor m_dragColor;
58 QPoint m_dragStart;
59 bool m_dragging;
60#endif
61 bool m_backgroundCheckered;
62
63 void slotEditColor();
64 QColor shownColor() const;
65 QPixmap generatePixmap() const;
66};
67
68void QtColorButtonPrivate::slotEditColor()
69{
70 const QColor newColor = QColorDialog::getColor(m_color, q_ptr, QString(), QColorDialog::ShowAlphaChannel);
71 if (!newColor.isValid() || newColor == q_ptr->color())
72 return;
73 q_ptr->setColor(newColor);
74 emit q_ptr->colorChanged(m_color);
75}
76
77QColor QtColorButtonPrivate::shownColor() const
78{
79#ifndef QT_NO_DRAGANDDROP
80 if (m_dragging)
81 return m_dragColor;
82#endif
83 return m_color;
84}
85
86QPixmap QtColorButtonPrivate::generatePixmap() const
87{
88 QPixmap pix(24, 24);
89
90 int pixSize = 20;
91 QBrush br(shownColor());
92
93 QPixmap pm(2 * pixSize, 2 * pixSize);
94 QPainter pmp(&pm);
95 pmp.fillRect(0, 0, pixSize, pixSize, Qt::lightGray);
96 pmp.fillRect(pixSize, pixSize, pixSize, pixSize, Qt::lightGray);
97 pmp.fillRect(0, pixSize, pixSize, pixSize, Qt::darkGray);
98 pmp.fillRect(pixSize, 0, pixSize, pixSize, Qt::darkGray);
99 pmp.fillRect(0, 0, 2 * pixSize, 2 * pixSize, shownColor());
100 br = QBrush(pm);
101
102 QPainter p(&pix);
103 int corr = 1;
104 QRect r = pix.rect().adjusted(corr, corr, -corr, -corr);
105 p.setBrushOrigin((r.width() % pixSize + pixSize) / 2 + corr, (r.height() % pixSize + pixSize) / 2 + corr);
106 p.fillRect(r, br);
107
108 p.fillRect(r.width() / 4 + corr, r.height() / 4 + corr,
109 r.width() / 2, r.height() / 2,
110 QColor(shownColor().rgb()));
111 p.drawRect(pix.rect().adjusted(0, 0, -1, -1));
112
113 return pix;
114}
115
116///////////////
117
118QtColorButton::QtColorButton(QWidget *parent)
119 : QToolButton(parent), d_ptr(new QtColorButtonPrivate)
120{
121 d_ptr->q_ptr = this;
122 d_ptr->m_dragging = false;
123 d_ptr->m_backgroundCheckered = true;
124
125 setAcceptDrops(true);
126
127 connect(this, SIGNAL(clicked()), this, SLOT(slotEditColor()));
128 setSizePolicy(QSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred));
129}
130
131QtColorButton::~QtColorButton()
132{
133}
134
135void QtColorButton::setColor(const QColor &color)
136{
137 if (d_ptr->m_color == color)
138 return;
139 d_ptr->m_color = color;
140 update();
141}
142
143QColor QtColorButton::color() const
144{
145 return d_ptr->m_color;
146}
147
148void QtColorButton::setBackgroundCheckered(bool checkered)
149{
150 if (d_ptr->m_backgroundCheckered == checkered)
151 return;
152 d_ptr->m_backgroundCheckered = checkered;
153 update();
154}
155
156bool QtColorButton::isBackgroundCheckered() const
157{
158 return d_ptr->m_backgroundCheckered;
159}
160
161void QtColorButton::paintEvent(QPaintEvent *event)
162{
163 QToolButton::paintEvent(event);
164 if (!isEnabled())
165 return;
166
167 const int pixSize = 10;
168 QBrush br(d_ptr->shownColor());
169 if (d_ptr->m_backgroundCheckered) {
170 QPixmap pm(2 * pixSize, 2 * pixSize);
171 QPainter pmp(&pm);
172 pmp.fillRect(0, 0, pixSize, pixSize, Qt::white);
173 pmp.fillRect(pixSize, pixSize, pixSize, pixSize, Qt::white);
174 pmp.fillRect(0, pixSize, pixSize, pixSize, Qt::black);
175 pmp.fillRect(pixSize, 0, pixSize, pixSize, Qt::black);
176 pmp.fillRect(0, 0, 2 * pixSize, 2 * pixSize, d_ptr->shownColor());
177 br = QBrush(pm);
178 }
179
180 QPainter p(this);
181 const int corr = 4;
182 QRect r = rect().adjusted(corr, corr, -corr, -corr);
183 p.setBrushOrigin((r.width() % pixSize + pixSize) / 2 + corr, (r.height() % pixSize + pixSize) / 2 + corr);
184 p.fillRect(r, br);
185
186 //const int adjX = qRound(r.width() / 4.0);
187 //const int adjY = qRound(r.height() / 4.0);
188 //p.fillRect(r.adjusted(adjX, adjY, -adjX, -adjY),
189 // QColor(d_ptr->shownColor().rgb()));
190 /*
191 p.fillRect(r.adjusted(0, r.height() * 3 / 4, 0, 0),
192 QColor(d_ptr->shownColor().rgb()));
193 p.fillRect(r.adjusted(0, 0, 0, -r.height() * 3 / 4),
194 QColor(d_ptr->shownColor().rgb()));
195 */
196 /*
197 const QColor frameColor0(0, 0, 0, qRound(0.2 * (0xFF - d_ptr->shownColor().alpha())));
198 p.setPen(frameColor0);
199 p.drawRect(r.adjusted(adjX, adjY, -adjX - 1, -adjY - 1));
200 */
201
202 const QColor frameColor1(0, 0, 0, 26);
203 p.setPen(frameColor1);
204 p.drawRect(r.adjusted(1, 1, -2, -2));
205 const QColor frameColor2(0, 0, 0, 51);
206 p.setPen(frameColor2);
207 p.drawRect(r.adjusted(0, 0, -1, -1));
208}
209
210void QtColorButton::mousePressEvent(QMouseEvent *event)
211{
212#ifndef QT_NO_DRAGANDDROP
213 if (event->button() == Qt::LeftButton)
214 d_ptr->m_dragStart = event->pos();
215#endif
216 QToolButton::mousePressEvent(event);
217}
218
219void QtColorButton::mouseMoveEvent(QMouseEvent *event)
220{
221#ifndef QT_NO_DRAGANDDROP
222 if (event->buttons() & Qt::LeftButton &&
223 (d_ptr->m_dragStart - event->pos()).manhattanLength() > QApplication::startDragDistance()) {
224 QMimeData *mime = new QMimeData;
225 mime->setColorData(color());
226 QDrag *drg = new QDrag(this);
227 drg->setMimeData(mime);
228 drg->setPixmap(d_ptr->generatePixmap());
229 setDown(false);
230 event->accept();
231 drg->exec(Qt::CopyAction);
232 return;
233 }
234#endif
235 QToolButton::mouseMoveEvent(event);
236}
237
238#ifndef QT_NO_DRAGANDDROP
239void QtColorButton::dragEnterEvent(QDragEnterEvent *event)
240{
241 const QMimeData *mime = event->mimeData();
242 if (!mime->hasColor())
243 return;
244
245 event->accept();
246 d_ptr->m_dragColor = qvariant_cast<QColor>(mime->colorData());
247 d_ptr->m_dragging = true;
248 update();
249}
250
251void QtColorButton::dragLeaveEvent(QDragLeaveEvent *event)
252{
253 event->accept();
254 d_ptr->m_dragging = false;
255 update();
256}
257
258void QtColorButton::dropEvent(QDropEvent *event)
259{
260 event->accept();
261 d_ptr->m_dragging = false;
262 if (d_ptr->m_dragColor == color())
263 return;
264 setColor(d_ptr->m_dragColor);
265 emit colorChanged(color());
266}
267#endif
268
269QT_END_NAMESPACE
270
271#include "moc_qtcolorbutton.cpp"
272