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>
This commit is contained in:
Berthold Stoeger 2018-07-25 21:23:19 +02:00 committed by Dirk Hohndel
parent 236f0512be
commit 89e0c3f464
7 changed files with 31 additions and 28 deletions

View file

@ -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);

View file

@ -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();

View file

@ -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);

View file

@ -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;
};

View file

@ -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());

View file

@ -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

View file

@ -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();
}
}