1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 10:38:11 +00:00

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
This commit is contained in:
AnotherTest 2020-08-23 18:35:47 +04:30 committed by Andreas Kling
parent 697faba147
commit e1a819827c
3 changed files with 31 additions and 5 deletions

View file

@ -165,7 +165,7 @@ void AbstractTableView::paint_headers(Painter& painter)
auto text_rect = cell_rect.translated(horizontal_padding(), 0); auto text_rect = cell_rect.translated(horizontal_padding(), 0);
if (pressed) if (pressed)
text_rect.move_by(1, 1); 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; 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; 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) void AbstractTableView::mousemove_event(MouseEvent& event)
{ {
if (!model()) if (!model())

View file

@ -32,7 +32,7 @@ namespace GUI {
class TableCellPaintingDelegate { class TableCellPaintingDelegate {
public: public:
virtual ~TableCellPaintingDelegate() {} virtual ~TableCellPaintingDelegate() { }
virtual void paint(Painter&, const Gfx::IntRect&, const Gfx::Palette&, const ModelIndex&) = 0; 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; } bool alternating_row_colors() const { return m_alternating_row_colors; }
void set_alternating_row_colors(bool b) { m_alternating_row_colors = b; } 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; } int header_height() const { return m_headers_visible ? 16 : 0; }
@ -52,6 +54,12 @@ public:
bool is_column_hidden(int) const; bool is_column_hidden(int) const;
void set_column_hidden(int, bool); 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>&&); void set_cell_painting_delegate(int column, OwnPtr<TableCellPaintingDelegate>&&);
int horizontal_padding() const { return m_horizontal_padding; } int horizontal_padding() const { return m_horizontal_padding; }
@ -83,7 +91,7 @@ protected:
virtual void leave_event(Core::Event&) override; virtual void leave_event(Core::Event&) override;
virtual void context_menu_event(ContextMenuEvent&) override; virtual void context_menu_event(ContextMenuEvent&) override;
virtual void toggle_index(const ModelIndex&) {} virtual void toggle_index(const ModelIndex&) { }
void paint_headers(Painter&); void paint_headers(Painter&);
Gfx::IntRect header_rect(int column) const; Gfx::IntRect header_rect(int column) const;
@ -97,6 +105,7 @@ protected:
bool has_initialized_width { false }; bool has_initialized_width { false };
bool visibility { true }; bool visibility { true };
RefPtr<Action> visibility_action; RefPtr<Action> visibility_action;
Gfx::TextAlignment header_alignment { Gfx::TextAlignment::CenterLeft };
OwnPtr<TableCellPaintingDelegate> cell_painting_delegate; OwnPtr<TableCellPaintingDelegate> cell_painting_delegate;
}; };
ColumnData& column_data(int column) const; ColumnData& column_data(int column) const;
@ -107,7 +116,6 @@ protected:
RefPtr<Menu> m_header_context_menu; RefPtr<Menu> m_header_context_menu;
Gfx::IntRect column_resize_grabbable_rect(int) const; Gfx::IntRect column_resize_grabbable_rect(int) const;
int column_width(int) const;
void update_content_size(); void update_content_size();
virtual void update_column_sizes(); virtual void update_column_sizes();
virtual int item_count() const; virtual int item_count() const;
@ -116,6 +124,7 @@ private:
bool m_headers_visible { true }; bool m_headers_visible { true };
bool m_in_column_resize { false }; bool m_in_column_resize { false };
bool m_alternating_row_colors { true }; bool m_alternating_row_colors { true };
bool m_highlight_selected_rows { true };
int m_horizontal_padding { 5 }; int m_horizontal_padding { 5 };
Gfx::IntPoint m_column_resize_origin; Gfx::IntPoint m_column_resize_origin;
int m_column_resize_original_width { 0 }; int m_column_resize_original_width { 0 };

View file

@ -84,7 +84,7 @@ void TableView::paint_event(PaintEvent& event)
Color background_color; Color background_color;
Color key_column_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(); background_color = is_focused() ? palette().selection() : palette().inactive_selection();
key_column_background_color = is_focused() ? palette().selection() : palette().inactive_selection(); key_column_background_color = is_focused() ? palette().selection() : palette().inactive_selection();
} else { } else {