summaryrefslogtreecommitdiff
path: root/LibGUI
diff options
context:
space:
mode:
authorAndreas Kling <awesomekling@gmail.com>2019-03-15 16:25:30 +0100
committerAndreas Kling <awesomekling@gmail.com>2019-03-15 16:25:30 +0100
commitc1f2f5a1533558bcc2eea2b10e6d7fa71ea0b3d2 (patch)
treeea56bd5e03fe1d226b8ba8202f13f353e9845a93 /LibGUI
parent84f5312dc279e2a155d777881d5e6eed69225c09 (diff)
downloadserenity-c1f2f5a1533558bcc2eea2b10e6d7fa71ea0b3d2.zip
LibGUI: Add a mode where GTableModel automatically activates on selection.
Diffstat (limited to 'LibGUI')
-rw-r--r--LibGUI/GModelIndex.h2
-rw-r--r--LibGUI/GTableModel.cpp11
-rw-r--r--LibGUI/GTableModel.h7
3 files changed, 19 insertions, 1 deletions
diff --git a/LibGUI/GModelIndex.h b/LibGUI/GModelIndex.h
index 9cd19ba3ac..a60b3c5efc 100644
--- a/LibGUI/GModelIndex.h
+++ b/LibGUI/GModelIndex.h
@@ -13,6 +13,8 @@ public:
int row() const { return m_row; }
int column() const { return m_column; }
+ bool operator==(const GModelIndex& other) const { return m_row == other.m_row && m_column == other.m_column; }
+
private:
int m_row { -1 };
int m_column { -1 };
diff --git a/LibGUI/GTableModel.cpp b/LibGUI/GTableModel.cpp
index e9185199b8..4cd6bdc468 100644
--- a/LibGUI/GTableModel.cpp
+++ b/LibGUI/GTableModel.cpp
@@ -33,3 +33,14 @@ void GTableModel::did_update()
view.did_update_model();
});
}
+
+void GTableModel::set_selected_index(const GModelIndex& index)
+{
+ if (m_selected_index == index)
+ return;
+ m_selected_index = index;
+ if (on_selection_changed)
+ on_selection_changed(index);
+ if (m_activates_on_selection && is_valid(index))
+ activate(index);
+}
diff --git a/LibGUI/GTableModel.h b/LibGUI/GTableModel.h
index baae6c4eff..200796f8a0 100644
--- a/LibGUI/GTableModel.h
+++ b/LibGUI/GTableModel.h
@@ -58,9 +58,12 @@ public:
return index.row() >= 0 && index.row() < row_count() && index.column() >= 0 && index.column() < column_count();
}
- void set_selected_index(const GModelIndex& index) { m_selected_index = index; }
+ void set_selected_index(const GModelIndex&);
GModelIndex selected_index() const { return m_selected_index; }
+ bool activates_on_selection() const { return m_activates_on_selection; }
+ void set_activates_on_selection(bool b) { m_activates_on_selection = b; }
+
virtual int key_column() const { return -1; }
virtual GSortOrder sort_order() const { return GSortOrder::None; }
virtual void set_key_column_and_sort_order(int, GSortOrder) { }
@@ -69,6 +72,7 @@ public:
void unregister_view(Badge<GTableView>, GTableView&);
Function<void(GTableModel&)> on_model_update;
+ Function<void(const GModelIndex&)> on_selection_changed;
protected:
GTableModel();
@@ -79,4 +83,5 @@ protected:
private:
HashTable<GTableView*> m_views;
GModelIndex m_selected_index;
+ bool m_activates_on_selection { false };
};