diff options
author | Tomaz Canabrava <tcanabrava@kde.org> | 2013-11-16 18:41:47 -0200 |
---|---|---|
committer | Dirk Hohndel <dirk@hohndel.org> | 2013-11-16 20:19:55 -0800 |
commit | 4f9526ef8122f3496ee0ead986fcc10c500e0584 (patch) | |
tree | 29c6c838928c03bb47a404f3ff0c889a25bc7869 /qt-ui/divelistview.cpp | |
parent | c1a05fd03443ac9604684fa22a7efd5172bd2ffc (diff) | |
download | subsurface-4f9526ef8122f3496ee0ead986fcc10c500e0584.tar.gz |
Fix wrong handling of Dive Table, and revert some wrong changes.
This patch just reverts some wrong changes that I'v done on a
past commit ( sorry ) and correctly handles the selectDive,
by using a IDX instead of the dive pointer, as dirk told me
it's extremely error-prone since the pointer can change.
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.cpp | 76 |
1 files changed, 25 insertions, 51 deletions
diff --git a/qt-ui/divelistview.cpp b/qt-ui/divelistview.cpp index 7689f06a9..a34484dba 100644 --- a/qt-ui/divelistview.cpp +++ b/qt-ui/divelistview.cpp @@ -110,8 +110,10 @@ void DiveListView::backupExpandedRows(){ } void DiveListView::restoreExpandedRows(){ + setAnimated(false); Q_FOREACH(const int &i, expandedRows) setExpanded( model()->index(i, 0), true ); + setAnimated(true); } void DiveListView::fixMessyQtModelBehaviour() { @@ -139,9 +141,7 @@ void DiveListView::restoreSelection() { unselectDives(); Q_FOREACH(int i, selectedDives) { - struct dive *d = get_dive(i); - if (d) - selectDive(d); + selectDive(i); } } @@ -150,25 +150,18 @@ void DiveListView::unselectDives() selectionModel()->clearSelection(); } -void DiveListView::selectDive(struct dive *dive, bool scrollto, bool toggle) +void DiveListView::selectDive(int i, bool scrollto, bool toggle) { - if (dive == NULL) - return; QSortFilterProxyModel *m = qobject_cast<QSortFilterProxyModel*>(model()); - QModelIndexList match = m->match(m->index(0,0), DiveTripModel::NR, dive->number, 1, Qt::MatchRecursive); + QModelIndexList match = m->match(m->index(0,0), DiveTripModel::DIVE_IDX, i, 2, Qt::MatchRecursive); QItemSelectionModel::SelectionFlags flags; QModelIndex idx = match.first(); - - QModelIndex parent = idx.parent(); - if (parent.isValid()) - expand(parent); flags = toggle ? QItemSelectionModel::Toggle : QItemSelectionModel::Select; flags |= QItemSelectionModel::Rows; selectionModel()->select(idx, flags); if (scrollto) scrollTo(idx, PositionAtCenter); } - void DiveListView::showSearchEdit() { searchBox->show(); @@ -196,25 +189,9 @@ bool DiveListView::eventFilter(QObject* , QEvent* event) // index. TRIP_ROLE vs DIVE_ROLE? void DiveListView::headerClicked(int i) { - sortColumn = i; - QItemSelection oldSelection = selectionModel()->selection(); - QList<struct dive*> currentSelectedDives; - DiveTripModel::Layout newLayout; - bool first = true; - - newLayout = i == (int) DiveTripModel::NR ? DiveTripModel::TREE : DiveTripModel::LIST; - - Q_FOREACH(const QModelIndex& index , oldSelection.indexes()) { - if (index.column() != 0) // We only care about the dives, so, let's stick to rows and discard columns. - continue; - - struct dive *d = (struct dive *) index.data(DiveTripModel::DIVE_ROLE).value<void*>(); - if (d) - currentSelectedDives.push_back(d); - } - + DiveTripModel::Layout newLayout = i == (int) DiveTripModel::NR ? DiveTripModel::TREE : DiveTripModel::LIST; + rememberSelection(); unselectDives(); - /* No layout change? Just re-sort, and scroll to first selection, making sure all selections are expanded */ if (currentLayout == newLayout) { currentOrder = (currentOrder == Qt::DescendingOrder) ? Qt::AscendingOrder : Qt::DescendingOrder; @@ -231,12 +208,7 @@ void DiveListView::headerClicked(int i) restoreExpandedRows(); } } - - // repopulate the selections. - Q_FOREACH(struct dive *d, currentSelectedDives) { - selectDive(d, first); - first = false; - } + restoreSelection(); } void DiveListView::reload(DiveTripModel::Layout layout, bool forceSort) @@ -263,7 +235,7 @@ void DiveListView::reload(DiveTripModel::Layout layout, bool forceSort) sortByColumn(sortColumn, currentOrder); if (amount_selected && current_dive != NULL) { - selectDive(current_dive, true); + selectDive(selected_dive, true); } else { QModelIndex firstDiveOrTrip = m->index(0,0); if (firstDiveOrTrip.isValid()) { @@ -495,23 +467,25 @@ void DiveListView::deleteDive() struct dive *d = (struct dive *) contextMenuIndex.data(DiveTripModel::DIVE_ROLE).value<void*>(); if (!d) return; - - QSortFilterProxyModel *proxy = qobject_cast<QSortFilterProxyModel*>(model()); - DiveTripModel *realModel = qobject_cast<DiveTripModel*>(proxy->sourceModel()); - realModel->deleteSelectedDives(); - - struct dive* next_dive = 0; + // after a dive is deleted the ones following it move forward in the dive_table + // so instead of using the for_each_dive macro I'm using an explicit for loop + // to make this easier to understand + for (i = 0; i < dive_table.nr; i++) { + d = get_dive(i); + if (!d->selected) + continue; + delete_single_dive(i); + i--; // so the next dive isn't skipped... it's now #i + } if (amount_selected == 0) { - if (i > 0){ - next_dive = get_dive(nr -1); - } - else{ + if (i > 0) + select_dive(nr - 1); + else mainWindow()->cleanUpEmpty(); - } } mark_divelist_changed(TRUE); - if (next_dive) - selectDive(next_dive); + mainWindow()->refreshDisplay(); + reload(currentLayout, false); } void DiveListView::testSlot() @@ -563,7 +537,7 @@ void DiveListView::contextMenuEvent(QContextMenuEvent *event) QAction * actionTaken = popup.exec(event->globalPos()); if (actionTaken == collapseAction && collapseAction) { this->setAnimated(false); - selectDive(current_dive, true); + selectDive(selected_dive, true); scrollTo(selectedIndexes().first()); this->setAnimated(true); } |