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
|
#include "frame.h"
#include "painter.h"
namespace GUI
{
FrameWidget::FrameWidget(Widget* parent, bool has_switch)
: Widget(parent)
, is_switched_on(!has_switch)
, bar_height(24)
{
if(has_switch)
{
power_button.move(4, 4);
power_button.resize(16, 16);
power_button.setChecked(is_switched_on);
CONNECT(&power_button, stateChangedNotifier, this,
&FrameWidget::powerButtonStateChanged);
}
power_button.setVisible(has_switch);
CONNECT(this, sizeChangeNotifier, this, &FrameWidget::sizeChanged);
}
void FrameWidget::repaintEvent(RepaintEvent* repaintEvent)
{
Painter p(*this);
int center_x = width() / 2;
auto title_buf = title.c_str();
p.setColour(enabled ? grey_box_colour : grey_box_colour_disabled);
p.drawFilledRectangle(1, 1, width() - 2, bar_height);
p.setColour(frame_colour_top);
p.drawLine(0, 0, width() - 1, 0);
p.setColour(frame_colour_bottom);
p.drawLine(0, height() - 1, width() - 1, height() - 1);
p.setColour(frame_colour_side);
p.drawLine(0, 0, 0, height() - 1);
p.drawLine(width() - 1, 0, width() - 1, height() - 1);
p.setColour(background_colour);
p.drawFilledRectangle(1, bar_height, width() - 2, height() - 2);
p.setColour(enabled ? label_colour : label_colour_disabled);
p.drawText(center_x - label_width, bar_height - 4, font, title_buf);
power_button.setEnabled(enabled);
}
void FrameWidget::powerButtonStateChanged(bool new_state)
{
is_switched_on = new_state;
onSwitchChangeNotifier(is_switched_on);
}
void FrameWidget::setTitle(std::string const& title)
{
this->title = title;
label_width = font.textWidth(title.c_str()) / 2 + 1;
}
void FrameWidget::setContent(Widget* content)
{
this->content = content;
content->reparent(this);
}
void FrameWidget::setOnSwitch(bool on)
{
is_switched_on = on;
power_button.setChecked(is_switched_on);
}
void FrameWidget::setEnabled(bool enabled)
{
this->enabled = enabled;
onEnabledChanged(enabled);
redraw();
}
void FrameWidget::sizeChanged(int width, int height)
{
if(content)
{
content_start_x = content_margin;
content_start_y = bar_height + content_margin;
content_width = std::max((int)width - 2 * content_margin, 0);
content_height = std::max((int)height - (bar_height + 2 * content_margin), 0);
content->move(content_start_x, content_start_y);
content->resize(content_width, content_height);
}
}
}
|