[Ktutorial-commits] SF.net SVN: ktutorial:[210] trunk/ktutorial/ktutorial-editor/src
Status: Alpha
Brought to you by:
danxuliu
From: <dan...@us...> - 2010-03-29 17:17:50
|
Revision: 210 http://ktutorial.svn.sourceforge.net/ktutorial/?rev=210&view=rev Author: danxuliu Date: 2010-03-29 17:17:44 +0000 (Mon, 29 Mar 2010) Log Message: ----------- Enable or disable Save action and set caption based on the undo stack state. 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 06:46:22 UTC (rev 209) +++ trunk/ktutorial/ktutorial-editor/src/KTutorialEditor.cpp 2010-03-29 17:17:44 UTC (rev 210) @@ -61,15 +61,14 @@ setCentralWidget(mTreeView); mUndoStack = new KUndoStack(this); - setTutorialToBeEdited(); + connect(mUndoStack, SIGNAL(cleanChanged(bool)), + this, SLOT(handleUndoStackCleanChanged(bool))); setupDocks(); setupActions(); - //Setting the url enables/disables File->Save action, so it has to be - //set after it is created - setTutorialUrl(KUrl()); + newTutorial(); setupGUI(); } @@ -115,24 +114,6 @@ mTutorial = tutorial; } -void KTutorialEditor::setTutorialUrl(const KUrl& url) { - mTutorialUrl = url; - - if (url.isEmpty()) { - actionCollection()->action("file_save")->setEnabled(false); - - setCaption(""); - } else { - actionCollection()->action("file_save")->setEnabled(true); - - QString caption = url.prettyUrl(); - if (caption.length() > 64) { - caption = "..." + caption.right(64); - } - setCaption(caption); - } -} - void KTutorialEditor::setupDocks() { mTutorialActionDock = new QDockWidget(i18nc("@title", "Edit tutorial"), this); @@ -308,6 +289,20 @@ mReactionActionDock->toggleViewAction()); } +QString KTutorialEditor::captionFromTutorialUrl() { + if (mTutorialUrl.isEmpty()) { + return i18nc("@title:window Window title for KTutorial editor when the \ +tutorial has no associated URL", "New file"); + } + + QString caption = mTutorialUrl.prettyUrl(); + if (caption.length() > 64) { + caption = "..." + caption.right(64); + } + + return caption; +} + int KTutorialEditor::showEditionDialog(CommandWidget* commandWidget) { commandWidget->setUndoStack(mUndoStack); @@ -351,9 +346,23 @@ } } +void KTutorialEditor::handleUndoStackCleanChanged(bool clean) { + QString caption = captionFromTutorialUrl(); + if (clean && !mTutorialUrl.isEmpty()) { + actionCollection()->action("file_save")->setEnabled(false); + setCaption(caption); + } else { + actionCollection()->action("file_save")->setEnabled(true); + setCaption(i18nc("@title:window Wrapper for the window title when the \ +tutorial was modified but not saved yet", "%1 [not saved]", caption)); + } +} + void KTutorialEditor::newTutorial() { setTutorialToBeEdited(); - setTutorialUrl(KUrl()); + mTutorialUrl = KUrl(); + //Force unclean state, as clearing the stack returns it to the clean state + handleUndoStackCleanChanged(false); } void KTutorialEditor::openTutorial() { @@ -388,10 +397,19 @@ } setTutorialToBeEdited(tutorial); - setTutorialUrl(dialog->selectedUrl()); + mTutorialUrl = dialog->selectedUrl(); + mUndoStack->setClean(); + //Force clean state, as setting an empty stack as clean would not emit + //cleanChanged() + handleUndoStackCleanChanged(true); } void KTutorialEditor::saveTutorial() { + if (mTutorialUrl.isEmpty()) { + saveTutorialAs(); + return; + } + try { Serialization::saveTutorial(mTutorial, mTutorialUrl); } catch (IOException e) { @@ -399,7 +417,10 @@ "save the tutorial:<nl/>%1", e.message()); QString caption = i18nc("@title:window", "Tutorial could not be saved"); KMessageBox::error(this, text, caption); + return; } + + mUndoStack->setClean(); } void KTutorialEditor::saveTutorialAs() { @@ -428,7 +449,11 @@ return; } - setTutorialUrl(dialog->selectedUrl()); + mTutorialUrl = dialog->selectedUrl(); + mUndoStack->setClean(); + //Force clean state, as setting an empty stack as clean would not emit + //cleanChanged() + handleUndoStackCleanChanged(true); } void KTutorialEditor::exportTutorial() { Modified: trunk/ktutorial/ktutorial-editor/src/KTutorialEditor.h =================================================================== --- trunk/ktutorial/ktutorial-editor/src/KTutorialEditor.h 2010-03-29 06:46:22 UTC (rev 209) +++ trunk/ktutorial/ktutorial-editor/src/KTutorialEditor.h 2010-03-29 17:17:44 UTC (rev 210) @@ -102,16 +102,6 @@ void setTutorialToBeEdited(Tutorial* tutorial = 0); /** - * Sets the URL to save the tutorial to. - * An empty URL disables Save action, otherwise it is enabled. - * The caption (window title) is updated with the URL. The URL in the - * caption is shown truncated if it is too lengthy. - * - * @param url The URL to save the tutorial to. - */ - void setTutorialUrl(const KUrl& url); - - /** * Sets up the dock widgets. */ void setupDocks(); @@ -122,6 +112,15 @@ void setupActions(); /** + * Returns a caption (window title) string based on the tutorial URL. + * The caption contains the URL, which is truncated if it is too lengthy. If + * the URL is empty, "New file" is returned. + * + * @return A caption string based on the tutorial URL. + */ + QString captionFromTutorialUrl(); + + /** * Shows an EditionDialog for the given CommandWidget. * The undo stack used in the CommandWidget is mUndoStack. * @@ -151,27 +150,46 @@ void selectReaction(Reaction* reaction); /** + * Modifies the caption and enables or disables Save action based on the + * clean state of the stack. + * When the stack is not clean or the tutorial does not have an associated + * URL, "[not saved]" is added after the caption (the URL or "New file", + * depending on the case). Otherwise, the caption is the URL associated to + * the tutorial. + * + * When the stack is clean and the tutorial has an associated URL, the Save + * action is disabled. Otherwise, it is enabled, so if there is no + * associated URL the Save action is always kept enabled, even if the stack + * is clean. + * + * @param clean Whether the undo stack entered clean state or not. + */ + void handleUndoStackCleanChanged(bool clean); + + /** * Creates a new empty tutorial replacing the current one, if any. - * The tutorial URL is cleared. + * The tutorial URL is cleared and an unclean state is forced. */ void newTutorial(); /** * Shows a KFileDialog to select the file to open the tutorial from. - * The tutorial URL is updated. + * The tutorial URL is updated and a clean state is forced. * An error message is shown if the tutorial couldn't be opened. */ void openTutorial(); /** * Saves the tutorial to the tutorial URL. + * 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. */ void saveTutorial(); /** * Shows a KFileDialog to select the file to save the tutorial to. - * The tutorial URL is updated. + * The tutorial URL is updated and a clean state is forced. * An error message is shown if the tutorial couldn't be saved. */ void saveTutorialAs(); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |