File indexing completed on 2024-12-22 04:15:01

0001 /*
0002  * SPDX-FileCopyrightText: 2020 Mathias Wein <lynx.mw+kde@gmail.com>
0003  *
0004  * SPDX-License-Identifier: GPL-3.0-or-later
0005  */
0006 
0007 #include "KisColorSourceToggle.h"
0008 
0009 #include <QPainter>
0010 
0011 class KisColorSourceToggle::Private
0012 {
0013 public:
0014     QColor foregroundColor;
0015     QColor backgroundColor;
0016 };
0017 
0018 KisColorSourceToggle::KisColorSourceToggle(QWidget *parent)
0019     : QAbstractButton(parent)
0020     , m_d(new Private)
0021 {
0022     setCheckable(true);
0023     setFocusPolicy(Qt::NoFocus);
0024     setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
0025 }
0026 
0027 KisColorSourceToggle::~KisColorSourceToggle()
0028 {}
0029 
0030 void KisColorSourceToggle::setForegroundColor(const QColor &color)
0031 {
0032     m_d->foregroundColor = color;
0033     update();
0034 }
0035 
0036 void KisColorSourceToggle::setBackgroundColor(const QColor &color)
0037 {
0038     m_d->backgroundColor = color;
0039     update();
0040 }
0041 
0042 void KisColorSourceToggle::paintEvent(QPaintEvent */*e*/)
0043 {
0044     int length = qMin(width(), height());
0045     int patchSize = length * 6 / 10;
0046     QRect fgRect(1, 1, patchSize, patchSize);
0047     QRect bgRect(length - patchSize - 2, length - patchSize - 2, patchSize, patchSize);
0048     QPainter painter(this);
0049     painter.translate(0.5, 0.5);
0050     painter.setRenderHint(QPainter::Antialiasing);
0051     if (isChecked()) {
0052         // inactive patch (lowered)
0053         painter.setPen(QPen(palette().shadow(), 1));
0054         painter.setBrush(m_d->foregroundColor);
0055         painter.drawRect(fgRect);
0056         // active patch (raised & highlighted)
0057         painter.setPen(QPen(palette().highlightedText(), 3));
0058         painter.setBrush(m_d->backgroundColor);
0059         painter.drawRect(bgRect);
0060         painter.setPen(QPen(palette().shadow(), 1));
0061         painter.drawRect(bgRect);
0062     }
0063     else {
0064         // inactive patch (lowered)
0065         painter.setPen(QPen(palette().shadow(), 1));
0066         painter.setBrush(m_d->backgroundColor);
0067         painter.drawRect(bgRect);
0068         // active patch (raised & highlighted)
0069         painter.setPen(QPen(palette().highlightedText(), 3));
0070         painter.setBrush(m_d->foregroundColor);
0071         painter.drawRect(fgRect);
0072         painter.setPen(QPen(palette().shadow(), 1));
0073         painter.drawRect(fgRect);
0074     }
0075 }
0076 
0077 QSize KisColorSourceToggle::sizeHint() const
0078 {
0079     return QSize(24, 24);
0080 }