File indexing completed on 2024-06-09 04:24:11

0001 /*
0002  *  SPDX-FileCopyrightText: 2016 Dmitry Kazakov <dimula73@gmail.com>
0003  *
0004  *  SPDX-License-Identifier: GPL-2.0-or-later
0005  */
0006 
0007 #include "kis_round_hud_button.h"
0008 
0009 #include <QPaintEvent>
0010 #include <QPainter>
0011 
0012 #include "kis_global.h"
0013 
0014 
0015 
0016 struct KisRoundHudButton::Private
0017 {
0018     Private() : isHighlighted(false) {}
0019 
0020     bool isHighlighted;
0021     QIcon onIcon;
0022     QIcon offIcon;
0023 };
0024 
0025 KisRoundHudButton::KisRoundHudButton(QWidget *parent)
0026     : QAbstractButton(parent),
0027       m_d(new Private)
0028 {
0029     setMouseTracking(true);
0030 }
0031 
0032 KisRoundHudButton::~KisRoundHudButton()
0033 {
0034 }
0035 
0036 void KisRoundHudButton::setOnOffIcons(const QIcon &on, const QIcon &off)
0037 {
0038     m_d->onIcon = on;
0039     m_d->offIcon = off;
0040 }
0041 
0042 void KisRoundHudButton::paintEvent(QPaintEvent */*event*/)
0043 {
0044     const int borderWidth = 3;
0045     const QPointF center = QRectF(rect()).center();
0046     const qreal radius = 0.5 * (center.x() + center.y()) - borderWidth;
0047 
0048     const QPen fgPen(palette().color(m_d->isHighlighted ? QPalette::Highlight : QPalette::WindowText), borderWidth);
0049     const QBrush bgBrush(palette().brush(isDown() || (isCheckable() && isChecked()) ? QPalette::Mid : QPalette::Window));
0050 
0051     QPainter painter(this);
0052     painter.setPen(fgPen);
0053     painter.setBrush(bgBrush);
0054     painter.setRenderHints(QPainter::Antialiasing);
0055 
0056     painter.drawEllipse(center, radius, radius);
0057 
0058     if (!icon().isNull()) {
0059         const QIcon::Mode mode = isEnabled() ? QIcon::Normal : QIcon::Disabled;
0060         const QIcon::State state = isCheckable() && isChecked() ? QIcon::On : QIcon::Off;
0061         const QSize size = iconSize();
0062 
0063         QPixmap pixmap = icon().pixmap(size, mode, state);
0064 
0065         QPointF iconOffset(0.5 * (width() - size.width()),
0066                            0.5 * (height() - size.height()));
0067 
0068         painter.drawPixmap(iconOffset, pixmap);
0069     }
0070 
0071     if (!m_d->onIcon.isNull()) {
0072         const QIcon::Mode mode = isEnabled() ? QIcon::Normal : QIcon::Disabled;
0073         const QIcon icon = isCheckable() && isChecked() ? m_d->onIcon : m_d->offIcon;
0074         const QSize size = iconSize();
0075 
0076         QPixmap pixmap = icon.pixmap(size, mode);
0077         QPointF iconOffset(0.5 * (width() - size.width()),
0078                            0.5 * (height() - size.height()));
0079 
0080         painter.drawPixmap(iconOffset, pixmap);
0081     }
0082 }
0083 
0084 bool KisRoundHudButton::hitButton(const QPoint &pos) const
0085 {
0086     const int borderWidth = 3;
0087     const QPointF center = QRectF(rect()).center();
0088     const qreal radius = 0.5 * (center.x() + center.y()) - borderWidth;
0089 
0090     return kisDistance(center, pos) < radius;
0091 }
0092 
0093 void KisRoundHudButton::mouseMoveEvent(QMouseEvent *event)
0094 {
0095     bool isOver = hitButton(event->pos());
0096     if (isOver != m_d->isHighlighted) {
0097         m_d->isHighlighted = isOver;
0098         update();
0099     }
0100     
0101     QAbstractButton::mouseMoveEvent(event);
0102 }
0103 
0104 void KisRoundHudButton::leaveEvent(QEvent *event)
0105 {
0106     Q_UNUSED(event);
0107     if (m_d->isHighlighted) {
0108         m_d->isHighlighted = false;
0109         update();
0110     }
0111 
0112     QAbstractButton::leaveEvent(event);
0113 }