compile
[kdegraphics.git] / kolourpaint / mainWindow / kpMainWindow_Colors.cpp
blobbec076a442bd1befd89251b02948be43ab1e2659
2 /*
3 Copyright (c) 2003-2007 Clarence Dang <dang@kde.org>
4 All rights reserved.
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions
8 are met:
10 1. Redistributions of source code must retain the above copyright
11 notice, this list of conditions and the following disclaimer.
12 2. Redistributions in binary form must reproduce the above copyright
13 notice, this list of conditions and the following disclaimer in the
14 documentation and/or other materials provided with the distribution.
16 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include <kpMainWindow.h>
30 #include <kpMainWindowPrivate.h>
32 #include <KAction>
33 #include <KActionCollection>
34 #include <KFileDialog>
35 #include <KMessageBox>
36 #include <KSelectAction>
37 #include <KStandardGuiItem>
38 #include <KDebug>
40 #include <kpColorCells.h>
41 #include <kpColorCollection.h>
42 #include <kpColorToolBar.h>
43 #include <kpUrlFormatter.h>
46 static QStringList KDEColorCollectionNames ()
48 return kpColorCollection::installedCollections ();
52 // private
53 void kpMainWindow::setupColorsMenuActions ()
55 KActionCollection *ac = actionCollection ();
58 d->actionColorsDefault = ac->addAction ("colors_default");
59 d->actionColorsDefault->setText (i18n ("Use KolourPaint Defaults"));
60 connect (d->actionColorsDefault, SIGNAL (triggered (bool)),
61 SLOT (slotColorsDefault ()));
63 d->actionColorsKDE = ac->add <KSelectAction> ("colors_kde");
64 d->actionColorsKDE->setText (i18n ("Use KDE's"));
65 // TODO: Will this slot be called spuriously if there are no colors
66 // installed?
67 connect (d->actionColorsKDE, SIGNAL (triggered (QAction *)),
68 SLOT (slotColorsKDE ()));
69 foreach (const QString &colName, ::KDEColorCollectionNames ())
70 d->actionColorsKDE->addAction (colName);
72 d->actionColorsOpen = ac->addAction ("colors_open");
73 d->actionColorsOpen->setText (i18n ("&Open..."));
74 connect (d->actionColorsOpen, SIGNAL (triggered (bool)),
75 SLOT (slotColorsOpen ()));
77 d->actionColorsReload = ac->addAction ("colors_reload");
78 d->actionColorsReload->setText (i18n ("Reloa&d"));
79 connect (d->actionColorsReload, SIGNAL (triggered (bool)),
80 SLOT (slotColorsReload ()));
83 d->actionColorsSave = ac->addAction ("colors_save");
84 d->actionColorsSave->setText (i18n ("&Save"));
85 connect (d->actionColorsSave, SIGNAL (triggered (bool)),
86 SLOT (slotColorsSave ()));
88 d->actionColorsSaveAs = ac->addAction ("colors_save_as");
89 d->actionColorsSaveAs->setText (i18n ("Save &As..."));
90 connect (d->actionColorsSaveAs, SIGNAL (triggered (bool)),
91 SLOT (slotColorsSaveAs ()));
94 d->actionColorsAppendRow = ac->addAction ("colors_append_row");
95 d->actionColorsAppendRow->setText (i18n ("Add Row"));
96 connect (d->actionColorsAppendRow, SIGNAL (triggered (bool)),
97 SLOT (slotColorsAppendRow ()));
99 d->actionColorsDeleteRow = ac->addAction ("colors_delete_row");
100 d->actionColorsDeleteRow->setText (i18n ("Delete Last Row"));
101 connect (d->actionColorsDeleteRow, SIGNAL (triggered (bool)),
102 SLOT (slotColorsDeleteRow ()));
105 enableColorsMenuDocumentActions (false);
108 // private
109 void kpMainWindow::createColorBox ()
111 d->colorToolBar = new kpColorToolBar (i18n ("Color Box"), this);
113 // (needed for QMainWindow::saveState())
114 d->colorToolBar->setObjectName ("Color Box");
116 connect (colorCells (), SIGNAL (rowCountChanged (int)),
117 SLOT (slotUpdateColorsDeleteRowActionEnabled ()));
120 // private
121 void kpMainWindow::enableColorsMenuDocumentActions (bool enable)
123 d->actionColorsDefault->setEnabled (enable);
124 d->actionColorsKDE->setEnabled (enable);
125 d->actionColorsOpen->setEnabled (enable);
126 d->actionColorsReload->setEnabled (enable);
128 d->actionColorsSave->setEnabled (enable);
129 d->actionColorsSaveAs->setEnabled (enable);
131 d->actionColorsAppendRow->setEnabled (enable);
133 d->colorMenuDocumentActionsEnabled = enable;
135 slotUpdateColorsDeleteRowActionEnabled ();
138 // private slot
139 void kpMainWindow::slotUpdateColorsDeleteRowActionEnabled ()
141 // Currently, this is always enabled since kpColorCells guarantees that
142 // there will be at least one row of cells (which might all be of the
143 // invalid color).
145 // But this method is left here for future extensibility.
146 d->actionColorsDeleteRow->setEnabled (
147 d->colorMenuDocumentActionsEnabled && (colorCells ()->rowCount () > 0));
151 // Used in 2 situations:
153 // 1. User opens a color without using the "Use KDE's" submenu.
154 // 2. User attempts to open a color using the "Use KDE's" submenu but the
155 // opening fails.
157 // TODO: Maybe we could put the 3 actions (for different ways of opening
158 // colors) in an exclusive group -- this might elminate the need for
159 // this hack.
161 // private
162 void kpMainWindow::deselectActionColorsKDE ()
164 d->actionColorsKDE->setCurrentItem (-1);
168 // private
169 bool kpMainWindow::queryCloseColors ()
171 #if DEBUG_KP_MAIN_WINDOW
172 kDebug () << "kpMainWindow::queryCloseColors() colorCells.modified="
173 << colorCells ()->isModified ();
174 #endif
176 toolEndShape ();
178 if (!colorCells ()->isModified ())
179 return true; // ok to close
181 int result = KMessageBox::Cancel;
184 if (!colorCells ()->url ().isEmpty ())
186 result = KMessageBox::warningYesNoCancel (this,
187 i18n ("The color palette \"%1\" has been modified.\n"
188 "Do you want to save it?",
189 kpUrlFormatter::PrettyFilename (colorCells ()->url ())),
190 QString ()/*caption*/,
191 KStandardGuiItem::save (), KStandardGuiItem::discard ());
193 else
195 const QString name = colorCells ()->colorCollection ()->name ();
196 if (!name.isEmpty ())
198 result = KMessageBox::warningYesNoCancel (this,
199 i18n ("The KDE color palette \"%1\" has been modified.\n"
200 "Do you want to save it to a file?",
201 name),
202 QString ()/*caption*/,
203 KStandardGuiItem::save (), KStandardGuiItem::discard ());
205 else
207 result = KMessageBox::warningYesNoCancel (this,
208 i18n ("The default color palette has been modified.\n"
209 "Do you want to save it to a file?"),
210 QString ()/*caption*/,
211 KStandardGuiItem::save (), KStandardGuiItem::discard ());
215 switch (result)
217 case KMessageBox::Yes:
218 return slotColorsSave (); // close only if save succeeds
219 case KMessageBox::No:
220 return true; // close without saving
221 default:
222 return false; // don't close current doc
227 // private
228 void kpMainWindow::openDefaultColors ()
230 colorCells ()->setColorCollection (
231 kpColorCells::DefaultColorCollection ());
234 // private slot
235 void kpMainWindow::slotColorsDefault ()
237 // Call just in case.
238 toolEndShape ();
240 if (!queryCloseColors ())
241 return;
243 openDefaultColors ();
245 deselectActionColorsKDE ();
248 // private
249 bool kpMainWindow::openKDEColors (const QString &name)
251 #if DEBUG_KP_MAIN_WINDOW
252 kDebug () << "kpMainWindow::openKDEColors(" << name << ")";
253 #endif
255 kpColorCollection colorCol;
256 if (colorCol.openKDE (name, this))
258 #if DEBUG_KP_MAIN_WINDOW
259 kDebug () << "opened";
260 #endif
261 colorCells ()->setColorCollection (colorCol);
262 return true;
264 else
266 #if DEBUG_KP_MAIN_WINDOW
267 kDebug () << "failed to open";
268 #endif
269 return false;
273 // private slot
274 void kpMainWindow::slotColorsKDE ()
276 // Call in case an error dialog appears.
277 toolEndShape ();
279 const int curItem = d->actionColorsKDE->currentItem ();
281 if (!queryCloseColors ())
283 deselectActionColorsKDE ();
284 return;
286 else
288 // queryCloseColors() calls slotColorSave(), which can call
289 // slotColorSaveAs(), which can call deselectActionColorsKDE().
290 d->actionColorsKDE->setCurrentItem (curItem);
293 const QStringList colNames = ::KDEColorCollectionNames ();
294 const int selected = d->actionColorsKDE->currentItem ();
295 Q_ASSERT (selected >= 0 && selected < colNames.size ());
297 if (!openKDEColors (colNames [selected]))
298 deselectActionColorsKDE ();
301 // private
302 bool kpMainWindow::openColors (const KUrl &url)
304 if (!colorCells ()->openColorCollection (url))
305 return false;
307 return true;
310 // private slot
311 void kpMainWindow::slotColorsOpen ()
313 // Call due to dialog.
314 toolEndShape ();
316 KFileDialog fd (colorCells ()->url (), QString()/*filter*/, this);
317 fd.setCaption (i18n ("Open Color Palette"));
318 fd.setOperationMode (KFileDialog::Opening);
320 if (fd.exec ())
322 if (!queryCloseColors ())
323 return;
325 if (openColors (fd.selectedUrl ()))
326 deselectActionColorsKDE ();
330 // private slot
331 void kpMainWindow::slotColorsReload ()
333 toolEndShape ();
335 if (colorCells ()->isModified ())
337 int result = KMessageBox::Cancel;
339 if (!colorCells ()->url ().isEmpty ())
341 result = KMessageBox::warningContinueCancel (this,
342 i18n ("The color palette \"%1\" has been modified.\n"
343 "Reloading will lose all changes since you last saved it.\n"
344 "Are you sure?",
345 kpUrlFormatter::PrettyFilename (colorCells ()->url ())),
346 QString ()/*caption*/,
347 KGuiItem(i18n ("&Reload")));
349 else
351 const QString name = colorCells ()->colorCollection ()->name ();
352 if (!name.isEmpty ())
354 result = KMessageBox::warningContinueCancel (this,
355 i18n ("The KDE color palette \"%1\" has been modified.\n"
356 "Reloading will lose all changes.\n"
357 "Are you sure?",
358 colorCells ()->colorCollection ()->name ()),
359 QString ()/*caption*/,
360 KGuiItem (i18n ("&Reload")));
362 else
364 result = KMessageBox::warningContinueCancel (this,
365 i18n ("The default color palette has been modified.\n"
366 "Reloading will lose all changes.\n"
367 "Are you sure?"),
368 QString ()/*caption*/,
369 KGuiItem (i18n ("&Reload")));
373 #if DEBUG_KP_MAIN_WINDOW
374 kDebug () << "result=" << result
375 << "vs KMessageBox::Continue" << KMessageBox::Continue;
376 #endif
377 if (result != KMessageBox::Continue)
378 return;
382 if (!colorCells ()->url ().isEmpty ())
384 openColors (colorCells ()->url ());
386 else
388 const QString name = colorCells ()->colorCollection ()->name ();
389 if (!name.isEmpty ())
390 openKDEColors (name);
391 else
392 openDefaultColors ();
397 // private slot
398 bool kpMainWindow::slotColorsSave ()
400 // Call due to dialog.
401 toolEndShape ();
403 if (colorCells ()->url ().isEmpty ())
405 return slotColorsSaveAs ();
408 return colorCells ()->saveColorCollection ();
411 // private slot
412 bool kpMainWindow::slotColorsSaveAs ()
414 // Call due to dialog.
415 toolEndShape ();
417 KFileDialog fd (colorCells ()->url (), QString()/*filter*/, this);
418 fd.setCaption (i18n ("Save Color Palette As"));
419 fd.setOperationMode (KFileDialog::Saving);
421 if (fd.exec ())
423 if (!colorCells ()->saveColorCollectionAs (fd.selectedUrl ()))
424 return false;
426 // We're definitely using our own color collection now.
427 deselectActionColorsKDE ();
429 return true;
431 else
432 return false;
436 // private slot
437 void kpMainWindow::slotColorsAppendRow ()
439 // Call just in case.
440 toolEndShape ();
442 kpColorCells *colorCells = d->colorToolBar->colorCells ();
443 colorCells->appendRow ();
446 // private slot
447 void kpMainWindow::slotColorsDeleteRow ()
449 // Call just in case.
450 toolEndShape ();
452 kpColorCells *colorCells = d->colorToolBar->colorCells ();
453 colorCells->deleteLastRow ();