diff options
author | Berthold Stoeger <bstoeger@mail.tuwien.ac.at> | 2019-06-23 09:22:26 +0200 |
---|---|---|
committer | bstoeger <32835590+bstoeger@users.noreply.github.com> | 2019-06-23 20:08:46 +0200 |
commit | 27944a52b1c2a1c68ccfe88c4a84d3f74fb8b512 (patch) | |
tree | 160fef9677e5cc2e907d4c32448553f74df4d2db | |
parent | cbcddaa396f6668fef7750eb2721bc70ca11d0e4 (diff) | |
download | subsurface-27944a52b1c2a1c68ccfe88c4a84d3f74fb8b512.tar.gz |
Undo: don't send signals batched by trip
Since the default view is batched by trips, signals were sent trip-wise.
This seemed like a good idea at first, but when more and more parts used
these signals, it became a burden. Therefore push the batching to the
part of the code where it is needed: the trip view.
The divesAdded and divesDeleted are not yet converted, because these
are combined with trip addition/deletion. This should also be detangled,
but not now.
Since the dive-lists were sorted in the processByTrip function, the
dive-list model now does its own sorting. This will have to be
audited.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
-rw-r--r-- | core/subsurface-qt/DiveListNotifier.h | 32 | ||||
-rw-r--r-- | desktop-widgets/command_divelist.cpp | 76 | ||||
-rw-r--r-- | desktop-widgets/command_divesite.cpp | 24 | ||||
-rw-r--r-- | desktop-widgets/command_edit.cpp | 52 | ||||
-rw-r--r-- | desktop-widgets/command_private.cpp | 16 | ||||
-rw-r--r-- | desktop-widgets/command_private.h | 44 | ||||
-rw-r--r-- | desktop-widgets/mapwidget.cpp | 3 | ||||
-rw-r--r-- | desktop-widgets/mapwidget.h | 2 | ||||
-rw-r--r-- | desktop-widgets/tab-widgets/TabDiveInformation.cpp | 3 | ||||
-rw-r--r-- | desktop-widgets/tab-widgets/TabDiveInformation.h | 2 | ||||
-rw-r--r-- | desktop-widgets/tab-widgets/maintab.cpp | 2 | ||||
-rw-r--r-- | desktop-widgets/tab-widgets/maintab.h | 2 | ||||
-rw-r--r-- | qt-models/cylindermodel.cpp | 3 | ||||
-rw-r--r-- | qt-models/cylindermodel.h | 2 | ||||
-rw-r--r-- | qt-models/divetripmodel.cpp | 89 | ||||
-rw-r--r-- | qt-models/divetripmodel.h | 21 | ||||
-rw-r--r-- | qt-models/weightmodel.cpp | 3 | ||||
-rw-r--r-- | qt-models/weightmodel.h | 2 |
18 files changed, 193 insertions, 185 deletions
diff --git a/core/subsurface-qt/DiveListNotifier.h b/core/subsurface-qt/DiveListNotifier.h index b7334287e..1d16652bd 100644 --- a/core/subsurface-qt/DiveListNotifier.h +++ b/core/subsurface-qt/DiveListNotifier.h @@ -37,39 +37,29 @@ enum class TripField { class DiveListNotifier : public QObject { Q_OBJECT signals: - - // Note that there are no signals for trips being added / created / time-shifted, - // because these events never happen without a dive being added / created / time-shifted. - - // We send one divesAdded, divesDeleted, divesChanged and divesTimeChanged, divesSelected - // signal per trip (non-associated dives being considered part of the null trip). This is - // ideal for the tree-view, but might be not-so-perfect for the list view, if trips intermingle - // or the deletion spans multiple trips. But most of the time only dives of a single trip - // will be affected and trips don't overlap, so these considerations are moot. - // Notes: - // - The dives are always sorted by according to the dives_less_than() function of the core. - // - The "trip" arguments are null for top-level-dives. + // Note that there are no signals for trips being added and created + // because these events never happen without a dive being added, removed or moved. + // The dives are always sorted according to the dives_less_than() function of the core. void divesAdded(dive_trip *trip, bool addTrip, const QVector<dive *> &dives); void divesDeleted(dive_trip *trip, bool deleteTrip, const QVector<dive *> &dives); - void divesChanged(dive_trip *trip, const QVector<dive *> &dives, DiveField field); void divesMovedBetweenTrips(dive_trip *from, dive_trip *to, bool deleteFrom, bool createTo, const QVector<dive *> &dives); - void divesTimeChanged(dive_trip *trip, timestamp_t delta, const QVector<dive *> &dives); + void divesChanged(const QVector<dive *> &dives, DiveField field); + void divesTimeChanged(timestamp_t delta, const QVector<dive *> &dives); - void cylindersReset(dive_trip *trip, const QVector<dive *> &dives); - void weightsystemsReset(dive_trip *trip, const QVector<dive *> &dives); + void cylindersReset(const QVector<dive *> &dives); + void weightsystemsReset(const QVector<dive *> &dives); // Trip edited signal void tripChanged(dive_trip *trip, TripField field); // Selection-signals come in two kinds: - // - divesSelected, divesDeselected and currentDiveChanged are finer grained and are - // called batch-wise per trip (except currentDiveChanged, of course). These signals - // are used by the dive-list model and view to correctly highlight the correct dives. + // - divesSelected, divesDeselected and currentDiveChanged are are used by the dive-list + // model and view to correctly highlight the correct dives. // - selectionChanged() is called once at the end of commands if either the selection // or the current dive changed. It is used by the main-window / profile to update // their data. - void divesSelected(dive_trip *trip, const QVector<dive *> &dives); - void divesDeselected(dive_trip *trip, const QVector<dive *> &dives); + void divesSelected(const QVector<dive *> &dives); + void divesDeselected(const QVector<dive *> &dives); void currentDiveChanged(); void selectionChanged(); diff --git a/desktop-widgets/command_divelist.cpp b/desktop-widgets/command_divelist.cpp index d2cd0a413..4c699fdd6 100644 --- a/desktop-widgets/command_divelist.cpp +++ b/desktop-widgets/command_divelist.cpp @@ -89,6 +89,37 @@ dive *DiveListBase::addDive(DiveToAdd &d) return res; } +// Some signals are sent in batches per trip. To avoid writing the same loop +// twice, this template takes a vector of trip / dive pairs, sorts it +// by trip and then calls a function-object with trip and a QVector of dives in that trip. +// The dives are sorted by the dive_less_than() function defined in the core. +// Input parameters: +// - dives: a vector of trip,dive pairs, which will be sorted and processed in batches by trip. +// - action: a function object, taking a trip-pointer and a QVector of dives, which will be called for each batch. +template<typename Function> +void processByTrip(std::vector<std::pair<dive_trip *, dive *>> &dives, Function action) +{ + // Sort lexicographically by trip then according to the dive_less_than() function. + std::sort(dives.begin(), dives.end(), + [](const std::pair<dive_trip *, dive *> &e1, const std::pair<dive_trip *, dive *> &e2) + { return e1.first == e2.first ? dive_less_than(e1.second, e2.second) : e1.first < e2.first; }); + + // Then, process the dives in batches by trip + size_t i, j; // Begin and end of batch + for (i = 0; i < dives.size(); i = j) { + dive_trip *trip = dives[i].first; + for (j = i + 1; j < dives.size() && dives[j].first == trip; ++j) + ; // pass + // Copy dives into a QVector. Some sort of "range_view" would be ideal, but Qt doesn't work this way. + QVector<dive *> divesInTrip(j - i); + for (size_t k = i; k < j; ++k) + divesInTrip[k - i] = dives[k].second; + + // Finally, emit the signal + action(trip, divesInTrip); + } +} + // This helper function calls removeDive() on a list of dives to be removed and // returns a vector of corresponding DiveToAdd objects, which can later be readded. // Moreover, a vector of deleted trips is returned, if trips became empty. @@ -143,8 +174,10 @@ DivesAndSitesToRemove DiveListBase::addDives(DivesAndTripsToAdd &toAdd) { std::vector<dive *> res; std::vector<dive_site *> sites; + std::vector<std::pair<dive_trip *, dive *>> dives; res.resize(toAdd.dives.size()); sites.reserve(toAdd.sites.size()); + dives.reserve(toAdd.sites.size()); // Make sure that the dive list is sorted. The added dives will be sent in a signal // and the recipients assume that the dives are sorted the same way as they are @@ -157,8 +190,10 @@ DivesAndSitesToRemove DiveListBase::addDives(DivesAndTripsToAdd &toAdd) // Note: the idiomatic STL-way would be std::transform, but let's use a loop since // that is closer to classical C-style. auto it2 = res.rbegin(); - for (auto it = toAdd.dives.rbegin(); it != toAdd.dives.rend(); ++it, ++it2) + for (auto it = toAdd.dives.rbegin(); it != toAdd.dives.rend(); ++it, ++it2) { *it2 = addDive(*it); + dives.push_back({ (*it2)->divetrip, *it2 }); + } toAdd.dives.clear(); // If the dives belong to new trips, add these as well. @@ -180,7 +215,7 @@ DivesAndSitesToRemove DiveListBase::addDives(DivesAndTripsToAdd &toAdd) toAdd.sites.clear(); // Send signals by trip. - processByTrip(res, [&](dive_trip *trip, const QVector<dive *> &divesInTrip) { + processByTrip(dives, [&](dive_trip *trip, const QVector<dive *> &divesInTrip) { // Now, let's check if this trip is supposed to be created, by checking if it was marked as "add it". bool createTrip = trip && std::find(addedTrips.begin(), addedTrips.end(), trip) != addedTrips.end(); // Finally, emit the signal @@ -191,30 +226,21 @@ DivesAndSitesToRemove DiveListBase::addDives(DivesAndTripsToAdd &toAdd) // This helper function renumbers dives according to an array of id/number pairs. // The old numbers are stored in the array, thus calling this function twice has no effect. -// TODO: switch from uniq-id to indices once all divelist-actions are controlled by undo-able commands static void renumberDives(QVector<QPair<dive *, int>> &divesToRenumber) { + QVector<dive *> dives; + dives.reserve(divesToRenumber.size()); for (auto &pair: divesToRenumber) { dive *d = pair.first; if (!d) continue; std::swap(d->number, pair.second); + dives.push_back(d); invalidate_dive_cache(d); } - // Emit changed signals per trip. - // First, collect all dives and sort by trip - std::vector<std::pair<dive_trip *, dive *>> dives; - dives.reserve(divesToRenumber.size()); - for (const auto &pair: divesToRenumber) { - dive *d = pair.first; - dives.push_back({ d->divetrip, d }); - } - // Send signals. - processByTrip(dives, [&](dive_trip *trip, const QVector<dive *> &divesInTrip) { - emit diveListNotifier.divesChanged(trip, divesInTrip, DiveField::NR); - }); + emit diveListNotifier.divesChanged(dives, DiveField::NR); } // This helper function moves a dive to a trip. The old trip is recorded in the @@ -571,20 +597,22 @@ ShiftTime::ShiftTime(const QVector<dive *> &changedDives, int amount) void ShiftTime::redoit() { - for (dive *d: diveList) + std::vector<dive_trip *> trips; + for (dive *d: diveList) { d->when += timeChanged; + if (d->divetrip && std::find(trips.begin(), trips.end(), d->divetrip) == trips.end()) + trips.push_back(d->divetrip); + } - // Changing times may have unsorted the dive table + // Changing times may have unsorted the dive and trip tables sort_dive_table(&dive_table); sort_trip_table(&trip_table); + for (dive_trip *trip: trips) + sort_dive_table(&trip->dives); // Keep the trip-table in order - // Send signals per trip (see comments in DiveListNotifier.h) and sort tables. - processByTrip(diveList, [&](dive_trip *trip, const QVector<dive *> &divesInTrip) { - if (trip) - sort_dive_table(&trip->dives); // Keep the trip-table in order - emit diveListNotifier.divesTimeChanged(trip, timeChanged, divesInTrip); - emit diveListNotifier.divesChanged(trip, divesInTrip, DiveField::DATETIME); - }); + // Send signals + emit diveListNotifier.divesTimeChanged(timeChanged, diveList); + emit diveListNotifier.divesChanged(diveList, DiveField::DATETIME); // Negate the time-shift so that the next call does the reverse timeChanged = -timeChanged; diff --git a/desktop-widgets/command_divesite.cpp b/desktop-widgets/command_divesite.cpp index 386159a7a..3ef4231f8 100644 --- a/desktop-widgets/command_divesite.cpp +++ b/desktop-widgets/command_divesite.cpp @@ -18,7 +18,7 @@ namespace Command { static std::vector<dive_site *> addDiveSites(std::vector<OwningDiveSitePtr> &sites) { std::vector<dive_site *> res; - std::vector<dive *> changedDives; + QVector<dive *> changedDives; res.reserve(sites.size()); for (OwningDiveSitePtr &ds: sites) { @@ -36,9 +36,7 @@ static std::vector<dive_site *> addDiveSites(std::vector<OwningDiveSitePtr> &sit emit diveListNotifier.diveSiteAdded(res.back(), idx); // Inform frontend of new dive site. } - processByTrip(changedDives, [&](dive_trip *trip, const QVector<dive *> &divesInTrip) { - emit diveListNotifier.divesChanged(trip, divesInTrip, DiveField::DIVESITE); - }); + emit diveListNotifier.divesChanged(changedDives, DiveField::DIVESITE); // Clear vector of unused owning pointers sites.clear(); @@ -52,7 +50,7 @@ static std::vector<dive_site *> addDiveSites(std::vector<OwningDiveSitePtr> &sit static std::vector<OwningDiveSitePtr> removeDiveSites(std::vector<dive_site *> &sites) { std::vector<OwningDiveSitePtr> res; - std::vector<dive *> changedDives; + QVector<dive *> changedDives; res.reserve(sites.size()); for (dive_site *ds: sites) { @@ -69,9 +67,7 @@ static std::vector<OwningDiveSitePtr> removeDiveSites(std::vector<dive_site *> & emit diveListNotifier.diveSiteDeleted(ds, idx); // Inform frontend of removed dive site. } - processByTrip(changedDives, [&](dive_trip *trip, const QVector<dive *> &divesInTrip) { - emit diveListNotifier.divesChanged(trip, divesInTrip, DiveField::DIVESITE); - }); + emit diveListNotifier.divesChanged(changedDives, DiveField::DIVESITE); sites.clear(); @@ -363,7 +359,7 @@ void MergeDiveSites::redo() sitesToAdd = std::move(removeDiveSites(sitesToRemove)); // Remember which dives changed so that we can send a single dives-edited signal - std::vector<dive *> divesChanged; + QVector<dive *> divesChanged; // The dives of the above dive sites were reset to no dive sites. // Add them to the merged-into dive site. Thankfully, we remember @@ -374,15 +370,13 @@ void MergeDiveSites::redo() divesChanged.push_back(site->dives.dives[i]); } } - processByTrip(divesChanged, [&](dive_trip *trip, const QVector<dive *> &divesInTrip) { - emit diveListNotifier.divesChanged(trip, divesInTrip, DiveField::DIVESITE); - }); + emit diveListNotifier.divesChanged(divesChanged, DiveField::DIVESITE); } void MergeDiveSites::undo() { // Remember which dives changed so that we can send a single dives-edited signal - std::vector<dive *> divesChanged; + QVector<dive *> divesChanged; // Before readding the dive sites, unregister the corresponding dives so that they can be // readded to their old dive sites. @@ -395,9 +389,7 @@ void MergeDiveSites::undo() sitesToRemove = std::move(addDiveSites(sitesToAdd)); - processByTrip(divesChanged, [&](dive_trip *trip, const QVector<dive *> &divesInTrip) { - emit diveListNotifier.divesChanged(trip, divesInTrip, DiveField::DIVESITE); - }); + emit diveListNotifier.divesChanged(divesChanged, DiveField::DIVESITE); } } // namespace Command diff --git a/desktop-widgets/command_edit.cpp b/desktop-widgets/command_edit.cpp index 89b864f60..5112043d2 100644 --- a/desktop-widgets/command_edit.cpp +++ b/desktop-widgets/command_edit.cpp @@ -97,9 +97,7 @@ void EditBase<T>::undo() // Send signals. DiveField id = fieldId(); - processByTrip(dives, [&](dive_trip *trip, const QVector<dive *> &divesInTrip) { - emit diveListNotifier.divesChanged(trip, divesInTrip, id); - }); + emit diveListNotifier.divesChanged(QVector<dive *>::fromStdVector(dives), id); if (setSelection(selectedDives, current)) emit diveListNotifier.selectionChanged(); // If the selection changed -> tell the frontend @@ -539,9 +537,7 @@ void EditTagsBase::undo() // Send signals. DiveField id = fieldId(); - processByTrip(dives, [&](dive_trip *trip, const QVector<dive *> &divesInTrip) { - emit diveListNotifier.divesChanged(trip, divesInTrip, id); - }); + emit diveListNotifier.divesChanged(QVector<dive *>::fromStdVector(dives), id); if (setSelection(selectedDives, current)) emit diveListNotifier.selectionChanged(); // If the selection changed -> tell the frontend @@ -750,7 +746,7 @@ void PasteDives::undo() } ownedDiveSites.clear(); - std::vector<dive *> divesToNotify; // Remember dives so that we can send signals later + QVector<dive *> divesToNotify; // Remember dives so that we can send signals later divesToNotify.reserve(dives.size()); for (PasteState &state: dives) { divesToNotify.push_back(state.d); @@ -780,28 +776,26 @@ void PasteDives::undo() // TODO: We send one signal per changed field. This means that the dive list may // update the entry numerous times. Perhaps change the field-id into flags? // There seems to be a number of enums / flags describing dive fields. Perhaps unify them all? - processByTrip(divesToNotify, [&](dive_trip *trip, const QVector<dive *> &divesInTrip) { - if (what.notes) - emit diveListNotifier.divesChanged(trip, divesInTrip, DiveField::NOTES); - if (what.divemaster) - emit diveListNotifier.divesChanged(trip, divesInTrip, DiveField::DIVEMASTER); - if (what.buddy) - emit diveListNotifier.divesChanged(trip, divesInTrip, DiveField::BUDDY); - if (what.suit) - emit diveListNotifier.divesChanged(trip, divesInTrip, DiveField::SUIT); - if (what.rating) - emit diveListNotifier.divesChanged(trip, divesInTrip, DiveField::RATING); - if (what.visibility) - emit diveListNotifier.divesChanged(trip, divesInTrip, DiveField::VISIBILITY); - if (what.divesite) - emit diveListNotifier.divesChanged(trip, divesInTrip, DiveField::DIVESITE); - if (what.tags) - emit diveListNotifier.divesChanged(trip, divesInTrip, DiveField::TAGS); - if (what.cylinders) - emit diveListNotifier.cylindersReset(trip, divesInTrip); - if (what.weights) - emit diveListNotifier.weightsystemsReset(trip, divesInTrip); - }); + if (what.notes) + emit diveListNotifier.divesChanged(divesToNotify, DiveField::NOTES); + if (what.divemaster) + emit diveListNotifier.divesChanged(divesToNotify, DiveField::DIVEMASTER); + if (what.buddy) + emit diveListNotifier.divesChanged(divesToNotify, DiveField::BUDDY); + if (what.suit) + emit diveListNotifier.divesChanged(divesToNotify, DiveField::SUIT); + if (what.rating) + emit diveListNotifier.divesChanged(divesToNotify, DiveField::RATING); + if (what.visibility) + emit diveListNotifier.divesChanged(divesToNotify, DiveField::VISIBILITY); + if (what.divesite) + emit diveListNotifier.divesChanged(divesToNotify, DiveField::DIVESITE); + if (what.tags) + emit diveListNotifier.divesChanged(divesToNotify, DiveField::TAGS); + if (what.cylinders) + emit diveListNotifier.cylindersReset(divesToNotify); + if (what.weights) + emit diveListNotifier.weightsystemsReset(divesToNotify); if (diveSiteListChanged) MapWidget::instance()->reload(); diff --git a/desktop-widgets/command_private.cpp b/desktop-widgets/command_private.cpp index 05947cac1..7e604dfc9 100644 --- a/desktop-widgets/command_private.cpp +++ b/desktop-widgets/command_private.cpp @@ -46,8 +46,8 @@ bool setSelection(const std::vector<dive *> &selection, dive *currentDive) { // To do so, generate vectors of dives to be selected and deselected. // We send signals batched by trip, so keep track of trip/dive pairs. - std::vector<std::pair<dive_trip *, dive *>> divesToSelect; - std::vector<std::pair<dive_trip *, dive *>> divesToDeselect; + QVector<dive *> divesToSelect; + QVector<dive *> divesToDeselect; // TODO: We might want to keep track of selected dives in a more efficient way! int i; @@ -73,20 +73,16 @@ bool setSelection(const std::vector<dive *> &selection, dive *currentDive) if (newState && !d->selected) { d->selected = true; ++amount_selected; - divesToSelect.push_back({ d->divetrip, d }); + divesToSelect.push_back(d); } else if (!newState && d->selected) { d->selected = false; - divesToDeselect.push_back({ d->divetrip, d }); + divesToDeselect.push_back(d); } } // Send the select and deselect signals - processByTrip(divesToSelect, [&](dive_trip *trip, const QVector<dive *> &divesInTrip) { - emit diveListNotifier.divesSelected(trip, divesInTrip); - }); - processByTrip(divesToDeselect, [&](dive_trip *trip, const QVector<dive *> &divesInTrip) { - emit diveListNotifier.divesDeselected(trip, divesInTrip); - }); + emit diveListNotifier.divesSelected(divesToSelect); + emit diveListNotifier.divesDeselected(divesToDeselect); bool currentDiveChanged = false; if (!currentDive) { diff --git a/desktop-widgets/command_private.h b/desktop-widgets/command_private.h index 1207e326f..6981b6e57 100644 --- a/desktop-widgets/command_private.h +++ b/desktop-widgets/command_private.h @@ -12,50 +12,6 @@ namespace Command { -// Generally, signals are sent in batches per trip. To avoid writing the same loop -// again and again, this template takes a vector of trip / dive pairs, sorts it -// by trip and then calls a function-object with trip and a QVector of dives in that trip. -// The dives are sorted by the dive_less_than() function defined in the core. -// Input parameters: -// - dives: a vector of trip,dive pairs, which will be sorted and processed in batches by trip. -// - action: a function object, taking a trip-pointer and a QVector of dives, which will be called for each batch. -template<typename Function> -void processByTrip(std::vector<std::pair<dive_trip *, dive *>> &dives, Function action) -{ - // Use std::tie for lexicographical sorting of trip, then start-time - std::sort(dives.begin(), dives.end(), - [](const std::pair<dive_trip *, dive *> &e1, const std::pair<dive_trip *, dive *> &e2) - { return e1.first == e2.first ? dive_less_than(e1.second, e2.second) : e1.first < e2.first; }); - - // Then, process the dives in batches by trip - size_t i, j; // Begin and end of batch - for (i = 0; i < dives.size(); i = j) { - dive_trip *trip = dives[i].first; - for (j = i + 1; j < dives.size() && dives[j].first == trip; ++j) - ; // pass - // Copy dives into a QVector. Some sort of "range_view" would be ideal, but Qt doesn't work this way. - QVector<dive *> divesInTrip(j - i); - for (size_t k = i; k < j; ++k) - divesInTrip[k - i] = dives[k].second; - - // Finally, emit the signal - action(trip, divesInTrip); - } -} - -// The processByTrip() function above takes a vector if (trip, dive) pairs. -// This overload takes a vector of dives. -template<typename Vector, typename Function> -void processByTrip(Vector &divesIn, Function action) -{ - std::vector<std::pair<dive_trip *, dive *>> dives; - dives.reserve(divesIn.size()); - for (dive *d: divesIn) - dives.push_back({ d->divetrip, d }); - processByTrip(dives, action); - -} - // Reset the selection to the dives of the "selection" vector and send the appropriate signals. // Set the current dive to "currentDive". "currentDive" must be an element of "selection" (or // null if "seletion" is empty). Return true if the selection or current dive changed. diff --git a/desktop-widgets/mapwidget.cpp b/desktop-widgets/mapwidget.cpp index 86ffaeabe..233b825eb 100644 --- a/desktop-widgets/mapwidget.cpp +++ b/desktop-widgets/mapwidget.cpp @@ -12,7 +12,6 @@ #include "mainwindow.h" #include "divelistview.h" #include "command.h" -#include "core/trip.h" // TODO: Needed because divesChanged uses a trip parameter -> remove that! static const QUrl urlMapWidget = QUrl(QStringLiteral("qrc:/qml/MapWidget.qml")); static const QUrl urlMapWidgetError = QUrl(QStringLiteral("qrc:/qml/MapWidgetError.qml")); @@ -91,7 +90,7 @@ void MapWidget::coordinatesChanged(struct dive_site *ds, const location_t &locat Command::editDiveSiteLocation(ds, location); } -void MapWidget::divesChanged(dive_trip *, const QVector<dive *> &, DiveField field) +void MapWidget::divesChanged(const QVector<dive *> &, DiveField field) { if (field == DiveField::DIVESITE) reload(); diff --git a/desktop-widgets/mapwidget.h b/desktop-widgets/mapwidget.h index 67801207a..c534c1bb3 100644 --- a/desktop-widgets/mapwidget.h +++ b/desktop-widgets/mapwidget.h @@ -31,7 +31,7 @@ public slots: void selectedDivesChanged(const QList<int> &); void coordinatesChanged(struct dive_site *ds, const location_t &); void doneLoading(QQuickWidget::Status status); - void divesChanged(dive_trip *, const QVector<dive *> &, DiveField field); + void divesChanged(const QVector<dive *> &, DiveField field); private: static MapWidget *m_instance; diff --git a/desktop-widgets/tab-widgets/TabDiveInformation.cpp b/desktop-widgets/tab-widgets/TabDiveInformation.cpp index 589071a5e..9af640a8e 100644 --- a/desktop-widgets/tab-widgets/TabDiveInformation.cpp +++ b/desktop-widgets/tab-widgets/TabDiveInformation.cpp @@ -5,7 +5,6 @@ #include "core/units.h" #include "core/dive.h" #include "desktop-widgets/command.h" -#include "core/trip.h" // TODO: Needed because divesChanged uses a trip parameter -> remove that! #include "core/qthelper.h" #include "core/statistics.h" #include "core/display.h" @@ -131,7 +130,7 @@ void TabDiveInformation::updateData() // This function gets called if a field gets updated by an undo command. // Refresh the corresponding UI field. -void TabDiveInformation::divesChanged(dive_trip *trip, const QVector<dive *> &dives, DiveField field) +void TabDiveInformation::divesChanged(const QVector<dive *> &dives, DiveField field) { // If the current dive is not in list of changed dives, do nothing if (!current_dive || !dives.contains(current_dive)) diff --git a/desktop-widgets/tab-widgets/TabDiveInformation.h b/desktop-widgets/tab-widgets/TabDiveInformation.h index e2b2e3c94..04c27acab 100644 --- a/desktop-widgets/tab-widgets/TabDiveInformation.h +++ b/desktop-widgets/tab-widgets/TabDiveInformation.h @@ -17,7 +17,7 @@ public: void updateData() override; void clear() override; private slots: - void divesChanged(dive_trip *trip, const QVector<dive *> &dives, DiveField field); + void divesChanged(const QVector<dive *> &dives, DiveField field); void on_atmPressVal_editingFinished(); void on_atmPressType_currentIndexChanged(int index); private: diff --git a/desktop-widgets/tab-widgets/maintab.cpp b/desktop-widgets/tab-widgets/maintab.cpp index 76c651328..ee246e3ab 100644 --- a/desktop-widgets/tab-widgets/maintab.cpp +++ b/desktop-widgets/tab-widgets/maintab.cpp @@ -281,7 +281,7 @@ static void profileFromDive(struct dive *d) // This function gets called if a field gets updated by an undo command. // Refresh the corresponding UI field. -void MainTab::divesChanged(dive_trip *trip, const QVector<dive *> &dives, DiveField field) +void MainTab::divesChanged(const QVector<dive *> &dives, DiveField field) { // If the current dive is not in list of changed dives, do nothing if (!current_dive || !dives.contains(current_dive)) diff --git a/desktop-widgets/tab-widgets/maintab.h b/desktop-widgets/tab-widgets/maintab.h index 5968c7d31..671f22ac4 100644 --- a/desktop-widgets/tab-widgets/maintab.h +++ b/desktop-widgets/tab-widgets/maintab.h @@ -46,7 +46,7 @@ public: public slots: - void divesChanged(dive_trip *trip, const QVector<dive *> &dives, DiveField field); + void divesChanged(const QVector<dive *> &dives, DiveField field); void diveSiteEdited(dive_site *ds, int field); void tripChanged(dive_trip *trip, TripField field); void updateDiveInfo(); diff --git a/qt-models/cylindermodel.cpp b/qt-models/cylindermodel.cpp index 0e5388818..4f16231b3 100644 --- a/qt-models/cylindermodel.cpp +++ b/qt-models/cylindermodel.cpp @@ -7,7 +7,6 @@ #include "qt-models/diveplannermodel.h" #include "core/gettextfromc.h" #include "core/subsurface-qt/DiveListNotifier.h" -#include "core/trip.h" // TODO: Needed because cylindersReset uses a trip parameter -> remove that! CylindersModel::CylindersModel(QObject *parent) : CleanerTableModel(parent), @@ -619,7 +618,7 @@ bool CylindersModel::updateBestMixes() return gasUpdated; } -void CylindersModel::cylindersReset(dive_trip *trip, const QVector<dive *> &dives) +void CylindersModel::cylindersReset(const QVector<dive *> &dives) { // This model only concerns the currently displayed dive. If this is not among the // dives that had their cylinders reset, exit. diff --git a/qt-models/cylindermodel.h b/qt-models/cylindermodel.h index d53aaa564..58d11e971 100644 --- a/qt-models/cylindermodel.h +++ b/qt-models/cylindermodel.h @@ -48,7 +48,7 @@ public: public slots: void remove(const QModelIndex &index); - void cylindersReset(dive_trip *trip, const QVector<dive *> &dives); + void cylindersReset(const QVector<dive *> &dives); bool updateBestMixes(); private: diff --git a/qt-models/divetripmodel.cpp b/qt-models/divetripmodel.cpp index 9fd323bcf..e15d5d0fd 100644 --- a/qt-models/divetripmodel.cpp +++ b/qt-models/divetripmodel.cpp @@ -417,14 +417,14 @@ bool DiveTripModelBase::setData(const QModelIndex &index, const QVariant &value, return true; } -void DiveTripModelBase::divesSelected(dive_trip *trip, const QVector<dive *> &dives) +void DiveTripModelBase::divesSelected(const QVector<dive *> &dives) { - changeDiveSelection(trip, dives, true); + changeDiveSelection(dives, true); } -void DiveTripModelBase::divesDeselected(dive_trip *trip, const QVector<dive *> &dives) +void DiveTripModelBase::divesDeselected(const QVector<dive *> &dives) { - changeDiveSelection(trip, dives, false); + changeDiveSelection(dives, false); } // Find a range of matching elements in a vector. @@ -915,7 +915,38 @@ void DiveTripModelTree::divesDeleted(dive_trip *trip, bool deleteTrip, const QVe } } -void DiveTripModelTree::divesChanged(dive_trip *trip, const QVector<dive *> &dives) +// The tree-version of the model wants to process the dives per trip. +// This template takes a vector of dives and calls a function batchwise for each trip. +template<typename Function> +void processByTrip(QVector<dive *> dives, Function action) +{ + // Sort lexicographically by trip then according to the dive_less_than() function. + std::sort(dives.begin(), dives.end(), [](const dive *d1, const dive *d2) + { return d1->divetrip == d2->divetrip ? dive_less_than(d1, d2) : d1->divetrip < d2->divetrip; }); + + // Then, process the dives in batches by trip + int i, j; // Begin and end of batch + for (i = 0; i < dives.size(); i = j) { + dive_trip *trip = dives[i]->divetrip; + for (j = i + 1; j < dives.size() && dives[j]->divetrip == trip; ++j) + ; // pass + // Copy dives into a QVector. Some sort of "range_view" would be ideal. + QVector<dive *> divesInTrip(j - i); + for (int k = i; k < j; ++k) + divesInTrip[k - i] = dives[k]; + + // Finally, emit the signal + action(trip, divesInTrip); + } +} + +void DiveTripModelTree::divesChanged(const QVector<dive *> &dives) +{ + processByTrip(dives, [this] (dive_trip *trip, const QVector<dive *> &divesInTrip) + { divesChangedTrip(trip, divesInTrip); }); +} + +void DiveTripModelTree::divesChangedTrip(dive_trip *trip, const QVector<dive *> &dives) { // Update filter flags. TODO: The filter should update the flag by itself when // recieving the signals below. @@ -992,7 +1023,7 @@ void DiveTripModelTree::divesMovedBetweenTrips(dive_trip *from, dive_trip *to, b // Move dives between trips. This is an "interesting" problem, as we might // move from trip to trip, from trip to top-level or from top-level to trip. // Moreover, we might have to add a trip first or delete an old trip. - // For simplicity, we will simply used the already existing divesAdded() / divesDeleted() + // For simplicity, we will simply use the already existing divesAdded() / divesDeleted() // functions. This *is* cheating. But let's just try this and see how graceful // this is handled by Qt and if it gives some ugly UI behavior! @@ -1006,10 +1037,16 @@ void DiveTripModelTree::divesMovedBetweenTrips(dive_trip *from, dive_trip *to, b QVector<dive *> selectedDives = filterSelectedDives(dives); divesAdded(to, createTo, dives); divesDeleted(from, deleteFrom, dives); - divesSelected(to, selectedDives); + changeDiveSelectionTrip(to, dives, true); +} + +void DiveTripModelTree::divesTimeChanged(timestamp_t delta, const QVector<dive *> &dives) +{ + processByTrip(dives, [this, delta] (dive_trip *trip, const QVector<dive *> &divesInTrip) + { divesTimeChangedTrip(trip, delta, divesInTrip); }); } -void DiveTripModelTree::divesTimeChanged(dive_trip *trip, timestamp_t delta, const QVector<dive *> &dives) +void DiveTripModelTree::divesTimeChangedTrip(dive_trip *trip, timestamp_t delta, const QVector<dive *> &dives) { // As in the case of divesMovedBetweenTrips(), this is a tricky, but solvable, problem. // We have to consider the direction (delta < 0 or delta >0) and that dives at their destination @@ -1024,10 +1061,16 @@ void DiveTripModelTree::divesTimeChanged(dive_trip *trip, timestamp_t delta, con QVector<dive *> selectedDives = filterSelectedDives(dives); divesDeleted(trip, false, dives); divesAdded(trip, false, dives); - divesSelected(trip, selectedDives); + changeDiveSelectionTrip(trip, selectedDives, true); +} + +void DiveTripModelTree::changeDiveSelection(const QVector<dive *> &dives, bool select) +{ + processByTrip(dives, [this, select] (dive_trip *trip, const QVector<dive *> &divesInTrip) + { changeDiveSelectionTrip(trip, divesInTrip, select); }); } -void DiveTripModelTree::changeDiveSelection(dive_trip *trip, const QVector<dive *> &dives, bool select) +void DiveTripModelTree::changeDiveSelectionTrip(dive_trip *trip, const QVector<dive *> &dives, bool select) { // We got a number of dives that have been selected. Turn this into QModelIndexes and // emit a signal, so that views can change the selection. @@ -1198,8 +1241,10 @@ QVariant DiveTripModelList::data(const QModelIndex &index, int role) const return d ? diveData(d, index.column(), role) : QVariant(); } -void DiveTripModelList::divesAdded(dive_trip *, bool, const QVector<dive *> &dives) +void DiveTripModelList::divesAdded(dive_trip *, bool, const QVector<dive *> &divesIn) { + QVector<dive *> dives = divesIn; + std::sort(dives.begin(), dives.end(), dive_less_than); addInBatches(items, dives, &dive_less_than, // comp [&](std::vector<dive *> &items, const QVector<dive *> &dives, int idx, int from, int to) { // inserter @@ -1209,8 +1254,10 @@ void DiveTripModelList::divesAdded(dive_trip *, bool, const QVector<dive *> &div }); } -void DiveTripModelList::divesDeleted(dive_trip *trip, bool deleteTrip, const QVector<dive *> &dives) +void DiveTripModelList::divesDeleted(dive_trip *, bool, const QVector<dive *> &divesIn) { + QVector<dive *> dives = divesIn; + std::sort(dives.begin(), dives.end(), dive_less_than); processRangesZip(items, dives, [](const dive *d1, const dive *d2) { return d1 == d2; }, // Condition (std::equal_to only in C++14) [&](std::vector<dive *> &items, const QVector<dive *> &, int from, int to, int) -> int { // Action @@ -1221,8 +1268,11 @@ void DiveTripModelList::divesDeleted(dive_trip *trip, bool deleteTrip, const QVe }); } -void DiveTripModelList::divesChanged(dive_trip *trip, const QVector<dive *> &dives) +void DiveTripModelList::divesChanged(const QVector<dive *> &divesIn) { + QVector<dive *> dives = divesIn; + std::sort(dives.begin(), dives.end(), dive_less_than); + // Update filter flags. TODO: The filter should update the flag by itself when // recieving the signals below. for (dive *d: dives) @@ -1240,16 +1290,19 @@ void DiveTripModelList::divesChanged(dive_trip *trip, const QVector<dive *> &div }); } -void DiveTripModelList::divesTimeChanged(dive_trip *trip, timestamp_t delta, const QVector<dive *> &dives) +void DiveTripModelList::divesTimeChanged(timestamp_t delta, const QVector<dive *> &divesIn) { + QVector<dive *> dives = divesIn; + std::sort(dives.begin(), dives.end(), dive_less_than); + // See comment for DiveTripModelTree::divesTimeChanged above. QVector<dive *> selectedDives = filterSelectedDives(dives); - divesDeleted(trip, false, dives); - divesAdded(trip, false, dives); - divesSelected(trip, selectedDives); + divesDeleted(nullptr, false, dives); + divesAdded(nullptr, false, dives); + divesSelected(selectedDives); } -void DiveTripModelList::changeDiveSelection(dive_trip *trip, const QVector<dive *> &dives, bool select) +void DiveTripModelList::changeDiveSelection(const QVector<dive *> &dives, bool select) { // We got a number of dives that have been selected. Turn this into QModelIndexes and // emit a signal, so that views can change the selection. diff --git a/qt-models/divetripmodel.h b/qt-models/divetripmodel.h index 592234917..8903cb0c1 100644 --- a/qt-models/divetripmodel.h +++ b/qt-models/divetripmodel.h @@ -92,15 +92,15 @@ signals: void selectionChanged(const QVector<QModelIndex> &indexes, bool select); void newCurrentDive(QModelIndex index); protected slots: - void divesSelected(dive_trip *trip, const QVector<dive *> &dives); - void divesDeselected(dive_trip *trip, const QVector<dive *> &dives); + void divesSelected(const QVector<dive *> &dives); + void divesDeselected(const QVector<dive *> &dives); protected: // Access trip and dive data static QVariant diveData(const struct dive *d, int column, int role); static QVariant tripData(const dive_trip *trip, int column, int role); // Select or deselect dives - virtual void changeDiveSelection(dive_trip *trip, const QVector<dive *> &dives, bool select) = 0; + virtual void changeDiveSelection(const QVector<dive *> &dives, bool select) = 0; virtual dive *diveOrNull(const QModelIndex &index) const = 0; // Returns a dive if this index represents a dive, null otherwise }; @@ -111,9 +111,9 @@ class DiveTripModelTree : public DiveTripModelBase public slots: void divesAdded(dive_trip *trip, bool addTrip, const QVector<dive *> &dives); void divesDeleted(dive_trip *trip, bool deleteTrip, const QVector<dive *> &dives); - void divesChanged(dive_trip *trip, const QVector<dive *> &dives); - void divesTimeChanged(dive_trip *trip, timestamp_t delta, const QVector<dive *> &dives); void divesMovedBetweenTrips(dive_trip *from, dive_trip *to, bool deleteFrom, bool createTo, const QVector<dive *> &dives); + void divesChanged(const QVector<dive *> &dives); + void divesTimeChanged(timestamp_t delta, const QVector<dive *> &dives); void currentDiveChanged(); void tripChanged(dive_trip *trip, TripField); @@ -126,9 +126,12 @@ private: QVariant data(const QModelIndex &index, int role) const override; void filterFinished() override; bool lessThan(const QModelIndex &i1, const QModelIndex &i2) const override; - void changeDiveSelection(dive_trip *trip, const QVector<dive *> &dives, bool select) override; + void changeDiveSelection(const QVector<dive *> &dives, bool select) override; + void changeDiveSelectionTrip(dive_trip *trip, const QVector<dive *> &dives, bool select); dive *diveOrNull(const QModelIndex &index) const override; bool setShown(const QModelIndex &idx, bool shown); + void divesChangedTrip(dive_trip *trip, const QVector<dive *> &dives); + void divesTimeChangedTrip(dive_trip *trip, timestamp_t delta, const QVector<dive *> &dives); // The tree model has two levels. At the top level, we have either trips or dives // that do not belong to trips. Such a top-level item is represented by the "Item" @@ -174,8 +177,8 @@ class DiveTripModelList : public DiveTripModelBase public slots: void divesAdded(dive_trip *trip, bool addTrip, const QVector<dive *> &dives); void divesDeleted(dive_trip *trip, bool deleteTrip, const QVector<dive *> &dives); - void divesChanged(dive_trip *trip, const QVector<dive *> &dives); - void divesTimeChanged(dive_trip *trip, timestamp_t delta, const QVector<dive *> &dives); + void divesChanged(const QVector<dive *> &dives); + void divesTimeChanged(timestamp_t delta, const QVector<dive *> &dives); // Does nothing in list view. //void divesMovedBetweenTrips(dive_trip *from, dive_trip *to, bool deleteFrom, bool createTo, const QVector<dive *> &dives); void currentDiveChanged(); @@ -189,7 +192,7 @@ private: QVariant data(const QModelIndex &index, int role) const override; void filterFinished() override; bool lessThan(const QModelIndex &i1, const QModelIndex &i2) const override; - void changeDiveSelection(dive_trip *trip, const QVector<dive *> &dives, bool select) override; + void changeDiveSelection(const QVector<dive *> &dives, bool select) override; dive *diveOrNull(const QModelIndex &index) const override; bool setShown(const QModelIndex &idx, bool shown); diff --git a/qt-models/weightmodel.cpp b/qt-models/weightmodel.cpp index 7285e40cd..d82b8a7e8 100644 --- a/qt-models/weightmodel.cpp +++ b/qt-models/weightmodel.cpp @@ -6,7 +6,6 @@ #include "core/qthelper.h" #include "core/subsurface-qt/DiveListNotifier.h" #include "qt-models/weightsysteminfomodel.h" -#include "core/trip.h" // TODO: Needed because weightsystemsReset uses a trip parameter -> remove that! WeightModel::WeightModel(QObject *parent) : CleanerTableModel(parent), changed(false), @@ -168,7 +167,7 @@ void WeightModel::updateDive() } } -void WeightModel::weightsystemsReset(dive_trip *trip, const QVector<dive *> &dives) +void WeightModel::weightsystemsReset(const QVector<dive *> &dives) { // This model only concerns the currently displayed dive. If this is not among the // dives that had their cylinders reset, exit. diff --git a/qt-models/weightmodel.h b/qt-models/weightmodel.h index ec43bbbb8..2f59daf4b 100644 --- a/qt-models/weightmodel.h +++ b/qt-models/weightmodel.h @@ -32,7 +32,7 @@ public: public slots: void remove(const QModelIndex &index); - void weightsystemsReset(dive_trip *trip, const QVector<dive *> &dives); + void weightsystemsReset(const QVector<dive *> &dives); private: int rows; |