GListView: Switch to using GModelSelection to support multi-select

This commit is contained in:
Andreas Kling 2019-09-07 20:31:11 +02:00
parent 56c360591c
commit d2d1a30d61
Notes: sideshowbarker 2024-07-19 12:13:31 +09:00

View File

@ -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);
});
}
}
}
}