| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2016 The Qt Company Ltd. |
| 4 | ** Contact: https://www.qt.io/licensing/ |
| 5 | ** |
| 6 | ** This file is part of the tools applications of the Qt Toolkit. |
| 7 | ** |
| 8 | ** $QT_BEGIN_LICENSE:LGPL$ |
| 9 | ** Commercial License Usage |
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in |
| 11 | ** accordance with the commercial license agreement provided with the |
| 12 | ** Software or, alternatively, in accordance with the terms contained in |
| 13 | ** a written agreement between you and The Qt Company. For licensing terms |
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
| 15 | ** information use the contact form at https://www.qt.io/contact-us. |
| 16 | ** |
| 17 | ** 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 3 as published by the Free Software |
| 20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
| 21 | ** packaging of this file. Please review the following information to |
| 22 | ** ensure the GNU Lesser General Public License version 3 requirements |
| 23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
| 24 | ** |
| 25 | ** GNU General Public License Usage |
| 26 | ** Alternatively, this file may be used under the terms of the GNU |
| 27 | ** General Public License version 2.0 or (at your option) the GNU General |
| 28 | ** Public license version 3 or any later version approved by the KDE Free |
| 29 | ** Qt Foundation. The licenses are as published by the Free Software |
| 30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
| 31 | ** included in the packaging of this file. Please review the following |
| 32 | ** information to ensure the GNU General Public License requirements will |
| 33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
| 34 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
| 35 | ** |
| 36 | ** $QT_END_LICENSE$ |
| 37 | ** |
| 38 | ****************************************************************************/ |
| 39 | |
| 40 | #include "qtcolorbutton.h" |
| 41 | #include <QtWidgets/QColorDialog> |
| 42 | #include <QtGui/QPainter> |
| 43 | #include <QtCore/QMimeData> |
| 44 | #include <QtGui/QDragEnterEvent> |
| 45 | #include <QtGui/QDrag> |
| 46 | #include <QtWidgets/QApplication> |
| 47 | |
| 48 | QT_BEGIN_NAMESPACE |
| 49 | |
| 50 | class QtColorButtonPrivate |
| 51 | { |
| 52 | QtColorButton *q_ptr; |
| 53 | Q_DECLARE_PUBLIC(QtColorButton) |
| 54 | public: |
| 55 | QColor m_color; |
| 56 | #ifndef QT_NO_DRAGANDDROP |
| 57 | QColor m_dragColor; |
| 58 | QPoint m_dragStart; |
| 59 | bool m_dragging; |
| 60 | #endif |
| 61 | bool m_backgroundCheckered; |
| 62 | |
| 63 | void slotEditColor(); |
| 64 | QColor shownColor() const; |
| 65 | QPixmap generatePixmap() const; |
| 66 | }; |
| 67 | |
| 68 | void QtColorButtonPrivate::slotEditColor() |
| 69 | { |
| 70 | const QColor newColor = QColorDialog::getColor(initial: m_color, parent: q_ptr, title: QString(), options: QColorDialog::ShowAlphaChannel); |
| 71 | if (!newColor.isValid() || newColor == q_ptr->color()) |
| 72 | return; |
| 73 | q_ptr->setColor(newColor); |
| 74 | emit q_ptr->colorChanged(color: m_color); |
| 75 | } |
| 76 | |
| 77 | QColor QtColorButtonPrivate::shownColor() const |
| 78 | { |
| 79 | #ifndef QT_NO_DRAGANDDROP |
| 80 | if (m_dragging) |
| 81 | return m_dragColor; |
| 82 | #endif |
| 83 | return m_color; |
| 84 | } |
| 85 | |
| 86 | QPixmap QtColorButtonPrivate::generatePixmap() const |
| 87 | { |
| 88 | QPixmap pix(24, 24); |
| 89 | |
| 90 | int pixSize = 20; |
| 91 | QBrush br(shownColor()); |
| 92 | |
| 93 | QPixmap pm(2 * pixSize, 2 * pixSize); |
| 94 | QPainter pmp(&pm); |
| 95 | pmp.fillRect(x: 0, y: 0, w: pixSize, h: pixSize, c: Qt::lightGray); |
| 96 | pmp.fillRect(x: pixSize, y: pixSize, w: pixSize, h: pixSize, c: Qt::lightGray); |
| 97 | pmp.fillRect(x: 0, y: pixSize, w: pixSize, h: pixSize, c: Qt::darkGray); |
| 98 | pmp.fillRect(x: pixSize, y: 0, w: pixSize, h: pixSize, c: Qt::darkGray); |
| 99 | pmp.fillRect(x: 0, y: 0, w: 2 * pixSize, h: 2 * pixSize, b: shownColor()); |
| 100 | br = QBrush(pm); |
| 101 | |
| 102 | QPainter p(&pix); |
| 103 | int corr = 1; |
| 104 | QRect r = pix.rect().adjusted(xp1: corr, yp1: corr, xp2: -corr, yp2: -corr); |
| 105 | p.setBrushOrigin(x: (r.width() % pixSize + pixSize) / 2 + corr, y: (r.height() % pixSize + pixSize) / 2 + corr); |
| 106 | p.fillRect(r, br); |
| 107 | |
| 108 | p.fillRect(x: r.width() / 4 + corr, y: r.height() / 4 + corr, |
| 109 | w: r.width() / 2, h: r.height() / 2, |
| 110 | b: QColor(shownColor().rgb())); |
| 111 | p.drawRect(r: pix.rect().adjusted(xp1: 0, yp1: 0, xp2: -1, yp2: -1)); |
| 112 | |
| 113 | return pix; |
| 114 | } |
| 115 | |
| 116 | /////////////// |
| 117 | |
| 118 | QtColorButton::QtColorButton(QWidget *parent) |
| 119 | : QToolButton(parent), d_ptr(new QtColorButtonPrivate) |
| 120 | { |
| 121 | d_ptr->q_ptr = this; |
| 122 | d_ptr->m_dragging = false; |
| 123 | d_ptr->m_backgroundCheckered = true; |
| 124 | |
| 125 | setAcceptDrops(true); |
| 126 | |
| 127 | connect(sender: this, SIGNAL(clicked()), receiver: this, SLOT(slotEditColor())); |
| 128 | setSizePolicy(QSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred)); |
| 129 | } |
| 130 | |
| 131 | QtColorButton::~QtColorButton() |
| 132 | { |
| 133 | } |
| 134 | |
| 135 | void QtColorButton::setColor(const QColor &color) |
| 136 | { |
| 137 | if (d_ptr->m_color == color) |
| 138 | return; |
| 139 | d_ptr->m_color = color; |
| 140 | update(); |
| 141 | } |
| 142 | |
| 143 | QColor QtColorButton::color() const |
| 144 | { |
| 145 | return d_ptr->m_color; |
| 146 | } |
| 147 | |
| 148 | void QtColorButton::setBackgroundCheckered(bool checkered) |
| 149 | { |
| 150 | if (d_ptr->m_backgroundCheckered == checkered) |
| 151 | return; |
| 152 | d_ptr->m_backgroundCheckered = checkered; |
| 153 | update(); |
| 154 | } |
| 155 | |
| 156 | bool QtColorButton::isBackgroundCheckered() const |
| 157 | { |
| 158 | return d_ptr->m_backgroundCheckered; |
| 159 | } |
| 160 | |
| 161 | void QtColorButton::paintEvent(QPaintEvent *event) |
| 162 | { |
| 163 | QToolButton::paintEvent(event); |
| 164 | if (!isEnabled()) |
| 165 | return; |
| 166 | |
| 167 | const int pixSize = 10; |
| 168 | QBrush br(d_ptr->shownColor()); |
| 169 | if (d_ptr->m_backgroundCheckered) { |
| 170 | QPixmap pm(2 * pixSize, 2 * pixSize); |
| 171 | QPainter pmp(&pm); |
| 172 | pmp.fillRect(x: 0, y: 0, w: pixSize, h: pixSize, c: Qt::white); |
| 173 | pmp.fillRect(x: pixSize, y: pixSize, w: pixSize, h: pixSize, c: Qt::white); |
| 174 | pmp.fillRect(x: 0, y: pixSize, w: pixSize, h: pixSize, c: Qt::black); |
| 175 | pmp.fillRect(x: pixSize, y: 0, w: pixSize, h: pixSize, c: Qt::black); |
| 176 | pmp.fillRect(x: 0, y: 0, w: 2 * pixSize, h: 2 * pixSize, b: d_ptr->shownColor()); |
| 177 | br = QBrush(pm); |
| 178 | } |
| 179 | |
| 180 | QPainter p(this); |
| 181 | const int corr = 4; |
| 182 | QRect r = rect().adjusted(xp1: corr, yp1: corr, xp2: -corr, yp2: -corr); |
| 183 | p.setBrushOrigin(x: (r.width() % pixSize + pixSize) / 2 + corr, y: (r.height() % pixSize + pixSize) / 2 + corr); |
| 184 | p.fillRect(r, br); |
| 185 | |
| 186 | //const int adjX = qRound(r.width() / 4.0); |
| 187 | //const int adjY = qRound(r.height() / 4.0); |
| 188 | //p.fillRect(r.adjusted(adjX, adjY, -adjX, -adjY), |
| 189 | // QColor(d_ptr->shownColor().rgb())); |
| 190 | /* |
| 191 | p.fillRect(r.adjusted(0, r.height() * 3 / 4, 0, 0), |
| 192 | QColor(d_ptr->shownColor().rgb())); |
| 193 | p.fillRect(r.adjusted(0, 0, 0, -r.height() * 3 / 4), |
| 194 | QColor(d_ptr->shownColor().rgb())); |
| 195 | */ |
| 196 | /* |
| 197 | const QColor frameColor0(0, 0, 0, qRound(0.2 * (0xFF - d_ptr->shownColor().alpha()))); |
| 198 | p.setPen(frameColor0); |
| 199 | p.drawRect(r.adjusted(adjX, adjY, -adjX - 1, -adjY - 1)); |
| 200 | */ |
| 201 | |
| 202 | const QColor frameColor1(0, 0, 0, 26); |
| 203 | p.setPen(frameColor1); |
| 204 | p.drawRect(r: r.adjusted(xp1: 1, yp1: 1, xp2: -2, yp2: -2)); |
| 205 | const QColor frameColor2(0, 0, 0, 51); |
| 206 | p.setPen(frameColor2); |
| 207 | p.drawRect(r: r.adjusted(xp1: 0, yp1: 0, xp2: -1, yp2: -1)); |
| 208 | } |
| 209 | |
| 210 | void QtColorButton::mousePressEvent(QMouseEvent *event) |
| 211 | { |
| 212 | #ifndef QT_NO_DRAGANDDROP |
| 213 | if (event->button() == Qt::LeftButton) |
| 214 | d_ptr->m_dragStart = event->pos(); |
| 215 | #endif |
| 216 | QToolButton::mousePressEvent(event); |
| 217 | } |
| 218 | |
| 219 | void QtColorButton::mouseMoveEvent(QMouseEvent *event) |
| 220 | { |
| 221 | #ifndef QT_NO_DRAGANDDROP |
| 222 | if (event->buttons() & Qt::LeftButton && |
| 223 | (d_ptr->m_dragStart - event->pos()).manhattanLength() > QApplication::startDragDistance()) { |
| 224 | QMimeData *mime = new QMimeData; |
| 225 | mime->setColorData(color()); |
| 226 | QDrag *drg = new QDrag(this); |
| 227 | drg->setMimeData(mime); |
| 228 | drg->setPixmap(d_ptr->generatePixmap()); |
| 229 | setDown(false); |
| 230 | event->accept(); |
| 231 | drg->exec(supportedActions: Qt::CopyAction); |
| 232 | return; |
| 233 | } |
| 234 | #endif |
| 235 | QToolButton::mouseMoveEvent(e: event); |
| 236 | } |
| 237 | |
| 238 | #ifndef QT_NO_DRAGANDDROP |
| 239 | void QtColorButton::dragEnterEvent(QDragEnterEvent *event) |
| 240 | { |
| 241 | const QMimeData *mime = event->mimeData(); |
| 242 | if (!mime->hasColor()) |
| 243 | return; |
| 244 | |
| 245 | event->accept(); |
| 246 | d_ptr->m_dragColor = qvariant_cast<QColor>(v: mime->colorData()); |
| 247 | d_ptr->m_dragging = true; |
| 248 | update(); |
| 249 | } |
| 250 | |
| 251 | void QtColorButton::dragLeaveEvent(QDragLeaveEvent *event) |
| 252 | { |
| 253 | event->accept(); |
| 254 | d_ptr->m_dragging = false; |
| 255 | update(); |
| 256 | } |
| 257 | |
| 258 | void QtColorButton::dropEvent(QDropEvent *event) |
| 259 | { |
| 260 | event->accept(); |
| 261 | d_ptr->m_dragging = false; |
| 262 | if (d_ptr->m_dragColor == color()) |
| 263 | return; |
| 264 | setColor(d_ptr->m_dragColor); |
| 265 | emit colorChanged(color: color()); |
| 266 | } |
| 267 | #endif |
| 268 | |
| 269 | QT_END_NAMESPACE |
| 270 | |
| 271 | #include "moc_qtcolorbutton.cpp" |
| 272 | |