AbstractTableView.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <LibGUI/AbstractView.h>
  8. namespace GUI {
  9. class TableCellPaintingDelegate {
  10. public:
  11. virtual ~TableCellPaintingDelegate() { }
  12. virtual void paint(Painter&, const Gfx::IntRect&, const Gfx::Palette&, const ModelIndex&) = 0;
  13. };
  14. class AbstractTableView : public AbstractView {
  15. public:
  16. int row_height() const { return font().glyph_height() + vertical_padding(); }
  17. virtual int horizontal_padding() const { return m_horizontal_padding; }
  18. void set_horizontal_padding(int padding) { m_horizontal_padding = padding; }
  19. virtual int vertical_padding() const { return m_vertical_padding; }
  20. void set_vertical_padding(int padding) { m_vertical_padding = padding; }
  21. bool alternating_row_colors() const { return m_alternating_row_colors; }
  22. void set_alternating_row_colors(bool b) { m_alternating_row_colors = b; }
  23. bool highlight_selected_rows() const { return m_highlight_selected_rows; }
  24. void set_highlight_selected_rows(bool b) { m_highlight_selected_rows = b; }
  25. bool column_headers_visible() const;
  26. void set_column_headers_visible(bool);
  27. void set_column_visible(int, bool);
  28. int column_width(int column) const;
  29. void set_column_width(int column, int width);
  30. void set_default_column_width(int column, int width);
  31. Gfx::TextAlignment column_header_alignment(int column) const;
  32. void set_column_header_alignment(int column, Gfx::TextAlignment);
  33. void set_column_painting_delegate(int column, OwnPtr<TableCellPaintingDelegate>);
  34. Gfx::IntPoint adjusted_position(const Gfx::IntPoint&) const;
  35. virtual Gfx::IntRect content_rect(const ModelIndex&) const override;
  36. Gfx::IntRect content_rect(int row, int column) const;
  37. Gfx::IntRect row_rect(int item_index) const;
  38. virtual void scroll_into_view(const ModelIndex&, bool scroll_horizontally = true, bool scroll_vertically = true) override;
  39. void scroll_into_view(const ModelIndex& index, Orientation orientation)
  40. {
  41. scroll_into_view(index, orientation == Gfx::Orientation::Horizontal, orientation == Gfx::Orientation::Vertical);
  42. }
  43. virtual ModelIndex index_at_event_position(const Gfx::IntPoint&, bool& is_toggle) const;
  44. virtual ModelIndex index_at_event_position(const Gfx::IntPoint&) const override;
  45. virtual void select_all() override;
  46. void header_did_change_section_visibility(Badge<HeaderView>, Gfx::Orientation, int section, bool visible);
  47. void header_did_change_section_size(Badge<HeaderView>, Gfx::Orientation, int section, int size);
  48. virtual void did_scroll() override;
  49. HeaderView& column_header() { return *m_column_header; }
  50. const HeaderView& column_header() const { return *m_column_header; }
  51. HeaderView& row_header() { return *m_row_header; }
  52. const HeaderView& row_header() const { return *m_row_header; }
  53. virtual void model_did_update(unsigned flags) override;
  54. protected:
  55. virtual ~AbstractTableView() override;
  56. AbstractTableView();
  57. virtual void mousedown_event(MouseEvent&) override;
  58. virtual void context_menu_event(ContextMenuEvent&) override;
  59. virtual void keydown_event(KeyEvent&) override;
  60. virtual void resize_event(ResizeEvent&) override;
  61. virtual void toggle_index(const ModelIndex&) { }
  62. void update_content_size();
  63. virtual void auto_resize_column(int column);
  64. virtual void update_column_sizes();
  65. virtual void update_row_sizes();
  66. virtual int item_count() const;
  67. TableCellPaintingDelegate* column_painting_delegate(int column) const;
  68. void move_cursor_relative(int vertical_steps, int horizontal_steps, SelectionUpdate);
  69. private:
  70. void layout_headers();
  71. bool is_navigation(GUI::KeyEvent&);
  72. RefPtr<HeaderView> m_column_header;
  73. RefPtr<HeaderView> m_row_header;
  74. RefPtr<Button> m_corner_button;
  75. HashMap<int, OwnPtr<TableCellPaintingDelegate>> m_column_painting_delegate;
  76. bool m_alternating_row_colors { true };
  77. bool m_highlight_selected_rows { true };
  78. int m_vertical_padding { 8 };
  79. int m_horizontal_padding { font().glyph_height() / 2 };
  80. int m_tab_moves { 0 };
  81. };
  82. }