aboutsummaryrefslogtreecommitdiffstats
path: root/qt-models
diff options
context:
space:
mode:
authorGravatar Berthold Stoeger <bstoeger@mail.tuwien.ac.at>2018-07-25 21:23:19 +0200
committerGravatar Dirk Hohndel <dirk@hohndel.org>2018-09-01 07:48:43 -0700
commit89e0c3f46498ba3e0844514b6709a07c200a68df (patch)
tree8609c84130cb04180d9db4bb43f2dfc11cf563c3 /qt-models
parent236f0512bec2946b35052abf637c7c8df97f34bf (diff)
downloadsubsurface-89e0c3f46498ba3e0844514b6709a07c200a68df.tar.gz
Cleanup: make DiveTripModel a global object
DiveTripModel (the model describing the dive-list) was destroyed and recreated on every reset of the list. This seems excessive. Instead - in analogy to most other models - make it a single global object. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
Diffstat (limited to 'qt-models')
-rw-r--r--qt-models/divetripmodel.cpp12
-rw-r--r--qt-models/divetripmodel.h2
-rw-r--r--qt-models/treemodel.cpp12
-rw-r--r--qt-models/treemodel.h5
-rw-r--r--qt-models/yearlystatisticsmodel.cpp6
5 files changed, 23 insertions, 14 deletions
diff --git a/qt-models/divetripmodel.cpp b/qt-models/divetripmodel.cpp
index 384da6b89..b94eaa985 100644
--- a/qt-models/divetripmodel.cpp
+++ b/qt-models/divetripmodel.cpp
@@ -437,6 +437,12 @@ int DiveItem::weight() const
return tw.grams;
}
+DiveTripModel *DiveTripModel::instance()
+{
+ static DiveTripModel self;
+ return &self;
+}
+
DiveTripModel::DiveTripModel(QObject *parent) :
TreeModel(parent),
currentLayout(TREE)
@@ -586,9 +592,11 @@ void DiveTripModel::setupModelData()
beginResetModel();
+ clear();
if (autogroup)
autogroup_dives();
dive_table.preexisting = dive_table.nr;
+ QMap<dive_trip_t *, TripItem *> trips;
while (--i >= 0) {
struct dive *dive = get_dive(i);
update_cylinder_related_info(dive);
@@ -598,7 +606,7 @@ void DiveTripModel::setupModelData()
diveItem->diveId = dive->id;
if (!trip || currentLayout == LIST) {
- diveItem->parent = rootItem;
+ diveItem->parent = rootItem.get();
rootItem->children.push_back(diveItem);
continue;
}
@@ -608,7 +616,7 @@ void DiveTripModel::setupModelData()
if (!trips.keys().contains(trip)) {
TripItem *tripItem = new TripItem();
tripItem->trip = trip;
- tripItem->parent = rootItem;
+ tripItem->parent = rootItem.get();
tripItem->children.push_back(diveItem);
trips[trip] = tripItem;
rootItem->children.push_back(tripItem);
diff --git a/qt-models/divetripmodel.h b/qt-models/divetripmodel.h
index fc17aff92..32a656bfe 100644
--- a/qt-models/divetripmodel.h
+++ b/qt-models/divetripmodel.h
@@ -93,6 +93,7 @@ public:
CURRENT
};
+ static DiveTripModel *instance();
Qt::ItemFlags flags(const QModelIndex &index) const;
QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const;
bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole);
@@ -102,7 +103,6 @@ public:
private:
void setupModelData();
- QMap<dive_trip_t *, TripItem *> trips;
Layout currentLayout;
};
diff --git a/qt-models/treemodel.cpp b/qt-models/treemodel.cpp
index 5ddd2d3a2..3c76cc939 100644
--- a/qt-models/treemodel.cpp
+++ b/qt-models/treemodel.cpp
@@ -32,12 +32,12 @@ QVariant TreeItem::data(int, int) const
TreeModel::TreeModel(QObject *parent) : QAbstractItemModel(parent)
{
columns = 0; // I'm not sure about this one - I can't see where it gets initialized
- rootItem = new TreeItem();
+ rootItem.reset(new TreeItem);
}
-TreeModel::~TreeModel()
+void TreeModel::clear()
{
- delete rootItem;
+ rootItem.reset(new TreeItem);
}
QVariant TreeModel::data(const QModelIndex &index, int role) const
@@ -64,7 +64,7 @@ QModelIndex TreeModel::index(int row, int column, const QModelIndex &parent) con
if (!hasIndex(row, column, parent))
return QModelIndex();
- TreeItem *parentItem = (!parent.isValid()) ? rootItem : static_cast<TreeItem *>(parent.internalPointer());
+ TreeItem *parentItem = (!parent.isValid()) ? rootItem.get() : static_cast<TreeItem *>(parent.internalPointer());
TreeItem *childItem = parentItem->children[row];
@@ -79,7 +79,7 @@ QModelIndex TreeModel::parent(const QModelIndex &index) const
TreeItem *childItem = static_cast<TreeItem *>(index.internalPointer());
TreeItem *parentItem = childItem->parent;
- if (parentItem == rootItem || !parentItem)
+ if (parentItem == rootItem.get() || !parentItem)
return QModelIndex();
return createIndex(parentItem->row(), 0, parentItem);
@@ -90,7 +90,7 @@ int TreeModel::rowCount(const QModelIndex &parent) const
TreeItem *parentItem;
if (!parent.isValid())
- parentItem = rootItem;
+ parentItem = rootItem.get();
else
parentItem = static_cast<TreeItem *>(parent.internalPointer());
diff --git a/qt-models/treemodel.h b/qt-models/treemodel.h
index e68ae1c32..3b2b407e0 100644
--- a/qt-models/treemodel.h
+++ b/qt-models/treemodel.h
@@ -4,6 +4,7 @@
#include <QAbstractItemModel>
#include <QCoreApplication>
+#include <memory>
struct TreeItem {
Q_DECLARE_TR_FUNCTIONS(TreeItemDT)
@@ -25,16 +26,16 @@ class TreeModel : public QAbstractItemModel {
Q_OBJECT
public:
TreeModel(QObject *parent = 0);
- ~TreeModel();
QVariant data(const QModelIndex &index, int role) const;
int rowCount(const QModelIndex &parent = QModelIndex()) const;
int columnCount(const QModelIndex &parent = QModelIndex()) const;
QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const;
QModelIndex parent(const QModelIndex &child) const;
+ void clear();
protected:
int columns;
- TreeItem *rootItem;
+ std::unique_ptr<TreeItem> rootItem;
};
#endif
diff --git a/qt-models/yearlystatisticsmodel.cpp b/qt-models/yearlystatisticsmodel.cpp
index 9cbb19a1d..c83a803c2 100644
--- a/qt-models/yearlystatisticsmodel.cpp
+++ b/qt-models/yearlystatisticsmodel.cpp
@@ -187,7 +187,7 @@ void YearlyStatisticsModel::update_yearly_stats()
month++;
}
rootItem->children.append(item);
- item->parent = rootItem;
+ item->parent = rootItem.get();
}
@@ -199,7 +199,7 @@ void YearlyStatisticsModel::update_yearly_stats()
iChild->parent = item;
}
rootItem->children.append(item);
- item->parent = rootItem;
+ item->parent = rootItem.get();
}
/* Show the statistic sorted by dive type */
@@ -213,6 +213,6 @@ void YearlyStatisticsModel::update_yearly_stats()
iChild->parent = item;
}
rootItem->children.append(item);
- item->parent = rootItem;
+ item->parent = rootItem.get();
}
}