| 1 |
/* |
| 2 |
* SPDX-FileCopyrightText: 2020 Mathias Wein <[email protected]> |
| 3 |
* |
| 4 |
* SPDX-License-Identifier: GPL-3.0-or-later |
| 5 |
*/ |
| 6 |
|
| 7 |
#include "KisColorSourceToggle.h" |
| 8 |
|
| 9 |
#include <QPainter> |
| 10 |
|
| 11 |
class KisColorSourceToggle::Private |
| 12 |
{ |
| 13 |
public: |
| 14 |
QColor foregroundColor; |
| 15 |
QColor backgroundColor; |
| 16 |
}; |
| 17 |
|
| 18 |
KisColorSourceToggle::KisColorSourceToggle(QWidget *parent) |
| 19 |
: QAbstractButton(parent) |
| 20 |
, m_d(new Private) |
| 21 |
{ |
| 22 |
setCheckable(true); |
| 23 |
setFocusPolicy(Qt::NoFocus); |
| 24 |
setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed); |
| 25 |
} |
| 26 |
|
| 27 |
KisColorSourceToggle::~KisColorSourceToggle() |
| 28 |
{} |
| 29 |
|
| 30 |
void KisColorSourceToggle::setForegroundColor(const QColor &color) |
| 31 |
{ |
| 32 |
m_d->foregroundColor = color; |
| 33 |
update(); |
| 34 |
} |
| 35 |
|
| 36 |
void KisColorSourceToggle::setBackgroundColor(const QColor &color) |
| 37 |
{ |
| 38 |
m_d->backgroundColor = color; |
| 39 |
update(); |
| 40 |
} |
| 41 |
|
| 42 |
void KisColorSourceToggle::paintEvent(QPaintEvent */*e*/) |
| 43 |
{ |
| 44 |
int length = qMin(width(), height()); |
| 45 |
int patchSize = length * 6 / 10; |
| 46 |
QRect fgRect(1, 1, patchSize, patchSize); |
| 47 |
QRect bgRect(length - patchSize - 2, length - patchSize - 2, patchSize, patchSize); |
| 48 |
QPainter painter(this); |
| 49 |
painter.translate(0.5, 0.5); |
| 50 |
painter.setRenderHint(QPainter::Antialiasing); |
| 51 |
if (isChecked()) { |
| 52 |
// inactive patch (lowered) |
| 53 |
painter.setPen(QPen(palette().shadow(), 1)); |
| 54 |
painter.setBrush(m_d->foregroundColor); |
| 55 |
painter.drawRect(fgRect); |
| 56 |
// active patch (raised & highlighted) |
| 57 |
painter.setPen(QPen(palette().highlightedText(), 3)); |
| 58 |
painter.setBrush(m_d->backgroundColor); |
| 59 |
painter.drawRect(bgRect); |
| 60 |
painter.setPen(QPen(palette().shadow(), 1)); |
| 61 |
painter.drawRect(bgRect); |
| 62 |
} |
| 63 |
else { |
| 64 |
// inactive patch (lowered) |
| 65 |
painter.setPen(QPen(palette().shadow(), 1)); |
| 66 |
painter.setBrush(m_d->backgroundColor); |
| 67 |
painter.drawRect(bgRect); |
| 68 |
// active patch (raised & highlighted) |
| 69 |
painter.setPen(QPen(palette().highlightedText(), 3)); |
| 70 |
painter.setBrush(m_d->foregroundColor); |
| 71 |
painter.drawRect(fgRect); |
| 72 |
painter.setPen(QPen(palette().shadow(), 1)); |
| 73 |
painter.drawRect(fgRect); |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
QSize KisColorSourceToggle::sizeHint() const |
| 78 |
{ |
| 79 |
return QSize(24, 24); |
| 80 |
} |