30 #include <QtCore/QMimeData>
31 #include <QApplication>
32 #include <QColorDialog>
33 #include <QDragEnterEvent>
45 #ifndef QT_NO_DRAGANDDROP
50 bool m_backgroundCheckered;
54 QColor shownColor()
const;
55 QPixmap generatePixmap()
const;
58 void QtColorButtonPrivate::slotEditColor()
63 const QRgb rgba = QColorDialog::getRgba(m_color.rgba(), &ok, q_ptr);
66 newColor = QColor::fromRgba(rgba);
68 newColor = QColorDialog::getColor(m_color, q_ptr);
69 if (!newColor.isValid())
72 if (newColor == q_ptr->
color())
78 QColor QtColorButtonPrivate::shownColor()
const
80 #ifndef QT_NO_DRAGANDDROP
87 QPixmap QtColorButtonPrivate::generatePixmap()
const
92 QBrush br(shownColor());
94 QPixmap pm(2 * pixSize, 2 * pixSize);
96 pmp.fillRect(0, 0, pixSize, pixSize, Qt::lightGray);
97 pmp.fillRect(pixSize, pixSize, pixSize, pixSize, Qt::lightGray);
98 pmp.fillRect(0, pixSize, pixSize, pixSize, Qt::darkGray);
99 pmp.fillRect(pixSize, 0, pixSize, pixSize, Qt::darkGray);
100 pmp.fillRect(0, 0, 2 * pixSize, 2 * pixSize, shownColor());
105 QRect r = pix.rect().adjusted(corr, corr, -corr, -corr);
106 p.setBrushOrigin((r.width() % pixSize + pixSize) / 2 + corr, (r.height() % pixSize + pixSize) / 2 + corr);
109 p.fillRect(r.width() / 4 + corr, r.height() / 4 + corr,
110 r.width() / 2, r.height() / 2,
111 QColor(shownColor().rgb()));
112 p.drawRect(pix.rect().adjusted(0, 0, -1, -1));
120 : QToolButton(parent)
124 d_ptr->m_dragging =
false;
125 d_ptr->m_backgroundCheckered =
true;
126 d_ptr->m_alphaAllowed =
true;
128 setAcceptDrops(
true);
130 connect(
this, SIGNAL(clicked()),
this, SLOT(slotEditColor()));
131 setSizePolicy(QSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred));
141 if (d_ptr->m_color == color)
143 d_ptr->m_color =
color;
149 return d_ptr->m_color;
154 if (d_ptr->m_backgroundCheckered == checkered)
156 d_ptr->m_backgroundCheckered = checkered;
162 return d_ptr->m_backgroundCheckered;
167 d_ptr->m_alphaAllowed = allowed;
172 return d_ptr->m_alphaAllowed;
177 QToolButton::paintEvent(event);
181 const int pixSize = 10;
182 QBrush br(d_ptr->shownColor());
183 if (d_ptr->m_backgroundCheckered) {
184 QPixmap pm(2 * pixSize, 2 * pixSize);
186 pmp.fillRect(0, 0, pixSize, pixSize, Qt::white);
187 pmp.fillRect(pixSize, pixSize, pixSize, pixSize, Qt::white);
188 pmp.fillRect(0, pixSize, pixSize, pixSize, Qt::black);
189 pmp.fillRect(pixSize, 0, pixSize, pixSize, Qt::black);
190 pmp.fillRect(0, 0, 2 * pixSize, 2 * pixSize, d_ptr->shownColor());
196 QRect r = rect().adjusted(corr, corr, -corr, -corr);
197 p.setBrushOrigin((r.width() % pixSize + pixSize) / 2 + corr, (r.height() % pixSize + pixSize) / 2 + corr);
216 const QColor frameColor1(0, 0, 0, 26);
217 p.setPen(frameColor1);
218 p.drawRect(r.adjusted(1, 1, -2, -2));
219 const QColor frameColor2(0, 0, 0, 51);
220 p.setPen(frameColor2);
221 p.drawRect(r.adjusted(0, 0, -1, -1));
226 #ifndef QT_NO_DRAGANDDROP
227 if (event->button() == Qt::LeftButton)
228 d_ptr->m_dragStart = event->pos();
230 QToolButton::mousePressEvent(event);
235 #ifndef QT_NO_DRAGANDDROP
236 if (event->buttons() & Qt::LeftButton &&
237 (d_ptr->m_dragStart -
event->pos()).manhattanLength() > QApplication::startDragDistance()) {
238 QMimeData *mime =
new QMimeData;
239 mime->setColorData(
color());
240 QDrag *drg =
new QDrag(
this);
241 drg->setMimeData(mime);
242 drg->setPixmap(d_ptr->generatePixmap());
249 QToolButton::mouseMoveEvent(event);
252 #ifndef QT_NO_DRAGANDDROP
255 const QMimeData *mime =
event->mimeData();
256 if (!mime->hasColor())
260 d_ptr->m_dragColor = qvariant_cast<QColor>(mime->colorData());
261 d_ptr->m_dragging =
true;
268 d_ptr->m_dragging =
false;
275 d_ptr->m_dragging =
false;
276 if (d_ptr->m_dragColor ==
color())
285 #include "moc_qtcolorbutton.cpp"