2 ******************************************************************************
4 * @file qtcolorbutton.cpp
5 * @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
6 * Parts by Nokia Corporation (qt-info@nokia.com) Copyright (C) 2009.
8 * @see The GNU Public License (GPL) Version 3
12 *****************************************************************************/
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 3 of the License, or
17 * (at your option) any later version.
19 * This program is distributed in the hope that it will be useful, but
20 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
21 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
24 * You should have received a copy of the GNU General Public License along
25 * with this program; if not, write to the Free Software Foundation, Inc.,
26 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 #include "qtcolorbutton.h"
31 #include <QtCore/QMimeData>
32 #include <QtWidgets/QApplication>
33 #include <QColorDialog>
34 #include <QDragEnterEvent>
39 class QtColorButtonPrivate
{
41 Q_DECLARE_PUBLIC(QtColorButton
)
44 #ifndef QT_NO_DRAGANDDROP
49 bool m_backgroundCheckered
;
53 QColor
shownColor() const;
54 QPixmap
generatePixmap() const;
57 void QtColorButtonPrivate::slotEditColor()
63 const QRgb rgba
= QColorDialog::getRgba(m_color
.rgba(), &ok
, q_ptr
);
67 newColor
= QColor::fromRgba(rgba
);
69 newColor
= QColorDialog::getColor(m_color
, q_ptr
);
70 if (!newColor
.isValid()) {
74 if (newColor
== q_ptr
->color()) {
77 q_ptr
->setColor(newColor
);
78 emit q_ptr
->colorChanged(m_color
);
81 QColor
QtColorButtonPrivate::shownColor() const
83 #ifndef QT_NO_DRAGANDDROP
91 QPixmap
QtColorButtonPrivate::generatePixmap() const
96 QBrush
br(shownColor());
98 QPixmap
pm(2 * pixSize
, 2 * pixSize
);
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());
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
);
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));
124 QtColorButton::QtColorButton(QWidget
*parent
)
125 : QToolButton(parent
)
127 d_ptr
= new QtColorButtonPrivate
;
129 d_ptr
->m_dragging
= false;
130 d_ptr
->m_backgroundCheckered
= true;
131 d_ptr
->m_alphaAllowed
= true;
133 setAcceptDrops(true);
135 connect(this, SIGNAL(clicked()), this, SLOT(slotEditColor()));
136 setSizePolicy(QSizePolicy(QSizePolicy::Preferred
, QSizePolicy::Preferred
));
139 QtColorButton::~QtColorButton()
144 void QtColorButton::setColor(const QColor
&color
)
146 if (d_ptr
->m_color
== color
) {
149 d_ptr
->m_color
= color
;
153 QColor
QtColorButton::color() const
155 return d_ptr
->m_color
;
158 void QtColorButton::setBackgroundCheckered(bool checkered
)
160 if (d_ptr
->m_backgroundCheckered
== checkered
) {
163 d_ptr
->m_backgroundCheckered
= checkered
;
167 bool QtColorButton::isBackgroundCheckered() const
169 return d_ptr
->m_backgroundCheckered
;
172 void QtColorButton::setAlphaAllowed(bool allowed
)
174 d_ptr
->m_alphaAllowed
= allowed
;
177 bool QtColorButton::isAlphaAllowed() const
179 return d_ptr
->m_alphaAllowed
;
182 void QtColorButton::paintEvent(QPaintEvent
*event
)
184 QToolButton::paintEvent(event
);
190 const int pixSize
= 10;
191 QBrush
br(d_ptr
->shownColor());
192 if (d_ptr
->m_backgroundCheckered
) {
193 QPixmap
pm(2 * pixSize
, 2 * pixSize
);
195 pmp
.fillRect(0, 0, pixSize
, pixSize
, Qt::white
);
196 pmp
.fillRect(pixSize
, pixSize
, pixSize
, pixSize
, Qt::white
);
197 pmp
.fillRect(0, pixSize
, pixSize
, pixSize
, Qt::black
);
198 pmp
.fillRect(pixSize
, 0, pixSize
, pixSize
, Qt::black
);
199 pmp
.fillRect(0, 0, 2 * pixSize
, 2 * pixSize
, d_ptr
->shownColor());
205 QRect r
= rect().adjusted(corr
, corr
, -corr
, -corr
);
206 p
.setBrushOrigin((r
.width() % pixSize
+ pixSize
) / 2 + corr
, (r
.height() % pixSize
+ pixSize
) / 2 + corr
);
209 // const int adjX = qRound(r.width() / 4.0);
210 // const int adjY = qRound(r.height() / 4.0);
211 // p.fillRect(r.adjusted(adjX, adjY, -adjX, -adjY),
212 // QColor(d_ptr->shownColor().rgb()));
214 p.fillRect(r.adjusted(0, r.height() * 3 / 4, 0, 0),
215 QColor(d_ptr->shownColor().rgb()));
216 p.fillRect(r.adjusted(0, 0, 0, -r.height() * 3 / 4),
217 QColor(d_ptr->shownColor().rgb()));
220 const QColor frameColor0(0, 0, 0, qRound(0.2 * (0xFF - d_ptr->shownColor().alpha())));
221 p.setPen(frameColor0);
222 p.drawRect(r.adjusted(adjX, adjY, -adjX - 1, -adjY - 1));
225 const QColor
frameColor1(0, 0, 0, 26);
226 p
.setPen(frameColor1
);
227 p
.drawRect(r
.adjusted(1, 1, -2, -2));
228 const QColor
frameColor2(0, 0, 0, 51);
229 p
.setPen(frameColor2
);
230 p
.drawRect(r
.adjusted(0, 0, -1, -1));
233 void QtColorButton::mousePressEvent(QMouseEvent
*event
)
235 #ifndef QT_NO_DRAGANDDROP
236 if (event
->button() == Qt::LeftButton
) {
237 d_ptr
->m_dragStart
= event
->pos();
240 QToolButton::mousePressEvent(event
);
243 void QtColorButton::mouseMoveEvent(QMouseEvent
*event
)
245 #ifndef QT_NO_DRAGANDDROP
246 if (event
->buttons() & Qt::LeftButton
&&
247 (d_ptr
->m_dragStart
- event
->pos()).manhattanLength() > QApplication::startDragDistance()) {
248 QMimeData
*mime
= new QMimeData
;
249 mime
->setColorData(color());
250 QDrag
*drg
= new QDrag(this);
251 drg
->setMimeData(mime
);
252 drg
->setPixmap(d_ptr
->generatePixmap());
259 QToolButton::mouseMoveEvent(event
);
262 #ifndef QT_NO_DRAGANDDROP
263 void QtColorButton::dragEnterEvent(QDragEnterEvent
*event
)
265 const QMimeData
*mime
= event
->mimeData();
267 if (!mime
->hasColor()) {
272 d_ptr
->m_dragColor
= qvariant_cast
<QColor
>(mime
->colorData());
273 d_ptr
->m_dragging
= true;
277 void QtColorButton::dragLeaveEvent(QDragLeaveEvent
*event
)
280 d_ptr
->m_dragging
= false;
284 void QtColorButton::dropEvent(QDropEvent
*event
)
287 d_ptr
->m_dragging
= false;
288 if (d_ptr
->m_dragColor
== color()) {
291 setColor(d_ptr
->m_dragColor
);
292 emit
colorChanged(color());
294 #endif // ifndef QT_NO_DRAGANDDROP
297 #include "moc_qtcolorbutton.cpp"