1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 13:18:13 +00:00

GTableView: Switch to using GModelSelection to support multi-select

This commit is contained in:
Andreas Kling 2019-09-07 19:35:45 +02:00
parent b0f42ee672
commit a5e0242992

View file

@ -174,8 +174,15 @@ void GTableView::mousedown_event(GMouseEvent& event)
return; return;
} }
model()->set_selected_index(index_at_event_position(event.position())); auto index = index_at_event_position(event.position());
update(); if (!index.is_valid()) {
selection().clear();
return;
}
if (event.modifiers() & Mod_Ctrl)
selection().toggle(index);
else
selection().set(index);
} }
GModelIndex GTableView::index_at_event_position(const Point& position) const GModelIndex GTableView::index_at_event_position(const Point& position) const
@ -257,7 +264,7 @@ void GTableView::paint_event(GPaintEvent& event)
int y_offset = header_height(); int y_offset = header_height();
for (int row_index = 0; row_index < model()->row_count(); ++row_index) { 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 = y_offset + painted_item_index * item_height(); int y = y_offset + painted_item_index * item_height();
Color background_color; Color background_color;
@ -369,17 +376,21 @@ void GTableView::keydown_event(GKeyEvent& event)
return; return;
auto& model = *this->model(); auto& model = *this->model();
if (event.key() == KeyCode::Key_Return) { if (event.key() == KeyCode::Key_Return) {
activate(model.selected_index()); selection().for_each_index([this](auto& index) {
activate(index);
});
return; return;
} }
if (event.key() == KeyCode::Key_Up) { if (event.key() == KeyCode::Key_Up) {
GModelIndex new_index; GModelIndex new_index;
if (model.selected_index().is_valid()) if (!selection().is_empty()) {
new_index = model.index(model.selected_index().row() - 1, model.selected_index().column()); auto old_index = selection().first();
else new_index = model.index(old_index.row() - 1, old_index.column());
} else {
new_index = model.index(0, 0); new_index = model.index(0, 0);
}
if (model.is_valid(new_index)) { if (model.is_valid(new_index)) {
model.set_selected_index(new_index); selection().set(new_index);
scroll_into_view(new_index, Orientation::Vertical); scroll_into_view(new_index, Orientation::Vertical);
update(); update();
} }
@ -387,12 +398,14 @@ void GTableView::keydown_event(GKeyEvent& event)
} }
if (event.key() == KeyCode::Key_Down) { if (event.key() == KeyCode::Key_Down) {
GModelIndex new_index; GModelIndex new_index;
if (model.selected_index().is_valid()) if (!selection().is_empty()) {
new_index = model.index(model.selected_index().row() + 1, model.selected_index().column()); auto old_index = selection().first();
else new_index = model.index(old_index.row() + 1, old_index.column());
} else {
new_index = model.index(0, 0); new_index = model.index(0, 0);
}
if (model.is_valid(new_index)) { if (model.is_valid(new_index)) {
model.set_selected_index(new_index); selection().set(new_index);
scroll_into_view(new_index, Orientation::Vertical); scroll_into_view(new_index, Orientation::Vertical);
update(); update();
} }
@ -400,9 +413,10 @@ void GTableView::keydown_event(GKeyEvent& event)
} }
if (event.key() == KeyCode::Key_PageUp) { if (event.key() == KeyCode::Key_PageUp) {
int items_per_page = visible_content_rect().height() / item_height(); 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)) { if (model.is_valid(new_index)) {
model.set_selected_index(new_index); selection().set(new_index);
scroll_into_view(new_index, Orientation::Vertical); scroll_into_view(new_index, Orientation::Vertical);
update(); update();
} }
@ -410,9 +424,10 @@ void GTableView::keydown_event(GKeyEvent& event)
} }
if (event.key() == KeyCode::Key_PageDown) { if (event.key() == KeyCode::Key_PageDown) {
int items_per_page = visible_content_rect().height() / item_height(); 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)) { if (model.is_valid(new_index)) {
model.set_selected_index(new_index); selection().set(new_index);
scroll_into_view(new_index, Orientation::Vertical); scroll_into_view(new_index, Orientation::Vertical);
update(); update();
} }
@ -453,15 +468,17 @@ void GTableView::doubleclick_event(GMouseEvent& event)
{ {
if (!model()) if (!model())
return; return;
auto& model = *this->model();
if (event.button() == GMouseButton::Left) { if (event.button() == GMouseButton::Left) {
if (event.y() < header_height()) if (event.y() < header_height())
return; return;
if (model.selected_index().is_valid()) { if (!selection().is_empty()) {
if (is_editable()) if (is_editable()) {
begin_editing(model.selected_index()); begin_editing(selection().first());
else } else {
activate(model.selected_index()); selection().for_each_index([this](auto& index) {
activate(index);
});
}
} }
} }
} }
@ -504,7 +521,7 @@ void GTableView::context_menu_event(GContextMenuEvent& event)
return; return;
dbgprintf("context menu requested for index (%d,%d) '%s'\n", index.row(), index.column(), model()->data(index).to_string().characters()); dbgprintf("context menu requested for index (%d,%d) '%s'\n", index.row(), index.column(), model()->data(index).to_string().characters());
model()->set_selected_index(index); selection().set(index);
update(); update();
if (on_context_menu_request) if (on_context_menu_request)
on_context_menu_request(index, event); on_context_menu_request(index, event);