| 1 | /* |
| 2 | * Copyright (C) 2017 Damir Porobic <https://github.com/damirporobic> |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation; either version 2 of the License, or |
| 7 | * (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, |
| 17 | * Boston, MA 02110-1301, USA. |
| 18 | */ |
| 19 | |
| 20 | #include "ColorButton.h" |
| 21 | |
| 22 | ColorButton::ColorButton(QWidget* parent) : |
| 23 | QPushButton(parent), |
| 24 | mShowAlphaChannel(false) |
| 25 | { |
| 26 | connect(this, &ColorButton::clicked, this, &ColorButton::openDialog); |
| 27 | setIconSize(QSize(48, iconSize().height())); |
| 28 | } |
| 29 | |
| 30 | QColor ColorButton::color() const |
| 31 | { |
| 32 | return mColor; |
| 33 | } |
| 34 | |
| 35 | void ColorButton::setColor(const QColor& color) |
| 36 | { |
| 37 | mColor = color; |
| 38 | auto pixmapIcon = createPixmapFromColor(mColor); |
| 39 | setIcon(pixmapIcon); |
| 40 | setToolTip(mShowAlphaChannel ? mColor.name(QColor::HexArgb) : mColor.name()); |
| 41 | } |
| 42 | |
| 43 | void ColorButton::setShowAlphaChannel(bool enabled) |
| 44 | { |
| 45 | mShowAlphaChannel = enabled; |
| 46 | } |
| 47 | |
| 48 | QPixmap ColorButton::createPixmapFromColor(const QColor& color) |
| 49 | { |
| 50 | QImage tiledBackground(QSize(10, 10), QImage::Format_ARGB32_Premultiplied); |
| 51 | tiledBackground.fill(Qt::white); |
| 52 | QPainter tiledBackgroundPainter(&tiledBackground); |
| 53 | tiledBackgroundPainter.setPen(Qt::NoPen); |
| 54 | tiledBackgroundPainter.setBrush(Qt::gray); |
| 55 | tiledBackgroundPainter.drawRect(0, 0, 5, 5); |
| 56 | tiledBackgroundPainter.drawRect(5, 5, 10, 10); |
| 57 | |
| 58 | QPixmap pixmap(iconSize()); |
| 59 | QPainter pixmapPainter(&pixmap); |
| 60 | pixmapPainter.setPen(Qt::gray); |
| 61 | pixmapPainter.setBrush(tiledBackground); |
| 62 | pixmapPainter.drawRect(0,0, iconSize().width() - 1, iconSize().height() - 1); |
| 63 | pixmapPainter.setBrush(color); |
| 64 | pixmapPainter.drawRect(0,0, iconSize().width() - 1, iconSize().height() - 1); |
| 65 | return pixmap; |
| 66 | } |
| 67 | |
| 68 | void ColorButton::openDialog() |
| 69 | { |
| 70 | QFlags<QColorDialog::ColorDialogOption> options; |
| 71 | |
| 72 | if(mShowAlphaChannel) { |
| 73 | options |= QColorDialog::ShowAlphaChannel; |
| 74 | } |
| 75 | |
| 76 | auto color = QColorDialog::getColor(mColor, parentWidget(), QString(), options); |
| 77 | |
| 78 | if (color.isValid() && color != mColor) { |
| 79 | setColor(color); |
| 80 | } |
| 81 | } |