diff options
Diffstat (limited to 'qt-ui/divelistview.cpp')
-rw-r--r-- | qt-ui/divelistview.cpp | 256 |
1 files changed, 88 insertions, 168 deletions
diff --git a/qt-ui/divelistview.cpp b/qt-ui/divelistview.cpp index 6fd5dee10..cbb75c102 100644 --- a/qt-ui/divelistview.cpp +++ b/qt-ui/divelistview.cpp @@ -9,6 +9,7 @@ #include "modeldelegates.h" #include "mainwindow.h" #include "subsurfacewebservices.h" +#include "divelogexportdialog.h" #include "../display.h" #include "exif.h" #include "../file.h" @@ -27,7 +28,8 @@ #include <iostream> #include "../qthelper.h" -DiveListView::DiveListView(QWidget *parent) : QTreeView(parent), mouseClickSelection(false), sortColumn(0), currentOrder(Qt::DescendingOrder), searchBox(this) +DiveListView::DiveListView(QWidget *parent) : QTreeView(parent), mouseClickSelection(false), sortColumn(0) + , currentOrder(Qt::DescendingOrder), searchBox(this), dontEmitDiveChangedSignal(false) { setItemDelegate(new DiveListDelegate(this)); setUniformRowHeights(true); @@ -57,7 +59,8 @@ 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))); - setupUi(); + // calling setupUi() here appears to be too early; it does NOT correctly set the column widths + // setupUi(); } DiveListView::~DiveListView() @@ -122,7 +125,7 @@ void DiveListView::backupExpandedRows() void DiveListView::restoreExpandedRows() { setAnimated(false); - Q_FOREACH(const int & i, expandedRows) + Q_FOREACH (const int &i, expandedRows) setExpanded(model()->index(i, 0), true); setAnimated(true); } @@ -139,7 +142,7 @@ void DiveListView::rememberSelection() { selectedDives.clear(); QItemSelection selection = selectionModel()->selection(); - Q_FOREACH(const QModelIndex & index, selection.indexes()) { + Q_FOREACH (const QModelIndex &index, selection.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 *>(); @@ -151,18 +154,18 @@ void DiveListView::rememberSelection() void DiveListView::restoreSelection() { unselectDives(); - Q_FOREACH(dive_trip_t * trip, selectedDives.keys()) { + 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) { + Q_FOREACH (int i, selectedDivesOnTrip) { selectDive(i); } } else { selectTrip(trip); - Q_FOREACH(int i, selectedDivesOnTrip) { + Q_FOREACH (int i, selectedDivesOnTrip) { selectDive(i); } } @@ -189,13 +192,20 @@ void DiveListView::selectTrip(dive_trip_t *trip) void DiveListView::unselectDives() { selectionModel()->clearSelection(); + // clearSelection should emit selectionChanged() but sometimes that + // appears not to happen + int i; + struct dive *dive; + for_each_dive(i, dive) { + deselect_dive(i); + } } QList<dive_trip_t *> DiveListView::selectedTrips() { QModelIndexList indexes = selectionModel()->selectedRows(); QList<dive_trip_t *> ret; - Q_FOREACH(const QModelIndex & index, indexes) { + 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; @@ -211,6 +221,8 @@ void DiveListView::selectDive(int i, bool scrollto, bool toggle) QSortFilterProxyModel *m = qobject_cast<QSortFilterProxyModel *>(model()); QModelIndexList match = m->match(m->index(0, 0), DiveTripModel::DIVE_IDX, i, 2, Qt::MatchRecursive); QItemSelectionModel::SelectionFlags flags; + if (match.isEmpty()) + return; QModelIndex idx = match.first(); flags = toggle ? QItemSelectionModel::Toggle : QItemSelectionModel::Select; flags |= QItemSelectionModel::Rows; @@ -231,54 +243,24 @@ void DiveListView::selectDives(const QList<int> &newDiveSelection) if (!newDiveSelection.count()) return; - disconnect(selectionModel(), SIGNAL(selectionChanged(QItemSelection, QItemSelection)), - this, SLOT(selectionChanged(QItemSelection, QItemSelection))); - disconnect(selectionModel(), SIGNAL(currentChanged(QModelIndex, QModelIndex)), - this, SLOT(currentChanged(QModelIndex, QModelIndex))); + dontEmitDiveChangedSignal = true; + // select the dives, highest index first - this way the oldest of the dives + // becomes the selected_dive that we scroll to + QList<int> sortedSelection = newDiveSelection; + qSort(sortedSelection.begin(), sortedSelection.end()); + while (!sortedSelection.isEmpty()) + selectDive(sortedSelection.takeLast()); - setAnimated(false); - collapseAll(); QSortFilterProxyModel *m = qobject_cast<QSortFilterProxyModel *>(model()); - QItemSelectionModel::SelectionFlags flags = QItemSelectionModel::Select | QItemSelectionModel::Rows; - - QItemSelection newDeselected = selectionModel()->selection(); - QModelIndexList diveList; - - //TODO: This should be called find_first_selected_dive and be ported to C code. - int firstSelectedDive = -1; - /* context for temp. variables. */ { - int i = 0; - struct dive *dive; - for_each_dive(i, dive) { - dive->selected = newDiveSelection.contains(i) == true; - if (firstSelectedDive == -1 && dive->selected) { - firstSelectedDive = i; - break; - } - } - } - select_dive(firstSelectedDive); - Q_FOREACH(int i, newDiveSelection) { - diveList.append(m->match(m->index(0, 0), DiveTripModel::DIVE_IDX, - i, 2, Qt::MatchRecursive).first()); - } - Q_FOREACH(const QModelIndex & idx, diveList) { - selectionModel()->select(idx, flags); - if (idx.parent().isValid() && !isExpanded(idx.parent())) { - expand(idx.parent()); - } - } - setAnimated(true); - QTreeView::selectionChanged(selectionModel()->selection(), newDeselected); - connect(selectionModel(), SIGNAL(selectionChanged(QItemSelection, QItemSelection)), - this, SLOT(selectionChanged(QItemSelection, QItemSelection))); - connect(selectionModel(), SIGNAL(currentChanged(QModelIndex, QModelIndex)), - this, SLOT(currentChanged(QModelIndex, QModelIndex))); - Q_EMIT currentDiveChanged(selected_dive); QModelIndex idx = m->match(m->index(0, 0), DiveTripModel::DIVE_IDX, selected_dive, 2, Qt::MatchRecursive).first(); if (idx.parent().isValid()) scrollTo(idx.parent()); scrollTo(idx); + + // now that everything is up to date, update the widgets + Q_EMIT currentDiveChanged(selected_dive); + dontEmitDiveChangedSignal = false; + return; } void DiveListView::showSearchEdit() @@ -328,10 +310,19 @@ void DiveListView::headerClicked(int i) } } restoreSelection(); + // remember the new sort column + sortColumn = i; } void DiveListView::reload(DiveTripModel::Layout layout, bool forceSort) { + // we want to run setupUi() once we actually are displaying something + // in the widget + static bool first = true; + if (first && dive_table.nr > 0) { + setupUi(); + first = false; + } if (layout == DiveTripModel::CURRENT) layout = currentLayout; else @@ -368,7 +359,6 @@ void DiveListView::reload(DiveTripModel::Layout layout, bool forceSort) setCurrentIndex(firstDiveOrTrip); } } - setupUi(); if (selectedIndexes().count()) { QModelIndex curr = selectedIndexes().first(); curr = curr.parent().isValid() ? curr.parent() : curr; @@ -396,7 +386,7 @@ void DiveListView::reloadHeaderActions() QString settingName = QString("showColumn%1").arg(i); QAction *a = new QAction(title, header()); bool showHeaderFirstRun = !( - i == DiveTripModel::MAXCNS || i == DiveTripModel::NITROX || i == DiveTripModel::OTU || i == DiveTripModel::TEMPERATURE || i == DiveTripModel::TOTALWEIGHT || i == DiveTripModel::SUIT || i == DiveTripModel::CYLINDER || i == DiveTripModel::SAC); + i == DiveTripModel::MAXCNS || i == DiveTripModel::NITROX || i == DiveTripModel::OTU || i == DiveTripModel::TEMPERATURE || i == DiveTripModel::TOTALWEIGHT || i == DiveTripModel::SUIT || i == DiveTripModel::CYLINDER || i == DiveTripModel::SAC); bool shown = s.value(settingName, showHeaderFirstRun).toBool(); a->setCheckable(true); a->setChecked(shown); @@ -445,39 +435,26 @@ void DiveListView::selectionChanged(const QItemSelection &selected, const QItemS disconnect(selectionModel(), SIGNAL(selectionChanged(QItemSelection, QItemSelection)), this, SLOT(selectionChanged(QItemSelection, QItemSelection))); disconnect(selectionModel(), SIGNAL(currentChanged(QModelIndex, QModelIndex)), this, SLOT(currentChanged(QModelIndex, QModelIndex))); - Q_FOREACH(const QModelIndex & index, newDeselected.indexes()) { + Q_FOREACH (const QModelIndex &index, newDeselected.indexes()) { if (index.column() != 0) continue; const QAbstractItemModel *model = index.model(); struct dive *dive = (struct dive *)model->data(index, DiveTripModel::DIVE_ROLE).value<void *>(); - if (!dive) { // it's a trip! - //TODO: deselect_trip_dives on c-code? - if (model->rowCount(index)) { - struct dive *child = (struct dive *)model->data(index.child(0, 0), DiveTripModel::DIVE_ROLE).value<void *>(); - while (child) { - deselect_dive(get_divenr(child)); - child = child->next; - } - } - } else { + if (!dive) // it's a trip! + deselect_dives_in_trip((dive_trip_t *)model->data(index, DiveTripModel::TRIP_ROLE).value<void *>()); + else deselect_dive(get_divenr(dive)); - } } - Q_FOREACH(const QModelIndex & index, newSelected.indexes()) { + Q_FOREACH (const QModelIndex &index, newSelected.indexes()) { if (index.column() != 0) continue; const QAbstractItemModel *model = index.model(); struct dive *dive = (struct dive *)model->data(index, DiveTripModel::DIVE_ROLE).value<void *>(); if (!dive) { // it's a trip! - //TODO: select_trip_dives on C code? if (model->rowCount(index)) { QItemSelection selection; - struct dive *child = (struct dive *)model->data(index.child(0, 0), DiveTripModel::DIVE_ROLE).value<void *>(); - while (child) { - select_dive(get_divenr(child)); - child = child->next; - } + select_dives_in_trip((dive_trip_t *)model->data(index, DiveTripModel::TRIP_ROLE).value<void *>()); selection.select(index.child(0, 0), index.child(model->rowCount(index) - 1, 0)); selectionModel()->select(selection, QItemSelectionModel::Select | QItemSelectionModel::Rows); selectionModel()->setCurrentIndex(index, QItemSelectionModel::Select | QItemSelectionModel::NoUpdate); @@ -491,8 +468,8 @@ void DiveListView::selectionChanged(const QItemSelection &selected, const QItemS QTreeView::selectionChanged(selectionModel()->selection(), newDeselected); connect(selectionModel(), SIGNAL(selectionChanged(QItemSelection, QItemSelection)), this, SLOT(selectionChanged(QItemSelection, QItemSelection))); connect(selectionModel(), SIGNAL(currentChanged(QModelIndex, QModelIndex)), this, SLOT(currentChanged(QModelIndex, QModelIndex))); - // now that everything is up to date, update the widgets - Q_EMIT currentDiveChanged(selected_dive); + if(!dontEmitDiveChangedSignal) + Q_EMIT currentDiveChanged(selected_dive); } static bool can_merge(const struct dive *a, const struct dive *b) @@ -512,7 +489,7 @@ void DiveListView::mergeDives() int i; struct dive *dive, *maindive = NULL; - for_each_dive(i, dive) { + for_each_dive (i, dive) { if (dive->selected) { if (!can_merge(maindive, dive)) { maindive = dive; @@ -525,6 +502,12 @@ void DiveListView::mergeDives() MainWindow::instance()->refreshDisplay(); } +void DiveListView::renumberDives() +{ + RenumberDialog::instance()->renumberOnlySelected(); + RenumberDialog::instance()->show(); +} + void DiveListView::merge_trip(const QModelIndex &a, int offset) { int i = a.row() + offset; @@ -532,17 +515,9 @@ void DiveListView::merge_trip(const QModelIndex &a, int offset) dive_trip_t *trip_a = (dive_trip_t *)a.data(DiveTripModel::TRIP_ROLE).value<void *>(); dive_trip_t *trip_b = (dive_trip_t *)b.data(DiveTripModel::TRIP_ROLE).value<void *>(); - // TODO: merge_trip on the C code? some part of this needs to stay ( getting the trips from the model, - // but not the algorithm. if (trip_a == trip_b || !trip_a || !trip_b) return; - - if (!trip_a->location && trip_b->location) - trip_a->location = strdup(trip_b->location); - if (!trip_a->notes && trip_b->notes) - trip_a->notes = strdup(trip_b->notes); - while (trip_b->dives) - add_dive_to_trip(trip_b->dives, trip_a); + combine_trips(trip_a, trip_b); rememberSelection(); reload(currentLayout, false); fixMessyQtModelBehaviour(); @@ -566,7 +541,7 @@ void DiveListView::removeFromTrip() //TODO: move this to C-code. int i; struct dive *d; - for_each_dive(i, d) { + for_each_dive (i, d) { if (d->selected) remove_dive_from_trip(d, false); } @@ -587,7 +562,7 @@ void DiveListView::newTripAbove() int idx; rememberSelection(); trip = create_and_hookup_trip_from_dive(d); - for_each_dive(idx, d) { + for_each_dive (idx, d) { if (d->selected) add_dive_to_trip(d, trip); } @@ -610,7 +585,7 @@ void DiveListView::addToTripAbove() void DiveListView::addToTrip(bool below) { - int delta = (currentOrder == Qt::AscendingOrder) ? -1 : +1; + int delta = (currentOrder == Qt::AscendingOrder) ? -1 : +1; struct dive *d = (struct dive *)contextMenuIndex.data(DiveTripModel::DIVE_ROLE).value<void *>(); rememberSelection(); @@ -623,18 +598,10 @@ void DiveListView::addToTrip(bool below) if (d->selected) { // we are right-clicking on one of possibly many selected dive(s) // find the top selected dive, depending on the list order - if (delta == 1) { - for_each_dive(idx, d) { - if (d->selected) - pd = d; - } - d = pd; // this way we have the chronologically last - } else { - for_each_dive(idx, d) { - if (d->selected) - break; // now that's the chronologically first - } - } + if (delta == 1) + d = last_selected_dive(); + else + d = first_selected_dive(); } // now find the trip "above" in the dive list if ((pd = get_dive(get_divenr(d) + delta)) != NULL) { @@ -645,7 +612,7 @@ void DiveListView::addToTrip(bool below) return; add_dive_to_trip(d, trip); if (d->selected) { // there are possibly other selected dives that we should add - for_each_dive(idx, d) { + for_each_dive (idx, d) { if (d->selected) add_dive_to_trip(d, trip); } @@ -664,7 +631,7 @@ void DiveListView::markDiveInvalid() struct dive *d = (struct dive *)contextMenuIndex.data(DiveTripModel::DIVE_ROLE).value<void *>(); if (!d) return; - for_each_dive(i, d) { + for_each_dive (i, d) { if (!d->selected) continue; //TODO: this should be done in the future @@ -696,8 +663,7 @@ void DiveListView::deleteDive() // so instead of using the for_each_dive macro I'm using an explicit for loop // to make this easier to understand int lastDiveNr = -1; - for (i = 0; i < dive_table.nr; i++) { - d = get_dive(i); + for_each_dive (i, d) { if (!d->selected) continue; delete_single_dive(i); @@ -747,8 +713,23 @@ void DiveListView::contextMenuEvent(QContextMenuEvent *event) if (d) { popup.addAction(tr("remove dive(s) from trip"), this, SLOT(removeFromTrip())); popup.addAction(tr("create new trip above"), this, SLOT(newTripAbove())); - popup.addAction(tr("add dive(s) to trip immediately above"), this, SLOT(addToTripAbove())); - popup.addAction(tr("add dive(s) to trip immediately below"), this, SLOT(addToTripBelow())); + if (!d->divetrip) { + struct dive *top = d; + struct dive *bottom = d; + if (d->selected) { + if (currentOrder == Qt::AscendingOrder) { + top = first_selected_dive(); + bottom = last_selected_dive(); + } else { + top = last_selected_dive(); + bottom = first_selected_dive(); + } + } + if (is_trip_before_after(top, (currentOrder == Qt::AscendingOrder))) + popup.addAction(tr("add dive(s) to trip immediately above"), this, SLOT(addToTripAbove())); + if (is_trip_before_after(bottom, (currentOrder == Qt::DescendingOrder))) + popup.addAction(tr("add dive(s) to trip immediately below"), this, SLOT(addToTripBelow())); + } } if (trip) { popup.addAction(tr("merge trip with trip above"), this, SLOT(mergeTripAbove())); @@ -764,14 +745,10 @@ void DiveListView::contextMenuEvent(QContextMenuEvent *event) if (amount_selected > 1 && consecutive_selected()) popup.addAction(tr("merge selected dives"), this, SLOT(mergeDives())); if (amount_selected >= 1) { - popup.addAction(tr("save As"), this, SLOT(saveSelectedDivesAs())); - popup.addAction(tr("export As UDDF"), this, SLOT(exportSelectedDivesAsUDDF())); - popup.addAction(tr("export As CSV"), this, SLOT(exportSelectedDivesAsCSV())); + popup.addAction(tr("renumber dive(s)"), this, SLOT(renumberDives())); popup.addAction(tr("shift times"), this, SLOT(shiftTimes())); popup.addAction(tr("load images"), this, SLOT(loadImages())); } - if (d) - popup.addAction(tr("upload dive(s) to divelogs.de"), this, SLOT(uploadToDivelogsDE())); // "collapse all" really closes all trips, // "collapse" keeps the trip with the selected dive open QAction *actionTaken = popup.exec(event->globalPos()); @@ -784,56 +761,6 @@ void DiveListView::contextMenuEvent(QContextMenuEvent *event) event->accept(); } -void DiveListView::saveSelectedDivesAs() -{ - QSettings settings; - QString lastDir = QDir::homePath(); - - settings.beginGroup("FileDialog"); - if (settings.contains("LastDir")) { - if (QDir::setCurrent(settings.value("LastDir").toString())) { - lastDir = settings.value("LastDir").toString(); - } - } - settings.endGroup(); - - QString fileName = QFileDialog::getSaveFileName(MainWindow::instance(), tr("Save Dives As..."), QDir::homePath()); - if (fileName.isEmpty()) - return; - - // Keep last open dir - QFileInfo fileInfo(fileName); - settings.beginGroup("FileDialog"); - settings.setValue("LastDir", fileInfo.dir().path()); - settings.endGroup(); - - QByteArray bt = QFile::encodeName(fileName); - save_dives_logic(bt.data(), true); -} - -void DiveListView::exportSelectedDivesAsUDDF() -{ - QString filename; - QFileInfo fi(system_default_filename()); - - filename = QFileDialog::getSaveFileName(this, tr("Export UDDF File as"), fi.absolutePath(), - tr("UDDF files (*.uddf *.UDDF)")); - if (!filename.isNull() && !filename.isEmpty()) - export_dives_xslt(filename.toUtf8(), true, "uddf-export.xslt"); -} - -void DiveListView::exportSelectedDivesAsCSV() -{ - QString filename; - QFileInfo fi(system_default_filename()); - - filename = QFileDialog::getSaveFileName(this, tr("Export CSV File as"), fi.absolutePath(), - tr("CSV files (*.csv *.CSV)")); - if (!filename.isNull() && !filename.isEmpty()) - export_dives_xslt(filename.toUtf8(), true, "xml2csv.xslt"); -} - - void DiveListView::shiftTimes() { ShiftTimesDialog::instance()->show(); @@ -874,11 +801,10 @@ void DiveListView::loadImages() imagetime += shiftDialog.amount(); // TODO: this should be cached and passed to the C-function int j = 0; struct dive *dive; - for_each_dive(j, dive) { + for_each_dive (j, dive) { if (!dive->selected) continue; - dc = &(dive->dc); - while (dc) { + for_each_dc (dive, dc) { when = dc->when ? dc->when : dive->when; duration_s = dc->duration.seconds ? dc->duration.seconds : dive->duration.seconds; if (when - 3600 < imagetime && when + duration_s + 3600 > imagetime) { @@ -899,17 +825,11 @@ void DiveListView::loadImages() MainWindow::instance()->refreshDisplay(); MainWindow::instance()->graphics()->replot(); } - dc = dc->next; } } } } -void DiveListView::uploadToDivelogsDE() -{ - DivelogsDeWebServices::instance()->prepareDivesForUpload(); -} - QString DiveListView::lastUsedImageDir() { QSettings settings; |