diff options
author | aqua <aqua@iserlohn-fortress.net> | 2022-08-24 16:38:49 +0300 |
---|---|---|
committer | aqua <aqua@iserlohn-fortress.net> | 2022-08-28 09:49:20 +0300 |
commit | c5ed279da0e74adb79c6c2a3e485cb9668b1c130 (patch) | |
tree | 4271dcd3530bec7df25b01190317bee56533df94 /src/settings | |
parent | Load plugins from AppLocalDataLocation by default (diff) | |
download | rekonq-c5ed279da0e74adb79c6c2a3e485cb9668b1c130.tar.xz |
SettingsDialog: save settings when changed
- connect Restore Defaults button
Diffstat (limited to 'src/settings')
-rw-r--r-- | src/settings/CMakeLists.txt | 2 | ||||
-rw-r--r-- | src/settings/settingsdialog.cpp | 36 | ||||
-rw-r--r-- | src/settings/settingsdialog.h | 5 | ||||
-rw-r--r-- | src/settings/settingsdialog.ui | 32 | ||||
-rw-r--r-- | src/settings/settingswidget.cpp | 81 | ||||
-rw-r--r-- | src/settings/settingswidgets.hpp | 29 | ||||
-rw-r--r-- | src/settings/test/test_settings.cpp | 37 |
7 files changed, 158 insertions, 64 deletions
diff --git a/src/settings/CMakeLists.txt b/src/settings/CMakeLists.txt index f597cfb9..29a011c1 100644 --- a/src/settings/CMakeLists.txt +++ b/src/settings/CMakeLists.txt @@ -18,7 +18,7 @@ add_custom_command(OUTPUT shortcutssettingswidget.cpp DEPENDS ${PROJECT_SOURCE_D add_library(settings STATIC ${PROJECT_SOURCE_DIR}/include/rsettings.hpp settings.cpp settings.hpp settingsdialog.cpp settingsdialog.h settingsdialog.ui - settingswidgets.hpp helpers.hpp + settingswidgets.hpp settingswidget.cpp helpers.hpp generalsettingswidget.cpp appearancesettingswidget.cpp networksettingswidget.cpp shortcutssettingswidget.cpp ) target_link_libraries(settings PUBLIC Qt6::Widgets) diff --git a/src/settings/settingsdialog.cpp b/src/settings/settingsdialog.cpp index 0c54856e..68fa7a26 100644 --- a/src/settings/settingsdialog.cpp +++ b/src/settings/settingsdialog.cpp @@ -13,17 +13,45 @@ #include "settingsdialog.h" #include "settingswidgets.hpp" #include "ui_settingsdialog.h" +#include <QPushButton> SettingsDialog::SettingsDialog(RekonqSettings *settings, QWidget *parent) : QDialog(parent), ui(new Ui::SettingsDialog) { ui->setupUi(this); + saveBtn = ui->buttonBox->button(QDialogButtonBox::Save); + Q_CHECK_PTR(saveBtn); + restoreDefaultsBtn = ui->buttonBox->button(QDialogButtonBox::RestoreDefaults); + Q_CHECK_PTR(restoreDefaultsBtn); - ui->stackedWidget->addWidget(new GeneralSettingsWidget(settings, this)); - ui->stackedWidget->addWidget(new AppearanceSettingsWidget(settings, this)); - ui->stackedWidget->addWidget(new NetworkSettingsWidget(settings, this)); - ui->stackedWidget->addWidget(new ShortcutsSettingsWidget(settings, this)); + if (!settings->value("FirstRun", true).toBool()) saveBtn->setEnabled(false); + + addPage(new GeneralSettingsWidget(settings, this)); + addPage(new AppearanceSettingsWidget(settings, this)); + addPage(new NetworkSettingsWidget(settings, this)); + addPage(new ShortcutsSettingsWidget(settings, this)); connect(ui->listWidget, &QListWidget::currentRowChanged, ui->stackedWidget, &QStackedWidget::setCurrentIndex); + + connect(restoreDefaultsBtn, &QPushButton::clicked, this, [this]() { + for (auto *w : ui->stackedWidget->findChildren<SettingsWidget *>(QString(), Qt::FindDirectChildrenOnly)) { + w->reset(); + } + }); + + connect(this, &QDialog::accepted, this, [this]() { + for (auto *w : ui->stackedWidget->findChildren<SettingsWidget *>(QString(), Qt::FindDirectChildrenOnly)) { + w->save(); + } + }); } SettingsDialog::~SettingsDialog() { delete ui; } + +void SettingsDialog::addPage(SettingsWidget *page) +{ + auto *item = new QListWidgetItem(page->objectName(), ui->listWidget); + item->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter); + ui->stackedWidget->addWidget(page); + + connect(page, &SettingsWidget::changed, this, [this]() { saveBtn->setEnabled(true); }); +}
\ No newline at end of file diff --git a/src/settings/settingsdialog.h b/src/settings/settingsdialog.h index 0c793f9e..433857b3 100644 --- a/src/settings/settingsdialog.h +++ b/src/settings/settingsdialog.h @@ -15,6 +15,7 @@ #include <QDialog> class RekonqSettings; +class SettingsWidget; namespace Ui { class SettingsDialog; @@ -28,5 +29,9 @@ public: ~SettingsDialog() override; private: + void addPage(SettingsWidget *page); + Ui::SettingsDialog *ui; + QPushButton *saveBtn; + QPushButton *restoreDefaultsBtn; }; diff --git a/src/settings/settingsdialog.ui b/src/settings/settingsdialog.ui index 1c19380d..14ab30ef 100644 --- a/src/settings/settingsdialog.ui +++ b/src/settings/settingsdialog.ui @@ -33,38 +33,6 @@ <property name="itemAlignment"> <set>Qt::AlignVCenter</set> </property> - <item> - <property name="text"> - <string>General</string> - </property> - <property name="textAlignment"> - <set>AlignCenter</set> - </property> - </item> - <item> - <property name="text"> - <string>Appearance</string> - </property> - <property name="textAlignment"> - <set>AlignCenter</set> - </property> - </item> - <item> - <property name="text"> - <string>Network</string> - </property> - <property name="textAlignment"> - <set>AlignCenter</set> - </property> - </item> - <item> - <property name="text"> - <string>Shortcuts</string> - </property> - <property name="textAlignment"> - <set>AlignCenter</set> - </property> - </item> </widget> </item> <item> diff --git a/src/settings/settingswidget.cpp b/src/settings/settingswidget.cpp new file mode 100644 index 00000000..b5a2432c --- /dev/null +++ b/src/settings/settingswidget.cpp @@ -0,0 +1,81 @@ +#include "settingswidgets.hpp" + +#include <QCheckBox> +#include <QFontComboBox> +#include <QKeySequenceEdit> +#include <QLineEdit> +#include <QSpinBox> + +void SettingsWidget::save() +{ + if (objectName() != QLatin1String("General")) m_settings->beginGroup(objectName()); + + // Int + for (const auto *spinbox : findChildren<QSpinBox *>(QString(), Qt::FindDirectChildrenOnly)) { + m_settings->setValue(spinbox->objectName(), spinbox->value()); + } + + // Bool + for (const auto *checkbox : findChildren<QCheckBox *>(QString(), Qt::FindDirectChildrenOnly)) { + m_settings->setValue(checkbox->objectName(), checkbox->isChecked()); + } + + // String + for (const auto *lineedit : findChildren<QLineEdit *>(QString(), Qt::FindDirectChildrenOnly)) { + m_settings->setValue(lineedit->objectName(), lineedit->text()); + } + + // Font + for (const auto *font : findChildren<QFontComboBox *>(QString(), Qt::FindDirectChildrenOnly)) { + m_settings->setValue(font->objectName(), font->currentFont().family()); + } + + // Shortcut + for (const auto *shortcut : findChildren<QKeySequenceEdit *>(QString(), Qt::FindDirectChildrenOnly)) { + m_settings->setValue(shortcut->objectName(), shortcut->keySequence()); + } + + if (objectName() != QLatin1String("General")) m_settings->endGroup(); +} + +void SettingsWidget::reset() +{ + if (objectName() != QLatin1String("General")) m_settings->beginGroup(objectName()); + + // Int + for (auto *spinbox : findChildren<QSpinBox *>(QString(), Qt::FindDirectChildrenOnly)) { + const auto value = spinbox->property("defaultValue"); + spinbox->setValue(value.toInt()); + m_settings->setValue(spinbox->objectName(), value); + } + + // Bool + for (auto *checkbox : findChildren<QCheckBox *>(QString(), Qt::FindDirectChildrenOnly)) { + const auto value = checkbox->property("defaultValue"); + checkbox->setChecked(value.toBool()); + m_settings->setValue(checkbox->objectName(), value); + } + + // String + for (auto *lineedit : findChildren<QLineEdit *>(QString(), Qt::FindDirectChildrenOnly)) { + const auto value = lineedit->property("defaultValue"); + lineedit->setText(value.toString()); + m_settings->setValue(lineedit->objectName(), value); + } + + // Font + for (auto *font : findChildren<QFontComboBox *>(QString(), Qt::FindDirectChildrenOnly)) { + const auto value = font->property("defaultValue"); + font->setFont(QFont(value.toString())); + m_settings->setValue(font->objectName(), value); + } + + // Shortcut + for (auto *shortcut : findChildren<QKeySequenceEdit *>(QString(), Qt::FindDirectChildrenOnly)) { + const auto value = shortcut->property("defaultValue"); + shortcut->setKeySequence(value.toString()); + m_settings->setValue(shortcut->objectName(), value); + } + + if (objectName() != QLatin1String("General")) m_settings->endGroup(); +}
\ No newline at end of file diff --git a/src/settings/settingswidgets.hpp b/src/settings/settingswidgets.hpp index 27ebe8a2..25fbe0a4 100644 --- a/src/settings/settingswidgets.hpp +++ b/src/settings/settingswidgets.hpp @@ -16,51 +16,42 @@ class SettingsWidget : public QWidget { Q_OBJECT public: - explicit SettingsWidget(RekonqSettings *, QWidget *parent = nullptr) : QWidget(parent) {} + explicit SettingsWidget(RekonqSettings *settings, QWidget *parent = nullptr) : QWidget(parent) + { + m_settings = settings; + } signals: void changed(); + public slots: - virtual void save() = 0; - virtual void reset() = 0; + void save(); + void reset(); + +protected: + RekonqSettings *m_settings; }; class GeneralSettingsWidget final : public SettingsWidget { Q_OBJECT public: explicit GeneralSettingsWidget(RekonqSettings *, QWidget *parent = nullptr); - -public slots: - void save() override; - void reset() override; }; class AppearanceSettingsWidget final : public SettingsWidget { Q_OBJECT public: explicit AppearanceSettingsWidget(RekonqSettings *, QWidget *parent = nullptr); - -public slots: - void save() override; - void reset() override; }; class NetworkSettingsWidget final : public SettingsWidget { Q_OBJECT public: explicit NetworkSettingsWidget(RekonqSettings *, QWidget *parent = nullptr); - -public slots: - void save() override; - void reset() override; }; class ShortcutsSettingsWidget final : public SettingsWidget { Q_OBJECT public: explicit ShortcutsSettingsWidget(RekonqSettings *, QWidget *parent = nullptr); - -public slots: - void save() override; - void reset() override; }; diff --git a/src/settings/test/test_settings.cpp b/src/settings/test/test_settings.cpp index 2ca5ba1a..fdb976f6 100644 --- a/src/settings/test/test_settings.cpp +++ b/src/settings/test/test_settings.cpp @@ -7,22 +7,34 @@ #include <QApplication> #include <QDir> +#include <QLineEdit> #include <QStandardPaths> #include <gtest/gtest.h> -#include <iostream> - // Software under Test #include "../helpers.hpp" #include "../settings.hpp" #include "../settingsdialog.h" #include "settings_mock.hpp" +using ::testing::_; using ::testing::AtLeast; +using ::testing::ContainerEq; using ::testing::ReturnArg; const char *settingsFile = nullptr; +MATCHER_P(QStringEq, a, "") +{ + *result_listener << "where the arg is " << qUtf8Printable(arg); + return arg.compare(a) == 0; +} +MATCHER_P(QVariantEq, a, "") +{ + *result_listener << "where the arg is " << qUtf8Printable(arg.toString()); + return arg.toString().compare(a) == 0; +} + TEST(settings, getFont) { const auto serif = getFont(QFont::Serif); @@ -60,12 +72,21 @@ TEST(settings, Settings) TEST(settings, SettingsDialog) { - MockSettings settings; - EXPECT_CALL(settings, beginGroup).Times(4); - EXPECT_CALL(settings, endGroup).Times(4); - EXPECT_CALL(settings, value).Times(AtLeast(4)).WillRepeatedly(ReturnArg<1>()); - - SettingsDialog dlg(&settings); + MockSettings mockSettings; + // There are 4 groups in total, but General should not be calling beginGroup/endGroup + // beginGroup/endGroup are called twice: during the ctor and when accepted + EXPECT_CALL(mockSettings, beginGroup).Times(3 * 2); + EXPECT_CALL(mockSettings, endGroup).Times(3 * 2); + // There are 35 settings in total, one of which is hidden and won't be set by the dialog + EXPECT_CALL(mockSettings, value).Times(35).WillRepeatedly(ReturnArg<1>()); + EXPECT_CALL(mockSettings, setValue(_, _)).Times(33); + EXPECT_CALL(mockSettings, setValue(QStringEq("homepage"), QVariantEq("about:blank"))); + + SettingsDialog dlg(&mockSettings); + auto *homepage = dlg.findChild<QLineEdit *>("homepage"); + EXPECT_TRUE(homepage->text() == QLatin1String("http://www.kde.org/")) << qUtf8Printable(homepage->text()); + homepage->setText("about:blank"); + dlg.accept(); } int main(int argc, char **argv) |