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 15 Lines | |||||
117 | { | 117 | { | ||
118 | if (buttonBox->standardButton(button) == QDialogButtonBox::Reset) | 118 | if (buttonBox->standardButton(button) == QDialogButtonBox::Reset) | ||
119 | { | 119 | { | ||
120 | m_config->markAsClean(); | 120 | m_config->markAsClean(); | ||
121 | m_config->reparseConfiguration(); | 121 | m_config->reparseConfiguration(); | ||
122 | updateTabs(); | 122 | updateTabs(); | ||
123 | setUnsavedChanges(false); | 123 | setUnsavedChanges(false); | ||
124 | } | 124 | } | ||
125 | else if (buttonBox->standardButton(button) == QDialogButtonBox::Save | 125 | else if (buttonBox->standardButton(button) == QDialogButtonBox::Save) | ||
126 | || buttonBox->standardButton(button) == QDialogButtonBox::Apply) | | |||
127 | { | 126 | { | ||
128 | saveScheme(); | 127 | saveScheme(false /*overwrite*/); | ||
128 | } | ||||
129 | else if (buttonBox->standardButton(button) == QDialogButtonBox::Apply) | ||||
130 | { | ||||
131 | saveScheme(true /*overwrite*/); | ||||
129 | } | 132 | } | ||
130 | else if (buttonBox->standardButton(button) == QDialogButtonBox::Close) | 133 | else if (buttonBox->standardButton(button) == QDialogButtonBox::Close) | ||
131 | { | 134 | { | ||
132 | if (m_unsavedChanges) { | 135 | if (m_unsavedChanges) { | ||
133 | KMessageBox::ButtonCode ans = KMessageBox::questionYesNo( | 136 | KMessageBox::ButtonCode ans = KMessageBox::questionYesNo( | ||
134 | this, i18n("You have unsaved changes. Do you really want to quit?"), | 137 | this, i18n("You have unsaved changes. Do you really want to quit?"), | ||
135 | i18n("Unsaved changes")); | 138 | i18n("Unsaved changes")); | ||
136 | if (ans == KMessageBox::No) | 139 | if (ans == KMessageBox::No) | ||
137 | { | 140 | { | ||
138 | return; | 141 | return; | ||
139 | } | 142 | } | ||
140 | } | 143 | } | ||
141 | m_config->markAsClean(); | 144 | m_config->markAsClean(); | ||
142 | m_config->reparseConfiguration(); | 145 | m_config->reparseConfiguration(); | ||
143 | this->accept(); | 146 | this->accept(); | ||
144 | } | 147 | } | ||
145 | } | 148 | } | ||
146 | 149 | | |||
147 | void SchemeEditorDialog::saveScheme() | 150 | void SchemeEditorDialog::saveScheme(bool overwrite) | ||
148 | { | 151 | { | ||
149 | QString name = m_schemeName; | 152 | QString name = m_schemeName; | ||
150 | 153 | | |||
151 | // prompt for the name to save as | 154 | // prompt for the name to save as | ||
152 | if (!m_overwriteOnSave) { | 155 | if (!overwrite) { | ||
153 | bool ok; | 156 | bool ok; | ||
154 | name = QInputDialog::getText(this, i18n("Save Color Scheme"), | 157 | name = QInputDialog::getText(this, i18n("Save Color Scheme"), | ||
155 | i18n("&Enter a name for the color scheme:"), QLineEdit::Normal, m_schemeName, &ok); | 158 | i18n("&Enter a name for the color scheme:"), QLineEdit::Normal, m_schemeName, &ok); | ||
156 | if (!ok) { | 159 | if (!ok) { | ||
157 | return; | 160 | return; | ||
158 | } | 161 | } | ||
159 | } | 162 | } | ||
160 | 163 | | |||
Show All 10 Lines | 173 | const QString path = QStandardPaths::locate(QStandardPaths::GenericDataLocation, | |||
171 | QStringLiteral("color-schemes/") + filename + QStringLiteral(".colors")); | 174 | QStringLiteral("color-schemes/") + filename + QStringLiteral(".colors")); | ||
172 | 175 | | |||
173 | QFile file(path); | 176 | QFile file(path); | ||
174 | const int permissions = file.permissions(); | 177 | const int permissions = file.permissions(); | ||
175 | const bool canWrite = (permissions & QFile::WriteUser); | 178 | const bool canWrite = (permissions & QFile::WriteUser); | ||
176 | // or if we can overwrite it if it exists | 179 | // or if we can overwrite it if it exists | ||
177 | if (path.isEmpty() || !file.exists() || canWrite) | 180 | if (path.isEmpty() || !file.exists() || canWrite) | ||
178 | { | 181 | { | ||
179 | if(canWrite && !m_overwriteOnSave){ | 182 | if(canWrite && !overwrite){ | ||
180 | int ret = KMessageBox::questionYesNo(this, | 183 | int ret = KMessageBox::questionYesNo(this, | ||
181 | i18n("A color scheme with that name already exists.\nDo you want to overwrite it?"), | 184 | i18n("A color scheme with that name already exists.\nDo you want to overwrite it?"), | ||
182 | i18n("Save Color Scheme"), | 185 | i18n("Save Color Scheme"), | ||
183 | KStandardGuiItem::overwrite(), | 186 | KStandardGuiItem::overwrite(), | ||
184 | KStandardGuiItem::cancel()); | 187 | KStandardGuiItem::cancel()); | ||
185 | 188 | | |||
186 | //on don't overwrite, call again the function | 189 | //on don't overwrite, call again the function | ||
187 | if(ret == KMessageBox::No){ | 190 | if(ret == KMessageBox::No){ | ||
188 | this->saveScheme(); | 191 | this->saveScheme(overwrite); | ||
189 | return; | 192 | return; | ||
190 | } | 193 | } | ||
191 | } | 194 | } | ||
192 | 195 | | |||
193 | // go ahead and save it | 196 | // go ahead and save it | ||
194 | QString newpath = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) | 197 | QString newpath = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) | ||
195 | + "/color-schemes/"; | 198 | + "/color-schemes/"; | ||
196 | QDir dir; | 199 | QDir dir; | ||
▲ Show 20 Lines • Show All 48 Lines • ▼ Show 20 Line(s) | 229 | { | |||
245 | m_disabledTab->updateValues(); | 248 | m_disabledTab->updateValues(); | ||
246 | } | 249 | } | ||
247 | 250 | | |||
248 | void SchemeEditorDialog::setUnsavedChanges(bool changes) | 251 | void SchemeEditorDialog::setUnsavedChanges(bool changes) | ||
249 | { | 252 | { | ||
250 | m_unsavedChanges = changes; | 253 | m_unsavedChanges = changes; | ||
251 | if (changes) | 254 | if (changes) | ||
252 | { | 255 | { | ||
253 | buttonBox->button(QDialogButtonBox::Save)->setEnabled(true); | | |||
254 | buttonBox->button(QDialogButtonBox::Apply)->setEnabled(true); | 256 | buttonBox->button(QDialogButtonBox::Apply)->setEnabled(true); | ||
255 | buttonBox->button(QDialogButtonBox::Reset)->setEnabled(true); | 257 | buttonBox->button(QDialogButtonBox::Reset)->setEnabled(true); | ||
256 | } | 258 | } | ||
257 | else | 259 | else | ||
258 | { | 260 | { | ||
259 | buttonBox->button(QDialogButtonBox::Save)->setEnabled(false); | | |||
260 | buttonBox->button(QDialogButtonBox::Apply)->setEnabled(false); | 261 | buttonBox->button(QDialogButtonBox::Apply)->setEnabled(false); | ||
261 | buttonBox->button(QDialogButtonBox::Reset)->setEnabled(false); | 262 | buttonBox->button(QDialogButtonBox::Reset)->setEnabled(false); | ||
262 | } | 263 | } | ||
263 | } | 264 | } |