1 /**************************************************************************
3 ** This file is part of Qt Creator
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
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
16 ** 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 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.
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.
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
32 **************************************************************************/
34 #include "qtcolorbutton.h"
36 #include <QtCore/QMimeData>
37 #include <QtGui/QApplication>
38 #include <QtGui/QColorDialog>
39 #include <QtGui/QDragEnterEvent>
40 #include <QtGui/QPainter>
44 class QtColorButtonPrivate
47 Q_DECLARE_PUBLIC(QtColorButton)
50 #ifndef QT_NO_DRAGANDDROP
55 bool m_backgroundCheckered;
59 QColor shownColor() const;
60 QPixmap generatePixmap() const;
63 void QtColorButtonPrivate::slotEditColor()
68 const QRgb rgba = QColorDialog::getRgba(m_color.rgba(), &ok, q_ptr);
71 newColor = QColor::fromRgba(rgba);
73 newColor = QColorDialog::getColor(m_color, q_ptr);
74 if (!newColor.isValid())
77 if (newColor == q_ptr->color())
79 q_ptr->setColor(newColor);
80 emit q_ptr->colorChanged(m_color);
83 QColor QtColorButtonPrivate::shownColor() const
85 #ifndef QT_NO_DRAGANDDROP
92 QPixmap QtColorButtonPrivate::generatePixmap() const
97 QBrush br(shownColor());
99 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)
148 d_ptr->m_color = color;
152 QColor QtColorButton::color() const
154 return d_ptr->m_color;
157 void QtColorButton::setBackgroundCheckered(bool checkered)
159 if (d_ptr->m_backgroundCheckered == checkered)
161 d_ptr->m_backgroundCheckered = checkered;
165 bool QtColorButton::isBackgroundCheckered() const
167 return d_ptr->m_backgroundCheckered;
170 void QtColorButton::setAlphaAllowed(bool allowed)
172 d_ptr->m_alphaAllowed = allowed;
175 bool QtColorButton::isAlphaAllowed() const
177 return d_ptr->m_alphaAllowed;
180 void QtColorButton::paintEvent(QPaintEvent *event)
182 QToolButton::paintEvent(event);
186 const int pixSize = 10;
187 QBrush br(d_ptr->shownColor());
188 if (d_ptr->m_backgroundCheckered) {
189 QPixmap pm(2 * pixSize, 2 * pixSize);
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());
201 QRect r = rect().adjusted(corr, corr, -corr, -corr);
202 p.setBrushOrigin((r.width() % pixSize + pixSize) / 2 + corr, (r.height() % pixSize + pixSize) / 2 + corr);
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()));
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()));
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));
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));
229 void QtColorButton::mousePressEvent(QMouseEvent *event)
231 #ifndef QT_NO_DRAGANDDROP
232 if (event->button() == Qt::LeftButton)
233 d_ptr->m_dragStart = event->pos();
235 QToolButton::mousePressEvent(event);
238 void QtColorButton::mouseMoveEvent(QMouseEvent *event)
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());
254 QToolButton::mouseMoveEvent(event);
257 #ifndef QT_NO_DRAGANDDROP
258 void QtColorButton::dragEnterEvent(QDragEnterEvent *event)
260 const QMimeData *mime = event->mimeData();
261 if (!mime->hasColor())
265 d_ptr->m_dragColor = qvariant_cast<QColor>(mime->colorData());
266 d_ptr->m_dragging = true;
270 void QtColorButton::dragLeaveEvent(QDragLeaveEvent *event)
273 d_ptr->m_dragging = false;
277 void QtColorButton::dropEvent(QDropEvent *event)
280 d_ptr->m_dragging = false;
281 if (d_ptr->m_dragColor == color())
283 setColor(d_ptr->m_dragColor);
284 emit colorChanged(color());
290 #include "moc_qtcolorbutton.cpp"