1 | /* |
2 | * Copyright (C) 2020-2022 Roy Qu (royqh1979@gmail.com) |
3 | * |
4 | * This program is free software: you can redistribute it and/or modify |
5 | * it under the terms of the GNU General Public License as published by |
6 | * the Free Software Foundation, either version 3 of the License, or |
7 | * (at your option) any later version. |
8 | * |
9 | * This program is distributed in the hope that it will be useful, |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 | * GNU General Public License for more details. |
13 | * |
14 | * You should have received a copy of the GNU General Public License |
15 | * along with this program. If not, see <https://www.gnu.org/licenses/>. |
16 | */ |
17 | #include "settingswidget.h" |
18 | |
19 | #include <QAbstractItemView> |
20 | #include <QCheckBox> |
21 | #include <QComboBox> |
22 | #include <QGroupBox> |
23 | #include <QLineEdit> |
24 | #include <QListView> |
25 | #include <QMessageBox> |
26 | #include <QPlainTextEdit> |
27 | #include <QListWidget> |
28 | #include <QRadioButton> |
29 | #include <QSpinBox> |
30 | #include "../widgets/coloredit.h" |
31 | #include "../utils.h" |
32 | #include "../iconsmanager.h" |
33 | |
34 | SettingsWidget::SettingsWidget(const QString &name, const QString &group, QWidget *parent): |
35 | QWidget(parent), |
36 | mSettingsChanged(false), |
37 | mGroup(group), |
38 | mName(name) |
39 | { |
40 | } |
41 | |
42 | void SettingsWidget::init() |
43 | { |
44 | connect(pIconsManager,&IconsManager::actionIconsUpdated, |
45 | this, &SettingsWidget::onUpdateIcons); |
46 | onUpdateIcons(); |
47 | load(); |
48 | connectInputs(); |
49 | } |
50 | |
51 | void SettingsWidget::load() |
52 | { |
53 | try { |
54 | doLoad(); |
55 | clearSettingsChanged(); |
56 | } catch (FileError & e) { |
57 | QMessageBox::warning(nullptr, |
58 | tr("Load Error" ), |
59 | e.reason()); |
60 | } |
61 | } |
62 | |
63 | void SettingsWidget::save() |
64 | { |
65 | try { |
66 | doSave(); |
67 | clearSettingsChanged(); |
68 | } catch (FileError & e) { |
69 | QMessageBox::warning(nullptr, |
70 | tr("Save Error" ), |
71 | e.reason()); |
72 | } |
73 | } |
74 | |
75 | void SettingsWidget::connectAbstractItemView(QAbstractItemView *pView) |
76 | { |
77 | connect(pView->model(),&QAbstractItemModel::rowsInserted,this,&SettingsWidget::setSettingsChanged); |
78 | connect(pView->model(),&QAbstractItemModel::rowsMoved,this,&SettingsWidget::setSettingsChanged); |
79 | connect(pView->model(),&QAbstractItemModel::rowsRemoved,this,&SettingsWidget::setSettingsChanged); |
80 | connect(pView->model(),&QAbstractItemModel::dataChanged,this,&SettingsWidget::setSettingsChanged); |
81 | connect(pView->model(),&QAbstractItemModel::modelReset,this,&SettingsWidget::setSettingsChanged); |
82 | } |
83 | |
84 | void SettingsWidget::disconnectAbstractItemView(QAbstractItemView *pView) |
85 | { |
86 | disconnect(pView->model(),&QAbstractItemModel::rowsInserted,this,&SettingsWidget::setSettingsChanged); |
87 | disconnect(pView->model(),&QAbstractItemModel::rowsMoved,this,&SettingsWidget::setSettingsChanged); |
88 | disconnect(pView->model(),&QAbstractItemModel::rowsRemoved,this,&SettingsWidget::setSettingsChanged); |
89 | disconnect(pView->model(),&QAbstractItemModel::dataChanged,this,&SettingsWidget::setSettingsChanged); |
90 | disconnect(pView->model(),&QAbstractItemModel::modelReset,this,&SettingsWidget::setSettingsChanged); |
91 | |
92 | } |
93 | |
94 | void SettingsWidget::updateIcons(const QSize &) |
95 | { |
96 | |
97 | } |
98 | |
99 | void SettingsWidget::connectInputs() |
100 | { |
101 | for (QLineEdit* p:findChildren<QLineEdit*>()) { |
102 | connect(p, &QLineEdit::textChanged, this, &SettingsWidget::setSettingsChanged); |
103 | } |
104 | for (QCheckBox* p:findChildren<QCheckBox*>()) { |
105 | connect(p, &QCheckBox::toggled, this, &SettingsWidget::setSettingsChanged); |
106 | } |
107 | for (QRadioButton* p:findChildren<QRadioButton*>()) { |
108 | connect(p, &QRadioButton::toggled, this, &SettingsWidget::setSettingsChanged); |
109 | } |
110 | for (QPlainTextEdit* p:findChildren<QPlainTextEdit*>()) { |
111 | connect(p, &QPlainTextEdit::textChanged, this, &SettingsWidget::setSettingsChanged); |
112 | } |
113 | for (QSpinBox* p:findChildren<QSpinBox*>()) { |
114 | connect(p, QOverload<int>::of(&QSpinBox::valueChanged), this, &SettingsWidget::setSettingsChanged); |
115 | } |
116 | for (ColorEdit* p:findChildren<ColorEdit*>()) { |
117 | connect(p, &ColorEdit::colorChanged, this, &SettingsWidget::setSettingsChanged); |
118 | } |
119 | for (QComboBox* p: findChildren<QComboBox*>()) { |
120 | connect(p, QOverload<int>::of(&QComboBox::currentIndexChanged) ,this, &SettingsWidget::setSettingsChanged); |
121 | } |
122 | for (QAbstractItemView* p: findChildren<QAbstractItemView*>()) { |
123 | connectAbstractItemView(p); |
124 | } |
125 | for (QListWidget* p:findChildren<QListWidget*>()) { |
126 | connect(p, QOverload<int>::of(&QListWidget::currentRowChanged) ,this, &SettingsWidget::setSettingsChanged); |
127 | } |
128 | for (QGroupBox* p: findChildren<QGroupBox*>()) { |
129 | connect(p, &QGroupBox::toggled,this, &SettingsWidget::setSettingsChanged); |
130 | } |
131 | |
132 | } |
133 | |
134 | void SettingsWidget::disconnectInputs() |
135 | { |
136 | for (QLineEdit* p:findChildren<QLineEdit*>()) { |
137 | disconnect(p, &QLineEdit::textChanged, this, &SettingsWidget::setSettingsChanged); |
138 | } |
139 | for (QCheckBox* p:findChildren<QCheckBox*>()) { |
140 | disconnect(p, &QCheckBox::stateChanged, this, &SettingsWidget::setSettingsChanged); |
141 | } |
142 | for (QRadioButton* p:findChildren<QRadioButton*>()) { |
143 | disconnect(p, &QRadioButton::toggled, this, &SettingsWidget::setSettingsChanged); |
144 | } |
145 | for (QPlainTextEdit* p:findChildren<QPlainTextEdit*>()) { |
146 | disconnect(p, &QPlainTextEdit::textChanged, this, &SettingsWidget::setSettingsChanged); |
147 | } |
148 | for (QSpinBox* p:findChildren<QSpinBox*>()) { |
149 | disconnect(p, QOverload<int>::of(&QSpinBox::valueChanged), this, &SettingsWidget::setSettingsChanged); |
150 | } |
151 | for (ColorEdit* p:findChildren<ColorEdit*>()) { |
152 | disconnect(p, &ColorEdit::colorChanged, this, &SettingsWidget::setSettingsChanged); |
153 | } |
154 | |
155 | for (QComboBox* p: findChildren<QComboBox*>()) { |
156 | disconnect(p, QOverload<int>::of(&QComboBox::currentIndexChanged) ,this, &SettingsWidget::setSettingsChanged); |
157 | } |
158 | for (QAbstractItemView* p: findChildren<QAbstractItemView*>()) { |
159 | disconnectAbstractItemView(p); |
160 | } |
161 | for (QListWidget* p:findChildren<QListWidget*>()) { |
162 | disconnect(p, QOverload<int>::of(&QListWidget::currentRowChanged) ,this, &SettingsWidget::setSettingsChanged); |
163 | } |
164 | for (QGroupBox* p: findChildren<QGroupBox*>()) { |
165 | disconnect(p, &QGroupBox::toggled,this, &SettingsWidget::setSettingsChanged); |
166 | } |
167 | |
168 | } |
169 | |
170 | const QString &SettingsWidget::group() |
171 | { |
172 | return mGroup; |
173 | } |
174 | |
175 | const QString &SettingsWidget::name() |
176 | { |
177 | return mName; |
178 | } |
179 | |
180 | bool SettingsWidget::isSettingsChanged() |
181 | { |
182 | return mSettingsChanged; |
183 | } |
184 | |
185 | void SettingsWidget::setSettingsChanged() |
186 | { |
187 | mSettingsChanged = true; |
188 | emit settingsChanged(true); |
189 | } |
190 | |
191 | void SettingsWidget::clearSettingsChanged() |
192 | { |
193 | mSettingsChanged = false; |
194 | emit settingsChanged(false); |
195 | } |
196 | |
197 | void SettingsWidget::onUpdateIcons() |
198 | { |
199 | updateIcons(pIconsManager->actionIconSize()); |
200 | } |
201 | |