[Ktutorial-commits] SF.net SVN: ktutorial:[211] trunk/ktutorial/ktutorial-editor/src
Status: Alpha
Brought to you by:
danxuliu
From: <dan...@us...> - 2010-03-29 22:24:52
|
Revision: 211 http://ktutorial.svn.sourceforge.net/ktutorial/?rev=211&view=rev Author: danxuliu Date: 2010-03-29 22:24:45 +0000 (Mon, 29 Mar 2010) Log Message: ----------- Warn the user when a tutorial is going to be closed and the changes were not saved yet. Modified Paths: -------------- trunk/ktutorial/ktutorial-editor/src/KTutorialEditor.cpp trunk/ktutorial/ktutorial-editor/src/KTutorialEditor.h Modified: trunk/ktutorial/ktutorial-editor/src/KTutorialEditor.cpp =================================================================== --- trunk/ktutorial/ktutorial-editor/src/KTutorialEditor.cpp 2010-03-29 17:17:44 UTC (rev 210) +++ trunk/ktutorial/ktutorial-editor/src/KTutorialEditor.cpp 2010-03-29 22:24:45 UTC (rev 211) @@ -73,9 +73,42 @@ setupGUI(); } +//protected: + +bool KTutorialEditor::queryClose() { + return queryCloseTutorial(); +} + //private: +bool KTutorialEditor::queryCloseTutorial() { + if (mUndoStack->isClean()) { + return true; + } + + QString text = i18nc("@label", "The tutorial has been modified.<nl/>" +"Do you want to save your changes or discard them?"); + QString caption = i18nc("@title:window", "Close tutorial"); + int button = KMessageBox::warningYesNoCancel(this, text, caption, + KStandardGuiItem::save(), + KStandardGuiItem::discard()); + + if (button == KMessageBox::Cancel) { + return false; + } + + if (button == KMessageBox::Yes) { + return saveTutorial(); + } + + return true; +} + void KTutorialEditor::setTutorialToBeEdited(Tutorial* tutorial) { + if (!queryCloseTutorial()) { + return; + } + if (!tutorial) { tutorial = new Tutorial(this); } @@ -147,7 +180,7 @@ actionCollection()->addAction("exportTutorial", action); connect(action, SIGNAL(triggered(bool)), this, SLOT(exportTutorial())); - KStandardAction::quit(kapp, SLOT(quit()), actionCollection()); + KStandardAction::quit(this, SLOT(close()), actionCollection()); mUndoStack->createUndoAction(actionCollection()); @@ -404,10 +437,9 @@ handleUndoStackCleanChanged(true); } -void KTutorialEditor::saveTutorial() { +bool KTutorialEditor::saveTutorial() { if (mTutorialUrl.isEmpty()) { - saveTutorialAs(); - return; + return saveTutorialAs(); } try { @@ -417,13 +449,15 @@ "save the tutorial:<nl/>%1", e.message()); QString caption = i18nc("@title:window", "Tutorial could not be saved"); KMessageBox::error(this, text, caption); - return; + return false; } mUndoStack->setClean(); + + return true; } -void KTutorialEditor::saveTutorialAs() { +bool KTutorialEditor::saveTutorialAs() { KUrl url = mTutorialUrl; QPointer<KFileDialog> dialog = new KFileDialog(url, QString(), this); @@ -436,7 +470,7 @@ dialog->filterWidget()->setEditable(false); if (dialog->exec() == QDialog::Rejected) { - return; + return false; } try { @@ -446,7 +480,7 @@ "save the tutorial:<nl/>%1", e.message()); QString caption = i18nc("@title:window", "Tutorial could not be saved"); KMessageBox::error(this, text, caption); - return; + return false; } mTutorialUrl = dialog->selectedUrl(); @@ -454,6 +488,8 @@ //Force clean state, as setting an empty stack as clean would not emit //cleanChanged() handleUndoStackCleanChanged(true); + + return true; } void KTutorialEditor::exportTutorial() { Modified: trunk/ktutorial/ktutorial-editor/src/KTutorialEditor.h =================================================================== --- trunk/ktutorial/ktutorial-editor/src/KTutorialEditor.h 2010-03-29 17:17:44 UTC (rev 210) +++ trunk/ktutorial/ktutorial-editor/src/KTutorialEditor.h 2010-03-29 22:24:45 UTC (rev 211) @@ -43,6 +43,19 @@ */ KTutorialEditor(); +protected: + + /** + * Called before the window is closed, either by the user or indirectly by + * the session manager. + * It checks whether the tutorial can be closed or not. + * + * Reimplemented from KMainWindow::queryClose(). + * + * @return True if the window can be closed, false otherwise. + */ + virtual bool queryClose(); + private: /** @@ -91,12 +104,23 @@ Reaction* mCurrentReaction; /** + * Checks whether the tutorial can be closed or not. + * If the tutorial is not clean, the user is asked if it has to be saved or + * not, or if the close operation should be cancelled. + * + * @return True if the tutorial can be closed, false otherwise. + */ + bool queryCloseTutorial(); + + /** * Sets the tutorial to be edited. * It creates a new tutorial, prepares the tree view to represent it and * handles the selection of items. * * If the tutorial is null, a new empty tutorial is set. * + * Nothing is done if the previous tutorial should not be closed. + * * @param tutorial The tutorial to set. */ void setTutorialToBeEdited(Tutorial* tutorial = 0); @@ -184,15 +208,19 @@ * A clean state is set. If there is no tutorial URL it behaves like * saveTutorialAs(). * An error message is shown if the tutorial couldn't be saved. + * + * @return True if the tutorial was successfully saved, false otherwise. */ - void saveTutorial(); + bool saveTutorial(); /** * Shows a KFileDialog to select the file to save the tutorial to. * The tutorial URL is updated and a clean state is forced. * An error message is shown if the tutorial couldn't be saved. + * + * @return True if the tutorial was successfully saved, false otherwise. */ - void saveTutorialAs(); + bool saveTutorialAs(); /** * Shows a KFileDialog to select the file to save the exported tutorial in. This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |