summaryrefslogtreecommitdiff
path: root/LibGUI/GTableView.cpp
blob: e753002ab7b920be4e568db44041f20b10b9cb7f (plain)
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
#include <LibGUI/GTableView.h>
#include <LibGUI/GModel.h>
#include <LibGUI/GScrollBar.h>
#include <SharedGraphics/Painter.h>
#include <Kernel/KeyCode.h>

GTableView::GTableView(GWidget* parent)
    : GAbstractView(parent)
{
}

GTableView::~GTableView()
{
}

void GTableView::update_content_size()
{
    if (!model())
        return set_content_size({ });

    int content_width = 0;
    int column_count = model()->column_count();
    for (int i = 0; i < column_count; ++i)
        content_width += model()->column_metadata(i).preferred_width + horizontal_padding() * 2;
    int content_height = item_count() * item_height();

    set_content_size({ content_width, content_height });
    set_size_occupied_by_fixed_elements({ 0, header_height() });
}

void GTableView::did_update_model()
{
    GAbstractView::did_update_model();
    update_content_size();
    update();
}

Rect GTableView::row_rect(int item_index) const
{
    return { 0, header_height() + (item_index * item_height()), max(content_size().width(), width()), item_height() };
}

int GTableView::column_width(int column_index) const
{
    return model()->column_metadata(column_index).preferred_width;
}

Rect GTableView::header_rect(int column_index) const
{
    if (is_column_hidden(column_index))
        return { };
    int x_offset = 0;
    for (int i = 0; i < column_index; ++i) {
        if (is_column_hidden(i))
            continue;
        x_offset += column_width(i) + horizontal_padding() * 2;
    }
    auto column_metadata = model()->column_metadata(column_index);
    int column_width = column_metadata.preferred_width;
    return { x_offset, 0, column_width + horizontal_padding() * 2, header_height() };
}

void GTableView::mousedown_event(GMouseEvent& event)
{
    if (event.y() < header_height()) {
        auto adjusted_position = event.position().translated(horizontal_scrollbar().value(), 0);
        for (int i = 0; i < model()->column_count(); ++i) {
            auto header_rect = this->header_rect(i);
            if (header_rect.contains(adjusted_position)) {
                auto new_sort_order = GSortOrder::Ascending;
                if (model()->key_column() == i)
                    new_sort_order = model()->sort_order() == GSortOrder::Ascending
                        ? GSortOrder::Descending
                        : GSortOrder::Ascending;
                model()->set_key_column_and_sort_order(i, new_sort_order);
                return;
            }
        }
        return;
    }

    if (event.button() == GMouseButton::Left) {
        auto adjusted_position = event.position().translated(0, vertical_scrollbar().value());
        for (int i = 0; i < item_count(); ++i) {
            if (row_rect(i).contains(adjusted_position)) {
                model()->set_selected_index({ i, 0 });
                update();
                return;
            }
        }
        model()->set_selected_index({ });
        update();
    }
}

