File indexing completed on 2024-12-22 04:14:39

0001 /*
0002  *  SPDX-FileCopyrightText: 2015 Dmitry Kazakov <dimula73@gmail.com>
0003  *
0004  *  SPDX-License-Identifier: GPL-2.0-or-later
0005  */
0006 
0007 #include "kis_equalizer_button.h"
0008 
0009 #include <QStyle>
0010 #include <QPainter>
0011 #include <QStyleOption>
0012 
0013 #include <QApplication>
0014 
0015 
0016 #include "KisAnimTimelineColors.h"
0017 #include "kis_global.h"
0018 #include "kis_debug.h"
0019 
0020 struct KisEqualizerButton::Private
0021 {
0022     Private(KisEqualizerButton *_q)
0023         : q(_q),
0024           isRightmost(false),
0025           isHovering(false) {}
0026 
0027     QRect boundingRect() const;
0028     QRect fillingRect() const;
0029 
0030     KisEqualizerButton *q;
0031     bool isRightmost;
0032     bool isHovering;
0033 };
0034 
0035 KisEqualizerButton::KisEqualizerButton(QWidget *parent)
0036     : QAbstractButton(parent),
0037       m_d(new Private(this))
0038 {
0039     setFocusPolicy(Qt::WheelFocus);
0040     setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
0041 }
0042 
0043 KisEqualizerButton::~KisEqualizerButton()
0044 {
0045 }
0046 
0047 void KisEqualizerButton::setRightmost(bool value)
0048 {
0049     m_d->isRightmost = value;
0050 }
0051 
0052 QRect KisEqualizerButton::Private::boundingRect() const
0053 {
0054     QRect bounds = q->rect().adjusted(0, 0, -static_cast<int>(isRightmost), 0);
0055     return bounds;
0056 }
0057 
0058 QRect KisEqualizerButton::Private::fillingRect() const
0059 {
0060     const int offset = 3;
0061     QRect filling = boundingRect().adjusted(offset + 1, offset + 1,
0062                                             -offset, -offset);
0063 
0064     return filling;
0065 }
0066 
0067 void KisEqualizerButton::paintEvent(QPaintEvent *event)
0068 {
0069     Q_UNUSED(event);
0070     const QRect bounds = m_d->boundingRect();
0071     const QRect filling = m_d->fillingRect();
0072     const QColor backgroundColor = palette().color(QPalette::Base);
0073 
0074     QPainter p(this);
0075 
0076     { // draw border
0077 
0078         QStyleOptionViewItem option; // empty!
0079         const int gridHint = style()->styleHint(QStyle::SH_Table_GridLineColor, &option, this);
0080         const QColor gridColor = static_cast<QRgb>(gridHint);
0081         const QPen gridPen(gridColor);
0082 
0083         p.setPen(gridPen);
0084         p.setBrush(backgroundColor);
0085         p.drawRect(bounds);
0086     }
0087 
0088     {
0089         QColor fillColor = KisAnimTimelineColors::instance()->onionSkinsButtonColor();
0090         QColor frameColor = KisAnimTimelineColors::instance()-> onionSkinsSliderEnabledColor();
0091 
0092         if (isChecked() || hasFocus() || m_d->isHovering) {
0093             p.setPen(hasFocus() || m_d->isHovering ? frameColor : Qt::transparent);
0094             p.setBrush(isChecked() ? fillColor : Qt::transparent);
0095             p.drawRect(filling);
0096         }
0097     }
0098 
0099     QString textValue = text();
0100 
0101     { // draw text
0102         QPalette::ColorRole textRole = QPalette::Text;
0103 
0104         //Draw text shadow, This will increase readability when the background of same color
0105         QRect shadowRect(bounds);
0106         shadowRect.translate(1,1);
0107         QColor textColor = palette().color(textRole);
0108         QColor shadowColor = (textColor.value() <= 128)
0109             ? QColor(255,255,255,160) : QColor(0,0,0,160);
0110 
0111         int flags = Qt::AlignCenter | Qt::TextHideMnemonic;
0112 
0113         p.setPen(shadowColor);
0114         p.drawText(shadowRect, flags, textValue);
0115 
0116         p.setPen(textColor);
0117         p.drawText(bounds, flags, textValue);
0118     }
0119 }
0120 
0121 QSize KisEqualizerButton::sizeHint() const
0122 {
0123     QFontMetrics metrics(this->font());
0124     const int minHeight = metrics.height() + 10;
0125     return QSize(15, minHeight);
0126 }
0127 
0128 QSize KisEqualizerButton::minimumSizeHint() const
0129 {
0130     QSize sh = sizeHint();
0131     return QSize(10, sh.height());
0132 }
0133 
0134 void KisEqualizerButton::enterEvent(QEvent *event)
0135 {
0136     Q_UNUSED(event);
0137 
0138     m_d->isHovering = true;
0139     update();
0140 }
0141 
0142 void KisEqualizerButton::leaveEvent(QEvent *event)
0143 {
0144     Q_UNUSED(event);
0145 
0146     m_d->isHovering = false;
0147     update();
0148 }