diff options
author | Berthold Stoeger <bstoeger@mail.tuwien.ac.at> | 2018-07-25 21:23:19 +0200 |
---|---|---|
committer | Dirk Hohndel <dirk@hohndel.org> | 2018-09-01 07:48:43 -0700 |
commit | 89e0c3f46498ba3e0844514b6709a07c200a68df (patch) | |
tree | 8609c84130cb04180d9db4bb43f2dfc11cf563c3 /desktop-widgets | |
parent | 236f0512bec2946b35052abf637c7c8df97f34bf (diff) | |
download | subsurface-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 'desktop-widgets')
-rw-r--r-- | desktop-widgets/divelistview.cpp | 20 | ||||
-rw-r--r-- | desktop-widgets/divelistview.h | 2 |
2 files changed, 8 insertions, 14 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(); |