summaryrefslogtreecommitdiffstats
path: root/qt-models
diff options
context:
space:
mode:
Diffstat (limited to 'qt-models')
-rw-r--r--qt-models/cylindermodel.cpp3
-rw-r--r--qt-models/cylindermodel.h2
-rw-r--r--qt-models/divetripmodel.cpp89
-rw-r--r--qt-models/divetripmodel.h21
-rw-r--r--qt-models/weightmodel.cpp3
-rw-r--r--qt-models/weightmodel.h2
6 files changed, 87 insertions, 33 deletions
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;