diff options
author | Hüseyin ASLITÜRK <asliturk@hotmail.com> | 2020-03-29 18:56:31 +0300 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2020-03-29 19:36:37 +0200 |
commit | 7194b4823e2a4902494e0548f9ecdfb55416e28e (patch) | |
tree | 0eaba97777a55fe3852763e127f471650b549c1e /Libraries/LibGUI | |
parent | db692a807ba663033c1e1f3830a38226aec2b212 (diff) | |
download | serenity-7194b4823e2a4902494e0548f9ecdfb55416e28e.zip |
LibGUI: ComboBox, add "set_selected_index" method
Easy way to set selected item.
Diffstat (limited to 'Libraries/LibGUI')
-rw-r--r-- | Libraries/LibGUI/ComboBox.cpp | 12 | ||||
-rw-r--r-- | Libraries/LibGUI/ComboBox.h | 2 |
2 files changed, 13 insertions, 1 deletions
diff --git a/Libraries/LibGUI/ComboBox.cpp b/Libraries/LibGUI/ComboBox.cpp index 6be9e6b2d6..9efc0a780c 100644 --- a/Libraries/LibGUI/ComboBox.cpp +++ b/Libraries/LibGUI/ComboBox.cpp @@ -67,7 +67,8 @@ ComboBox::ComboBox() ASSERT(model()); auto new_value = model()->data(index).to_string(); m_editor->set_text(new_value); - m_editor->select_all(); + if (!m_only_allow_values_from_model) + m_editor->select_all(); close(); deferred_invoke([this, index](auto&) { if (on_change) @@ -94,6 +95,15 @@ void ComboBox::set_model(NonnullRefPtr<Model> model) m_list_view->set_model(move(model)); } +void ComboBox::set_selected_index(size_t index) +{ + auto model = this->m_list_view->model(); + + auto model_index = model->index(index, 0); + if (model->is_valid(model_index)) + this->m_list_view->selection().set(model_index); +} + void ComboBox::select_all() { m_editor->select_all(); diff --git a/Libraries/LibGUI/ComboBox.h b/Libraries/LibGUI/ComboBox.h index f766a052d8..8e3232996b 100644 --- a/Libraries/LibGUI/ComboBox.h +++ b/Libraries/LibGUI/ComboBox.h @@ -46,6 +46,8 @@ public: const Model* model() const; void set_model(NonnullRefPtr<Model>); + void set_selected_index(size_t index); + bool only_allow_values_from_model() const { return m_only_allow_values_from_model; } void set_only_allow_values_from_model(bool); |