From 369c5b0dc656cbf4da2c8185debc355af4243cc5 Mon Sep 17 00:00:00 2001 From: Berthold Stoeger Date: Tue, 24 Nov 2020 19:57:14 +0100 Subject: [PATCH] desktop: init dive list header actions in constructor The main window called a function to init the header actions (i.e. the context menu) of the dive-list. There is no reason why this shouldn't be done in the constructor of the dive list, since it only accesses the QSettings, which are available at application startup. This improves modularity of the code (by a tiny, tiny bit). Moreover, the initialization function was at the same time the header-reloading function. That function can now be folded into the settings-changed function, since that is the only remaining user. Signed-off-by: Berthold Stoeger --- desktop-widgets/divelistview.cpp | 66 ++++++++++++++------------------ desktop-widgets/divelistview.h | 1 - desktop-widgets/mainwindow.cpp | 2 - 3 files changed, 29 insertions(+), 40 deletions(-) diff --git a/desktop-widgets/divelistview.cpp b/desktop-widgets/divelistview.cpp index 07f7435c0..14973cf0a 100644 --- a/desktop-widgets/divelistview.cpp +++ b/desktop-widgets/divelistview.cpp @@ -61,6 +61,32 @@ DiveListView::DiveListView(QWidget *parent) : QTreeView(parent), for (int i = DiveTripModelBase::NR; i < DiveTripModelBase::COLUMNS; i++) calculateInitialColumnWidth(i); setColumnWidths(); + + QSettings s; + s.beginGroup("DiveListColumnState"); + for (int i = 0; i < model()->columnCount(); i++) { + QString title = QString("%1").arg(model()->headerData(i, Qt::Horizontal).toString()); + QString settingName = QString("showColumn%1").arg(i); + QAction *a = new QAction(title, header()); + bool showHeaderFirstRun = !(i == DiveTripModelBase::MAXCNS || + i == DiveTripModelBase::GAS || + i == DiveTripModelBase::OTU || + i == DiveTripModelBase::TEMPERATURE || + i == DiveTripModelBase::TOTALWEIGHT || + i == DiveTripModelBase::SUIT || + i == DiveTripModelBase::CYLINDER || + i == DiveTripModelBase::SAC || + i == DiveTripModelBase::TAGS); + bool shown = s.value(settingName, showHeaderFirstRun).toBool(); + a->setCheckable(true); + a->setChecked(shown); + a->setProperty("index", i); + a->setProperty("settingName", settingName); + connect(a, SIGNAL(triggered(bool)), this, SLOT(toggleColumnVisibilityByIndex())); + header()->addAction(a); + setColumnHidden(i, !shown); + } + s.endGroup(); } DiveListView::~DiveListView() @@ -353,44 +379,10 @@ void DiveListView::reload() void DiveListView::settingsChanged() { update(); - reloadHeaderActions(); -} -void DiveListView::reloadHeaderActions() -{ - // Populate the context menu of the headers that will show - // the menu to show / hide columns. - if (!header()->actions().size()) { - QSettings s; - s.beginGroup("DiveListColumnState"); - for (int i = 0; i < model()->columnCount(); i++) { - QString title = QString("%1").arg(model()->headerData(i, Qt::Horizontal).toString()); - QString settingName = QString("showColumn%1").arg(i); - QAction *a = new QAction(title, header()); - bool showHeaderFirstRun = !(i == DiveTripModelBase::MAXCNS || - i == DiveTripModelBase::GAS || - i == DiveTripModelBase::OTU || - i == DiveTripModelBase::TEMPERATURE || - i == DiveTripModelBase::TOTALWEIGHT || - i == DiveTripModelBase::SUIT || - i == DiveTripModelBase::CYLINDER || - i == DiveTripModelBase::SAC || - i == DiveTripModelBase::TAGS); - bool shown = s.value(settingName, showHeaderFirstRun).toBool(); - a->setCheckable(true); - a->setChecked(shown); - a->setProperty("index", i); - a->setProperty("settingName", settingName); - connect(a, SIGNAL(triggered(bool)), this, SLOT(toggleColumnVisibilityByIndex())); - header()->addAction(a); - setColumnHidden(i, !shown); - } - s.endGroup(); - } else { - for (int i = 0; i < model()->columnCount(); i++) { - QString title = QString("%1").arg(model()->headerData(i, Qt::Horizontal).toString()); - header()->actions()[i]->setText(title); - } + for (int i = 0; i < model()->columnCount(); i++) { + QString title = model()->headerData(i, Qt::Horizontal).toString(); + header()->actions()[i]->setText(title); } } diff --git a/desktop-widgets/divelistview.h b/desktop-widgets/divelistview.h index 990d0446b..1853dab1d 100644 --- a/desktop-widgets/divelistview.h +++ b/desktop-widgets/divelistview.h @@ -34,7 +34,6 @@ signals: public slots: void settingsChanged(); - void reloadHeaderActions(); private slots: void toggleColumnVisibilityByIndex(); diff --git a/desktop-widgets/mainwindow.cpp b/desktop-widgets/mainwindow.cpp index dfe039c51..c4a234b72 100644 --- a/desktop-widgets/mainwindow.cpp +++ b/desktop-widgets/mainwindow.cpp @@ -219,8 +219,6 @@ MainWindow::MainWindow() : QMainWindow(), graphics->setEmptyState(); initialUiSetup(); readSettings(); - diveList->reload(); - diveList->reloadHeaderActions(); diveList->setFocus(); MapWidget::instance()->reload(); diveList->expand(diveList->model()->index(0, 0));