[PATCH] Qt: Implement trip merging logic

Linus Torvalds torvalds at linux-foundation.org
Wed Jun 26 22:22:11 PDT 2013


From: Linus Torvalds <torvalds at linux-foundation.org>
Date: Wed, 26 Jun 2013 19:16:40 -1000
Subject: [PATCH] Qt: Implement trip merging logic

So during my Maui trip, I had a short hiatus in diving, causing
subsurface to start a new trip for the last day of diving.  I could have
just started the old gtk branch to fix it up, but decided that I might
as well try to implement the "merge trip" logic in the Qt branch instead.

This is the end result of that.

Signed-off-by: Linus Torvalds <torvalds at linux-foundation.org>
---

So stupid C++/Qt question: I'd really like to make the "merge_trip()" 
helper function to be static, do I really have to declare it at all? Is 
there some way to avoid having to mention it as a private function in the 
header file? I hate having to list these private helpers.

Oh, and this is almost entirely untested. But it worked for the one case I 
wrote the code for, so it must be perfect. Ship it.

 qt-ui/divelistview.cpp | 35 +++++++++++++++++++++++++++++++++++
 qt-ui/divelistview.h   |  3 +++
 qt-ui/models.cpp       |  3 +++
 qt-ui/models.h         |  2 +-
 4 files changed, 42 insertions(+), 1 deletion(-)

diff --git a/qt-ui/divelistview.cpp b/qt-ui/divelistview.cpp
index 480decd2d49b..4b856f60157d 100644
--- a/qt-ui/divelistview.cpp
+++ b/qt-ui/divelistview.cpp
@@ -298,6 +298,36 @@ void DiveListView::selectionChanged(const QItemSelection& selected, const QItemS
 	Q_EMIT currentDiveChanged(selected_dive);
 }
 
+void DiveListView::merge_trip(const QModelIndex &a, int offset)
+{
+	int i = a.row() + offset;
+	QModelIndex b = a.sibling(i,0);
+
+	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*>();
+
+	if (trip_a == trip_b || !trip_a || !trip_b)
+		return;
+
+	if (!trip_a->location)
+		trip_a->location = strdup(trip_b->location);
+	if (!trip_a->notes)
+		trip_a->notes = strdup(trip_b->notes);
+	while (trip_b->dives)
+		add_dive_to_trip(trip_b->dives, trip_a);
+	reload(currentLayout, false);
+}
+
+void DiveListView::mergeTripAbove()
+{
+	merge_trip(contextMenuIndex, -1);
+}
+
+void DiveListView::mergeTripBelow()
+{
+	merge_trip(contextMenuIndex, +1);
+}
+
 void DiveListView::removeFromTrip()
 {
 	struct dive *d = (struct dive *) contextMenuIndex.data(DiveTripModel::DIVE_ROLE).value<void*>();
@@ -336,6 +366,7 @@ void DiveListView::contextMenuEvent(QContextMenuEvent *event)
 	// let's remember where we are
 	contextMenuIndex = indexAt(event->pos());
 	struct dive *d = (struct dive *) contextMenuIndex.data(DiveTripModel::DIVE_ROLE).value<void*>();
+	dive_trip_t *trip = (dive_trip_t *) contextMenuIndex.data(DiveTripModel::TRIP_ROLE).value<void*>();
 	QMenu popup(this);
 	if (currentLayout == DiveTripModel::TREE) {
 		popup.addAction(tr("expand all"), this, SLOT(expandAll()));
@@ -344,6 +375,10 @@ void DiveListView::contextMenuEvent(QContextMenuEvent *event)
 		if (d) {
 			popup.addAction(tr("remove dive from trip"), this, SLOT(removeFromTrip()));
 		}
+		if (trip) {
+			popup.addAction(tr("Merge trip with trip above"), this, SLOT(mergeTripAbove()));
+			popup.addAction(tr("Merge trip with trip below"), this, SLOT(mergeTripBelow()));
+		}
 	}
 	if (d)
 		popup.addAction(tr("delete dive"), this, SLOT(deleteDive()));
diff --git a/qt-ui/divelistview.h b/qt-ui/divelistview.h
index 3aee1912825c..1faa3613a1bd 100644
--- a/qt-ui/divelistview.h
+++ b/qt-ui/divelistview.h
@@ -40,6 +40,8 @@ public slots:
 	void deleteDive();
 	void testSlot();
 	void fixMessyQtModelBehaviour();
+	void mergeTripAbove();
+	void mergeTripBelow();
 
 signals:
 	void currentDiveChanged(int divenr);
@@ -50,6 +52,7 @@ private:
 	DiveTripModel::Layout currentLayout;
 	QLineEdit *searchBox;
 	QModelIndex contextMenuIndex;
+	void merge_trip(const QModelIndex &a, const int offset);
 };
 
 #endif // DIVELISTVIEW_H
diff --git a/qt-ui/models.cpp b/qt-ui/models.cpp
index 0288e7107b8d..1fd5f74cba7e 100644
--- a/qt-ui/models.cpp
+++ b/qt-ui/models.cpp
@@ -909,6 +909,9 @@ QVariant TripItem::data(int column, int role) const
 {
 	QVariant ret;
 
+	if (role == DiveTripModel::TRIP_ROLE)
+		return QVariant::fromValue<void*>(trip);
+
 	if (role == DiveTripModel::SORT_ROLE)
 		return (qulonglong)trip->when;
 
diff --git a/qt-ui/models.h b/qt-ui/models.h
index f6fb3f5c0a64..ec15174f6034 100644
--- a/qt-ui/models.h
+++ b/qt-ui/models.h
@@ -162,7 +162,7 @@ public:
 	enum Column {NR, DATE, RATING, DEPTH, DURATION, TEMPERATURE, TOTALWEIGHT,
 		SUIT, CYLINDER, NITROX, SAC, OTU, MAXCNS, LOCATION, COLUMNS };
 
-	enum ExtraRoles{STAR_ROLE = Qt::UserRole + 1, DIVE_ROLE, SORT_ROLE};
+	enum ExtraRoles{STAR_ROLE = Qt::UserRole + 1, DIVE_ROLE, TRIP_ROLE, SORT_ROLE};
 	enum Layout{TREE, LIST, CURRENT};
 
 	Qt::ItemFlags flags(const QModelIndex &index) const;
-- 
1.8.3



More information about the subsurface mailing list