|
17 | 17 | */ |
18 | 18 |
|
19 | 19 | #include <QCloseEvent> |
| 20 | +#include <QButtonGroup> |
| 21 | +#include <QFile> |
| 22 | +#include <QIcon> |
| 23 | +#include <QLabel> |
20 | 24 | #include "SettingsDialog.h" |
21 | 25 | #include "ui_SettingsDialog.h" |
22 | 26 | #include "Settings.h" |
23 | 27 | #include "AutoCorrectDialog.h" |
| 28 | +#include "ToggleSwitch.h" |
24 | 29 |
|
25 | | -SettingsDialog::SettingsDialog(QWidget *parent) : |
| 30 | +SettingsDialog::SettingsDialog(bool darkMode, QWidget *parent) : |
26 | 31 | QDialog(parent), |
27 | | - ui(new Ui::SettingsDialog) { |
| 32 | + ui(new Ui::SettingsDialog), |
| 33 | + m_darkMode(darkMode) { |
28 | 34 | ui->setupUi(this); |
29 | 35 | autoCorrectDialog = new AutoCorrectDialog(this); |
30 | 36 |
|
| 37 | + applyTheme(); |
| 38 | + |
31 | 39 | ui->cmbOrientation->insertItems(0, {"Horizontal", "Vertical"}); |
32 | 40 | ui->cmbEncoding->insertItems(0, {"Unicode", "ANSI"}); |
33 | 41 | ui->cmbKarOrder->insertItems(0, {"Modern", "Old"}); |
34 | 42 |
|
| 43 | + setupSidebar(); |
35 | 44 | implementSignals(); |
36 | 45 | updateSettings(); |
37 | 46 | } |
38 | 47 |
|
| 48 | +void SettingsDialog::applyTheme() { |
| 49 | + // Load the matching theme stylesheet. Applied on the dialog so it cascades |
| 50 | + // to every child widget, including the ToggleSwitch qproperty colors. |
| 51 | + QString qssPath = m_darkMode ? ":/styles/dark.qss" : ":/styles/light.qss"; |
| 52 | + QFile file(qssPath); |
| 53 | + if (file.open(QFile::ReadOnly | QFile::Text)) { |
| 54 | + setStyleSheet(QString::fromUtf8(file.readAll())); |
| 55 | + } |
| 56 | + |
| 57 | + // Icons ship in black/white variants; pick the one that reads on the theme. |
| 58 | + QString theme = m_darkMode ? "white" : "black"; |
| 59 | + auto icon = [&](const QString &name) { |
| 60 | + return QIcon(":/images/" + theme + "/" + name + ".svg"); |
| 61 | + }; |
| 62 | + ui->sidebarGeneral->setIcon(icon("settings")); |
| 63 | + ui->sidebarPhonetic->setIcon(icon("translate")); |
| 64 | + ui->sidebarFixed->setIcon(icon("layout")); |
| 65 | + ui->pageGeneralIcon->setPixmap(icon("settings").pixmap(24, 24)); |
| 66 | + ui->pagePhoneticIcon->setPixmap(icon("translate").pixmap(24, 24)); |
| 67 | + ui->pageFixedIcon->setPixmap(icon("layout").pixmap(24, 24)); |
| 68 | +} |
| 69 | + |
| 70 | +void SettingsDialog::setupSidebar() { |
| 71 | + auto *group = new QButtonGroup(this); |
| 72 | + group->setExclusive(true); |
| 73 | + group->addButton(ui->sidebarGeneral, 0); |
| 74 | + group->addButton(ui->sidebarPhonetic, 1); |
| 75 | + group->addButton(ui->sidebarFixed, 2); |
| 76 | + ui->sidebarGeneral->setChecked(true); |
| 77 | + ui->pages->setCurrentIndex(0); |
| 78 | + connect(group, QOverload<int>::of(&QButtonGroup::idClicked), |
| 79 | + ui->pages, &QStackedWidget::setCurrentIndex); |
| 80 | +} |
| 81 | + |
| 82 | +void SettingsDialog::bindToggle(ToggleSwitch *sw, QLabel *status) { |
| 83 | + auto update = [status](bool on) { status->setText(on ? "On" : "Off"); }; |
| 84 | + connect(sw, &QAbstractButton::toggled, status, update); |
| 85 | + update(sw->isChecked()); |
| 86 | +} |
| 87 | + |
39 | 88 | SettingsDialog::~SettingsDialog() { |
40 | 89 | delete autoCorrectDialog; |
41 | 90 | delete ui; |
42 | 91 | } |
43 | 92 |
|
44 | 93 | void SettingsDialog::implementSignals() { |
45 | 94 | // General Group |
46 | | - connect(ui->btnEnterClosePW, &QPushButton::toggled, [=](bool checked) { |
47 | | - ui->btnEnterClosePW->setText(checked ? "On" : "Off"); |
48 | | - }); |
49 | | - connect(ui->btnIncludeEnglishPrevWin, &QPushButton::toggled, [=](bool checked) { |
50 | | - ui->btnIncludeEnglishPrevWin->setText(checked ? "On" : "Off"); |
51 | | - }); |
52 | | - connect(ui->btnSmartQuote, &QPushButton::toggled, [=](bool checked) { |
53 | | - ui->btnSmartQuote->setText(checked ? "On" : "Off"); |
54 | | - }); |
55 | | - |
| 95 | + bindToggle(ui->btnEnterClosePW, ui->btnEnterClosePWStatus); |
| 96 | + bindToggle(ui->btnIncludeEnglishPrevWin, ui->btnIncludeEnglishPrevWinStatus); |
| 97 | + bindToggle(ui->btnSmartQuote, ui->btnSmartQuoteStatus); |
| 98 | + |
56 | 99 | // Phonetic Keyboard Layout Group. |
57 | | - connect(ui->btnSuggestionPhonetic, &QPushButton::toggled, [=](bool checked) { |
58 | | - ui->btnSuggestionPhonetic->setText(checked ? "On" : "Off"); |
59 | | - // Control other Preview window related settings. |
60 | | - ui->btnACUpdate->setEnabled(checked); |
61 | | - }); |
| 100 | + bindToggle(ui->btnSuggestionPhonetic, ui->btnSuggestionPhoneticStatus); |
| 101 | + // Control other Preview window related settings. |
| 102 | + connect(ui->btnSuggestionPhonetic, &QAbstractButton::toggled, |
| 103 | + ui->btnACUpdate, &QWidget::setEnabled); |
| 104 | + ui->btnACUpdate->setEnabled(ui->btnSuggestionPhonetic->isChecked()); |
62 | 105 | connect(ui->btnACUpdate, &QPushButton::clicked, [=]() { |
63 | 106 | autoCorrectDialog->open(); |
64 | 107 | }); |
65 | 108 |
|
66 | 109 | // Fixed Keyboard Layout Group. |
67 | | - connect(ui->btnSuggestionFixed, &QPushButton::toggled, [=](bool checked) { |
68 | | - ui->btnSuggestionFixed->setText(checked ? "On" : "Off"); |
69 | | - }); |
70 | | - connect(ui->btnAutoVowel, &QPushButton::toggled, [=](bool checked) { |
71 | | - ui->btnAutoVowel->setText(checked ? "On" : "Off"); |
72 | | - }); |
73 | | - connect(ui->btnKarJoining, &QPushButton::toggled, [=](bool checked) { |
74 | | - ui->btnKarJoining->setText(checked ? "On" : "Off"); |
75 | | - }); |
76 | | - connect(ui->btnAutoChandra, &QPushButton::toggled, [=](bool checked) { |
77 | | - ui->btnAutoChandra->setText(checked ? "On" : "Off"); |
78 | | - }); |
79 | | - connect(ui->btnOldReph, &QPushButton::toggled, [=](bool checked) { |
80 | | - ui->btnOldReph->setText(checked ? "On" : "Off"); |
81 | | - }); |
82 | | - connect(ui->btnNumberpad, &QPushButton::toggled, [=](bool checked) { |
83 | | - ui->btnNumberpad->setText(checked ? "On" : "Off"); |
84 | | - }); |
| 110 | + bindToggle(ui->btnSuggestionFixed, ui->btnSuggestionFixedStatus); |
| 111 | + bindToggle(ui->btnAutoVowel, ui->btnAutoVowelStatus); |
| 112 | + bindToggle(ui->btnKarJoining, ui->btnKarJoiningStatus); |
| 113 | + bindToggle(ui->btnAutoChandra, ui->btnAutoChandraStatus); |
| 114 | + bindToggle(ui->btnOldReph, ui->btnOldRephStatus); |
| 115 | + bindToggle(ui->btnNumberpad, ui->btnNumberpadStatus); |
85 | 116 |
|
86 | 117 | connect(ui->btnOK, &QPushButton::clicked, [=]() { |
87 | 118 | saveSettings(); |
|
0 commit comments