From d2d1a30d61f7ee0f5ceaaee0bcbade9936e62b2f Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sat, 7 Sep 2019 20:31:11 +0200 Subject: GListView: Switch to using GModelSelection to support multi-select --- Libraries/LibGUI/GListView.cpp | 60 +++++++++++++++++++++++++----------------- 1 file changed, 36 insertions(+), 24 deletions(-) (limited to 'Libraries/LibGUI') diff --git a/Libraries/LibGUI/GListView.cpp b/Libraries/LibGUI/GListView.cpp index bcf33a8fb4..dae6d9ae15 100644 --- a/Libraries/LibGUI/GListView.cpp +++ b/Libraries/LibGUI/GListView.cpp @@ -72,12 +72,14 @@ void GListView::mousedown_event(GMouseEvent& event) for (int row = 0, row_count = model()->row_count(); row < row_count; ++row) { if (!content_rect(row).contains(adjusted_position)) continue; - model()->set_selected_index(model()->index(row, m_model_column)); - update(); + auto index = model()->index(row, m_model_column); + if (event.modifiers() & Mod_Ctrl) + selection().toggle(index); + else + selection().set(index); return; } - model()->set_selected_index({}); - update(); + selection().clear(); } void GListView::paint_event(GPaintEvent& event) @@ -97,7 +99,7 @@ void GListView::paint_event(GPaintEvent& event) int painted_item_index = 0; for (int row_index = 0; row_index < model()->row_count(); ++row_index) { - bool is_selected_row = row_index == model()->selected_index().row(); + bool is_selected_row = selection().contains_row(row_index); int y = painted_item_index * item_height(); Color background_color; @@ -153,17 +155,21 @@ void GListView::keydown_event(GKeyEvent& event) return; auto& model = *this->model(); if (event.key() == KeyCode::Key_Return) { - activate(model.selected_index()); + selection().for_each_index([this](auto& index) { + activate(index); + }); return; } if (event.key() == KeyCode::Key_Up) { GModelIndex new_index; - if (model.selected_index().is_valid()) - new_index = model.index(model.selected_index().row() - 1, model.selected_index().column()); - else + if (!selection().is_empty()) { + auto old_index = selection().first(); + new_index = model.index(old_index.row() - 1, old_index.column()); + } else { new_index = model.index(0, 0); + } if (model.is_valid(new_index)) { - model.set_selected_index(new_index); + selection().set(new_index); scroll_into_view(new_index, Orientation::Vertical); update(); } @@ -171,12 +177,14 @@ void GListView::keydown_event(GKeyEvent& event) } if (event.key() == KeyCode::Key_Down) { GModelIndex new_index; - if (model.selected_index().is_valid()) - new_index = model.index(model.selected_index().row() + 1, model.selected_index().column()); - else + if (!selection().is_empty()) { + auto old_index = selection().first(); + new_index = model.index(old_index.row() + 1, old_index.column()); + } else { new_index = model.index(0, 0); + } if (model.is_valid(new_index)) { - model.set_selected_index(new_index); + selection().set(new_index); scroll_into_view(new_index, Orientation::Vertical); update(); } @@ -184,9 +192,10 @@ void GListView::keydown_event(GKeyEvent& event) } if (event.key() == KeyCode::Key_PageUp) { int items_per_page = visible_content_rect().height() / item_height(); - auto new_index = model.index(max(0, model.selected_index().row() - items_per_page), model.selected_index().column()); + auto old_index = selection().first(); + auto new_index = model.index(max(0, old_index.row() - items_per_page), old_index.column()); if (model.is_valid(new_index)) { - model.set_selected_index(new_index); + selection().set(new_index); scroll_into_view(new_index, Orientation::Vertical); update(); } @@ -194,9 +203,10 @@ void GListView::keydown_event(GKeyEvent& event) } if (event.key() == KeyCode::Key_PageDown) { int items_per_page = visible_content_rect().height() / item_height(); - auto new_index = model.index(min(model.row_count() - 1, model.selected_index().row() + items_per_page), model.selected_index().column()); + auto old_index = selection().first(); + auto new_index = model.index(min(model.row_count() - 1, old_index.row() + items_per_page), old_index.column()); if (model.is_valid(new_index)) { - model.set_selected_index(new_index); + selection().set(new_index); scroll_into_view(new_index, Orientation::Vertical); update(); } @@ -215,13 +225,15 @@ void GListView::doubleclick_event(GMouseEvent& event) { if (!model()) return; - auto& model = *this->model(); if (event.button() == GMouseButton::Left) { - if (model.selected_index().is_valid()) { - if (is_editable()) - begin_editing(model.selected_index()); - else - activate(model.selected_index()); + if (!selection().is_empty()) { + if (is_editable()) { + begin_editing(selection().first()); + } else { + selection().for_each_index([this](auto& index) { + activate(index); + }); + } } } } -- cgit v1.2.3