6#include <QDragEnterEvent>
16class ColorButtonPrivate {
18 Q_DECLARE_PUBLIC(ColorButton)
21#ifndef QT_NO_DRAGANDDROP
26 bool m_backgroundCheckered;
29 QColor shownColor()
const;
30 QPixmap generatePixmap()
const;
33void ColorButtonPrivate::slotEditColor()
35 const QColor newColor = QColorDialog::getColor(m_color, q_ptr, QString(), QColorDialog::ShowAlphaChannel);
36 if (!newColor.isValid() || newColor == q_ptr->color())
38 q_ptr->setColor(newColor);
41QColor ColorButtonPrivate::shownColor()
const
43#ifndef QT_NO_DRAGANDDROP
50QPixmap ColorButtonPrivate::generatePixmap()
const
55 QBrush br(shownColor());
57 QPixmap pm(2 * pixSize, 2 * pixSize);
59 pmp.fillRect(0, 0, pixSize, pixSize, Qt::lightGray);
60 pmp.fillRect(pixSize, pixSize, pixSize, pixSize, Qt::lightGray);
61 pmp.fillRect(0, pixSize, pixSize, pixSize, Qt::darkGray);
62 pmp.fillRect(pixSize, 0, pixSize, pixSize, Qt::darkGray);
63 pmp.fillRect(0, 0, 2 * pixSize, 2 * pixSize, shownColor());
68 QRect r = pix.rect().adjusted(corr, corr, -corr, -corr);
69 p.setBrushOrigin((r.width() % pixSize + pixSize) / 2 + corr, (r.height() % pixSize + pixSize) / 2 + corr);
72 p.fillRect(r.width() / 4 + corr, r.height() / 4 + corr, r.width() / 2, r.height() / 2, QColor(shownColor().rgb()));
73 p.drawRect(pix.rect().adjusted(0, 0, -1, -1));
84 , d_ptr(new ColorButtonPrivate)
87 d_ptr->m_dragging =
false;
88 d_ptr->m_backgroundCheckered =
true;
92 connect(
this, SIGNAL(clicked()),
this, SLOT(slotEditColor()));
93 setSizePolicy(QSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred));
102 if (d_ptr->m_color ==
color)
110 return d_ptr->m_color;
115 if (d_ptr->m_backgroundCheckered == checkered)
117 d_ptr->m_backgroundCheckered = checkered;
123 return d_ptr->m_backgroundCheckered;
128 QToolButton::paintEvent(event);
132 const int pixSize = 10;
133 QBrush br(d_ptr->shownColor());
134 if (d_ptr->m_backgroundCheckered) {
135 QPixmap pm(2 * pixSize, 2 * pixSize);
137 pmp.fillRect(0, 0, pixSize, pixSize, Qt::white);
138 pmp.fillRect(pixSize, pixSize, pixSize, pixSize, Qt::white);
139 pmp.fillRect(0, pixSize, pixSize, pixSize, Qt::black);
140 pmp.fillRect(pixSize, 0, pixSize, pixSize, Qt::black);
141 pmp.fillRect(0, 0, 2 * pixSize, 2 * pixSize, d_ptr->shownColor());
147 QRect r = rect().adjusted(corr, corr, -corr, -corr);
148 p.setBrushOrigin((r.width() % pixSize + pixSize) / 2 + corr, (r.height() % pixSize + pixSize) / 2 + corr);
151 const QColor frameColor1(0, 0, 0, 26);
152 p.setPen(frameColor1);
153 p.drawRect(r.adjusted(1, 1, -2, -2));
154 const QColor frameColor2(0, 0, 0, 51);
155 p.setPen(frameColor2);
156 p.drawRect(r.adjusted(0, 0, -1, -1));
161#ifndef QT_NO_DRAGANDDROP
162 if (event->button() == Qt::LeftButton)
163 d_ptr->m_dragStart =
event->pos();
165 QToolButton::mousePressEvent(event);
170#ifndef QT_NO_DRAGANDDROP
171 if (event->buttons() & Qt::LeftButton && (d_ptr->m_dragStart - event->pos()).manhattanLength() > QApplication::startDragDistance()) {
172 auto *
const mime =
new QMimeData;
173 mime->setColorData(
color());
174 auto *
const drg =
new QDrag(
this);
175 drg->setMimeData(mime);
176 drg->setPixmap(d_ptr->generatePixmap());
179 drg->exec(Qt::CopyAction);
183 QToolButton::mouseMoveEvent(event);
186#ifndef QT_NO_DRAGANDDROP
189 const QMimeData *mime =
event->mimeData();
190 if (!mime->hasColor())
194 d_ptr->m_dragColor = qvariant_cast<QColor>(mime->colorData());
195 d_ptr->m_dragging =
true;
202 d_ptr->m_dragging =
false;
209 d_ptr->m_dragging =
false;
210 if (d_ptr->m_dragColor ==
color())
217#include "moc_colorbutton.cpp"