aboutsummaryrefslogtreecommitdiffstats
path: root/qt-models
diff options
context:
space:
mode:
Diffstat (limited to 'qt-models')
-rw-r--r--qt-models/diveimportedmodel.cpp3
-rw-r--r--qt-models/filtermodels.cpp86
-rw-r--r--qt-models/filtermodels.h19
3 files changed, 63 insertions, 45 deletions
diff --git a/qt-models/diveimportedmodel.cpp b/qt-models/diveimportedmodel.cpp
index 4cc5a0191..ea70ece39 100644
--- a/qt-models/diveimportedmodel.cpp
+++ b/qt-models/diveimportedmodel.cpp
@@ -180,7 +180,8 @@ void DiveImportedModel::recordDives()
}
diveTable->nr = 0;
process_dives(true, true);
- autogroup_dives();
+ if (autogroup)
+ autogroup_dives();
}
QHash<int, QByteArray> DiveImportedModel::roleNames() const {
diff --git a/qt-models/filtermodels.cpp b/qt-models/filtermodels.cpp
index 718611611..217d456bf 100644
--- a/qt-models/filtermodels.cpp
+++ b/qt-models/filtermodels.cpp
@@ -10,6 +10,7 @@
#endif
#include <QDebug>
+#include <algorithm>
#define CREATE_INSTANCE_METHOD( CLASS ) \
CLASS *CLASS::instance() \
@@ -39,8 +40,7 @@ bool CLASS::setData(const QModelIndex &index, const QVariant &value, int role) \
#define CREATE_CLEAR_FILTER_METHOD( CLASS ) \
void CLASS::clearFilter() \
{ \
- memset(checkState, false, rowCount()); \
- checkState[rowCount() - 1] = false; \
+ std::fill(checkState.begin(), checkState.end(), false); \
anyChecked = false; \
emit dataChanged(createIndex(0,0), createIndex(rowCount()-1, 0)); \
}
@@ -78,7 +78,40 @@ CREATE_COMMON_METHODS_FOR_FILTER(SuitsFilterModel, count_dives_with_suit)
CREATE_INSTANCE_METHOD(MultiFilterSortModel)
-SuitsFilterModel::SuitsFilterModel(QObject *parent) : QStringListModel(parent)
+FilterModelBase::FilterModelBase(QObject *parent) : QStringListModel(parent)
+{
+}
+
+// Update the stringList and the checkState array.
+// The last item is supposed to be the "Show Empty Tags" entry.
+void FilterModelBase::updateList(const QStringList &newList)
+{
+ // Keep copy of old checkState array to reimport them later.
+ std::vector<char> oldCheckState = checkState;
+ checkState.resize(newList.count());
+ std::fill(checkState.begin(), checkState.end(), false);
+ anyChecked = false;
+
+ // Ignore last item, since this is the "Show Empty Tags" entry.
+ for (int i = 0; i < rowCount() - 1; i++) {
+ if (oldCheckState[i]) {
+ int ind = newList.indexOf(stringList()[i]);
+ if (ind >= 0 && ind < newList.count() - 1) {
+ checkState[ind] = true;
+ anyChecked = true;
+ }
+ }
+ }
+
+ // On program startup, the old list is empty.
+ if (rowCount() > 0 && oldCheckState.back()) {
+ checkState.back() = true;
+ anyChecked = true;
+ }
+ setStringList(newList);
+}
+
+SuitsFilterModel::SuitsFilterModel(QObject *parent) : FilterModelBase(parent)
{
}
@@ -127,15 +160,10 @@ void SuitsFilterModel::repopulate()
}
qSort(list);
list << tr("No suit set");
- setStringList(list);
- delete[] checkState;
- checkState = new bool[list.count()];
- memset(checkState, false, list.count());
- checkState[list.count() - 1] = false;
- anyChecked = false;
+ updateList(list);
}
-TagFilterModel::TagFilterModel(QObject *parent) : QStringListModel(parent)
+TagFilterModel::TagFilterModel(QObject *parent) : FilterModelBase(parent)
{
}
@@ -152,12 +180,7 @@ void TagFilterModel::repopulate()
}
qSort(list);
list << tr("Empty tags");
- setStringList(list);
- delete[] checkState;
- checkState = new bool[list.count()];
- memset(checkState, false, list.count());
- checkState[list.count() - 1] = false;
- anyChecked = false;
+ updateList(list);
}
bool TagFilterModel::doFilter(dive *d, QModelIndex &index0, QAbstractItemModel *sourceModel) const
@@ -194,7 +217,7 @@ bool TagFilterModel::doFilter(dive *d, QModelIndex &index0, QAbstractItemModel *
return false;
}
-BuddyFilterModel::BuddyFilterModel(QObject *parent) : QStringListModel(parent)
+BuddyFilterModel::BuddyFilterModel(QObject *parent) : FilterModelBase(parent)
{
}
@@ -247,15 +270,10 @@ void BuddyFilterModel::repopulate()
}
qSort(list);
list << tr("No buddies");
- setStringList(list);
- delete[] checkState;
- checkState = new bool[list.count()];
- memset(checkState, false, list.count());
- checkState[list.count() - 1] = false;
- anyChecked = false;
+ updateList(list);
}
-LocationFilterModel::LocationFilterModel(QObject *parent) : QStringListModel(parent)
+LocationFilterModel::LocationFilterModel(QObject *parent) : FilterModelBase(parent)
{
}
@@ -277,15 +295,12 @@ bool LocationFilterModel::doFilter(struct dive *d, QModelIndex &index0, QAbstrac
return true;
}
- // there is a location selected
+ // There is a location selected
QStringList locationList = stringList();
- if (!locationList.isEmpty()) {
- locationList.removeLast(); // remove the "Show Empty Tags";
- for (int i = 0; i < rowCount(); i++) {
- if (checkState[i] && (location.indexOf(stringList()[i]) != -1)) {
- return true;
- }
- }
+ // Ignore last item, since this is the "Show Empty Tags" entry
+ for (int i = 0; i < rowCount() - 1; i++) {
+ if (checkState[i] && location == locationList[i])
+ return true;
}
return false;
}
@@ -303,12 +318,7 @@ void LocationFilterModel::repopulate()
}
qSort(list);
list << tr("No location set");
- setStringList(list);
- delete[] checkState;
- checkState = new bool[list.count()];
- memset(checkState, false, list.count());
- checkState[list.count() - 1] = false;
- anyChecked = false;
+ updateList(list);
}
MultiFilterSortModel::MultiFilterSortModel(QObject *parent) :
diff --git a/qt-models/filtermodels.h b/qt-models/filtermodels.h
index 992582776..3060f7cf3 100644
--- a/qt-models/filtermodels.h
+++ b/qt-models/filtermodels.h
@@ -5,17 +5,24 @@
#include <QStringListModel>
#include <QSortFilterProxyModel>
#include <stdint.h>
+#include <vector>
class MultiFilterInterface {
public:
- MultiFilterInterface() : checkState(NULL), anyChecked(false) {}
+ MultiFilterInterface() : anyChecked(false) {}
virtual bool doFilter(struct dive *d, QModelIndex &index0, QAbstractItemModel *sourceModel) const = 0;
virtual void clearFilter() = 0;
- bool *checkState;
+ std::vector<char> checkState;
bool anyChecked;
};
-class TagFilterModel : public QStringListModel, public MultiFilterInterface {
+class FilterModelBase : public QStringListModel, public MultiFilterInterface {
+protected:
+ explicit FilterModelBase(QObject *parent = 0);
+ void updateList(const QStringList &new_list);
+};
+
+class TagFilterModel : public FilterModelBase {
Q_OBJECT
public:
static TagFilterModel *instance();
@@ -32,7 +39,7 @@ private:
explicit TagFilterModel(QObject *parent = 0);
};
-class BuddyFilterModel : public QStringListModel, public MultiFilterInterface {
+class BuddyFilterModel : public FilterModelBase {
Q_OBJECT
public:
static BuddyFilterModel *instance();
@@ -49,7 +56,7 @@ private:
explicit BuddyFilterModel(QObject *parent = 0);
};
-class LocationFilterModel : public QStringListModel, public MultiFilterInterface {
+class LocationFilterModel : public FilterModelBase {
Q_OBJECT
public:
static LocationFilterModel *instance();
@@ -66,7 +73,7 @@ private:
explicit LocationFilterModel(QObject *parent = 0);
};
-class SuitsFilterModel : public QStringListModel, public MultiFilterInterface {
+class SuitsFilterModel : public FilterModelBase {
Q_OBJECT
public:
static SuitsFilterModel *instance();