diff options
author | Dirk Hohndel <dirk@hohndel.org> | 2013-05-12 06:54:34 -0700 |
---|---|---|
committer | Dirk Hohndel <dirk@hohndel.org> | 2013-05-12 06:58:56 -0700 |
commit | c729ae92ed1ea444ed2c9a38178ebe4cb22e54c4 (patch) | |
tree | 82a933a1a9b0022721cc7dfc531648d27b0a6e6c /qt-ui | |
parent | a180af38797f23b975bc092cf75e9703c4c93921 (diff) | |
download | subsurface-c729ae92ed1ea444ed2c9a38178ebe4cb22e54c4.tar.gz |
Allow walking the dive list with the cursor keys
Figure out what is our first selected element (in case we start out from a
multiple selection) and then move to the next logical element. So the code
traverses an expanded tree (from a trip 'down' to its first dive or 'up'
to the last dive of the previous trip - and similar from a first dive in a
trip 'up' to its trip and from a last dive in a trip 'down' to the next
trip.
This does not take 'shift-cursor-up/down' into account (i.e. manual
selection extension). Instead with just cursor up and down a single dive
(or single trip) is selected.
My guess is that the code will make someone's eyes bleed. Be warned.
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
Diffstat (limited to 'qt-ui')
-rw-r--r-- | qt-ui/mainwindow.cpp | 98 | ||||
-rw-r--r-- | qt-ui/mainwindow.h | 8 |
2 files changed, 106 insertions, 0 deletions
diff --git a/qt-ui/mainwindow.cpp b/qt-ui/mainwindow.cpp index 91de38a3f..3ba4e6881 100644 --- a/qt-ui/mainwindow.cpp +++ b/qt-ui/mainwindow.cpp @@ -43,6 +43,14 @@ MainWindow::MainWindow() : ui(new Ui::MainWindow()), ui->ListWidget->setCurrentIndex(sortModel->index(0,0, firstDiveOrTrip)); else ui->ListWidget->setCurrentIndex(firstDiveOrTrip); + QAction *actionNextDive = new QAction(this); + addAction(actionNextDive); + actionNextDive->setShortcut(Qt::Key_Down); + connect(actionNextDive, SIGNAL(triggered()), this, SLOT(nextDive_triggered())); + QAction *actionPreviousDive = new QAction(this); + addAction(actionPreviousDive); + actionPreviousDive->setShortcut(Qt::Key_Up); + connect(actionPreviousDive, SIGNAL(triggered()), this, SLOT(previousDive_triggered())); } void MainWindow::redrawProfile() @@ -50,6 +58,96 @@ void MainWindow::redrawProfile() ui->ProfileWidget->plot(get_dive(selected_dive)); } +void MainWindow::nextDive_triggered() +{ + // Get the current Selection: + QItemSelectionModel *m = ui->ListWidget->selectionModel(); + QModelIndexList selection = m->selectedRows(); + + if (!selection.size()) + return; + + // check if it's a dive or trip: + QModelIndex index = selection.first(); + struct dive *d = (struct dive*) index.data(TreeItemDT::DIVE_ROLE).value<void*>(); + const QAbstractItemModel *model = index.model(); + + QItemSelectionModel::SelectionFlags flags = (QItemSelectionModel::Select | QItemSelectionModel::Rows); + + if (d) { + // it's a dive. + QModelIndex trip = index.parent(); + + // checks if it's the last dive on a trip list: + if (index.row() == model->rowCount(trip) - 1) { + // selects a trip. + QModelIndex nexttrip = model->index(trip.row()+1, trip.column(), trip.parent()); + if (nexttrip.isValid()) { + m->clear(); + m->select(nexttrip, flags); + } + } else { + m->clear(); + m->select(model->index(index.row()+1, index.column(), index.parent()), flags); + } + } else { + // it's a trip (and we have no empty trips, so there is a first child) + QModelIndex child = index.child(0,0); + m->select(model->index(0, index.column(), index), flags); + } +} + +void MainWindow::previousDive_triggered() +{ + // Get the current Selection: + QItemSelectionModel *m = ui->ListWidget->selectionModel(); + QModelIndexList selection = m->selectedRows(); + + if (!selection.size()) + return; + + // check if it's a dive or trip: + QModelIndex index = selection.first(); + struct dive *d = (struct dive*) index.data(TreeItemDT::DIVE_ROLE).value<void*>(); + const QAbstractItemModel *model = index.model(); + + QItemSelectionModel::SelectionFlags flags = (QItemSelectionModel::Select | QItemSelectionModel::Rows); + + if (d) { + // it's a dive. + QModelIndex trip = index.parent(); + + // checks if it's the first dive on a trip list: + if (index.row() == 0) { + if (trip.isValid()) { + // select the trip this dive is in + m->clear(); + m->select(model->index(trip.row(), trip.column(), trip.parent()),flags); + } + } else { + // select the previous dive + m->clear(); + m->select(model->index(index.row() - 1, index.column(), index.parent()), flags); + } + } else { + // it's a trip. + if (index.row() != 0) { + QModelIndex prevtrip = index.sibling(index.row() - 1, 0); + if (!prevtrip.isValid()) + return; + int cnt = prevtrip.model()->rowCount(); + QModelIndex child = prevtrip.child(prevtrip.model()->rowCount() - 1, 0); + /* I don't understand why this gives me incorrect rowCount... */ + while(!child.isValid() && cnt > 0) + child = prevtrip.child(--cnt, 0); + if (!child.isValid()) + return; + m->clear(); + m->select(model->index(child.row(), index.column(), prevtrip), flags); + } + } +} + void MainWindow::on_actionNew_triggered() { qDebug("actionNew"); diff --git a/qt-ui/mainwindow.h b/qt-ui/mainwindow.h index b76199a8b..8c07d048c 100644 --- a/qt-ui/mainwindow.h +++ b/qt-ui/mainwindow.h @@ -9,6 +9,7 @@ #include <QMainWindow> #include <QModelIndex> +#include <QAction> class QSortFilterProxyModel; class DiveTripModel; @@ -68,6 +69,10 @@ private Q_SLOTS: void on_actionAboutSubsurface_triggered(); void on_actionUserManual_triggered(); + /* keyboard actions */ + void nextDive_triggered(); + void previousDive_triggered(); + void dive_selection_changed(const QItemSelection& newSelection, const QItemSelection& oldSelection); @@ -78,6 +83,9 @@ private: Ui::MainWindow *ui; DiveTripModel *model; QSortFilterProxyModel *sortModel; + QAction *actionNextDive; + QAction *actionPreviousDive; + QString filter(); bool askSaveChanges(); void readSettings(); |