-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
kcalc_bitbutton.cpp
157 lines (136 loc) · 4.79 KB
/
kcalc_bitbutton.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*
SPDX-FileCopyrightText: 2012-2013 Evan Teran <[email protected]>
SPDX-FileCopyrightText: 2006 Michel Marti <[email protected]>
SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "kcalc_bitbutton.h"
#include <QEvent>
#include <QPainter>
//------------------------------------------------------------------------------
// Name: BitButton
// Desc: constructor
//------------------------------------------------------------------------------
BitButton::BitButton(QWidget *parent)
: QAbstractButton(parent)
{
// too many bits for tab focus
setFocusPolicy(Qt::ClickFocus);
setCheckable(true);
// size button by font
QSize size = fontMetrics().size(0, QStringLiteral("M"));
if (size.width() < size.height()) {
size.setHeight(size.width());
} else {
size.setWidth(size.height());
}
setMinimumSize(size);
setRenderSize(size);
setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
this->setAttribute(Qt::WA_Hover, true);
}
//------------------------------------------------------------------------------
// Name: paintEvent
// Desc: draws the button
//------------------------------------------------------------------------------
void BitButton::paintEvent(QPaintEvent *)
{
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing, true);
QPen pen(palette().highlight(), 2);
pen.setJoinStyle(Qt::MiterJoin);
painter.setPen(over_ ? pen : Qt::NoPen);
if (isDown()) {
painter.setBrush(palette().highlight());
} else {
painter.setBrush(on_ ? palette().text() : palette().base());
}
// Prepare button size (should be square)
QRect square = rect();
square.setSize(renderSize());
painter.translate(QPoint(0, (rect().height() - square.height()) / 2)); // center button
painter.drawRoundedRect(square, 3.0, 3.0);
pen.setColor(palette().color(isDown() ? QPalette::HighlightedText : (on_ ? QPalette::Window : QPalette::Text)));
painter.setPen(pen);
painter.drawText(square, Qt::AlignCenter, QString::number(on_ ? 1 : 0));
}
//------------------------------------------------------------------------------
// Name: isOn
// Desc: returns true if this bit-button is "on"
//------------------------------------------------------------------------------
bool BitButton::isOn() const
{
return on_;
}
//------------------------------------------------------------------------------
// Name: setOn
// Desc: changes the "on" value for the bitset
//------------------------------------------------------------------------------
void BitButton::setOn(bool value)
{
on_ = value;
update();
}
//------------------------------------------------------------------------------
// Name: renderSize
// Desc: returns current render size
//------------------------------------------------------------------------------
void BitButton::setRenderSize(const QSize &size)
{
if (size == renderSize_)
return;
renderSize_ = size;
// resize font for painting 0/1
auto actualFont = font();
actualFont.setPixelSize(qFloor(renderSize_.height() * 0.9));
setFont(actualFont);
}
//------------------------------------------------------------------------------
// Name: renderSize
// Desc: returns current render size
//------------------------------------------------------------------------------
QSize BitButton::renderSize() const
{
return renderSize_;
}
//------------------------------------------------------------------------------
// Name: enterEvent
// Desc: sets to true the "over" variable on Enter event
//------------------------------------------------------------------------------
void BitButton::enterEvent(QEnterEvent *event)
{
Q_UNUSED(event)
over_ = true;
update();
}
//------------------------------------------------------------------------------
// Name: leaveEvent
// Desc: sets to false the "over" variable on Leave event
//------------------------------------------------------------------------------
void BitButton::leaveEvent(QEvent *event)
{
if (event->type() == QEvent::Leave) {
over_ = false;
update();
}
}
//------------------------------------------------------------------------------
// Name: focusInEvent
// Desc: sets to true the "over" variable on Enter event
//------------------------------------------------------------------------------
void BitButton::focusInEvent(QFocusEvent *event)
{
Q_UNUSED(event)
over_ = true;
update();
}
//------------------------------------------------------------------------------
// Name: focusOutEvent
// Desc: sets to false the "over" variable on Leave event
//------------------------------------------------------------------------------
void BitButton::focusOutEvent(QFocusEvent *event)
{
Q_UNUSED(event)
over_ = false;
update();
}
#include "moc_kcalc_bitbutton.cpp"