[Ktutorial-commits] SF.net SVN: ktutorial:[205] trunk/ktutorial/ktutorial-editor
Status: Alpha
Brought to you by:
danxuliu
|
From: <dan...@us...> - 2010-03-29 00:36:20
|
Revision: 205
http://ktutorial.svn.sourceforge.net/ktutorial/?rev=205&view=rev
Author: danxuliu
Date: 2010-03-29 00:36:12 +0000 (Mon, 29 Mar 2010)
Log Message:
-----------
Add undo/redo support for every edit action
Modified Paths:
--------------
trunk/ktutorial/ktutorial-editor/src/CMakeLists.txt
trunk/ktutorial/ktutorial-editor/src/KTutorialEditor.cpp
trunk/ktutorial/ktutorial-editor/src/KTutorialEditor.h
trunk/ktutorial/ktutorial-editor/src/view/CMakeLists.txt
trunk/ktutorial/ktutorial-editor/src/view/LicenseWidget.cpp
trunk/ktutorial/ktutorial-editor/src/view/LicenseWidget.h
trunk/ktutorial/ktutorial-editor/src/view/ReactionWidget.cpp
trunk/ktutorial/ktutorial-editor/src/view/ReactionWidget.h
trunk/ktutorial/ktutorial-editor/src/view/StepCustomCodeWidget.cpp
trunk/ktutorial/ktutorial-editor/src/view/StepCustomCodeWidget.h
trunk/ktutorial/ktutorial-editor/src/view/StepDataWidget.cpp
trunk/ktutorial/ktutorial-editor/src/view/StepDataWidget.h
trunk/ktutorial/ktutorial-editor/src/view/TutorialCustomCodeWidget.cpp
trunk/ktutorial/ktutorial-editor/src/view/TutorialCustomCodeWidget.h
trunk/ktutorial/ktutorial-editor/src/view/TutorialInformationWidget.cpp
trunk/ktutorial/ktutorial-editor/src/view/TutorialInformationWidget.h
trunk/ktutorial/ktutorial-editor/tests/unit/CMakeLists.txt
trunk/ktutorial/ktutorial-editor/tests/unit/view/CMakeLists.txt
Added Paths:
-----------
trunk/ktutorial/ktutorial-editor/src/commands/
trunk/ktutorial/ktutorial-editor/src/commands/CMakeLists.txt
trunk/ktutorial/ktutorial-editor/src/commands/ReactionCommands.cpp
trunk/ktutorial/ktutorial-editor/src/commands/ReactionCommands.h
trunk/ktutorial/ktutorial-editor/src/commands/StepCommands.cpp
trunk/ktutorial/ktutorial-editor/src/commands/StepCommands.h
trunk/ktutorial/ktutorial-editor/src/commands/TutorialCommands.cpp
trunk/ktutorial/ktutorial-editor/src/commands/TutorialCommands.h
trunk/ktutorial/ktutorial-editor/src/view/CommandWidget.cpp
trunk/ktutorial/ktutorial-editor/src/view/CommandWidget.h
trunk/ktutorial/ktutorial-editor/tests/unit/commands/
trunk/ktutorial/ktutorial-editor/tests/unit/commands/CMakeLists.txt
trunk/ktutorial/ktutorial-editor/tests/unit/commands/ReactionCommandsTest.cpp
trunk/ktutorial/ktutorial-editor/tests/unit/commands/StepCommandsTest.cpp
trunk/ktutorial/ktutorial-editor/tests/unit/commands/TutorialCommandsTest.cpp
trunk/ktutorial/ktutorial-editor/tests/unit/view/CommandWidgetTest.cpp
Modified: trunk/ktutorial/ktutorial-editor/src/CMakeLists.txt
===================================================================
--- trunk/ktutorial/ktutorial-editor/src/CMakeLists.txt 2010-03-28 07:46:36 UTC (rev 204)
+++ trunk/ktutorial/ktutorial-editor/src/CMakeLists.txt 2010-03-29 00:36:12 UTC (rev 205)
@@ -4,6 +4,7 @@
include_directories(${KDE4_INCLUDES})
+add_subdirectory(commands)
add_subdirectory(data)
add_subdirectory(serialization)
add_subdirectory(view)
Modified: trunk/ktutorial/ktutorial-editor/src/KTutorialEditor.cpp
===================================================================
--- trunk/ktutorial/ktutorial-editor/src/KTutorialEditor.cpp 2010-03-28 07:46:36 UTC (rev 204)
+++ trunk/ktutorial/ktutorial-editor/src/KTutorialEditor.cpp 2010-03-29 00:36:12 UTC (rev 205)
@@ -28,8 +28,11 @@
#include <KFileFilterCombo>
#include <KLocalizedString>
#include <KMessageBox>
+#include <KUndoStack>
#include <KIO/NetAccess>
+#include "commands/StepCommands.h"
+#include "commands/TutorialCommands.h"
#include "data/Reaction.h"
#include "data/Step.h"
#include "data/Tutorial.h"
@@ -57,6 +60,7 @@
mTreeView->setObjectName("centralTreeView");
setCentralWidget(mTreeView);
+ mUndoStack = new KUndoStack(this);
setTutorialToBeEdited();
setupDocks();
@@ -105,6 +109,8 @@
connect(selectionManager, SIGNAL(reactionSelected(Reaction*)),
this, SLOT(selectReaction(Reaction*)));
+ mUndoStack->clear();
+
delete mTutorial;
mTutorial = tutorial;
}
@@ -162,6 +168,10 @@
KStandardAction::quit(kapp, SLOT(quit()), actionCollection());
+ mUndoStack->createUndoAction(actionCollection());
+
+ mUndoStack->createRedoAction(actionCollection());
+
ActionListWidget* actionListWidget =
new ActionListWidget(mTutorialActionDock);
@@ -298,8 +308,10 @@
mReactionActionDock->toggleViewAction());
}
-int KTutorialEditor::showEditionDialog(EditionWidget* editionWidget) {
- EditionDialog* dialog = new EditionDialog(editionWidget, this);
+int KTutorialEditor::showEditionDialog(CommandWidget* commandWidget) {
+ commandWidget->setUndoStack(mUndoStack);
+
+ EditionDialog* dialog = new EditionDialog(commandWidget, this);
dialog->setObjectName("editionDialog");
int dialogCode = dialog->exec();
dialog->deleteLater();
@@ -468,10 +480,14 @@
void KTutorialEditor::addStep() {
Step* step = new Step();
- if (showEditionDialog(new StepDataWidget(step)) == QDialog::Accepted) {
- mTutorial->addStep(step);
- } else {
- delete step;
+ QUndoCommand* parentCommand = new QUndoCommand();
+ parentCommand->setText(i18nc("@action", "Add step"));
+ TutorialCommands(mTutorial).addStep(step, parentCommand);
+
+ CommandWidget* widget = new StepDataWidget(step);
+ widget->setParentUndoCommand(parentCommand);
+ if (showEditionDialog(widget) == QDialog::Rejected) {
+ delete parentCommand;
}
}
@@ -494,12 +510,7 @@
void KTutorialEditor::removeStep() {
Q_ASSERT(mCurrentStep);
- //When the step is removed, mCurrentStep is changed because the selected
- //item in the tree view changes. As mCurrentStep is one of the valid steps
- //in the tutorial, deleting it leads to a crash the next time it is used.
- Step* stepToRemove = mCurrentStep;
- mTutorial->removeStep(stepToRemove);
- stepToRemove->deleteLater();
+ mUndoStack->push(TutorialCommands(mTutorial).removeStep(mCurrentStep));
}
void KTutorialEditor::addReaction() {
@@ -507,10 +518,14 @@
Reaction* reaction = new Reaction();
- if (showEditionDialog(new ReactionWidget(reaction)) == QDialog::Accepted) {
- mCurrentStep->addReaction(reaction);
- } else {
- delete reaction;
+ QUndoCommand* parentCommand = new QUndoCommand();
+ parentCommand->setText(i18nc("@action", "Add reaction"));
+ StepCommands(mCurrentStep).addReaction(reaction, parentCommand);
+
+ CommandWidget* widget = new ReactionWidget(reaction);
+ widget->setParentUndoCommand(parentCommand);
+ if (showEditionDialog(widget) == QDialog::Rejected) {
+ delete parentCommand;
}
}
@@ -523,11 +538,6 @@
void KTutorialEditor::removeReaction() {
Q_ASSERT(mCurrentStep);
- //When the reaction is removed, mCurrentReaction is changed because the
- //selected item in the tree view changes. As mCurrentReaction is one of the
- //valid reactions in the step, deleting it leads to a crash the next time it
- //is used.
- Reaction* reactionToRemove = mCurrentReaction;
- mCurrentStep->removeReaction(reactionToRemove);
- reactionToRemove->deleteLater();
+ mUndoStack->push(StepCommands(mCurrentStep).removeReaction(
+ mCurrentReaction));
}
Modified: trunk/ktutorial/ktutorial-editor/src/KTutorialEditor.h
===================================================================
--- trunk/ktutorial/ktutorial-editor/src/KTutorialEditor.h 2010-03-28 07:46:36 UTC (rev 204)
+++ trunk/ktutorial/ktutorial-editor/src/KTutorialEditor.h 2010-03-29 00:36:12 UTC (rev 205)
@@ -22,7 +22,9 @@
#include <KXmlGuiWindow>
#include <KUrl>
+class CommandWidget;
class EditionWidget;
+class KUndoStack;
class QTreeView;
class Reaction;
class Step;
@@ -64,6 +66,11 @@
QDockWidget* mReactionActionDock;
/**
+ * The stack of undoable commands.
+ */
+ KUndoStack* mUndoStack;
+
+ /**
* The tutorial being edited.
*/
Tutorial* mTutorial;
@@ -115,13 +122,14 @@
void setupActions();
/**
- * Shows an EditionDialog for the given EditionWidget.
+ * Shows an EditionDialog for the given CommandWidget.
+ * The undo stack used in the CommandWidget is mUndoStack.
*
- * @param editionWidget The EditionWidget to wrap.
+ * @param commandWidget The CommandWidget to wrap.
* @return QDialog::Accepted if the dialog was accepted, or
* QDialog::Rejected if the dialog was rejected.
*/
- int showEditionDialog(EditionWidget* editionWidget);
+ int showEditionDialog(CommandWidget* commandWidget);
private Q_SLOTS:
Added: trunk/ktutorial/ktutorial-editor/src/commands/CMakeLists.txt
===================================================================
--- trunk/ktutorial/ktutorial-editor/src/commands/CMakeLists.txt (rev 0)
+++ trunk/ktutorial/ktutorial-editor/src/commands/CMakeLists.txt 2010-03-29 00:36:12 UTC (rev 205)
@@ -0,0 +1,9 @@
+set(ktutorial_editor_commands_SRCS
+ ReactionCommands.cpp
+ StepCommands.cpp
+ TutorialCommands.cpp
+)
+
+kde4_add_library(ktutorial_editor_commands ${ktutorial_editor_commands_SRCS})
+
+target_link_libraries(ktutorial_editor_commands ktutorial_editor_data ${KDE4_KDEUI_LIBS})
Property changes on: trunk/ktutorial/ktutorial-editor/src/commands/CMakeLists.txt
___________________________________________________________________
Added: svn:eol-style
+ native
Added: trunk/ktutorial/ktutorial-editor/src/commands/ReactionCommands.cpp
===================================================================
--- trunk/ktutorial/ktutorial-editor/src/commands/ReactionCommands.cpp (rev 0)
+++ trunk/ktutorial/ktutorial-editor/src/commands/ReactionCommands.cpp 2010-03-29 00:36:12 UTC (rev 205)
@@ -0,0 +1,226 @@
+/***************************************************************************
+ * Copyright (C) 2010 by Daniel Calviño Sánchez *
+ * dan...@gm... *
+ * *
+ * This program is free software; you can redistribute it and/or modify *
+ * it under the terms of the GNU General Public License as published by *
+ * the Free Software Foundation; either version 3 of the License, or *
+ * (at your option) any later version. *
+ * *
+ * This program is distributed in the hope that it will be useful, *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
+ * GNU General Public License for more details. *
+ * *
+ * You should have received a copy of the GNU General Public License *
+ * along with this program; If not, see <http://www.gnu.org/licenses/>. *
+ ***************************************************************************/
+
+#include "ReactionCommands.h"
+
+#include <QUndoCommand>
+
+#include <KLocalizedString>
+
+#include "../data/Reaction.h"
+#include "../data/WaitFor.h"
+
+class SetReactionTriggerType: public QUndoCommand {
+public:
+
+ Reaction* mReaction;
+ Reaction::TriggerType mOldTriggerType;
+ Reaction::TriggerType mNewTriggerType;
+
+ SetReactionTriggerType(QUndoCommand* parent = 0): QUndoCommand(parent) {
+ setText(i18nc("@action", "Set reaction trigger type"));
+ }
+
+ virtual void redo() {
+ mOldTriggerType = mReaction->triggerType();
+ mReaction->setTriggerType(mNewTriggerType);
+ }
+
+ virtual void undo() {
+ mReaction->setTriggerType(mOldTriggerType);
+ }
+
+};
+
+class SetReactionOptionName: public QUndoCommand {
+public:
+
+ Reaction* mReaction;
+ QString mOldOptionName;
+ QString mNewOptionName;
+
+ SetReactionOptionName(QUndoCommand* parent = 0): QUndoCommand(parent) {
+ setText(i18nc("@action", "Set reaction option name"));
+ }
+
+ virtual void redo() {
+ mOldOptionName = mReaction->optionName();
+ mReaction->setOptionName(mNewOptionName);
+ }
+
+ virtual void undo() {
+ mReaction->setOptionName(mOldOptionName);
+ }
+
+};
+
+class SetReactionWaitFor: public QUndoCommand {
+public:
+
+ Reaction* mReaction;
+ WaitFor* mOldWaitFor;
+ WaitFor* mNewWaitFor;
+ bool mRedoDone;
+
+ SetReactionWaitFor(QUndoCommand* parent = 0): QUndoCommand(parent),
+ mRedoDone(false) {
+ setText(i18nc("@action", "Set reaction condition to wait for"));
+ }
+
+ virtual ~SetReactionWaitFor() {
+ if (mRedoDone) {
+ delete mOldWaitFor;
+ } else {
+ delete mNewWaitFor;
+ }
+ }
+
+ virtual void redo() {
+ mRedoDone = true;
+ mOldWaitFor = mReaction->waitFor();
+ mReaction->setWaitFor(mNewWaitFor);
+ }
+
+ virtual void undo() {
+ mRedoDone = false;
+ mReaction->setWaitFor(mOldWaitFor);
+ }
+
+};
+
+class SetReactionResponseType: public QUndoCommand {
+public:
+
+ Reaction* mReaction;
+ Reaction::ResponseType mOldResponseType;
+ Reaction::ResponseType mNewResponseType;
+
+ SetReactionResponseType(QUndoCommand* parent = 0): QUndoCommand(parent) {
+ setText(i18nc("@action", "Set reaction response type"));
+ }
+
+ virtual void redo() {
+ mOldResponseType = mReaction->responseType();
+ mReaction->setResponseType(mNewResponseType);
+ }
+
+ virtual void undo() {
+ mReaction->setResponseType(mOldResponseType);
+ }
+
+};
+
+class SetReactionNextStepId: public QUndoCommand {
+public:
+
+ Reaction* mReaction;
+ QString mOldNextStepId;
+ QString mNewNextStepId;
+
+ SetReactionNextStepId(QUndoCommand* parent = 0): QUndoCommand(parent) {
+ setText(i18nc("@action", "Set reaction next step id"));
+ }
+
+ virtual void redo() {
+ mOldNextStepId = mReaction->nextStepId();
+ mReaction->setNextStepId(mNewNextStepId);
+ }
+
+ virtual void undo() {
+ mReaction->setNextStepId(mOldNextStepId);
+ }
+
+};
+
+class SetReactionCustomCode: public QUndoCommand {
+public:
+
+ Reaction* mReaction;
+ QString mOldCustomCode;
+ QString mNewCustomCode;
+
+ SetReactionCustomCode(QUndoCommand* parent = 0): QUndoCommand(parent) {
+ setText(i18nc("@action", "Set reaction custom code"));
+ }
+
+ virtual void redo() {
+ mOldCustomCode = mReaction->customCode();
+ mReaction->setCustomCode(mNewCustomCode);
+ }
+
+ virtual void undo() {
+ mReaction->setCustomCode(mOldCustomCode);
+ }
+
+};
+
+//public:
+
+ReactionCommands::ReactionCommands(Reaction* reaction):
+ mReaction(reaction) {
+}
+
+QUndoCommand* ReactionCommands::setTriggerType(
+ Reaction::TriggerType triggerType,
+ QUndoCommand* parent) {
+ SetReactionTriggerType* command = new SetReactionTriggerType(parent);
+ command->mReaction = mReaction;
+ command->mNewTriggerType = triggerType;
+ return command;
+}
+
+QUndoCommand* ReactionCommands::setOptionName(const QString& optionName,
+ QUndoCommand* parent) {
+ SetReactionOptionName* command = new SetReactionOptionName(parent);
+ command->mReaction = mReaction;
+ command->mNewOptionName = optionName;
+ return command;
+}
+
+QUndoCommand* ReactionCommands::setWaitFor(WaitFor* waitFor,
+ QUndoCommand* parent) {
+ SetReactionWaitFor* command = new SetReactionWaitFor(parent);
+ command->mReaction = mReaction;
+ command->mNewWaitFor = waitFor;
+ return command;
+}
+
+QUndoCommand* ReactionCommands::setResponseType(
+ Reaction::ResponseType responseType,
+ QUndoCommand* parent) {
+ SetReactionResponseType* command = new SetReactionResponseType(parent);
+ command->mReaction = mReaction;
+ command->mNewResponseType = responseType;
+ return command;
+}
+
+QUndoCommand* ReactionCommands::setNextStepId(const QString& nextStepId,
+ QUndoCommand* parent) {
+ SetReactionNextStepId* command = new SetReactionNextStepId(parent);
+ command->mReaction = mReaction;
+ command->mNewNextStepId = nextStepId;
+ return command;
+}
+
+QUndoCommand* ReactionCommands::setCustomCode(const QString& customCode,
+ QUndoCommand* parent) {
+ SetReactionCustomCode* command = new SetReactionCustomCode(parent);
+ command->mReaction = mReaction;
+ command->mNewCustomCode = customCode;
+ return command;
+}
Property changes on: trunk/ktutorial/ktutorial-editor/src/commands/ReactionCommands.cpp
___________________________________________________________________
Added: svn:eol-style
+ native
Added: trunk/ktutorial/ktutorial-editor/src/commands/ReactionCommands.h
===================================================================
--- trunk/ktutorial/ktutorial-editor/src/commands/ReactionCommands.h (rev 0)
+++ trunk/ktutorial/ktutorial-editor/src/commands/ReactionCommands.h 2010-03-29 00:36:12 UTC (rev 205)
@@ -0,0 +1,118 @@
+/***************************************************************************
+ * Copyright (C) 2010 by Daniel Calviño Sánchez *
+ * dan...@gm... *
+ * *
+ * This program is free software; you can redistribute it and/or modify *
+ * it under the terms of the GNU General Public License as published by *
+ * the Free Software Foundation; either version 3 of the License, or *
+ * (at your option) any later version. *
+ * *
+ * This program is distributed in the hope that it will be useful, *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
+ * GNU General Public License for more details. *
+ * *
+ * You should have received a copy of the GNU General Public License *
+ * along with this program; If not, see <http://www.gnu.org/licenses/>. *
+ ***************************************************************************/
+
+#ifndef REACTIONCOMMANDS_H
+#define REACTIONCOMMANDS_H
+
+#include <QString>
+
+#include "../data/Reaction.h"
+
+class QUndoCommand;
+class WaitFor;
+
+/**
+ * Factory for reaction edition commands.
+ * This class provides methods to create undoable commands to edit a reaction.
+ * A different command is provided for each data modification method in the
+ * Reaction class.
+ *
+ * Note that each method just creates and returns the command. It does not
+ * execute it before returning.
+ *
+ * @see QUndoCommand
+ */
+class ReactionCommands {
+public:
+
+ /**
+ * Creates a new ReactionCommands for the given reaction.
+ *
+ * @param reaction The reaction to create the commands for.
+ */
+ explicit ReactionCommands(Reaction* reaction);
+
+ /**
+ * Creates a new undoable command to set the trigger type.
+ *
+ * @param triggerType The trigger type of the reaction to set.
+ * @param parent The parent QUndoCommand.
+ * @return The new QUndoCommand.
+ */
+ QUndoCommand* setTriggerType(Reaction::TriggerType triggerType,
+ QUndoCommand* parent = 0);
+
+ /**
+ * Creates a new undoable command to set the option name.
+ *
+ * @param optionName The option name of the reaction to set.
+ * @param parent The parent QUndoCommand.
+ * @return The new QUndoCommand.
+ */
+ QUndoCommand* setOptionName(const QString& optionName,
+ QUndoCommand* parent = 0);
+
+ /**
+ * Creates a new undoable command to set the condition to wait for.
+ *
+ * @param waitFor The condition to wait for of the reaction to set.
+ * @param parent The parent QUndoCommand.
+ * @return The new QUndoCommand.
+ */
+ QUndoCommand* setWaitFor(WaitFor* waitFor, QUndoCommand* parent = 0);
+
+ /**
+ * Creates a new undoable command to set the response type.
+ *
+ * @param responseType The response type of the reaction to set.
+ * @param parent The parent QUndoCommand.
+ * @return The new QUndoCommand.
+ */
+ QUndoCommand* setResponseType(Reaction::ResponseType responseType,
+ QUndoCommand* parent = 0);
+
+ /**
+ * Creates a new undoable command to set the next step id.
+ *
+ * @param nextStepId The next step id of the reaction to set.
+ * @param parent The parent QUndoCommand.
+ * @return The new QUndoCommand.
+ */
+ QUndoCommand* setNextStepId(const QString& nextStepId,
+ QUndoCommand* parent = 0);
+
+ /**
+ * Creates a new undoable command to set the custom response code.
+ *
+ * @param customCode The custom response code of the reaction to set.
+ * @param parent The parent QUndoCommand.
+ * @return The new QUndoCommand.
+ */
+ QUndoCommand* setCustomCode(const QString& customCode,
+ QUndoCommand* parent = 0);
+
+private:
+
+ /**
+ * The reaction to create the commands for.
+ */
+ Reaction* mReaction;
+
+};
+
+#endif
Property changes on: trunk/ktutorial/ktutorial-editor/src/commands/ReactionCommands.h
___________________________________________________________________
Added: svn:eol-style
+ native
Added: trunk/ktutorial/ktutorial-editor/src/commands/StepCommands.cpp
===================================================================
--- trunk/ktutorial/ktutorial-editor/src/commands/StepCommands.cpp (rev 0)
+++ trunk/ktutorial/ktutorial-editor/src/commands/StepCommands.cpp 2010-03-29 00:36:12 UTC (rev 205)
@@ -0,0 +1,227 @@
+/***************************************************************************
+ * Copyright (C) 2010 by Daniel Calviño Sánchez *
+ * dan...@gm... *
+ * *
+ * This program is free software; you can redistribute it and/or modify *
+ * it under the terms of the GNU General Public License as published by *
+ * the Free Software Foundation; either version 3 of the License, or *
+ * (at your option) any later version. *
+ * *
+ * This program is distributed in the hope that it will be useful, *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
+ * GNU General Public License for more details. *
+ * *
+ * You should have received a copy of the GNU General Public License *
+ * along with this program; If not, see <http://www.gnu.org/licenses/>. *
+ ***************************************************************************/
+
+#include "StepCommands.h"
+
+#include <QUndoCommand>
+
+#include <KLocalizedString>
+
+#include "../data/Reaction.h"
+#include "../data/Step.h"
+
+class SetStepId: public QUndoCommand {
+public:
+
+ Step* mStep;
+ QString mOldId;
+ QString mNewId;
+
+ SetStepId(QUndoCommand* parent = 0): QUndoCommand(parent) {
+ setText(i18nc("@action", "Set step id"));
+ }
+
+ virtual void redo() {
+ mOldId = mStep->id();
+ mStep->setId(mNewId);
+ }
+
+ virtual void undo() {
+ mStep->setId(mOldId);
+ }
+
+};
+
+class SetStepText: public QUndoCommand {
+public:
+
+ Step* mStep;
+ QString mOldText;
+ QString mNewText;
+
+ SetStepText(QUndoCommand* parent = 0): QUndoCommand(parent) {
+ setText(i18nc("@action", "Set step text"));
+ }
+
+ virtual void redo() {
+ mOldText = mStep->text();
+ mStep->setText(mNewText);
+ }
+
+ virtual void undo() {
+ mStep->setText(mOldText);
+ }
+
+};
+
+class SetStepCustomSetupCode: public QUndoCommand {
+public:
+
+ Step* mStep;
+ QString mOldCustomSetupCode;
+ QString mNewCustomSetupCode;
+
+ SetStepCustomSetupCode(QUndoCommand* parent = 0): QUndoCommand(parent) {
+ setText(i18nc("@action", "Set step setup code"));
+ }
+
+ virtual void redo() {
+ mOldCustomSetupCode = mStep->customSetupCode();
+ mStep->setCustomSetupCode(mNewCustomSetupCode);
+ }
+
+ virtual void undo() {
+ mStep->setCustomSetupCode(mOldCustomSetupCode);
+ }
+
+};
+
+class SetStepCustomTearDownCode: public QUndoCommand {
+public:
+
+ Step* mStep;
+ QString mOldCustomTearDownCode;
+ QString mNewCustomTearDownCode;
+
+ SetStepCustomTearDownCode(QUndoCommand* parent = 0): QUndoCommand(parent) {
+ setText(i18nc("@action", "Set step tear down code"));
+ }
+
+ virtual void redo() {
+ mOldCustomTearDownCode = mStep->customTearDownCode();
+ mStep->setCustomTearDownCode(mNewCustomTearDownCode);
+ }
+
+ virtual void undo() {
+ mStep->setCustomTearDownCode(mOldCustomTearDownCode);
+ }
+
+};
+
+class BaseReactionCommand: public QUndoCommand {
+public:
+
+ Step* mStep;
+ Reaction* mReaction;
+ bool mDeleteReactionInDestructor;
+
+ BaseReactionCommand(QUndoCommand* parent = 0): QUndoCommand(parent),
+ mStep(0),
+ mReaction(0),
+ mDeleteReactionInDestructor(false) {
+ }
+
+ virtual ~BaseReactionCommand() {
+ if (mDeleteReactionInDestructor) {
+ delete mReaction;
+ }
+ }
+
+};
+
+class AddReaction: public BaseReactionCommand {
+public:
+
+ AddReaction(QUndoCommand* parent = 0): BaseReactionCommand(parent) {
+ setText(i18nc("@action", "Add reaction"));
+ mDeleteReactionInDestructor = true;
+ }
+
+ virtual void redo() {
+ mDeleteReactionInDestructor = false;
+ mStep->addReaction(mReaction);
+ }
+
+ virtual void undo() {
+ mDeleteReactionInDestructor = true;
+ mStep->removeReaction(mReaction);
+ }
+
+};
+
+class RemoveReaction: public BaseReactionCommand {
+public:
+
+ RemoveReaction(QUndoCommand* parent = 0): BaseReactionCommand(parent) {
+ setText(i18nc("@action", "Remove reaction"));
+ mDeleteReactionInDestructor = false;
+ }
+
+ virtual void redo() {
+ mDeleteReactionInDestructor = true;
+ mStep->removeReaction(mReaction);
+ }
+
+ virtual void undo() {
+ mDeleteReactionInDestructor = false;
+ mStep->addReaction(mReaction);
+ }
+
+};
+
+//public:
+
+StepCommands::StepCommands(Step* step):
+ mStep(step) {
+}
+
+QUndoCommand* StepCommands::setId(const QString& id, QUndoCommand* parent) {
+ SetStepId* command = new SetStepId(parent);
+ command->mStep = mStep;
+ command->mNewId = id;
+ return command;
+}
+
+QUndoCommand* StepCommands::setText(const QString& text, QUndoCommand* parent) {
+ SetStepText* command = new SetStepText(parent);
+ command->mStep = mStep;
+ command->mNewText = text;
+ return command;
+}
+
+QUndoCommand* StepCommands::setCustomSetupCode(const QString& code,
+ QUndoCommand* parent) {
+ SetStepCustomSetupCode* command = new SetStepCustomSetupCode(parent);
+ command->mStep = mStep;
+ command->mNewCustomSetupCode = code;
+ return command;
+}
+
+QUndoCommand* StepCommands::setCustomTearDownCode(const QString& code,
+ QUndoCommand* parent) {
+ SetStepCustomTearDownCode* command = new SetStepCustomTearDownCode(parent);
+ command->mStep = mStep;
+ command->mNewCustomTearDownCode = code;
+ return command;
+}
+
+QUndoCommand* StepCommands::addReaction(Reaction* reaction,
+ QUndoCommand* parent) {
+ AddReaction* command = new AddReaction(parent);
+ command->mStep = mStep;
+ command->mReaction = reaction;
+ return command;
+}
+
+QUndoCommand* StepCommands::removeReaction(Reaction* reaction,
+ QUndoCommand* parent) {
+ RemoveReaction* command = new RemoveReaction(parent);
+ command->mStep = mStep;
+ command->mReaction = reaction;
+ return command;
+}
Property changes on: trunk/ktutorial/ktutorial-editor/src/commands/StepCommands.cpp
___________________________________________________________________
Added: svn:eol-style
+ native
Added: trunk/ktutorial/ktutorial-editor/src/commands/StepCommands.h
===================================================================
--- trunk/ktutorial/ktutorial-editor/src/commands/StepCommands.h (rev 0)
+++ trunk/ktutorial/ktutorial-editor/src/commands/StepCommands.h 2010-03-29 00:36:12 UTC (rev 205)
@@ -0,0 +1,119 @@
+/***************************************************************************
+ * Copyright (C) 2010 by Daniel Calviño Sánchez *
+ * dan...@gm... *
+ * *
+ * This program is free software; you can redistribute it and/or modify *
+ * it under the terms of the GNU General Public License as published by *
+ * the Free Software Foundation; either version 3 of the License, or *
+ * (at your option) any later version. *
+ * *
+ * This program is distributed in the hope that it will be useful, *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
+ * GNU General Public License for more details. *
+ * *
+ * You should have received a copy of the GNU General Public License *
+ * along with this program; If not, see <http://www.gnu.org/licenses/>. *
+ ***************************************************************************/
+
+#ifndef STEPCOMMANDS_H
+#define STEPCOMMANDS_H
+
+#include <QString>
+
+class QUndoCommand;
+class Reaction;
+class Step;
+
+/**
+ * Factory for step edition commands.
+ * This class provides methods to create undoable commands to edit a step.
+ * A different command is provided for each data modification method in the Step
+ * class.
+ *
+ * Note that each method just creates and returns the command. It does not
+ * execute it before returning.
+ *
+ * @see QUndoCommand
+ */
+class StepCommands {
+public:
+
+ /**
+ * Creates a new StepCommands for the given step.
+ *
+ * @param step The step to create the commands for.
+ */
+ explicit StepCommands(Step* step);
+
+ /**
+ * Creates a new undoable command to set the step id.
+ *
+ * @param id The id of the step to set.
+ * @param parent The parent QUndoCommand.
+ * @return The new QUndoCommand.
+ */
+ QUndoCommand* setId(const QString& id, QUndoCommand* parent = 0);
+
+ /**
+ * Creates a new undoable command to set the step text.
+ *
+ * @param text The text of the step to set.
+ * @param parent The parent QUndoCommand.
+ * @return The new QUndoCommand.
+ */
+ QUndoCommand* setText(const QString& text, QUndoCommand* parent = 0);
+
+ /**
+ * Creates a new undoable command to set the step custom setup code.
+ *
+ * @param code The custom setup code of the step to set.
+ * @param parent The parent QUndoCommand.
+ * @return The new QUndoCommand.
+ */
+ QUndoCommand* setCustomSetupCode(const QString& code,
+ QUndoCommand* parent = 0);
+
+ /**
+ * Creates a new undoable command to set the step custom tear down code.
+ *
+ * @param code The custom tear down code of the step to set.
+ * @param parent The parent QUndoCommand.
+ * @return The new QUndoCommand.
+ */
+ QUndoCommand* setCustomTearDownCode(const QString& code,
+ QUndoCommand* parent = 0);
+
+ /**
+ * Creates a new undoable command to add the given reaction to the step.
+ * If the command was not executed yet, or it was undone, the reaction is
+ * deleted when the command is deleted.
+ *
+ * @param reaction The reaction to add to the step.
+ * @param parent The parent QUndoCommand.
+ * @return The new QUndoCommand.
+ */
+ QUndoCommand* addReaction(Reaction* reaction, QUndoCommand* parent = 0);
+
+ /**
+ * Creates a new undoable command to remove the given reaction from the
+ * step.
+ * If the command was executed and not undone, the reaction is deleted when
+ * the command is deleted.
+ *
+ * @param reaction The reaction to remove from the step.
+ * @param parent The parent QUndoCommand.
+ * @return The new QUndoCommand.
+ */
+ QUndoCommand* removeReaction(Reaction* reaction, QUndoCommand* parent = 0);
+
+private:
+
+ /**
+ * The step to create the commands for.
+ */
+ Step* mStep;
+
+};
+
+#endif
Property changes on: trunk/ktutorial/ktutorial-editor/src/commands/StepCommands.h
___________________________________________________________________
Added: svn:eol-style
+ native
Added: trunk/ktutorial/ktutorial-editor/src/commands/TutorialCommands.cpp
===================================================================
--- trunk/ktutorial/ktutorial-editor/src/commands/TutorialCommands.cpp (rev 0)
+++ trunk/ktutorial/ktutorial-editor/src/commands/TutorialCommands.cpp 2010-03-29 00:36:12 UTC (rev 205)
@@ -0,0 +1,260 @@
+/***************************************************************************
+ * Copyright (C) 2010 by Daniel Calviño Sánchez *
+ * dan...@gm... *
+ * *
+ * This program is free software; you can redistribute it and/or modify *
+ * it under the terms of the GNU General Public License as published by *
+ * the Free Software Foundation; either version 3 of the License, or *
+ * (at your option) any later version. *
+ * *
+ * This program is distributed in the hope that it will be useful, *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
+ * GNU General Public License for more details. *
+ * *
+ * You should have received a copy of the GNU General Public License *
+ * along with this program; If not, see <http://www.gnu.org/licenses/>. *
+ ***************************************************************************/
+
+#include "TutorialCommands.h"
+
+#include <QUndoCommand>
+
+#include <KLocalizedString>
+
+#include "../data/Step.h"
+#include "../data/Tutorial.h"
+
+class SetTutorialName: public QUndoCommand {
+public:
+
+ Tutorial* mTutorial;
+ QString mOldName;
+ QString mNewName;
+
+ SetTutorialName(QUndoCommand* parent = 0): QUndoCommand(parent) {
+ setText(i18nc("@action", "Set tutorial name"));
+ }
+
+ virtual void redo() {
+ mOldName = mTutorial->name();
+ mTutorial->setName(mNewName);
+ }
+
+ virtual void undo() {
+ mTutorial->setName(mOldName);
+ }
+
+};
+
+class SetTutorialDescription: public QUndoCommand {
+public:
+
+ Tutorial* mTutorial;
+ QString mOldDescription;
+ QString mNewDescription;
+
+ SetTutorialDescription(QUndoCommand* parent = 0): QUndoCommand(parent) {
+ setText(i18nc("@action", "Set tutorial description"));
+ }
+
+ virtual void redo() {
+ mOldDescription = mTutorial->description();
+ mTutorial->setDescription(mNewDescription);
+ }
+
+ virtual void undo() {
+ mTutorial->setDescription(mOldDescription);
+ }
+
+};
+
+class SetTutorialLicenseText: public QUndoCommand {
+public:
+
+ Tutorial* mTutorial;
+ QString mOldLicenseText;
+ QString mNewLicenseText;
+
+ SetTutorialLicenseText(QUndoCommand* parent = 0): QUndoCommand(parent) {
+ setText(i18nc("@action", "Set tutorial license"));
+ }
+
+ virtual void redo() {
+ mOldLicenseText = mTutorial->licenseText();
+ mTutorial->setLicenseText(mNewLicenseText);
+ }
+
+ virtual void undo() {
+ mTutorial->setLicenseText(mOldLicenseText);
+ }
+
+};
+
+class SetTutorialCustomSetupCode: public QUndoCommand {
+public:
+
+ Tutorial* mTutorial;
+ QString mOldCustomSetupCode;
+ QString mNewCustomSetupCode;
+
+ SetTutorialCustomSetupCode(QUndoCommand* parent = 0): QUndoCommand(parent) {
+ setText(i18nc("@action", "Set tutorial setup code"));
+ }
+
+ virtual void redo() {
+ mOldCustomSetupCode = mTutorial->customSetupCode();
+ mTutorial->setCustomSetupCode(mNewCustomSetupCode);
+ }
+
+ virtual void undo() {
+ mTutorial->setCustomSetupCode(mOldCustomSetupCode);
+ }
+
+};
+
+class SetTutorialCustomTearDownCode: public QUndoCommand {
+public:
+
+ Tutorial* mTutorial;
+ QString mOldCustomTearDownCode;
+ QString mNewCustomTearDownCode;
+
+ SetTutorialCustomTearDownCode(QUndoCommand* parent = 0):
+ QUndoCommand(parent) {
+ setText(i18nc("@action", "Set tutorial tear down code"));
+ }
+
+ virtual void redo() {
+ mOldCustomTearDownCode = mTutorial->customTearDownCode();
+ mTutorial->setCustomTearDownCode(mNewCustomTearDownCode);
+ }
+
+ virtual void undo() {
+ mTutorial->setCustomTearDownCode(mOldCustomTearDownCode);
+ }
+
+};
+
+class BaseStepCommand: public QUndoCommand {
+public:
+
+ Tutorial* mTutorial;
+ Step* mStep;
+ bool mDeleteStepInDestructor;
+
+ BaseStepCommand(QUndoCommand* parent = 0): QUndoCommand(parent),
+ mTutorial(0),
+ mStep(0),
+ mDeleteStepInDestructor(false) {
+ }
+
+ virtual ~BaseStepCommand() {
+ if (mDeleteStepInDestructor) {
+ delete mStep;
+ }
+ }
+
+};
+
+class AddStep: public BaseStepCommand {
+public:
+
+ AddStep(QUndoCommand* parent = 0): BaseStepCommand(parent) {
+ setText(i18nc("@action", "Add step"));
+ mDeleteStepInDestructor = true;
+ }
+
+ virtual void redo() {
+ mDeleteStepInDestructor = false;
+ mTutorial->addStep(mStep);
+ }
+
+ virtual void undo() {
+ mDeleteStepInDestructor = true;
+ mTutorial->removeStep(mStep);
+ }
+
+};
+
+class RemoveStep: public BaseStepCommand {
+public:
+
+ RemoveStep(QUndoCommand* parent = 0): BaseStepCommand(parent) {
+ setText(i18nc("@action", "Remove step"));
+ mDeleteStepInDestructor = false;
+ }
+
+ virtual void redo() {
+ mDeleteStepInDestructor = true;
+ mTutorial->removeStep(mStep);
+ }
+
+ virtual void undo() {
+ mDeleteStepInDestructor = false;
+ mTutorial->addStep(mStep);
+ }
+
+};
+
+//public:
+
+TutorialCommands::TutorialCommands(Tutorial* tutorial):
+ mTutorial(tutorial) {
+}
+
+QUndoCommand* TutorialCommands::setName(const QString& name,
+ QUndoCommand* parent) {
+ SetTutorialName* command = new SetTutorialName(parent);
+ command->mTutorial = mTutorial;
+ command->mNewName = name;
+ return command;
+}
+
+QUndoCommand* TutorialCommands::setDescription(const QString& description,
+ QUndoCommand* parent) {
+ SetTutorialDescription* command = new SetTutorialDescription(parent);
+ command->mTutorial = mTutorial;
+ command->mNewDescription = description;
+ return command;
+}
+
+QUndoCommand* TutorialCommands::setLicenseText(const QString& licenseText,
+ QUndoCommand* parent) {
+ SetTutorialLicenseText* command = new SetTutorialLicenseText(parent);
+ command->mTutorial = mTutorial;
+ command->mNewLicenseText = licenseText;
+ return command;
+}
+
+QUndoCommand* TutorialCommands::setCustomSetupCode(const QString& code,
+ QUndoCommand* parent) {
+ SetTutorialCustomSetupCode* command =
+ new SetTutorialCustomSetupCode(parent);
+ command->mTutorial = mTutorial;
+ command->mNewCustomSetupCode = code;
+ return command;
+}
+
+QUndoCommand* TutorialCommands::setCustomTearDownCode(const QString& code,
+ QUndoCommand* parent) {
+ SetTutorialCustomTearDownCode* command =
+ new SetTutorialCustomTearDownCode(parent);
+ command->mTutorial = mTutorial;
+ command->mNewCustomTearDownCode = code;
+ return command;
+}
+
+QUndoCommand* TutorialCommands::addStep(Step* step, QUndoCommand* parent) {
+ AddStep* command = new AddStep(parent);
+ command->mTutorial = mTutorial;
+ command->mStep = step;
+ return command;
+}
+
+QUndoCommand* TutorialCommands::removeStep(Step* step, QUndoCommand* parent) {
+ RemoveStep* command = new RemoveStep(parent);
+ command->mTutorial = mTutorial;
+ command->mStep = step;
+ return command;
+}
Property changes on: trunk/ktutorial/ktutorial-editor/src/commands/TutorialCommands.cpp
___________________________________________________________________
Added: svn:eol-style
+ native
Added: trunk/ktutorial/ktutorial-editor/src/commands/TutorialCommands.h
===================================================================
--- trunk/ktutorial/ktutorial-editor/src/commands/TutorialCommands.h (rev 0)
+++ trunk/ktutorial/ktutorial-editor/src/commands/TutorialCommands.h 2010-03-29 00:36:12 UTC (rev 205)
@@ -0,0 +1,130 @@
+/***************************************************************************
+ * Copyright (C) 2010 by Daniel Calviño Sánchez *
+ * dan...@gm... *
+ * *
+ * This program is free software; you can redistribute it and/or modify *
+ * it under the terms of the GNU General Public License as published by *
+ * the Free Software Foundation; either version 3 of the License, or *
+ * (at your option) any later version. *
+ * *
+ * This program is distributed in the hope that it will be useful, *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
+ * GNU General Public License for more details. *
+ * *
+ * You should have received a copy of the GNU General Public License *
+ * along with this program; If not, see <http://www.gnu.org/licenses/>. *
+ ***************************************************************************/
+
+#ifndef TUTORIALCOMMANDS_H
+#define TUTORIALCOMMANDS_H
+
+#include <QString>
+
+class QUndoCommand;
+class Step;
+class Tutorial;
+
+/**
+ * Factory for tutorial edition commands.
+ * This class provides methods to create undoable commands to edit a tutorial.
+ * A different command is provided for each data modification method in the
+ * Tutorial class.
+ *
+ * Note that each method just creates and returns the command. It does not
+ * execute it before returning.
+ *
+ * @see QUndoCommand
+ */
+class TutorialCommands {
+public:
+
+ /**
+ * Creates a new TutorialCommands for the given tutorial.
+ *
+ * @param tutorial The tutorial to create the commands for.
+ */
+ explicit TutorialCommands(Tutorial* tutorial);
+
+ /**
+ * Creates a new undoable command to set the tutorial name.
+ *
+ * @param name The name of the tutorial to set.
+ * @param parent The parent QUndoCommand.
+ * @return The new QUndoCommand.
+ */
+ QUndoCommand* setName(const QString& name, QUndoCommand* parent = 0);
+
+ /**
+ * Creates a new undoable command to set the tutorial description.
+ *
+ * @param description The description of the tutorial to set.
+ * @param parent The parent QUndoCommand.
+ * @return The new QUndoCommand.
+ */
+ QUndoCommand* setDescription(const QString& description,
+ QUndoCommand* parent = 0);
+
+ /**
+ * Creates a new undoable command to set the tutorial license text.
+ *
+ * @param licenseText The license text of the tutorial to set.
+ * @param parent The parent QUndoCommand.
+ * @return The new QUndoCommand.
+ */
+ QUndoCommand* setLicenseText(const QString& licenseText,
+ QUndoCommand* parent = 0);
+
+ /**
+ * Creates a new undoable command to set the tutorial custom setup code.
+ *
+ * @param code The custom setup code of the tutorial to set.
+ * @param parent The parent QUndoCommand.
+ * @return The new QUndoCommand.
+ */
+ QUndoCommand* setCustomSetupCode(const QString& code,
+ QUndoCommand* parent = 0);
+
+ /**
+ * Creates a new undoable command to set the tutorial custom tear down code.
+ *
+ * @param code The custom tear down code of the tutorial to set.
+ * @param parent The parent QUndoCommand.
+ * @return The new QUndoCommand.
+ */
+ QUndoCommand* setCustomTearDownCode(const QString& code,
+ QUndoCommand* parent = 0);
+
+ /**
+ * Creates a new undoable command to add the given step to the tutorial.
+ * If the command was not executed yet, or it was undone, the step is
+ * deleted when the command is deleted.
+ *
+ * @param step The step to add to the tutorial.
+ * @param parent The parent QUndoCommand.
+ * @return The new QUndoCommand.
+ */
+ QUndoCommand* addStep(Step* step, QUndoCommand* parent = 0);
+
+ /**
+ * Creates a new undoable command to remove the given step from the
+ * tutorial.
+ * If the command was executed and not undone, the step is deleted when the
+ * command is deleted.
+ *
+ * @param step The step to remove from the tutorial.
+ * @param parent The parent QUndoCommand.
+ * @return The new QUndoCommand.
+ */
+ QUndoCommand* removeStep(Step* step, QUndoCommand* parent = 0);
+
+private:
+
+ /**
+ * The tutorial to create the commands for.
+ */
+ Tutorial* mTutorial;
+
+};
+
+#endif
Property changes on: trunk/ktutorial/ktutorial-editor/src/commands/TutorialCommands.h
___________________________________________________________________
Added: svn:eol-style
+ native
Modified: trunk/ktutorial/ktutorial-editor/src/view/CMakeLists.txt
===================================================================
--- trunk/ktutorial/ktutorial-editor/src/view/CMakeLists.txt 2010-03-28 07:46:36 UTC (rev 204)
+++ trunk/ktutorial/ktutorial-editor/src/view/CMakeLists.txt 2010-03-29 00:36:12 UTC (rev 205)
@@ -1,5 +1,6 @@
set(ktutorial_editor_view_SRCS
ActionListWidget.cpp
+ CommandWidget.cpp
EditionDialog.cpp
EditionWidget.cpp
LicenseWidget.cpp
@@ -41,4 +42,8 @@
kde4_add_library(ktutorial_editor_view ${ktutorial_editor_view_SRCS})
-target_link_libraries(ktutorial_editor_view ktutorial_editor_data ${KDE4_KDEUI_LIBS})
+target_link_libraries(ktutorial_editor_view
+ ktutorial_editor_commands
+ ktutorial_editor_data
+ ${KDE4_KDEUI_LIBS}
+)
Added: trunk/ktutorial/ktutorial-editor/src/view/CommandWidget.cpp
===================================================================
--- trunk/ktutorial/ktutorial-editor/src/view/CommandWidget.cpp (rev 0)
+++ trunk/ktutorial/ktutorial-editor/src/view/CommandWidget.cpp 2010-03-29 00:36:12 UTC (rev 205)
@@ -0,0 +1,68 @@
+/***************************************************************************
+ * Copyright (C) 2010 by Daniel Calviño Sánchez *
+ * dan...@gm... *
+ * *
+ * This program is free software; you can redistribute it and/or modify *
+ * it under the terms of the GNU General Public License as published by *
+ * the Free Software Foundation; either version 3 of the License, or *
+ * (at your option) any later version. *
+ * *
+ * This program is distributed in the hope that it will be useful, *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
+ * GNU General Public License for more details. *
+ * *
+ * You should have received a copy of the GNU General Public License *
+ * along with this program; If not, see <http://www.gnu.org/licenses/>. *
+ ***************************************************************************/
+
+#include "CommandWidget.h"
+
+#include <QUndoStack>
+
+//public:
+
+CommandWidget::CommandWidget(QWidget* parent): EditionWidget(parent),
+ mParentUndoCommand(0),
+ mUndoStack(0) {
+}
+
+void CommandWidget::saveChanges() {
+ QUndoCommand* parent = mParentUndoCommand;
+ if (!parent) {
+ parent = new QUndoCommand();
+ }
+
+ QList<QUndoCommand*> commands = createSaveCommands(parent);
+
+ if (!mParentUndoCommand && commands.count() > 1) {
+ parent->setText(windowTitle());
+ } else if (!mParentUndoCommand && commands.count() == 1) {
+ parent->setText(parent->child(0)->text());
+ }
+
+ if (mUndoStack && (commands.count() > 0 || mParentUndoCommand)){
+ mUndoStack->push(parent);
+ return;
+ }
+
+ if (mUndoStack) {
+ delete parent;
+ return;
+ }
+
+ if (mParentUndoCommand) {
+ return;
+ }
+
+ parent->redo();
+ delete parent;
+}
+
+void CommandWidget::setParentUndoCommand(QUndoCommand* parentUndoCommand) {
+ mParentUndoCommand = parentUndoCommand;
+}
+
+void CommandWidget::setUndoStack(QUndoStack* undoStack) {
+ mUndoStack = undoStack;
+}
Property changes on: trunk/ktutorial/ktutorial-editor/src/view/CommandWidget.cpp
___________________________________________________________________
Added: svn:eol-style
+ native
Added: trunk/ktutorial/ktutorial-editor/src/view/CommandWidget.h
===================================================================
--- trunk/ktutorial/ktutorial-editor/src/view/CommandWidget.h (rev 0)
+++ trunk/ktutorial/ktutorial-editor/src/view/CommandWidget.h 2010-03-29 00:36:12 UTC (rev 205)
@@ -0,0 +1,122 @@
+/***************************************************************************
+ * Copyright (C) 2010 by Daniel Calviño Sánchez *
+ * dan...@gm... *
+ * *
+ * This program is free software; you can redistribute it and/or modify *
+ * it under the terms of the GNU General Public License as published by *
+ * the Free Software Foundation; either version 3 of the License, or *
+ * (at your option) any later version. *
+ * *
+ * This program is distributed in the hope that it will be useful, *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
+ * GNU General Public License for more details. *
+ * *
+ * You should have received a copy of the GNU General Public License *
+ * along with this program; If not, see <http://www.gnu.org/licenses/>. *
+ ***************************************************************************/
+
+#ifndef COMMANDWIDGET_H
+#define COMMANDWIDGET_H
+
+#include "EditionWidget.h"
+
+class QUndoCommand;
+class QUndoStack;
+
+/**
+ * Base abstract class for command widgets.
+ * Command widgets are edition widgets that use undoable commands to save the
+ * data, for example, to set the name of a tutorial.
+ *
+ * To be done or undone by the user, the command must be added to a stack of
+ * commands. The stack that the commands will be added to can be set using
+ * setUndoStack(QUndoStack*) method. When the stack is set, the commands that
+ * are executed to save the data are added to the stack, which effectively
+ * causes them to be executed.
+ *
+ * However, the CommandWidget does not add the commands directly to the stack.
+ * Each command can have a parent command. When this happens, the child commands
+ * are done when their parent is done, and are undone when their parent is
+ * undone. It makes easier to compose several small commands to be executed as
+ * one bigger command.
+ *
+ * All the commands created in the widget have the same parent command, which is
+ * the one added directly to the stack. If no parent command was set, a new one
+ * is created automatically. If it has several children, its name will be the
+ * window title of the widget. If it has only one child, its name will be the
+ * name of its child.
+ *
+ * Note that if there is a stack, but no parent command and no child commands
+ * (for example, because there were no changes to save), the automatically
+ * created parent command is not added to the stack. However, if a parent
+ * command was explicitly set it is added to the stack even if no children were
+ * added to it.
+ *
+ * If no stack is set but there is a parent command, the commands will be added
+ * to the parent without executing them.
+ *
+ * If no stack and no parent are set, the commands will be executed when the
+ * changes are saved, but they will be deleted afterwards.
+ *
+ * Subclasses must implement createSaveCommands(QUndoCommand*) method. It has
+ * to return the list of the commands to execute to save the changes.
+ *
+ * @see QUndoCommand
+ */
+class CommandWidget: public EditionWidget {
+Q_OBJECT
+public:
+
+ /**
+ * Creates a new CommandWidget with the given parent widget.
+ *
+ * @param parent The parent widget.
+ */
+ CommandWidget(QWidget* parent = 0);
+
+ /**
+ * Saves the changes to the object being edited.
+ */
+ virtual void saveChanges();
+
+ /**
+ * Sets the parent command.
+ *
+ * @param parentUndoCommand The parent command.
+ */
+ void setParentUndoCommand(QUndoCommand* parentUndoCommand);
+
+ /**
+ * Sets the undo stack to add the commands to.
+ *
+ * @param undoStack The undo stack.
+ */
+ void setUndoStack(QUndoStack* undoStack);
+
+protected:
+
+ /**
+ * Creates the commands needed to save the changes.
+ * Subclasses must implement this method.
+ *
+ * @param parent The parent of all the created commands.
+ * @return A list with the commands.
+ */
+ virtual QList<QUndoCommand*> createSaveCommands(QUndoCommand* parent) = 0;
+
+private:
+
+ /**
+ * The explicitly set parent command.
+ */
+ QUndoCommand* mParentUndoCommand;
+
+ /**
+ * The undo stack to add the commands to.
+ */
+ QUndoStack* mUndoStack;
+
+};
+
+#endif
Property changes on: trunk/ktutorial/ktutorial-editor/src/view/CommandWidget.h
___________________________________________________________________
Added: svn:eol-style
+ native
Modified: trunk/ktutorial/ktutorial-editor/src/view/LicenseWidget.cpp
===================================================================
--- trunk/ktutorial/ktutorial-editor/src/view/LicenseWidget.cpp 2010-03-28 07:46:36 UTC (rev 204)
+++ trunk/ktutorial/ktutorial-editor/src/view/LicenseWidget.cpp 2010-03-29 00:36:12 UTC (rev 205)
@@ -19,12 +19,13 @@
#include "LicenseWidget.h"
#include "ui_LicenseWidget.h"
+#include "../commands/TutorialCommands.h"
#include "../data/Tutorial.h"
//public:
LicenseWidget::LicenseWidget(Tutorial* tutorial, QWidget* parent):
- EditionWidget(parent),
+ CommandWidget(parent),
mTutorial(tutorial) {
ui = new Ui::LicenseWidget();
ui->setupUi(this);
@@ -36,9 +37,16 @@
delete ui;
}
-void LicenseWidget::saveChanges() {
+//protected:
+
+QList<QUndoCommand*> LicenseWidget::createSaveCommands(QUndoCommand* parent) {
+ QList<QUndoCommand*> commands;
+ TutorialCommands tutorialCommands(mTutorial);
+
QString licenseText = ui->licenseTextEdit->toPlainText();
if (mTutorial->licenseText() != licenseText) {
- mTutorial->setLicenseText(licenseText);
+ commands.append(tutorialCommands.setLicenseText(licenseText, parent));
}
+
+ return commands;
}
Modified: trunk/ktutorial/ktutorial-editor/src/view/LicenseWidget.h
===================================================================
--- trunk/ktutorial/ktutorial-editor/src/view/LicenseWidget.h 2010-03-28 07:46:36 UTC (rev 204)
+++ trunk/ktutorial/ktutorial-editor/src/view/LicenseWidget.h 2010-03-29 00:36:12 UTC (rev 205)
@@ -19,7 +19,7 @@
#ifndef LICENSEWIDGET_H
#define LICENSEWIDGET_H
-#include "EditionWidget.h"
+#include "CommandWidget.h"
class Tutorial;
@@ -28,9 +28,9 @@
}
/**
- * Edition widget for the license of a tutorial.
+ * Command widget for the license of a tutorial.
*/
-class LicenseWidget: public EditionWidget {
+class LicenseWidget: public CommandWidget {
Q_OBJECT
public:
@@ -47,10 +47,15 @@
*/
virtual ~LicenseWidget();
+protected:
+
/**
- * Saves the license in the tutorial.
+ * Commands to save the license of the tutorial.
+ *
+ * @param parent The parent command.
+ * @return A list with the commands.
*/
- virtual void saveChanges();
+ virtual QList<QUndoCommand*> createSaveCommands(QUndoCommand* parent);
private:
Modified: trunk/ktutorial/ktutorial-editor/src/view/ReactionWidget.cpp
===================================================================
--- trunk/ktutorial/ktutorial-editor/src/view/ReactionWidget.cpp 2010-03-28 07:46:36 UTC (rev 204)
+++ trunk/ktutorial/ktutorial-editor/src/view/ReactionWidget.cpp 2010-03-29 00:36:12 UTC (rev 205)
@@ -20,13 +20,14 @@
#include "ui_ReactionWidget.h"
#include "WaitForWidget.h"
+#include "../commands/ReactionCommands.h"
#include "../data/Reaction.h"
#include "../data/WaitFor.h"
//public:
ReactionWidget::ReactionWidget(Reaction* reaction, QWidget* parent):
- EditionWidget(parent),
+ CommandWidget(parent),
mReaction(reaction),
mWaitForClone(0) {
@@ -72,51 +73,64 @@
delete ui;
}
-void ReactionWidget::saveChanges() {
- if (mReaction->optionName() != ui->triggerOptionLineEdit->text()) {
- mReaction->setOptionName(ui->triggerOptionLineEdit->text());
+//protected:
+
+QList<QUndoCommand*> ReactionWidget::createSaveCommands(QUndoCommand...
[truncated message content] |