File indexing completed on 2022-01-30 18:32:30

0001 /*
0002     SPDX-FileCopyrightText: 2012-2013 Evan Teran <evan.teran@gmail.com>
0003     SPDX-FileCopyrightText: 2006 Michel Marti <mma@objectxp.com>
0004 
0005     SPDX-License-Identifier: GPL-2.0-or-later
0006 */
0007 
0008 #include "bitbutton.h"
0009 #include <QApplication>
0010 #include <QPainter>
0011 
0012 //------------------------------------------------------------------------------
0013 // Name: BitButton
0014 // Desc: constructor
0015 //------------------------------------------------------------------------------
0016 BitButton::BitButton(QWidget *parent)
0017     : QAbstractButton(parent)
0018 {
0019     // too many bits for tab focus
0020     setFocusPolicy(Qt::ClickFocus);
0021 
0022     // size button by font
0023     QSize size = fontMetrics().size(0, QStringLiteral("M"));
0024 
0025     if (size.width() < size.height()) {
0026         size.setHeight(size.width());
0027     } else {
0028         size.setWidth(size.height());
0029     }
0030 
0031     setMinimumSize(size);
0032     setRenderSize(size);
0033     setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
0034 
0035     this->setAttribute(Qt::WA_Hover, true);
0036 }
0037 
0038 //------------------------------------------------------------------------------
0039 // Name: paintEvent
0040 // Desc: draws the button
0041 //------------------------------------------------------------------------------
0042 void BitButton::paintEvent(QPaintEvent *)
0043 {
0044     uint8_t alpha = 0x60;
0045     QPainter painter(this);
0046     painter.setRenderHint(QPainter::Antialiasing, true);
0047     QPen pen(palette().text(), 1);
0048     pen.setJoinStyle(Qt::MiterJoin);
0049     painter.setPen(pen);
0050 
0051     if (on_) {
0052         painter.setBrush(palette().text());
0053         alpha = 0xB0;
0054     } else {
0055         painter.setBrush(palette().base());
0056     }
0057 
0058     if (over_) {
0059         painter.setBrush(QColor(palette().text().color().red(), palette().text().color().green(), palette().text().color().blue(), alpha));
0060     }
0061 
0062     // Prepare button size (should be square)
0063     QRect square = rect();
0064     square.setSize(renderSize());
0065 
0066     // Draw button
0067     painter.translate(QPoint(0, (rect().height() - square.height()) / 2)); // center button
0068     painter.drawRect(square.adjusted(1, 1, -1, -1));
0069 }
0070 
0071 //------------------------------------------------------------------------------
0072 // Name: isOn
0073 // Desc: returns true if this bit-button is "on"
0074 //------------------------------------------------------------------------------
0075 bool BitButton::isOn() const
0076 {
0077     return on_;
0078 }
0079 
0080 //------------------------------------------------------------------------------
0081 // Name: setOn
0082 // Desc: changes the "on" value for the bitset
0083 //------------------------------------------------------------------------------
0084 void BitButton::setOn(bool value)
0085 {
0086     on_ = value;
0087     update();
0088 }
0089 
0090 //------------------------------------------------------------------------------
0091 // Name: renderSize
0092 // Desc: returns current render size
0093 //------------------------------------------------------------------------------
0094 void BitButton::setRenderSize(const QSize &size)
0095 {
0096     renderSize_ = size;
0097 }
0098 
0099 //------------------------------------------------------------------------------
0100 // Name: renderSize
0101 // Desc: returns current render size
0102 //------------------------------------------------------------------------------
0103 QSize BitButton::renderSize() const
0104 {
0105     return renderSize_;
0106 }
0107 
0108 //------------------------------------------------------------------------------
0109 // Name: enterEvent
0110 // Desc: sets to true the "over" variable on Enter event
0111 //------------------------------------------------------------------------------
0112 #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
0113 void BitButton::enterEvent(QEnterEvent *event)
0114 #else
0115 void BitButton::enterEvent(QEvent *event)
0116 #endif
0117 {
0118 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
0119     if (event->type() == QEvent::Enter)
0120 #endif
0121     {
0122         over_ = true;
0123         update();
0124     }
0125 }
0126 
0127 //------------------------------------------------------------------------------
0128 // Name: leaveEvent
0129 // Desc: sets to false the "over" variable on Leave event
0130 //------------------------------------------------------------------------------
0131 void BitButton::leaveEvent(QEvent *event)
0132 {
0133     if (event->type() == QEvent::Leave) {
0134         over_ = false;
0135         update();
0136     }
0137 }