[PATCH] Add a "sort role" for sorting the dive list

Linus Torvalds torvalds at linux-foundation.org
Wed May 29 03:53:13 PDT 2013


From: Linus Torvalds <torvalds at linux-foundation.org>
Date: Wed, 29 May 2013 14:54:39 +0900
Subject: [PATCH] Add a "sort role" for sorting the dive list

By default, sorting is done by the display role, but then we end up
sorting by the string we display, which is almost always the wrong thing.

So this adds a new "SORT_ROLE" that is used for sorting, and then the
data lookup can return the raw data we want to sort by.

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

This gets the sorting *within* a trip right. However, it doesn't discard 
the trip data when sorting by non-date, so it's not all that useful, but 
even just sorting the trips themselves by date helps a lot.

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

diff --git a/qt-ui/divelistview.cpp b/qt-ui/divelistview.cpp
index 4957e33745b2..4bee0195632c 100644
--- a/qt-ui/divelistview.cpp
+++ b/qt-ui/divelistview.cpp
@@ -21,6 +21,7 @@ DiveListView::DiveListView(QWidget *parent) : QTreeView(parent), mouseClickSelec
  	setUniformRowHeights(true);
  	setItemDelegateForColumn(TreeItemDT::RATING, new StarWidgetsDelegate());
  	QSortFilterProxyModel *model = new QSortFilterProxyModel(this);
+	model->setSortRole(TreeItemDT::SORT_ROLE);
  	setModel(model);
  	header()->setContextMenuPolicy(Qt::ActionsContextMenu);
  }
diff --git a/qt-ui/models.cpp b/qt-ui/models.cpp
index 38ef8938cbfd..0dfb504c43a4 100644
--- a/qt-ui/models.cpp
+++ b/qt-ui/models.cpp
@@ -822,6 +822,9 @@ QVariant TripItem::data(int column, int role) const
  {
  	QVariant ret;

+	if (role == SORT_ROLE)
+		return (qulonglong)trip->when;
+
  	if (role == Qt::DisplayRole) {
  		switch (column) {
  		case LOCATION:
@@ -848,6 +851,13 @@ struct DiveItem : public TreeItemDT {
  	int weight() const;
  };

+static int nitrox_sort_value(struct dive *dive)
+{
+	int o2, he, o2low;
+	get_dive_gas(dive, &o2, &he, &o2low);
+	return he*1000 + o2;
+}
+
  QVariant DiveItem::data(int column, int role) const
  {
  	QVariant retVal;
@@ -865,6 +875,23 @@ QVariant DiveItem::data(int column, int role) const
  			break;
  		}
  		break;
+	case SORT_ROLE:
+	switch (column) {
+		case NR:		return dive->number;
+		case DATE:		return (qulonglong) dive->when;
+		case DEPTH:		return dive->maxdepth.mm;
+		case DURATION:		return dive->duration.seconds;
+		case TEMPERATURE:	return dive->watertemp.mkelvin;
+		case TOTALWEIGHT:	return total_weight(dive);
+		case SUIT:		return QString(dive->suit);
+		case CYLINDER:		return QString(dive->cylinder[0].type.description);
+		case NITROX:		return nitrox_sort_value(dive);
+		case SAC:		return dive->sac;
+		case OTU:		return dive->otu;
+		case MAXCNS:		return dive->maxcns;
+		case LOCATION:		return QString(dive->location);
+		break;
+	}
  	case Qt::DisplayRole:
  		switch (column) {
  		case NR:
diff --git a/qt-ui/models.h b/qt-ui/models.h
index f51566c1b044..f6022c4348ef 100644
--- a/qt-ui/models.h
+++ b/qt-ui/models.h
@@ -124,7 +124,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};
+	enum ExtraRoles{STAR_ROLE = Qt::UserRole + 1, DIVE_ROLE, SORT_ROLE};

  	virtual ~TreeItemDT();
  	int columnCount() const {


More information about the subsurface mailing list