Serenity Operating System
at master 129 lines 4.8 kB view raw
1/* 2 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org> 3 * Copyright (c) 2022, networkException <networkexception@serenityos.org> 4 * Copyright (c) 2022, the SerenityOS developers. 5 * 6 * SPDX-License-Identifier: BSD-2-Clause 7 */ 8 9#pragma once 10 11#include <LibGUI/AbstractView.h> 12#include <LibGUI/Button.h> 13#include <LibGUI/HeaderView.h> 14 15namespace GUI { 16 17class TableCellPaintingDelegate { 18public: 19 virtual ~TableCellPaintingDelegate() = default; 20 21 virtual bool should_paint(ModelIndex const&) { return true; } 22 virtual void paint(Painter&, Gfx::IntRect const&, Gfx::Palette const&, ModelIndex const&) = 0; 23}; 24 25class AbstractTableView : public AbstractView { 26public: 27 int row_height() const { return font().pixel_size_rounded_up() + vertical_padding(); } 28 29 virtual int horizontal_padding() const { return m_horizontal_padding; } 30 void set_horizontal_padding(int padding) { m_horizontal_padding = padding; } 31 virtual int vertical_padding() const { return m_vertical_padding; } 32 void set_vertical_padding(int padding) { m_vertical_padding = padding; } 33 34 bool alternating_row_colors() const { return m_alternating_row_colors; } 35 void set_alternating_row_colors(bool b) { m_alternating_row_colors = b; } 36 bool highlight_selected_rows() const { return m_highlight_selected_rows; } 37 void set_highlight_selected_rows(bool b) { m_highlight_selected_rows = b; } 38 39 bool column_headers_visible() const; 40 void set_column_headers_visible(bool); 41 42 void set_column_visible(int, bool); 43 44 int column_width(int column) const; 45 void set_column_width(int column, int width); 46 void set_default_column_width(int column, int width); 47 virtual int minimum_column_width(int column); 48 virtual int minimum_row_height(int row); 49 50 Gfx::TextAlignment column_header_alignment(int column) const; 51 void set_column_header_alignment(int column, Gfx::TextAlignment); 52 53 void set_column_painting_delegate(int column, OwnPtr<TableCellPaintingDelegate>); 54 55 Gfx::IntPoint adjusted_position(Gfx::IntPoint) const; 56 57 virtual Gfx::IntRect content_rect(ModelIndex const&) const override; 58 Gfx::IntRect content_rect_minus_scrollbars(ModelIndex const&) const; 59 Gfx::IntRect content_rect(int row, int column) const; 60 Gfx::IntRect row_rect(int item_index) const; 61 62 virtual Gfx::IntRect paint_invalidation_rect(ModelIndex const& index) const override; 63 64 virtual void scroll_into_view(ModelIndex const&, bool scroll_horizontally = true, bool scroll_vertically = true) override; 65 void scroll_into_view(ModelIndex const& index, Orientation orientation) 66 { 67 scroll_into_view(index, orientation == Gfx::Orientation::Horizontal, orientation == Gfx::Orientation::Vertical); 68 } 69 70 virtual ModelIndex index_at_event_position(Gfx::IntPoint, bool& is_toggle) const; 71 virtual ModelIndex index_at_event_position(Gfx::IntPoint) const override; 72 73 virtual void select_all() override; 74 75 void header_did_change_section_visibility(Badge<HeaderView>, Gfx::Orientation, int section, bool visible); 76 void header_did_change_section_size(Badge<HeaderView>, Gfx::Orientation, int section, int size); 77 78 virtual void did_scroll() override; 79 80 HeaderView& column_header() { return *m_column_header; } 81 HeaderView const& column_header() const { return *m_column_header; } 82 83 HeaderView& row_header() { return *m_row_header; } 84 HeaderView const& row_header() const { return *m_row_header; } 85 86 virtual void model_did_update(unsigned flags) override; 87 88protected: 89 virtual ~AbstractTableView() override = default; 90 AbstractTableView(); 91 92 virtual void mousedown_event(MouseEvent&) override; 93 virtual void context_menu_event(ContextMenuEvent&) override; 94 virtual void keydown_event(KeyEvent&) override; 95 virtual void resize_event(ResizeEvent&) override; 96 97 virtual void toggle_index(ModelIndex const&) { } 98 99 void update_content_size(); 100 virtual void auto_resize_column(int column); 101 virtual void update_column_sizes(); 102 virtual void update_row_sizes(); 103 virtual int item_count() const; 104 105 TableCellPaintingDelegate* column_painting_delegate(int column) const; 106 107 void move_cursor_relative(int vertical_steps, int horizontal_steps, SelectionUpdate); 108 109 virtual Gfx::IntPoint automatic_scroll_delta_from_position(Gfx::IntPoint pos) const override; 110 111private: 112 void layout_headers(); 113 bool is_navigation(GUI::KeyEvent&); 114 115 RefPtr<HeaderView> m_column_header; 116 RefPtr<HeaderView> m_row_header; 117 RefPtr<Button> m_corner_button; 118 119 HashMap<int, OwnPtr<TableCellPaintingDelegate>> m_column_painting_delegate; 120 121 bool m_alternating_row_colors { true }; 122 bool m_highlight_selected_rows { true }; 123 124 int m_vertical_padding { 8 }; 125 int m_horizontal_padding { font().pixel_size_rounded_up() / 2 }; 126 int m_tab_moves { 0 }; 127}; 128 129}