summaryrefslogtreecommitdiffstats
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
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>
-rw-r--r--desktop-widgets/divelistview.cpp20
-rw-r--r--desktop-widgets/divelistview.h2
-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
7 files changed, 31 insertions, 28 deletions
diff --git a/desktop-widgets/divelistview.cpp b/desktop-widgets/divelistview.cpp
index eb225e593..57bc0d9e1 100644
--- a/desktop-widgets/divelistview.cpp
+++ b/desktop-widgets/divelistview.cpp
@@ -36,8 +36,7 @@ DiveListView::DiveListView(QWidget *parent) : QTreeView(parent), mouseClickSelec
model->setSortRole(DiveTripModel::SORT_ROLE);
model->setFilterKeyColumn(-1); // filter all columns
model->setFilterCaseSensitivity(Qt::CaseInsensitive);
- DiveTripModel *tripModel = new DiveTripModel(this);
- model->setSourceModel(tripModel);
+ model->setSourceModel(DiveTripModel::instance());
setModel(model);
setSortingEnabled(false);
@@ -50,7 +49,7 @@ DiveListView::DiveListView(QWidget *parent) : QTreeView(parent), mouseClickSelec
installEventFilter(this);
for (int i = DiveTripModel::NR; i < DiveTripModel::COLUMNS; i++)
- calculateInitialColumnWidth(tripModel, i);
+ calculateInitialColumnWidth(i);
setColumnWidths();
}
@@ -72,13 +71,13 @@ DiveListView::~DiveListView()
settings.endGroup();
}
-void DiveListView::calculateInitialColumnWidth(const DiveTripModel &tripModel, int col)
+void DiveListView::calculateInitialColumnWidth(int col)
{
const QFontMetrics metrics(defaultModelFont());
int em = metrics.width('m');
int zw = metrics.width('0');
- QString header_txt = tripModel.headerData(col, Qt::Horizontal, Qt::DisplayRole).toString();
+ QString header_txt = DiveTripModel::instance()->headerData(col, Qt::Horizontal, Qt::DisplayRole).toString();
int width = metrics.width(header_txt);
int sw = 0;
switch (col) {
@@ -422,18 +421,13 @@ void DiveListView::reload(DiveTripModel::Layout layout, bool forceSort)
header()->setSectionsClickable(true);
connect(header(), SIGNAL(sectionPressed(int)), this, SLOT(headerClicked(int)), Qt::UniqueConnection);
- QSortFilterProxyModel *m = qobject_cast<QSortFilterProxyModel *>(model());
- QAbstractItemModel *oldModel = m->sourceModel();
- DiveTripModel *tripModel = new DiveTripModel(this);
- tripModel->setLayout(layout);
-
- m->setSourceModel(tripModel);
- if (oldModel)
- delete oldModel;
+ DiveTripModel *tripModel = DiveTripModel::instance();
+ tripModel->setLayout(layout); // Note: setLayout() resets the whole model
if (!forceSort)
return;
+ QSortFilterProxyModel *m = qobject_cast<QSortFilterProxyModel *>(model());
sortByColumn(sortColumn, currentOrder);
if (amount_selected && current_dive != NULL) {
selectDive(selected_dive, true);
diff --git a/desktop-widgets/divelistview.h b/desktop-widgets/divelistview.h
index 396bef8ce..7e2b6425d 100644
--- a/desktop-widgets/divelistview.h
+++ b/desktop-widgets/divelistview.h
@@ -78,7 +78,7 @@ private:
QMultiHash<dive_trip_t *, int> selectedDives;
void merge_trip(const QModelIndex &a, const int offset);
void setColumnWidths();
- void calculateInitialColumnWidth(const DiveTripModel &tripModel, int col);
+ void calculateInitialColumnWidth(int col);
void backupExpandedRows();
void restoreExpandedRows();
int lastVisibleColumn();
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();
}
}