diff options
author | AnotherTest <ali.mpfard@gmail.com> | 2020-08-23 18:35:47 +0430 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2020-08-24 18:21:33 +0200 |
commit | e1a819827ca226c436a9618b2e2a097c20de07aa (patch) | |
tree | d70b6785ffc99a5d403c1b44ebbac94fda8bfa2e /Libraries/LibGUI | |
parent | 697faba1470e2899555764d3c871e1324868d3a9 (diff) | |
download | serenity-e1a819827ca226c436a9618b2e2a097c20de07aa.zip |
LibGUI: Make AbstractTableView and TableView more customisable
This patchset adds a few getters/setters to AbstractTableView to make
its looks more customisable:
- Header width & text alignment
- Default column width
- Ability to disable selected row highlighting
Diffstat (limited to 'Libraries/LibGUI')
-rw-r--r-- | Libraries/LibGUI/AbstractTableView.cpp | 19 | ||||
-rw-r--r-- | Libraries/LibGUI/AbstractTableView.h | 15 | ||||
-rw-r--r-- | Libraries/LibGUI/TableView.cpp | 2 |
3 files changed, 31 insertions, 5 deletions
diff --git a/Libraries/LibGUI/AbstractTableView.cpp b/Libraries/LibGUI/AbstractTableView.cpp index 797ac8a57e..bd7aac4f43 100644 --- a/Libraries/LibGUI/AbstractTableView.cpp +++ b/Libraries/LibGUI/AbstractTableView.cpp @@ -165,7 +165,7 @@ void AbstractTableView::paint_headers(Painter& painter) auto text_rect = cell_rect.translated(horizontal_padding(), 0); if (pressed) text_rect.move_by(1, 1); - painter.draw_text(text_rect, text, header_font(), Gfx::TextAlignment::CenterLeft, palette().button_text()); + painter.draw_text(text_rect, text, header_font(), column_header_alignment(column_index), palette().button_text()); x_offset += column_width + horizontal_padding() * 2; } } @@ -249,6 +249,23 @@ int AbstractTableView::column_width(int column_index) const return column_data(column_index).width; } +void AbstractTableView::set_column_width(int column, int width) +{ + column_data(column).width = width; +} + +Gfx::TextAlignment AbstractTableView::column_header_alignment(int column_index) const +{ + if (!model()) + return Gfx::TextAlignment::CenterLeft; + return column_data(column_index).header_alignment; +} + +void AbstractTableView::set_column_header_alignment(int column, Gfx::TextAlignment alignment) +{ + column_data(column).header_alignment = alignment; +} + void AbstractTableView::mousemove_event(MouseEvent& event) { if (!model()) diff --git a/Libraries/LibGUI/AbstractTableView.h b/Libraries/LibGUI/AbstractTableView.h index cbc80f6528..e7bfdaac96 100644 --- a/Libraries/LibGUI/AbstractTableView.h +++ b/Libraries/LibGUI/AbstractTableView.h @@ -32,7 +32,7 @@ namespace GUI { class TableCellPaintingDelegate { public: - virtual ~TableCellPaintingDelegate() {} + virtual ~TableCellPaintingDelegate() { } virtual void paint(Painter&, const Gfx::IntRect&, const Gfx::Palette&, const ModelIndex&) = 0; }; @@ -43,6 +43,8 @@ public: bool alternating_row_colors() const { return m_alternating_row_colors; } void set_alternating_row_colors(bool b) { m_alternating_row_colors = b; } + bool highlight_selected_rows() const { return m_highlight_selected_rows; } + void set_highlight_selected_rows(bool b) { m_highlight_selected_rows = b; } int header_height() const { return m_headers_visible ? 16 : 0; } @@ -52,6 +54,12 @@ public: bool is_column_hidden(int) const; void set_column_hidden(int, bool); + int column_width(int column) const; + void set_column_width(int column, int width); + + Gfx::TextAlignment column_header_alignment(int column) const; + void set_column_header_alignment(int column, Gfx::TextAlignment); + void set_cell_painting_delegate(int column, OwnPtr<TableCellPaintingDelegate>&&); int horizontal_padding() const { return m_horizontal_padding; } @@ -83,7 +91,7 @@ protected: virtual void leave_event(Core::Event&) override; virtual void context_menu_event(ContextMenuEvent&) override; - virtual void toggle_index(const ModelIndex&) {} + virtual void toggle_index(const ModelIndex&) { } void paint_headers(Painter&); Gfx::IntRect header_rect(int column) const; @@ -97,6 +105,7 @@ protected: bool has_initialized_width { false }; bool visibility { true }; RefPtr<Action> visibility_action; + Gfx::TextAlignment header_alignment { Gfx::TextAlignment::CenterLeft }; OwnPtr<TableCellPaintingDelegate> cell_painting_delegate; }; ColumnData& column_data(int column) const; @@ -107,7 +116,6 @@ protected: RefPtr<Menu> m_header_context_menu; Gfx::IntRect column_resize_grabbable_rect(int) const; - int column_width(int) const; void update_content_size(); virtual void update_column_sizes(); virtual int item_count() const; @@ -116,6 +124,7 @@ private: bool m_headers_visible { true }; bool m_in_column_resize { false }; bool m_alternating_row_colors { true }; + bool m_highlight_selected_rows { true }; int m_horizontal_padding { 5 }; Gfx::IntPoint m_column_resize_origin; int m_column_resize_original_width { 0 }; diff --git a/Libraries/LibGUI/TableView.cpp b/Libraries/LibGUI/TableView.cpp index 6e1ba7fe4b..2dfd0c4443 100644 --- a/Libraries/LibGUI/TableView.cpp +++ b/Libraries/LibGUI/TableView.cpp @@ -84,7 +84,7 @@ void TableView::paint_event(PaintEvent& event) Color background_color; Color key_column_background_color; - if (is_selected_row) { + if (is_selected_row && highlight_selected_rows()) { background_color = is_focused() ? palette().selection() : palette().inactive_selection(); key_column_background_color = is_focused() ? palette().selection() : palette().inactive_selection(); } else { |