1
0
forked from blue/squawk
squawk/ui/widgets/settings/settings.cpp

99 lines
3.2 KiB
C++
Raw Normal View History

/*
* Squawk messenger.
* Copyright (C) 2019 Yury Gubich <blue@macaw.me>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
2022-01-16 19:54:57 +00:00
#include "settings.h"
#include "ui_settings.h"
Settings::Settings(QWidget* parent):
QWidget(parent),
2022-01-21 19:02:50 +00:00
m_ui(new Ui::Settings()),
modifiedSettings()
2022-01-16 19:54:57 +00:00
{
m_ui->setupUi(this);
connect(m_ui->list, &QListWidget::currentItemChanged, this, &Settings::onCurrentPageChanged);
2022-01-21 19:02:50 +00:00
connect(m_ui->General, &PageGeneral::variableModified, this, &Settings::onVariableModified);
connect(m_ui->Appearance, &PageAppearance::variableModified, this, &Settings::onVariableModified);
connect(m_ui->applyButton, &QPushButton::clicked, this, &Settings::apply);
connect(m_ui->okButton, &QPushButton::clicked, this, &Settings::confirm);
connect(m_ui->cancelButton, &QPushButton::clicked, this, &Settings::close);
2022-01-16 19:54:57 +00:00
}
Settings::~Settings()
{
}
void Settings::onCurrentPageChanged(QListWidgetItem* current)
{
if (current != nullptr) {
m_ui->header->setText(current->text());
m_ui->content->setCurrentIndex(m_ui->list->currentRow());
}
}
2022-01-21 19:02:50 +00:00
void Settings::onVariableModified(const QString& key, const QVariant& value)
{
modifiedSettings[key] = value;
}
void Settings::apply()
{
QSettings settings;
bool trayChanged = false;
2022-01-21 19:02:50 +00:00
for (const std::pair<const QString, QVariant>& pair: modifiedSettings) {
2022-01-27 17:44:32 +00:00
if (pair.first == "style") {
Shared::Global::setStyle(pair.second.toString());
2022-02-19 18:31:49 +00:00
settings.setValue(pair.first, pair.second);
2022-01-27 17:44:32 +00:00
} else if (pair.first == "theme") {
Shared::Global::setTheme(pair.second.toString());
2022-02-19 18:31:49 +00:00
settings.setValue(pair.first, pair.second);
} else if (pair.first == "downloadsPath") {
QString path = pair.second.toString();
if (!Shared::isSubdirectoryOfSettings(path)) {
path = Shared::getAbsoluteWritablePath(path);
if (path.size() > 0) {
settings.setValue(pair.first, path);
emit changeDownloadsPath(path);
}
}
} else if (pair.first == "tray" || pair.first == "hideTray") {
bool oldValue = settings.value(pair.first, false).toBool();
bool newValue = pair.second.toBool();
if (oldValue != newValue) {
trayChanged = true;
settings.setValue(pair.first, pair.second);
}
2022-01-21 19:02:50 +00:00
}
}
2022-01-21 19:02:50 +00:00
if (trayChanged) {
emit changeTray(settings.value("tray", false).toBool(), settings.value("hideTray", false).toBool());
2022-01-21 19:02:50 +00:00
}
modifiedSettings.clear();
}
void Settings::confirm()
{
apply();
close();
}