LibGUI: Move AbstractTableView::keydown_event() down to TableView

We can't really share this stuff with TreeView anyway, since tables
and trees have very different spatial relationships between indexes.
This commit is contained in:
Andreas Kling 2020-05-21 13:40:52 +02:00
parent 9d9a31384e
commit 80f43ffc27
Notes: sideshowbarker 2024-07-19 06:17:30 +09:00
4 changed files with 44 additions and 43 deletions

View file

@ -447,48 +447,6 @@ void AbstractTableView::move_selection(int steps)
}
}
void AbstractTableView::keydown_event(KeyEvent& event)
{
if (!model())
return;
auto& model = *this->model();
if (event.key() == KeyCode::Key_Return) {
activate_selected();
return;
}
if (event.key() == KeyCode::Key_Up) {
move_selection(-1);
return;
}
if (event.key() == KeyCode::Key_Down) {
move_selection(1);
return;
}
if (event.key() == KeyCode::Key_PageUp) {
int items_per_page = visible_content_rect().height() / item_height();
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)) {
selection().set(new_index);
scroll_into_view(new_index, Orientation::Vertical);
update();
}
return;
}
if (event.key() == KeyCode::Key_PageDown) {
int items_per_page = visible_content_rect().height() / item_height();
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)) {
selection().set(new_index);
scroll_into_view(new_index, Orientation::Vertical);
update();
}
return;
}
return Widget::keydown_event(event);
}
void AbstractTableView::scroll_into_view(const ModelIndex& index, Orientation orientation)
{
auto rect = row_rect(index.row()).translated(0, -header_height());

View file

@ -83,7 +83,6 @@ protected:
virtual void mousedown_event(MouseEvent&) override;
virtual void mousemove_event(MouseEvent&) override;
virtual void doubleclick_event(MouseEvent&) override;
virtual void keydown_event(KeyEvent&) override;
virtual void leave_event(Core::Event&) override;
virtual void context_menu_event(ContextMenuEvent&) override;

View file

@ -155,4 +155,47 @@ void TableView::paint_event(PaintEvent& event)
paint_headers(painter);
}
void TableView::keydown_event(KeyEvent& event)
{
if (!model())
return;
auto& model = *this->model();
if (event.key() == KeyCode::Key_Return) {
activate_selected();
return;
}
if (event.key() == KeyCode::Key_Up) {
move_selection(-1);
return;
}
if (event.key() == KeyCode::Key_Down) {
move_selection(1);
return;
}
if (event.key() == KeyCode::Key_PageUp) {
int items_per_page = visible_content_rect().height() / item_height();
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)) {
selection().set(new_index);
scroll_into_view(new_index, Orientation::Vertical);
update();
}
return;
}
if (event.key() == KeyCode::Key_PageDown) {
int items_per_page = visible_content_rect().height() / item_height();
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)) {
selection().set(new_index);
scroll_into_view(new_index, Orientation::Vertical);
update();
}
return;
}
return Widget::keydown_event(event);
}
}

View file

@ -38,6 +38,7 @@ public:
protected:
TableView();
virtual void keydown_event(KeyEvent&) override;
virtual void paint_event(PaintEvent&) override;
};