summaryrefslogtreecommitdiffstats
path: root/qt-ui/divelistview.cpp
diff options
context:
space:
mode:
authorGravatar Tomaz Canabrava <tcanabrava@kde.org>2013-11-26 15:44:18 -0200
committerGravatar Dirk Hohndel <dirk@hohndel.org>2013-11-26 11:25:50 -0800
commite175b1d1ab76ee8edcd3beb1c027a58a336af96e (patch)
treef2b296d4bc9b80f0bc79cf6ed761f57d844d17c6 /qt-ui/divelistview.cpp
parente91a1fc26c6c0ac2efee0a13e7ab29b3caa61f0c (diff)
downloadsubsurface-e175b1d1ab76ee8edcd3beb1c027a58a336af96e.tar.gz
Remember Trip Selection.
This patch remembers the trip selection across the Dive Tree Model. It's a tiny bit big because we used to have a variable 'selected trips' that's now calculed dynamically - this is more future proof. This is a start of Un-cluttering the view ( for 4.1 I hope to reduce the code in this class to nearly a half. ) Fixes #303 Signed-off-by: Tomaz Canabrava <tcanabrava@kde.org> Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
Diffstat (limited to 'qt-ui/divelistview.cpp')
-rw-r--r--qt-ui/divelistview.cpp56
1 files changed, 46 insertions, 10 deletions
diff --git a/qt-ui/divelistview.cpp b/qt-ui/divelistview.cpp
index e646bab04..d8538a5fd 100644
--- a/qt-ui/divelistview.cpp
+++ b/qt-ui/divelistview.cpp
@@ -47,8 +47,6 @@ DiveListView::DiveListView(QWidget *parent) : QTreeView(parent), mouseClickSelec
searchBox->hide();
connect(showSearchBox, SIGNAL(triggered(bool)), this, SLOT(showSearchEdit()));
connect(searchBox, SIGNAL(textChanged(QString)), model, SLOT(setFilterFixedString(QString)));
- selectedTrips.clear();
-
setupUi();
}
@@ -133,24 +131,66 @@ void DiveListView::rememberSelection()
continue;
struct dive *d = (struct dive *) index.data(DiveTripModel::DIVE_ROLE).value<void*>();
if (d)
- selectedDives.push_front(get_divenr(d));
+ selectedDives.insert(d->divetrip, get_divenr(d));
}
}
void DiveListView::restoreSelection()
{
unselectDives();
- selectedTrips.clear(); // I wish we didn't lose those...
- Q_FOREACH(int i, selectedDives) {
- selectDive(i);
+ Q_FOREACH(dive_trip_t *trip, selectedDives.keys()){
+ QList<int> divesOnTrip = getDivesInTrip(trip);
+ QList<int> selectedDivesOnTrip = selectedDives.values(trip);
+
+ // Trip was not selected, let's select single-dives.
+ if (trip == NULL || divesOnTrip.count() != selectedDivesOnTrip.count()){
+ Q_FOREACH(int i, selectedDivesOnTrip){
+ selectDive(i);
+ }
+ }else{
+ selectTrip(trip);
+ Q_FOREACH(int i, selectedDivesOnTrip){
+ selectDive(i);
+ }
+ }
}
}
+void DiveListView::selectTrip ( dive_trip_t* trip )
+{
+ if (!trip)
+ return;
+
+ QSortFilterProxyModel *m = qobject_cast<QSortFilterProxyModel*>(model());
+ QModelIndexList match = m->match(m->index(0,0), DiveTripModel::TRIP_ROLE, QVariant::fromValue<void*>(trip), 2, Qt::MatchRecursive);
+ QItemSelectionModel::SelectionFlags flags;
+ if (!match.count())
+ return;
+ QModelIndex idx = match.first();
+ flags = QItemSelectionModel::Select;
+ flags |= QItemSelectionModel::Rows;
+ selectionModel()->select(idx, flags);
+ expand(idx);
+}
+
void DiveListView::unselectDives()
{
selectionModel()->clearSelection();
}
+QList< dive_trip_t* > DiveListView::selectedTrips()
+{
+ QModelIndexList indexes = selectionModel()->selectedRows();
+ QList<dive_trip_t*> ret;
+ Q_FOREACH(const QModelIndex& index, indexes){
+ dive_trip_t *trip = static_cast<dive_trip_t*>(index.data(DiveTripModel::TRIP_ROLE).value<void*>());
+ if(!trip)
+ continue;
+ ret.push_back(trip);
+ }
+ return ret;
+}
+
void DiveListView::selectDive(int i, bool scrollto, bool toggle)
{
if( i == -1)
@@ -347,8 +387,6 @@ void DiveListView::selectionChanged(const QItemSelection& selected, const QItemS
if (!dive) { // it's a trip!
if (model->rowCount(index)) {
struct dive *child = (struct dive*) model->data(index.child(0,0), DiveTripModel::DIVE_ROLE).value<void*>();
- if (child && child->divetrip)
- selectedTrips.remove(child->divetrip);
while (child) {
deselect_dive(get_divenr(child));
child = child->next;
@@ -368,8 +406,6 @@ void DiveListView::selectionChanged(const QItemSelection& selected, const QItemS
if (model->rowCount(index)) {
QItemSelection selection;
struct dive *child = (struct dive*) model->data(index.child(0,0), DiveTripModel::DIVE_ROLE).value<void*>();
- if (child && child->divetrip)
- selectedTrips.insert(child->divetrip);
while (child) {
select_dive(get_divenr(child));
child = child->next;