Changeset View
Changeset View
Standalone View
Standalone View
kcms/colors/editor/scmeditordialog.cpp
- This file was moved from kcms/colors/scmeditordialog.cpp.
Show All 15 Lines | |||||
16 | * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | 16 | * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | ||
17 | * Boston, MA 02110-1301, USA. | 17 | * Boston, MA 02110-1301, USA. | ||
18 | */ | 18 | */ | ||
19 | 19 | | |||
20 | #include "scmeditordialog.h" | 20 | #include "scmeditordialog.h" | ||
21 | #include "scmeditoroptions.h" | 21 | #include "scmeditoroptions.h" | ||
22 | #include "scmeditorcolors.h" | 22 | #include "scmeditorcolors.h" | ||
23 | #include "scmeditoreffects.h" | 23 | #include "scmeditoreffects.h" | ||
24 | #include "colorscm.h" | | |||
25 | 24 | | |||
26 | #include <QDebug> | 25 | #include <QDebug> | ||
27 | #include <QDir> | 26 | #include <QDir> | ||
28 | #include <QInputDialog> | 27 | #include <QInputDialog> | ||
29 | #include <QLineEdit> | 28 | #include <QLineEdit> | ||
30 | #include <QUrl> | 29 | #include <QUrl> | ||
31 | 30 | | |||
32 | #include <KConfigGroup> | 31 | #include <KConfigGroup> | ||
33 | #include <KColorScheme> | 32 | #include <KColorScheme> | ||
34 | #include <KMessageBox> | 33 | #include <KMessageBox> | ||
34 | #include <KWindowSystem> | ||||
35 | 35 | | |||
36 | #include <KNS3/UploadDialog> | 36 | #include <KNS3/UploadDialog> | ||
37 | 37 | | |||
38 | SchemeEditorDialog::SchemeEditorDialog(KSharedConfigPtr config, KColorCm *parent) | 38 | SchemeEditorDialog::SchemeEditorDialog(KSharedConfigPtr config, QWidget *parent) | ||
39 | : QDialog( parent ) | 39 | : QDialog(parent) | ||
40 | , m_disableUpdates(false) | | |||
41 | , m_unsavedChanges(false) | | |||
42 | , m_kcm(parent) | | |||
43 | { | 40 | { | ||
44 | m_config = config; | 41 | m_config = config; | ||
45 | init(); | 42 | init(); | ||
46 | } | 43 | } | ||
47 | 44 | | |||
48 | SchemeEditorDialog::SchemeEditorDialog(const QString &path, KColorCm *parent) | 45 | SchemeEditorDialog::SchemeEditorDialog(const QString &path, QWidget *parent) | ||
49 | : QDialog( parent ) | 46 | : QDialog(parent) | ||
50 | , m_filePath(path) | 47 | , m_filePath(path) | ||
51 | , m_disableUpdates(false) | | |||
52 | , m_unsavedChanges(false) | | |||
53 | , m_kcm(parent) | | |||
54 | { | 48 | { | ||
55 | m_config = KSharedConfig::openConfig(path); | 49 | m_config = KSharedConfig::openConfig(path); | ||
56 | 50 | | |||
57 | m_schemeName = KConfigGroup(m_config, "General").readEntry("Name"); | 51 | m_schemeName = KConfigGroup(m_config, "General").readEntry("Name"); | ||
58 | this->setWindowTitle(m_schemeName); | 52 | this->setWindowTitle(m_schemeName); | ||
59 | init(); | 53 | init(); | ||
60 | } | 54 | } | ||
61 | 55 | | |||
62 | void SchemeEditorDialog::init() | 56 | bool SchemeEditorDialog::overwriteOnSave() const | ||
63 | { | 57 | { | ||
64 | setupUi(this); | 58 | return m_overwriteOnSave; | ||
59 | } | ||||
65 | 60 | | |||
66 | //we are in standalone appication mode? don't show the apply button | 61 | void SchemeEditorDialog::setOverwriteOnSave(bool overwrite) | ||
67 | if (!m_kcm) { | 62 | { | ||
68 | buttonBox->button(QDialogButtonBox::Apply)->setVisible(false); | 63 | m_overwriteOnSave = overwrite; | ||
64 | | ||||
65 | buttonBox->button(QDialogButtonBox::Apply)->setVisible(overwrite); | ||||
66 | buttonBox->button(QDialogButtonBox::Save)->setVisible(!overwrite); | ||||
69 | } | 67 | } | ||
70 | 68 | | |||
69 | void SchemeEditorDialog::init() | ||||
70 | { | ||||
71 | setupUi(this); | ||||
72 | | ||||
71 | schemeKnsUploadButton->setIcon( QIcon::fromTheme(QStringLiteral("get-hot-new-stuff")) ); | 73 | schemeKnsUploadButton->setIcon( QIcon::fromTheme(QStringLiteral("get-hot-new-stuff")) ); | ||
72 | 74 | | |||
73 | m_optionTab = new SchemeEditorOptions(m_config); | 75 | m_optionTab = new SchemeEditorOptions(m_config); | ||
74 | m_colorTab = new SchemeEditorColors(m_config); | 76 | m_colorTab = new SchemeEditorColors(m_config); | ||
75 | m_disabledTab = new SchemeEditorEffects(m_config, QPalette::Disabled); | 77 | m_disabledTab = new SchemeEditorEffects(m_config, QPalette::Disabled); | ||
76 | m_inactiveTab = new SchemeEditorEffects(m_config, QPalette::Inactive); | 78 | m_inactiveTab = new SchemeEditorEffects(m_config, QPalette::Inactive); | ||
77 | tabWidget->addTab(m_optionTab, i18n("Options")); | 79 | tabWidget->addTab(m_optionTab, i18n("Options")); | ||
78 | tabWidget->addTab(m_colorTab, i18n("Colors")); | 80 | tabWidget->addTab(m_colorTab, i18n("Colors")); | ||
79 | tabWidget->addTab(m_disabledTab, i18n("Disabled")); | 81 | tabWidget->addTab(m_disabledTab, i18n("Disabled")); | ||
80 | tabWidget->setCurrentWidget(m_colorTab); | 82 | tabWidget->setCurrentWidget(m_colorTab); | ||
81 | 83 | | |||
82 | connect(m_optionTab, &SchemeEditorOptions::changed, this, &SchemeEditorDialog::updateTabs); | 84 | connect(m_optionTab, &SchemeEditorOptions::changed, this, &SchemeEditorDialog::updateTabs); | ||
83 | connect(m_colorTab, &SchemeEditorColors::changed, this, &SchemeEditorDialog::updateTabs); | 85 | connect(m_colorTab, &SchemeEditorColors::changed, this, &SchemeEditorDialog::updateTabs); | ||
84 | connect(m_disabledTab, &SchemeEditorEffects::changed, this, &SchemeEditorDialog::updateTabs); | 86 | connect(m_disabledTab, &SchemeEditorEffects::changed, this, &SchemeEditorDialog::updateTabs); | ||
85 | connect(m_inactiveTab, &SchemeEditorEffects::changed, this, &SchemeEditorDialog::updateTabs); | 87 | connect(m_inactiveTab, &SchemeEditorEffects::changed, this, &SchemeEditorDialog::updateTabs); | ||
86 | 88 | | |||
89 | // In overwrite mode we use "Apply", in regular mode "Save" button | ||||
90 | buttonBox->button(QDialogButtonBox::Apply)->setVisible(false); | ||||
91 | buttonBox->button(QDialogButtonBox::Apply)->setEnabled(false); | ||||
92 | | ||||
87 | buttonBox->button(QDialogButtonBox::Save)->setEnabled(false); | 93 | buttonBox->button(QDialogButtonBox::Save)->setEnabled(false); | ||
88 | buttonBox->button(QDialogButtonBox::Reset)->setEnabled(false); | 94 | buttonBox->button(QDialogButtonBox::Reset)->setEnabled(false); | ||
89 | updateTabs(); | 95 | updateTabs(); | ||
90 | } | 96 | } | ||
91 | 97 | | |||
92 | void SchemeEditorDialog::on_schemeKnsUploadButton_clicked() | 98 | void SchemeEditorDialog::on_schemeKnsUploadButton_clicked() | ||
93 | { | 99 | { | ||
94 | if (m_unsavedChanges) | 100 | if (m_unsavedChanges) | ||
Show All 25 Lines | |||||
120 | { | 126 | { | ||
121 | if (buttonBox->standardButton(button) == QDialogButtonBox::Reset) | 127 | if (buttonBox->standardButton(button) == QDialogButtonBox::Reset) | ||
122 | { | 128 | { | ||
123 | m_config->markAsClean(); | 129 | m_config->markAsClean(); | ||
124 | m_config->reparseConfiguration(); | 130 | m_config->reparseConfiguration(); | ||
125 | updateTabs(); | 131 | updateTabs(); | ||
126 | setUnsavedChanges(false); | 132 | setUnsavedChanges(false); | ||
127 | } | 133 | } | ||
128 | else if (buttonBox->standardButton(button) == QDialogButtonBox::Save) | 134 | else if (buttonBox->standardButton(button) == QDialogButtonBox::Save | ||
135 | || buttonBox->standardButton(button) == QDialogButtonBox::Apply) | ||||
129 | { | 136 | { | ||
130 | saveScheme(); | 137 | saveScheme(); | ||
131 | } | 138 | } | ||
132 | else if (buttonBox->standardButton(button) == QDialogButtonBox::Apply) | | |||
133 | { | | |||
134 | applyScheme(); | | |||
135 | emit applied(); | | |||
136 | } | | |||
137 | else if (buttonBox->standardButton(button) == QDialogButtonBox::Close) | 139 | else if (buttonBox->standardButton(button) == QDialogButtonBox::Close) | ||
138 | { | 140 | { | ||
139 | if (m_unsavedChanges) { | 141 | if (m_unsavedChanges) { | ||
140 | KMessageBox::ButtonCode ans = KMessageBox::questionYesNo( | 142 | KMessageBox::ButtonCode ans = KMessageBox::questionYesNo( | ||
141 | this, i18n("You have unsaved changes. Do you really want to quit?"), | 143 | this, i18n("You have unsaved changes. Do you really want to quit?"), | ||
142 | i18n("Unsaved changes")); | 144 | i18n("Unsaved changes")); | ||
143 | if (ans == KMessageBox::No) | 145 | if (ans == KMessageBox::No) | ||
144 | { | 146 | { | ||
145 | return; | 147 | return; | ||
146 | } | 148 | } | ||
147 | } | 149 | } | ||
148 | m_config->markAsClean(); | 150 | m_config->markAsClean(); | ||
149 | m_config->reparseConfiguration(); | 151 | m_config->reparseConfiguration(); | ||
150 | this->accept(); | 152 | this->accept(); | ||
151 | } | 153 | } | ||
152 | } | 154 | } | ||
153 | 155 | | |||
154 | void SchemeEditorDialog::applyScheme() | | |||
155 | { | | |||
156 | if (!m_kcm) { | | |||
157 | return; | | |||
158 | } | | |||
159 | | ||||
160 | m_kcm->updateConfig(m_config); | | |||
161 | m_kcm->save(); | | |||
162 | } | | |||
163 | | ||||
164 | void SchemeEditorDialog::saveScheme() | 156 | void SchemeEditorDialog::saveScheme() | ||
165 | { | 157 | { | ||
158 | QString name = m_schemeName; | ||||
159 | | ||||
166 | // prompt for the name to save as | 160 | // prompt for the name to save as | ||
161 | if (!m_overwriteOnSave) { | ||||
167 | bool ok; | 162 | bool ok; | ||
168 | QString schemeName = KConfigGroup(m_config, "General").readEntry("Name"); | 163 | name = QInputDialog::getText(this, i18n("Save Color Scheme"), | ||
169 | QString name = QInputDialog::getText(this, i18n("Save Color Scheme"), | | |||
170 | i18n("&Enter a name for the color scheme:"), QLineEdit::Normal, m_schemeName, &ok); | 164 | i18n("&Enter a name for the color scheme:"), QLineEdit::Normal, m_schemeName, &ok); | ||
171 | if (!ok) { | 165 | if (!ok) { | ||
172 | return; | 166 | return; | ||
173 | } | 167 | } | ||
168 | } | ||||
174 | 169 | | |||
175 | QString filename = name; | 170 | QString filename = name; | ||
176 | filename.remove(QLatin1Char('\'')); // So Foo's does not become FooS | 171 | filename.remove(QLatin1Char('\'')); // So Foo's does not become FooS | ||
177 | QRegExp fixer(QStringLiteral("[\\W,.-]+(.?)")); | 172 | QRegExp fixer(QStringLiteral("[\\W,.-]+(.?)")); | ||
178 | int offset; | 173 | int offset; | ||
179 | while ((offset = fixer.indexIn(filename)) >= 0) | 174 | while ((offset = fixer.indexIn(filename)) >= 0) | ||
180 | filename.replace(offset, fixer.matchedLength(), fixer.cap(1).toUpper()); | 175 | filename.replace(offset, fixer.matchedLength(), fixer.cap(1).toUpper()); | ||
181 | filename.replace(0, 1, filename.at(0).toUpper()); | 176 | filename.replace(0, 1, filename.at(0).toUpper()); | ||
182 | 177 | | |||
183 | // check if that name is already in the list | 178 | // check if that name is already in the list | ||
184 | const QString path = QStandardPaths::locate(QStandardPaths::GenericDataLocation, | 179 | const QString path = QStandardPaths::locate(QStandardPaths::GenericDataLocation, | ||
185 | QStringLiteral("color-schemes/") + filename + QStringLiteral(".colors")); | 180 | QStringLiteral("color-schemes/") + filename + QStringLiteral(".colors")); | ||
186 | 181 | | |||
187 | QFile file(path); | 182 | QFile file(path); | ||
188 | const int permissions = file.permissions(); | 183 | const int permissions = file.permissions(); | ||
189 | const bool canWrite = (permissions & QFile::WriteUser); | 184 | const bool canWrite = (permissions & QFile::WriteUser); | ||
190 | // or if we can overwrite it if it exists | 185 | // or if we can overwrite it if it exists | ||
191 | if (path.isEmpty() || !file.exists() || canWrite) | 186 | if (path.isEmpty() || !file.exists() || canWrite) | ||
192 | { | 187 | { | ||
193 | if(canWrite){ | 188 | if(canWrite && !m_overwriteOnSave){ | ||
194 | int ret = KMessageBox::questionYesNo(this, | 189 | int ret = KMessageBox::questionYesNo(this, | ||
195 | i18n("A color scheme with that name already exists.\nDo you want to overwrite it?"), | 190 | i18n("A color scheme with that name already exists.\nDo you want to overwrite it?"), | ||
196 | i18n("Save Color Scheme"), | 191 | i18n("Save Color Scheme"), | ||
197 | KStandardGuiItem::overwrite(), | 192 | KStandardGuiItem::overwrite(), | ||
198 | KStandardGuiItem::cancel()); | 193 | KStandardGuiItem::cancel()); | ||
199 | 194 | | |||
200 | //on don't overwrite, call again the function | 195 | //on don't overwrite, call again the function | ||
201 | if(ret == KMessageBox::No){ | 196 | if(ret == KMessageBox::No){ | ||
Show All 19 Lines | |||||
221 | config->sync(); | 216 | config->sync(); | ||
222 | delete config; | 217 | delete config; | ||
223 | // reopen and update window | 218 | // reopen and update window | ||
224 | m_config = KSharedConfig::openConfig(newpath); | 219 | m_config = KSharedConfig::openConfig(newpath); | ||
225 | m_schemeName = name; | 220 | m_schemeName = name; | ||
226 | setWindowTitle(name); | 221 | setWindowTitle(name); | ||
227 | 222 | | |||
228 | setUnsavedChanges(false); | 223 | setUnsavedChanges(false); | ||
224 | | ||||
225 | QTextStream out(stdout); | ||||
226 | out << filename << endl; | ||||
229 | } | 227 | } | ||
230 | else if (!canWrite && file.exists()) | 228 | else if (!canWrite && file.exists()) | ||
231 | { | 229 | { | ||
232 | KMessageBox::error(this, i18n("You do not have permission to overwrite that scheme"), i18n("Error")); | 230 | KMessageBox::error(this, i18n("You do not have permission to overwrite that scheme"), i18n("Error")); | ||
233 | } | 231 | } | ||
234 | } | 232 | } | ||
235 | 233 | | |||
236 | void SchemeEditorDialog::updateTabs(bool madeByUser) | 234 | void SchemeEditorDialog::updateTabs(bool madeByUser) | ||
Show All 20 Lines | |||||
257 | } | 255 | } | ||
258 | 256 | | |||
259 | void SchemeEditorDialog::setUnsavedChanges(bool changes) | 257 | void SchemeEditorDialog::setUnsavedChanges(bool changes) | ||
260 | { | 258 | { | ||
261 | m_unsavedChanges = changes; | 259 | m_unsavedChanges = changes; | ||
262 | if (changes) | 260 | if (changes) | ||
263 | { | 261 | { | ||
264 | buttonBox->button(QDialogButtonBox::Save)->setEnabled(true); | 262 | buttonBox->button(QDialogButtonBox::Save)->setEnabled(true); | ||
263 | buttonBox->button(QDialogButtonBox::Apply)->setEnabled(true); | ||||
265 | buttonBox->button(QDialogButtonBox::Reset)->setEnabled(true); | 264 | buttonBox->button(QDialogButtonBox::Reset)->setEnabled(true); | ||
266 | } | 265 | } | ||
267 | else | 266 | else | ||
268 | { | 267 | { | ||
269 | buttonBox->button(QDialogButtonBox::Save)->setEnabled(false); | 268 | buttonBox->button(QDialogButtonBox::Save)->setEnabled(false); | ||
269 | buttonBox->button(QDialogButtonBox::Apply)->setEnabled(false); | ||||
270 | buttonBox->button(QDialogButtonBox::Reset)->setEnabled(false); | 270 | buttonBox->button(QDialogButtonBox::Reset)->setEnabled(false); | ||
271 | } | 271 | } | ||
272 | } | 272 | } |