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 <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2020-11-24 19:57:14 +01:00 committed by Dirk Hohndel
parent 712e4680ca
commit 369c5b0dc6
3 changed files with 29 additions and 40 deletions

View file

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

View file

@ -34,7 +34,6 @@ signals:
public
slots:
void settingsChanged();
void reloadHeaderActions();
private
slots:
void toggleColumnVisibilityByIndex();

View file

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