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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
|
#include <LibGUI/GTableView.h>
#include <LibGUI/GTableModel.h>
#include <LibGUI/GScrollBar.h>
#include <SharedGraphics/Painter.h>
#include <Kernel/KeyCode.h>
GTableView::GTableView(GWidget* parent)
: GWidget(parent)
{
set_fill_with_background_color(false);
m_vertical_scrollbar = new GScrollBar(Orientation::Vertical, this);
m_vertical_scrollbar->set_step(4);
m_vertical_scrollbar->on_change = [this] (int) {
update();
};
m_horizontal_scrollbar = new GScrollBar(Orientation::Horizontal, this);
m_horizontal_scrollbar->set_step(4);
m_horizontal_scrollbar->set_big_step(30);
m_horizontal_scrollbar->on_change = [this] (int) {
update();
};
}
GTableView::~GTableView()
{
}
void GTableView::set_model(OwnPtr<GTableModel>&& model)
{
if (model.ptr() == m_model.ptr())
return;
if (m_model)
m_model->unregister_view(Badge<GTableView>(), *this);
m_model = move(model);
if (m_model)
m_model->register_view(Badge<GTableView>(), *this);
}
void GTableView::resize_event(GResizeEvent& event)
{
update_scrollbar_ranges();
m_vertical_scrollbar->set_relative_rect(event.size().width() - m_vertical_scrollbar->preferred_size().width(), 0, m_vertical_scrollbar->preferred_size().width(), event.size().height() - m_horizontal_scrollbar->preferred_size().height());
m_horizontal_scrollbar->set_relative_rect(0, event.size().height() - m_horizontal_scrollbar->preferred_size().height(), event.size().width() - m_vertical_scrollbar->preferred_size().width(), m_horizontal_scrollbar->preferred_size().height());
}
void GTableView::update_scrollbar_ranges()
{
int available_height = height() - header_height() - m_horizontal_scrollbar->height();
int excess_height = max(0, (item_count() * item_height()) - available_height);
m_vertical_scrollbar->set_range(0, excess_height);
int available_width = width() - m_vertical_scrollbar->width();
int excess_width = max(0, content_width() - available_width);
m_horizontal_scrollbar->set_range(0, excess_width);
m_vertical_scrollbar->set_big_step(visible_content_rect().height() - item_height());
}
int GTableView::content_width() const
{
if (!m_model)
return 0;
int width = 0;
int column_count = m_model->column_count();
for (int i = 0; i < column_count; ++i)
width += m_model->column_metadata(i).preferred_width + horizontal_padding() * 2;
return width;
}
void GTableView::model_notification(const GModelNotification&)
{
}
void GTableView::did_update_model()
{
update_scrollbar_ranges();
update();
model_notification(GModelNotification(GModelNotification::ModelUpdated));
}
Rect GTableView::row_rect(int item_index) const
{
return { 0, header_height() + (item_index * item_height()), max(content_width(), width()), item_height() };
}
void GTableView::mousedown_event(GMouseEvent& event)
{
auto adjusted_position = event.position().translated(0, m_vertical_scrollbar->value());
if (event.button() == GMouseButton::Left) {
for (int i = 0; i < item_count(); ++i) {
if (row_rect(i).contains(adjusted_position)) {
m_model->set_selected_index({ i, 0 });
update();
return;
}
}
}
m_model->set_selected_index({ });
update();
}
void GTableView::paint_event(GPaintEvent& event)
{
Painter painter(*this);
painter.set_clip_rect(event.rect());
painter.translate(-m_horizontal_scrollbar->value(), -m_vertical_scrollbar->value());
int exposed_width = max(content_width(), width());
int painted_item_index = 0;
int y_offset = header_height();
for (int row_index = 0; row_index < m_model->row_count(); ++row_index) {
int y = y_offset + painted_item_index * item_height();
Color background_color;
Color text_color;
if (row_index == m_model->selected_index().row()) {
background_color = Color::from_rgb(0x84351a);
text_color = Color::White;
} else {
background_color = painted_item_index % 2 ? Color(210, 210, 210) : Color::White;
text_color = Color::Black;
}
painter.fill_rect(row_rect(painted_item_index), background_color);
int x_offset = 0;
for (int column_index = 0; column_index < m_model->column_count(); ++column_index) {
auto column_metadata = m_model->column_metadata(column_index);
int column_width = column_metadata.preferred_width;
Rect cell_rect(horizontal_padding() + x_offset, y, column_width, item_height());
auto data = m_model->data(row_index, column_index);
if (data.is_bitmap())
painter.blit(cell_rect.location(), data.as_bitmap(), data.as_bitmap().rect());
else
painter.draw_text(cell_rect, data.to_string(), column_metadata.text_alignment, text_color);
x_offset += column_width + horizontal_padding() * 2;
}
++painted_item_index;
};
Rect unpainted_rect(0, header_height() + painted_item_index * item_height(), exposed_width, height());
painter.fill_rect(unpainted_rect, Color::White);
// Untranslate the painter and paint the column headers.
painter.translate(0, m_vertical_scrollbar->value());
painter.fill_rect({ 0, 0, exposed_width, header_height() }, Color::LightGray);
int x_offset = 0;
for (int column_index = 0; column_index < m_model->column_count(); ++column_index) {
auto column_metadata = m_model->column_metadata(column_index);
int column_width = column_metadata.preferred_width;
Rect cell_rect(x_offset, 0, column_width + horizontal_padding() * 2, item_height());
painter.set_font(Font::default_bold_font());
painter.draw_text(cell_rect.translated(horizontal_padding(), 0), m_model->column_name(column_index), TextAlignment::CenterLeft, Color::Black);
x_offset += column_width + horizontal_padding() * 2;
painter.draw_line(cell_rect.top_left(), cell_rect.bottom_left(), Color::White);
painter.draw_line(cell_rect.top_right(), cell_rect.bottom_right(), Color::DarkGray);
}
painter.draw_line({ 0, 0 }, { exposed_width - 1, 0 }, Color::White);
painter.draw_line({ 0, header_height() - 1 }, { exposed_width - 1, header_height() - 1 }, Color::DarkGray);
// Then untranslate and fill in the scroll corner. This is pretty messy, tbh.
painter.translate(m_horizontal_scrollbar->value(), 0);
painter.fill_rect({ m_horizontal_scrollbar->relative_rect().top_right().translated(1, 0), { m_vertical_scrollbar->preferred_size().width(), m_horizontal_scrollbar->preferred_size().height() } }, Color::LightGray);
}
int GTableView::item_count() const
{
return m_model->row_count();
}
void GTableView::keydown_event(GKeyEvent& event)
{
if (!model())
return;
auto& model = *this->model();
if (event.key() == KeyCode::Key_Return) {
model.activate(model.selected_index());
return;
}
if (event.key() == KeyCode::Key_Up) {
GModelIndex new_index(model.selected_index().row() - 1, model.selected_index().column());
if (model.is_valid(new_index)) {
model.set_selected_index(new_index);
scroll_into_view(new_index, Orientation::Vertical);
update();
}
return;
}
if (event.key() == KeyCode::Key_Down) {
GModelIndex new_index(model.selected_index().row() + 1, model.selected_index().column());
if (model.is_valid(new_index)) {
model.set_selected_index(new_index);
scroll_into_view(new_index, Orientation::Vertical);
update();
}
return;
}
if (event.key() == KeyCode::Key_PageUp) {
int items_per_page = visible_content_rect().height() / item_height();
GModelIndex new_index(max(0, model.selected_index().row() - items_per_page), model.selected_index().column());
if (model.is_valid(new_index)) {
model.set_selected_index(new_index);
scroll_into_view(new_index, Orientation::Vertical);
update();
}
return;
}
if (event.key() == KeyCode::Key_PageDown) {
int items_per_page = visible_content_rect().height() / item_height();
GModelIndex new_index(min(model.row_count() - 1, model.selected_index().row() + items_per_page), model.selected_index().column());
if (model.is_valid(new_index)) {
model.set_selected_index(new_index);
scroll_into_view(new_index, Orientation::Vertical);
update();
}
return;
}
return GWidget::keydown_event(event);
}
Rect GTableView::visible_content_rect() const
{
return {
m_horizontal_scrollbar->value(),
m_vertical_scrollbar->value(),
width() - m_vertical_scrollbar->width(),
height() - header_height() - m_horizontal_scrollbar->height()
};
}
void GTableView::scroll_into_view(const GModelIndex& index, Orientation orientation)
{
auto visible_content_rect = this->visible_content_rect();
auto rect = row_rect(index.row()).translated(0, -header_height());
if (visible_content_rect.contains(rect))
return;
if (orientation == Orientation::Vertical) {
if (rect.top() < visible_content_rect.top())
m_vertical_scrollbar->set_value(rect.top());
else if (rect.bottom() > visible_content_rect.bottom())
m_vertical_scrollbar->set_value(rect.bottom() - visible_content_rect.height());
} else {
if (rect.left() < visible_content_rect.left())
m_horizontal_scrollbar->set_value(rect.left());
else if (rect.right() > visible_content_rect.right())
m_horizontal_scrollbar->set_value(rect.right() - visible_content_rect.width());
}
}
|