1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
|
/*
Copied from Qt 5.6.3 sources, module qttools, directory src/shared/qtgradienteditor
with small modifications to match more modern C++ standards and for no alpha-channel
This file is part of the tools applications of the Qt Toolkit.
SPDX-FileCopyrightText: 2015 The Qt Company Ltd. <http://www.qt.io/licensing/>
SPDX-License-Identifier: LGPL-2.1-only WITH Qt-LGPL-exception-1.1 OR LGPL-3.0-only WITH Qt-LGPL-exception-1.1 OR LicenseRef-Qt-Commercial
*/
#include "qtcolorbutton.h"
#include <QColorDialog>
#include <QPainter>
#include <QMimeData>
#include <QDragEnterEvent>
#include <QDrag>
#include <QApplication>
QT_BEGIN_NAMESPACE
class QtColorButtonPrivate
{
QtColorButton *q_ptr;
Q_DECLARE_PUBLIC(QtColorButton)
public:
QColor m_color;
#ifndef QT_NO_DRAGANDDROP
QColor m_dragColor;
QPoint m_dragStart;
bool m_dragging;
#endif
bool m_backgroundCheckered;
void slotEditColor();
QColor shownColor() const;
QPixmap generatePixmap() const;
};
void QtColorButtonPrivate::slotEditColor()
{
const QColor newColor = QColorDialog::getColor(m_color, q_ptr);
if (!newColor.isValid() || newColor == q_ptr->color())
return;
q_ptr->setColor(newColor);
emit q_ptr->colorChanged(m_color);
}
QColor QtColorButtonPrivate::shownColor() const
{
#ifndef QT_NO_DRAGANDDROP
if (m_dragging)
return m_dragColor;
#endif
return m_color;
}
QPixmap QtColorButtonPrivate::generatePixmap() const
{
QPixmap pix(24, 24);
int pixSize = 20;
QBrush br(shownColor());
QPixmap pm(2 * pixSize, 2 * pixSize);
QPainter pmp(&pm);
pmp.fillRect(0, 0, pixSize, pixSize, Qt::lightGray);
pmp.fillRect(pixSize, pixSize, pixSize, pixSize, Qt::lightGray);
pmp.fillRect(0, pixSize, pixSize, pixSize, Qt::darkGray);
pmp.fillRect(pixSize, 0, pixSize, pixSize, Qt::darkGray);
pmp.fillRect(0, 0, 2 * pixSize, 2 * pixSize, shownColor());
br = QBrush(pm);
QPainter p(&pix);
int corr = 1;
QRect r = pix.rect().adjusted(corr, corr, -corr, -corr);
p.setBrushOrigin((r.width() % pixSize + pixSize) / 2 + corr, (r.height() % pixSize + pixSize) / 2 + corr);
p.fillRect(r, br);
p.fillRect(r.width() / 4 + corr, r.height() / 4 + corr,
r.width() / 2, r.height() / 2,
QColor(shownColor().rgb()));
p.drawRect(pix.rect().adjusted(0, 0, -1, -1));
return pix;
}
///////////////
QtColorButton::QtColorButton(QWidget *parent)
: QToolButton(parent), d_ptr(new QtColorButtonPrivate)
{
d_ptr->q_ptr = this;
d_ptr->m_dragging = false;
d_ptr->m_backgroundCheckered = true;
setAcceptDrops(true);
connect(this, SIGNAL(clicked()), this, SLOT(slotEditColor()));
setSizePolicy(QSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred));
}
QtColorButton::~QtColorButton()
{
}
void QtColorButton::setColor(const QColor &color)
{
if (d_ptr->m_color == color)
return;
d_ptr->m_color = color;
update();
}
QColor QtColorButton::color() const
{
return d_ptr->m_color;
}
void QtColorButton::setBackgroundCheckered(bool checkered)
{
if (d_ptr->m_backgroundCheckered == checkered)
return;
d_ptr->m_backgroundCheckered = checkered;
update();
}
bool QtColorButton::isBackgroundCheckered() const
{
return d_ptr->m_backgroundCheckered;
}
void QtColorButton::paintEvent(QPaintEvent *event)
{
QToolButton::paintEvent(event);
if (!isEnabled())
return;
const int pixSize = 10;
QBrush br(d_ptr->shownColor());
if (d_ptr->m_backgroundCheckered) {
QPixmap pm(2 * pixSize, 2 * pixSize);
QPainter pmp(&pm);
pmp.fillRect(0, 0, pixSize, pixSize, Qt::white);
pmp.fillRect(pixSize, pixSize, pixSize, pixSize, Qt::white);
pmp.fillRect(0, pixSize, pixSize, pixSize, Qt::black);
pmp.fillRect(pixSize, 0, pixSize, pixSize, Qt::black);
pmp.fillRect(0, 0, 2 * pixSize, 2 * pixSize, d_ptr->shownColor());
br = QBrush(pm);
}
QPainter p(this);
const int corr = 4;
QRect r = rect().adjusted(corr, corr, -corr, -corr);
p.setBrushOrigin((r.width() % pixSize + pixSize) / 2 + corr, (r.height() % pixSize + pixSize) / 2 + corr);
p.fillRect(r, br);
//const int adjX = qRound(r.width() / 4.0);
//const int adjY = qRound(r.height() / 4.0);
//p.fillRect(r.adjusted(adjX, adjY, -adjX, -adjY),
// QColor(d_ptr->shownColor().rgb()));
/*
p.fillRect(r.adjusted(0, r.height() * 3 / 4, 0, 0),
QColor(d_ptr->shownColor().rgb()));
p.fillRect(r.adjusted(0, 0, 0, -r.height() * 3 / 4),
QColor(d_ptr->shownColor().rgb()));
*/
/*
const QColor frameColor0(0, 0, 0, qRound(0.2 * (0xFF - d_ptr->shownColor().alpha())));
p.setPen(frameColor0);
p.drawRect(r.adjusted(adjX, adjY, -adjX - 1, -adjY - 1));
*/
const QColor frameColor1(0, 0, 0, 26);
p.setPen(frameColor1);
p.drawRect(r.adjusted(1, 1, -2, -2));
const QColor frameColor2(0, 0, 0, 51);
p.setPen(frameColor2);
p.drawRect(r.adjusted(0, 0, -1, -1));
}
void QtColorButton::mousePressEvent(QMouseEvent *event)
{
#ifndef QT_NO_DRAGANDDROP
if (event->button() == Qt::LeftButton)
d_ptr->m_dragStart = event->pos();
#endif
QToolButton::mousePressEvent(event);
}
void QtColorButton::mouseMoveEvent(QMouseEvent *event)
{
#ifndef QT_NO_DRAGANDDROP
if (event->buttons() & Qt::LeftButton &&
(d_ptr->m_dragStart - event->pos()).manhattanLength() > QApplication::startDragDistance()) {
QMimeData *mime = new QMimeData;
mime->setColorData(color());
QDrag *drg = new QDrag(this);
drg->setMimeData(mime);
drg->setPixmap(d_ptr->generatePixmap());
setDown(false);
event->accept();
drg->exec();
return;
}
#endif
QToolButton::mouseMoveEvent(event);
}
#ifndef QT_NO_DRAGANDDROP
void QtColorButton::dragEnterEvent(QDragEnterEvent *event)
{
const QMimeData *mime = event->mimeData();
if (!mime->hasColor())
return;
event->accept();
d_ptr->m_dragColor = qvariant_cast<QColor>(mime->colorData());
d_ptr->m_dragging = true;
update();
}
void QtColorButton::dragLeaveEvent(QDragLeaveEvent *event)
{
event->accept();
d_ptr->m_dragging = false;
update();
}
void QtColorButton::dropEvent(QDropEvent *event)
{
event->accept();
d_ptr->m_dragging = false;
if (d_ptr->m_dragColor == color())
return;
setColor(d_ptr->m_dragColor);
emit colorChanged(color());
}
#endif
QT_END_NAMESPACE
#include "moc_qtcolorbutton.cpp"
|