3 Copyright (c) 2003-2007 Clarence Dang <dang@kde.org>
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions
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>
33 #include <KActionCollection>
34 #include <KFileDialog>
35 #include <KMessageBox>
36 #include <KSelectAction>
37 #include <KStandardGuiItem>
40 #include <kpColorCells.h>
41 #include <kpColorCollection.h>
42 #include <kpColorToolBar.h>
43 #include <kpUrlFormatter.h>
46 static QStringList
KDEColorCollectionNames ()
48 return kpColorCollection::installedCollections ();
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
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);
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 ()));
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 ();
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
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
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
162 void kpMainWindow::deselectActionColorsKDE ()
164 d
->actionColorsKDE
->setCurrentItem (-1);
169 bool kpMainWindow::queryCloseColors ()
171 #if DEBUG_KP_MAIN_WINDOW
172 kDebug () << "kpMainWindow::queryCloseColors() colorCells.modified="
173 << colorCells ()->isModified ();
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 ());
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?",
202 QString ()/*caption*/,
203 KStandardGuiItem::save (), KStandardGuiItem::discard ());
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 ());
217 case KMessageBox::Yes
:
218 return slotColorsSave (); // close only if save succeeds
219 case KMessageBox::No
:
220 return true; // close without saving
222 return false; // don't close current doc
228 void kpMainWindow::openDefaultColors ()
230 colorCells ()->setColorCollection (
231 kpColorCells::DefaultColorCollection ());
235 void kpMainWindow::slotColorsDefault ()
237 // Call just in case.
240 if (!queryCloseColors ())
243 openDefaultColors ();
245 deselectActionColorsKDE ();
249 bool kpMainWindow::openKDEColors (const QString
&name
)
251 #if DEBUG_KP_MAIN_WINDOW
252 kDebug () << "kpMainWindow::openKDEColors(" << name
<< ")";
255 kpColorCollection colorCol
;
256 if (colorCol
.openKDE (name
, this))
258 #if DEBUG_KP_MAIN_WINDOW
259 kDebug () << "opened";
261 colorCells ()->setColorCollection (colorCol
);
266 #if DEBUG_KP_MAIN_WINDOW
267 kDebug () << "failed to open";
274 void kpMainWindow::slotColorsKDE ()
276 // Call in case an error dialog appears.
279 const int curItem
= d
->actionColorsKDE
->currentItem ();
281 if (!queryCloseColors ())
283 deselectActionColorsKDE ();
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 ();
302 bool kpMainWindow::openColors (const KUrl
&url
)
304 if (!colorCells ()->openColorCollection (url
))
311 void kpMainWindow::slotColorsOpen ()
313 // Call due to dialog.
316 KFileDialog
fd (colorCells ()->url (), QString()/*filter*/, this);
317 fd
.setCaption (i18n ("Open Color Palette"));
318 fd
.setOperationMode (KFileDialog::Opening
);
322 if (!queryCloseColors ())
325 if (openColors (fd
.selectedUrl ()))
326 deselectActionColorsKDE ();
331 void kpMainWindow::slotColorsReload ()
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"
345 kpUrlFormatter::PrettyFilename (colorCells ()->url ())),
346 QString ()/*caption*/,
347 KGuiItem(i18n ("&Reload")));
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"
358 colorCells ()->colorCollection ()->name ()),
359 QString ()/*caption*/,
360 KGuiItem (i18n ("&Reload")));
364 result
= KMessageBox::warningContinueCancel (this,
365 i18n ("The default color palette has been modified.\n"
366 "Reloading will lose all changes.\n"
368 QString ()/*caption*/,
369 KGuiItem (i18n ("&Reload")));
373 #if DEBUG_KP_MAIN_WINDOW
374 kDebug () << "result=" << result
375 << "vs KMessageBox::Continue" << KMessageBox::Continue
;
377 if (result
!= KMessageBox::Continue
)
382 if (!colorCells ()->url ().isEmpty ())
384 openColors (colorCells ()->url ());
388 const QString name
= colorCells ()->colorCollection ()->name ();
389 if (!name
.isEmpty ())
390 openKDEColors (name
);
392 openDefaultColors ();
398 bool kpMainWindow::slotColorsSave ()
400 // Call due to dialog.
403 if (colorCells ()->url ().isEmpty ())
405 return slotColorsSaveAs ();
408 return colorCells ()->saveColorCollection ();
412 bool kpMainWindow::slotColorsSaveAs ()
414 // Call due to dialog.
417 KFileDialog
fd (colorCells ()->url (), QString()/*filter*/, this);
418 fd
.setCaption (i18n ("Save Color Palette As"));
419 fd
.setOperationMode (KFileDialog::Saving
);
423 if (!colorCells ()->saveColorCollectionAs (fd
.selectedUrl ()))
426 // We're definitely using our own color collection now.
427 deselectActionColorsKDE ();
437 void kpMainWindow::slotColorsAppendRow ()
439 // Call just in case.
442 kpColorCells
*colorCells
= d
->colorToolBar
->colorCells ();
443 colorCells
->appendRow ();
447 void kpMainWindow::slotColorsDeleteRow ()
449 // Call just in case.
452 kpColorCells
*colorCells
= d
->colorToolBar
->colorCells ();
453 colorCells
->deleteLastRow ();