diff options
author | Andreas Kling <awesomekling@gmail.com> | 2019-08-14 20:34:46 +0200 |
---|---|---|
committer | Andreas Kling <awesomekling@gmail.com> | 2019-08-14 20:34:46 +0200 |
commit | 9789ee9b58fdcffc032424b0c4f0be3c240d84b9 (patch) | |
tree | 53671807ca17e95b0263d93da1982a416f9cf67e | |
parent | b777f740a40a02e9a593da385b7e656da0925707 (diff) | |
download | serenity-9789ee9b58fdcffc032424b0c4f0be3c240d84b9.zip |
GTableView: Add a way to customize cell painting per-column
You can now set a GTableCellPaintingDelegate per column in GTableView.
For columns with a painting delegate set, the view will defer to the
delegate for painting each cell in that column.
-rw-r--r-- | Libraries/LibGUI/GTableView.cpp | 36 | ||||
-rw-r--r-- | Libraries/LibGUI/GTableView.h | 11 |
2 files changed, 34 insertions, 13 deletions
diff --git a/Libraries/LibGUI/GTableView.cpp b/Libraries/LibGUI/GTableView.cpp index b7472afd07..9e4e153957 100644 --- a/Libraries/LibGUI/GTableView.cpp +++ b/Libraries/LibGUI/GTableView.cpp @@ -158,7 +158,7 @@ void GTableView::mousedown_event(GMouseEvent& event) return; } auto header_rect = this->header_rect(i); - auto column_metadata = model()->column_metadata(i); + auto column_metadata = model()->column_metadata(i); if (header_rect.contains(event.position()) && column_metadata.sortable == GModel::ColumnMetadata::Sortable::True) { auto new_sort_order = GSortOrder::Ascending; if (model()->key_column() == i) @@ -288,19 +288,24 @@ void GTableView::paint_event(GPaintEvent& event) painter.fill_rect(cell_rect_for_fill, key_column_background_color); } auto cell_index = model()->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 if (data.is_icon()) { - if (auto bitmap = data.as_icon().bitmap_for_size(16)) - painter.blit(cell_rect.location(), *bitmap, bitmap->rect()); + + if (auto* delegate = column_data(column_index).cell_painting_delegate.ptr()) { + delegate->paint(painter, cell_rect, *model(), cell_index); } 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, TextElision::Right); + auto data = model()->data(cell_index); + if (data.is_bitmap()) { + painter.blit(cell_rect.location(), data.as_bitmap(), data.as_bitmap().rect()); + } else if (data.is_icon()) { + if (auto bitmap = data.as_icon().bitmap_for_size(16)) + painter.blit(cell_rect.location(), *bitmap, 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, TextElision::Right); + } } x_offset += column_width + horizontal_padding() * 2; } @@ -512,3 +517,8 @@ const Font& GTableView::header_font() { return Font::default_bold_font(); } + +void GTableView::set_cell_painting_delegate(int column, OwnPtr<GTableCellPaintingDelegate>&& delegate) +{ + column_data(column).cell_painting_delegate = move(delegate); +} diff --git a/Libraries/LibGUI/GTableView.h b/Libraries/LibGUI/GTableView.h index 1935bab13e..a469740cf1 100644 --- a/Libraries/LibGUI/GTableView.h +++ b/Libraries/LibGUI/GTableView.h @@ -5,9 +5,17 @@ #include <LibGUI/GAbstractView.h> #include <LibGUI/GModel.h> +class GPainter; class GScrollBar; class Painter; +class GTableCellPaintingDelegate { +public: + virtual ~GTableCellPaintingDelegate() {} + + virtual void paint(GPainter&, const Rect&, const GModel&, const GModelIndex&) = 0; +}; + class GTableView : public GAbstractView { C_OBJECT(GTableView) public: @@ -38,6 +46,8 @@ public: virtual Rect content_rect(const GModelIndex&) const override; + void set_cell_painting_delegate(int column, OwnPtr<GTableCellPaintingDelegate>&&); + private: virtual void did_update_model() override; virtual void paint_event(GPaintEvent&) override; @@ -67,6 +77,7 @@ private: bool has_initialized_width { false }; bool visibility { true }; RefPtr<GAction> visibility_action; + OwnPtr<GTableCellPaintingDelegate> cell_painting_delegate; }; ColumnData& column_data(int column) const; |