void GTableView::paint_event(GPaintEvent& event)
{
    Painter painter(*this);
    painter.set_clip_rect(event.rect());
    painter.save();
    painter.translate(-horizontal_scrollbar().value(), -vertical_scrollbar().value());

    int exposed_width = max(content_size().width(), width());
    int painted_item_index = 0;
    int y_offset = header_height();

    for (int row_index = 0; row_index < model()->row_count(); ++row_index) {
        bool is_selected_row = row_index == model()->selected_index().row();
        int y = y_offset + painted_item_index * item_height();

        Color background_color;
        Color key_column_background_color;
        if (is_selected_row) {
            background_color = is_focused() ? Color::from_rgb(0x84351a) : Color::from_rgb(0x606060);
            key_column_background_color = is_focused() ? Color::from_rgb(0x84351a) : Color::from_rgb(0x606060);
        } else {
            if (alternating_row_colors() && (painted_item_index % 2)) {
                background_color = Color(210, 210, 210);
                key_column_background_color = Color(190, 190, 190);
            } else {
                background_color = Color::White;
                key_column_background_color = Color(235, 235, 235);
            }
        }
        painter.fill_rect(row_rect(painted_item_index), background_color);

        int x_offset = 0;
        for (int column_index = 0; column_index < model()->column_count(); ++column_index) {
            if (is_column_hidden(column_index))
                continue;
            auto column_metadata = model()->column_metadata(column_index);
            int column_width = column_metadata.preferred_width;
            const Font& font = column_metadata.font ? *column_metadata.font : this->font();
            bool is_key_column = model()->key_column() == column_index;
            Rect cell_rect(horizontal_padding() + x_offset, y, column_width, item_height());
            if (is_key_column) {
                auto cell_rect_for_fill = cell_rect.inflated(horizontal_padding() * 2, 0);
                painter.fill_rect(cell_rect_for_fill, key_column_background_color);
            }
            GModelIndex cell_index(row_index, column_index);
            auto data = model()->data(cell_index);
            if (data.is_bitmap()) {
                painter.blit(cell_rect.location(), data.as_bitmap(), data.as_bitmap().rect());
            } else {
                Color text_color;
                if (is_selected_row)
                    text_color = Color::White;
                else
                    text_color = model()->data(cell_index, GModel::Role::ForegroundColor).to_color(Color::Black);
                painter.draw_text(cell_rect, data.to_string(), font, 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 vertically and do the column headers.
    painter.translate(0, vertical_scrollbar().value());
    if (headers_visible())
        paint_headers(painter);

    painter.restore();

    if (is_focused()) {
        Rect item_area_rect {
            0,
            header_height(),
            width() - vertical_scrollbar().width(),
            height() - header_height() - horizontal_scrollbar().height()
        };
        painter.draw_rect(item_area_rect, Color::from_rgb(0x84351a));
    };
}

void GTableView::paint_headers(Painter& painter)
{
    int exposed_width = max(content_size().width(), width());
    painter.fill_rect({ 0, 0, exposed_width, header_height() }, Color::LightGray);
    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);
    int x_offset = 0;
    for (int column_index = 0; column_index < model()->column_count(); ++column_index) {
        if (is_column_hidden(column_index))
            continue;
        auto column_metadata = model()->column_metadata(column_index);
        int column_width = column_metadata.preferred_width;
        bool is_key_column = model()->key_column() == column_index;
        Rect cell_rect(x_offset, 0, column_width + horizontal_padding() * 2, header_height());
        if (is_key_column) {
            painter.fill_rect(cell_rect.shrunken(2, 2), Color::from_rgb(0xdddddd));
        }
        painter.draw_text(cell_rect.translated(horizontal_padding(), 0), model()->column_name(column_index), Font::default_bold_font(), TextAlignment::CenterLeft, Color::Black);
        x_offset += column_width + horizontal_padding() * 2;
        // Draw column separator.
        painter.draw_line(cell_rect.top_left().translated(0, 1), cell_rect.bottom_left().translated(0, -1), Color::White);
        painter.draw_line(cell_rect.top_right(), cell_rect.bottom_right().translated(0, -1), Color::DarkGray);
    }
    // Draw the "start" of a new column to make the last separator look right.
    painter.draw_line({ x_offset, 1 }, { x_offset, header_height() - 2 }, Color::White);
}

int GTableView::item_count() const
{
    return 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;
        if (model.selected_index().is_valid())
            new_index = { model.selected_index().row() - 1, model.selected_index().column() };
        else
            new_index = { 0, 0 };
        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;
        if (model.selected_index().is_valid())
            new_index = { model.selected_index().row() + 1, model.selected_index().column() };
        else
            new_index = { 0, 0 };
        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);
}

void GTableView::scroll_into_view(const GModelIndex& index, Orientation orientation)
{
    auto rect = row_rect(index.row()).translated(0, -header_height());
    GScrollableWidget::scroll_into_view(rect, orientation);
}

bool GTableView::is_column_hidden(int column) const
{
    if (column >= 0 && column < m_column_visibility.size())
        return !m_column_visibility[column];
    return false;
}

void GTableView::set_column_hidden(int column, bool hidden)
{
    ASSERT(column >= 0);
    if (m_column_visibility.size() <= column) {
        int previous_column_count = m_column_visibility.size();
        m_column_visibility.resize(column + 1);
        for (int i = previous_column_count; i < m_column_visibility.size(); ++i)
            m_column_visibility[i] = true;
    }
    m_column_visibility[column] = !hidden;
}