summaryrefslogtreecommitdiff
path: root/LibGUI
diff options
context:
space:
mode:
authorAndreas Kling <awesomekling@gmail.com>2019-03-09 13:59:01 +0100
committerAndreas Kling <awesomekling@gmail.com>2019-03-09 13:59:01 +0100
commit3681a402ba06bc50c87daec98280945c6c176a39 (patch)
tree9f7f9b5ae9e04563f20ed154f18f370ddf54b6dd /LibGUI
parent7d1142c7d9faaa1ca2f8e9c246c618292ced0119 (diff)
downloadserenity-3681a402ba06bc50c87daec98280945c6c176a39.zip
GTableView: Allow changing sort options by clicking column headers.
Also paint the sorted column slightly differently to indicate what's up.
Diffstat (limited to 'LibGUI')
-rw-r--r--LibGUI/GTableView.cpp38
-rw-r--r--LibGUI/GTableView.h2
2 files changed, 40 insertions, 0 deletions
diff --git a/LibGUI/GTableView.cpp b/LibGUI/GTableView.cpp
index 95da5cfe35..f9363b720e 100644
--- a/LibGUI/GTableView.cpp
+++ b/LibGUI/GTableView.cpp
@@ -85,10 +85,36 @@ Rect GTableView::row_rect(int item_index) const
return { 0, header_height() + (item_index * item_height()), max(content_width(), width()), item_height() };
}
+int GTableView::column_width(int column_index) const
+{
+ return m_model->column_metadata(column_index).preferred_width;
+}
+
+Rect GTableView::header_rect(int column_index) const
+{
+ int x_offset = 0;
+ for (int i = 0; i < column_index; ++i)
+ x_offset += column_width(i) + horizontal_padding() * 2;
+ auto column_metadata = m_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()) {
// FIXME: Do something when clicking on a header.
+ auto adjusted_position = event.position().translated(m_horizontal_scrollbar->value(), 0);
+ for (int i = 0; i < m_model->column_count(); ++i) {
+ auto header_rect = this->header_rect(i);
+ if (header_rect.contains(adjusted_position)) {
+ auto new_sort_order = GSortOrder::Descending;
+ if (m_model->key_column() == i)
+ new_sort_order = m_model->sort_order() == GSortOrder::Ascending ? GSortOrder::Descending : GSortOrder::Ascending;
+ m_model->set_key_column_and_sort_order(i, new_sort_order);
+ return;
+ }
+ }
return;
}
@@ -120,12 +146,15 @@ void GTableView::paint_event(GPaintEvent& event)
int y = y_offset + painted_item_index * item_height();
Color background_color;
+ Color key_column_background_color;
Color text_color;
if (row_index == m_model->selected_index().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);
text_color = Color::White;
} else {
background_color = painted_item_index % 2 ? Color(210, 210, 210) : Color::White;
+ key_column_background_color = painted_item_index % 2 ? Color(190, 190, 190) : Color(235, 235, 235);
text_color = Color::Black;
}
@@ -134,7 +163,12 @@ void GTableView::paint_event(GPaintEvent& event)
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;
+ bool is_key_column = m_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);
+ }
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());
@@ -157,7 +191,11 @@ void GTableView::paint_event(GPaintEvent& event)
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;
+ bool is_key_column = m_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.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;
diff --git a/LibGUI/GTableView.h b/LibGUI/GTableView.h
index 109782b2e8..803d699d09 100644
--- a/LibGUI/GTableView.h
+++ b/LibGUI/GTableView.h
@@ -40,6 +40,8 @@ private:
void update_scrollbar_ranges();
int item_count() const;
Rect row_rect(int item_index) const;
+ Rect header_rect(int) const;
+ int column_width(int) const;
GScrollBar* m_vertical_scrollbar { nullptr };
GScrollBar* m_horizontal_scrollbar { nullptr };