Krita Source Code Documentation
Loading...
Searching...
No Matches
KisDialogStateSaver.cpp
Go to the documentation of this file.
1 /*
2 * SPDX-FileCopyrightText: 2019 Boudewijn Rempt <boud@kde.org>
3 *
4 * SPDX-License-Identifier: GPL-2.0-or-later
5 */
7
8#include <ksharedconfig.h>
9#include <kconfiggroup.h>
10
11#include <QDebug>
12#include <QCheckBox>
13#include <QComboBox>
14#include <QLineEdit>
15#include <QTextEdit>
16#include <QAbstractSlider>
17#include <QSpinBox>
18#include <QDoubleSpinBox>
19#include <QGroupBox>
20#include <QRadioButton>
21
25#include "kis_slider_spin_box.h"
26
27
28void KisDialogStateSaver::saveState(QWidget *parent, const QString &dialogName)
29{
30 Q_ASSERT(parent);
31 Q_ASSERT(!dialogName.isEmpty());
32
33 KConfigGroup group(KSharedConfig::openConfig(), dialogName);
34 Q_FOREACH(QWidget *widget, parent->findChildren<QWidget*>(QString())) {
35
36 if (!widget->objectName().isEmpty() ) {
37 if (qobject_cast<KisIntParseSpinBox*>(widget)) {
38 group.writeEntry(widget->objectName(), qobject_cast<KisIntParseSpinBox*>(widget)->value());
39 }
40 else if (qobject_cast<KisDoubleParseSpinBox*>(widget)) {
41 group.writeEntry(widget->objectName(), qobject_cast<KisDoubleParseSpinBox*>(widget)->value());
42 }
43 else if (qobject_cast<KisDoubleParseUnitSpinBox*>(widget)) {
44 // XXX: also save the unit
45 group.writeEntry(widget->objectName(), qobject_cast<KisDoubleParseUnitSpinBox*>(widget)->value());
46 }
47 else if (qobject_cast<QCheckBox*>(widget)) {
48 group.writeEntry(widget->objectName(), qobject_cast<const QCheckBox*>(widget)->isChecked());
49 }
50 else if (qobject_cast<QComboBox*>(widget)) {
51 group.writeEntry(widget->objectName(), qobject_cast<QComboBox*>(widget)->currentIndex());
52 }
53 else if (qobject_cast<QLineEdit*>(widget)) {
54 group.writeEntry(widget->objectName(), qobject_cast<QLineEdit*>(widget)->text());
55 }
56 else if (qobject_cast<QTextEdit*>(widget)) {
57 group.writeEntry(widget->objectName(), qobject_cast<QTextEdit *>(widget)->toPlainText());
58 }
59 else if (qobject_cast<QAbstractSlider*>(widget)) {
60 group.writeEntry(widget->objectName(), qobject_cast<QAbstractSlider*>(widget)->value());
61 }
62 else if (qobject_cast<QSpinBox*>(widget)) {
63 group.writeEntry(widget->objectName(), qobject_cast<QSpinBox*>(widget)->value());
64 }
65 else if (qobject_cast<QDoubleSpinBox*>(widget)) {
66 group.writeEntry(widget->objectName(), qobject_cast<QDoubleSpinBox*>(widget)->value());
67 }
68 else if (qobject_cast<QRadioButton*>(widget)) {
69 group.writeEntry(widget->objectName(), qobject_cast<QRadioButton*>(widget)->isChecked());
70 }
71 else if (qobject_cast<KisSliderSpinBox*>(widget)){
72 group.writeEntry(widget->objectName(), qobject_cast<KisSliderSpinBox*>(widget)->value());
73 }
74
75 else {
76 //qWarning() << "Cannot save state for object" << widget;
77 }
78 }
79 else {
80 qWarning() << "Dialog" << dialogName << "has a widget without an objectName:" << widget;
81 }
82
83 }
84}
85
86void KisDialogStateSaver::restoreState(QWidget *parent, const QString &dialogName, const QMap<QString, QVariant> &defaults)
87{
88 Q_ASSERT(parent);
89 Q_ASSERT(!dialogName.isEmpty());
90
91 KConfigGroup group( KSharedConfig::openConfig(), dialogName);
92
93 Q_FOREACH(QWidget *widget, parent->findChildren<QWidget*>(QString())) {
94
95 if (!widget->objectName().isEmpty()) {
96
97 QString widgetName = widget->objectName();
98
99 QVariant defaultValue;
100 if (defaults.contains(widgetName)) {
101 defaultValue = defaults[widgetName];
102 }
103
104 if (qobject_cast<KisIntParseSpinBox*>(widget)) {
105 if (defaultValue.isValid()) {
106 qobject_cast<KisIntParseSpinBox*>(widget)->setValue(defaultValue.toInt());
107 }
108 else {
109 qobject_cast<KisIntParseSpinBox*>(widget)->setValue(group.readEntry<int>(widgetName, qobject_cast<KisIntParseSpinBox*>(widget)->value()));
110 }
111 }
112 else if (qobject_cast<KisDoubleParseSpinBox*>(widget)) {
113 if (defaultValue.isValid()) {
114 qobject_cast<KisDoubleParseSpinBox*>(widget)->setValue(defaultValue.toInt());
115 }
116 else {
117 qobject_cast<KisDoubleParseSpinBox*>(widget)->setValue(group.readEntry<int>(widgetName, qobject_cast<KisDoubleParseSpinBox*>(widget)->value()));
118 }
119 }
120 else if (qobject_cast<KisDoubleParseUnitSpinBox*>(widget)) {
121 if (defaultValue.isValid()) {
122 qobject_cast<KisDoubleParseUnitSpinBox*>(widget)->setValue(defaultValue.toInt());
123 }
124 else {
125 qobject_cast<KisDoubleParseUnitSpinBox*>(widget)->setValue(group.readEntry<int>(widgetName, qobject_cast<KisDoubleParseUnitSpinBox*>(widget)->value()));
126 }
127 }
128 else if (qobject_cast<QCheckBox*>(widget)) {
129 if (defaultValue.isValid()) {
130 qobject_cast<QCheckBox*>(widget)->setChecked(defaultValue.toBool());
131 }
132 else {
133 qobject_cast<QCheckBox*>(widget)->setChecked(group.readEntry<bool>(widgetName, qobject_cast<QCheckBox*>(widget)->isChecked()));
134 }
135 }
136 else if (qobject_cast<QComboBox*>(widget)) {
137 if (defaultValue.isValid()) {
138 qobject_cast<QComboBox*>(widget)->setCurrentIndex(defaultValue.toInt());
139 }
140 else {
141 qobject_cast<QComboBox*>(widget)->setCurrentIndex(group.readEntry<int>(widgetName, qobject_cast<QComboBox*>(widget)->currentIndex()));
142 }
143 }
144 else if (qobject_cast<QLineEdit*>(widget)) {
145 if (defaultValue.isValid()) {
146 qobject_cast<QLineEdit*>(widget)->setText(defaultValue.toString());
147 }
148 else {
149 qobject_cast<QLineEdit*>(widget)->setText(group.readEntry<QString>(widgetName, qobject_cast<QLineEdit*>(widget)->text()));
150 }
151 }
152 else if (qobject_cast<QTextEdit *>(widget)) {
153 if (defaultValue.isValid()) {
154 qobject_cast<QTextEdit *>(widget)->setPlainText(defaultValue.toString());
155 } else {
156 qobject_cast<QTextEdit *>(widget)->setPlainText(group.readEntry<QString>(widgetName, qobject_cast<QTextEdit *>(widget)->toPlainText()));
157 }
158 }
159 else if (qobject_cast<QAbstractSlider*>(widget)) {
160 if (defaultValue.isValid()) {
161 qobject_cast<QAbstractSlider*>(widget)->setValue(defaultValue.toInt());
162 }
163 else {
164 qobject_cast<QAbstractSlider*>(widget)->setValue(group.readEntry<int>(widgetName, qobject_cast<QAbstractSlider*>(widget)->value()));
165 }
166 }
167 else if (qobject_cast<QSpinBox*>(widget)) {
168 if (defaultValue.isValid()) {
169 qobject_cast<QSpinBox*>(widget)->setValue(defaultValue.toInt());
170 }
171 else {
172 qobject_cast<QSpinBox*>(widget)->setValue(group.readEntry<int>(widgetName, qobject_cast<QSpinBox*>(widget)->value()));
173 }
174 }
175 else if (qobject_cast<QDoubleSpinBox*>(widget)) {
176 if (defaultValue.isValid()) {
177 qobject_cast<QDoubleSpinBox*>(widget)->setValue(defaultValue.toDouble());
178 }
179 else {
180 qobject_cast<QDoubleSpinBox*>(widget)->setValue(group.readEntry<int>(widgetName, qobject_cast<QDoubleSpinBox*>(widget)->value()));
181 }
182 }
183 else if (qobject_cast<QRadioButton*>(widget)) {
184 if (defaultValue.isValid()) {
185 qobject_cast<QRadioButton*>(widget)->setChecked(defaultValue.toBool());
186 }
187 else {
188 qobject_cast<QRadioButton*>(widget)->setChecked(group.readEntry<bool>(widgetName, qobject_cast<QRadioButton*>(widget)->isChecked()));
189 }
190 }
191 else if (qobject_cast<KisSliderSpinBox*>(widget)) {
192 if (defaultValue.isValid()) {
193 qobject_cast<KisSliderSpinBox*>(widget)->setValue(defaultValue.toInt());
194 }
195 else {
196 qobject_cast<KisSliderSpinBox*>(widget)->setValue(group.readEntry<int>(widgetName, qobject_cast<KisSliderSpinBox*>(widget)->value()));
197 }
198 }
199
200 else {
201 //qWarning() << "Cannot restore state for object" << widget;
202 }
203 }
204 else {
205 qWarning() << "Dialog" << dialogName << "has a widget without an object name:" << widget;
206 }
207 }
208}
KRITAWIDGETUTILS_EXPORT void restoreState(QWidget *parent, const QString &dialogName, const QMap< QString, QVariant > &defaults=QMap< QString, QVariant >())
restoreState restores the state of the dialog
KRITAWIDGETUTILS_EXPORT void saveState(QWidget *parent, const QString &dialogName)
saveState saves the state for the specified widgets