Browse Source

LibGUI: Move selection behavior from TableView up to AbstractView

Let's make SelectionBehavior a view concept where views can either
select individual items (row, index) or whole rows. Maybe some day
we'll do whole columns, but I don't think we need that now.
Andreas Kling 4 years ago
parent
commit
f0138fcb25

+ 1 - 1
Applications/Spreadsheet/SpreadsheetView.cpp

@@ -148,7 +148,7 @@ SpreadsheetView::SpreadsheetView(Sheet& sheet)
     set_layout<GUI::VerticalBoxLayout>().set_margins({ 2, 2, 2, 2 });
     m_table_view = add<InfinitelyScrollableTableView>();
     m_table_view->set_grid_style(GUI::TableView::GridStyle::Both);
-    m_table_view->set_cursor_style(GUI::TableView::CursorStyle::Item);
+    m_table_view->set_selection_behavior(GUI::AbstractView::SelectionBehavior::SelectItems);
     m_table_view->set_edit_triggers(GUI::AbstractView::EditTrigger::EditKeyPressed | GUI::AbstractView::AnyKeyPressed | GUI::AbstractView::DoubleClicked);
     m_table_view->set_tab_key_navigation_enabled(true);
     m_table_view->row_header().set_visible(true);

+ 1 - 0
Libraries/LibGUI/AbstractTableView.cpp

@@ -41,6 +41,7 @@ namespace GUI {
 
 AbstractTableView::AbstractTableView()
 {
+    set_selection_behavior(SelectionBehavior::SelectRows);
     m_corner_button = add<Button>();
     m_corner_button->move_to_back();
     m_corner_button->set_background_role(Gfx::ColorRole::ThreedShadow1);

+ 9 - 0
Libraries/LibGUI/AbstractView.h

@@ -60,6 +60,11 @@ public:
         ClearIfNotSelected
     };
 
+    enum class SelectionBehavior {
+        SelectItems,
+        SelectRows,
+    };
+
     virtual void move_cursor(CursorMovement, SelectionUpdate) { }
 
     void set_model(RefPtr<Model>);
@@ -88,6 +93,9 @@ public:
     unsigned edit_triggers() const { return m_edit_triggers; }
     void set_edit_triggers(unsigned);
 
+    SelectionBehavior selection_behavior() const { return m_selection_behavior; }
+    void set_selection_behavior(SelectionBehavior behavior) { m_selection_behavior = behavior; }
+
     bool is_multi_select() const { return m_multi_select; }
     void set_multi_select(bool);
 
@@ -188,6 +196,7 @@ private:
     String m_searching;
     RefPtr<Core::Timer> m_searching_timer;
     ModelIndex m_cursor_index;
+    SelectionBehavior m_selection_behavior { SelectionBehavior::SelectItems };
     unsigned m_edit_triggers { EditTrigger::DoubleClicked | EditTrigger::EditKeyPressed };
     bool m_activates_on_selection { false };
     bool m_multi_select { true };

+ 2 - 10
Libraries/LibGUI/TableView.cpp

@@ -149,13 +149,13 @@ void TableView::paint_event(PaintEvent& event)
             if (m_grid_style == GridStyle::Vertical || m_grid_style == GridStyle::Both)
                 painter.draw_line(cell_rect_for_fill.top_right(), cell_rect_for_fill.bottom_right(), palette().ruler());
 
-            if (m_cursor_style == CursorStyle::Item && cell_index == cursor_index())
+            if (selection_behavior() == SelectionBehavior::SelectItems && cell_index == cursor_index())
                 painter.draw_rect(cell_rect_for_fill, palette().text_cursor());
 
             x += column_width + horizontal_padding() * 2;
         }
 
-        if (is_focused() && cursor_style() == CursorStyle::Row && row_index == cursor_index().row()) {
+        if (is_focused() && selection_behavior() == SelectionBehavior::SelectRows && row_index == cursor_index().row()) {
             painter.draw_rect(row_rect, widget_background_color);
             painter.draw_focus_rect(row_rect, palette().focus_outline());
         }
@@ -244,12 +244,4 @@ void TableView::set_grid_style(GridStyle style)
     update();
 }
 
-void TableView::set_cursor_style(CursorStyle style)
-{
-    if (m_cursor_style == style)
-        return;
-    m_cursor_style = style;
-    update();
-}
-
 }

+ 0 - 4
Libraries/LibGUI/TableView.h

@@ -51,9 +51,6 @@ public:
     GridStyle grid_style() const { return m_grid_style; }
     void set_grid_style(GridStyle);
 
-    CursorStyle cursor_style() const { return m_cursor_style; }
-    void set_cursor_style(CursorStyle);
-
     virtual void move_cursor(CursorMovement, SelectionUpdate) override;
 
 protected:
@@ -64,7 +61,6 @@ protected:
 
 private:
     GridStyle m_grid_style { GridStyle::None };
-    CursorStyle m_cursor_style { CursorStyle::Row };
 };
 
 }