From 89e0c3f46498ba3e0844514b6709a07c200a68df Mon Sep 17 00:00:00 2001 From: Berthold Stoeger Date: Wed, 25 Jul 2018 21:23:19 +0200 Subject: 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 --- desktop-widgets/divelistview.cpp | 20 +++++++------------- 1 file changed, 7 insertions(+), 13 deletions(-) (limited to 'desktop-widgets/divelistview.cpp') 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(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(model()); sortByColumn(sortColumn, currentOrder); if (amount_selected && current_dive != NULL) { selectDive(selected_dive, true); -- cgit v1.2.3-70-g09d2