Files
QtZeiterfassung/dialogs/settingsdialog.cpp
T

103 lines
3.3 KiB
C++
Raw Permalink Normal View History

2017-12-04 20:11:11 +01:00
#include "settingsdialog.h"
#include "ui_settingsdialog.h"
2017-12-05 21:24:55 +01:00
#include <QMessageBox>
2017-12-12 21:19:17 +01:00
#include <QStringBuilder>
#include <QDir>
#include <QApplication>
#include <QFile>
#include <QTextStream>
2017-12-05 21:24:55 +01:00
2017-12-04 20:11:11 +01:00
#include "zeiterfassungsettings.h"
SettingsDialog::SettingsDialog(ZeiterfassungSettings &settings, QWidget *parent) :
QDialog(parent),
ui(new Ui::SettingsDialog),
m_settings(settings)
{
ui->setupUi(this);
2017-12-05 21:24:55 +01:00
ui->comboBoxLanguage->addItem(tr("English"), QLocale::English);
ui->comboBoxLanguage->addItem(tr("German"), QLocale::German);
2017-12-12 21:19:17 +01:00
{
auto index = ui->comboBoxLanguage->findData(settings.language());
if(index == -1)
QMessageBox::warning(this, tr("Invalid settings!"), tr("Invalid settings!") % "\n\n" % tr("Unknown language!"));
ui->comboBoxLanguage->setCurrentIndex(index);
}
ui->comboBoxTheme->addItem(tr("Default"), QString());
for(const auto &entry : QDir(QDir(QCoreApplication::applicationDirPath()).absoluteFilePath(QStringLiteral("themes"))).entryInfoList(QStringList { QStringLiteral("*.qss") }, QDir::Files))
ui->comboBoxTheme->addItem(entry.baseName(), entry.baseName());
if(!settings.theme().isEmpty())
{
auto index = ui->comboBoxTheme->findData(settings.theme());
if(index == -1)
QMessageBox::warning(this, tr("Invalid settings!"), tr("Invalid settings!") % "\n\n" % tr("Unknown theme!"));
ui->comboBoxTheme->setCurrentIndex(index);
}
2017-12-04 20:11:11 +01:00
connect(ui->buttonBox, &QDialogButtonBox::accepted, this, &SettingsDialog::submit);
}
SettingsDialog::~SettingsDialog()
{
delete ui;
}
void SettingsDialog::submit()
{
2017-12-12 21:19:17 +01:00
auto warning = false;
if(ui->comboBoxLanguage->currentIndex() == -1 ||
ui->comboBoxTheme->currentIndex() == -1)
{
QMessageBox::warning(this, tr("Invalid settings!"), tr("Invalid settings!") % "\n\n" % tr("Please fill all options with valid values!"));
return;
}
2017-12-05 21:24:55 +01:00
if(ui->comboBoxLanguage->currentData().value<QLocale::Language>() != m_settings.language())
{
m_settings.setLanguage(ui->comboBoxLanguage->currentData().value<QLocale::Language>());
2017-12-12 21:19:17 +01:00
warning = true;
2017-12-05 21:24:55 +01:00
}
2017-12-04 20:11:11 +01:00
auto theme = ui->comboBoxTheme->currentData().toString();
if(theme != m_settings.theme())
2017-12-12 21:19:17 +01:00
{
if(theme.isEmpty())
qApp->setStyleSheet(QString());
else
{
auto themePath = QDir(QDir(QCoreApplication::applicationDirPath()).absoluteFilePath(QStringLiteral("themes"))).absoluteFilePath(theme);
QFile file(themePath % ".qss");
if(!file.exists())
{
QMessageBox::warning(this, tr("Could not load theme!"), tr("Could not load theme!") % "\n\n" % tr("Theme file does not exist!"));
return;
}
if(!file.open(QIODevice::ReadOnly | QIODevice::Text))
{
QMessageBox::warning(this, tr("Could not load theme!"), tr("Could not load theme!") % "\n\n" % file.errorString());
return;
}
QTextStream textStream(&file);
qApp->setStyleSheet(textStream.readAll().replace(QStringLiteral("@THEME_RESOURCES@"), themePath));
}
m_settings.setTheme(theme);
2017-12-12 21:19:17 +01:00
}
if(warning)
QMessageBox::information(this, tr("Restart required!"), tr("To apply the new settings a restart is required!"));
2017-12-04 20:11:11 +01:00
accept();
}