[Ktutorial-commits] SF.net SVN: ktutorial:[208] trunk/ktutorial/ktutorial-editor
Status: Alpha
Brought to you by:
danxuliu
From: <dan...@us...> - 2010-03-29 05:56:14
|
Revision: 208 http://ktutorial.svn.sourceforge.net/ktutorial/?rev=208&view=rev Author: danxuliu Date: 2010-03-29 05:56:07 +0000 (Mon, 29 Mar 2010) Log Message: ----------- Fix removing an step from the middle and undoing the operation (the step was readded at the end). It required being able to add steps at any position in the tutorial, and not only at the end. Modified Paths: -------------- trunk/ktutorial/ktutorial-editor/src/commands/TutorialCommands.cpp trunk/ktutorial/ktutorial-editor/src/commands/TutorialCommands.h trunk/ktutorial/ktutorial-editor/src/data/Tutorial.cpp trunk/ktutorial/ktutorial-editor/src/data/Tutorial.h trunk/ktutorial/ktutorial-editor/src/view/TutorialTreeItem.cpp trunk/ktutorial/ktutorial-editor/src/view/TutorialTreeItem.h trunk/ktutorial/ktutorial-editor/tests/unit/commands/TutorialCommandsTest.cpp trunk/ktutorial/ktutorial-editor/tests/unit/data/TutorialTest.cpp trunk/ktutorial/ktutorial-editor/tests/unit/view/TutorialTreeItemTest.cpp Modified: trunk/ktutorial/ktutorial-editor/src/commands/TutorialCommands.cpp =================================================================== --- trunk/ktutorial/ktutorial-editor/src/commands/TutorialCommands.cpp 2010-03-29 03:59:41 UTC (rev 207) +++ trunk/ktutorial/ktutorial-editor/src/commands/TutorialCommands.cpp 2010-03-29 05:56:07 UTC (rev 208) @@ -165,21 +165,46 @@ mDeleteStepInDestructor = true; } + virtual void undo() { + mDeleteStepInDestructor = true; + mTutorial->removeStep(mStep); + } + +}; + +class AddStepAtEnd: public AddStep { +public: + + AddStepAtEnd(QUndoCommand* parent = 0): AddStep(parent) { + } + virtual void redo() { mDeleteStepInDestructor = false; mTutorial->addStep(mStep); } - virtual void undo() { - mDeleteStepInDestructor = true; - mTutorial->removeStep(mStep); +}; + +class AddStepAtIndex: public AddStep { +public: + + int mIndex; + + AddStepAtIndex(QUndoCommand* parent = 0): AddStep(parent) { } + virtual void redo() { + mDeleteStepInDestructor = false; + mTutorial->addStep(mStep, mIndex); + } + }; class RemoveStep: public BaseStepCommand { public: + int mIndex; + RemoveStep(QUndoCommand* parent = 0): BaseStepCommand(parent) { setText(i18nc("@action", "Remove step")); mDeleteStepInDestructor = false; @@ -187,12 +212,13 @@ virtual void redo() { mDeleteStepInDestructor = true; + mIndex = mTutorial->steps().indexOf(mStep); mTutorial->removeStep(mStep); } virtual void undo() { mDeleteStepInDestructor = false; - mTutorial->addStep(mStep); + mTutorial->addStep(mStep, mIndex); } }; @@ -246,12 +272,21 @@ } QUndoCommand* TutorialCommands::addStep(Step* step, QUndoCommand* parent) { - AddStep* command = new AddStep(parent); + AddStepAtEnd* command = new AddStepAtEnd(parent); command->mTutorial = mTutorial; command->mStep = step; return command; } +QUndoCommand* TutorialCommands::addStep(Step* step, int index, + QUndoCommand* parent) { + AddStepAtIndex* command = new AddStepAtIndex(parent); + command->mTutorial = mTutorial; + command->mStep = step; + command->mIndex = index; + return command; +} + QUndoCommand* TutorialCommands::removeStep(Step* step, QUndoCommand* parent) { RemoveStep* command = new RemoveStep(parent); command->mTutorial = mTutorial; Modified: trunk/ktutorial/ktutorial-editor/src/commands/TutorialCommands.h =================================================================== --- trunk/ktutorial/ktutorial-editor/src/commands/TutorialCommands.h 2010-03-29 03:59:41 UTC (rev 207) +++ trunk/ktutorial/ktutorial-editor/src/commands/TutorialCommands.h 2010-03-29 05:56:07 UTC (rev 208) @@ -107,6 +107,19 @@ QUndoCommand* addStep(Step* step, QUndoCommand* parent = 0); /** + * Creates a new undoable command to add the given step to the tutorial at + * the given index. + * 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 index The index to add the step at. + * @param parent The parent QUndoCommand. + * @return The new QUndoCommand. + */ + QUndoCommand* addStep(Step* step, int index, 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 Modified: trunk/ktutorial/ktutorial-editor/src/data/Tutorial.cpp =================================================================== --- trunk/ktutorial/ktutorial-editor/src/data/Tutorial.cpp 2010-03-29 03:59:41 UTC (rev 207) +++ trunk/ktutorial/ktutorial-editor/src/data/Tutorial.cpp 2010-03-29 05:56:07 UTC (rev 208) @@ -86,11 +86,15 @@ } void Tutorial::addStep(Step* step) { + addStep(step, mSteps.count()); +} + +void Tutorial::addStep(Step* step, int index) { Q_ASSERT(!mSteps.contains(step)); - mSteps.append(step); + mSteps.insert(index, step); - emit stepAdded(step); + emit stepAdded(step, index); } QList<Step*> Tutorial::steps() const { Modified: trunk/ktutorial/ktutorial-editor/src/data/Tutorial.h =================================================================== --- trunk/ktutorial/ktutorial-editor/src/data/Tutorial.h 2010-03-29 03:59:41 UTC (rev 207) +++ trunk/ktutorial/ktutorial-editor/src/data/Tutorial.h 2010-03-29 05:56:07 UTC (rev 208) @@ -31,8 +31,8 @@ * generate the code to create a true KTutorial::Tutorial. * * When any attribute is modified, dataChanged(Tutorial*) signal is emitted. - * When steps are added or removed, stepAdded(Step*) and stepRemoved(Step*) are - * emitted. + * When steps are added or removed, stepAdded(Step*, int) and stepRemoved(Step*) + * are emitted. */ class Tutorial: public QObject { Q_OBJECT @@ -76,6 +76,17 @@ * @param step The step to add. */ void addStep(Step* step); + + /** + * Adds a new step to this Tutorial at the given position. + * The Tutorial gets ownership of the Step, so it is deleted when the + * Tutorial is deleted. + * + * @param step The step to add. + * @param index The index to add the step at. + */ + void addStep(Step* step, int index); + QList<Step*> steps() const; /** @@ -99,8 +110,9 @@ * Emitted when the step is added to the tutorial. * * @param step The step added. + * @param index The index where the step was added. */ - void stepAdded(Step* step); + void stepAdded(Step* step, int index); /** * Emitted when the step is removed from the tutorial. Modified: trunk/ktutorial/ktutorial-editor/src/view/TutorialTreeItem.cpp =================================================================== --- trunk/ktutorial/ktutorial-editor/src/view/TutorialTreeItem.cpp 2010-03-29 03:59:41 UTC (rev 207) +++ trunk/ktutorial/ktutorial-editor/src/view/TutorialTreeItem.cpp 2010-03-29 05:56:07 UTC (rev 208) @@ -44,10 +44,10 @@ this, SLOT(update(Tutorial*))); foreach(Step* step, tutorial->steps()) { - addStep(step); + addStep(step, mStepTreeItems.count()); } - connect(tutorial, SIGNAL(stepAdded(Step*)), - this, SLOT(addStep(Step*))); + connect(tutorial, SIGNAL(stepAdded(Step*, int)), + this, SLOT(addStep(Step*, int))); connect(tutorial, SIGNAL(stepRemoved(Step*)), this, SLOT(removeStep(Step*))); } @@ -131,12 +131,14 @@ childIndex++; } + + mStepTreeItemBaseIndex = childIndex; } -void TutorialTreeItem::addStep(Step* step) { +void TutorialTreeItem::addStep(Step* step, int index) { StepTreeItem* stepTreeItem = new StepTreeItem(step, this); - appendChild(stepTreeItem); - mStepTreeItems.append(stepTreeItem); + insertChild(stepTreeItem, mStepTreeItemBaseIndex + index); + mStepTreeItems.insert(index, stepTreeItem); } void TutorialTreeItem::removeStep(Step* step) { Modified: trunk/ktutorial/ktutorial-editor/src/view/TutorialTreeItem.h =================================================================== --- trunk/ktutorial/ktutorial-editor/src/view/TutorialTreeItem.h 2010-03-29 03:59:41 UTC (rev 207) +++ trunk/ktutorial/ktutorial-editor/src/view/TutorialTreeItem.h 2010-03-29 05:56:07 UTC (rev 208) @@ -38,9 +38,9 @@ * | -The custom setup code * |-Tear down: * | -The custom tear down code - * |-Step first step added + * |-Step first step * | ... - * |-Step second step added + * |-Step second step * | ... * ... * @@ -131,6 +131,13 @@ QList<StepTreeItem*> mStepTreeItems; /** + * The base index to add new StepTreeItems. + * It is the next index to the last optional data item (license, setup or + * tear down code). + */ + int mStepTreeItemBaseIndex; + + /** * Returns the StepTreeItem for the given Step. * * @param step The Step to get its StepTreeItem. @@ -156,8 +163,9 @@ * Adds a new StepTreeItem when a Step is added in the tutorial. * * @param step The Step added in the Tutorial. + * @param index The index where the Step was added in the Tutorial. */ - void addStep(Step* step); + void addStep(Step* step, int index); /** * Removes the StepTreeItem for the Step removed in the tutorial. Modified: trunk/ktutorial/ktutorial-editor/tests/unit/commands/TutorialCommandsTest.cpp =================================================================== --- trunk/ktutorial/ktutorial-editor/tests/unit/commands/TutorialCommandsTest.cpp 2010-03-29 03:59:41 UTC (rev 207) +++ trunk/ktutorial/ktutorial-editor/tests/unit/commands/TutorialCommandsTest.cpp 2010-03-29 05:56:07 UTC (rev 208) @@ -50,8 +50,12 @@ void testAddStepRedo(); void testAddStepUndo(); + void testAddStepAtIndexRedo(); + void testAddStepAtIndexUndo(); + void testRemoveStepRedo(); void testRemoveStepUndo(); + void testRemoveStepAtMiddleUndo(); }; @@ -239,6 +243,50 @@ QCOMPARE(tutorial.steps().count(), 0); } +void TutorialCommandsTest::testAddStepAtIndexRedo() { + Tutorial tutorial; + TutorialCommands commands(&tutorial); + QUndoCommand parent; + Step* step1 = new Step(); + Step* step2 = new Step(); + Step* step3 = new Step(); + + tutorial.addStep(step1); + tutorial.addStep(step3); + + QUndoCommand* command = commands.addStep(step2, 1, &parent); + command->redo(); + + QCOMPARE(command->text(), i18nc("@action", "Add step")); + QCOMPARE(parent.child(0), command); + QCOMPARE(tutorial.steps().count(), 3); + QCOMPARE(tutorial.steps()[0], step1); + QCOMPARE(tutorial.steps()[1], step2); + QCOMPARE(tutorial.steps()[2], step3); +} + +void TutorialCommandsTest::testAddStepAtIndexUndo() { + Tutorial tutorial; + TutorialCommands commands(&tutorial); + QUndoCommand parent; + Step* step1 = new Step(); + Step* step2 = new Step(); + Step* step3 = new Step(); + + tutorial.addStep(step1); + tutorial.addStep(step3); + + QUndoCommand* command = commands.addStep(step2, 1, &parent); + command->redo(); + command->undo(); + + QCOMPARE(command->text(), i18nc("@action", "Add step")); + QCOMPARE(parent.child(0), command); + QCOMPARE(tutorial.steps().count(), 2); + QCOMPARE(tutorial.steps()[0], step1); + QCOMPARE(tutorial.steps()[1], step3); +} + void TutorialCommandsTest::testRemoveStepRedo() { Tutorial tutorial; TutorialCommands commands(&tutorial); @@ -273,6 +321,30 @@ QCOMPARE(tutorial.steps()[0], step); } +void TutorialCommandsTest::testRemoveStepAtMiddleUndo() { + Tutorial tutorial; + TutorialCommands commands(&tutorial); + QUndoCommand parent; + Step* step1 = new Step(); + Step* step2 = new Step(); + Step* step3 = new Step(); + + tutorial.addStep(step1); + tutorial.addStep(step2); + tutorial.addStep(step3); + + QUndoCommand* command = commands.removeStep(step2, &parent); + command->redo(); + command->undo(); + + QCOMPARE(command->text(), i18nc("@action", "Remove step")); + QCOMPARE(parent.child(0), command); + QCOMPARE(tutorial.steps().count(), 3); + QCOMPARE(tutorial.steps()[0], step1); + QCOMPARE(tutorial.steps()[1], step2); + QCOMPARE(tutorial.steps()[2], step3); +} + QTEST_MAIN(TutorialCommandsTest) #include "TutorialCommandsTest.moc" Modified: trunk/ktutorial/ktutorial-editor/tests/unit/data/TutorialTest.cpp =================================================================== --- trunk/ktutorial/ktutorial-editor/tests/unit/data/TutorialTest.cpp 2010-03-29 03:59:41 UTC (rev 207) +++ trunk/ktutorial/ktutorial-editor/tests/unit/data/TutorialTest.cpp 2010-03-29 05:56:07 UTC (rev 208) @@ -45,13 +45,16 @@ void testSetCustomTearDownCode(); void testAddStep(); + void testAddStepAtIndex(); void testRemoveStep(); private: int mStepStarType; - void assertStepSignal(const QSignalSpy& spy, int index, Step* step); + void assertStepAddedSignal(const QSignalSpy& spy, int index, Step* step, + int stepIndex); + void assertStepRemovedSignal(const QSignalSpy& spy, int index, Step* step); }; @@ -180,7 +183,7 @@ Step* step2 = new Step(); Step* step3 = new Step(); - QSignalSpy stepAddedSpy(&tutorial, SIGNAL(stepAdded(Step*))); + QSignalSpy stepAddedSpy(&tutorial, SIGNAL(stepAdded(Step*, int))); tutorial.addStep(step1); tutorial.addStep(step2); @@ -191,11 +194,33 @@ QCOMPARE(tutorial.steps()[1], step2); QCOMPARE(tutorial.steps()[2], step3); QCOMPARE(stepAddedSpy.count(), 3); - assertStepSignal(stepAddedSpy, 0, step1); - assertStepSignal(stepAddedSpy, 1, step2); - assertStepSignal(stepAddedSpy, 2, step3); + assertStepAddedSignal(stepAddedSpy, 0, step1, 0); + assertStepAddedSignal(stepAddedSpy, 1, step2, 1); + assertStepAddedSignal(stepAddedSpy, 2, step3, 2); } +void TutorialTest::testAddStepAtIndex() { + Tutorial tutorial; + Step* step1 = new Step(); + Step* step2 = new Step(); + Step* step3 = new Step(); + + QSignalSpy stepAddedSpy(&tutorial, SIGNAL(stepAdded(Step*, int))); + + tutorial.addStep(step2, 0); + tutorial.addStep(step1, 0); + tutorial.addStep(step3, 2); + + QCOMPARE(tutorial.steps().count(), 3); + QCOMPARE(tutorial.steps()[0], step1); + QCOMPARE(tutorial.steps()[1], step2); + QCOMPARE(tutorial.steps()[2], step3); + QCOMPARE(stepAddedSpy.count(), 3); + assertStepAddedSignal(stepAddedSpy, 0, step2, 0); + assertStepAddedSignal(stepAddedSpy, 1, step1, 0); + assertStepAddedSignal(stepAddedSpy, 2, step3, 2); +} + void TutorialTest::testRemoveStep() { Tutorial tutorial; @@ -217,21 +242,33 @@ QCOMPARE(tutorial.steps()[0], &step1); QCOMPARE(tutorial.steps()[1], &step3); QCOMPARE(stepRemovedSpy.count(), 1); - assertStepSignal(stepRemovedSpy, 0, &step2); + assertStepRemovedSignal(stepRemovedSpy, 0, &step2); tutorial.removeStep(&step1); tutorial.removeStep(&step3); QCOMPARE(tutorial.steps().count(), 0); QCOMPARE(stepRemovedSpy.count(), 3); - assertStepSignal(stepRemovedSpy, 1, &step1); - assertStepSignal(stepRemovedSpy, 2, &step3); + assertStepRemovedSignal(stepRemovedSpy, 1, &step1); + assertStepRemovedSignal(stepRemovedSpy, 2, &step3); } /////////////////////////////////// Helpers //////////////////////////////////// -void TutorialTest::assertStepSignal(const QSignalSpy& spy, int index, - Step* step) { +void TutorialTest::assertStepAddedSignal(const QSignalSpy& spy, int index, + Step* step, int stepIndex) { + QCOMPARE(spy.at(index).count(), 2); + + QVariant argument = spy.at(index).at(0); + QCOMPARE(argument.userType(), mStepStarType); + QCOMPARE(qvariant_cast<Step*>(argument), step); + argument = spy.at(index).at(1); + QCOMPARE(argument.type(), QVariant::Int); + QCOMPARE(argument.toInt(), stepIndex); +} + +void TutorialTest::assertStepRemovedSignal(const QSignalSpy& spy, int index, + Step* step) { QCOMPARE(spy.at(index).count(), 1); QVariant argument = spy.at(index).at(0); Modified: trunk/ktutorial/ktutorial-editor/tests/unit/view/TutorialTreeItemTest.cpp =================================================================== --- trunk/ktutorial/ktutorial-editor/tests/unit/view/TutorialTreeItemTest.cpp 2010-03-29 03:59:41 UTC (rev 207) +++ trunk/ktutorial/ktutorial-editor/tests/unit/view/TutorialTreeItemTest.cpp 2010-03-29 05:56:07 UTC (rev 208) @@ -57,6 +57,7 @@ void testTutorialSetCustomTearDownCodeEmpty(); void testTutorialAddStep(); + void testTutorialAddStepAtIndex(); void testTutorialRemoveStep(); @@ -398,6 +399,30 @@ assertStep(item.child(2), "Step id"); } +void TutorialTreeItemTest::testTutorialAddStepAtIndex() { + Tutorial tutorial; + TutorialTreeItem item(&tutorial); + + Step* step2 = new Step(); + step2->setId("Step id2"); + tutorial.addStep(step2, 0); + + Step* step1 = new Step(); + step1->setId("Step id1"); + tutorial.addStep(step1, 0); + + Step* step3 = new Step(); + step3->setId("Step id3"); + tutorial.addStep(step3, 2); + + QCOMPARE(item.childCount(), 5); + assertEmptyName(item.child(0)); + assertEmptyDescription(item.child(1)); + assertStep(item.child(2), "Step id1"); + assertStep(item.child(3), "Step id2"); + assertStep(item.child(4), "Step id3"); +} + void TutorialTreeItemTest::testTutorialRemoveStep() { Tutorial tutorial; TutorialTreeItem item(&tutorial); @@ -478,17 +503,32 @@ assertStep(item.child(4), "First step"); assertStep(item.child(5), "Second step"); - tutorial.setLicenseText("The license text"); + Step* step3 = new Step(); + step3->setId("Third step"); + tutorial.addStep(step3, 1); QCOMPARE(item.text(), i18nc("@item", "Tutorial %1", "theName")); QCOMPARE(item.childCount(), 7); assertName(item.child(0), "The name"); assertDescription(item.child(1), "The description"); + assertCustomSetupCode(item.child(2), "The setup code"); + assertCustomTearDownCode(item.child(3), "The tear down code"); + assertStep(item.child(4), "First step"); + assertStep(item.child(5), "Third step"); + assertStep(item.child(6), "Second step"); + + tutorial.setLicenseText("The license text"); + + QCOMPARE(item.text(), i18nc("@item", "Tutorial %1", "theName")); + QCOMPARE(item.childCount(), 8); + assertName(item.child(0), "The name"); + assertDescription(item.child(1), "The description"); assertLicenseText(item.child(2), "The license text"); assertCustomSetupCode(item.child(3), "The setup code"); assertCustomTearDownCode(item.child(4), "The tear down code"); assertStep(item.child(5), "First step"); - assertStep(item.child(6), "Second step"); + assertStep(item.child(6), "Third step"); + assertStep(item.child(7), "Second step"); } void TutorialTreeItemTest::testChildOrderWhenUnsettingDataInTutorial() { This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |