Changeset View
Changeset View
Standalone View
Standalone View
kcms/colors/editor/scmeditordialog.cpp
Show All 25 Lines | |||||
26 | #include <QDir> | 26 | #include <QDir> | ||
27 | #include <QInputDialog> | 27 | #include <QInputDialog> | ||
28 | #include <QLineEdit> | 28 | #include <QLineEdit> | ||
29 | #include <QUrl> | 29 | #include <QUrl> | ||
30 | 30 | | |||
31 | #include <KConfigGroup> | 31 | #include <KConfigGroup> | ||
32 | #include <KColorScheme> | 32 | #include <KColorScheme> | ||
33 | #include <KMessageBox> | 33 | #include <KMessageBox> | ||
34 | #include <KStandardGuiItem> | ||||
34 | #include <KWindowSystem> | 35 | #include <KWindowSystem> | ||
35 | 36 | | |||
36 | #include <KNS3/UploadDialog> | 37 | #include <KNS3/UploadDialog> | ||
37 | 38 | | |||
38 | SchemeEditorDialog::SchemeEditorDialog(KSharedConfigPtr config, QWidget *parent) | 39 | SchemeEditorDialog::SchemeEditorDialog(KSharedConfigPtr config, QWidget *parent) | ||
39 | : QDialog(parent) | 40 | : QDialog(parent) | ||
40 | { | 41 | { | ||
41 | m_config = config; | 42 | m_config = config; | ||
42 | init(); | 43 | init(); | ||
43 | } | 44 | } | ||
44 | 45 | | |||
45 | SchemeEditorDialog::SchemeEditorDialog(const QString &path, QWidget *parent) | 46 | SchemeEditorDialog::SchemeEditorDialog(const QString &path, QWidget *parent) | ||
46 | : QDialog(parent) | 47 | : QDialog(parent) | ||
47 | , m_filePath(path) | 48 | , m_filePath(path) | ||
48 | { | 49 | { | ||
49 | m_config = KSharedConfig::openConfig(path); | 50 | m_config = KSharedConfig::openConfig(path); | ||
50 | 51 | | |||
51 | m_schemeName = KConfigGroup(m_config, "General").readEntry("Name"); | 52 | m_schemeName = KConfigGroup(m_config, "General").readEntry("Name"); | ||
52 | this->setWindowTitle(m_schemeName); | 53 | this->setWindowTitle(m_schemeName); | ||
53 | init(); | 54 | init(); | ||
54 | } | 55 | } | ||
55 | 56 | | |||
56 | bool SchemeEditorDialog::overwriteOnSave() const | 57 | bool SchemeEditorDialog::showApplyOverwriteButton() const | ||
57 | { | 58 | { | ||
58 | return m_overwriteOnSave; | 59 | return m_showApplyOverwriteButton; | ||
59 | } | 60 | } | ||
60 | 61 | | |||
61 | void SchemeEditorDialog::setOverwriteOnSave(bool overwrite) | 62 | void SchemeEditorDialog::setShowApplyOverwriteButton(bool show) | ||
62 | { | 63 | { | ||
63 | m_overwriteOnSave = overwrite; | 64 | m_showApplyOverwriteButton = show; | ||
64 | 65 | | |||
65 | buttonBox->button(QDialogButtonBox::Apply)->setVisible(overwrite); | 66 | buttonBox->button(QDialogButtonBox::Apply)->setVisible(show); | ||
66 | buttonBox->button(QDialogButtonBox::Save)->setVisible(!overwrite); | | |||
67 | } | 67 | } | ||
68 | 68 | | |||
69 | void SchemeEditorDialog::init() | 69 | void SchemeEditorDialog::init() | ||
70 | { | 70 | { | ||
71 | setupUi(this); | 71 | setupUi(this); | ||
72 | 72 | | |||
73 | schemeKnsUploadButton->setIcon( QIcon::fromTheme(QStringLiteral("get-hot-new-stuff")) ); | 73 | schemeKnsUploadButton->setIcon( QIcon::fromTheme(QStringLiteral("get-hot-new-stuff")) ); | ||
74 | 74 | | |||
75 | m_optionTab = new SchemeEditorOptions(m_config); | 75 | m_optionTab = new SchemeEditorOptions(m_config); | ||
76 | m_colorTab = new SchemeEditorColors(m_config); | 76 | m_colorTab = new SchemeEditorColors(m_config); | ||
77 | m_disabledTab = new SchemeEditorEffects(m_config, QPalette::Disabled); | 77 | m_disabledTab = new SchemeEditorEffects(m_config, QPalette::Disabled); | ||
78 | m_inactiveTab = new SchemeEditorEffects(m_config, QPalette::Inactive); | 78 | m_inactiveTab = new SchemeEditorEffects(m_config, QPalette::Inactive); | ||
79 | tabWidget->addTab(m_optionTab, i18n("Options")); | 79 | tabWidget->addTab(m_optionTab, i18n("Options")); | ||
80 | tabWidget->addTab(m_colorTab, i18n("Colors")); | 80 | tabWidget->addTab(m_colorTab, i18n("Colors")); | ||
81 | tabWidget->addTab(m_disabledTab, i18n("Disabled")); | 81 | tabWidget->addTab(m_disabledTab, i18n("Disabled")); | ||
82 | tabWidget->setCurrentWidget(m_colorTab); | 82 | tabWidget->setCurrentWidget(m_colorTab); | ||
83 | 83 | | |||
84 | connect(m_optionTab, &SchemeEditorOptions::changed, this, &SchemeEditorDialog::updateTabs); | 84 | connect(m_optionTab, &SchemeEditorOptions::changed, this, &SchemeEditorDialog::updateTabs); | ||
85 | connect(m_colorTab, &SchemeEditorColors::changed, this, &SchemeEditorDialog::updateTabs); | 85 | connect(m_colorTab, &SchemeEditorColors::changed, this, &SchemeEditorDialog::updateTabs); | ||
86 | connect(m_disabledTab, &SchemeEditorEffects::changed, this, &SchemeEditorDialog::updateTabs); | 86 | connect(m_disabledTab, &SchemeEditorEffects::changed, this, &SchemeEditorDialog::updateTabs); | ||
87 | connect(m_inactiveTab, &SchemeEditorEffects::changed, this, &SchemeEditorDialog::updateTabs); | 87 | connect(m_inactiveTab, &SchemeEditorEffects::changed, this, &SchemeEditorDialog::updateTabs); | ||
88 | 88 | | |||
89 | // In overwrite mode we use "Apply", in regular mode "Save" button | 89 | // "Apply" is only shown in overwrite mode | ||
90 | buttonBox->button(QDialogButtonBox::Apply)->setVisible(false); | 90 | buttonBox->button(QDialogButtonBox::Apply)->setVisible(false); | ||
91 | buttonBox->button(QDialogButtonBox::Apply)->setEnabled(false); | 91 | buttonBox->button(QDialogButtonBox::Apply)->setEnabled(false); | ||
92 | 92 | | |||
93 | buttonBox->button(QDialogButtonBox::Save)->setEnabled(false); | 93 | KGuiItem::assign(buttonBox->button(QDialogButtonBox::Save), KStandardGuiItem::saveAs()); | ||
94 | buttonBox->button(QDialogButtonBox::Reset)->setEnabled(false); | 94 | buttonBox->button(QDialogButtonBox::Reset)->setEnabled(false); | ||
95 | updateTabs(); | 95 | updateTabs(); | ||
96 | } | 96 | } | ||
97 | 97 | | |||
98 | void SchemeEditorDialog::on_schemeKnsUploadButton_clicked() | 98 | void SchemeEditorDialog::on_schemeKnsUploadButton_clicked() | ||
99 | { | 99 | { | ||
100 | if (m_unsavedChanges) | 100 | if (m_unsavedChanges) | ||
101 | { | 101 | { | ||
Show All 24 Lines | |||||
126 | { | 126 | { | ||
127 | if (buttonBox->standardButton(button) == QDialogButtonBox::Reset) | 127 | if (buttonBox->standardButton(button) == QDialogButtonBox::Reset) | ||
128 | { | 128 | { | ||
129 | m_config->markAsClean(); | 129 | m_config->markAsClean(); | ||
130 | m_config->reparseConfiguration(); | 130 | m_config->reparseConfiguration(); | ||
131 | updateTabs(); | 131 | updateTabs(); | ||
132 | setUnsavedChanges(false); | 132 | setUnsavedChanges(false); | ||
133 | } | 133 | } | ||
134 | else if (buttonBox->standardButton(button) == QDialogButtonBox::Save | 134 | else if (buttonBox->standardButton(button) == QDialogButtonBox::Save) | ||
135 | || buttonBox->standardButton(button) == QDialogButtonBox::Apply) | | |||
136 | { | 135 | { | ||
137 | saveScheme(); | 136 | saveScheme(false /*overwrite*/); | ||
137 | } | ||||
138 | else if (buttonBox->standardButton(button) == QDialogButtonBox::Apply) | ||||
139 | { | ||||
140 | saveScheme(true /*overwrite*/); | ||||
138 | } | 141 | } | ||
139 | else if (buttonBox->standardButton(button) == QDialogButtonBox::Close) | 142 | else if (buttonBox->standardButton(button) == QDialogButtonBox::Close) | ||
140 | { | 143 | { | ||
141 | if (m_unsavedChanges) { | 144 | if (m_unsavedChanges) { | ||
142 | KMessageBox::ButtonCode ans = KMessageBox::questionYesNo( | 145 | KMessageBox::ButtonCode ans = KMessageBox::questionYesNo( | ||
143 | this, i18n("You have unsaved changes. Do you really want to quit?"), | 146 | this, i18n("You have unsaved changes. Do you really want to quit?"), | ||
144 | i18n("Unsaved changes")); | 147 | i18n("Unsaved changes")); | ||
145 | if (ans == KMessageBox::No) | 148 | if (ans == KMessageBox::No) | ||
146 | { | 149 | { | ||
147 | return; | 150 | return; | ||
148 | } | 151 | } | ||
149 | } | 152 | } | ||
150 | m_config->markAsClean(); | 153 | m_config->markAsClean(); | ||
151 | m_config->reparseConfiguration(); | 154 | m_config->reparseConfiguration(); | ||
152 | this->accept(); | 155 | this->accept(); | ||
153 | } | 156 | } | ||
154 | } | 157 | } | ||
155 | 158 | | |||
156 | void SchemeEditorDialog::saveScheme() | 159 | void SchemeEditorDialog::saveScheme(bool overwrite) | ||
157 | { | 160 | { | ||
158 | QString name = m_schemeName; | 161 | QString name = m_schemeName; | ||
159 | 162 | | |||
160 | // prompt for the name to save as | 163 | // prompt for the name to save as | ||
161 | if (!m_overwriteOnSave) { | 164 | if (!overwrite) { | ||
162 | bool ok; | 165 | bool ok; | ||
163 | name = QInputDialog::getText(this, i18n("Save Color Scheme"), | 166 | name = QInputDialog::getText(this, i18n("Save Color Scheme"), | ||
164 | i18n("&Enter a name for the color scheme:"), QLineEdit::Normal, m_schemeName, &ok); | 167 | i18n("&Enter a name for the color scheme:"), QLineEdit::Normal, m_schemeName, &ok); | ||
165 | if (!ok) { | 168 | if (!ok) { | ||
166 | return; | 169 | return; | ||
167 | } | 170 | } | ||
168 | } | 171 | } | ||
169 | 172 | | |||
Show All 10 Lines | 182 | const QString path = QStandardPaths::locate(QStandardPaths::GenericDataLocation, | |||
180 | QStringLiteral("color-schemes/") + filename + QStringLiteral(".colors")); | 183 | QStringLiteral("color-schemes/") + filename + QStringLiteral(".colors")); | ||
181 | 184 | | |||
182 | QFile file(path); | 185 | QFile file(path); | ||
183 | const int permissions = file.permissions(); | 186 | const int permissions = file.permissions(); | ||
184 | const bool canWrite = (permissions & QFile::WriteUser); | 187 | const bool canWrite = (permissions & QFile::WriteUser); | ||
185 | // or if we can overwrite it if it exists | 188 | // or if we can overwrite it if it exists | ||
186 | if (path.isEmpty() || !file.exists() || canWrite) | 189 | if (path.isEmpty() || !file.exists() || canWrite) | ||
187 | { | 190 | { | ||
188 | if(canWrite && !m_overwriteOnSave){ | 191 | if(canWrite && !overwrite){ | ||
189 | int ret = KMessageBox::questionYesNo(this, | 192 | int ret = KMessageBox::questionYesNo(this, | ||
190 | i18n("A color scheme with that name already exists.\nDo you want to overwrite it?"), | 193 | i18n("A color scheme with that name already exists.\nDo you want to overwrite it?"), | ||
191 | i18n("Save Color Scheme"), | 194 | i18n("Save Color Scheme"), | ||
192 | KStandardGuiItem::overwrite(), | 195 | KStandardGuiItem::overwrite(), | ||
193 | KStandardGuiItem::cancel()); | 196 | KStandardGuiItem::cancel()); | ||
194 | 197 | | |||
195 | //on don't overwrite, call again the function | 198 | //on don't overwrite, call again the function | ||
196 | if(ret == KMessageBox::No){ | 199 | if(ret == KMessageBox::No){ | ||
197 | this->saveScheme(); | 200 | this->saveScheme(overwrite); | ||
198 | return; | 201 | return; | ||
199 | } | 202 | } | ||
200 | } | 203 | } | ||
201 | 204 | | |||
202 | // go ahead and save it | 205 | // go ahead and save it | ||
203 | QString newpath = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) | 206 | QString newpath = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) | ||
204 | + "/color-schemes/"; | 207 | + "/color-schemes/"; | ||
205 | QDir dir; | 208 | QDir dir; | ||
▲ Show 20 Lines • Show All 48 Lines • ▼ Show 20 Line(s) | 238 | { | |||
254 | m_disabledTab->updateValues(); | 257 | m_disabledTab->updateValues(); | ||
255 | } | 258 | } | ||
256 | 259 | | |||
257 | void SchemeEditorDialog::setUnsavedChanges(bool changes) | 260 | void SchemeEditorDialog::setUnsavedChanges(bool changes) | ||
258 | { | 261 | { | ||
259 | m_unsavedChanges = changes; | 262 | m_unsavedChanges = changes; | ||
260 | if (changes) | 263 | if (changes) | ||
261 | { | 264 | { | ||
262 | buttonBox->button(QDialogButtonBox::Save)->setEnabled(true); | | |||
263 | buttonBox->button(QDialogButtonBox::Apply)->setEnabled(true); | 265 | buttonBox->button(QDialogButtonBox::Apply)->setEnabled(true); | ||
264 | buttonBox->button(QDialogButtonBox::Reset)->setEnabled(true); | 266 | buttonBox->button(QDialogButtonBox::Reset)->setEnabled(true); | ||
265 | } | 267 | } | ||
266 | else | 268 | else | ||
267 | { | 269 | { | ||
268 | buttonBox->button(QDialogButtonBox::Save)->setEnabled(false); | | |||
269 | buttonBox->button(QDialogButtonBox::Apply)->setEnabled(false); | 270 | buttonBox->button(QDialogButtonBox::Apply)->setEnabled(false); | ||
270 | buttonBox->button(QDialogButtonBox::Reset)->setEnabled(false); | 271 | buttonBox->button(QDialogButtonBox::Reset)->setEnabled(false); | ||
271 | } | 272 | } | ||
272 | } | 273 | } |