subsurface/desktop-widgets/preferences/preferencesdialog.cpp
Berthold Stoeger ccdd92aeb7 preferences: use std::string in struct preferences
This is a messy commit, because the "qPref" system relies
heavily on QString, which means lots of conversions between
the two worlds. Ultimately, I plan to base the preferences
system on std::string and only convert to QString when
pushing through Qt's property system or when writing into
Qt's settings.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2024-08-13 19:28:30 +02:00

131 lines
3.5 KiB
C++

// SPDX-License-Identifier: GPL-2.0
#include "preferencesdialog.h"
#include "abstractpreferenceswidget.h"
#include "preferences_language.h"
#include "preferences_georeference.h"
#include "preferences_defaults.h"
#include "preferences_units.h"
#include "preferences_graph.h"
#include "preferences_network.h"
#include "preferences_cloud.h"
#include "preferences_equipment.h"
#include "preferences_media.h"
#include "preferences_log.h"
#include "preferences_reset.h"
#include "core/qthelper.h"
#include "core/subsurface-qt/divelistnotifier.h"
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QListWidget>
#include <QStackedWidget>
#include <QDialogButtonBox>
#include <QAbstractButton>
PreferencesDialog *PreferencesDialog::instance()
{
static PreferencesDialog *self = new PreferencesDialog();
return self;
}
static bool abstractpreferenceswidget_lessthan(const AbstractPreferencesWidget *p1, const AbstractPreferencesWidget *p2)
{
return p1->positionHeight() < p2->positionHeight();
}
PreferencesDialog::PreferencesDialog()
{
setWindowIcon(QIcon(":subsurface-icon"));
setWindowTitle(tr("Preferences"));
pagesList = new QListWidget();
pagesStack = new QStackedWidget();
buttonBox = new QDialogButtonBox(
QDialogButtonBox::Save |
QDialogButtonBox::Apply |
QDialogButtonBox::Cancel);
pagesList->setMinimumWidth(140);
pagesList->setMaximumWidth(140);
QHBoxLayout *h = new QHBoxLayout();
h->addWidget(pagesList);
h->addWidget(pagesStack);
QVBoxLayout *v = new QVBoxLayout();
v->addLayout(h);
v->addWidget(buttonBox);
setLayout(v);
pages.push_back(new PreferencesLanguage);
pages.push_back(new PreferencesGeoreference);
pages.push_back(new PreferencesDefaults);
pages.push_back(new PreferencesUnits);
pages.push_back(new PreferencesGraph);
pages.push_back(new PreferencesNetwork);
pages.push_back(new PreferencesCloud);
pages.push_back(new PreferencesEquipment);
pages.push_back(new PreferencesMedia);
pages.push_back(new PreferencesLog);
pages.push_back(new PreferencesReset);
std::sort(pages.begin(), pages.end(), abstractpreferenceswidget_lessthan);
for (AbstractPreferencesWidget *page: pages) {
QListWidgetItem *item = new QListWidgetItem(page->icon(), page->name());
pagesList->addItem(item);
pagesStack->addWidget(page);
page->refreshSettings();
connect(page, &AbstractPreferencesWidget::settingsChanged, &diveListNotifier, &DiveListNotifier::settingsChanged);
}
connect(pagesList, &QListWidget::currentRowChanged,
pagesStack, &QStackedWidget::setCurrentIndex);
connect(buttonBox, &QDialogButtonBox::clicked,
this, &PreferencesDialog::buttonClicked);
}
PreferencesDialog::~PreferencesDialog()
{
}
void PreferencesDialog::buttonClicked(QAbstractButton* btn)
{
QDialogButtonBox::ButtonRole role = buttonBox->buttonRole(btn);
switch(role) {
case QDialogButtonBox::ApplyRole : applyRequested(false); return;
case QDialogButtonBox::AcceptRole : applyRequested(true); return;
case QDialogButtonBox::RejectRole : cancelRequested(); return;
case QDialogButtonBox::ResetRole : defaultsRequested(); return;
default: return;
}
}
void PreferencesDialog::refreshPages()
{
for (AbstractPreferencesWidget *page: pages)
page->refreshSettings();
}
void PreferencesDialog::applyRequested(bool closeIt)
{
for (AbstractPreferencesWidget *page: pages)
page->syncSettings();
emit diveListNotifier.settingsChanged();
if (closeIt)
accept();
}
void PreferencesDialog::cancelRequested()
{
refreshPages();
reject();
}
void PreferencesDialog::defaultsRequested()
{
prefs = default_prefs;
refreshPages();
emit diveListNotifier.settingsChanged();
accept();
}