[Ktutorial-commits] SF.net SVN: ktutorial:[158] trunk/ktutorial/ktutorial-editor
Status: Alpha
Brought to you by:
danxuliu
From: <dan...@us...> - 2010-03-15 19:02:12
|
Revision: 158 http://ktutorial.svn.sourceforge.net/ktutorial/?rev=158&view=rev Author: danxuliu Date: 2010-03-15 19:02:05 +0000 (Mon, 15 Mar 2010) Log Message: ----------- Include a list of Reactions in Step, and adapt StepTreeItem to show them. Modified Paths: -------------- trunk/ktutorial/ktutorial-editor/src/Step.cpp trunk/ktutorial/ktutorial-editor/src/Step.h trunk/ktutorial/ktutorial-editor/src/view/StepTreeItem.cpp trunk/ktutorial/ktutorial-editor/src/view/StepTreeItem.h trunk/ktutorial/ktutorial-editor/tests/unit/StepTest.cpp trunk/ktutorial/ktutorial-editor/tests/unit/view/StepTreeItemTest.cpp Modified: trunk/ktutorial/ktutorial-editor/src/Step.cpp =================================================================== --- trunk/ktutorial/ktutorial-editor/src/Step.cpp 2010-03-15 18:58:22 UTC (rev 157) +++ trunk/ktutorial/ktutorial-editor/src/Step.cpp 2010-03-15 19:02:05 UTC (rev 158) @@ -17,12 +17,17 @@ ***************************************************************************/ #include "Step.h" +#include "Reaction.h" //public: Step::Step(QObject* parent): QObject(parent) { } +Step::~Step() { + qDeleteAll(mReactions); +} + QString Step::id() const { return mId; } @@ -62,3 +67,23 @@ emit dataChanged(this); } + +void Step::addReaction(Reaction* reaction) { + Q_ASSERT(!mReactions.contains(reaction)); + + mReactions.append(reaction); + + emit reactionAdded(reaction); +} + +QList<Reaction*> Step::reactions() const { + return mReactions; +} + +void Step::removeReaction(Reaction* reaction) { + Q_ASSERT(mReactions.contains(reaction)); + + mReactions.removeOne(reaction); + + emit reactionRemoved(reaction); +} Modified: trunk/ktutorial/ktutorial-editor/src/Step.h =================================================================== --- trunk/ktutorial/ktutorial-editor/src/Step.h 2010-03-15 18:58:22 UTC (rev 157) +++ trunk/ktutorial/ktutorial-editor/src/Step.h 2010-03-15 19:02:05 UTC (rev 158) @@ -21,13 +21,17 @@ #include <QObject> +class Reaction; + /** * Container for step data. * It stores the data used in KTutorial steps, but it has nothing to do with * them (they don't even know each other). Its purpose is store the data needed to * generate the code to create a true KTutorial::Step. * - * When any attribute is modified, dataChanged(Step*) signal is emitted. + * When any attribute is modified, dataChanged(Step*) signal is emitted. When + * reactions are added or removed, reactionAdded(Reaction*) and + * reactionRemoved(Reaction*) are emitted. */ class Step: public QObject { Q_OBJECT @@ -35,6 +39,11 @@ Step(QObject* parent = 0); + /** + * Destroys this Step and all its reactions. + */ + virtual ~Step(); + QString id() const; void setId(const QString& id); @@ -47,6 +56,24 @@ QString customTearDownCode() const; void setCustomTearDownCode(const QString& code); + /** + * Adds a new reaction to this Step. + * The Step gets ownership of the Reaction, so it is deleted when the + * Step is deleted. + * + * @param reaction The reaction to add. + */ + void addReaction(Reaction* reaction); + QList<Reaction*> reactions() const; + + /** + * Removes a reaction from this Step. + * The Reaction must be deleted explicitly. + * + * @param reaction The reaction to remove. + */ + void removeReaction(Reaction* reaction); + Q_SIGNALS: /** @@ -56,12 +83,27 @@ */ void dataChanged(Step* step); + /** + * Emitted when the reaction is added to this Step. + * + * @param reaction The reaction added. + */ + void reactionAdded(Reaction* reaction); + + /** + * Emitted when the reaction is removed from this Step. + * + * @param reaction The reaction removed. + */ + void reactionRemoved(Reaction* reaction); + private: QString mId; QString mText; QString mCustomSetupCode; QString mCustomTearDownCode; + QList<Reaction*> mReactions; }; Modified: trunk/ktutorial/ktutorial-editor/src/view/StepTreeItem.cpp =================================================================== --- trunk/ktutorial/ktutorial-editor/src/view/StepTreeItem.cpp 2010-03-15 18:58:22 UTC (rev 157) +++ trunk/ktutorial/ktutorial-editor/src/view/StepTreeItem.cpp 2010-03-15 19:02:05 UTC (rev 158) @@ -20,6 +20,7 @@ #include <KLocalizedString> +#include "ReactionTreeItem.h" #include "TextTreeItem.h" #include "TreeItemUtil.h" #include "../Step.h" @@ -38,6 +39,14 @@ update(step); connect(step, SIGNAL(dataChanged(Step*)), this, SLOT(update(Step*))); + + foreach(Reaction* reaction, step->reactions()) { + addReaction(reaction); + } + connect(step, SIGNAL(reactionAdded(Reaction*)), + this, SLOT(addReaction(Reaction*))); + connect(step, SIGNAL(reactionRemoved(Reaction*)), + this, SLOT(removeReaction(Reaction*))); } QString StepTreeItem::text() const { @@ -52,6 +61,19 @@ return mStep; } +//private: + +ReactionTreeItem* StepTreeItem::reactionTreeItemForReaction( + Reaction* reaction) const { + foreach (ReactionTreeItem* reactionTreeItem, mReactionTreeItems) { + if (reactionTreeItem->reaction() == reaction) { + return reactionTreeItem; + } + } + + return 0; +} + //private slots: void StepTreeItem::update(Step* step) { @@ -96,3 +118,17 @@ childIndex++; } } + +void StepTreeItem::addReaction(Reaction* reaction) { + ReactionTreeItem* reactionTreeItem = new ReactionTreeItem(reaction, this); + appendChild(reactionTreeItem); + mReactionTreeItems.append(reactionTreeItem); +} + +void StepTreeItem::removeReaction(Reaction* reaction) { + ReactionTreeItem* reactionTreeItem = reactionTreeItemForReaction(reaction); + + removeChild(reactionTreeItem); + mReactionTreeItems.removeOne(reactionTreeItem); + delete reactionTreeItem; +} Modified: trunk/ktutorial/ktutorial-editor/src/view/StepTreeItem.h =================================================================== --- trunk/ktutorial/ktutorial-editor/src/view/StepTreeItem.h 2010-03-15 18:58:22 UTC (rev 157) +++ trunk/ktutorial/ktutorial-editor/src/view/StepTreeItem.h 2010-03-15 19:02:05 UTC (rev 158) @@ -21,6 +21,8 @@ #include "TreeItem.h" +class Reaction; +class ReactionTreeItem; class Step; class TextTreeItem; @@ -32,7 +34,12 @@ * |-Setup: * | -The custom setup code * --Tear down: - * -The custom tear down code + * | -The custom tear down code + * |-Reaction + * | ... + * |-Reaction + * | ... + * ... * * The items only appear if they have some data to show. For example, if only * the text of the Step is set, its representation is: @@ -48,6 +55,8 @@ * Also note that the order of the child elements is always the same. Even if, * for example, the setup code is set first and then the text, the text item * will appear first and then the setup code item. + * + * @see ReactionTreeItem */ class StepTreeItem: public TreeItem { Q_OBJECT @@ -108,6 +117,19 @@ */ TextTreeItem* mTearDownItem; + /** + * The ReactionTreeItems for each Reaction in the Step. + */ + QList<ReactionTreeItem*> mReactionTreeItems; + + /** + * Returns the ReactionTreeItem for the given Reaction. + * + * @param reaction The Reaction to get its ReactionTreeItem. + * @return The ReactionTreeItem. + */ + ReactionTreeItem* reactionTreeItemForReaction(Reaction* reaction) const; + private Q_SLOTS: /** @@ -122,6 +144,20 @@ */ void update(Step* step); + /** + * Adds a new ReactionTreeItem when a Reaction is added in the step. + * + * @param step The Reaction added in the Step. + */ + void addReaction(Reaction* reaction); + + /** + * Removes the ReactionTreeItem for the Reaction removed in the step. + * + * @param reaction The Reaction removed in the Step. + */ + void removeReaction(Reaction* reaction); + }; #endif Modified: trunk/ktutorial/ktutorial-editor/tests/unit/StepTest.cpp =================================================================== --- trunk/ktutorial/ktutorial-editor/tests/unit/StepTest.cpp 2010-03-15 18:58:22 UTC (rev 157) +++ trunk/ktutorial/ktutorial-editor/tests/unit/StepTest.cpp 2010-03-15 19:02:05 UTC (rev 158) @@ -19,12 +19,15 @@ #include <QtTest> #include "Step.h" +#include "Reaction.h" class StepTest: public QObject { Q_OBJECT private slots: + void initTestCase(); + void testConstructor(); void testSetId(); @@ -35,8 +38,22 @@ void testSetCustomTearDownCode(); + void testAddReaction(); + void testRemoveReaction(); + +private: + + int mReactionStarType; + + void assertReactionSignal(const QSignalSpy& spy, int index, + Reaction* reaction) const; }; +void StepTest::initTestCase() { + //Reaction* must be registered in order to be used with QSignalSpy + mReactionStarType = qRegisterMetaType<Reaction*>("Reaction*"); +} + void StepTest::testConstructor() { QObject parent; Step* step = new Step(&parent); @@ -111,6 +128,74 @@ QCOMPARE(qvariant_cast<Step*>(argument), &step); } +void StepTest::testAddReaction() { + Step step; + Reaction* reaction1 = new Reaction(); + Reaction* reaction2 = new Reaction(); + Reaction* reaction3 = new Reaction(); + + QSignalSpy reactionAddedSpy(&step, SIGNAL(reactionAdded(Reaction*))); + + step.addReaction(reaction1); + step.addReaction(reaction2); + step.addReaction(reaction3); + + QCOMPARE(step.reactions().count(), 3); + QCOMPARE(step.reactions()[0], reaction1); + QCOMPARE(step.reactions()[1], reaction2); + QCOMPARE(step.reactions()[2], reaction3); + QCOMPARE(reactionAddedSpy.count(), 3); + assertReactionSignal(reactionAddedSpy, 0, reaction1); + assertReactionSignal(reactionAddedSpy, 1, reaction2); + assertReactionSignal(reactionAddedSpy, 2, reaction3); +} + +void StepTest::testRemoveReaction() { + Step step; + + //They will be removed and not deleted by the Step, so they are created in + //stack + Reaction reaction1; + Reaction reaction2; + Reaction reaction3; + + step.addReaction(&reaction1); + step.addReaction(&reaction2); + step.addReaction(&reaction3); + + QSignalSpy reactionRemovedSpy(&step, SIGNAL(reactionRemoved(Reaction*))); + + step.removeReaction(&reaction2); + + QCOMPARE(step.reactions().count(), 2); + QCOMPARE(step.reactions()[0], &reaction1); + QCOMPARE(step.reactions()[1], &reaction3); + QCOMPARE(reactionRemovedSpy.count(), 1); + assertReactionSignal(reactionRemovedSpy, 0, &reaction2); + + step.removeReaction(&reaction1); + step.removeReaction(&reaction3); + + QCOMPARE(step.reactions().count(), 0); + QCOMPARE(reactionRemovedSpy.count(), 3); + assertReactionSignal(reactionRemovedSpy, 1, &reaction1); + assertReactionSignal(reactionRemovedSpy, 2, &reaction3); +} + +/////////////////////////////////// Helpers //////////////////////////////////// + +//Reaction* must be declared as a metatype to be used in qvariant_cast +Q_DECLARE_METATYPE(Reaction*); + +void StepTest::assertReactionSignal(const QSignalSpy& spy, int index, + Reaction* reaction) const { + QCOMPARE(spy.at(index).count(), 1); + + QVariant argument = spy.at(index).at(0); + QCOMPARE(argument.userType(), mReactionStarType); + QCOMPARE(qvariant_cast<Reaction*>(argument), reaction); +} + QTEST_MAIN(StepTest) #include "StepTest.moc" Modified: trunk/ktutorial/ktutorial-editor/tests/unit/view/StepTreeItemTest.cpp =================================================================== --- trunk/ktutorial/ktutorial-editor/tests/unit/view/StepTreeItemTest.cpp 2010-03-15 18:58:22 UTC (rev 157) +++ trunk/ktutorial/ktutorial-editor/tests/unit/view/StepTreeItemTest.cpp 2010-03-15 19:02:05 UTC (rev 158) @@ -22,7 +22,9 @@ #include <KLocalizedString> +#include "ReactionTreeItem.h" #include "../Step.h" +#include "../Reaction.h" class StepTreeItemTest: public QObject { Q_OBJECT @@ -50,6 +52,10 @@ void testStepSetCustomTearDownCodeChange(); void testStepSetCustomTearDownCodeEmpty(); + void testStepAddReaction(); + + void testStepRemoveReaction(); + void testChildOrderWhenSettingDataInStep(); void testChildOrderWhenUnsettingDataInStep(); @@ -61,6 +67,7 @@ void assertCustomSetupCode(TreeItem* setupItem, const QString& code) const; void assertCustomTearDownCode(TreeItem* tearDownItem, const QString& code) const; + void assertReaction(TreeItem* reactionItem, const Reaction* reaction) const; void assertDataChanged(const QSignalSpy& spy, int index, TreeItem* item) const; @@ -98,16 +105,24 @@ step.setCustomSetupCode("The setup code"); step.setCustomTearDownCode("The tear down code"); + Reaction* reaction1 = new Reaction(); + step.addReaction(reaction1); + + Reaction* reaction2 = new Reaction(); + step.addReaction(reaction2); + StubTreeItem parent; StepTreeItem item(&step, &parent); QCOMPARE(item.parent(), &parent); QCOMPARE(item.text(), i18nc("@item", "Step %1", "The id")); QCOMPARE(item.step(), &step); - QCOMPARE(item.childCount(), 3); + QCOMPARE(item.childCount(), 5); assertText(item.child(0), "The text"); assertCustomSetupCode(item.child(1), "The setup code"); assertCustomTearDownCode(item.child(2), "The tear down code"); + assertReaction(item.child(3), reaction1); + assertReaction(item.child(4), reaction2); } //TreeItem* must be declared as a metatype to be used in qvariant_cast @@ -271,6 +286,30 @@ QCOMPARE(item.childCount(), 0); } +void StepTreeItemTest::testStepAddReaction() { + Step step; + StepTreeItem item(&step); + + Reaction* reaction = new Reaction(); + step.addReaction(reaction); + + QCOMPARE(item.childCount(), 1); + assertReaction(item.child(0), reaction); +} + +void StepTreeItemTest::testStepRemoveReaction() { + Step step; + StepTreeItem item(&step); + + //It will be removed and not deleted by the Step, so it is created in + //stack + Reaction reaction; + step.addReaction(&reaction); + step.removeReaction(&reaction); + + QCOMPARE(item.childCount(), 0); +} + void StepTreeItemTest::testChildOrderWhenSettingDataInStep() { Step step; StepTreeItem item(&step); @@ -288,21 +327,44 @@ assertText(item.child(0), "The text"); assertCustomSetupCode(item.child(1), "The setup code"); - step.setCustomTearDownCode("The tear down code"); + Reaction* reaction1 = new Reaction(); + step.addReaction(reaction1); QCOMPARE(item.text(), i18nc("@item", "Step")); QCOMPARE(item.childCount(), 3); assertText(item.child(0), "The text"); assertCustomSetupCode(item.child(1), "The setup code"); + assertReaction(item.child(2), reaction1); + + step.setCustomTearDownCode("The tear down code"); + + QCOMPARE(item.text(), i18nc("@item", "Step")); + QCOMPARE(item.childCount(), 4); + assertText(item.child(0), "The text"); + assertCustomSetupCode(item.child(1), "The setup code"); assertCustomTearDownCode(item.child(2), "The tear down code"); + assertReaction(item.child(3), reaction1); + Reaction* reaction2 = new Reaction(); + step.addReaction(reaction2); + + QCOMPARE(item.text(), i18nc("@item", "Step")); + QCOMPARE(item.childCount(), 5); + assertText(item.child(0), "The text"); + assertCustomSetupCode(item.child(1), "The setup code"); + assertCustomTearDownCode(item.child(2), "The tear down code"); + assertReaction(item.child(3), reaction1); + assertReaction(item.child(4), reaction2); + step.setId("The id"); QCOMPARE(item.text(), i18nc("@item", "Step %1", "The id")); - QCOMPARE(item.childCount(), 3); + QCOMPARE(item.childCount(), 5); assertText(item.child(0), "The text"); assertCustomSetupCode(item.child(1), "The setup code"); assertCustomTearDownCode(item.child(2), "The tear down code"); + assertReaction(item.child(3), reaction1); + assertReaction(item.child(4), reaction2); } void StepTreeItemTest::testChildOrderWhenUnsettingDataInStep() { @@ -312,30 +374,54 @@ step.setCustomSetupCode("The setup code"); step.setCustomTearDownCode("The tear down code"); + Reaction reaction1; + step.addReaction(&reaction1); + + Reaction reaction2; + step.addReaction(&reaction2); + StepTreeItem item(&step); step.setText(""); QCOMPARE(item.text(), i18nc("@item", "Step %1", "The id")); - QCOMPARE(item.childCount(), 2); + QCOMPARE(item.childCount(), 4); assertCustomSetupCode(item.child(0), "The setup code"); assertCustomTearDownCode(item.child(1), "The tear down code"); + assertReaction(item.child(2), &reaction1); + assertReaction(item.child(3), &reaction2); + step.removeReaction(&reaction1); + + QCOMPARE(item.text(), i18nc("@item", "Step %1", "The id")); + QCOMPARE(item.childCount(), 3); + assertCustomSetupCode(item.child(0), "The setup code"); + assertCustomTearDownCode(item.child(1), "The tear down code"); + assertReaction(item.child(2), &reaction2); + step.setCustomTearDownCode(""); QCOMPARE(item.text(), i18nc("@item", "Step %1", "The id")); - QCOMPARE(item.childCount(), 1); + QCOMPARE(item.childCount(), 2); assertCustomSetupCode(item.child(0), "The setup code"); + assertReaction(item.child(1), &reaction2); step.setId(""); QCOMPARE(item.text(), i18nc("@item", "Step")); - QCOMPARE(item.childCount(), 1); + QCOMPARE(item.childCount(), 2); assertCustomSetupCode(item.child(0), "The setup code"); + assertReaction(item.child(1), &reaction2); step.setCustomSetupCode(""); QCOMPARE(item.text(), i18nc("@item", "Step")); + QCOMPARE(item.childCount(), 1); + assertReaction(item.child(0), &reaction2); + + step.removeReaction(&reaction2); + + QCOMPARE(item.text(), i18nc("@item", "Step")); QCOMPARE(item.childCount(), 0); } @@ -360,6 +446,13 @@ QCOMPARE(tearDownItem->child(0)->text(), i18nc("@item", code.toAscii())); } +void StepTreeItemTest::assertReaction(TreeItem* reactionItem, + const Reaction* reaction) const { + QVERIFY(qobject_cast<ReactionTreeItem*>(reactionItem)); + QCOMPARE(static_cast<ReactionTreeItem*>(reactionItem)->reaction(), + reaction); +} + void StepTreeItemTest::assertDataChanged(const QSignalSpy& spy, int index, TreeItem* item) const { QCOMPARE(spy.at(index).count(), 1); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |