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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
|
#include "tabwidget.h"
#include "painter.h"
namespace GUI
{
TabWidget::TabWidget(Widget *parent)
: Widget(parent)
, stack(this)
{
CONNECT(this, sizeChangeNotifier, this, &TabWidget::sizeChanged);
CONNECT(&stack, currentChanged, this, &TabWidget::setActiveButtons);
}
TabID TabWidget::addTab(const std::string& title, Widget* widget)
{
buttons.emplace_back(this, widget);
auto& button = buttons.back();
button.setText(title);
stack.addWidget(widget);
CONNECT(&button, switchTabNotifier, this, &TabWidget::switchTab);
CONNECT(&button, scrollNotifier, this, &TabWidget::rotateTab);
sizeChanged(width(), height());
return button.getID();
}
void TabWidget::setTabWidth(std::size_t width)
{
tab_width = width;
relayout();
}
std::size_t TabWidget::getTabWidth() const
{
return tab_width;
}
void TabWidget::setVisible(TabID tab_id, bool visible)
{
for (auto& button : buttons)
{
if(button.getID() == tab_id)
{
button.setVisible(visible);
relayout();
return;
}
}
}
std::size_t TabWidget::getBarHeight() const
{
return topbar.height();
}
void TabWidget::rotateTab(float delta)
{
Widget* widget{nullptr};
Widget* current = stack.getCurrentWidget();
if(delta > 0.0f)
{
while((widget = stack.getWidgetAfter(current)) != nullptr)
{
auto button = getButtonFromWidget(widget);
if(!button || !button->visible())
{
current = widget;
continue;
}
break;
}
}
else
{
while((widget = stack.getWidgetBefore(current)) != nullptr)
{
auto button = getButtonFromWidget(widget);
if(!button || !button->visible())
{
current = widget;
continue;
}
break;
}
}
if(widget)
{
switchTab(widget);
}
}
void TabWidget::switchTab(Widget* tab_widget)
{
stack.setCurrentWidget(tab_widget);
}
void TabWidget::setActiveButtons(Widget* current_widget)
{
for (auto& button : buttons) {
if (button.getTabWidget() == current_widget) {
button.setActive(true);
}
else
{
button.setActive(false);
}
}
}
void TabWidget::sizeChanged(int width, int height)
{
std::size_t pos = 0;
int button_width = tab_width;
int bar_height = 25;
int button_border_width = 10;
int button_padding_left = 25;
int button_padding_inner = 3;
int logo_padding_right = button_padding_left / 2;
Painter p(*this);
if(buttons.size() > 0)
{
for(auto& button : buttons)
{
if(!button.visible())
{
continue;
}
int min_width = button.getMinimalWidth();
button_width = std::max(button_width, min_width + button_border_width);
}
button_width = std::min(button_width, width / (int)buttons.size());
}
topbar.setSize(width, bar_height);
p.drawImage(0, 0, topbar);
auto x_logo = width - toplogo.width() - logo_padding_right;
auto y_logo = (bar_height - toplogo.height()) / 2;
p.drawImage(x_logo, y_logo, toplogo);
pos = button_padding_left;
for(auto& button : buttons)
{
if(!button.visible())
{
continue;
}
button.resize(button_width, bar_height);
button.move(pos, 0);
pos += button_width + button_padding_inner;
}
stack.move(0, bar_height);
stack.resize(width, std::max((int)height - bar_height, 0));
}
void TabWidget::relayout()
{
sizeChanged(TabWidget::width(), TabWidget::height());
}
const TabButton* TabWidget::getButtonFromWidget(const Widget* tab_widget)
{
if(tab_widget == nullptr)
{
return nullptr;
}
for(auto& button : buttons)
{
if(button.getTabWidget() == tab_widget)
{
return &button;
}
}
return nullptr;
}
}
|