From 66aeaddd0f9ccdb46bb0879c8a9f7e6181c3d571 Mon Sep 17 00:00:00 2001
From: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
Date: Mon, 27 Aug 2018 12:53:46 +0200
Subject: [PATCH] Filter: cache number of dives fulfilling filter rules

Currently, in FilterModelBase::data() the number of dives is recalculated.
This happens for every mouse-over event!

Calculate the number of dives only on recalculation and store the count
in the items-struct.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
---
 qt-models/filtermodels.cpp | 15 ++++++++++++---
 qt-models/filtermodels.h   |  1 +
 2 files changed, 13 insertions(+), 3 deletions(-)

diff --git a/qt-models/filtermodels.cpp b/qt-models/filtermodels.cpp
index 611c3b78d..beeb7501b 100644
--- a/qt-models/filtermodels.cpp
+++ b/qt-models/filtermodels.cpp
@@ -69,6 +69,16 @@ void FilterModelBase::updateList(const QStringList &newList)
 		items.back().checked = true;
 		anyChecked = true;
 	}
+
+	// Finally, calculate and cache the counts. Ignore the last item, since
+	// this is the "Show Empty Tags" entry.
+	for (int i = 0; i < (int)newList.size() - 1; i++)
+		items[i].count = countDives(qPrintable(newList[i]));
+
+	// Calculate count of "Empty Tags".
+	if (!items.empty())
+		items.back().count = countDives("");
+
 	setStringList(newList);
 }
 
@@ -99,9 +109,8 @@ QVariant FilterModelBase::data(const QModelIndex &index, int role) const
 	if (role == Qt::CheckStateRole) {
 		return items[index.row()].checked ? Qt::Checked : Qt::Unchecked;
 	} else if (role == Qt::DisplayRole) {
-		QString value = stringList()[index.row()];
-		int count = countDives((index.row() == rowCount() - 1) ? "" : qPrintable(value));
-		return value + QString(" (%1)").arg(count);
+		int row = index.row();
+		return QStringLiteral("%1 (%2)").arg(stringList()[row], QString::number(items[row].count));
 	}
 	return QVariant();
 }
diff --git a/qt-models/filtermodels.h b/qt-models/filtermodels.h
index 8bac44680..846949809 100644
--- a/qt-models/filtermodels.h
+++ b/qt-models/filtermodels.h
@@ -14,6 +14,7 @@ class FilterModelBase : public QStringListModel {
 protected:
 	struct Item {
 		bool checked;
+		int count;
 	};
 	std::vector<Item> items;
 public: