ktutorial-commits Mailing List for KTutorial (Page 11)
Status: Alpha
Brought to you by:
danxuliu
You can subscribe to this list here.
2010 |
Jan
(1) |
Feb
(36) |
Mar
(117) |
Apr
(11) |
May
(8) |
Jun
(1) |
Jul
|
Aug
(2) |
Sep
(21) |
Oct
(16) |
Nov
|
Dec
|
---|---|---|---|---|---|---|---|---|---|---|---|---|
2011 |
Jan
(1) |
Feb
|
Mar
(6) |
Apr
(6) |
May
(15) |
Jun
(15) |
Jul
(6) |
Aug
|
Sep
(1) |
Oct
(4) |
Nov
|
Dec
|
2012 |
Jan
|
Feb
|
Mar
|
Apr
|
May
|
Jun
(10) |
Jul
(4) |
Aug
(29) |
Sep
(4) |
Oct
|
Nov
|
Dec
(2) |
From: <dan...@us...> - 2010-03-09 20:04:34
|
Revision: 137 http://ktutorial.svn.sourceforge.net/ktutorial/?rev=137&view=rev Author: danxuliu Date: 2010-03-09 20:04:23 +0000 (Tue, 09 Mar 2010) Log Message: ----------- Fix Krazy2 explicit constructor issue. Modified Paths: -------------- trunk/ktutorial/ktutorial-editor/src/view/TutorialTreeSelectionManager.h Modified: trunk/ktutorial/ktutorial-editor/src/view/TutorialTreeSelectionManager.h =================================================================== --- trunk/ktutorial/ktutorial-editor/src/view/TutorialTreeSelectionManager.h 2010-03-09 19:45:44 UTC (rev 136) +++ trunk/ktutorial/ktutorial-editor/src/view/TutorialTreeSelectionManager.h 2010-03-09 20:04:23 UTC (rev 137) @@ -44,8 +44,8 @@ * selection. * @param parent The parent object. */ - TutorialTreeSelectionManager(QItemSelectionModel* itemSelectionModel, - QObject* parent = 0); + explicit TutorialTreeSelectionManager( + QItemSelectionModel* itemSelectionModel, QObject* parent = 0); Q_SIGNALS: This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <dan...@us...> - 2010-03-09 19:45:51
|
Revision: 136 http://ktutorial.svn.sourceforge.net/ktutorial/?rev=136&view=rev Author: danxuliu Date: 2010-03-09 19:45:44 +0000 (Tue, 09 Mar 2010) Log Message: ----------- Add LicenseWidget to set the license of the tutorial. Modified Paths: -------------- trunk/ktutorial/ktutorial-editor/src/KTutorialEditor.cpp trunk/ktutorial/ktutorial-editor/src/KTutorialEditor.h trunk/ktutorial/ktutorial-editor/src/ktutorial-editorui.rc trunk/ktutorial/ktutorial-editor/src/view/CMakeLists.txt trunk/ktutorial/ktutorial-editor/tests/unit/view/CMakeLists.txt Added Paths: ----------- trunk/ktutorial/ktutorial-editor/src/view/LicenseWidget.cpp trunk/ktutorial/ktutorial-editor/src/view/LicenseWidget.h trunk/ktutorial/ktutorial-editor/src/view/LicenseWidget.ui trunk/ktutorial/ktutorial-editor/tests/unit/view/LicenseWidgetTest.cpp Modified: trunk/ktutorial/ktutorial-editor/src/KTutorialEditor.cpp =================================================================== --- trunk/ktutorial/ktutorial-editor/src/KTutorialEditor.cpp 2010-03-09 19:16:53 UTC (rev 135) +++ trunk/ktutorial/ktutorial-editor/src/KTutorialEditor.cpp 2010-03-09 19:45:44 UTC (rev 136) @@ -28,6 +28,7 @@ #include "Step.h" #include "Tutorial.h" #include "view/EditionDialog.h" +#include "view/LicenseWidget.h" #include "view/StepCustomCodeWidget.h" #include "view/StepDataWidget.h" #include "view/TreeModel.h" @@ -80,6 +81,13 @@ this, SLOT(setTutorialInformation())); action = new KAction(this); + action->setText(i18nc("@action", "Set license...")); + action->setStatusTip(i18nc("@info:status", "Set the license text of the " +"tutorial.")); + actionCollection()->addAction("setTutorialLicense", action); + connect(action, SIGNAL(triggered(bool)), this, SLOT(setTutorialLicense())); + + action = new KAction(this); action->setText(i18nc("@action", "Set setup code...")); action->setStatusTip(i18nc("@info:status", "Set the custom code to be " "executed when the tutorial starts.")); @@ -163,6 +171,10 @@ showEditionDialog(new TutorialInformationWidget(mTutorial)); } +void KTutorialEditor::setTutorialLicense() { + showEditionDialog(new LicenseWidget(mTutorial)); +} + void KTutorialEditor::setTutorialSetup() { showEditionDialog(new TutorialCustomCodeWidget(mTutorial, TutorialCustomCodeWidget::Setup)); Modified: trunk/ktutorial/ktutorial-editor/src/KTutorialEditor.h =================================================================== --- trunk/ktutorial/ktutorial-editor/src/KTutorialEditor.h 2010-03-09 19:16:53 UTC (rev 135) +++ trunk/ktutorial/ktutorial-editor/src/KTutorialEditor.h 2010-03-09 19:45:44 UTC (rev 136) @@ -91,6 +91,11 @@ void setTutorialInformation(); /** + * Shows a LicenseWidget for the tutorial. + */ + void setTutorialLicense(); + + /** * Shows a TutorialCustomCodeWidget for the setup code of the tutorial. */ void setTutorialSetup(); Modified: trunk/ktutorial/ktutorial-editor/src/ktutorial-editorui.rc =================================================================== --- trunk/ktutorial/ktutorial-editor/src/ktutorial-editorui.rc 2010-03-09 19:16:53 UTC (rev 135) +++ trunk/ktutorial/ktutorial-editor/src/ktutorial-editorui.rc 2010-03-09 19:45:44 UTC (rev 136) @@ -6,6 +6,7 @@ <Menu name="editTutorials"> <Text context="@item:inmenu">Tutorial</Text> <Action name="setTutorialInformation"/> + <Action name="setTutorialLicense"/> <Action name="setTutorialSetup"/> <Action name="setTutorialTearDown"/> </Menu> Modified: trunk/ktutorial/ktutorial-editor/src/view/CMakeLists.txt =================================================================== --- trunk/ktutorial/ktutorial-editor/src/view/CMakeLists.txt 2010-03-09 19:16:53 UTC (rev 135) +++ trunk/ktutorial/ktutorial-editor/src/view/CMakeLists.txt 2010-03-09 19:45:44 UTC (rev 136) @@ -3,6 +3,7 @@ set(ktutorial_editor_view_SRCS EditionDialog.cpp EditionWidget.cpp + LicenseWidget.cpp StepCustomCodeWidget.cpp StepDataWidget.cpp StepTreeItem.cpp @@ -18,6 +19,7 @@ kde4_add_ui_files(ktutorial_editor_view_SRCS CustomCodeWidget.ui + LicenseWidget.ui StepDataWidget.ui TutorialInformationWidget.ui ) Added: trunk/ktutorial/ktutorial-editor/src/view/LicenseWidget.cpp =================================================================== --- trunk/ktutorial/ktutorial-editor/src/view/LicenseWidget.cpp (rev 0) +++ trunk/ktutorial/ktutorial-editor/src/view/LicenseWidget.cpp 2010-03-09 19:45:44 UTC (rev 136) @@ -0,0 +1,44 @@ +/*************************************************************************** + * 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 "LicenseWidget.h" + +#include "ui_LicenseWidget.h" +#include "../Tutorial.h" + +//public: + +LicenseWidget::LicenseWidget(Tutorial* tutorial, QWidget* parent): + EditionWidget(parent), + mTutorial(tutorial) { + ui = new Ui::LicenseWidget(); + ui->setupUi(this); + + ui->licenseTextEdit->setText(tutorial->licenseText()); +} + +LicenseWidget::~LicenseWidget() { + delete ui; +} + +void LicenseWidget::saveChanges() { + QString licenseText = ui->licenseTextEdit->toPlainText(); + if (mTutorial->licenseText() != licenseText) { + mTutorial->setLicenseText(licenseText); + } +} Property changes on: trunk/ktutorial/ktutorial-editor/src/view/LicenseWidget.cpp ___________________________________________________________________ Added: svn:eol-style + native Added: trunk/ktutorial/ktutorial-editor/src/view/LicenseWidget.h =================================================================== --- trunk/ktutorial/ktutorial-editor/src/view/LicenseWidget.h (rev 0) +++ trunk/ktutorial/ktutorial-editor/src/view/LicenseWidget.h 2010-03-09 19:45:44 UTC (rev 136) @@ -0,0 +1,69 @@ +/*************************************************************************** + * 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 LICENSEWIDGET_H +#define LICENSEWIDGET_H + +#include "EditionWidget.h" + +class Tutorial; + +namespace Ui { +class LicenseWidget; +} + +/** + * Edition widget for the license of a tutorial. + */ +class LicenseWidget: public EditionWidget { +Q_OBJECT +public: + + /** + * Creates a new LicenseWidget for the given Tutorial. + * + * @param tutorial The tutorial to set its license. + * @param parent The parent QWidget. + */ + explicit LicenseWidget(Tutorial* tutorial, QWidget* parent = 0); + + /** + * Destroys this widget. + */ + virtual ~LicenseWidget(); + + /** + * Saves the license in the tutorial. + */ + virtual void saveChanges(); + +private: + + /** + * The tutorial to set its license. + */ + Tutorial* mTutorial; + + /** + * The Ui Designer generated class. + */ + Ui::LicenseWidget* ui; + +}; + +#endif Property changes on: trunk/ktutorial/ktutorial-editor/src/view/LicenseWidget.h ___________________________________________________________________ Added: svn:eol-style + native Added: trunk/ktutorial/ktutorial-editor/src/view/LicenseWidget.ui =================================================================== --- trunk/ktutorial/ktutorial-editor/src/view/LicenseWidget.ui (rev 0) +++ trunk/ktutorial/ktutorial-editor/src/view/LicenseWidget.ui 2010-03-09 19:45:44 UTC (rev 136) @@ -0,0 +1,44 @@ +<?xml version="1.0" encoding="UTF-8"?> +<ui version="4.0"> + <class>LicenseWidget</class> + <widget class="QWidget" name="LicenseWidget"> + <property name="geometry"> + <rect> + <x>0</x> + <y>0</y> + <width>400</width> + <height>300</height> + </rect> + </property> + <property name="windowTitle"> + <string comment="@title">Set tutorial license</string> + </property> + <property name="whatsThis"> + <string comment="@info:whatsthis"><para>Set the license of the tutorial.</para> +<para>The license is copied, commented, to the top of the exported tutorial file. That is, you don't have to worry about how comments are written in the programming language the tutorial will be exported to. Just set the pure text of the license, and the editor will take care of adding the necessary characters in each line to mark them as comments instead of source code.</para></string> + </property> + <layout class="QVBoxLayout" name="LicenseWidgetLayout"> + <item> + <widget class="QGroupBox" name="licenseGroupBox"> + <property name="title"> + <string comment="@title">License text</string> + </property> + <layout class="QVBoxLayout" name="customCodeGroupBoxLayout"> + <item> + <widget class="KTextEdit" name="licenseTextEdit"/> + </item> + </layout> + </widget> + </item> + </layout> + </widget> + <customwidgets> + <customwidget> + <class>KTextEdit</class> + <extends>QTextEdit</extends> + <header>ktextedit.h</header> + </customwidget> + </customwidgets> + <resources/> + <connections/> +</ui> Modified: trunk/ktutorial/ktutorial-editor/tests/unit/view/CMakeLists.txt =================================================================== --- trunk/ktutorial/ktutorial-editor/tests/unit/view/CMakeLists.txt 2010-03-09 19:16:53 UTC (rev 135) +++ trunk/ktutorial/ktutorial-editor/tests/unit/view/CMakeLists.txt 2010-03-09 19:45:44 UTC (rev 136) @@ -16,7 +16,7 @@ ENDFOREACH(_className) ENDMACRO(UNIT_TESTS) -unit_tests(EditionDialog StepCustomCodeWidget StepDataWidget StepTreeItem TextTreeItem TreeItem TreeItemUtil TreeModel TutorialCustomCodeWidget TutorialInformationWidget TutorialTreeItem TutorialTreeSelectionManager) +unit_tests(EditionDialog LicenseWidget StepCustomCodeWidget StepDataWidget StepTreeItem TextTreeItem TreeItem TreeItemUtil TreeModel TutorialCustomCodeWidget TutorialInformationWidget TutorialTreeItem TutorialTreeSelectionManager) MACRO(MEM_TESTS) FOREACH(_testname ${ARGN}) @@ -24,4 +24,4 @@ ENDFOREACH(_testname) ENDMACRO(MEM_TESTS) -mem_tests(EditionDialog StepCustomCodeWidget StepDataWidget StepTreeItem TextTreeItem TreeItem TreeItemUtil TreeModel TutorialCustomCodeWidget TutorialInformationWidget TutorialTreeItem TutorialTreeSelectionManager) +mem_tests(EditionDialog LicenseWidget StepCustomCodeWidget StepDataWidget StepTreeItem TextTreeItem TreeItem TreeItemUtil TreeModel TutorialCustomCodeWidget TutorialInformationWidget TutorialTreeItem TutorialTreeSelectionManager) Added: trunk/ktutorial/ktutorial-editor/tests/unit/view/LicenseWidgetTest.cpp =================================================================== --- trunk/ktutorial/ktutorial-editor/tests/unit/view/LicenseWidgetTest.cpp (rev 0) +++ trunk/ktutorial/ktutorial-editor/tests/unit/view/LicenseWidgetTest.cpp 2010-03-09 19:45:44 UTC (rev 136) @@ -0,0 +1,74 @@ +/*************************************************************************** + * 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 <QtTest> + +#include "LicenseWidget.h" + +#include <KLineEdit> +#include <KTextEdit> + +#include "../Tutorial.h" + +class LicenseWidgetTest: public QObject { +Q_OBJECT + +private slots: + + void testConstructor(); + + void testSaveChanges(); + +private: + + KTextEdit* licenseTextEdit(LicenseWidget* widget) const; + +}; + +void LicenseWidgetTest::testConstructor() { + Tutorial tutorial; + tutorial.setLicenseText("The license"); + + QWidget parent; + LicenseWidget* widget = new LicenseWidget(&tutorial, &parent); + + QCOMPARE(widget->parentWidget(), &parent); + QCOMPARE(licenseTextEdit(widget)->toPlainText(), QString("The license")); +} + +void LicenseWidgetTest::testSaveChanges() { + Tutorial tutorial; + tutorial.setLicenseText("The license"); + + LicenseWidget widget(&tutorial); + licenseTextEdit(&widget)->setText("The new license"); + + widget.saveChanges(); + + QCOMPARE(tutorial.licenseText(), QString("The new license")); +} + +/////////////////////////////////// Helpers //////////////////////////////////// + +KTextEdit* LicenseWidgetTest::licenseTextEdit(LicenseWidget* widget) const { + return widget->findChild<KTextEdit*>("licenseTextEdit"); +} + +QTEST_MAIN(LicenseWidgetTest) + +#include "LicenseWidgetTest.moc" Property changes on: trunk/ktutorial/ktutorial-editor/tests/unit/view/LicenseWidgetTest.cpp ___________________________________________________________________ Added: svn:eol-style + native This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <dan...@us...> - 2010-03-09 19:17:07
|
Revision: 135 http://ktutorial.svn.sourceforge.net/ktutorial/?rev=135&view=rev Author: danxuliu Date: 2010-03-09 19:16:53 +0000 (Tue, 09 Mar 2010) Log Message: ----------- Add TutorialCustomCodeWidget and StepCustomCodeWidget widgets to edit the setup and tear down code of tutorials and steps. Modified Paths: -------------- trunk/ktutorial/ktutorial-editor/src/KTutorialEditor.cpp trunk/ktutorial/ktutorial-editor/src/KTutorialEditor.h trunk/ktutorial/ktutorial-editor/src/ktutorial-editorui.rc trunk/ktutorial/ktutorial-editor/src/view/CMakeLists.txt trunk/ktutorial/ktutorial-editor/tests/unit/view/CMakeLists.txt Added Paths: ----------- trunk/ktutorial/ktutorial-editor/src/view/CustomCodeWidget.ui trunk/ktutorial/ktutorial-editor/src/view/StepCustomCodeWidget.cpp trunk/ktutorial/ktutorial-editor/src/view/StepCustomCodeWidget.h trunk/ktutorial/ktutorial-editor/src/view/TutorialCustomCodeWidget.cpp trunk/ktutorial/ktutorial-editor/src/view/TutorialCustomCodeWidget.h trunk/ktutorial/ktutorial-editor/tests/unit/view/StepCustomCodeWidgetTest.cpp trunk/ktutorial/ktutorial-editor/tests/unit/view/TutorialCustomCodeWidgetTest.cpp Modified: trunk/ktutorial/ktutorial-editor/src/KTutorialEditor.cpp =================================================================== --- trunk/ktutorial/ktutorial-editor/src/KTutorialEditor.cpp 2010-03-09 16:14:46 UTC (rev 134) +++ trunk/ktutorial/ktutorial-editor/src/KTutorialEditor.cpp 2010-03-09 19:16:53 UTC (rev 135) @@ -28,8 +28,10 @@ #include "Step.h" #include "Tutorial.h" #include "view/EditionDialog.h" +#include "view/StepCustomCodeWidget.h" #include "view/StepDataWidget.h" #include "view/TreeModel.h" +#include "view/TutorialCustomCodeWidget.h" #include "view/TutorialInformationWidget.h" #include "view/TutorialTreeItem.h" #include "view/TutorialTreeSelectionManager.h" @@ -78,6 +80,20 @@ this, SLOT(setTutorialInformation())); action = new KAction(this); + action->setText(i18nc("@action", "Set setup code...")); + action->setStatusTip(i18nc("@info:status", "Set the custom code to be " +"executed when the tutorial starts.")); + actionCollection()->addAction("setTutorialSetup", action); + connect(action, SIGNAL(triggered(bool)), this, SLOT(setTutorialSetup())); + + action = new KAction(this); + action->setText(i18nc("@action", "Set tear down code...")); + action->setStatusTip(i18nc("@info:status", "Set the custom code to be " +"executed when the tutorial finishes.")); + actionCollection()->addAction("setTutorialTearDown", action); + connect(action, SIGNAL(triggered(bool)), this, SLOT(setTutorialTearDown())); + + action = new KAction(this); action->setText(i18nc("@action", "Add step...")); action->setStatusTip(i18nc("@info:status", "Add a new step to the " "tutorial.")); @@ -93,6 +109,23 @@ connect(action, SIGNAL(triggered(bool)), this, SLOT(setStepData())); action = new KAction(this); + action->setText(i18nc("@action", "Set setup code...")); + action->setStatusTip(i18nc("@info:status", "Set the custom code to be " +"executed when the tutorial passes to the currently selected step.")); + action->setEnabled(false); + actionCollection()->addAction("setStepSetup", action); + connect(action, SIGNAL(triggered(bool)), this, SLOT(setStepSetup())); + + action = new KAction(this); + action->setText(i18nc("@action", "Set tear down code...")); + action->setStatusTip(i18nc("@info:status", "Set the custom code to be " +"executed when the tutorial changes from the currently selected step to " +"another step.")); + action->setEnabled(false); + actionCollection()->addAction("setStepTearDown", action); + connect(action, SIGNAL(triggered(bool)), this, SLOT(setStepTearDown())); + + action = new KAction(this); action->setText(i18nc("@action", "Remove step")); action->setStatusTip(i18nc("@info:status", "Removes the currently selected " "step from the tutorial.")); @@ -115,9 +148,13 @@ if (mCurrentStep) { actionCollection()->action("setStepData")->setEnabled(true); + actionCollection()->action("setStepSetup")->setEnabled(true); + actionCollection()->action("setStepTearDown")->setEnabled(true); actionCollection()->action("removeStep")->setEnabled(true); } else { actionCollection()->action("setStepData")->setEnabled(false); + actionCollection()->action("setStepSetup")->setEnabled(false); + actionCollection()->action("setStepTearDown")->setEnabled(false); actionCollection()->action("removeStep")->setEnabled(false); } } @@ -126,6 +163,16 @@ showEditionDialog(new TutorialInformationWidget(mTutorial)); } +void KTutorialEditor::setTutorialSetup() { + showEditionDialog(new TutorialCustomCodeWidget(mTutorial, + TutorialCustomCodeWidget::Setup)); +} + +void KTutorialEditor::setTutorialTearDown() { + showEditionDialog(new TutorialCustomCodeWidget(mTutorial, + TutorialCustomCodeWidget::TearDown)); +} + void KTutorialEditor::addStep() { Step* step = new Step(); mTutorial->addStep(step); @@ -139,6 +186,16 @@ showEditionDialog(new StepDataWidget(mCurrentStep)); } +void KTutorialEditor::setStepSetup() { + showEditionDialog(new StepCustomCodeWidget(mCurrentStep, + StepCustomCodeWidget::Setup)); +} + +void KTutorialEditor::setStepTearDown() { + showEditionDialog(new StepCustomCodeWidget(mCurrentStep, + StepCustomCodeWidget::TearDown)); +} + void KTutorialEditor::removeStep() { Q_ASSERT(mCurrentStep); Modified: trunk/ktutorial/ktutorial-editor/src/KTutorialEditor.h =================================================================== --- trunk/ktutorial/ktutorial-editor/src/KTutorialEditor.h 2010-03-09 16:14:46 UTC (rev 134) +++ trunk/ktutorial/ktutorial-editor/src/KTutorialEditor.h 2010-03-09 19:16:53 UTC (rev 135) @@ -91,6 +91,16 @@ void setTutorialInformation(); /** + * Shows a TutorialCustomCodeWidget for the setup code of the tutorial. + */ + void setTutorialSetup(); + + /** + * Shows a TutorialCustomCodeWidget for the tear down code of the tutorial. + */ + void setTutorialTearDown(); + + /** * Adds a new step to the tutorial and shows a StepDataWidget for it. */ void addStep(); @@ -101,6 +111,16 @@ void setStepData(); /** + * Shows a StepCustomCodeWidget for the setup code of the current step. + */ + void setStepSetup(); + + /** + * Shows a StepCustomCodeWidget for the tear down code of the current step. + */ + void setStepTearDown(); + + /** * Removes the current step from the tutorial. */ void removeStep(); Modified: trunk/ktutorial/ktutorial-editor/src/ktutorial-editorui.rc =================================================================== --- trunk/ktutorial/ktutorial-editor/src/ktutorial-editorui.rc 2010-03-09 16:14:46 UTC (rev 134) +++ trunk/ktutorial/ktutorial-editor/src/ktutorial-editorui.rc 2010-03-09 19:16:53 UTC (rev 135) @@ -6,11 +6,15 @@ <Menu name="editTutorials"> <Text context="@item:inmenu">Tutorial</Text> <Action name="setTutorialInformation"/> + <Action name="setTutorialSetup"/> + <Action name="setTutorialTearDown"/> </Menu> <Menu name="editSteps"> <Text context="@item:inmenu">Step</Text> <Action name="addStep"/> <Action name="setStepData"/> + <Action name="setStepSetup"/> + <Action name="setStepTearDown"/> <Action name="removeStep"/> </Menu> </Menu> Modified: trunk/ktutorial/ktutorial-editor/src/view/CMakeLists.txt =================================================================== --- trunk/ktutorial/ktutorial-editor/src/view/CMakeLists.txt 2010-03-09 16:14:46 UTC (rev 134) +++ trunk/ktutorial/ktutorial-editor/src/view/CMakeLists.txt 2010-03-09 19:16:53 UTC (rev 135) @@ -3,18 +3,21 @@ set(ktutorial_editor_view_SRCS EditionDialog.cpp EditionWidget.cpp + StepCustomCodeWidget.cpp StepDataWidget.cpp StepTreeItem.cpp TextTreeItem.cpp TreeItem.cpp TreeItemUtil.cpp TreeModel.cpp + TutorialCustomCodeWidget.cpp TutorialInformationWidget.cpp TutorialTreeItem.cpp TutorialTreeSelectionManager.cpp ) kde4_add_ui_files(ktutorial_editor_view_SRCS + CustomCodeWidget.ui StepDataWidget.ui TutorialInformationWidget.ui ) Added: trunk/ktutorial/ktutorial-editor/src/view/CustomCodeWidget.ui =================================================================== --- trunk/ktutorial/ktutorial-editor/src/view/CustomCodeWidget.ui (rev 0) +++ trunk/ktutorial/ktutorial-editor/src/view/CustomCodeWidget.ui 2010-03-09 19:16:53 UTC (rev 135) @@ -0,0 +1,37 @@ +<?xml version="1.0" encoding="UTF-8"?> +<ui version="4.0"> + <class>CustomCodeWidget</class> + <widget class="QWidget" name="CustomCodeWidget"> + <property name="geometry"> + <rect> + <x>0</x> + <y>0</y> + <width>400</width> + <height>300</height> + </rect> + </property> + <property name="windowTitle"> + <string comment="@title">Set custom code</string> + </property> + <layout class="QVBoxLayout" name="CustomCodeWidgetLayout"> + <item> + <widget class="QGroupBox" name="customCodeGroupBox"> + <layout class="QVBoxLayout" name="customCodeGroupBoxLayout"> + <item> + <widget class="KTextEdit" name="customCodeTextEdit"/> + </item> + </layout> + </widget> + </item> + </layout> + </widget> + <customwidgets> + <customwidget> + <class>KTextEdit</class> + <extends>QTextEdit</extends> + <header>ktextedit.h</header> + </customwidget> + </customwidgets> + <resources/> + <connections/> +</ui> Added: trunk/ktutorial/ktutorial-editor/src/view/StepCustomCodeWidget.cpp =================================================================== --- trunk/ktutorial/ktutorial-editor/src/view/StepCustomCodeWidget.cpp (rev 0) +++ trunk/ktutorial/ktutorial-editor/src/view/StepCustomCodeWidget.cpp 2010-03-09 19:16:53 UTC (rev 135) @@ -0,0 +1,74 @@ +/*************************************************************************** + * 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 "StepCustomCodeWidget.h" + +#include <KLocalizedString> + +#include "ui_CustomCodeWidget.h" +#include "../Step.h" + +//public: + +StepCustomCodeWidget::StepCustomCodeWidget(Step* step, Type type, + QWidget* parent): + EditionWidget(parent), + mStep(step), + mType(type) { + + ui = new Ui::CustomCodeWidget(); + ui->setupUi(this); + + if (type == Setup) { + ui->customCodeTextEdit->setText(step->customSetupCode()); + ui->customCodeGroupBox->setTitle(i18nc("@ŧitle", "Step setup code")); + setWindowTitle(i18nc("@title", "Set the step setup code")); + setWhatsThis(i18nc("@info:whatsthis", "<para>Set the code to be " +"executed when the tutorials passes to the step.</para>" +"<para>The code will be written as is to the setup of the step, after the " +"setup code generated automatically by the editor. This means that the code " +"must be written in the same programming language the tutorial will be " +"exported to.</para>")); + } else { + ui->customCodeTextEdit->setText(step->customTearDownCode()); + ui->customCodeGroupBox->setTitle(i18nc("@ŧitle", + "Step tear down code")); + setWindowTitle(i18nc("@title", "Set the step tear down code")); + setWhatsThis(i18nc("@info:whatsthis", "<para>Set the code to be " +"executed when the tutorial finishes.</para>" +"<para>The code will be written as is to the tear down of the step, before the " +"tear down code generated automatically by the editor. This means that the " +"code must be written in the same programming language the tutorial will be " +"exported to.</para>")); + } +} + +StepCustomCodeWidget::~StepCustomCodeWidget() { + delete ui; +} + +void StepCustomCodeWidget::saveChanges() { + QString customCode = ui->customCodeTextEdit->toPlainText(); + if (mType == Setup && mStep->customSetupCode() != customCode) { + mStep->setCustomSetupCode(customCode); + } + + if (mType == TearDown && mStep->customTearDownCode() != customCode) { + mStep->setCustomTearDownCode(customCode); + } +} Property changes on: trunk/ktutorial/ktutorial-editor/src/view/StepCustomCodeWidget.cpp ___________________________________________________________________ Added: svn:eol-style + native Added: trunk/ktutorial/ktutorial-editor/src/view/StepCustomCodeWidget.h =================================================================== --- trunk/ktutorial/ktutorial-editor/src/view/StepCustomCodeWidget.h (rev 0) +++ trunk/ktutorial/ktutorial-editor/src/view/StepCustomCodeWidget.h 2010-03-09 19:16:53 UTC (rev 135) @@ -0,0 +1,83 @@ +/*************************************************************************** + * 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 STEPCUSTOMCODEWIDGET_H +#define STEPCUSTOMCODEWIDGET_H + +#include "EditionWidget.h" + +class Step; + +namespace Ui { +class CustomCodeWidget; +} + +/** + * Edition widget for the setup and tear down code of a Step. + * The code that is edited (setup or tear down) is specified when the widget + * is created. + */ +class StepCustomCodeWidget: public EditionWidget { +Q_OBJECT +public: + + enum Type { + Setup, + TearDown + }; + + /** + * Creates a new StepCustomCodeWidget for the given type of code of the + * given step. + * + * @param step The step to edit its code. + * @param type The type of code to edit. + * @param parent The parent QWidget. + */ + StepCustomCodeWidget(Step* step, Type type, QWidget* parent = 0); + + /** + * Destroys this widget. + */ + virtual ~StepCustomCodeWidget(); + + /** + * Saves the code in the step. + */ + virtual void saveChanges(); + +private: + + /** + * The step to set its code. + */ + Step* mStep; + + /** + * The type of custom code to edit. + */ + Type mType; + + /** + * The Ui Designer generated class. + */ + Ui::CustomCodeWidget* ui; + +}; + +#endif Property changes on: trunk/ktutorial/ktutorial-editor/src/view/StepCustomCodeWidget.h ___________________________________________________________________ Added: svn:eol-style + native Added: trunk/ktutorial/ktutorial-editor/src/view/TutorialCustomCodeWidget.cpp =================================================================== --- trunk/ktutorial/ktutorial-editor/src/view/TutorialCustomCodeWidget.cpp (rev 0) +++ trunk/ktutorial/ktutorial-editor/src/view/TutorialCustomCodeWidget.cpp 2010-03-09 19:16:53 UTC (rev 135) @@ -0,0 +1,75 @@ +/*************************************************************************** + * 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 "TutorialCustomCodeWidget.h" + +#include <KLocalizedString> + +#include "ui_CustomCodeWidget.h" +#include "../Tutorial.h" + +//public: + +TutorialCustomCodeWidget::TutorialCustomCodeWidget(Tutorial* tutorial, + Type type, QWidget* parent): + EditionWidget(parent), + mTutorial(tutorial), + mType(type) { + + ui = new Ui::CustomCodeWidget(); + ui->setupUi(this); + + if (type == Setup) { + ui->customCodeTextEdit->setText(tutorial->customSetupCode()); + ui->customCodeGroupBox->setTitle(i18nc("@ŧitle", + "Tutorial setup code")); + setWindowTitle(i18nc("@title", "Set the tutorial setup code")); + setWhatsThis(i18nc("@info:whatsthis", "<para>Set the code to be " +"executed when the tutorial starts.</para>" +"<para>The code will be written as is to the setup of the tutorial, after the " +"setup code generated automatically by the editor. This means that the code " +"must be written in the same programming language the tutorial will be " +"exported to.</para>")); + } else { + ui->customCodeTextEdit->setText(tutorial->customTearDownCode()); + ui->customCodeGroupBox->setTitle(i18nc("@ŧitle", + "Tutorial tear down code")); + setWindowTitle(i18nc("@title", "Set the tutorial tear down code")); + setWhatsThis(i18nc("@info:whatsthis", "<para>Set the code to be " +"executed when the tutorial finishes.</para>" +"<para>The code will be written as is to the tear down of the tutorial, before " +"the tear down code generated automatically by the editor. This means that the " +"code must be written in the same programming language the tutorial will be " +"exported to.</para>")); + } +} + +TutorialCustomCodeWidget::~TutorialCustomCodeWidget() { + delete ui; +} + +void TutorialCustomCodeWidget::saveChanges() { + QString customCode = ui->customCodeTextEdit->toPlainText(); + if (mType == Setup && mTutorial->customSetupCode() != customCode) { + mTutorial->setCustomSetupCode(customCode); + } + + if (mType == TearDown && mTutorial->customTearDownCode() != customCode) { + mTutorial->setCustomTearDownCode(customCode); + } +} Property changes on: trunk/ktutorial/ktutorial-editor/src/view/TutorialCustomCodeWidget.cpp ___________________________________________________________________ Added: svn:eol-style + native Added: trunk/ktutorial/ktutorial-editor/src/view/TutorialCustomCodeWidget.h =================================================================== --- trunk/ktutorial/ktutorial-editor/src/view/TutorialCustomCodeWidget.h (rev 0) +++ trunk/ktutorial/ktutorial-editor/src/view/TutorialCustomCodeWidget.h 2010-03-09 19:16:53 UTC (rev 135) @@ -0,0 +1,84 @@ +/*************************************************************************** + * 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 TUTORIALCUSTOMCODEWIDGET_H +#define TUTORIALCUSTOMCODEWIDGET_H + +#include "EditionWidget.h" + +class Tutorial; + +namespace Ui { +class CustomCodeWidget; +} + +/** + * Edition widget for the setup and tear down code of a Tutorial. + * The code that is edited (setup or tear down) is specified when the widget + * is created. + */ +class TutorialCustomCodeWidget: public EditionWidget { +Q_OBJECT +public: + + enum Type { + Setup, + TearDown + }; + + /** + * Creates a new TutorialCustomCodeWidget for the given type of code of the + * given tutorial. + * + * @param tutorial The tutorial to edit its code. + * @param type The type of code to edit. + * @param parent The parent QWidget. + */ + TutorialCustomCodeWidget(Tutorial* tutorial, Type type, + QWidget* parent = 0); + + /** + * Destroys this widget. + */ + virtual ~TutorialCustomCodeWidget(); + + /** + * Saves the code in the tutorial. + */ + virtual void saveChanges(); + +private: + + /** + * The tutorial to set its code. + */ + Tutorial* mTutorial; + + /** + * The type of custom code to edit. + */ + Type mType; + + /** + * The Ui Designer generated class. + */ + Ui::CustomCodeWidget* ui; + +}; + +#endif Property changes on: trunk/ktutorial/ktutorial-editor/src/view/TutorialCustomCodeWidget.h ___________________________________________________________________ Added: svn:eol-style + native Modified: trunk/ktutorial/ktutorial-editor/tests/unit/view/CMakeLists.txt =================================================================== --- trunk/ktutorial/ktutorial-editor/tests/unit/view/CMakeLists.txt 2010-03-09 16:14:46 UTC (rev 134) +++ trunk/ktutorial/ktutorial-editor/tests/unit/view/CMakeLists.txt 2010-03-09 19:16:53 UTC (rev 135) @@ -16,7 +16,7 @@ ENDFOREACH(_className) ENDMACRO(UNIT_TESTS) -unit_tests(EditionDialog StepDataWidget StepTreeItem TextTreeItem TreeItem TreeItemUtil TreeModel TutorialInformationWidget TutorialTreeItem TutorialTreeSelectionManager) +unit_tests(EditionDialog StepCustomCodeWidget StepDataWidget StepTreeItem TextTreeItem TreeItem TreeItemUtil TreeModel TutorialCustomCodeWidget TutorialInformationWidget TutorialTreeItem TutorialTreeSelectionManager) MACRO(MEM_TESTS) FOREACH(_testname ${ARGN}) @@ -24,4 +24,4 @@ ENDFOREACH(_testname) ENDMACRO(MEM_TESTS) -mem_tests(EditionDialog StepDataWidget StepTreeItem TextTreeItem TreeItem TreeItemUtil TreeModel TutorialInformationWidget TutorialTreeItem TutorialTreeSelectionManager) +mem_tests(EditionDialog StepCustomCodeWidget StepDataWidget StepTreeItem TextTreeItem TreeItem TreeItemUtil TreeModel TutorialCustomCodeWidget TutorialInformationWidget TutorialTreeItem TutorialTreeSelectionManager) Added: trunk/ktutorial/ktutorial-editor/tests/unit/view/StepCustomCodeWidgetTest.cpp =================================================================== --- trunk/ktutorial/ktutorial-editor/tests/unit/view/StepCustomCodeWidgetTest.cpp (rev 0) +++ trunk/ktutorial/ktutorial-editor/tests/unit/view/StepCustomCodeWidgetTest.cpp 2010-03-09 19:16:53 UTC (rev 135) @@ -0,0 +1,108 @@ +/*************************************************************************** + * 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 <QtTest> + +#include "StepCustomCodeWidget.h" + +#include <KLineEdit> +#include <KTextEdit> + +#include "../Step.h" + +class StepCustomCodeWidgetTest: public QObject { +Q_OBJECT + +private slots: + + void testConstructorSetup(); + void testConstructorTearDown(); + + void testSaveChangesSetup(); + void testSaveChangesTearDown(); + +private: + + KTextEdit* customCodeTextEdit(StepCustomCodeWidget* widget) const; + +}; + +void StepCustomCodeWidgetTest::testConstructorSetup() { + Step step; + step.setCustomSetupCode("The setup code"); + + QWidget parent; + StepCustomCodeWidget* widget = + new StepCustomCodeWidget(&step, + StepCustomCodeWidget::Setup, + &parent); + + QCOMPARE(widget->parentWidget(), &parent); + QCOMPARE(customCodeTextEdit(widget)->toPlainText(), + QString("The setup code")); +} + +void StepCustomCodeWidgetTest::testConstructorTearDown() { + Step step; + step.setCustomTearDownCode("The tear down code"); + + QWidget parent; + StepCustomCodeWidget* widget = + new StepCustomCodeWidget(&step, + StepCustomCodeWidget::TearDown, + &parent); + + QCOMPARE(widget->parentWidget(), &parent); + QCOMPARE(customCodeTextEdit(widget)->toPlainText(), + QString("The tear down code")); +} + +void StepCustomCodeWidgetTest::testSaveChangesSetup() { + Step step; + step.setCustomSetupCode("The setup code"); + + StepCustomCodeWidget widget(&step, StepCustomCodeWidget::Setup); + customCodeTextEdit(&widget)->setText("The new setup code"); + + widget.saveChanges(); + + QCOMPARE(step.customSetupCode(), QString("The new setup code")); +} + +void StepCustomCodeWidgetTest::testSaveChangesTearDown() { + Step step; + step.setCustomTearDownCode("The tear down code"); + + StepCustomCodeWidget widget(&step, StepCustomCodeWidget::TearDown); + customCodeTextEdit(&widget)->setText("The new tear down code"); + + widget.saveChanges(); + + QCOMPARE(step.customTearDownCode(), QString("The new tear down code")); +} + +/////////////////////////////////// Helpers //////////////////////////////////// + +KTextEdit* StepCustomCodeWidgetTest::customCodeTextEdit( + StepCustomCodeWidget* widget) const { + return widget->findChild<KTextEdit*>("customCodeTextEdit"); +} + +QTEST_MAIN(StepCustomCodeWidgetTest) + +#include "StepCustomCodeWidgetTest.moc" Property changes on: trunk/ktutorial/ktutorial-editor/tests/unit/view/StepCustomCodeWidgetTest.cpp ___________________________________________________________________ Added: svn:eol-style + native Added: trunk/ktutorial/ktutorial-editor/tests/unit/view/TutorialCustomCodeWidgetTest.cpp =================================================================== --- trunk/ktutorial/ktutorial-editor/tests/unit/view/TutorialCustomCodeWidgetTest.cpp (rev 0) +++ trunk/ktutorial/ktutorial-editor/tests/unit/view/TutorialCustomCodeWidgetTest.cpp 2010-03-09 19:16:53 UTC (rev 135) @@ -0,0 +1,109 @@ +/*************************************************************************** + * 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 <QtTest> + +#include "TutorialCustomCodeWidget.h" + +#include <KLineEdit> +#include <KTextEdit> + +#include "../Tutorial.h" + +class TutorialCustomCodeWidgetTest: public QObject { +Q_OBJECT + +private slots: + + void testConstructorSetup(); + void testConstructorTearDown(); + + void testSaveChangesSetup(); + void testSaveChangesTearDown(); + +private: + + KTextEdit* customCodeTextEdit(TutorialCustomCodeWidget* widget) const; + +}; + +void TutorialCustomCodeWidgetTest::testConstructorSetup() { + Tutorial tutorial; + tutorial.setCustomSetupCode("The setup code"); + + QWidget parent; + TutorialCustomCodeWidget* widget = + new TutorialCustomCodeWidget(&tutorial, + TutorialCustomCodeWidget::Setup, + &parent); + + QCOMPARE(widget->parentWidget(), &parent); + QCOMPARE(customCodeTextEdit(widget)->toPlainText(), + QString("The setup code")); +} + +void TutorialCustomCodeWidgetTest::testConstructorTearDown() { + Tutorial tutorial; + tutorial.setCustomTearDownCode("The tear down code"); + + QWidget parent; + TutorialCustomCodeWidget* widget = + new TutorialCustomCodeWidget(&tutorial, + TutorialCustomCodeWidget::TearDown, + &parent); + + QCOMPARE(widget->parentWidget(), &parent); + QCOMPARE(customCodeTextEdit(widget)->toPlainText(), + QString("The tear down code")); +} + +void TutorialCustomCodeWidgetTest::testSaveChangesSetup() { + Tutorial tutorial; + tutorial.setCustomSetupCode("The setup code"); + + TutorialCustomCodeWidget widget(&tutorial, TutorialCustomCodeWidget::Setup); + customCodeTextEdit(&widget)->setText("The new setup code"); + + widget.saveChanges(); + + QCOMPARE(tutorial.customSetupCode(), QString("The new setup code")); +} + +void TutorialCustomCodeWidgetTest::testSaveChangesTearDown() { + Tutorial tutorial; + tutorial.setCustomTearDownCode("The tear down code"); + + TutorialCustomCodeWidget widget(&tutorial, + TutorialCustomCodeWidget::TearDown); + customCodeTextEdit(&widget)->setText("The new tear down code"); + + widget.saveChanges(); + + QCOMPARE(tutorial.customTearDownCode(), QString("The new tear down code")); +} + +/////////////////////////////////// Helpers //////////////////////////////////// + +KTextEdit* TutorialCustomCodeWidgetTest::customCodeTextEdit( + TutorialCustomCodeWidget* widget) const { + return widget->findChild<KTextEdit*>("customCodeTextEdit"); +} + +QTEST_MAIN(TutorialCustomCodeWidgetTest) + +#include "TutorialCustomCodeWidgetTest.moc" Property changes on: trunk/ktutorial/ktutorial-editor/tests/unit/view/TutorialCustomCodeWidgetTest.cpp ___________________________________________________________________ Added: svn:eol-style + native This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <dan...@us...> - 2010-03-09 16:14:52
|
Revision: 134 http://ktutorial.svn.sourceforge.net/ktutorial/?rev=134&view=rev Author: danxuliu Date: 2010-03-09 16:14:46 +0000 (Tue, 09 Mar 2010) Log Message: ----------- -Remove superflous Whats this help in labels, as the widget Whats this is clearer enough. -Use a new line for each paragraph in Whats this text (doesn't change the way it is shown, but it is clearer for edition). Modified Paths: -------------- trunk/ktutorial/ktutorial-editor/src/view/TutorialInformationWidget.ui Modified: trunk/ktutorial/ktutorial-editor/src/view/TutorialInformationWidget.ui =================================================================== --- trunk/ktutorial/ktutorial-editor/src/view/TutorialInformationWidget.ui 2010-03-09 16:12:12 UTC (rev 133) +++ trunk/ktutorial/ktutorial-editor/src/view/TutorialInformationWidget.ui 2010-03-09 16:14:46 UTC (rev 134) @@ -14,7 +14,8 @@ <string comment="@title">Set tutorial information</string> </property> <property name="whatsThis"> - <string comment="@info:whatsthis"><para>Set the name and description of the tutorial.</para><para>The name and the description are shown in the dialog where the tutorial to be started is selected.</para></string> + <string comment="@info:whatsthis"><para>Set the name and description of the tutorial.</para> +<para>The name and the description are shown in the dialog where the tutorial to be started is selected.</para></string> </property> <layout class="QVBoxLayout" name="tutorialInformationWidgetLayout"> <item> @@ -27,9 +28,6 @@ <layout class="QHBoxLayout" name="nameLayout"> <item> <widget class="QLabel" name="nameLabel"> - <property name="whatsThis"> - <string comment="@info:whatsthis">The name to set in the tutorial.</string> - </property> <property name="text"> <string comment="@label:textbox">Name:</string> </property> @@ -47,9 +45,6 @@ <layout class="QVBoxLayout" name="descriptionLayout"> <item> <widget class="QLabel" name="descriptionLabel"> - <property name="whatsThis"> - <string comment="@info:whatsthis">The description to set in the tutorial.</string> - </property> <property name="text"> <string comment="@label:textbox">Description:</string> </property> This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <dan...@us...> - 2010-03-09 16:12:22
|
Revision: 133 http://ktutorial.svn.sourceforge.net/ktutorial/?rev=133&view=rev Author: danxuliu Date: 2010-03-09 16:12:12 +0000 (Tue, 09 Mar 2010) Log Message: ----------- Rewrite the Whats this help of StepDataWidget for better clarity. Modified Paths: -------------- trunk/ktutorial/ktutorial-editor/src/view/StepDataWidget.ui Modified: trunk/ktutorial/ktutorial-editor/src/view/StepDataWidget.ui =================================================================== --- trunk/ktutorial/ktutorial-editor/src/view/StepDataWidget.ui 2010-03-09 08:35:33 UTC (rev 132) +++ trunk/ktutorial/ktutorial-editor/src/view/StepDataWidget.ui 2010-03-09 16:12:12 UTC (rev 133) @@ -16,7 +16,7 @@ <property name="whatsThis"> <string comment="@info:whatsthis"><para>Set the id and the text of the step</para> <para>The text is shown to the user when the tutorial changes to that step.</para> -<para>On the other hand, the id is only used internally and never shown to the user. It is used to tell the tutorial which is the next step to change to.</para></string> +<para>On the other hand, the id is only used internally and never shown to the user. It is used to identify the steps in the tutorial when it has to change from one to step to another.</para></string> </property> <layout class="QVBoxLayout" name="StepDataWidgetLayout"> <item> This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <dan...@us...> - 2010-03-09 08:35:39
|
Revision: 132 http://ktutorial.svn.sourceforge.net/ktutorial/?rev=132&view=rev Author: danxuliu Date: 2010-03-09 08:35:33 +0000 (Tue, 09 Mar 2010) Log Message: ----------- Set context data to menu name Modified Paths: -------------- trunk/ktutorial/ktutorial-editor/src/ktutorial-editorui.rc Modified: trunk/ktutorial/ktutorial-editor/src/ktutorial-editorui.rc =================================================================== --- trunk/ktutorial/ktutorial-editor/src/ktutorial-editorui.rc 2010-03-09 08:05:23 UTC (rev 131) +++ trunk/ktutorial/ktutorial-editor/src/ktutorial-editorui.rc 2010-03-09 08:35:33 UTC (rev 132) @@ -4,11 +4,11 @@ <MenuBar> <Menu name="edit"> <Menu name="editTutorials"> - <Text>Tutorial</Text> + <Text context="@item:inmenu">Tutorial</Text> <Action name="setTutorialInformation"/> </Menu> <Menu name="editSteps"> - <Text>Step</Text> + <Text context="@item:inmenu">Step</Text> <Action name="addStep"/> <Action name="setStepData"/> <Action name="removeStep"/> This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <dan...@us...> - 2010-03-09 08:05:31
|
Revision: 131 http://ktutorial.svn.sourceforge.net/ktutorial/?rev=131&view=rev Author: danxuliu Date: 2010-03-09 08:05:23 +0000 (Tue, 09 Mar 2010) Log Message: ----------- -Add actions to set the tutorial information, the step data, add steps and remove steps. -Enable or disable some actions based on the selected step. Modified Paths: -------------- trunk/ktutorial/ktutorial-editor/src/KTutorialEditor.cpp trunk/ktutorial/ktutorial-editor/src/KTutorialEditor.h trunk/ktutorial/ktutorial-editor/src/ktutorial-editorui.rc Modified: trunk/ktutorial/ktutorial-editor/src/KTutorialEditor.cpp =================================================================== --- trunk/ktutorial/ktutorial-editor/src/KTutorialEditor.cpp 2010-03-09 05:25:28 UTC (rev 130) +++ trunk/ktutorial/ktutorial-editor/src/KTutorialEditor.cpp 2010-03-09 08:05:23 UTC (rev 131) @@ -23,24 +23,129 @@ #include <KAction> #include <KActionCollection> #include <KApplication> +#include <KLocalizedString> +#include "Step.h" +#include "Tutorial.h" +#include "view/EditionDialog.h" +#include "view/StepDataWidget.h" +#include "view/TreeModel.h" +#include "view/TutorialInformationWidget.h" +#include "view/TutorialTreeItem.h" +#include "view/TutorialTreeSelectionManager.h" + //public: KTutorialEditor::KTutorialEditor(): KXmlGuiWindow(0) { - QTreeView* treeView = new QTreeView(); - treeView->setObjectName("centralTreeView"); - treeView->setHeaderHidden(true); + mTreeView = new QTreeView(); + mTreeView->setObjectName("centralTreeView"); + setCentralWidget(mTreeView); - setCentralWidget(treeView); + setupTutorialToBeEdited(); setupActions(); - //Don't use a status bar (which is setupGUI default behavior) - setupGUI(ToolBar | Keys | Save | Create); + setupGUI(); } //private: +void KTutorialEditor::setupTutorialToBeEdited() { + mTutorial = new Tutorial(this); + + TutorialTreeItem* tutorialTreeItem = new TutorialTreeItem(mTutorial); + TreeModel* model = new TreeModel(tutorialTreeItem, mTreeView); + + QAbstractItemModel* oldModel = mTreeView->model(); + mTreeView->setModel(model); + delete oldModel; + + TutorialTreeSelectionManager* selectionManager = + new TutorialTreeSelectionManager(mTreeView->selectionModel(), this); + connect(selectionManager, SIGNAL(stepSelected(Step*)), + this, SLOT(selectStep(Step*))); +} + void KTutorialEditor::setupActions() { KStandardAction::quit(kapp, SLOT(quit()), actionCollection()); + + KAction* action = new KAction(this); + action->setText(i18nc("@action", "Set information...")); + action->setStatusTip(i18nc("@info:status", "Set the name and description " +"of the tutorial.")); + actionCollection()->addAction("setTutorialInformation", action); + connect(action, SIGNAL(triggered(bool)), + this, SLOT(setTutorialInformation())); + + action = new KAction(this); + action->setText(i18nc("@action", "Add step...")); + action->setStatusTip(i18nc("@info:status", "Add a new step to the " +"tutorial.")); + actionCollection()->addAction("addStep", action); + connect(action, SIGNAL(triggered(bool)), this, SLOT(addStep())); + + action = new KAction(this); + action->setText(i18nc("@action", "Set data...")); + action->setStatusTip(i18nc("@info:status", "Set the name and text of the " +"currently selected step.")); + action->setEnabled(false); + actionCollection()->addAction("setStepData", action); + connect(action, SIGNAL(triggered(bool)), this, SLOT(setStepData())); + + action = new KAction(this); + action->setText(i18nc("@action", "Remove step")); + action->setStatusTip(i18nc("@info:status", "Removes the currently selected " +"step from the tutorial.")); + action->setEnabled(false); + actionCollection()->addAction("removeStep", action); + connect(action, SIGNAL(triggered(bool)), this, SLOT(removeStep())); } + +void KTutorialEditor::showEditionDialog(EditionWidget* editionWidget) { + EditionDialog* dialog = new EditionDialog(editionWidget, this); + dialog->setObjectName("editionDialog"); + dialog->exec(); + dialog->deleteLater(); +} + +//private slots: + +void KTutorialEditor::selectStep(Step* step) { + mCurrentStep = step; + + if (mCurrentStep) { + actionCollection()->action("setStepData")->setEnabled(true); + actionCollection()->action("removeStep")->setEnabled(true); + } else { + actionCollection()->action("setStepData")->setEnabled(false); + actionCollection()->action("removeStep")->setEnabled(false); + } +} + +void KTutorialEditor::setTutorialInformation() { + showEditionDialog(new TutorialInformationWidget(mTutorial)); +} + +void KTutorialEditor::addStep() { + Step* step = new Step(); + mTutorial->addStep(step); + + showEditionDialog(new StepDataWidget(step)); +} + +void KTutorialEditor::setStepData() { + Q_ASSERT(mCurrentStep); + + showEditionDialog(new StepDataWidget(mCurrentStep)); +} + +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(); +} Modified: trunk/ktutorial/ktutorial-editor/src/KTutorialEditor.h =================================================================== --- trunk/ktutorial/ktutorial-editor/src/KTutorialEditor.h 2010-03-09 05:25:28 UTC (rev 130) +++ trunk/ktutorial/ktutorial-editor/src/KTutorialEditor.h 2010-03-09 08:05:23 UTC (rev 131) @@ -21,6 +21,11 @@ #include <KXmlGuiWindow> +class EditionWidget; +class QTreeView; +class Step; +class Tutorial; + /** * KTutorial editor main window. * It wires up all the components in the application. @@ -37,10 +42,69 @@ private: /** + * The main tree view that shows the tutorial. + */ + QTreeView* mTreeView; + + /** + * The tutorial being edited. + */ + Tutorial* mTutorial; + + /** + * The currently selected step. + */ + Step* mCurrentStep; + + /** + * Sets up the tutorial to be edited. + * It creates a new tutorial, prepares the tree view to represent it and + * handles the selection of items. + */ + void setupTutorialToBeEdited(); + + /** * Sets up all the actions used in the application. */ void setupActions(); + /** + * Shows an EditionDialog for the given EditionWidget. + * + * @param editionWidget The EditionWidget to wrap. + */ + void showEditionDialog(EditionWidget* editionWidget); + +private Q_SLOTS: + + /** + * Sets the current step and enables or disables the actions that depend on + * a step as needed. + * + * @param step The step to select, or null to deselect the current one. + */ + void selectStep(Step* step); + + /** + * Shows a TutorialInformationWidget for the tutorial. + */ + void setTutorialInformation(); + + /** + * Adds a new step to the tutorial and shows a StepDataWidget for it. + */ + void addStep(); + + /** + * Shows a StepDataWidget for the current step. + */ + void setStepData(); + + /** + * Removes the current step from the tutorial. + */ + void removeStep(); + }; #endif Modified: trunk/ktutorial/ktutorial-editor/src/ktutorial-editorui.rc =================================================================== --- trunk/ktutorial/ktutorial-editor/src/ktutorial-editorui.rc 2010-03-09 05:25:28 UTC (rev 130) +++ trunk/ktutorial/ktutorial-editor/src/ktutorial-editorui.rc 2010-03-09 08:05:23 UTC (rev 131) @@ -1,4 +1,18 @@ <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE kpartgui SYSTEM "kpartgui.dtd"> <gui name="ktutorial-editor" version="1"> + <MenuBar> + <Menu name="edit"> + <Menu name="editTutorials"> + <Text>Tutorial</Text> + <Action name="setTutorialInformation"/> + </Menu> + <Menu name="editSteps"> + <Text>Step</Text> + <Action name="addStep"/> + <Action name="setStepData"/> + <Action name="removeStep"/> + </Menu> + </Menu> + </MenuBar> </gui> This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <dan...@us...> - 2010-03-09 05:25:35
|
Revision: 130 http://ktutorial.svn.sourceforge.net/ktutorial/?rev=130&view=rev Author: danxuliu Date: 2010-03-09 05:25:28 +0000 (Tue, 09 Mar 2010) Log Message: ----------- Add StepDataWidget to edit the id and text of a step. Modified Paths: -------------- trunk/ktutorial/ktutorial-editor/src/view/CMakeLists.txt trunk/ktutorial/ktutorial-editor/tests/unit/view/CMakeLists.txt Added Paths: ----------- trunk/ktutorial/ktutorial-editor/src/view/StepDataWidget.cpp trunk/ktutorial/ktutorial-editor/src/view/StepDataWidget.h trunk/ktutorial/ktutorial-editor/src/view/StepDataWidget.ui trunk/ktutorial/ktutorial-editor/tests/unit/view/StepDataWidgetTest.cpp Modified: trunk/ktutorial/ktutorial-editor/src/view/CMakeLists.txt =================================================================== --- trunk/ktutorial/ktutorial-editor/src/view/CMakeLists.txt 2010-03-09 04:53:34 UTC (rev 129) +++ trunk/ktutorial/ktutorial-editor/src/view/CMakeLists.txt 2010-03-09 05:25:28 UTC (rev 130) @@ -3,6 +3,7 @@ set(ktutorial_editor_view_SRCS EditionDialog.cpp EditionWidget.cpp + StepDataWidget.cpp StepTreeItem.cpp TextTreeItem.cpp TreeItem.cpp @@ -14,6 +15,7 @@ ) kde4_add_ui_files(ktutorial_editor_view_SRCS + StepDataWidget.ui TutorialInformationWidget.ui ) Added: trunk/ktutorial/ktutorial-editor/src/view/StepDataWidget.cpp =================================================================== --- trunk/ktutorial/ktutorial-editor/src/view/StepDataWidget.cpp (rev 0) +++ trunk/ktutorial/ktutorial-editor/src/view/StepDataWidget.cpp 2010-03-09 05:25:28 UTC (rev 130) @@ -0,0 +1,50 @@ +/*************************************************************************** + * 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 "StepDataWidget.h" + +#include "ui_StepDataWidget.h" +#include "../Step.h" + +//public: + +StepDataWidget::StepDataWidget(Step* step, QWidget* parent): + EditionWidget(parent), + mStep(step) { + ui = new Ui::StepDataWidget(); + ui->setupUi(this); + + ui->idLineEdit->setText(step->id()); + ui->textTextEdit->setText(step->text()); +} + +StepDataWidget::~StepDataWidget() { + delete ui; +} + +void StepDataWidget::saveChanges() { + QString id = ui->idLineEdit->text(); + if (mStep->id() != id) { + mStep->setId(id); + } + + QString text = ui->textTextEdit->toPlainText(); + if (mStep->text() != text) { + mStep->setText(text); + } +} Property changes on: trunk/ktutorial/ktutorial-editor/src/view/StepDataWidget.cpp ___________________________________________________________________ Added: svn:eol-style + native Added: trunk/ktutorial/ktutorial-editor/src/view/StepDataWidget.h =================================================================== --- trunk/ktutorial/ktutorial-editor/src/view/StepDataWidget.h (rev 0) +++ trunk/ktutorial/ktutorial-editor/src/view/StepDataWidget.h 2010-03-09 05:25:28 UTC (rev 130) @@ -0,0 +1,69 @@ +/*************************************************************************** + * 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 STEPDATAWIDGET_H +#define STEPDATAWIDGET_H + +#include "EditionWidget.h" + +class Step; + +namespace Ui { +class StepDataWidget; +} + +/** + * Edition widget for the id and text of a Step. + */ +class StepDataWidget: public EditionWidget { +Q_OBJECT +public: + + /** + * Creates a new StepDataWidget for the given Step. + * + * @param step The step to edit. + * @param parent The parent QWidget. + */ + explicit StepDataWidget(Step* step, QWidget* parent = 0); + + /** + * Destroys this widget. + */ + virtual ~StepDataWidget(); + + /** + * Saves the id and text in the step. + */ + virtual void saveChanges(); + +private: + + /** + * The step to edit. + */ + Step* mStep; + + /** + * The Ui Designer generated class. + */ + Ui::StepDataWidget* ui; + +}; + +#endif Property changes on: trunk/ktutorial/ktutorial-editor/src/view/StepDataWidget.h ___________________________________________________________________ Added: svn:eol-style + native Added: trunk/ktutorial/ktutorial-editor/src/view/StepDataWidget.ui =================================================================== --- trunk/ktutorial/ktutorial-editor/src/view/StepDataWidget.ui (rev 0) +++ trunk/ktutorial/ktutorial-editor/src/view/StepDataWidget.ui 2010-03-09 05:25:28 UTC (rev 130) @@ -0,0 +1,81 @@ +<?xml version="1.0" encoding="UTF-8"?> +<ui version="4.0"> + <class>StepDataWidget</class> + <widget class="QWidget" name="StepDataWidget"> + <property name="geometry"> + <rect> + <x>0</x> + <y>0</y> + <width>400</width> + <height>300</height> + </rect> + </property> + <property name="windowTitle"> + <string comment="@title">Set step data</string> + </property> + <property name="whatsThis"> + <string comment="@info:whatsthis"><para>Set the id and the text of the step</para> +<para>The text is shown to the user when the tutorial changes to that step.</para> +<para>On the other hand, the id is only used internally and never shown to the user. It is used to tell the tutorial which is the next step to change to.</para></string> + </property> + <layout class="QVBoxLayout" name="StepDataWidgetLayout"> + <item> + <widget class="QGroupBox" name="stepDataGroupBox"> + <property name="title"> + <string comment="@title">Step data</string> + </property> + <layout class="QVBoxLayout" name="stepDataGroupBoxLayout"> + <item> + <layout class="QHBoxLayout" name="idLayout"> + <item> + <widget class="QLabel" name="idLabel"> + <property name="text"> + <string comment="@label:textbox">Id:</string> + </property> + <property name="buddy"> + <cstring>idLineEdit</cstring> + </property> + </widget> + </item> + <item> + <widget class="KLineEdit" name="idLineEdit"/> + </item> + </layout> + </item> + <item> + <layout class="QVBoxLayout" name="textLayout"> + <item> + <widget class="QLabel" name="textLabel"> + <property name="text"> + <string comment="@label:textbox">Text:</string> + </property> + <property name="buddy"> + <cstring>textTextEdit</cstring> + </property> + </widget> + </item> + <item> + <widget class="KTextEdit" name="textTextEdit"/> + </item> + </layout> + </item> + </layout> + </widget> + </item> + </layout> + </widget> + <customwidgets> + <customwidget> + <class>KLineEdit</class> + <extends>QLineEdit</extends> + <header>klineedit.h</header> + </customwidget> + <customwidget> + <class>KTextEdit</class> + <extends>QTextEdit</extends> + <header>ktextedit.h</header> + </customwidget> + </customwidgets> + <resources/> + <connections/> +</ui> Modified: trunk/ktutorial/ktutorial-editor/tests/unit/view/CMakeLists.txt =================================================================== --- trunk/ktutorial/ktutorial-editor/tests/unit/view/CMakeLists.txt 2010-03-09 04:53:34 UTC (rev 129) +++ trunk/ktutorial/ktutorial-editor/tests/unit/view/CMakeLists.txt 2010-03-09 05:25:28 UTC (rev 130) @@ -16,7 +16,7 @@ ENDFOREACH(_className) ENDMACRO(UNIT_TESTS) -unit_tests(EditionDialog StepTreeItem TextTreeItem TreeItem TreeItemUtil TreeModel TutorialInformationWidget TutorialTreeItem TutorialTreeSelectionManager) +unit_tests(EditionDialog StepDataWidget StepTreeItem TextTreeItem TreeItem TreeItemUtil TreeModel TutorialInformationWidget TutorialTreeItem TutorialTreeSelectionManager) MACRO(MEM_TESTS) FOREACH(_testname ${ARGN}) @@ -24,4 +24,4 @@ ENDFOREACH(_testname) ENDMACRO(MEM_TESTS) -mem_tests(EditionDialog StepTreeItem TextTreeItem TreeItem TreeItemUtil TreeModel TutorialInformationWidget TutorialTreeItem TutorialTreeSelectionManager) +mem_tests(EditionDialog StepDataWidget StepTreeItem TextTreeItem TreeItem TreeItemUtil TreeModel TutorialInformationWidget TutorialTreeItem TutorialTreeSelectionManager) Added: trunk/ktutorial/ktutorial-editor/tests/unit/view/StepDataWidgetTest.cpp =================================================================== --- trunk/ktutorial/ktutorial-editor/tests/unit/view/StepDataWidgetTest.cpp (rev 0) +++ trunk/ktutorial/ktutorial-editor/tests/unit/view/StepDataWidgetTest.cpp 2010-03-09 05:25:28 UTC (rev 130) @@ -0,0 +1,84 @@ +/*************************************************************************** + * 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 <QtTest> + +#include "StepDataWidget.h" + +#include <KLineEdit> +#include <KTextEdit> + +#include "../Step.h" + +class StepDataWidgetTest: public QObject { +Q_OBJECT + +private slots: + + void testConstructor(); + + void testSaveChanges(); + +private: + + KLineEdit* idLineEdit(StepDataWidget* widget) const; + KTextEdit* textTextEdit(StepDataWidget* widget) const; + +}; + +void StepDataWidgetTest::testConstructor() { + Step step; + step.setId("The id"); + step.setText("The text"); + + QWidget parent; + StepDataWidget* widget = new StepDataWidget(&step, &parent); + + QCOMPARE(widget->parentWidget(), &parent); + QCOMPARE(idLineEdit(widget)->text(), QString("The id")); + QCOMPARE(textTextEdit(widget)->toPlainText(), QString("The text")); +} + +void StepDataWidgetTest::testSaveChanges() { + Step step; + step.setId("The id"); + step.setText("The text"); + + StepDataWidget widget(&step); + idLineEdit(&widget)->setText("The new id"); + textTextEdit(&widget)->setText("The new text"); + + widget.saveChanges(); + + QCOMPARE(step.id(), QString("The new id")); + QCOMPARE(step.text(), QString("The new text")); +} + +/////////////////////////////////// Helpers //////////////////////////////////// + +KLineEdit* StepDataWidgetTest::idLineEdit(StepDataWidget* widget) const { + return widget->findChild<KLineEdit*>("idLineEdit"); +} + +KTextEdit* StepDataWidgetTest::textTextEdit(StepDataWidget* widget) const { + return widget->findChild<KTextEdit*>("textTextEdit"); +} + +QTEST_MAIN(StepDataWidgetTest) + +#include "StepDataWidgetTest.moc" Property changes on: trunk/ktutorial/ktutorial-editor/tests/unit/view/StepDataWidgetTest.cpp ___________________________________________________________________ Added: svn:eol-style + native This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <dan...@us...> - 2010-03-09 04:53:40
|
Revision: 129 http://ktutorial.svn.sourceforge.net/ktutorial/?rev=129&view=rev Author: danxuliu Date: 2010-03-09 04:53:34 +0000 (Tue, 09 Mar 2010) Log Message: ----------- Fix wrong class/object name for the TutorialInformationWidget Designer generated class. Modified Paths: -------------- trunk/ktutorial/ktutorial-editor/src/view/TutorialInformationWidget.cpp trunk/ktutorial/ktutorial-editor/src/view/TutorialInformationWidget.h trunk/ktutorial/ktutorial-editor/src/view/TutorialInformationWidget.ui Modified: trunk/ktutorial/ktutorial-editor/src/view/TutorialInformationWidget.cpp =================================================================== --- trunk/ktutorial/ktutorial-editor/src/view/TutorialInformationWidget.cpp 2010-03-09 04:33:47 UTC (rev 128) +++ trunk/ktutorial/ktutorial-editor/src/view/TutorialInformationWidget.cpp 2010-03-09 04:53:34 UTC (rev 129) @@ -27,7 +27,7 @@ QWidget* parent): EditionWidget(parent), mTutorial(tutorial) { - ui = new Ui::TutorialInformationWidgetUi(); + ui = new Ui::TutorialInformationWidget(); ui->setupUi(this); ui->nameLineEdit->setText(tutorial->name()); Modified: trunk/ktutorial/ktutorial-editor/src/view/TutorialInformationWidget.h =================================================================== --- trunk/ktutorial/ktutorial-editor/src/view/TutorialInformationWidget.h 2010-03-09 04:33:47 UTC (rev 128) +++ trunk/ktutorial/ktutorial-editor/src/view/TutorialInformationWidget.h 2010-03-09 04:53:34 UTC (rev 129) @@ -24,7 +24,7 @@ class Tutorial; namespace Ui { -class TutorialInformationWidgetUi; +class TutorialInformationWidget; } /** @@ -62,7 +62,7 @@ /** * The Ui Designer generated class. */ - Ui::TutorialInformationWidgetUi* ui; + Ui::TutorialInformationWidget* ui; }; Modified: trunk/ktutorial/ktutorial-editor/src/view/TutorialInformationWidget.ui =================================================================== --- trunk/ktutorial/ktutorial-editor/src/view/TutorialInformationWidget.ui 2010-03-09 04:33:47 UTC (rev 128) +++ trunk/ktutorial/ktutorial-editor/src/view/TutorialInformationWidget.ui 2010-03-09 04:53:34 UTC (rev 129) @@ -1,7 +1,7 @@ <?xml version="1.0" encoding="UTF-8"?> <ui version="4.0"> - <class>TutorialInformationWidgetUi</class> - <widget class="QWidget" name="TutorialInformationWidgetUi"> + <class>TutorialInformationWidget</class> + <widget class="QWidget" name="TutorialInformationWidget"> <property name="geometry"> <rect> <x>0</x> @@ -16,7 +16,7 @@ <property name="whatsThis"> <string comment="@info:whatsthis"><para>Set the name and description of the tutorial.</para><para>The name and the description are shown in the dialog where the tutorial to be started is selected.</para></string> </property> - <layout class="QVBoxLayout" name="tutorialInformationWidgetUiLayout"> + <layout class="QVBoxLayout" name="tutorialInformationWidgetLayout"> <item> <widget class="QGroupBox" name="tutorialInformationGroupBox"> <property name="title"> This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <dan...@us...> - 2010-03-09 04:33:54
|
Revision: 128 http://ktutorial.svn.sourceforge.net/ktutorial/?rev=128&view=rev Author: danxuliu Date: 2010-03-09 04:33:47 +0000 (Tue, 09 Mar 2010) Log Message: ----------- Add TutorialInformationWidget to edit the name and description of a tutorial. Modified Paths: -------------- trunk/ktutorial/ktutorial-editor/src/view/CMakeLists.txt trunk/ktutorial/ktutorial-editor/tests/unit/view/CMakeLists.txt Added Paths: ----------- trunk/ktutorial/ktutorial-editor/src/view/TutorialInformationWidget.cpp trunk/ktutorial/ktutorial-editor/src/view/TutorialInformationWidget.h trunk/ktutorial/ktutorial-editor/src/view/TutorialInformationWidget.ui trunk/ktutorial/ktutorial-editor/tests/unit/view/TutorialInformationWidgetTest.cpp Modified: trunk/ktutorial/ktutorial-editor/src/view/CMakeLists.txt =================================================================== --- trunk/ktutorial/ktutorial-editor/src/view/CMakeLists.txt 2010-03-09 03:41:25 UTC (rev 127) +++ trunk/ktutorial/ktutorial-editor/src/view/CMakeLists.txt 2010-03-09 04:33:47 UTC (rev 128) @@ -8,10 +8,15 @@ TreeItem.cpp TreeItemUtil.cpp TreeModel.cpp + TutorialInformationWidget.cpp TutorialTreeItem.cpp TutorialTreeSelectionManager.cpp ) +kde4_add_ui_files(ktutorial_editor_view_SRCS + TutorialInformationWidget.ui +) + kde4_add_library(ktutorial_editor_view ${ktutorial_editor_view_SRCS}) target_link_libraries(ktutorial_editor_view ktutorial_editor ${KDE4_KDEUI_LIBS}) Added: trunk/ktutorial/ktutorial-editor/src/view/TutorialInformationWidget.cpp =================================================================== --- trunk/ktutorial/ktutorial-editor/src/view/TutorialInformationWidget.cpp (rev 0) +++ trunk/ktutorial/ktutorial-editor/src/view/TutorialInformationWidget.cpp 2010-03-09 04:33:47 UTC (rev 128) @@ -0,0 +1,51 @@ +/*************************************************************************** + * 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 "TutorialInformationWidget.h" + +#include "ui_TutorialInformationWidget.h" +#include "../Tutorial.h" + +//public: + +TutorialInformationWidget::TutorialInformationWidget(Tutorial* tutorial, + QWidget* parent): + EditionWidget(parent), + mTutorial(tutorial) { + ui = new Ui::TutorialInformationWidgetUi(); + ui->setupUi(this); + + ui->nameLineEdit->setText(tutorial->name()); + ui->descriptionTextEdit->setText(tutorial->description()); +} + +TutorialInformationWidget::~TutorialInformationWidget() { + delete ui; +} + +void TutorialInformationWidget::saveChanges() { + QString name = ui->nameLineEdit->text(); + if (mTutorial->name() != name) { + mTutorial->setName(name); + } + + QString description = ui->descriptionTextEdit->toPlainText(); + if (mTutorial->description() != description) { + mTutorial->setDescription(description); + } +} Property changes on: trunk/ktutorial/ktutorial-editor/src/view/TutorialInformationWidget.cpp ___________________________________________________________________ Added: svn:eol-style + native Added: trunk/ktutorial/ktutorial-editor/src/view/TutorialInformationWidget.h =================================================================== --- trunk/ktutorial/ktutorial-editor/src/view/TutorialInformationWidget.h (rev 0) +++ trunk/ktutorial/ktutorial-editor/src/view/TutorialInformationWidget.h 2010-03-09 04:33:47 UTC (rev 128) @@ -0,0 +1,69 @@ +/*************************************************************************** + * 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 TUTORIALINFORMATIONWIDGET_H +#define TUTORIALINFORMATIONWIDGET_H + +#include "EditionWidget.h" + +class Tutorial; + +namespace Ui { +class TutorialInformationWidgetUi; +} + +/** + * Edition widget for the name and description of a Tutorial. + */ +class TutorialInformationWidget: public EditionWidget { +Q_OBJECT +public: + + /** + * Creates a new TutorialInformationWidget for the given Tutorial. + * + * @param tutorial The tutorial to edit. + * @param parent The parent QWidget. + */ + explicit TutorialInformationWidget(Tutorial* tutorial, QWidget* parent = 0); + + /** + * Destroys this widget. + */ + virtual ~TutorialInformationWidget(); + + /** + * Saves the name and description in the tutorial. + */ + virtual void saveChanges(); + +private: + + /** + * The tutorial to edit. + */ + Tutorial* mTutorial; + + /** + * The Ui Designer generated class. + */ + Ui::TutorialInformationWidgetUi* ui; + +}; + +#endif Property changes on: trunk/ktutorial/ktutorial-editor/src/view/TutorialInformationWidget.h ___________________________________________________________________ Added: svn:eol-style + native Added: trunk/ktutorial/ktutorial-editor/src/view/TutorialInformationWidget.ui =================================================================== --- trunk/ktutorial/ktutorial-editor/src/view/TutorialInformationWidget.ui (rev 0) +++ trunk/ktutorial/ktutorial-editor/src/view/TutorialInformationWidget.ui 2010-03-09 04:33:47 UTC (rev 128) @@ -0,0 +1,85 @@ +<?xml version="1.0" encoding="UTF-8"?> +<ui version="4.0"> + <class>TutorialInformationWidgetUi</class> + <widget class="QWidget" name="TutorialInformationWidgetUi"> + <property name="geometry"> + <rect> + <x>0</x> + <y>0</y> + <width>400</width> + <height>300</height> + </rect> + </property> + <property name="windowTitle"> + <string comment="@title">Set tutorial information</string> + </property> + <property name="whatsThis"> + <string comment="@info:whatsthis"><para>Set the name and description of the tutorial.</para><para>The name and the description are shown in the dialog where the tutorial to be started is selected.</para></string> + </property> + <layout class="QVBoxLayout" name="tutorialInformationWidgetUiLayout"> + <item> + <widget class="QGroupBox" name="tutorialInformationGroupBox"> + <property name="title"> + <string comment="@title">Tutorial information</string> + </property> + <layout class="QVBoxLayout" name="tutorialInformationGroupBoxLayout"> + <item> + <layout class="QHBoxLayout" name="nameLayout"> + <item> + <widget class="QLabel" name="nameLabel"> + <property name="whatsThis"> + <string comment="@info:whatsthis">The name to set in the tutorial.</string> + </property> + <property name="text"> + <string comment="@label:textbox">Name:</string> + </property> + <property name="buddy"> + <cstring>nameLineEdit</cstring> + </property> + </widget> + </item> + <item> + <widget class="KLineEdit" name="nameLineEdit"/> + </item> + </layout> + </item> + <item> + <layout class="QVBoxLayout" name="descriptionLayout"> + <item> + <widget class="QLabel" name="descriptionLabel"> + <property name="whatsThis"> + <string comment="@info:whatsthis">The description to set in the tutorial.</string> + </property> + <property name="text"> + <string comment="@label:textbox">Description:</string> + </property> + <property name="buddy"> + <cstring>descriptionTextEdit</cstring> + </property> + </widget> + </item> + <item> + <widget class="KTextEdit" name="descriptionTextEdit"/> + </item> + </layout> + </item> + </layout> + </widget> + </item> + </layout> + </widget> + <customwidgets> + <customwidget> + <class>KLineEdit</class> + <extends>QLineEdit</extends> + <header>klineedit.h</header> + </customwidget> + <customwidget> + <class>KTextEdit</class> + <extends>QTextEdit</extends> + <header>ktextedit.h</header> + </customwidget> + </customwidgets> + <resources/> + <connections/> +</ui> Modified: trunk/ktutorial/ktutorial-editor/tests/unit/view/CMakeLists.txt =================================================================== --- trunk/ktutorial/ktutorial-editor/tests/unit/view/CMakeLists.txt 2010-03-09 03:41:25 UTC (rev 127) +++ trunk/ktutorial/ktutorial-editor/tests/unit/view/CMakeLists.txt 2010-03-09 04:33:47 UTC (rev 128) @@ -16,7 +16,7 @@ ENDFOREACH(_className) ENDMACRO(UNIT_TESTS) -unit_tests(EditionDialog StepTreeItem TextTreeItem TreeItem TreeItemUtil TreeModel TutorialTreeItem TutorialTreeSelectionManager) +unit_tests(EditionDialog StepTreeItem TextTreeItem TreeItem TreeItemUtil TreeModel TutorialInformationWidget TutorialTreeItem TutorialTreeSelectionManager) MACRO(MEM_TESTS) FOREACH(_testname ${ARGN}) @@ -24,4 +24,4 @@ ENDFOREACH(_testname) ENDMACRO(MEM_TESTS) -mem_tests(EditionDialog StepTreeItem TextTreeItem TreeItem TreeItemUtil TreeModel TutorialTreeItem TutorialTreeSelectionManager) +mem_tests(EditionDialog StepTreeItem TextTreeItem TreeItem TreeItemUtil TreeModel TutorialInformationWidget TutorialTreeItem TutorialTreeSelectionManager) Added: trunk/ktutorial/ktutorial-editor/tests/unit/view/TutorialInformationWidgetTest.cpp =================================================================== --- trunk/ktutorial/ktutorial-editor/tests/unit/view/TutorialInformationWidgetTest.cpp (rev 0) +++ trunk/ktutorial/ktutorial-editor/tests/unit/view/TutorialInformationWidgetTest.cpp 2010-03-09 04:33:47 UTC (rev 128) @@ -0,0 +1,88 @@ +/*************************************************************************** + * 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 <QtTest> + +#include "TutorialInformationWidget.h" + +#include <KLineEdit> +#include <KTextEdit> + +#include "../Tutorial.h" + +class TutorialInformationWidgetTest: public QObject { +Q_OBJECT + +private slots: + + void testConstructor(); + + void testSaveChanges(); + +private: + + KLineEdit* nameLineEdit(TutorialInformationWidget* widget) const; + KTextEdit* descriptionTextEdit(TutorialInformationWidget* widget) const; + +}; + +void TutorialInformationWidgetTest::testConstructor() { + Tutorial tutorial; + tutorial.setName("The name"); + tutorial.setDescription("The description"); + + QWidget parent; + TutorialInformationWidget* widget = new TutorialInformationWidget(&tutorial, + &parent); + + QCOMPARE(widget->parentWidget(), &parent); + QCOMPARE(nameLineEdit(widget)->text(), QString("The name")); + QCOMPARE(descriptionTextEdit(widget)->toPlainText(), + QString("The description")); +} + +void TutorialInformationWidgetTest::testSaveChanges() { + Tutorial tutorial; + tutorial.setName("The name"); + tutorial.setDescription("The description"); + + TutorialInformationWidget widget(&tutorial); + nameLineEdit(&widget)->setText("The new name"); + descriptionTextEdit(&widget)->setText("The new description"); + + widget.saveChanges(); + + QCOMPARE(tutorial.name(), QString("The new name")); + QCOMPARE(tutorial.description(), QString("The new description")); +} + +/////////////////////////////////// Helpers //////////////////////////////////// + +KLineEdit* TutorialInformationWidgetTest::nameLineEdit( + TutorialInformationWidget* widget) const { + return widget->findChild<KLineEdit*>("nameLineEdit"); +} + +KTextEdit* TutorialInformationWidgetTest::descriptionTextEdit( + TutorialInformationWidget* widget) const { + return widget->findChild<KTextEdit*>("descriptionTextEdit"); +} + +QTEST_MAIN(TutorialInformationWidgetTest) + +#include "TutorialInformationWidgetTest.moc" Property changes on: trunk/ktutorial/ktutorial-editor/tests/unit/view/TutorialInformationWidgetTest.cpp ___________________________________________________________________ Added: svn:eol-style + native This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <dan...@us...> - 2010-03-09 03:41:31
|
Revision: 127 http://ktutorial.svn.sourceforge.net/ktutorial/?rev=127&view=rev Author: danxuliu Date: 2010-03-09 03:41:25 +0000 (Tue, 09 Mar 2010) Log Message: ----------- Add parent argument to TutorialTreeSelectionManager constructor. Modified Paths: -------------- trunk/ktutorial/ktutorial-editor/src/view/TutorialTreeSelectionManager.cpp trunk/ktutorial/ktutorial-editor/src/view/TutorialTreeSelectionManager.h trunk/ktutorial/ktutorial-editor/tests/unit/view/TutorialTreeSelectionManagerTest.cpp Modified: trunk/ktutorial/ktutorial-editor/src/view/TutorialTreeSelectionManager.cpp =================================================================== --- trunk/ktutorial/ktutorial-editor/src/view/TutorialTreeSelectionManager.cpp 2010-03-09 03:01:27 UTC (rev 126) +++ trunk/ktutorial/ktutorial-editor/src/view/TutorialTreeSelectionManager.cpp 2010-03-09 03:41:25 UTC (rev 127) @@ -24,7 +24,8 @@ //public: TutorialTreeSelectionManager::TutorialTreeSelectionManager( - QItemSelectionModel* itemSelectionModel) { + QItemSelectionModel* itemSelectionModel, + QObject* parent): QObject(parent) { connect(itemSelectionModel, SIGNAL(selectionChanged(QItemSelection, QItemSelection)), this, Modified: trunk/ktutorial/ktutorial-editor/src/view/TutorialTreeSelectionManager.h =================================================================== --- trunk/ktutorial/ktutorial-editor/src/view/TutorialTreeSelectionManager.h 2010-03-09 03:01:27 UTC (rev 126) +++ trunk/ktutorial/ktutorial-editor/src/view/TutorialTreeSelectionManager.h 2010-03-09 03:41:25 UTC (rev 127) @@ -42,8 +42,10 @@ * * @param itemSelectionModel The selection model to watch for changes in the * selection. + * @param parent The parent object. */ - TutorialTreeSelectionManager(QItemSelectionModel* itemSelectionModel); + TutorialTreeSelectionManager(QItemSelectionModel* itemSelectionModel, + QObject* parent = 0); Q_SIGNALS: Modified: trunk/ktutorial/ktutorial-editor/tests/unit/view/TutorialTreeSelectionManagerTest.cpp =================================================================== --- trunk/ktutorial/ktutorial-editor/tests/unit/view/TutorialTreeSelectionManagerTest.cpp 2010-03-09 03:01:27 UTC (rev 126) +++ trunk/ktutorial/ktutorial-editor/tests/unit/view/TutorialTreeSelectionManagerTest.cpp 2010-03-09 03:41:25 UTC (rev 127) @@ -35,6 +35,8 @@ void cleanup(); void cleanupTestCase(); + void testConstructor(); + void testSelectTutorialOrChildren(); void testSelectStepOrChildrenChangingToOtherItems(); @@ -107,6 +109,14 @@ delete mTutorial; } +void TutorialTreeSelectionManagerTest::testConstructor() { + QObject parent; + TutorialTreeSelectionManager* selectionManager = + new TutorialTreeSelectionManager(mSelectionModel, &parent); + + QCOMPARE(selectionManager->parent(), &parent); +} + void TutorialTreeSelectionManagerTest::testSelectTutorialOrChildren() { //Name select(mTreeModel->index(0, 0)); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <dan...@us...> - 2010-03-09 03:01:33
|
Revision: 126 http://ktutorial.svn.sourceforge.net/ktutorial/?rev=126&view=rev Author: danxuliu Date: 2010-03-09 03:01:27 +0000 (Tue, 09 Mar 2010) Log Message: ----------- Fix header data update in TreeModel. Modified Paths: -------------- trunk/ktutorial/ktutorial-editor/src/view/TreeModel.cpp trunk/ktutorial/ktutorial-editor/tests/unit/view/TreeModelTest.cpp Modified: trunk/ktutorial/ktutorial-editor/src/view/TreeModel.cpp =================================================================== --- trunk/ktutorial/ktutorial-editor/src/view/TreeModel.cpp 2010-03-09 02:18:24 UTC (rev 125) +++ trunk/ktutorial/ktutorial-editor/src/view/TreeModel.cpp 2010-03-09 03:01:27 UTC (rev 126) @@ -188,6 +188,11 @@ } void TreeModel::treeItemDataChanged(TreeItem* item) { + if (item == mRootItem) { + emit headerDataChanged(Qt::Horizontal, 0, 0); + return; + } + QModelIndex index = createIndex(item->childIndex(), 0, item); emit dataChanged(index, index); Modified: trunk/ktutorial/ktutorial-editor/tests/unit/view/TreeModelTest.cpp =================================================================== --- trunk/ktutorial/ktutorial-editor/tests/unit/view/TreeModelTest.cpp 2010-03-09 02:18:24 UTC (rev 125) +++ trunk/ktutorial/ktutorial-editor/tests/unit/view/TreeModelTest.cpp 2010-03-09 03:01:27 UTC (rev 126) @@ -49,6 +49,7 @@ void testChangeChildrenOfRemovedNestedItem(); void testChangeItemData(); + void testChangeRootItemData(); void testDataWithInvalidIndex(); void testDataWithInvalidRole(); @@ -516,6 +517,37 @@ QCOMPARE(qvariant_cast<QModelIndex>(argument), index); } +//Qt::Orientation must be declared as a metatype to be used in qvariant_cast +Q_DECLARE_METATYPE(Qt::Orientation); + +void TreeModelTest::testChangeRootItemData() { + StubTreeItem* rootItem = new StubTreeItem("root"); + TreeModel model(rootItem); + + //Qt::Orientation must be registered in order to be used with QSignalSpy + int orientationIndexType = qRegisterMetaType<Qt::Orientation>("Qt::Orientation"); + QSignalSpy headerDataChangedSpy(&model, + SIGNAL(headerDataChanged(Qt::Orientation, int, int))); + + rootItem->setText("root modified"); + + QCOMPARE(model.rowCount(), 0); + QCOMPARE(model.columnCount(), 1); + QCOMPARE(model.headerData(0, Qt::Horizontal).toString(), + QString("root modified")); + + QCOMPARE(headerDataChangedSpy.count(), 1); + QVariant argument = headerDataChangedSpy.at(0).at(0); + QCOMPARE(argument.userType(), orientationIndexType); + QCOMPARE(qvariant_cast<Qt::Orientation>(argument), Qt::Horizontal); + argument = headerDataChangedSpy.at(0).at(1); + QCOMPARE(argument.type(), QVariant::Int); + QCOMPARE(argument.toInt(), 0); + argument = headerDataChangedSpy.at(0).at(2); + QCOMPARE(argument.type(), QVariant::Int); + QCOMPARE(argument.toInt(), 0); +} + void TreeModelTest::testDataWithInvalidIndex() { TreeModel model(mSingleItem); mSingleItem = 0; This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <dan...@us...> - 2010-03-09 02:18:31
|
Revision: 125 http://ktutorial.svn.sourceforge.net/ktutorial/?rev=125&view=rev Author: danxuliu Date: 2010-03-09 02:18:24 +0000 (Tue, 09 Mar 2010) Log Message: ----------- -Added EditionWidget as base class for widgets that edit data in a tutorial. -Added EditionDialog class that wraps an EditionWidget and provides "Ok" and "Cancel" buttons. Modified Paths: -------------- trunk/ktutorial/ktutorial-editor/src/view/CMakeLists.txt trunk/ktutorial/ktutorial-editor/tests/unit/view/CMakeLists.txt Added Paths: ----------- trunk/ktutorial/ktutorial-editor/src/view/EditionDialog.cpp trunk/ktutorial/ktutorial-editor/src/view/EditionDialog.h trunk/ktutorial/ktutorial-editor/src/view/EditionWidget.cpp trunk/ktutorial/ktutorial-editor/src/view/EditionWidget.h trunk/ktutorial/ktutorial-editor/tests/unit/view/EditionDialogTest.cpp Modified: trunk/ktutorial/ktutorial-editor/src/view/CMakeLists.txt =================================================================== --- trunk/ktutorial/ktutorial-editor/src/view/CMakeLists.txt 2010-03-08 18:47:18 UTC (rev 124) +++ trunk/ktutorial/ktutorial-editor/src/view/CMakeLists.txt 2010-03-09 02:18:24 UTC (rev 125) @@ -1,6 +1,8 @@ include_directories(${KDE4_INCLUDES}) set(ktutorial_editor_view_SRCS + EditionDialog.cpp + EditionWidget.cpp StepTreeItem.cpp TextTreeItem.cpp TreeItem.cpp Added: trunk/ktutorial/ktutorial-editor/src/view/EditionDialog.cpp =================================================================== --- trunk/ktutorial/ktutorial-editor/src/view/EditionDialog.cpp (rev 0) +++ trunk/ktutorial/ktutorial-editor/src/view/EditionDialog.cpp 2010-03-09 02:18:24 UTC (rev 125) @@ -0,0 +1,49 @@ +/*************************************************************************** + * 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 "EditionDialog.h" + +#include <KLocalizedString> + +#include "EditionWidget.h" + +//public: + +EditionDialog::EditionDialog(EditionWidget* editionWidget, QWidget* parent): + KDialog(parent), + mEditionWidget(editionWidget) { + + editionWidget->setParent(this); + + setMainWidget(editionWidget); + setWindowTitle(editionWidget->windowTitle()); + + setModal(true); + + setButtons(KDialog::Ok | KDialog::Cancel); +} + +//protected slots: + +void EditionDialog::slotButtonClicked(int button) { + if (button == KDialog::Ok) { + mEditionWidget->saveChanges(); + } + + KDialog::slotButtonClicked(button); +} Property changes on: trunk/ktutorial/ktutorial-editor/src/view/EditionDialog.cpp ___________________________________________________________________ Added: svn:executable + * Added: svn:eol-style + native Added: trunk/ktutorial/ktutorial-editor/src/view/EditionDialog.h =================================================================== --- trunk/ktutorial/ktutorial-editor/src/view/EditionDialog.h (rev 0) +++ trunk/ktutorial/ktutorial-editor/src/view/EditionDialog.h 2010-03-09 02:18:24 UTC (rev 125) @@ -0,0 +1,69 @@ +/*************************************************************************** + * 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 EDITIONDIALOG_H +#define EDITIONDIALOG_H + +#include <KDialog> + +class EditionWidget; + +/** + * Wrapper dialog for EditionWidgets that provides Ok and Cancel buttons. + * When the user clicks the "Ok" button, the changes are saved to the object + * being edited by the EditionWidget. + * + * By default, the dialog is modal and gets its window title from the edition + * widget own window title. + * + * @see EditionWidgdet + */ +class EditionDialog: public KDialog { +Q_OBJECT +public: + + /** + * Creates and shows a new TutorialManagerDialog. + * The EditionWidget is reparented to this dialog and destroyed when this + * EditionDialog is destroyed. + * + * @param editionWidget The EditionWidget to wrap. + * @param parent The parent widget of this dialog, defaults to null. + */ + explicit EditionDialog(EditionWidget* editionWidget, QWidget* parent = 0); + +protected Q_SLOTS: + + /** + * Hides the dialog, saving the changes if the button clicked was "Ok" + * button. + * + * @param button The button clicked. + */ + virtual void slotButtonClicked(int button); + +private: + + /** + * The EditionWidget wrapped. + */ + EditionWidget* mEditionWidget; + +}; + +#endif Property changes on: trunk/ktutorial/ktutorial-editor/src/view/EditionDialog.h ___________________________________________________________________ Added: svn:executable + * Added: svn:eol-style + native Added: trunk/ktutorial/ktutorial-editor/src/view/EditionWidget.cpp =================================================================== --- trunk/ktutorial/ktutorial-editor/src/view/EditionWidget.cpp (rev 0) +++ trunk/ktutorial/ktutorial-editor/src/view/EditionWidget.cpp 2010-03-09 02:18:24 UTC (rev 125) @@ -0,0 +1,24 @@ +/*************************************************************************** + * 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 "EditionWidget.h" + +//public: + +EditionWidget::EditionWidget(QWidget* parent): QWidget(parent) { +} Property changes on: trunk/ktutorial/ktutorial-editor/src/view/EditionWidget.cpp ___________________________________________________________________ Added: svn:eol-style + native Added: trunk/ktutorial/ktutorial-editor/src/view/EditionWidget.h =================================================================== --- trunk/ktutorial/ktutorial-editor/src/view/EditionWidget.h (rev 0) +++ trunk/ktutorial/ktutorial-editor/src/view/EditionWidget.h 2010-03-09 02:18:24 UTC (rev 125) @@ -0,0 +1,55 @@ +/*************************************************************************** + * 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 EDITIONWIDGET_H +#define EDITIONWIDGET_H + +#include <QWidget> + +/** + * Base abstract class for edition widgets. + * Widgets mean to edit any data in the tutorial must inherit from this class. + * A generic edition dialog with Ok and Cancel buttons is provided by + * EditionDialog class. The dialog wraps an EditionWidget and saves the changes + * using saveChanges() method when needed. + * + * Subclasses must implement saveChanges method to commit the changes to the + * object being edited. + * + * @see EditionDialog + */ +class EditionWidget: public QWidget { +Q_OBJECT +public: + + /** + * Creates a new EditionWidget with the given parent. + * + * @param parent The parent widget. + */ + EditionWidget(QWidget* parent = 0); + + /** + * Saves the changes to the object being edited. + * This method must be implemented in subclasses. + */ + virtual void saveChanges() = 0; + +}; + +#endif Property changes on: trunk/ktutorial/ktutorial-editor/src/view/EditionWidget.h ___________________________________________________________________ Added: svn:eol-style + native Modified: trunk/ktutorial/ktutorial-editor/tests/unit/view/CMakeLists.txt =================================================================== --- trunk/ktutorial/ktutorial-editor/tests/unit/view/CMakeLists.txt 2010-03-08 18:47:18 UTC (rev 124) +++ trunk/ktutorial/ktutorial-editor/tests/unit/view/CMakeLists.txt 2010-03-09 02:18:24 UTC (rev 125) @@ -16,7 +16,7 @@ ENDFOREACH(_className) ENDMACRO(UNIT_TESTS) -unit_tests(StepTreeItem TextTreeItem TreeItem TreeItemUtil TreeModel TutorialTreeItem TutorialTreeSelectionManager) +unit_tests(EditionDialog StepTreeItem TextTreeItem TreeItem TreeItemUtil TreeModel TutorialTreeItem TutorialTreeSelectionManager) MACRO(MEM_TESTS) FOREACH(_testname ${ARGN}) @@ -24,4 +24,4 @@ ENDFOREACH(_testname) ENDMACRO(MEM_TESTS) -mem_tests(StepTreeItem TextTreeItem TreeItem TreeItemUtil TreeModel TutorialTreeItem TutorialTreeSelectionManager) +mem_tests(EditionDialog StepTreeItem TextTreeItem TreeItem TreeItemUtil TreeModel TutorialTreeItem TutorialTreeSelectionManager) Added: trunk/ktutorial/ktutorial-editor/tests/unit/view/EditionDialogTest.cpp =================================================================== --- trunk/ktutorial/ktutorial-editor/tests/unit/view/EditionDialogTest.cpp (rev 0) +++ trunk/ktutorial/ktutorial-editor/tests/unit/view/EditionDialogTest.cpp 2010-03-09 02:18:24 UTC (rev 125) @@ -0,0 +1,91 @@ +/*************************************************************************** + * 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 <QtTest> + +#include "EditionDialog.h" + +#include <KPushButton> + +#include "EditionWidget.h" + +class EditionDialogTest: public QObject { +Q_OBJECT + +private slots: + + void testConstructor(); + + void testOk(); + void testCancel(); + +}; + +class MockEditionWidget: public EditionWidget { +public: + + int mSaveChangesCallCount; + + MockEditionWidget(QWidget* parent = 0): EditionWidget(parent), + mSaveChangesCallCount(0) { + setWindowTitle("The edition widget title"); + } + + virtual void saveChanges() { + mSaveChangesCallCount++; + } + +}; + +void EditionDialogTest::testConstructor() { + EditionWidget* editionWidget = new MockEditionWidget(); + QWidget parentWidget; + EditionDialog* dialog = new EditionDialog(editionWidget, &parentWidget); + + QCOMPARE(dialog->mainWidget(), editionWidget); + QCOMPARE(dialog->mainWidget()->parent(), dialog); + QCOMPARE(dialog->parentWidget(), &parentWidget); + QVERIFY(dialog->isModal()); + QCOMPARE(dialog->windowTitle(), QString("The edition widget title")); + QVERIFY(dialog->button(KDialog::Ok)); + QVERIFY(dialog->button(KDialog::Cancel)); +} + +void EditionDialogTest::testOk() { + MockEditionWidget* editionWidget = new MockEditionWidget(); + EditionDialog dialog(editionWidget); + + dialog.button(KDialog::Ok)->click(); + + QCOMPARE(editionWidget->mSaveChangesCallCount, 1); + QVERIFY(dialog.isHidden()); +} + +void EditionDialogTest::testCancel() { + MockEditionWidget* editionWidget = new MockEditionWidget(); + EditionDialog dialog(editionWidget); + + dialog.button(KDialog::Cancel)->click(); + + QCOMPARE(editionWidget->mSaveChangesCallCount, 0); + QVERIFY(dialog.isHidden()); +} + +QTEST_MAIN(EditionDialogTest) + +#include "EditionDialogTest.moc" Property changes on: trunk/ktutorial/ktutorial-editor/tests/unit/view/EditionDialogTest.cpp ___________________________________________________________________ Added: svn:eol-style + native This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <dan...@us...> - 2010-03-08 18:47:25
|
Revision: 124 http://ktutorial.svn.sourceforge.net/ktutorial/?rev=124&view=rev Author: danxuliu Date: 2010-03-08 18:47:18 +0000 (Mon, 08 Mar 2010) Log Message: ----------- Fix application title. Modified Paths: -------------- trunk/ktutorial/ktutorial-editor/src/main.cpp Modified: trunk/ktutorial/ktutorial-editor/src/main.cpp =================================================================== --- trunk/ktutorial/ktutorial-editor/src/main.cpp 2010-03-08 17:41:03 UTC (rev 123) +++ trunk/ktutorial/ktutorial-editor/src/main.cpp 2010-03-08 18:47:18 UTC (rev 124) @@ -24,7 +24,7 @@ int main (int argc, char *argv[]) { KAboutData aboutData("ktutorial-editor", 0, - ki18nc("@title", "KTutorial"), + ki18nc("@title", "KTutorial editor"), "0.1", ki18nc("@info", "An editor to create tutorials for " "<application>KTutorial</application>."), This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <dan...@us...> - 2010-03-08 17:41:12
|
Revision: 123 http://ktutorial.svn.sourceforge.net/ktutorial/?rev=123&view=rev Author: danxuliu Date: 2010-03-08 17:41:03 +0000 (Mon, 08 Mar 2010) Log Message: ----------- Add TutorialTreeSelectionManager class to watch changes in selections and notify when a Step was selected in a TreeModel. Modified Paths: -------------- trunk/ktutorial/ktutorial-editor/src/view/CMakeLists.txt trunk/ktutorial/ktutorial-editor/tests/unit/view/CMakeLists.txt Added Paths: ----------- trunk/ktutorial/ktutorial-editor/src/view/TutorialTreeSelectionManager.cpp trunk/ktutorial/ktutorial-editor/src/view/TutorialTreeSelectionManager.h trunk/ktutorial/ktutorial-editor/tests/unit/view/TutorialTreeSelectionManagerTest.cpp Modified: trunk/ktutorial/ktutorial-editor/src/view/CMakeLists.txt =================================================================== --- trunk/ktutorial/ktutorial-editor/src/view/CMakeLists.txt 2010-03-07 23:13:10 UTC (rev 122) +++ trunk/ktutorial/ktutorial-editor/src/view/CMakeLists.txt 2010-03-08 17:41:03 UTC (rev 123) @@ -7,6 +7,7 @@ TreeItemUtil.cpp TreeModel.cpp TutorialTreeItem.cpp + TutorialTreeSelectionManager.cpp ) kde4_add_library(ktutorial_editor_view ${ktutorial_editor_view_SRCS}) Added: trunk/ktutorial/ktutorial-editor/src/view/TutorialTreeSelectionManager.cpp =================================================================== --- trunk/ktutorial/ktutorial-editor/src/view/TutorialTreeSelectionManager.cpp (rev 0) +++ trunk/ktutorial/ktutorial-editor/src/view/TutorialTreeSelectionManager.cpp 2010-03-08 17:41:03 UTC (rev 123) @@ -0,0 +1,91 @@ +/*************************************************************************** + * 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 "TutorialTreeSelectionManager.h" + +#include "StepTreeItem.h" +#include "TreeItem.h" + +//public: + +TutorialTreeSelectionManager::TutorialTreeSelectionManager( + QItemSelectionModel* itemSelectionModel) { + connect(itemSelectionModel, + SIGNAL(selectionChanged(QItemSelection, QItemSelection)), + this, + SLOT(handleSelectionChanged(QItemSelection, QItemSelection))); +} + +//private: + +void TutorialTreeSelectionManager::updateStepSelection(TreeItem* selected, + TreeItem* deselected) { + Step* selectedStep = getStepForTreeItem(selected); + Step* deselectedStep = getStepForTreeItem(deselected); + + if (selectedStep && selectedStep != deselectedStep) { + emit stepSelected(selectedStep); + return; + } + + if (!selectedStep && deselectedStep) { + emit stepSelected(0); + return; + } +} + +Step* TutorialTreeSelectionManager::getStepForTreeItem(TreeItem* item) { + if (qobject_cast<StepTreeItem*>(item)) { + return static_cast<StepTreeItem*>(item)->step(); + } + + if (item == 0 || item->parent() == 0) { + return 0; + } + + return getStepForTreeItem(item->parent()); +} + +//private slots: + +void TutorialTreeSelectionManager::handleSelectionChanged( + const QItemSelection& selected, + const QItemSelection& deselected) { + //Only single selections are supported + Q_ASSERT(selected.count() <= 1); + Q_ASSERT(deselected.count() <= 1); + + TreeItem* selectedItem = 0; + TreeItem* deselectedItem = 0; + + if (selected.count() == 1) { + Q_ASSERT(selected.at(0).indexes().count() == 1); + + QModelIndex index = selected.at(0).indexes().at(0); + selectedItem = static_cast<TreeItem*>(index.internalPointer()); + } + + if (deselected.count() == 1) { + Q_ASSERT(deselected.at(0).indexes().count() == 1); + + QModelIndex index = deselected.at(0).indexes().at(0); + deselectedItem = static_cast<TreeItem*>(index.internalPointer()); + } + + updateStepSelection(selectedItem, deselectedItem); +} Property changes on: trunk/ktutorial/ktutorial-editor/src/view/TutorialTreeSelectionManager.cpp ___________________________________________________________________ Added: svn:eol-style + native Added: trunk/ktutorial/ktutorial-editor/src/view/TutorialTreeSelectionManager.h =================================================================== --- trunk/ktutorial/ktutorial-editor/src/view/TutorialTreeSelectionManager.h (rev 0) +++ trunk/ktutorial/ktutorial-editor/src/view/TutorialTreeSelectionManager.h 2010-03-08 17:41:03 UTC (rev 123) @@ -0,0 +1,99 @@ +/*************************************************************************** + * 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 TUTORIALTREESELECTIONMANAGER_H +#define TUTORIALTREESELECTIONMANAGER_H + +#include <QItemSelectionModel> +#include <QObject> + +class Step; +class TreeItem; + +/** + * Watches the QItemSelectionModel of a TreeModel for changes in the selection. + * When an item is selected in the TreeModel, it is checked what kind of data it + * represents. If it is a Step, stepSelected(Step*) signal is emitted. + * + * Only single item selections are supported. + */ +class TutorialTreeSelectionManager: public QObject { +Q_OBJECT +public: + + /** + * Creates a new TutorialTreeSelectionManager that watchs the given + * selection model. + * + * @param itemSelectionModel The selection model to watch for changes in the + * selection. + */ + TutorialTreeSelectionManager(QItemSelectionModel* itemSelectionModel); + +Q_SIGNALS: + + /** + * Emitted when a Step (or any of its child items) is selected. + * If the Step is deselected and the new selected item isn't a Step, the + * signal is emitted with a null pointer. + * No signal is emitted if the selected item changes to another item that + * selects the same Step already selected. + * + * @param step The selected Step, or null if it was deselected. + */ + void stepSelected(Step* step); + +private: + + /** + * Emits stepSelected(Step*) signal based on the selected and deselected + * items. + * + * @param selected The selected item, if any. + * @param deselected The deselected item, if any. + */ + void updateStepSelection(TreeItem* selected, TreeItem* deselected); + + /** + * Returns the Step represented by the given item. + * Any recursive child item of a StepTreeItem, or a StepTreeItem itself, + * represents a Step. + * + * If the item doesn't represent a Step or there is no item, a null pointer + * is returned. + * + * @param item The item to get its represented Step. + * @return The Step. + */ + Step* getStepForTreeItem(TreeItem* item); + +private Q_SLOTS: + + /** + * Handles a change in the selection in the watched selection model. + * Signals are emitted as needed based on the selected and deselected items. + * + * @param selected The item selection of selected items. + * @param selected The item selection of deselected items. + */ + void handleSelectionChanged(const QItemSelection& selected, + const QItemSelection& deselected); + +}; + +#endif Property changes on: trunk/ktutorial/ktutorial-editor/src/view/TutorialTreeSelectionManager.h ___________________________________________________________________ Added: svn:eol-style + native Modified: trunk/ktutorial/ktutorial-editor/tests/unit/view/CMakeLists.txt =================================================================== --- trunk/ktutorial/ktutorial-editor/tests/unit/view/CMakeLists.txt 2010-03-07 23:13:10 UTC (rev 122) +++ trunk/ktutorial/ktutorial-editor/tests/unit/view/CMakeLists.txt 2010-03-08 17:41:03 UTC (rev 123) @@ -16,7 +16,7 @@ ENDFOREACH(_className) ENDMACRO(UNIT_TESTS) -unit_tests(StepTreeItem TextTreeItem TreeItem TreeItemUtil TreeModel TutorialTreeItem) +unit_tests(StepTreeItem TextTreeItem TreeItem TreeItemUtil TreeModel TutorialTreeItem TutorialTreeSelectionManager) MACRO(MEM_TESTS) FOREACH(_testname ${ARGN}) @@ -24,4 +24,4 @@ ENDFOREACH(_testname) ENDMACRO(MEM_TESTS) -mem_tests(StepTreeItem TextTreeItem TreeItem TreeItemUtil TreeModel TutorialTreeItem) +mem_tests(StepTreeItem TextTreeItem TreeItem TreeItemUtil TreeModel TutorialTreeItem TutorialTreeSelectionManager) Added: trunk/ktutorial/ktutorial-editor/tests/unit/view/TutorialTreeSelectionManagerTest.cpp =================================================================== --- trunk/ktutorial/ktutorial-editor/tests/unit/view/TutorialTreeSelectionManagerTest.cpp (rev 0) +++ trunk/ktutorial/ktutorial-editor/tests/unit/view/TutorialTreeSelectionManagerTest.cpp 2010-03-08 17:41:03 UTC (rev 123) @@ -0,0 +1,328 @@ +/*************************************************************************** + * 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 <QtTest> + +#include "TutorialTreeSelectionManager.h" + +#include "TreeModel.h" +#include "TutorialTreeItem.h" +#include "../Step.h" +#include "../Tutorial.h" + +class TutorialTreeSelectionManagerTest: public QObject { +Q_OBJECT + +private slots: + + void initTestCase(); + void init(); + void cleanup(); + void cleanupTestCase(); + + void testSelectTutorialOrChildren(); + + void testSelectStepOrChildrenChangingToOtherItems(); + void testSelectStepOrChildrenWithoutChangingStep(); + void testSelectStepOrChildrenChangingToAnotherStep(); + +private: + + int mStepStarType; + + Tutorial* mTutorial; + Step* mStep1; + Step* mStep2; + TreeModel* mTreeModel; + + QItemSelectionModel* mSelectionModel; + TutorialTreeSelectionManager* mSelectionManager; + QSignalSpy* mStepSelectedSpy; + + void select(const QModelIndex& index); + + void assertStepSignal(const QSignalSpy* spy, int index, Step* step) const; + +}; + +void TutorialTreeSelectionManagerTest::initTestCase() { + //Step* must be registered in order to be used with QSignalSpy + mStepStarType = qRegisterMetaType<Step*>("Step*"); + + mTutorial = new Tutorial(); + mTutorial->setName("The name"); + mTutorial->setDescription("The description"); + mTutorial->setLicenseText("The license text"); + mTutorial->setCustomSetupCode("The setup code"); + mTutorial->setCustomTearDownCode("The tear down code"); + + mStep1 = new Step(); + mStep1->setId("First step"); + mStep1->setText("Text 1"); + mStep1->setCustomSetupCode("Setup 1"); + mStep1->setCustomTearDownCode("Tear down 1"); + mTutorial->addStep(mStep1); + + mStep2 = new Step(); + mStep2->setId("Second step"); + mStep2->setText("Text 2"); + mStep2->setCustomSetupCode("Setup 2"); + mStep2->setCustomTearDownCode("Tear down 2"); + mTutorial->addStep(mStep2); + + mTreeModel = new TreeModel(new TutorialTreeItem(mTutorial)); +} + +void TutorialTreeSelectionManagerTest::init() { + mSelectionModel = new QItemSelectionModel(mTreeModel); + mSelectionManager = new TutorialTreeSelectionManager(mSelectionModel); + + mStepSelectedSpy = new QSignalSpy(mSelectionManager, + SIGNAL(stepSelected(Step*))); +} + +void TutorialTreeSelectionManagerTest::cleanup() { + delete mStepSelectedSpy; + delete mSelectionManager; + delete mSelectionModel; +} + +void TutorialTreeSelectionManagerTest::cleanupTestCase() { + delete mTreeModel; + delete mTutorial; +} + +void TutorialTreeSelectionManagerTest::testSelectTutorialOrChildren() { + //Name + select(mTreeModel->index(0, 0)); + //Description + select(mTreeModel->index(1, 0)); + //License + select(mTreeModel->index(2, 0)); + select(mTreeModel->index(0, 0, mTreeModel->index(2, 0))); + //Setup + select(mTreeModel->index(3, 0)); + select(mTreeModel->index(0, 0, mTreeModel->index(3, 0))); + //Tear down + select(mTreeModel->index(4, 0)); + select(mTreeModel->index(0, 0, mTreeModel->index(4, 0))); + //Steps root + select(mTreeModel->index(5, 0)); + + QCOMPARE(mStepSelectedSpy->count(), 0); +} + +void TutorialTreeSelectionManagerTest:: + testSelectStepOrChildrenChangingToOtherItems() { + QModelIndex step1Index = mTreeModel->index(0, 0, mTreeModel->index(5, 0)); + + select(step1Index); + + QCOMPARE(mStepSelectedSpy->count(), 1); + assertStepSignal(mStepSelectedSpy, 0, mStep1); + + //Steps root + select(mTreeModel->index(5, 0)); + + QCOMPARE(mStepSelectedSpy->count(), 2); + assertStepSignal(mStepSelectedSpy, 1, 0); + + //Text + select(mTreeModel->index(0, 0, step1Index)); + + QCOMPARE(mStepSelectedSpy->count(), 3); + assertStepSignal(mStepSelectedSpy, 2, mStep1); + + select(mTreeModel->index(5, 0)); + + QCOMPARE(mStepSelectedSpy->count(), 4); + assertStepSignal(mStepSelectedSpy, 3, 0); + + //Setup + select(mTreeModel->index(1, 0, step1Index)); + + QCOMPARE(mStepSelectedSpy->count(), 5); + assertStepSignal(mStepSelectedSpy, 4, mStep1); + + select(mTreeModel->index(5, 0)); + + QCOMPARE(mStepSelectedSpy->count(), 6); + assertStepSignal(mStepSelectedSpy, 5, 0); + + //Setup code + select(mTreeModel->index(0, 0, mTreeModel->index(1, 0, step1Index))); + + QCOMPARE(mStepSelectedSpy->count(), 7); + assertStepSignal(mStepSelectedSpy, 6, mStep1); + + select(mTreeModel->index(5, 0)); + + QCOMPARE(mStepSelectedSpy->count(), 8); + assertStepSignal(mStepSelectedSpy, 7, 0); + + //Tear down + select(mTreeModel->index(2, 0, step1Index)); + + QCOMPARE(mStepSelectedSpy->count(), 9); + assertStepSignal(mStepSelectedSpy, 8, mStep1); + + select(mTreeModel->index(5, 0)); + + QCOMPARE(mStepSelectedSpy->count(), 10); + assertStepSignal(mStepSelectedSpy, 9, 0); + + //Tear down code + select(mTreeModel->index(0, 0, mTreeModel->index(2, 0, step1Index))); + + QCOMPARE(mStepSelectedSpy->count(), 11); + assertStepSignal(mStepSelectedSpy, 10, mStep1); + + select(mTreeModel->index(5, 0)); + + QCOMPARE(mStepSelectedSpy->count(), 12); + assertStepSignal(mStepSelectedSpy, 11, 0); +} + +void TutorialTreeSelectionManagerTest:: + testSelectStepOrChildrenWithoutChangingStep() { + QModelIndex step1Index = mTreeModel->index(0, 0, mTreeModel->index(5, 0)); + + select(step1Index); + + QCOMPARE(mStepSelectedSpy->count(), 1); + assertStepSignal(mStepSelectedSpy, 0, mStep1); + + //Text + select(mTreeModel->index(0, 0, step1Index)); + + QCOMPARE(mStepSelectedSpy->count(), 1); + + //Setup + select(mTreeModel->index(1, 0, step1Index)); + + QCOMPARE(mStepSelectedSpy->count(), 1); + + //Setup code + select(mTreeModel->index(0, 0, mTreeModel->index(1, 0, step1Index))); + + QCOMPARE(mStepSelectedSpy->count(), 1); + + //Tear down + select(mTreeModel->index(2, 0, step1Index)); + + QCOMPARE(mStepSelectedSpy->count(), 1); + + //Tear down code + select(mTreeModel->index(0, 0, mTreeModel->index(2, 0, step1Index))); + + QCOMPARE(mStepSelectedSpy->count(), 1); +} + +void TutorialTreeSelectionManagerTest:: + testSelectStepOrChildrenChangingToAnotherStep() { + QModelIndex step1Index = mTreeModel->index(0, 0, mTreeModel->index(5, 0)); + QModelIndex step2Index = mTreeModel->index(1, 0, mTreeModel->index(5, 0)); + + select(step1Index); + + QCOMPARE(mStepSelectedSpy->count(), 1); + assertStepSignal(mStepSelectedSpy, 0, mStep1); + + select(step2Index); + + QCOMPARE(mStepSelectedSpy->count(), 2); + assertStepSignal(mStepSelectedSpy, 1, mStep2); + + //Text + select(mTreeModel->index(0, 0, step1Index)); + + QCOMPARE(mStepSelectedSpy->count(), 3); + assertStepSignal(mStepSelectedSpy, 2, mStep1); + + select(mTreeModel->index(0, 0, step2Index)); + + QCOMPARE(mStepSelectedSpy->count(), 4); + assertStepSignal(mStepSelectedSpy, 3, mStep2); + + //Setup + select(mTreeModel->index(1, 0, step1Index)); + + QCOMPARE(mStepSelectedSpy->count(), 5); + assertStepSignal(mStepSelectedSpy, 4, mStep1); + + select(mTreeModel->index(1, 0, step2Index)); + + QCOMPARE(mStepSelectedSpy->count(), 6); + assertStepSignal(mStepSelectedSpy, 5, mStep2); + + //Setup code + select(mTreeModel->index(0, 0, mTreeModel->index(1, 0, step1Index))); + + QCOMPARE(mStepSelectedSpy->count(), 7); + assertStepSignal(mStepSelectedSpy, 6, mStep1); + + select(mTreeModel->index(0, 0, mTreeModel->index(1, 0, step2Index))); + + QCOMPARE(mStepSelectedSpy->count(), 8); + assertStepSignal(mStepSelectedSpy, 7, mStep2); + + //Tear down + select(mTreeModel->index(2, 0, step1Index)); + + QCOMPARE(mStepSelectedSpy->count(), 9); + assertStepSignal(mStepSelectedSpy, 8, mStep1); + + select(mTreeModel->index(2, 0, step2Index)); + + QCOMPARE(mStepSelectedSpy->count(), 10); + assertStepSignal(mStepSelectedSpy, 9, mStep2); + + //Tear down code + select(mTreeModel->index(0, 0, mTreeModel->index(2, 0, step1Index))); + + QCOMPARE(mStepSelectedSpy->count(), 11); + assertStepSignal(mStepSelectedSpy, 10, mStep1); + + select(mTreeModel->index(0, 0, mTreeModel->index(2, 0, step2Index))); + + QCOMPARE(mStepSelectedSpy->count(), 12); + assertStepSignal(mStepSelectedSpy, 11, mStep2); +} + +/////////////////////////////////// Helpers //////////////////////////////////// + +void TutorialTreeSelectionManagerTest::select(const QModelIndex& index) { + mSelectionModel->select(index, QItemSelectionModel::SelectCurrent); +} + +//Step* must be declared as a metatype to be used in qvariant_cast +Q_DECLARE_METATYPE(Step*); + +void TutorialTreeSelectionManagerTest::assertStepSignal(const QSignalSpy* spy, + int index, + Step* step) const { + QCOMPARE(spy->at(index).count(), 1); + + QVariant argument = spy->at(index).at(0); + QCOMPARE(argument.userType(), mStepStarType); + QCOMPARE(qvariant_cast<Step*>(argument), step); +} + +QTEST_MAIN(TutorialTreeSelectionManagerTest) + +#include "TutorialTreeSelectionManagerTest.moc" Property changes on: trunk/ktutorial/ktutorial-editor/tests/unit/view/TutorialTreeSelectionManagerTest.cpp ___________________________________________________________________ Added: svn:eol-style + native This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <dan...@us...> - 2010-03-07 23:13:16
|
Revision: 122 http://ktutorial.svn.sourceforge.net/ktutorial/?rev=122&view=rev Author: danxuliu Date: 2010-03-07 23:13:10 +0000 (Sun, 07 Mar 2010) Log Message: ----------- Fix memory leak in test. Modified Paths: -------------- trunk/ktutorial/ktutorial-editor/tests/unit/view/TutorialTreeItemTest.cpp Modified: trunk/ktutorial/ktutorial-editor/tests/unit/view/TutorialTreeItemTest.cpp =================================================================== --- trunk/ktutorial/ktutorial-editor/tests/unit/view/TutorialTreeItemTest.cpp 2010-03-07 23:11:37 UTC (rev 121) +++ trunk/ktutorial/ktutorial-editor/tests/unit/view/TutorialTreeItemTest.cpp 2010-03-07 23:13:10 UTC (rev 122) @@ -357,10 +357,12 @@ Tutorial tutorial; TutorialTreeItem item(&tutorial); - Step* step = new Step(); - step->setId("Step id"); - tutorial.addStep(step); - tutorial.removeStep(step); + //It will be removed and not deleted by the Tutorial, so it is created in + //stack + Step step; + step.setId("Step id"); + tutorial.addStep(&step); + tutorial.removeStep(&step); QCOMPARE(item.childCount(), 0); } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <dan...@us...> - 2010-03-07 23:11:43
|
Revision: 121 http://ktutorial.svn.sourceforge.net/ktutorial/?rev=121&view=rev Author: danxuliu Date: 2010-03-07 23:11:37 +0000 (Sun, 07 Mar 2010) Log Message: ----------- Fix setting parent QObject in Step and Tutorial classes. Modified Paths: -------------- trunk/ktutorial/ktutorial-editor/src/Step.cpp trunk/ktutorial/ktutorial-editor/src/Tutorial.cpp trunk/ktutorial/ktutorial-editor/tests/unit/StepTest.cpp trunk/ktutorial/ktutorial-editor/tests/unit/TutorialTest.cpp Modified: trunk/ktutorial/ktutorial-editor/src/Step.cpp =================================================================== --- trunk/ktutorial/ktutorial-editor/src/Step.cpp 2010-03-07 22:50:16 UTC (rev 120) +++ trunk/ktutorial/ktutorial-editor/src/Step.cpp 2010-03-07 23:11:37 UTC (rev 121) @@ -20,7 +20,7 @@ //public: -Step::Step(QObject* parent) { +Step::Step(QObject* parent): QObject(parent) { } QString Step::id() const { Modified: trunk/ktutorial/ktutorial-editor/src/Tutorial.cpp =================================================================== --- trunk/ktutorial/ktutorial-editor/src/Tutorial.cpp 2010-03-07 22:50:16 UTC (rev 120) +++ trunk/ktutorial/ktutorial-editor/src/Tutorial.cpp 2010-03-07 23:11:37 UTC (rev 121) @@ -24,7 +24,7 @@ //public: -Tutorial::Tutorial(QObject* parent) { +Tutorial::Tutorial(QObject* parent): QObject(parent) { } Tutorial::~Tutorial() { Modified: trunk/ktutorial/ktutorial-editor/tests/unit/StepTest.cpp =================================================================== --- trunk/ktutorial/ktutorial-editor/tests/unit/StepTest.cpp 2010-03-07 22:50:16 UTC (rev 120) +++ trunk/ktutorial/ktutorial-editor/tests/unit/StepTest.cpp 2010-03-07 23:11:37 UTC (rev 121) @@ -25,6 +25,8 @@ private slots: + void testConstructor(); + void testSetId(); void testSetText(); @@ -35,6 +37,13 @@ }; +void StepTest::testConstructor() { + QObject parent; + Step* step = new Step(&parent); + + QCOMPARE(step->parent(), &parent); +} + //Step* must be declared as a metatype to be used in qvariant_cast Q_DECLARE_METATYPE(Step*); Modified: trunk/ktutorial/ktutorial-editor/tests/unit/TutorialTest.cpp =================================================================== --- trunk/ktutorial/ktutorial-editor/tests/unit/TutorialTest.cpp 2010-03-07 22:50:16 UTC (rev 120) +++ trunk/ktutorial/ktutorial-editor/tests/unit/TutorialTest.cpp 2010-03-07 23:11:37 UTC (rev 121) @@ -28,6 +28,8 @@ void initTestCase(); + void testConstructor(); + void testId(); void testIdWithEmptyName(); void testIdWithSpaceName(); @@ -58,6 +60,13 @@ mStepStarType = qRegisterMetaType<Tutorial*>("Step*"); } +void TutorialTest::testConstructor() { + QObject parent; + Tutorial* tutorial = new Tutorial(&parent); + + QCOMPARE(tutorial->parent(), &parent); +} + void TutorialTest::testId() { Tutorial tutorial; tutorial.setName("ThE name of a tutoRial"); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <dan...@us...> - 2010-03-07 22:50:22
|
Revision: 120 http://ktutorial.svn.sourceforge.net/ktutorial/?rev=120&view=rev Author: danxuliu Date: 2010-03-07 22:50:16 +0000 (Sun, 07 Mar 2010) Log Message: ----------- Include a list of steps in tutorial. Modified Paths: -------------- trunk/ktutorial/ktutorial-editor/src/Tutorial.cpp trunk/ktutorial/ktutorial-editor/src/Tutorial.h trunk/ktutorial/ktutorial-editor/src/view/StepTreeItem.cpp trunk/ktutorial/ktutorial-editor/src/view/StepTreeItem.h trunk/ktutorial/ktutorial-editor/src/view/TutorialTreeItem.cpp trunk/ktutorial/ktutorial-editor/src/view/TutorialTreeItem.h trunk/ktutorial/ktutorial-editor/tests/unit/TutorialTest.cpp trunk/ktutorial/ktutorial-editor/tests/unit/view/StepTreeItemTest.cpp trunk/ktutorial/ktutorial-editor/tests/unit/view/TutorialTreeItemTest.cpp Modified: trunk/ktutorial/ktutorial-editor/src/Tutorial.cpp =================================================================== --- trunk/ktutorial/ktutorial-editor/src/Tutorial.cpp 2010-03-07 18:50:37 UTC (rev 119) +++ trunk/ktutorial/ktutorial-editor/src/Tutorial.cpp 2010-03-07 22:50:16 UTC (rev 120) @@ -20,11 +20,17 @@ #include <QStringList> +#include "Step.h" + //public: Tutorial::Tutorial(QObject* parent) { } +Tutorial::~Tutorial() { + qDeleteAll(mSteps); +} + QString Tutorial::id() const { return toLowerCamelCase(mName); } @@ -79,6 +85,26 @@ emit dataChanged(this); } +void Tutorial::addStep(Step* step) { + Q_ASSERT(!mSteps.contains(step)); + + mSteps.append(step); + + emit stepAdded(step); +} + +QList<Step*> Tutorial::steps() const { + return mSteps; +} + +void Tutorial::removeStep(Step* step) { + Q_ASSERT(mSteps.contains(step)); + + mSteps.removeOne(step); + + emit stepRemoved(step); +} + //private: QString Tutorial::toLowerCamelCase(const QString& text) const { Modified: trunk/ktutorial/ktutorial-editor/src/Tutorial.h =================================================================== --- trunk/ktutorial/ktutorial-editor/src/Tutorial.h 2010-03-07 18:50:37 UTC (rev 119) +++ trunk/ktutorial/ktutorial-editor/src/Tutorial.h 2010-03-07 22:50:16 UTC (rev 120) @@ -19,8 +19,11 @@ #ifndef TUTORIAL_H #define TUTORIAL_H +#include <QList> #include <QObject> +class Step; + /** * Container for tutorial data. * It stores the data used in KTutorial tutorials, but it has nothing to do with @@ -28,6 +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. */ class Tutorial: public QObject { Q_OBJECT @@ -36,6 +41,11 @@ Tutorial(QObject* parent = 0); /** + * Destroys this Tutorial and all its steps. + */ + virtual ~Tutorial(); + + /** * Returns the id of this Tutorial. * The id is a lowerCamelCase version of the name. * @@ -58,6 +68,24 @@ QString customTearDownCode() const; void setCustomTearDownCode(const QString& code); + /** + * Adds a new step to this Tutorial. + * The Tutorial gets ownership of the Step, so it is deleted when the + * Tutorial is deleted. + * + * @param step The step to add. + */ + void addStep(Step* step); + QList<Step*> steps() const; + + /** + * Removes a step from this Tutorial. + * The Step must be deleted explicitly. + * + * @param step The step to remove. + */ + void removeStep(Step* step); + Q_SIGNALS: /** @@ -67,6 +95,20 @@ */ void dataChanged(Tutorial* tutorial); + /** + * Emitted when the step is added to the tutorial. + * + * @param step The step added. + */ + void stepAdded(Step* step); + + /** + * Emitted when the step is removed from the tutorial. + * + * @param step The step removed. + */ + void stepRemoved(Step* step); + private: QString mName; @@ -74,6 +116,7 @@ QString mLicenseText; QString mCustomSetupCode; QString mCustomTearDownCode; + QList<Step*> mSteps; /** * Returns the lowerCamelCase version of the given text. Modified: trunk/ktutorial/ktutorial-editor/src/view/StepTreeItem.cpp =================================================================== --- trunk/ktutorial/ktutorial-editor/src/view/StepTreeItem.cpp 2010-03-07 18:50:37 UTC (rev 119) +++ trunk/ktutorial/ktutorial-editor/src/view/StepTreeItem.cpp 2010-03-07 22:50:16 UTC (rev 120) @@ -27,7 +27,8 @@ //public: StepTreeItem::StepTreeItem(Step* step, TreeItem* parent): - TreeItem(parent) { + TreeItem(parent), + mStep(step) { Q_ASSERT(step); mTextItem = 0; @@ -47,6 +48,10 @@ return i18nc("@item", "Step %1", mStepId); } +Step* StepTreeItem::step() const { + return mStep; +} + //private slots: void StepTreeItem::update(Step* step) { Modified: trunk/ktutorial/ktutorial-editor/src/view/StepTreeItem.h =================================================================== --- trunk/ktutorial/ktutorial-editor/src/view/StepTreeItem.h 2010-03-07 18:50:37 UTC (rev 119) +++ trunk/ktutorial/ktutorial-editor/src/view/StepTreeItem.h 2010-03-07 22:50:16 UTC (rev 120) @@ -70,9 +70,21 @@ */ virtual QString text() const; + /** + * Returns the Step. + * + * @return The Step. + */ + Step* step() const; + private: /** + * The Step. + */ + Step* mStep; + + /** * The id of the step. */ QString mStepId; Modified: trunk/ktutorial/ktutorial-editor/src/view/TutorialTreeItem.cpp =================================================================== --- trunk/ktutorial/ktutorial-editor/src/view/TutorialTreeItem.cpp 2010-03-07 18:50:37 UTC (rev 119) +++ trunk/ktutorial/ktutorial-editor/src/view/TutorialTreeItem.cpp 2010-03-07 22:50:16 UTC (rev 120) @@ -20,6 +20,7 @@ #include <KLocalizedString> +#include "StepTreeItem.h" #include "TextTreeItem.h" #include "TreeItemUtil.h" #include "../Tutorial.h" @@ -39,6 +40,14 @@ update(tutorial); connect(tutorial, SIGNAL(dataChanged(Tutorial*)), this, SLOT(update(Tutorial*))); + + foreach(Step* step, tutorial->steps()) { + addStep(step); + } + connect(tutorial, SIGNAL(stepAdded(Step*)), + this, SLOT(addStep(Step*))); + connect(tutorial, SIGNAL(stepRemoved(Step*)), + this, SLOT(removeStep(Step*))); } QString TutorialTreeItem::text() const { @@ -49,6 +58,18 @@ return i18nc("@item", "Tutorial %1", mTutorialId); } +//private: + +StepTreeItem* TutorialTreeItem::stepTreeItemForStep(Step* step) { + foreach (StepTreeItem* stepTreeItem, mStepTreeItems) { + if (stepTreeItem->step() == step) { + return stepTreeItem; + } + } + + return 0; +} + //private slots: void TutorialTreeItem::update(Tutorial* tutorial) { @@ -111,3 +132,35 @@ childIndex++; } } + +void TutorialTreeItem::addStep(Step* step) { + TreeItem* rootStepItem; + + if (mStepTreeItems.isEmpty()) { + TextTreeItem* textRootStepItem = new TextTreeItem(this); + textRootStepItem->setText(i18nc("@item", "Steps:")); + appendChild(textRootStepItem); + + rootStepItem = textRootStepItem; + } else { + rootStepItem = mStepTreeItems.at(0)->parent(); + } + + StepTreeItem* stepTreeItem = new StepTreeItem(step, rootStepItem); + rootStepItem->appendChild(stepTreeItem); + mStepTreeItems.append(stepTreeItem); +} + +void TutorialTreeItem::removeStep(Step* step) { + StepTreeItem* stepTreeItem = stepTreeItemForStep(step); + TreeItem* rootStepItem = stepTreeItem->parent(); + + rootStepItem->removeChild(stepTreeItem); + mStepTreeItems.removeOne(stepTreeItem); + delete stepTreeItem; + + if (mStepTreeItems.isEmpty()) { + removeChild(rootStepItem); + delete rootStepItem; + } +} Modified: trunk/ktutorial/ktutorial-editor/src/view/TutorialTreeItem.h =================================================================== --- trunk/ktutorial/ktutorial-editor/src/view/TutorialTreeItem.h 2010-03-07 18:50:37 UTC (rev 119) +++ trunk/ktutorial/ktutorial-editor/src/view/TutorialTreeItem.h 2010-03-07 22:50:16 UTC (rev 120) @@ -21,6 +21,8 @@ #include "TreeItem.h" +class Step; +class StepTreeItem; class TextTreeItem; class Tutorial; @@ -34,8 +36,14 @@ * | -The license text * |-Setup: * | -The custom setup code - * --Tear down: - * -The custom tear down code + * |-Tear down: + * | -The custom tear down code + * --Steps: + * -Step first step added + * ... + * -Step second step added + * ... + * ... * * The items only appear if they have some data to show. For example, if only * the name of the Tutorial is set, its representation is: @@ -51,6 +59,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 name, the name item * will appear first and then the setup code item. + * + * @see StepTreeItem */ class TutorialTreeItem: public TreeItem { Q_OBJECT @@ -111,6 +121,19 @@ */ TextTreeItem* mTearDownItem; + /** + * The StepTreeItems for each Step in the Tutorial. + */ + QList<StepTreeItem*> mStepTreeItems; + + /** + * Returns the StepTreeItem for the given Step. + * + * @param step The Step to get its StepTreeItem. + * @return The StepTreeItem. + */ + StepTreeItem* stepTreeItemForStep(Step* step); + private Q_SLOTS: /** @@ -125,6 +148,22 @@ */ void update(Tutorial* tutorial); + /** + * Adds a new StepTreeItem when a Step is added in the tutorial. + * If there were no other steps yet, the parent "Steps:" item is also added. + * + * @param step The Step added in the Tutorial. + */ + void addStep(Step* step); + + /** + * Removes the StepTreeItem for the Step removed in the tutorial. + * If there are no other steps, the parent "Steps:" item is also removed. + * + * @param step The Step removed in the Tutorial. + */ + void removeStep(Step* step); + }; #endif Modified: trunk/ktutorial/ktutorial-editor/tests/unit/TutorialTest.cpp =================================================================== --- trunk/ktutorial/ktutorial-editor/tests/unit/TutorialTest.cpp 2010-03-07 18:50:37 UTC (rev 119) +++ trunk/ktutorial/ktutorial-editor/tests/unit/TutorialTest.cpp 2010-03-07 22:50:16 UTC (rev 120) @@ -19,12 +19,15 @@ #include <QtTest> #include "Tutorial.h" +#include "Step.h" class TutorialTest: public QObject { Q_OBJECT private slots: + void initTestCase(); + void testId(); void testIdWithEmptyName(); void testIdWithSpaceName(); @@ -39,8 +42,22 @@ void testSetCustomTearDownCode(); + void testAddStep(); + void testRemoveStep(); + +private: + + int mStepStarType; + + void assertStepSignal(const QSignalSpy& spy, int index, Step* step); + }; +void TutorialTest::initTestCase() { + //Step* must be registered in order to be used with QSignalSpy + mStepStarType = qRegisterMetaType<Tutorial*>("Step*"); +} + void TutorialTest::testId() { Tutorial tutorial; tutorial.setName("ThE name of a tutoRial"); @@ -145,6 +162,74 @@ QCOMPARE(qvariant_cast<Tutorial*>(argument), &tutorial); } +//Step* must be declared as a metatype to be used in qvariant_cast +Q_DECLARE_METATYPE(Step*); + +void TutorialTest::testAddStep() { + Tutorial tutorial; + Step* step1 = new Step(); + Step* step2 = new Step(); + Step* step3 = new Step(); + + QSignalSpy stepAddedSpy(&tutorial, SIGNAL(stepAdded(Step*))); + + tutorial.addStep(step1); + tutorial.addStep(step2); + tutorial.addStep(step3); + + QCOMPARE(tutorial.steps().count(), 3); + QCOMPARE(tutorial.steps()[0], step1); + 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); +} + +void TutorialTest::testRemoveStep() { + Tutorial tutorial; + + //They will be removed and not deleted by the Tutorial, so they are created + //in stack + Step step1; + Step step2; + Step step3; + + tutorial.addStep(&step1); + tutorial.addStep(&step2); + tutorial.addStep(&step3); + + QSignalSpy stepRemovedSpy(&tutorial, SIGNAL(stepRemoved(Step*))); + + tutorial.removeStep(&step2); + + QCOMPARE(tutorial.steps().count(), 2); + QCOMPARE(tutorial.steps()[0], &step1); + QCOMPARE(tutorial.steps()[1], &step3); + QCOMPARE(stepRemovedSpy.count(), 1); + assertStepSignal(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); +} + +/////////////////////////////////// Helpers //////////////////////////////////// + +void TutorialTest::assertStepSignal(const QSignalSpy& spy, int index, + Step* step) { + QCOMPARE(spy.at(index).count(), 1); + + QVariant argument = spy.at(index).at(0); + QCOMPARE(argument.userType(), mStepStarType); + QCOMPARE(qvariant_cast<Step*>(argument), step); +} + QTEST_MAIN(TutorialTest) #include "TutorialTest.moc" Modified: trunk/ktutorial/ktutorial-editor/tests/unit/view/StepTreeItemTest.cpp =================================================================== --- trunk/ktutorial/ktutorial-editor/tests/unit/view/StepTreeItemTest.cpp 2010-03-07 18:50:37 UTC (rev 119) +++ trunk/ktutorial/ktutorial-editor/tests/unit/view/StepTreeItemTest.cpp 2010-03-07 22:50:16 UTC (rev 120) @@ -87,6 +87,7 @@ QCOMPARE(item.parent(), &parent); QCOMPARE(item.text(), i18nc("@item", "Step")); + QCOMPARE(item.step(), &step); QCOMPARE(item.childCount(), 0); } @@ -102,6 +103,7 @@ QCOMPARE(item.parent(), &parent); QCOMPARE(item.text(), i18nc("@item", "Step %1", "The id")); + QCOMPARE(item.step(), &step); QCOMPARE(item.childCount(), 3); assertText(item.child(0), "The text"); assertCustomSetupCode(item.child(1), "The setup code"); Modified: trunk/ktutorial/ktutorial-editor/tests/unit/view/TutorialTreeItemTest.cpp =================================================================== --- trunk/ktutorial/ktutorial-editor/tests/unit/view/TutorialTreeItemTest.cpp 2010-03-07 18:50:37 UTC (rev 119) +++ trunk/ktutorial/ktutorial-editor/tests/unit/view/TutorialTreeItemTest.cpp 2010-03-07 22:50:16 UTC (rev 120) @@ -22,6 +22,8 @@ #include <KLocalizedString> +#include "StepTreeItem.h" +#include "../Step.h" #include "../Tutorial.h" class TutorialTreeItemTest: public QObject { @@ -54,6 +56,10 @@ void testTutorialSetCustomTearDownCodeChange(); void testTutorialSetCustomTearDownCodeEmpty(); + void testTutorialAddStep(); + + void testTutorialRemoveStep(); + void testChildOrderWhenSettingDataInTutorial(); void testChildOrderWhenUnsettingDataInTutorial(); @@ -69,6 +75,8 @@ void assertCustomSetupCode(TreeItem* setupItem, const QString& code) const; void assertCustomTearDownCode(TreeItem* tearDownItem, const QString& code) const; + void assertStep(TreeItem* rootStepItem, int index, + const QString& stepId) const; void assertDataChanged(const QSignalSpy& spy, int index, TreeItem* item) const; @@ -106,17 +114,28 @@ tutorial.setCustomSetupCode("The setup code"); tutorial.setCustomTearDownCode("The tear down code"); + Step* step1 = new Step(); + step1->setId("First step"); + tutorial.addStep(step1); + + Step* step2 = new Step(); + step2->setId("Second step"); + tutorial.addStep(step2); + StubTreeItem parent; TutorialTreeItem item(&tutorial, &parent); QCOMPARE(item.parent(), &parent); QCOMPARE(item.text(), i18nc("@item", "Tutorial %1", "theName")); - QCOMPARE(item.childCount(), 5); + QCOMPARE(item.childCount(), 6); 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"); + QCOMPARE(item.child(5)->childCount(), 2); + assertStep(item.child(5), 0, "First step"); + assertStep(item.child(5), 1, "Second step"); } //TreeItem* must be declared as a metatype to be used in qvariant_cast @@ -322,6 +341,30 @@ QCOMPARE(item.childCount(), 0); } +void TutorialTreeItemTest::testTutorialAddStep() { + Tutorial tutorial; + TutorialTreeItem item(&tutorial); + + Step* step = new Step(); + step->setId("Step id"); + tutorial.addStep(step); + + QCOMPARE(item.childCount(), 1); + assertStep(item.child(0), 0, "Step id"); +} + +void TutorialTreeItemTest::testTutorialRemoveStep() { + Tutorial tutorial; + TutorialTreeItem item(&tutorial); + + Step* step = new Step(); + step->setId("Step id"); + tutorial.addStep(step); + tutorial.removeStep(step); + + QCOMPARE(item.childCount(), 0); +} + void TutorialTreeItemTest::testChildOrderWhenSettingDataInTutorial() { Tutorial tutorial; TutorialTreeItem item(&tutorial); @@ -347,24 +390,55 @@ assertCustomSetupCode(item.child(1), "The setup code"); assertCustomTearDownCode(item.child(2), "The tear down code"); + Step* step1 = new Step(); + step1->setId("First step"); + tutorial.addStep(step1); + + QCOMPARE(item.text(), i18nc("@item", "Tutorial")); + QCOMPARE(item.childCount(), 4); + assertDescription(item.child(0), "The description"); + assertCustomSetupCode(item.child(1), "The setup code"); + assertCustomTearDownCode(item.child(2), "The tear down code"); + QCOMPARE(item.child(3)->childCount(), 1); + assertStep(item.child(3), 0, "First step"); + tutorial.setName("The name"); QCOMPARE(item.text(), i18nc("@item", "Tutorial %1", "theName")); - QCOMPARE(item.childCount(), 4); + QCOMPARE(item.childCount(), 5); 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"); + QCOMPARE(item.child(4)->childCount(), 1); + assertStep(item.child(4), 0, "First step"); - tutorial.setLicenseText("The license text"); + Step* step2 = new Step(); + step2->setId("Second step"); + tutorial.addStep(step2); QCOMPARE(item.text(), i18nc("@item", "Tutorial %1", "theName")); QCOMPARE(item.childCount(), 5); 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"); + QCOMPARE(item.child(4)->childCount(), 2); + assertStep(item.child(4), 0, "First step"); + assertStep(item.child(4), 1, "Second step"); + + tutorial.setLicenseText("The license text"); + + QCOMPARE(item.text(), i18nc("@item", "Tutorial %1", "theName")); + QCOMPARE(item.childCount(), 6); + 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"); + QCOMPARE(item.child(5)->childCount(), 2); + assertStep(item.child(5), 0, "First step"); + assertStep(item.child(5), 1, "Second step"); } void TutorialTreeItemTest::testChildOrderWhenUnsettingDataInTutorial() { @@ -375,41 +449,78 @@ tutorial.setCustomSetupCode("The setup code"); tutorial.setCustomTearDownCode("The tear down code"); + //They will be removed and not deleted by the Tutorial, so they are created + //in stack + Step step1; + step1.setId("First step"); + tutorial.addStep(&step1); + + Step step2; + step2.setId("Second step"); + tutorial.addStep(&step2); + TutorialTreeItem item(&tutorial); tutorial.setLicenseText(""); QCOMPARE(item.text(), i18nc("@item", "Tutorial %1", "theName")); - QCOMPARE(item.childCount(), 4); + QCOMPARE(item.childCount(), 5); 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"); + QCOMPARE(item.child(4)->childCount(), 2); + assertStep(item.child(4), 0, "First step"); + assertStep(item.child(4), 1, "Second step"); tutorial.setName(""); QCOMPARE(item.text(), i18nc("@item", "Tutorial")); - QCOMPARE(item.childCount(), 3); + QCOMPARE(item.childCount(), 4); assertDescription(item.child(0), "The description"); assertCustomSetupCode(item.child(1), "The setup code"); assertCustomTearDownCode(item.child(2), "The tear down code"); + QCOMPARE(item.child(3)->childCount(), 2); + assertStep(item.child(3), 0, "First step"); + assertStep(item.child(3), 1, "Second step"); + tutorial.removeStep(&step1); + + QCOMPARE(item.text(), i18nc("@item", "Tutorial")); + QCOMPARE(item.childCount(), 4); + assertDescription(item.child(0), "The description"); + assertCustomSetupCode(item.child(1), "The setup code"); + assertCustomTearDownCode(item.child(2), "The tear down code"); + QCOMPARE(item.child(3)->childCount(), 1); + assertStep(item.child(3), 0, "Second step"); + tutorial.setCustomTearDownCode(""); QCOMPARE(item.text(), i18nc("@item", "Tutorial")); - QCOMPARE(item.childCount(), 2); + QCOMPARE(item.childCount(), 3); assertDescription(item.child(0), "The description"); assertCustomSetupCode(item.child(1), "The setup code"); + QCOMPARE(item.child(2)->childCount(), 1); + assertStep(item.child(2), 0, "Second step"); tutorial.setDescription(""); QCOMPARE(item.text(), i18nc("@item", "Tutorial")); - QCOMPARE(item.childCount(), 1); + QCOMPARE(item.childCount(), 2); assertCustomSetupCode(item.child(0), "The setup code"); + QCOMPARE(item.child(1)->childCount(), 1); + assertStep(item.child(1), 0, "Second step"); tutorial.setCustomSetupCode(""); QCOMPARE(item.text(), i18nc("@item", "Tutorial")); + QCOMPARE(item.childCount(), 1); + QCOMPARE(item.child(0)->childCount(), 1); + assertStep(item.child(0), 0, "Second step"); + + tutorial.removeStep(&step2); + + QCOMPARE(item.text(), i18nc("@item", "Tutorial")); QCOMPARE(item.childCount(), 0); } @@ -448,6 +559,14 @@ QCOMPARE(tearDownItem->child(0)->text(), i18nc("@item", code.toAscii())); } +void TutorialTreeItemTest::assertStep(TreeItem* rootStepItem, int index, + const QString& stepId) const { + QCOMPARE(rootStepItem->text(), i18nc("@item", "Steps:")); + QVERIFY(qobject_cast<StepTreeItem*>(rootStepItem->child(index))); + QCOMPARE(rootStepItem->child(index)->text(), + i18nc("@item", "Step %1", stepId)); +} + void TutorialTreeItemTest::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. |
From: <dan...@us...> - 2010-03-07 18:50:44
|
Revision: 119 http://ktutorial.svn.sourceforge.net/ktutorial/?rev=119&view=rev Author: danxuliu Date: 2010-03-07 18:50:37 +0000 (Sun, 07 Mar 2010) Log Message: ----------- -Add StepTreeItem class to show a Step in a TreeModel. -Extract common methods from TutorialTreeItem and StepTreeItem to TreeItemUtil class. Modified Paths: -------------- trunk/ktutorial/ktutorial-editor/src/view/CMakeLists.txt trunk/ktutorial/ktutorial-editor/src/view/TutorialTreeItem.cpp trunk/ktutorial/ktutorial-editor/src/view/TutorialTreeItem.h trunk/ktutorial/ktutorial-editor/tests/unit/view/CMakeLists.txt Added Paths: ----------- trunk/ktutorial/ktutorial-editor/src/view/StepTreeItem.cpp trunk/ktutorial/ktutorial-editor/src/view/StepTreeItem.h trunk/ktutorial/ktutorial-editor/src/view/TreeItemUtil.cpp trunk/ktutorial/ktutorial-editor/src/view/TreeItemUtil.h trunk/ktutorial/ktutorial-editor/tests/unit/view/StepTreeItemTest.cpp trunk/ktutorial/ktutorial-editor/tests/unit/view/TreeItemUtilTest.cpp Modified: trunk/ktutorial/ktutorial-editor/src/view/CMakeLists.txt =================================================================== --- trunk/ktutorial/ktutorial-editor/src/view/CMakeLists.txt 2010-03-07 07:38:57 UTC (rev 118) +++ trunk/ktutorial/ktutorial-editor/src/view/CMakeLists.txt 2010-03-07 18:50:37 UTC (rev 119) @@ -1,8 +1,10 @@ include_directories(${KDE4_INCLUDES}) set(ktutorial_editor_view_SRCS + StepTreeItem.cpp TextTreeItem.cpp TreeItem.cpp + TreeItemUtil.cpp TreeModel.cpp TutorialTreeItem.cpp ) Added: trunk/ktutorial/ktutorial-editor/src/view/StepTreeItem.cpp =================================================================== --- trunk/ktutorial/ktutorial-editor/src/view/StepTreeItem.cpp (rev 0) +++ trunk/ktutorial/ktutorial-editor/src/view/StepTreeItem.cpp 2010-03-07 18:50:37 UTC (rev 119) @@ -0,0 +1,93 @@ +/*************************************************************************** + * 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 "StepTreeItem.h" + +#include <KLocalizedString> + +#include "TextTreeItem.h" +#include "TreeItemUtil.h" +#include "../Step.h" + +//public: + +StepTreeItem::StepTreeItem(Step* step, TreeItem* parent): + TreeItem(parent) { + Q_ASSERT(step); + + mTextItem = 0; + mSetupItem = 0; + mTearDownItem = 0; + + update(step); + connect(step, SIGNAL(dataChanged(Step*)), + this, SLOT(update(Step*))); +} + +QString StepTreeItem::text() const { + if (mStepId.isEmpty()) { + return i18nc("@item", "Step"); + } + + return i18nc("@item", "Step %1", mStepId); +} + +//private slots: + +void StepTreeItem::update(Step* step) { + int childIndex = 0; + + if (step->id().isEmpty()) { + if (!mStepId.isEmpty()) { + mStepId = QString(); + emit dataChanged(this); + } + } else { + mStepId = step->id(); + emit dataChanged(this); + } + + if (step->text().isEmpty()) { + TreeItemUtil::removeFlatItemIfNeeded(mTextItem); + } else { + TreeItemUtil::addFlatItemIfNeeded(this, mTextItem, childIndex); + mTextItem->setText(i18nc("@item", "Text: %1", step->text())); + + childIndex++; + } + + if (step->customSetupCode().isEmpty()) { + TreeItemUtil::removeNestedItemIfNeeded(mSetupItem); + } else { + TreeItemUtil::addNestedItemIfNeeded(this, mSetupItem, childIndex, + i18nc("@item", "Setup:")); + mSetupItem->setText(step->customSetupCode()); + + childIndex++; + } + + if (step->customTearDownCode().isEmpty()) { + TreeItemUtil::removeNestedItemIfNeeded(mTearDownItem); + } else { + TreeItemUtil::addNestedItemIfNeeded(this, mTearDownItem, childIndex, + i18nc("@item", "Tear down:")); + mTearDownItem->setText(step->customTearDownCode()); + + childIndex++; + } +} Property changes on: trunk/ktutorial/ktutorial-editor/src/view/StepTreeItem.cpp ___________________________________________________________________ Added: svn:eol-style + native Added: trunk/ktutorial/ktutorial-editor/src/view/StepTreeItem.h =================================================================== --- trunk/ktutorial/ktutorial-editor/src/view/StepTreeItem.h (rev 0) +++ trunk/ktutorial/ktutorial-editor/src/view/StepTreeItem.h 2010-03-07 18:50:37 UTC (rev 119) @@ -0,0 +1,115 @@ +/*************************************************************************** + * 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 STEPTREEITEM_H +#define STEPTREEITEM_H + +#include "TreeItem.h" + +class Step; +class TextTreeItem; + +/** + * A TreeItem that represents a Step. + * The tree representation of a step is: + * Step theId + * |-Text: the text + * |-Setup: + * | -The custom setup code + * --Tear down: + * -The custom tear down code + * + * 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: + * Step + * -The text + * + * Note that composed elements like custom setup code don't appear at all, not + * even the parent item with just "Setup:". + * + * Whenever the step data changes, the StepTreeItem and its child items + * are updated as needed. + * + * 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. + */ +class StepTreeItem: public TreeItem { +Q_OBJECT +public: + + /** + * Creates a new StepTreeItem for the given Step and with the given + * parent. + * + * @param step The step to represent. + * @param parent The parent TreeItem. + */ + explicit StepTreeItem(Step* step, TreeItem* parent = 0); + + /** + * Returns "Step " and the id of the step, or just "Step" if + * there is no id. + * + * @return The text for this TreeItem. + */ + virtual QString text() const; + +private: + + /** + * The id of the step. + */ + QString mStepId; + + /** + * The child item containing the text. + */ + TextTreeItem* mTextItem; + + /** + * The child item containing the custom setup code. + * This is a nested item. Its parent (and the direct child of this + * StepTreeItem) contains the "Setup:" text. + */ + TextTreeItem* mSetupItem; + + /** + * The child item containing the custom tear down code. + * This is a nested item. Its parent (and the direct child of this + * StepTreeItem) contains the "Tear down:" text. + */ + TextTreeItem* mTearDownItem; + +private Q_SLOTS: + + /** + * Updates this StepTreeItem when the data of its step changed. + * If a child item is needed to show some data, it is inserted or updated + * (depending on whether it existed previously or not). + * If the child item is no longer needed, it is removed. + * + * Items may be flat or nested, depending on the data to show. + * + * @param step The step. + */ + void update(Step* step); + +}; + +#endif Property changes on: trunk/ktutorial/ktutorial-editor/src/view/StepTreeItem.h ___________________________________________________________________ Added: svn:eol-style + native Added: trunk/ktutorial/ktutorial-editor/src/view/TreeItemUtil.cpp =================================================================== --- trunk/ktutorial/ktutorial-editor/src/view/TreeItemUtil.cpp (rev 0) +++ trunk/ktutorial/ktutorial-editor/src/view/TreeItemUtil.cpp 2010-03-07 18:50:37 UTC (rev 119) @@ -0,0 +1,60 @@ +/*************************************************************************** + * 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 "TreeItemUtil.h" + +#include "TextTreeItem.h" + +//public: + +void TreeItemUtil::addFlatItemIfNeeded(TreeItem* root, TextTreeItem*& item, + int index) { + if (item == 0) { + item = new TextTreeItem(root); + root->insertChild(item, index); + } +} + +void TreeItemUtil::removeFlatItemIfNeeded(TextTreeItem*& item) { + if (item != 0) { + item->parent()->removeChild(item); + delete item; + item = 0; + } +} + +void TreeItemUtil::addNestedItemIfNeeded(TreeItem* root, TextTreeItem*& item, + int index, const QString& parentText) { + if (item == 0) { + TextTreeItem* parentItem = new TextTreeItem(root); + parentItem->setText(parentText); + + item = new TextTreeItem(parentItem); + parentItem->appendChild(item); + + root->insertChild(parentItem, index); + } +} + +void TreeItemUtil::removeNestedItemIfNeeded(TextTreeItem*& item) { + if (item != 0) { + item->parent()->parent()->removeChild(item->parent()); + delete item->parent(); + item = 0; + } +} Property changes on: trunk/ktutorial/ktutorial-editor/src/view/TreeItemUtil.cpp ___________________________________________________________________ Added: svn:eol-style + native Added: trunk/ktutorial/ktutorial-editor/src/view/TreeItemUtil.h =================================================================== --- trunk/ktutorial/ktutorial-editor/src/view/TreeItemUtil.h (rev 0) +++ trunk/ktutorial/ktutorial-editor/src/view/TreeItemUtil.h 2010-03-07 18:50:37 UTC (rev 119) @@ -0,0 +1,79 @@ +/*************************************************************************** + * 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 TREEITEMUTIL_H +#define TREEITEMUTIL_H + +#include "TreeItem.h" + +class TextTreeItem; + +/** + * Utility class for TreeItems. + * It provides some static methods to modify the structure of TreeItems, adding + * or removing children. + */ +class TreeItemUtil { +public: + + /** + * If there is no item, inserts a new TextTreeItem at the given index in the + * root item. + * The new TextTreeItem is stored in the given item. + * + * @param root The root item to add the item to. + * @param item The item to check and the variable to store the new item in. + * @param index The index in the root item to add the item. + */ + static void addFlatItemIfNeeded(TreeItem* root, TextTreeItem*& item, + int index); + + /** + * If there is an item, it is removed from its parent item. + * The given item is set to null after deleting it. + * + * @param item The item to remove, delete and clean. + */ + static void removeFlatItemIfNeeded(TextTreeItem*& item); + + /** + * If there is no item, inserts a new nested TextTreeItem at the given index + * in the root item. + * A TextTreeItem is inserted with the given parentText, and a new + * TextTreeItem child is appended to that parent. + * The child is stored in the given item. + * + * @param root The root item to add the parent item to. + * @param item The item to check and the place to store the new item. + * @param index The index in the root item to add the parent item. + * @param parentText The text to set in the added parent item. + */ + static void addNestedItemIfNeeded(TreeItem* root, TextTreeItem*& item, + int index, const QString& parentText); + + /** + * If there is an item, its parent is removed from its parent item. + * The given item is set to null after its parent is deleted. + * + * @param item The item to clean after removing and deleting its parent. + */ + static void removeNestedItemIfNeeded(TextTreeItem*& item); + +}; + +#endif Property changes on: trunk/ktutorial/ktutorial-editor/src/view/TreeItemUtil.h ___________________________________________________________________ Added: svn:eol-style + native Modified: trunk/ktutorial/ktutorial-editor/src/view/TutorialTreeItem.cpp =================================================================== --- trunk/ktutorial/ktutorial-editor/src/view/TutorialTreeItem.cpp 2010-03-07 07:38:57 UTC (rev 118) +++ trunk/ktutorial/ktutorial-editor/src/view/TutorialTreeItem.cpp 2010-03-07 18:50:37 UTC (rev 119) @@ -21,6 +21,7 @@ #include <KLocalizedString> #include "TextTreeItem.h" +#include "TreeItemUtil.h" #include "../Tutorial.h" //public: @@ -54,14 +55,14 @@ int childIndex = 0; if (tutorial->name().isEmpty()) { - removeFlatItemIfNeeded(mNameItem); + TreeItemUtil::removeFlatItemIfNeeded(mNameItem); if (!mTutorialId.isEmpty()) { mTutorialId = QString(); emit dataChanged(this); } } else { - addFlatItemIfNeeded(mNameItem, childIndex); + TreeItemUtil::addFlatItemIfNeeded(this, mNameItem, childIndex); mNameItem->setText(i18nc("@item", "Name: %1", tutorial->name())); mTutorialId = tutorial->id(); @@ -71,9 +72,9 @@ } if (tutorial->description().isEmpty()) { - removeFlatItemIfNeeded(mDescriptionItem); + TreeItemUtil::removeFlatItemIfNeeded(mDescriptionItem); } else { - addFlatItemIfNeeded(mDescriptionItem, childIndex); + TreeItemUtil::addFlatItemIfNeeded(this, mDescriptionItem, childIndex); mDescriptionItem->setText(i18nc("@item", "Description: %1", tutorial->description())); @@ -81,70 +82,32 @@ } if (tutorial->licenseText().isEmpty()) { - removeNestedItemIfNeeded(mLicenseItem); + TreeItemUtil::removeNestedItemIfNeeded(mLicenseItem); } else { - addNestedItemIfNeeded(mLicenseItem, childIndex, - i18nc("@item", "License:")); + TreeItemUtil::addNestedItemIfNeeded(this, mLicenseItem, childIndex, + i18nc("@item", "License:")); mLicenseItem->setText(tutorial->licenseText()); childIndex++; } if (tutorial->customSetupCode().isEmpty()) { - removeNestedItemIfNeeded(mSetupItem); + TreeItemUtil::removeNestedItemIfNeeded(mSetupItem); } else { - addNestedItemIfNeeded(mSetupItem, childIndex, - i18nc("@item", "Setup:")); + TreeItemUtil::addNestedItemIfNeeded(this, mSetupItem, childIndex, + i18nc("@item", "Setup:")); mSetupItem->setText(tutorial->customSetupCode()); childIndex++; } if (tutorial->customTearDownCode().isEmpty()) { - removeNestedItemIfNeeded(mTearDownItem); + TreeItemUtil::removeNestedItemIfNeeded(mTearDownItem); } else { - addNestedItemIfNeeded(mTearDownItem, childIndex, - i18nc("@item", "Tear down:")); + TreeItemUtil::addNestedItemIfNeeded(this, mTearDownItem, childIndex, + i18nc("@item", "Tear down:")); mTearDownItem->setText(tutorial->customTearDownCode()); childIndex++; } } - -void TutorialTreeItem::addFlatItemIfNeeded(TextTreeItem*& item, - int childIndex) { - if (item == 0) { - item = new TextTreeItem(this); - insertChild(item, childIndex); - } -} - -void TutorialTreeItem::removeFlatItemIfNeeded(TextTreeItem*& item) { - if (item != 0) { - removeChild(item); - delete item; - item = 0; - } -} - -void TutorialTreeItem::addNestedItemIfNeeded(TextTreeItem*& item, - int childIndex, - const QString& parentText) { - if (item == 0) { - TextTreeItem* parentItem = new TextTreeItem(this); - parentItem->setText(parentText); - - item = new TextTreeItem(parentItem); - parentItem->appendChild(item); - - insertChild(parentItem, childIndex); - } -} - -void TutorialTreeItem::removeNestedItemIfNeeded(TextTreeItem*& item) { - if (item != 0) { - removeChild(item->parent()); - delete item->parent(); - item = 0; - } -} Modified: trunk/ktutorial/ktutorial-editor/src/view/TutorialTreeItem.h =================================================================== --- trunk/ktutorial/ktutorial-editor/src/view/TutorialTreeItem.h 2010-03-07 07:38:57 UTC (rev 118) +++ trunk/ktutorial/ktutorial-editor/src/view/TutorialTreeItem.h 2010-03-07 18:50:37 UTC (rev 119) @@ -125,45 +125,6 @@ */ void update(Tutorial* tutorial); - /** - * If there is no item, inserts a new TextTreeItem at the given childIndex - * in this TutorialTreeItem. - * The new TextTreeItem is stored in the given item. - * - * @param item The item to check and the place to store the new item. - * @param childIndex The index in this TutorialTreeItem to add the item. - */ - void addFlatItemIfNeeded(TextTreeItem*& item, int childIndex); - - /** - * If there is an item, it is removed from this TutorialTreeItem. - * The given item is set to null after deleting it. - * - * @param item The item to remove, delete and clean. - */ - void removeFlatItemIfNeeded(TextTreeItem*& item); - - /** - * If there is no item, inserts a new nested TextTreeItem at the given - * childIndex in this TutorialTreeItem. - * A TextTreeItem is inserted with the given parentText, and a new - * TextTreeItem child is appended to that parent. - * The child is stored in the given item. - * - * @param item The item to check and the place to store the new item. - * @param childIndex The index in this TutorialTreeItem to add the item. - */ - void addNestedItemIfNeeded(TextTreeItem*& item, int childIndex, - const QString& parentText); - - /** - * If there is an item, its parent is removed from this TutorialTreeItem. - * The given item is set to null after its parent is deleted. - * - * @param item The item to clean after removing and deleting its parent. - */ - void removeNestedItemIfNeeded(TextTreeItem*& item); - }; #endif Modified: trunk/ktutorial/ktutorial-editor/tests/unit/view/CMakeLists.txt =================================================================== --- trunk/ktutorial/ktutorial-editor/tests/unit/view/CMakeLists.txt 2010-03-07 07:38:57 UTC (rev 118) +++ trunk/ktutorial/ktutorial-editor/tests/unit/view/CMakeLists.txt 2010-03-07 18:50:37 UTC (rev 119) @@ -16,7 +16,7 @@ ENDFOREACH(_className) ENDMACRO(UNIT_TESTS) -unit_tests(TextTreeItem TreeItem TreeModel TutorialTreeItem) +unit_tests(StepTreeItem TextTreeItem TreeItem TreeItemUtil TreeModel TutorialTreeItem) MACRO(MEM_TESTS) FOREACH(_testname ${ARGN}) @@ -24,4 +24,4 @@ ENDFOREACH(_testname) ENDMACRO(MEM_TESTS) -mem_tests(TextTreeItem TreeItem TreeModel TutorialTreeItem) +mem_tests(StepTreeItem TextTreeItem TreeItem TreeItemUtil TreeModel TutorialTreeItem) Added: trunk/ktutorial/ktutorial-editor/tests/unit/view/StepTreeItemTest.cpp =================================================================== --- trunk/ktutorial/ktutorial-editor/tests/unit/view/StepTreeItemTest.cpp (rev 0) +++ trunk/ktutorial/ktutorial-editor/tests/unit/view/StepTreeItemTest.cpp 2010-03-07 18:50:37 UTC (rev 119) @@ -0,0 +1,372 @@ +/*************************************************************************** + * 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 <QtTest> + +#include "StepTreeItem.h" + +#include <KLocalizedString> + +#include "../Step.h" + +class StepTreeItemTest: public QObject { +Q_OBJECT + +private slots: + + void initTestCase(); + + void testConstructor(); + void testConstructorFullStep(); + + void testStepSetId(); + void testStepSetIdChange(); + void testStepSetIdEmpty(); + + void testStepSetText(); + void testStepSetTextChange(); + void testStepSetTextEmpty(); + + void testStepSetCustomSetupCode(); + void testStepSetCustomSetupCodeChange(); + void testStepSetCustomSetupCodeEmpty(); + + void testStepSetCustomTearDownCode(); + void testStepSetCustomTearDownCodeChange(); + void testStepSetCustomTearDownCodeEmpty(); + + void testChildOrderWhenSettingDataInStep(); + void testChildOrderWhenUnsettingDataInStep(); + +private: + + int mTreeItemStarType; + + void assertText(TreeItem* textItem, const QString& licenseText) const; + void assertCustomSetupCode(TreeItem* setupItem, const QString& code) const; + void assertCustomTearDownCode(TreeItem* tearDownItem, + const QString& code) const; + + void assertDataChanged(const QSignalSpy& spy, int index, + TreeItem* item) const; + +}; + +class StubTreeItem: public TreeItem { +public: + virtual QString text() const { + return ""; + } +}; + +void StepTreeItemTest::initTestCase() { + //TreeItem* must be registered in order to be used with QSignalSpy + mTreeItemStarType = qRegisterMetaType<TreeItem*>("TreeItem*"); +} + +void StepTreeItemTest::testConstructor() { + Step step; + + StubTreeItem parent; + StepTreeItem item(&step, &parent); + + QCOMPARE(item.parent(), &parent); + QCOMPARE(item.text(), i18nc("@item", "Step")); + QCOMPARE(item.childCount(), 0); +} + +void StepTreeItemTest::testConstructorFullStep() { + Step step; + step.setId("The id"); + step.setText("The text"); + step.setCustomSetupCode("The setup code"); + step.setCustomTearDownCode("The tear down code"); + + StubTreeItem parent; + StepTreeItem item(&step, &parent); + + QCOMPARE(item.parent(), &parent); + QCOMPARE(item.text(), i18nc("@item", "Step %1", "The id")); + QCOMPARE(item.childCount(), 3); + assertText(item.child(0), "The text"); + assertCustomSetupCode(item.child(1), "The setup code"); + assertCustomTearDownCode(item.child(2), "The tear down code"); +} + +//TreeItem* must be declared as a metatype to be used in qvariant_cast +Q_DECLARE_METATYPE(TreeItem*); + +void StepTreeItemTest::testStepSetId() { + Step step; + StepTreeItem item(&step); + + //Setting the id changes the data returned by text() in the + //StepTreeItem itself + QSignalSpy dataChangedSpy(&item, SIGNAL(dataChanged(TreeItem*))); + + step.setId("The id"); + + QCOMPARE(item.text(), i18nc("@item", "Step %1", "The id")); + QCOMPARE(item.childCount(), 0); + QCOMPARE(dataChangedSpy.count(), 1); + assertDataChanged(dataChangedSpy, 0, &item); +} + +void StepTreeItemTest::testStepSetIdChange() { + Step step; + StepTreeItem item(&step); + + step.setId("The id"); + + QSignalSpy dataChangedSpy(&item, SIGNAL(dataChanged(TreeItem*))); + + step.setId("The id changed"); + + QCOMPARE(item.text(), i18nc("@item", "Step %1", "The id changed")); + QCOMPARE(item.childCount(), 0); + QCOMPARE(dataChangedSpy.count(), 1); + assertDataChanged(dataChangedSpy, 0, &item); +} + +void StepTreeItemTest::testStepSetIdEmpty() { + Step step; + StepTreeItem item(&step); + + step.setId("The id"); + + QSignalSpy dataChangedSpy(&item, SIGNAL(dataChanged(TreeItem*))); + + step.setId(""); + + QCOMPARE(item.text(), i18nc("@item", "Step")); + QCOMPARE(item.childCount(), 0); + QCOMPARE(dataChangedSpy.count(), 1); + assertDataChanged(dataChangedSpy, 0, &item); +} + +void StepTreeItemTest::testStepSetText() { + Step step; + StepTreeItem item(&step); + + step.setText("The text"); + + QCOMPARE(item.childCount(), 1); + assertText(item.child(0), "The text"); +} + +void StepTreeItemTest::testStepSetTextChange() { + Step step; + StepTreeItem item(&step); + + step.setText("The text"); + + QSignalSpy dataChangedSpy(item.child(0), SIGNAL(dataChanged(TreeItem*))); + + step.setText("The text changed"); + + QCOMPARE(item.childCount(), 1); + assertText(item.child(0), "The text changed"); + QCOMPARE(dataChangedSpy.count(), 1); + assertDataChanged(dataChangedSpy, 0, item.child(0)); +} + +void StepTreeItemTest::testStepSetTextEmpty() { + Step step; + StepTreeItem item(&step); + + step.setText("The text"); + step.setText(""); + + QCOMPARE(item.childCount(), 0); +} + +void StepTreeItemTest::testStepSetCustomSetupCode() { + Step step; + StepTreeItem item(&step); + + step.setCustomSetupCode("The setup code"); + + QCOMPARE(item.childCount(), 1); + assertCustomSetupCode(item.child(0), "The setup code"); +} + +void StepTreeItemTest::testStepSetCustomSetupCodeChange() { + Step step; + StepTreeItem item(&step); + + step.setCustomSetupCode("The setup code"); + + QSignalSpy dataChangedSpy(item.child(0)->child(0), + SIGNAL(dataChanged(TreeItem*))); + + step.setCustomSetupCode("The setup code changed"); + + QCOMPARE(item.childCount(), 1); + assertCustomSetupCode(item.child(0), "The setup code changed"); + QCOMPARE(dataChangedSpy.count(), 1); + assertDataChanged(dataChangedSpy, 0, item.child(0)->child(0)); +} + +void StepTreeItemTest::testStepSetCustomSetupCodeEmpty() { + Step step; + StepTreeItem item(&step); + + step.setCustomSetupCode("The setup code"); + step.setCustomSetupCode(""); + + QCOMPARE(item.childCount(), 0); +} + +void StepTreeItemTest::testStepSetCustomTearDownCode() { + Step step; + StepTreeItem item(&step); + + step.setCustomTearDownCode("The tear down code"); + + QCOMPARE(item.childCount(), 1); + assertCustomTearDownCode(item.child(0), "The tear down code"); +} + +void StepTreeItemTest::testStepSetCustomTearDownCodeChange() { + Step step; + StepTreeItem item(&step); + + step.setCustomTearDownCode("The tear down code"); + + QSignalSpy dataChangedSpy(item.child(0)->child(0), + SIGNAL(dataChanged(TreeItem*))); + + step.setCustomTearDownCode("The tear down code changed"); + + QCOMPARE(item.childCount(), 1); + assertCustomTearDownCode(item.child(0), "The tear down code changed"); + QCOMPARE(dataChangedSpy.count(), 1); + assertDataChanged(dataChangedSpy, 0, item.child(0)->child(0)); +} + +void StepTreeItemTest::testStepSetCustomTearDownCodeEmpty() { + Step step; + StepTreeItem item(&step); + + step.setCustomTearDownCode("The tear down code"); + step.setCustomTearDownCode(""); + + QCOMPARE(item.childCount(), 0); +} + +void StepTreeItemTest::testChildOrderWhenSettingDataInStep() { + Step step; + StepTreeItem item(&step); + + step.setCustomSetupCode("The setup code"); + + QCOMPARE(item.text(), i18nc("@item", "Step")); + QCOMPARE(item.childCount(), 1); + assertCustomSetupCode(item.child(0), "The setup code"); + + step.setText("The text"); + + QCOMPARE(item.text(), i18nc("@item", "Step")); + QCOMPARE(item.childCount(), 2); + assertText(item.child(0), "The text"); + assertCustomSetupCode(item.child(1), "The setup code"); + + step.setCustomTearDownCode("The tear down code"); + + QCOMPARE(item.text(), i18nc("@item", "Step")); + QCOMPARE(item.childCount(), 3); + assertText(item.child(0), "The text"); + assertCustomSetupCode(item.child(1), "The setup code"); + assertCustomTearDownCode(item.child(2), "The tear down code"); + + step.setId("The id"); + + QCOMPARE(item.text(), i18nc("@item", "Step %1", "The id")); + QCOMPARE(item.childCount(), 3); + assertText(item.child(0), "The text"); + assertCustomSetupCode(item.child(1), "The setup code"); + assertCustomTearDownCode(item.child(2), "The tear down code"); +} + +void StepTreeItemTest::testChildOrderWhenUnsettingDataInStep() { + Step step; + step.setId("The id"); + step.setText("The text"); + step.setCustomSetupCode("The setup code"); + step.setCustomTearDownCode("The tear down code"); + + StepTreeItem item(&step); + + step.setText(""); + + QCOMPARE(item.text(), i18nc("@item", "Step %1", "The id")); + QCOMPARE(item.childCount(), 2); + assertCustomSetupCode(item.child(0), "The setup code"); + assertCustomTearDownCode(item.child(1), "The tear down code"); + + step.setCustomTearDownCode(""); + + QCOMPARE(item.text(), i18nc("@item", "Step %1", "The id")); + QCOMPARE(item.childCount(), 1); + assertCustomSetupCode(item.child(0), "The setup code"); + + step.setId(""); + + QCOMPARE(item.text(), i18nc("@item", "Step")); + QCOMPARE(item.childCount(), 1); + assertCustomSetupCode(item.child(0), "The setup code"); + + step.setCustomSetupCode(""); + + QCOMPARE(item.text(), i18nc("@item", "Step")); + QCOMPARE(item.childCount(), 0); +} + +/////////////////////////////////// Helpers //////////////////////////////////// + +void StepTreeItemTest::assertText(TreeItem* textItem, + const QString& name) const { + QCOMPARE(textItem->text(), i18nc("@item", "Text: %1", name)); +} + +void StepTreeItemTest::assertCustomSetupCode(TreeItem* setupItem, + const QString& code) const { + QCOMPARE(setupItem->text(), i18nc("@item", "Setup:")); + QCOMPARE(setupItem->childCount(), 1); + QCOMPARE(setupItem->child(0)->text(), i18nc("@item", code.toAscii())); +} + +void StepTreeItemTest::assertCustomTearDownCode(TreeItem* tearDownItem, + const QString& code) const { + QCOMPARE(tearDownItem->text(), i18nc("@item", "Tear down:")); + QCOMPARE(tearDownItem->childCount(), 1); + QCOMPARE(tearDownItem->child(0)->text(), i18nc("@item", code.toAscii())); +} + +void StepTreeItemTest::assertDataChanged(const QSignalSpy& spy, int index, + TreeItem* item) const { + QCOMPARE(spy.at(index).count(), 1); + + QVariant argument = spy.at(index).at(0); + QCOMPARE(argument.userType(), mTreeItemStarType); + QCOMPARE(qvariant_cast<TreeItem*>(argument), item); +} + +QTEST_MAIN(StepTreeItemTest) + +#include "StepTreeItemTest.moc" Property changes on: trunk/ktutorial/ktutorial-editor/tests/unit/view/StepTreeItemTest.cpp ___________________________________________________________________ Added: svn:eol-style + native Added: trunk/ktutorial/ktutorial-editor/tests/unit/view/TreeItemUtilTest.cpp =================================================================== --- trunk/ktutorial/ktutorial-editor/tests/unit/view/TreeItemUtilTest.cpp (rev 0) +++ trunk/ktutorial/ktutorial-editor/tests/unit/view/TreeItemUtilTest.cpp 2010-03-07 18:50:37 UTC (rev 119) @@ -0,0 +1,191 @@ +/*************************************************************************** + * 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 <QtTest> + +#include "TreeItemUtil.h" + +#include "TextTreeItem.h" + +class TreeItemUtilTest: public QObject { +Q_OBJECT + +private slots: + + void testAddFlatItem(); + void testAddFlatItemAlreadyAdded(); + + void testRemoveFlatItem(); + void testRemoveFlatItemAlreadyRemoved(); + + void testAddNestedItem(); + void testAddNestedItemAlreadyAdded(); + + void testRemoveNestedItem(); + void testRemoveNestedItemAlreadyRemoved(); + +}; + +class StubTreeItem: public TreeItem { +public: + virtual QString text() const { + return ""; + } +}; + +void TreeItemUtilTest::testAddFlatItem() { + StubTreeItem parent; + TreeItem* lastItem = new TextTreeItem(&parent); + parent.appendChild(lastItem); + + TextTreeItem* item = 0; + + TreeItemUtil::addFlatItemIfNeeded(&parent, item, 0); + + QVERIFY(item); + QCOMPARE(item->parent(), &parent); + QCOMPARE(parent.childCount(), 2); + QCOMPARE(parent.child(0), item); + QCOMPARE(parent.child(1), lastItem); +} + +void TreeItemUtilTest::testAddFlatItemAlreadyAdded() { + StubTreeItem parent; + TreeItem* lastItem = new TextTreeItem(&parent); + parent.appendChild(lastItem); + + TextTreeItem* item = 0; + + TreeItemUtil::addFlatItemIfNeeded(&parent, item, 0); + TreeItemUtil::addFlatItemIfNeeded(&parent, item, 0); + + QVERIFY(item); + QCOMPARE(item->parent(), &parent); + QCOMPARE(parent.childCount(), 2); + QCOMPARE(parent.child(0), item); + QCOMPARE(parent.child(1), lastItem); +} + +void TreeItemUtilTest::testRemoveFlatItem() { + StubTreeItem parent; + TreeItem* lastItem = new TextTreeItem(&parent); + parent.appendChild(lastItem); + + TextTreeItem* item = new TextTreeItem(&parent); + parent.insertChild(item, 0); + + TreeItemUtil::removeFlatItemIfNeeded(item); + + QVERIFY(!item); + QCOMPARE(parent.childCount(), 1); + QCOMPARE(parent.child(0), lastItem); +} + +void TreeItemUtilTest::testRemoveFlatItemAlreadyRemoved() { + StubTreeItem parent; + TreeItem* lastItem = new TextTreeItem(&parent); + parent.appendChild(lastItem); + + TextTreeItem* item = new TextTreeItem(&parent); + parent.insertChild(item, 0); + + TreeItemUtil::removeFlatItemIfNeeded(item); + TreeItemUtil::removeFlatItemIfNeeded(item); + + QVERIFY(!item); + QCOMPARE(parent.childCount(), 1); + QCOMPARE(parent.child(0), lastItem); +} + +void TreeItemUtilTest::testAddNestedItem() { + StubTreeItem parent; + TreeItem* lastItem = new TextTreeItem(&parent); + parent.appendChild(lastItem); + + TextTreeItem* item = 0; + + TreeItemUtil::addNestedItemIfNeeded(&parent, item, 0, "Parent text"); + + QVERIFY(item); + QCOMPARE(parent.childCount(), 2); + QCOMPARE(item->parent(), parent.child(0)); + QCOMPARE(parent.child(0)->parent(), &parent); + QCOMPARE(parent.child(0)->text(), QString("Parent text")); + QCOMPARE(parent.child(0)->childCount(), 1); + QCOMPARE(parent.child(0)->child(0), item); + QCOMPARE(parent.child(1), lastItem); +} + +void TreeItemUtilTest::testAddNestedItemAlreadyAdded() { + StubTreeItem parent; + TreeItem* lastItem = new TextTreeItem(&parent); + parent.appendChild(lastItem); + + TextTreeItem* item = 0; + + TreeItemUtil::addNestedItemIfNeeded(&parent, item, 0, "Parent text"); + TreeItemUtil::addNestedItemIfNeeded(&parent, item, 0, "Parent text2"); + + QVERIFY(item); + QCOMPARE(parent.childCount(), 2); + QCOMPARE(item->parent(), parent.child(0)); + QCOMPARE(parent.child(0)->parent(), &parent); + QCOMPARE(parent.child(0)->text(), QString("Parent text")); + QCOMPARE(parent.child(0)->childCount(), 1); + QCOMPARE(parent.child(0)->child(0), item); + QCOMPARE(parent.child(1), lastItem); +} + +void TreeItemUtilTest::testRemoveNestedItem() { + StubTreeItem parent; + TreeItem* lastItem = new TextTreeItem(&parent); + parent.appendChild(lastItem); + + TextTreeItem* item = new TextTreeItem(&parent); + parent.insertChild(item, 0); + TextTreeItem* childItem = new TextTreeItem(item); + item->appendChild(childItem); + + TreeItemUtil::removeNestedItemIfNeeded(childItem); + + QVERIFY(!childItem); + QCOMPARE(parent.childCount(), 1); + QCOMPARE(parent.child(0), lastItem); +} + +void TreeItemUtilTest::testRemoveNestedItemAlreadyRemoved() { + StubTreeItem parent; + TreeItem* lastItem = new TextTreeItem(&parent); + parent.appendChild(lastItem); + + TextTreeItem* item = new TextTreeItem(&parent); + parent.insertChild(item, 0); + TextTreeItem* childItem = new TextTreeItem(item); + item->appendChild(childItem); + + TreeItemUtil::removeNestedItemIfNeeded(childItem); + TreeItemUtil::removeNestedItemIfNeeded(childItem); + + QVERIFY(!childItem); + QCOMPARE(parent.childCount(), 1); + QCOMPARE(parent.child(0), lastItem); +} + +QTEST_MAIN(TreeItemUtilTest) + +#include "TreeItemUtilTest.moc" Property changes on: trunk/ktutorial/ktutorial-editor/tests/unit/view/TreeItemUtilTest.cpp ___________________________________________________________________ Added: svn:eol-style + native This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <dan...@us...> - 2010-03-07 07:39:04
|
Revision: 118 http://ktutorial.svn.sourceforge.net/ktutorial/?rev=118&view=rev Author: danxuliu Date: 2010-03-07 07:38:57 +0000 (Sun, 07 Mar 2010) Log Message: ----------- Add basic version of Step class to store the data of a step. Modified Paths: -------------- trunk/ktutorial/ktutorial-editor/src/CMakeLists.txt trunk/ktutorial/ktutorial-editor/tests/unit/CMakeLists.txt Added Paths: ----------- trunk/ktutorial/ktutorial-editor/src/Step.cpp trunk/ktutorial/ktutorial-editor/src/Step.h trunk/ktutorial/ktutorial-editor/tests/unit/StepTest.cpp Modified: trunk/ktutorial/ktutorial-editor/src/CMakeLists.txt =================================================================== --- trunk/ktutorial/ktutorial-editor/src/CMakeLists.txt 2010-03-07 07:21:18 UTC (rev 117) +++ trunk/ktutorial/ktutorial-editor/src/CMakeLists.txt 2010-03-07 07:38:57 UTC (rev 118) @@ -8,6 +8,7 @@ set(ktutorial_editor_SRCS KTutorialEditor.cpp + Step.cpp Tutorial.cpp ) Added: trunk/ktutorial/ktutorial-editor/src/Step.cpp =================================================================== --- trunk/ktutorial/ktutorial-editor/src/Step.cpp (rev 0) +++ trunk/ktutorial/ktutorial-editor/src/Step.cpp 2010-03-07 07:38:57 UTC (rev 118) @@ -0,0 +1,64 @@ +/*************************************************************************** + * 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 "Step.h" + +//public: + +Step::Step(QObject* parent) { +} + +QString Step::id() const { + return mId; +} + +void Step::setId(const QString& id) { + mId = id; + + emit dataChanged(this); +} + +QString Step::text() const { + return mText; +} + +void Step::setText(const QString& text) { + mText = text; + + emit dataChanged(this); +} + +QString Step::customSetupCode() const { + return mCustomSetupCode; +} + +void Step::setCustomSetupCode(const QString& code) { + mCustomSetupCode = code; + + emit dataChanged(this); +} + +QString Step::customTearDownCode() const { + return mCustomTearDownCode; +} + +void Step::setCustomTearDownCode(const QString& code) { + mCustomTearDownCode = code; + + emit dataChanged(this); +} Property changes on: trunk/ktutorial/ktutorial-editor/src/Step.cpp ___________________________________________________________________ Added: svn:eol-style + native Added: trunk/ktutorial/ktutorial-editor/src/Step.h =================================================================== --- trunk/ktutorial/ktutorial-editor/src/Step.h (rev 0) +++ trunk/ktutorial/ktutorial-editor/src/Step.h 2010-03-07 07:38:57 UTC (rev 118) @@ -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/>. * + ***************************************************************************/ + +#ifndef STEP_H +#define STEP_H + +#include <QObject> + +/** + * 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. + */ +class Step: public QObject { +Q_OBJECT +public: + + Step(QObject* parent = 0); + + QString id() const; + void setId(const QString& id); + + QString text() const; + void setText(const QString& text); + + QString customSetupCode() const; + void setCustomSetupCode(const QString& code); + + QString customTearDownCode() const; + void setCustomTearDownCode(const QString& code); + +Q_SIGNALS: + + /** + * Emitted when any data in the step changed. + * + * @param step This step. + */ + void dataChanged(Step* step); + +private: + + QString mId; + QString mText; + QString mCustomSetupCode; + QString mCustomTearDownCode; + +}; + +#endif Property changes on: trunk/ktutorial/ktutorial-editor/src/Step.h ___________________________________________________________________ Added: svn:eol-style + native Modified: trunk/ktutorial/ktutorial-editor/tests/unit/CMakeLists.txt =================================================================== --- trunk/ktutorial/ktutorial-editor/tests/unit/CMakeLists.txt 2010-03-07 07:21:18 UTC (rev 117) +++ trunk/ktutorial/ktutorial-editor/tests/unit/CMakeLists.txt 2010-03-07 07:38:57 UTC (rev 118) @@ -14,7 +14,7 @@ ENDFOREACH(_className) ENDMACRO(UNIT_TESTS) -unit_tests(Tutorial) +unit_tests(Step Tutorial) MACRO(MEM_TESTS) FOREACH(_testname ${ARGN}) @@ -22,4 +22,4 @@ ENDFOREACH(_testname) ENDMACRO(MEM_TESTS) -mem_tests(Tutorial) +mem_tests(Step Tutorial) Added: trunk/ktutorial/ktutorial-editor/tests/unit/StepTest.cpp =================================================================== --- trunk/ktutorial/ktutorial-editor/tests/unit/StepTest.cpp (rev 0) +++ trunk/ktutorial/ktutorial-editor/tests/unit/StepTest.cpp 2010-03-07 07:38:57 UTC (rev 118) @@ -0,0 +1,107 @@ +/*************************************************************************** + * 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 <QtTest> + +#include "Step.h" + +class StepTest: public QObject { +Q_OBJECT + +private slots: + + void testSetId(); + + void testSetText(); + + void testSetCustomSetupCode(); + + void testSetCustomTearDownCode(); + +}; + +//Step* must be declared as a metatype to be used in qvariant_cast +Q_DECLARE_METATYPE(Step*); + +void StepTest::testSetId() { + Step step; + + //Step* must be registered in order to be used with QSignalSpy + int stepStarType = qRegisterMetaType<Step*>("Step*"); + QSignalSpy dataChangedSpy(&step, SIGNAL(dataChanged(Step*))); + + step.setId("The id"); + + QCOMPARE(step.id(), QString("The id")); + QCOMPARE(dataChangedSpy.count(), 1); + QVariant argument = dataChangedSpy.at(0).at(0); + QCOMPARE(argument.userType(), stepStarType); + QCOMPARE(qvariant_cast<Step*>(argument), &step); +} + +void StepTest::testSetText() { + Step step; + + //Step* must be registered in order to be used with QSignalSpy + int stepStarType = qRegisterMetaType<Step*>("Step*"); + QSignalSpy dataChangedSpy(&step, SIGNAL(dataChanged(Step*))); + + step.setText("The text"); + + QCOMPARE(step.text(), QString("The text")); + QCOMPARE(dataChangedSpy.count(), 1); + QVariant argument = dataChangedSpy.at(0).at(0); + QCOMPARE(argument.userType(), stepStarType); + QCOMPARE(qvariant_cast<Step*>(argument), &step); +} + +void StepTest::testSetCustomSetupCode() { + Step step; + + //Step* must be registered in order to be used with QSignalSpy + int stepStarType = qRegisterMetaType<Step*>("Step*"); + QSignalSpy dataChangedSpy(&step, SIGNAL(dataChanged(Step*))); + + step.setCustomSetupCode("The setup code"); + + QCOMPARE(step.customSetupCode(), QString("The setup code")); + QCOMPARE(dataChangedSpy.count(), 1); + QVariant argument = dataChangedSpy.at(0).at(0); + QCOMPARE(argument.userType(), stepStarType); + QCOMPARE(qvariant_cast<Step*>(argument), &step); +} + +void StepTest::testSetCustomTearDownCode() { + Step step; + + //Step* must be registered in order to be used with QSignalSpy + int stepStarType = qRegisterMetaType<Step*>("Step*"); + QSignalSpy dataChangedSpy(&step, SIGNAL(dataChanged(Step*))); + + step.setCustomTearDownCode("The tear down code"); + + QCOMPARE(step.customTearDownCode(), QString("The tear down code")); + QCOMPARE(dataChangedSpy.count(), 1); + QVariant argument = dataChangedSpy.at(0).at(0); + QCOMPARE(argument.userType(), stepStarType); + QCOMPARE(qvariant_cast<Step*>(argument), &step); +} + +QTEST_MAIN(StepTest) + +#include "StepTest.moc" Property changes on: trunk/ktutorial/ktutorial-editor/tests/unit/StepTest.cpp ___________________________________________________________________ Added: svn:eol-style + native This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <dan...@us...> - 2010-03-07 07:21:24
|
Revision: 117 http://ktutorial.svn.sourceforge.net/ktutorial/?rev=117&view=rev Author: danxuliu Date: 2010-03-07 07:21:18 +0000 (Sun, 07 Mar 2010) Log Message: ----------- Add TutorialTreeItem class to show a Tutorial in a TreeModel. Modified Paths: -------------- trunk/ktutorial/ktutorial-editor/src/view/CMakeLists.txt trunk/ktutorial/ktutorial-editor/tests/unit/view/CMakeLists.txt Added Paths: ----------- trunk/ktutorial/ktutorial-editor/src/view/TutorialTreeItem.cpp trunk/ktutorial/ktutorial-editor/src/view/TutorialTreeItem.h trunk/ktutorial/ktutorial-editor/tests/unit/view/TutorialTreeItemTest.cpp Modified: trunk/ktutorial/ktutorial-editor/src/view/CMakeLists.txt =================================================================== --- trunk/ktutorial/ktutorial-editor/src/view/CMakeLists.txt 2010-03-07 06:21:19 UTC (rev 116) +++ trunk/ktutorial/ktutorial-editor/src/view/CMakeLists.txt 2010-03-07 07:21:18 UTC (rev 117) @@ -4,8 +4,9 @@ TextTreeItem.cpp TreeItem.cpp TreeModel.cpp + TutorialTreeItem.cpp ) kde4_add_library(ktutorial_editor_view ${ktutorial_editor_view_SRCS}) -target_link_libraries(ktutorial_editor_view ${KDE4_KDEUI_LIBS}) +target_link_libraries(ktutorial_editor_view ktutorial_editor ${KDE4_KDEUI_LIBS}) Added: trunk/ktutorial/ktutorial-editor/src/view/TutorialTreeItem.cpp =================================================================== --- trunk/ktutorial/ktutorial-editor/src/view/TutorialTreeItem.cpp (rev 0) +++ trunk/ktutorial/ktutorial-editor/src/view/TutorialTreeItem.cpp 2010-03-07 07:21:18 UTC (rev 117) @@ -0,0 +1,150 @@ +/*************************************************************************** + * 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 "TutorialTreeItem.h" + +#include <KLocalizedString> + +#include "TextTreeItem.h" +#include "../Tutorial.h" + +//public: + +TutorialTreeItem::TutorialTreeItem(Tutorial* tutorial, TreeItem* parent): + TreeItem(parent) { + Q_ASSERT(tutorial); + + mNameItem = 0; + mDescriptionItem = 0; + mLicenseItem = 0; + mSetupItem = 0; + mTearDownItem = 0; + + update(tutorial); + connect(tutorial, SIGNAL(dataChanged(Tutorial*)), + this, SLOT(update(Tutorial*))); +} + +QString TutorialTreeItem::text() const { + if (mTutorialId.isEmpty()) { + return i18nc("@item", "Tutorial"); + } + + return i18nc("@item", "Tutorial %1", mTutorialId); +} + +//private slots: + +void TutorialTreeItem::update(Tutorial* tutorial) { + int childIndex = 0; + + if (tutorial->name().isEmpty()) { + removeFlatItemIfNeeded(mNameItem); + + if (!mTutorialId.isEmpty()) { + mTutorialId = QString(); + emit dataChanged(this); + } + } else { + addFlatItemIfNeeded(mNameItem, childIndex); + mNameItem->setText(i18nc("@item", "Name: %1", tutorial->name())); + + mTutorialId = tutorial->id(); + emit dataChanged(this); + + childIndex++; + } + + if (tutorial->description().isEmpty()) { + removeFlatItemIfNeeded(mDescriptionItem); + } else { + addFlatItemIfNeeded(mDescriptionItem, childIndex); + mDescriptionItem->setText(i18nc("@item", "Description: %1", + tutorial->description())); + + childIndex++; + } + + if (tutorial->licenseText().isEmpty()) { + removeNestedItemIfNeeded(mLicenseItem); + } else { + addNestedItemIfNeeded(mLicenseItem, childIndex, + i18nc("@item", "License:")); + mLicenseItem->setText(tutorial->licenseText()); + + childIndex++; + } + + if (tutorial->customSetupCode().isEmpty()) { + removeNestedItemIfNeeded(mSetupItem); + } else { + addNestedItemIfNeeded(mSetupItem, childIndex, + i18nc("@item", "Setup:")); + mSetupItem->setText(tutorial->customSetupCode()); + + childIndex++; + } + + if (tutorial->customTearDownCode().isEmpty()) { + removeNestedItemIfNeeded(mTearDownItem); + } else { + addNestedItemIfNeeded(mTearDownItem, childIndex, + i18nc("@item", "Tear down:")); + mTearDownItem->setText(tutorial->customTearDownCode()); + + childIndex++; + } +} + +void TutorialTreeItem::addFlatItemIfNeeded(TextTreeItem*& item, + int childIndex) { + if (item == 0) { + item = new TextTreeItem(this); + insertChild(item, childIndex); + } +} + +void TutorialTreeItem::removeFlatItemIfNeeded(TextTreeItem*& item) { + if (item != 0) { + removeChild(item); + delete item; + item = 0; + } +} + +void TutorialTreeItem::addNestedItemIfNeeded(TextTreeItem*& item, + int childIndex, + const QString& parentText) { + if (item == 0) { + TextTreeItem* parentItem = new TextTreeItem(this); + parentItem->setText(parentText); + + item = new TextTreeItem(parentItem); + parentItem->appendChild(item); + + insertChild(parentItem, childIndex); + } +} + +void TutorialTreeItem::removeNestedItemIfNeeded(TextTreeItem*& item) { + if (item != 0) { + removeChild(item->parent()); + delete item->parent(); + item = 0; + } +} Property changes on: trunk/ktutorial/ktutorial-editor/src/view/TutorialTreeItem.cpp ___________________________________________________________________ Added: svn:eol-style + native Added: trunk/ktutorial/ktutorial-editor/src/view/TutorialTreeItem.h =================================================================== --- trunk/ktutorial/ktutorial-editor/src/view/TutorialTreeItem.h (rev 0) +++ trunk/ktutorial/ktutorial-editor/src/view/TutorialTreeItem.h 2010-03-07 07:21:18 UTC (rev 117) @@ -0,0 +1,169 @@ +/*************************************************************************** + * 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 TUTORIALTREEITEM_H +#define TUTORIALTREEITEM_H + +#include "TreeItem.h" + +class TextTreeItem; +class Tutorial; + +/** + * A TreeItem that represents a Tutorial. + * The tree representation of a tutorial is: + * Tutorial theId + * |-Name: the name + * |-Description: the description + * |-License: + * | -The license text + * |-Setup: + * | -The custom setup code + * --Tear down: + * -The custom tear down code + * + * The items only appear if they have some data to show. For example, if only + * the name of the Tutorial is set, its representation is: + * Tutorial theId + * -Name: the name + * + * Note that composed elements like license don't appear at all, not even the + * parent item with just "License:". + * + * Whenever the tutorial data changes, the TutorialTreeItem and its child items + * are updated as needed. + * + * 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 name, the name item + * will appear first and then the setup code item. + */ +class TutorialTreeItem: public TreeItem { +Q_OBJECT +public: + + /** + * Creates a new TutorialTreeItem for the given Tutorial and with the given + * parent. + * + * @param tutorial The tutorial to represent. + * @param parent The parent TreeItem. + */ + explicit TutorialTreeItem(Tutorial* tutorial, TreeItem* parent = 0); + + /** + * Returns "Tutorial " and the id of the tutorial, or just "Tutorial" if + * there is no id. + * + * @return The text for this TreeItem. + */ + virtual QString text() const; + +private: + + /** + * The id of the tutorial. + */ + QString mTutorialId; + + /** + * The child item containing the name. + */ + TextTreeItem* mNameItem; + + /** + * The child item containing the description. + */ + TextTreeItem* mDescriptionItem; + + /** + * The child item containing the license. + * This is a nested item. Its parent (and the direct child of this + * TutorialTreeItem) contains the "License:" text. + */ + TextTreeItem* mLicenseItem; + + /** + * The child item containing the custom setup code. + * This is a nested item. Its parent (and the direct child of this + * TutorialTreeItem) contains the "Setup:" text. + */ + TextTreeItem* mSetupItem; + + /** + * The child item containing the custom tear down code. + * This is a nested item. Its parent (and the direct child of this + * TutorialTreeItem) contains the "Tear down:" text. + */ + TextTreeItem* mTearDownItem; + +private Q_SLOTS: + + /** + * Updates this TutorialTreeItem when the data of its tutorial changed. + * If a child item is needed to show some data, it is inserted or updated + * (depending on whether it existed previously or not). + * If the child item is no longer needed, it is removed. + * + * Items may be flat or nested, depending on the data to show. + * + * @param tutorial The tutorial. + */ + void update(Tutorial* tutorial); + + /** + * If there is no item, inserts a new TextTreeItem at the given childIndex + * in this TutorialTreeItem. + * The new TextTreeItem is stored in the given item. + * + * @param item The item to check and the place to store the new item. + * @param childIndex The index in this TutorialTreeItem to add the item. + */ + void addFlatItemIfNeeded(TextTreeItem*& item, int childIndex); + + /** + * If there is an item, it is removed from this TutorialTreeItem. + * The given item is set to null after deleting it. + * + * @param item The item to remove, delete and clean. + */ + void removeFlatItemIfNeeded(TextTreeItem*& item); + + /** + * If there is no item, inserts a new nested TextTreeItem at the given + * childIndex in this TutorialTreeItem. + * A TextTreeItem is inserted with the given parentText, and a new + * TextTreeItem child is appended to that parent. + * The child is stored in the given item. + * + * @param item The item to check and the place to store the new item. + * @param childIndex The index in this TutorialTreeItem to add the item. + */ + void addNestedItemIfNeeded(TextTreeItem*& item, int childIndex, + const QString& parentText); + + /** + * If there is an item, its parent is removed from this TutorialTreeItem. + * The given item is set to null after its parent is deleted. + * + * @param item The item to clean after removing and deleting its parent. + */ + void removeNestedItemIfNeeded(TextTreeItem*& item); + +}; + +#endif Property changes on: trunk/ktutorial/ktutorial-editor/src/view/TutorialTreeItem.h ___________________________________________________________________ Added: svn:eol-style + native Modified: trunk/ktutorial/ktutorial-editor/tests/unit/view/CMakeLists.txt =================================================================== --- trunk/ktutorial/ktutorial-editor/tests/unit/view/CMakeLists.txt 2010-03-07 06:21:19 UTC (rev 116) +++ trunk/ktutorial/ktutorial-editor/tests/unit/view/CMakeLists.txt 2010-03-07 07:21:18 UTC (rev 117) @@ -12,11 +12,11 @@ FOREACH(_className ${ARGN}) set(_testName ${_className}Test) kde4_add_unit_test(${_testName} TESTNAME ktutorial-editor-unit-${_testName} ${_testName}.cpp) - target_link_libraries(${_testName} ktutorial_editor_view ${QT_QTTEST_LIBRARY}) + target_link_libraries(${_testName} ktutorial_editor ktutorial_editor_view ${QT_QTTEST_LIBRARY}) ENDFOREACH(_className) ENDMACRO(UNIT_TESTS) -unit_tests(TextTreeItem TreeItem TreeModel) +unit_tests(TextTreeItem TreeItem TreeModel TutorialTreeItem) MACRO(MEM_TESTS) FOREACH(_testname ${ARGN}) @@ -24,4 +24,4 @@ ENDFOREACH(_testname) ENDMACRO(MEM_TESTS) -mem_tests(TextTreeItem TreeItem TreeModel) +mem_tests(TextTreeItem TreeItem TreeModel TutorialTreeItem) Added: trunk/ktutorial/ktutorial-editor/tests/unit/view/TutorialTreeItemTest.cpp =================================================================== --- trunk/ktutorial/ktutorial-editor/tests/unit/view/TutorialTreeItemTest.cpp (rev 0) +++ trunk/ktutorial/ktutorial-editor/tests/unit/view/TutorialTreeItemTest.cpp 2010-03-07 07:21:18 UTC (rev 117) @@ -0,0 +1,462 @@ +/*************************************************************************** + * 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 <QtTest> + +#include "TutorialTreeItem.h" + +#include <KLocalizedString> + +#include "../Tutorial.h" + +class TutorialTreeItemTest: public QObject { +Q_OBJECT + +private slots: + + void initTestCase(); + + void testConstructor(); + void testConstructorFullTutorial(); + + void testTutorialSetName(); + void testTutorialSetNameChange(); + void testTutorialSetNameEmpty(); + + void testTutorialSetDescription(); + void testTutorialSetDescriptionChange(); + void testTutorialSetDescriptionEmpty(); + + void testTutorialSetLicenseText(); + void testTutorialSetLicenseTextChange(); + void testTutorialSetLicenseTextEmpty(); + + void testTutorialSetCustomSetupCode(); + void testTutorialSetCustomSetupCodeChange(); + void testTutorialSetCustomSetupCodeEmpty(); + + void testTutorialSetCustomTearDownCode(); + void testTutorialSetCustomTearDownCodeChange(); + void testTutorialSetCustomTearDownCodeEmpty(); + + void testChildOrderWhenSettingDataInTutorial(); + void testChildOrderWhenUnsettingDataInTutorial(); + +private: + + int mTreeItemStarType; + + void assertName(TreeItem* nameItem, const QString& name) const; + void assertDescription(TreeItem* descriptionItem, + const QString& description) const; + void assertLicenseText(TreeItem* licenseItem, + const QString& licenseText) const; + void assertCustomSetupCode(TreeItem* setupItem, const QString& code) const; + void assertCustomTearDownCode(TreeItem* tearDownItem, + const QString& code) const; + + void assertDataChanged(const QSignalSpy& spy, int index, + TreeItem* item) const; + +}; + +class StubTreeItem: public TreeItem { +public: + virtual QString text() const { + return ""; + } +}; + +void TutorialTreeItemTest::initTestCase() { + //TreeItem* must be registered in order to be used with QSignalSpy + mTreeItemStarType = qRegisterMetaType<TreeItem*>("TreeItem*"); +} + +void TutorialTreeItemTest::testConstructor() { + Tutorial tutorial; + + StubTreeItem parent; + TutorialTreeItem item(&tutorial, &parent); + + QCOMPARE(item.parent(), &parent); + QCOMPARE(item.text(), i18nc("@item", "Tutorial")); + QCOMPARE(item.childCount(), 0); +} + +void TutorialTreeItemTest::testConstructorFullTutorial() { + Tutorial tutorial; + tutorial.setName("The name"); + tutorial.setDescription("The description"); + tutorial.setLicenseText("The license text"); + tutorial.setCustomSetupCode("The setup code"); + tutorial.setCustomTearDownCode("The tear down code"); + + StubTreeItem parent; + TutorialTreeItem item(&tutorial, &parent); + + QCOMPARE(item.parent(), &parent); + QCOMPARE(item.text(), i18nc("@item", "Tutorial %1", "theName")); + QCOMPARE(item.childCount(), 5); + 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"); +} + +//TreeItem* must be declared as a metatype to be used in qvariant_cast +Q_DECLARE_METATYPE(TreeItem*); + +void TutorialTreeItemTest::testTutorialSetName() { + Tutorial tutorial; + TutorialTreeItem item(&tutorial); + + //Setting the name changes the data returned by text() in the + //TutorialTreeItem itself, as the id is based on the name + QSignalSpy dataChangedSpy(&item, SIGNAL(dataChanged(TreeItem*))); + + tutorial.setName("The name"); + + QCOMPARE(item.text(), i18nc("@item", "Tutorial %1", "theName")); + QCOMPARE(item.childCount(), 1); + assertName(item.child(0), "The name"); + QCOMPARE(dataChangedSpy.count(), 1); + assertDataChanged(dataChangedSpy, 0, &item); +} + +void TutorialTreeItemTest::testTutorialSetNameChange() { + Tutorial tutorial; + TutorialTreeItem item(&tutorial); + + tutorial.setName("The name"); + + QSignalSpy dataChangedRootSpy(&item, SIGNAL(dataChanged(TreeItem*))); + QSignalSpy dataChangedSpy(item.child(0), SIGNAL(dataChanged(TreeItem*))); + + tutorial.setName("The name changed"); + + QCOMPARE(item.text(), i18nc("@item", "Tutorial %1", "theNameChanged")); + QCOMPARE(item.childCount(), 1); + assertName(item.child(0), "The name changed"); + QCOMPARE(dataChangedRootSpy.count(), 1); + assertDataChanged(dataChangedRootSpy, 0, &item); + QCOMPARE(dataChangedSpy.count(), 1); + assertDataChanged(dataChangedSpy, 0, item.child(0)); +} + +void TutorialTreeItemTest::testTutorialSetNameEmpty() { + Tutorial tutorial; + TutorialTreeItem item(&tutorial); + + tutorial.setName("The name"); + + QSignalSpy dataChangedSpy(&item, SIGNAL(dataChanged(TreeItem*))); + + tutorial.setName(""); + + QCOMPARE(item.text(), i18nc("@item", "Tutorial")); + QCOMPARE(item.childCount(), 0); + QCOMPARE(dataChangedSpy.count(), 1); + assertDataChanged(dataChangedSpy, 0, &item); +} + +void TutorialTreeItemTest::testTutorialSetDescription() { + Tutorial tutorial; + TutorialTreeItem item(&tutorial); + + tutorial.setDescription("The description"); + + QCOMPARE(item.childCount(), 1); + assertDescription(item.child(0), "The description"); +} + +void TutorialTreeItemTest::testTutorialSetDescriptionChange() { + Tutorial tutorial; + TutorialTreeItem item(&tutorial); + + tutorial.setDescription("The description"); + + QSignalSpy dataChangedSpy(item.child(0), SIGNAL(dataChanged(TreeItem*))); + + tutorial.setDescription("The description changed"); + + QCOMPARE(item.childCount(), 1); + assertDescription(item.child(0), "The description changed"); + QCOMPARE(dataChangedSpy.count(), 1); + assertDataChanged(dataChangedSpy, 0, item.child(0)); +} + +void TutorialTreeItemTest::testTutorialSetDescriptionEmpty() { + Tutorial tutorial; + TutorialTreeItem item(&tutorial); + + tutorial.setDescription("The description"); + tutorial.setDescription(""); + + QCOMPARE(item.childCount(), 0); +} + +void TutorialTreeItemTest::testTutorialSetLicenseText() { + Tutorial tutorial; + TutorialTreeItem item(&tutorial); + + tutorial.setLicenseText("The license text"); + + QCOMPARE(item.childCount(), 1); + assertLicenseText(item.child(0), "The license text"); +} + +void TutorialTreeItemTest::testTutorialSetLicenseTextChange() { + Tutorial tutorial; + TutorialTreeItem item(&tutorial); + + tutorial.setLicenseText("The license text"); + + QSignalSpy dataChangedSpy(item.child(0)->child(0), + SIGNAL(dataChanged(TreeItem*))); + + tutorial.setLicenseText("The license text changed"); + + QCOMPARE(item.childCount(), 1); + assertLicenseText(item.child(0), "The license text changed"); + QCOMPARE(dataChangedSpy.count(), 1); + assertDataChanged(dataChangedSpy, 0, item.child(0)->child(0)); +} + +void TutorialTreeItemTest::testTutorialSetLicenseTextEmpty() { + Tutorial tutorial; + TutorialTreeItem item(&tutorial); + + tutorial.setLicenseText("The license text"); + tutorial.setLicenseText(""); + + QCOMPARE(item.childCount(), 0); +} + +void TutorialTreeItemTest::testTutorialSetCustomSetupCode() { + Tutorial tutorial; + TutorialTreeItem item(&tutorial); + + tutorial.setCustomSetupCode("The setup code"); + + QCOMPARE(item.childCount(), 1); + assertCustomSetupCode(item.child(0), "The setup code"); +} + +void TutorialTreeItemTest::testTutorialSetCustomSetupCodeChange() { + Tutorial tutorial; + TutorialTreeItem item(&tutorial); + + tutorial.setCustomSetupCode("The setup code"); + + QSignalSpy dataChangedSpy(item.child(0)->child(0), + SIGNAL(dataChanged(TreeItem*))); + + tutorial.setCustomSetupCode("The setup code changed"); + + QCOMPARE(item.childCount(), 1); + assertCustomSetupCode(item.child(0), "The setup code changed"); + QCOMPARE(dataChangedSpy.count(), 1); + assertDataChanged(dataChangedSpy, 0, item.child(0)->child(0)); +} + +void TutorialTreeItemTest::testTutorialSetCustomSetupCodeEmpty() { + Tutorial tutorial; + TutorialTreeItem item(&tutorial); + + tutorial.setCustomSetupCode("The setup code"); + tutorial.setCustomSetupCode(""); + + QCOMPARE(item.childCount(), 0); +} + +void TutorialTreeItemTest::testTutorialSetCustomTearDownCode() { + Tutorial tutorial; + TutorialTreeItem item(&tutorial); + + tutorial.setCustomTearDownCode("The tear down code"); + + QCOMPARE(item.childCount(), 1); + assertCustomTearDownCode(item.child(0), "The tear down code"); +} + +void TutorialTreeItemTest::testTutorialSetCustomTearDownCodeChange() { + Tutorial tutorial; + TutorialTreeItem item(&tutorial); + + tutorial.setCustomTearDownCode("The tear down code"); + + QSignalSpy dataChangedSpy(item.child(0)->child(0), + SIGNAL(dataChanged(TreeItem*))); + + tutorial.setCustomTearDownCode("The tear down code changed"); + + QCOMPARE(item.childCount(), 1); + assertCustomTearDownCode(item.child(0), "The tear down code changed"); + QCOMPARE(dataChangedSpy.count(), 1); + assertDataChanged(dataChangedSpy, 0, item.child(0)->child(0)); +} + +void TutorialTreeItemTest::testTutorialSetCustomTearDownCodeEmpty() { + Tutorial tutorial; + TutorialTreeItem item(&tutorial); + + tutorial.setCustomTearDownCode("The tear down code"); + tutorial.setCustomTearDownCode(""); + + QCOMPARE(item.childCount(), 0); +} + +void TutorialTreeItemTest::testChildOrderWhenSettingDataInTutorial() { + Tutorial tutorial; + TutorialTreeItem item(&tutorial); + + tutorial.setCustomSetupCode("The setup code"); + + QCOMPARE(item.text(), i18nc("@item", "Tutorial")); + QCOMPARE(item.childCount(), 1); + assertCustomSetupCode(item.child(0), "The setup code"); + + tutorial.setDescription("The description"); + + QCOMPARE(item.text(), i18nc("@item", "Tutorial")); + QCOMPARE(item.childCount(), 2); + assertDescription(item.child(0), "The description"); + assertCustomSetupCode(item.child(1), "The setup code"); + + tutorial.setCustomTearDownCode("The tear down code"); + + QCOMPARE(item.text(), i18nc("@item", "Tutorial")); + QCOMPARE(item.childCount(), 3); + assertDescription(item.child(0), "The description"); + assertCustomSetupCode(item.child(1), "The setup code"); + assertCustomTearDownCode(item.child(2), "The tear down code"); + + tutorial.setName("The name"); + + QCOMPARE(item.text(), i18nc("@item", "Tutorial %1", "theName")); + QCOMPARE(item.childCount(), 4); + 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"); + + tutorial.setLicenseText("The license text"); + + QCOMPARE(item.text(), i18nc("@item", "Tutorial %1", "theName")); + QCOMPARE(item.childCount(), 5); + 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"); +} + +void TutorialTreeItemTest::testChildOrderWhenUnsettingDataInTutorial() { + Tutorial tutorial; + tutorial.setName("The name"); + tutorial.setDescription("The description"); + tutorial.setLicenseText("The license text"); + tutorial.setCustomSetupCode("The setup code"); + tutorial.setCustomTearDownCode("The tear down code"); + + TutorialTreeItem item(&tutorial); + + tutorial.setLicenseText(""); + + QCOMPARE(item.text(), i18nc("@item", "Tutorial %1", "theName")); + QCOMPARE(item.childCount(), 4); + 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"); + + tutorial.setName(""); + + QCOMPARE(item.text(), i18nc("@item", "Tutorial")); + QCOMPARE(item.childCount(), 3); + assertDescription(item.child(0), "The description"); + assertCustomSetupCode(item.child(1), "The setup code"); + assertCustomTearDownCode(item.child(2), "The tear down code"); + + tutorial.setCustomTearDownCode(""); + + QCOMPARE(item.text(), i18nc("@item", "Tutorial")); + QCOMPARE(item.childCount(), 2); + assertDescription(item.child(0), "The description"); + assertCustomSetupCode(item.child(1), "The setup code"); + + tutorial.setDescription(""); + + QCOMPARE(item.text(), i18nc("@item", "Tutorial")); + QCOMPARE(item.childCount(), 1); + assertCustomSetupCode(item.child(0), "The setup code"); + + tutorial.setCustomSetupCode(""); + + QCOMPARE(item.text(), i18nc("@item", "Tutorial")); + QCOMPARE(item.childCount(), 0); +} + +/////////////////////////////////// Helpers //////////////////////////////////// + +void TutorialTreeItemTest::assertName(TreeItem* nameItem, + const QString& name) const { + QCOMPARE(nameItem->text(), i18nc("@item", "Name: %1", name)); +} + +void TutorialTreeItemTest::assertDescription(TreeItem* descriptionItem, + const QString& description) const { + QCOMPARE(descriptionItem->text(), + i18nc("@item", "Description: %1", description)); +} + +void TutorialTreeItemTest::assertLicenseText(TreeItem* licenseItem, + const QString& licenseText) const { + QCOMPARE(licenseItem->text(), i18nc("@item", "License:")); + QCOMPARE(licenseItem->childCount(), 1); + QCOMPARE(licenseItem->child(0)->text(), + i18nc("@item", licenseText.toAscii())); +} + +void TutorialTreeItemTest::assertCustomSetupCode(TreeItem* setupItem, + const QString& code) const { + QCOMPARE(setupItem->text(), i18nc("@item", "Setup:")); + QCOMPARE(setupItem->childCount(), 1); + QCOMPARE(setupItem->child(0)->text(), i18nc("@item", code.toAscii())); +} + +void TutorialTreeItemTest::assertCustomTearDownCode(TreeItem* tearDownItem, + const QString& code) const { + QCOMPARE(tearDownItem->text(), i18nc("@item", "Tear down:")); + QCOMPARE(tearDownItem->childCount(), 1); + QCOMPARE(tearDownItem->child(0)->text(), i18nc("@item", code.toAscii())); +} + +void TutorialTreeItemTest::assertDataChanged(const QSignalSpy& spy, int index, + TreeItem* item) const { + QCOMPARE(spy.at(index).count(), 1); + + QVariant argument = spy.at(index).at(0); + QCOMPARE(argument.userType(), mTreeItemStarType); + QCOMPARE(qvariant_cast<TreeItem*>(argument), item); +} + +QTEST_MAIN(TutorialTreeItemTest) + +#include "TutorialTreeItemTest.moc" Property changes on: trunk/ktutorial/ktutorial-editor/tests/unit/view/TutorialTreeItemTest.cpp ___________________________________________________________________ Added: svn:eol-style + native This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <dan...@us...> - 2010-03-07 06:21:26
|
Revision: 116 http://ktutorial.svn.sourceforge.net/ktutorial/?rev=116&view=rev Author: danxuliu Date: 2010-03-07 06:21:19 +0000 (Sun, 07 Mar 2010) Log Message: ----------- Fix the path to runMemCheck.py in MEM_TESTS macro. Modified Paths: -------------- trunk/ktutorial/ktutorial-editor/tests/unit/CMakeLists.txt Modified: trunk/ktutorial/ktutorial-editor/tests/unit/CMakeLists.txt =================================================================== --- trunk/ktutorial/ktutorial-editor/tests/unit/CMakeLists.txt 2010-03-06 22:52:52 UTC (rev 115) +++ trunk/ktutorial/ktutorial-editor/tests/unit/CMakeLists.txt 2010-03-07 06:21:19 UTC (rev 116) @@ -18,7 +18,7 @@ MACRO(MEM_TESTS) FOREACH(_testname ${ARGN}) - add_test(ktutorial-editor-unit-mem-${_testname} ${CMAKE_CURRENT_SOURCE_DIR}/../runMemcheck.py ${CMAKE_CURRENT_BINARY_DIR}/${_testname}Test ${CMAKE_CURRENT_BINARY_DIR}) + add_test(ktutorial-editor-unit-mem-${_testname} ${CMAKE_CURRENT_SOURCE_DIR}/runMemcheck.py ${CMAKE_CURRENT_BINARY_DIR}/${_testname}Test ${CMAKE_CURRENT_BINARY_DIR}) ENDFOREACH(_testname) ENDMACRO(MEM_TESTS) This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <dan...@us...> - 2010-03-06 22:53:00
|
Revision: 115 http://ktutorial.svn.sourceforge.net/ktutorial/?rev=115&view=rev Author: danxuliu Date: 2010-03-06 22:52:52 +0000 (Sat, 06 Mar 2010) Log Message: ----------- Fix crash when an empty string or a spaces only string is converted to lowerCamelCase. Modified Paths: -------------- trunk/ktutorial/ktutorial-editor/src/Tutorial.cpp trunk/ktutorial/ktutorial-editor/tests/unit/TutorialTest.cpp Modified: trunk/ktutorial/ktutorial-editor/src/Tutorial.cpp =================================================================== --- trunk/ktutorial/ktutorial-editor/src/Tutorial.cpp 2010-03-06 20:52:29 UTC (rev 114) +++ trunk/ktutorial/ktutorial-editor/src/Tutorial.cpp 2010-03-06 22:52:52 UTC (rev 115) @@ -84,6 +84,10 @@ QString Tutorial::toLowerCamelCase(const QString& text) const { QStringList words = text.split(' ', QString::SkipEmptyParts); + if (words.count() == 0) { + return ""; + } + QString lowerCamelCase; words[0][0] = words[0][0].toLower(); lowerCamelCase += words[0]; Modified: trunk/ktutorial/ktutorial-editor/tests/unit/TutorialTest.cpp =================================================================== --- trunk/ktutorial/ktutorial-editor/tests/unit/TutorialTest.cpp 2010-03-06 20:52:29 UTC (rev 114) +++ trunk/ktutorial/ktutorial-editor/tests/unit/TutorialTest.cpp 2010-03-06 22:52:52 UTC (rev 115) @@ -26,6 +26,8 @@ private slots: void testId(); + void testIdWithEmptyName(); + void testIdWithSpaceName(); void testSetName(); @@ -46,6 +48,20 @@ QCOMPARE(tutorial.id(), QString("thENameOfATutoRial")); } +void TutorialTest::testIdWithEmptyName() { + Tutorial tutorial; + tutorial.setName(""); + + QCOMPARE(tutorial.id(), QString("")); +} + +void TutorialTest::testIdWithSpaceName() { + Tutorial tutorial; + tutorial.setName(" "); + + QCOMPARE(tutorial.id(), QString("")); +} + //Tutorial* must be declared as a metatype to be used in qvariant_cast Q_DECLARE_METATYPE(Tutorial*); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <dan...@us...> - 2010-03-06 20:52:36
|
Revision: 114 http://ktutorial.svn.sourceforge.net/ktutorial/?rev=114&view=rev Author: danxuliu Date: 2010-03-06 20:52:29 +0000 (Sat, 06 Mar 2010) Log Message: ----------- Add basic version of Tutorial class to store the data of a tutorial. Modified Paths: -------------- trunk/ktutorial/ktutorial-editor/src/CMakeLists.txt trunk/ktutorial/ktutorial-editor/tests/unit/CMakeLists.txt Added Paths: ----------- trunk/ktutorial/ktutorial-editor/src/Tutorial.cpp trunk/ktutorial/ktutorial-editor/src/Tutorial.h trunk/ktutorial/ktutorial-editor/tests/unit/TutorialTest.cpp Modified: trunk/ktutorial/ktutorial-editor/src/CMakeLists.txt =================================================================== --- trunk/ktutorial/ktutorial-editor/src/CMakeLists.txt 2010-03-06 17:12:46 UTC (rev 113) +++ trunk/ktutorial/ktutorial-editor/src/CMakeLists.txt 2010-03-06 20:52:29 UTC (rev 114) @@ -8,6 +8,7 @@ set(ktutorial_editor_SRCS KTutorialEditor.cpp + Tutorial.cpp ) # Instead of compiling the executable directly from the sources, the sources are Added: trunk/ktutorial/ktutorial-editor/src/Tutorial.cpp =================================================================== --- trunk/ktutorial/ktutorial-editor/src/Tutorial.cpp (rev 0) +++ trunk/ktutorial/ktutorial-editor/src/Tutorial.cpp 2010-03-06 20:52:29 UTC (rev 114) @@ -0,0 +1,97 @@ +/*************************************************************************** + * 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 "Tutorial.h" + +#include <QStringList> + +//public: + +Tutorial::Tutorial(QObject* parent) { +} + +QString Tutorial::id() const { + return toLowerCamelCase(mName); +} + +QString Tutorial::name() const { + return mName; +} + +void Tutorial::setName(const QString& name) { + mName = name; + + emit dataChanged(this); +} + +QString Tutorial::description() const { + return mDescription; +} + +void Tutorial::setDescription(const QString& description) { + mDescription = description; + + emit dataChanged(this); +} + +QString Tutorial::licenseText() const { + return mLicenseText; +} + +void Tutorial::setLicenseText(const QString& licenseText) { + mLicenseText = licenseText; + + emit dataChanged(this); +} + +QString Tutorial::customSetupCode() const { + return mCustomSetupCode; +} + +void Tutorial::setCustomSetupCode(const QString& code) { + mCustomSetupCode = code; + + emit dataChanged(this); +} + +QString Tutorial::customTearDownCode() const { + return mCustomTearDownCode; +} + +void Tutorial::setCustomTearDownCode(const QString& code) { + mCustomTearDownCode = code; + + emit dataChanged(this); +} + +//private: + +QString Tutorial::toLowerCamelCase(const QString& text) const { + QStringList words = text.split(' ', QString::SkipEmptyParts); + + QString lowerCamelCase; + words[0][0] = words[0][0].toLower(); + lowerCamelCase += words[0]; + + for (int i=1; i<words.count(); ++i) { + words[i][0] = words[i][0].toUpper(); + lowerCamelCase += words[i]; + } + + return lowerCamelCase; +} Property changes on: trunk/ktutorial/ktutorial-editor/src/Tutorial.cpp ___________________________________________________________________ Added: svn:eol-style + native Added: trunk/ktutorial/ktutorial-editor/src/Tutorial.h =================================================================== --- trunk/ktutorial/ktutorial-editor/src/Tutorial.h (rev 0) +++ trunk/ktutorial/ktutorial-editor/src/Tutorial.h 2010-03-06 20:52:29 UTC (rev 114) @@ -0,0 +1,88 @@ +/*************************************************************************** + * 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 TUTORIAL_H +#define TUTORIAL_H + +#include <QObject> + +/** + * Container for tutorial data. + * It stores the data used in KTutorial tutorials, but it has nothing to do with + * it (they don't even know each other). Its purpose is store the data needed to + * generate the code to create a true KTutorial::Tutorial. + * + * When any attribute is modified, dataChanged(Tutorial*) signal is emitted. + */ +class Tutorial: public QObject { +Q_OBJECT +public: + + Tutorial(QObject* parent = 0); + + /** + * Returns the id of this Tutorial. + * The id is a lowerCamelCase version of the name. + * + * @return The id of this Tutorial. + */ + QString id() const; + + QString name() const; + void setName(const QString& name); + + QString description() const; + void setDescription(const QString& description); + + QString licenseText() const; + void setLicenseText(const QString& licenseText); + + QString customSetupCode() const; + void setCustomSetupCode(const QString& code); + + QString customTearDownCode() const; + void setCustomTearDownCode(const QString& code); + +Q_SIGNALS: + + /** + * Emitted when any data in the tutorial changed. + * + * @param tutorial This tutorial. + */ + void dataChanged(Tutorial* tutorial); + +private: + + QString mName; + QString mDescription; + QString mLicenseText; + QString mCustomSetupCode; + QString mCustomTearDownCode; + + /** + * Returns the lowerCamelCase version of the given text. + * + * @param text The string to get its lowerCamelCase version. + * @return The lowerCamelCase version of the text. + */ + QString toLowerCamelCase(const QString& text) const; + +}; + +#endif Property changes on: trunk/ktutorial/ktutorial-editor/src/Tutorial.h ___________________________________________________________________ Added: svn:eol-style + native Modified: trunk/ktutorial/ktutorial-editor/tests/unit/CMakeLists.txt =================================================================== --- trunk/ktutorial/ktutorial-editor/tests/unit/CMakeLists.txt 2010-03-06 17:12:46 UTC (rev 113) +++ trunk/ktutorial/ktutorial-editor/tests/unit/CMakeLists.txt 2010-03-06 20:52:29 UTC (rev 114) @@ -1 +1,25 @@ add_subdirectory(view) + +# Used by kde4_add_unit_test to set the full path to test executables +set(EXECUTABLE_OUTPUT_PATH ${CMAKE_CURRENT_BINARY_DIR}) + +# include_directories(${CMAKE_CURRENT_BINARY_DIR} ${ktutorial-editor_SOURCE_DIR}/src/view ${ktutorial-editor_BINARY_DIR}/src/view ${KDE4_INCLUDES}) +include_directories(${ktutorial-editor_SOURCE_DIR}/src/ ${KDE4_INCLUDES}) + +MACRO(UNIT_TESTS) + FOREACH(_className ${ARGN}) + set(_testName ${_className}Test) + kde4_add_unit_test(${_testName} TESTNAME ktutorial-editor-unit-${_testName} ${_testName}.cpp) + target_link_libraries(${_testName} ktutorial_editor ${QT_QTTEST_LIBRARY}) + ENDFOREACH(_className) +ENDMACRO(UNIT_TESTS) + +unit_tests(Tutorial) + +MACRO(MEM_TESTS) + FOREACH(_testname ${ARGN}) + add_test(ktutorial-editor-unit-mem-${_testname} ${CMAKE_CURRENT_SOURCE_DIR}/../runMemcheck.py ${CMAKE_CURRENT_BINARY_DIR}/${_testname}Test ${CMAKE_CURRENT_BINARY_DIR}) + ENDFOREACH(_testname) +ENDMACRO(MEM_TESTS) + +mem_tests(Tutorial) Added: trunk/ktutorial/ktutorial-editor/tests/unit/TutorialTest.cpp =================================================================== --- trunk/ktutorial/ktutorial-editor/tests/unit/TutorialTest.cpp (rev 0) +++ trunk/ktutorial/ktutorial-editor/tests/unit/TutorialTest.cpp 2010-03-06 20:52:29 UTC (rev 114) @@ -0,0 +1,134 @@ +/*************************************************************************** + * 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 <QtTest> + +#include "Tutorial.h" + +class TutorialTest: public QObject { +Q_OBJECT + +private slots: + + void testId(); + + void testSetName(); + + void testSetDescription(); + + void testSetLicenseText(); + + void testSetCustomSetupCode(); + + void testSetCustomTearDownCode(); + +}; + +void TutorialTest::testId() { + Tutorial tutorial; + tutorial.setName("ThE name of a tutoRial"); + + QCOMPARE(tutorial.id(), QString("thENameOfATutoRial")); +} + +//Tutorial* must be declared as a metatype to be used in qvariant_cast +Q_DECLARE_METATYPE(Tutorial*); + +void TutorialTest::testSetName() { + Tutorial tutorial; + + //Tutorial* must be registered in order to be used with QSignalSpy + int tutorialStarType = qRegisterMetaType<Tutorial*>("Tutorial*"); + QSignalSpy dataChangedSpy(&tutorial, SIGNAL(dataChanged(Tutorial*))); + + tutorial.setName("The name"); + + QCOMPARE(tutorial.name(), QString("The name")); + QCOMPARE(dataChangedSpy.count(), 1); + QVariant argument = dataChangedSpy.at(0).at(0); + QCOMPARE(argument.userType(), tutorialStarType); + QCOMPARE(qvariant_cast<Tutorial*>(argument), &tutorial); +} + +void TutorialTest::testSetDescription() { + Tutorial tutorial; + + //Tutorial* must be registered in order to be used with QSignalSpy + int tutorialStarType = qRegisterMetaType<Tutorial*>("Tutorial*"); + QSignalSpy dataChangedSpy(&tutorial, SIGNAL(dataChanged(Tutorial*))); + + tutorial.setDescription("The description"); + + QCOMPARE(tutorial.description(), QString("The description")); + QCOMPARE(dataChangedSpy.count(), 1); + QVariant argument = dataChangedSpy.at(0).at(0); + QCOMPARE(argument.userType(), tutorialStarType); + QCOMPARE(qvariant_cast<Tutorial*>(argument), &tutorial); +} + +void TutorialTest::testSetLicenseText() { + Tutorial tutorial; + + //Tutorial* must be registered in order to be used with QSignalSpy + int tutorialStarType = qRegisterMetaType<Tutorial*>("Tutorial*"); + QSignalSpy dataChangedSpy(&tutorial, SIGNAL(dataChanged(Tutorial*))); + + tutorial.setLicenseText("The license text"); + + QCOMPARE(tutorial.licenseText(), QString("The license text")); + QCOMPARE(dataChangedSpy.count(), 1); + QVariant argument = dataChangedSpy.at(0).at(0); + QCOMPARE(argument.userType(), tutorialStarType); + QCOMPARE(qvariant_cast<Tutorial*>(argument), &tutorial); +} + +void TutorialTest::testSetCustomSetupCode() { + Tutorial tutorial; + + //Tutorial* must be registered in order to be used with QSignalSpy + int tutorialStarType = qRegisterMetaType<Tutorial*>("Tutorial*"); + QSignalSpy dataChangedSpy(&tutorial, SIGNAL(dataChanged(Tutorial*))); + + tutorial.setCustomSetupCode("The setup code"); + + QCOMPARE(tutorial.customSetupCode(), QString("The setup code")); + QCOMPARE(dataChangedSpy.count(), 1); + QVariant argument = dataChangedSpy.at(0).at(0); + QCOMPARE(argument.userType(), tutorialStarType); + QCOMPARE(qvariant_cast<Tutorial*>(argument), &tutorial); +} + +void TutorialTest::testSetCustomTearDownCode() { + Tutorial tutorial; + + //Tutorial* must be registered in order to be used with QSignalSpy + int tutorialStarType = qRegisterMetaType<Tutorial*>("Tutorial*"); + QSignalSpy dataChangedSpy(&tutorial, SIGNAL(dataChanged(Tutorial*))); + + tutorial.setCustomTearDownCode("The tear down code"); + + QCOMPARE(tutorial.customTearDownCode(), QString("The tear down code")); + QCOMPARE(dataChangedSpy.count(), 1); + QVariant argument = dataChangedSpy.at(0).at(0); + QCOMPARE(argument.userType(), tutorialStarType); + QCOMPARE(qvariant_cast<Tutorial*>(argument), &tutorial); +} + +QTEST_MAIN(TutorialTest) + +#include "TutorialTest.moc" Property changes on: trunk/ktutorial/ktutorial-editor/tests/unit/TutorialTest.cpp ___________________________________________________________________ Added: svn:eol-style + native This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <dan...@us...> - 2010-03-06 17:12:55
|
Revision: 113 http://ktutorial.svn.sourceforge.net/ktutorial/?rev=113&view=rev Author: danxuliu Date: 2010-03-06 17:12:46 +0000 (Sat, 06 Mar 2010) Log Message: ----------- Watch the signals emitted by the TreeItems added to a TreeModel and notify the views to be updated as needed. Modified Paths: -------------- trunk/ktutorial/ktutorial-editor/src/view/TreeModel.cpp trunk/ktutorial/ktutorial-editor/src/view/TreeModel.h trunk/ktutorial/ktutorial-editor/tests/unit/view/TreeModelTest.cpp Modified: trunk/ktutorial/ktutorial-editor/src/view/TreeModel.cpp =================================================================== --- trunk/ktutorial/ktutorial-editor/src/view/TreeModel.cpp 2010-03-06 17:11:26 UTC (rev 112) +++ trunk/ktutorial/ktutorial-editor/src/view/TreeModel.cpp 2010-03-06 17:12:46 UTC (rev 113) @@ -26,6 +26,8 @@ QAbstractItemModel(parent), mRootItem(rootItem) { Q_ASSERT(rootItem); + + registerTreeItemForUpdates(rootItem); } TreeModel::~TreeModel() { @@ -114,3 +116,79 @@ return 1; } + +//private: + +void TreeModel::registerTreeItemForUpdates(TreeItem* item) { + connect(item, SIGNAL(childAboutToBeInserted(TreeItem*, int)), + this, SLOT(treeItemAboutToBeInserted(TreeItem*, int))); + connect(item, SIGNAL(childInserted(TreeItem*)), + this, SLOT(treeItemInserted(TreeItem*))); + connect(item, SIGNAL(childAboutToBeRemoved(TreeItem*)), + this, SLOT(treeItemAboutToBeRemoved(TreeItem*))); + connect(item, SIGNAL(childRemoved(TreeItem*)), + this, SLOT(treeItemRemoved(TreeItem*))); + connect(item, SIGNAL(dataChanged(TreeItem*)), + this, SLOT(treeItemDataChanged(TreeItem*))); + + for (int i=0; i<item->childCount(); ++i) { + registerTreeItemForUpdates(item->child(i)); + } +} + +void TreeModel::deregisterTreeItemFromUpdates(TreeItem* item) { + disconnect(item, SIGNAL(childAboutToBeInserted(TreeItem*, int)), + this, SLOT(treeItemAboutToBeInserted(TreeItem*, int))); + disconnect(item, SIGNAL(childInserted(TreeItem*)), + this, SLOT(treeItemInserted(TreeItem*))); + disconnect(item, SIGNAL(childAboutToBeRemoved(TreeItem*)), + this, SLOT(treeItemAboutToBeRemoved(TreeItem*))); + disconnect(item, SIGNAL(childRemoved(TreeItem*)), + this, SLOT(treeItemRemoved(TreeItem*))); + disconnect(item, SIGNAL(dataChanged(TreeItem*)), + this, SLOT(treeItemDataChanged(TreeItem*))); + + for (int i=0; i<item->childCount(); ++i) { + deregisterTreeItemFromUpdates(item->child(i)); + } +} + +QModelIndex TreeModel::getParentIndex(TreeItem* item) { + QModelIndex parent; + + if (item->parent() != mRootItem) { + parent = createIndex(item->parent()->childIndex(), 0, item->parent()); + } + + return parent; +} + +//private slots: + +void TreeModel::treeItemAboutToBeInserted(TreeItem* item, int treeItemIndex) { + beginInsertRows(getParentIndex(item), treeItemIndex, treeItemIndex); +} + +void TreeModel::treeItemInserted(TreeItem* item) { + endInsertRows(); + + registerTreeItemForUpdates(item); +} + +void TreeModel::treeItemAboutToBeRemoved(TreeItem* item) { + int treeItemIndex = item->childIndex(); + + beginRemoveRows(getParentIndex(item), treeItemIndex, treeItemIndex); +} + +void TreeModel::treeItemRemoved(TreeItem* item) { + deregisterTreeItemFromUpdates(item); + + endRemoveRows(); +} + +void TreeModel::treeItemDataChanged(TreeItem* item) { + QModelIndex index = createIndex(item->childIndex(), 0, item); + + emit dataChanged(index, index); +} Modified: trunk/ktutorial/ktutorial-editor/src/view/TreeModel.h =================================================================== --- trunk/ktutorial/ktutorial-editor/src/view/TreeModel.h 2010-03-06 17:11:26 UTC (rev 112) +++ trunk/ktutorial/ktutorial-editor/src/view/TreeModel.h 2010-03-06 17:12:46 UTC (rev 113) @@ -40,6 +40,9 @@ * Each TreeItem provides the data to be shown by its associated model item. * Display role shows the text of the TreeItem. * + * Changes in the tree layout or data provided by the TreeItems are notified to + * the views, so they can be updated as needed. + * * @see TreeItem */ class TreeModel: public QAbstractItemModel { @@ -145,6 +148,70 @@ */ TreeItem* mRootItem; + /** + * Starts watching changes in the given item and its children. + * When something change in a registered item (the data is modified, a child + * is added or a child is removed), the views are notified so they can be + * updated as needed. + * + * @param item The item to register. + */ + void registerTreeItemForUpdates(TreeItem* item); + + /** + * Stops watching changes in the given item and its children. + * + * @param item The item to deregister. + */ + void deregisterTreeItemFromUpdates(TreeItem* item); + + /** + * Returns the index for the parent of the given item. + * + * @param item The item to get the index of its parent. + * @return The parent index. + */ + QModelIndex getParentIndex(TreeItem* item); + +private Q_SLOTS: + + /** + * Notifies the views that the given item is going to be added. + * + * @param item The item that is going to be added. + * @param treeItemIndex The index in the parent item where the item is going + * to be added. + */ + void treeItemAboutToBeInserted(TreeItem* item, int treeItemIndex); + + /** + * Notifies the views that the given item has been added, + * + * @param item The item added. + */ + void treeItemInserted(TreeItem* item); + + /** + * Notifies the views that the given item is going to be removed. + * + * @param item The item that is going to be removed. + */ + void treeItemAboutToBeRemoved(TreeItem* item); + + /** + * Notifies the views that the given item has been removed. + * + * @param item The item removed. + */ + void treeItemRemoved(TreeItem* item); + + /** + * Notifies the views about a change in the data of the given item. + * + * @param item The item that changed. + */ + void treeItemDataChanged(TreeItem* item); + }; #endif Modified: trunk/ktutorial/ktutorial-editor/tests/unit/view/TreeModelTest.cpp =================================================================== --- trunk/ktutorial/ktutorial-editor/tests/unit/view/TreeModelTest.cpp 2010-03-06 17:11:26 UTC (rev 112) +++ trunk/ktutorial/ktutorial-editor/tests/unit/view/TreeModelTest.cpp 2010-03-06 17:12:46 UTC (rev 113) @@ -27,6 +27,8 @@ private slots: + void initTestCase(); + void init(); void cleanup(); @@ -36,6 +38,18 @@ void testConstructorSingleNestedItem(); void testConstructorSeveralNestedItems(); + void testAppendTopLevelItem(); + void testInsertTopLevelItem(); + void testRemoveTopLevelItem(); + + void testChangeChildrenOfNestedItemAddedBeforeConstructor(); + void testChangeChildrenOfNestedItemAddedAfterConstructor(); + + void testChangeChildrenOfRemovedItem(); + void testChangeChildrenOfRemovedNestedItem(); + + void testChangeItemData(); + void testDataWithInvalidIndex(); void testDataWithInvalidRole(); @@ -51,6 +65,8 @@ private: + int mModelIndexType; + TreeItem* mEmptyRootItem; TreeItem* mSingleItem; TreeItem* mSeveralFlatItems; @@ -60,9 +76,13 @@ void assertItem(const QModelIndex& index, const QString& displayRoleData, int childrenCount, const QModelIndex& parent) const; + void assertSignal(const QSignalSpy& spy, int signalIndex, + const QModelIndex& index, int row) const; + }; class StubTreeItem: public TreeItem { +Q_OBJECT public: QString mText; @@ -76,8 +96,19 @@ return mText; } + void setText(const QString& text) { + mText = text; + + emit dataChanged(this); + } + }; +void TreeModelTest::initTestCase() { + //QModelIndex must be registered in order to be used with QSignalSpy + mModelIndexType = qRegisterMetaType<QModelIndex>("QModelIndex"); +} + void TreeModelTest::init() { mEmptyRootItem = new StubTreeItem("root"); @@ -202,6 +233,289 @@ assertItem(index, "root-3-2", 0, parent); } +//QModelIndex must be declared as a metatype to be used in qvariant_cast +Q_DECLARE_METATYPE(QModelIndex); + +void TreeModelTest::testAppendTopLevelItem() { + TreeItem* rootItem = mEmptyRootItem; + mEmptyRootItem = 0; + + TreeModel model(rootItem); + + QSignalSpy aboutToSpy(&model, + SIGNAL(rowsAboutToBeInserted(QModelIndex, int, int))); + QSignalSpy insertedSpy(&model, SIGNAL(rowsInserted(QModelIndex, int, int))); + + rootItem->appendChild(new StubTreeItem("root-1", rootItem)); + + QCOMPARE(model.rowCount(), 1); + QCOMPARE(model.columnCount(), 1); + QCOMPARE(model.headerData(0, Qt::Horizontal).toString(), QString("root")); + + QModelIndex index = model.index(0, 0); + assertItem(index, "root-1", 0, QModelIndex()); + + //The proper way to check this would be asserting not only that the signals + //were emitted, but that when they were emitted the item wasn't and was + //already added (depending on the signal). However, that is too much effort + //for little gain. + QCOMPARE(aboutToSpy.count(), 1); + assertSignal(aboutToSpy, 0, QModelIndex(), 0); + QCOMPARE(insertedSpy.count(), 1); + assertSignal(insertedSpy, 0, QModelIndex(), 0); +} + +void TreeModelTest::testInsertTopLevelItem() { + TreeItem* rootItem = mSeveralFlatItems; + mSeveralFlatItems = 0; + + TreeModel model(rootItem); + + QSignalSpy aboutToSpy(&model, + SIGNAL(rowsAboutToBeInserted(QModelIndex, int, int))); + QSignalSpy insertedSpy(&model, SIGNAL(rowsInserted(QModelIndex, int, int))); + + rootItem->insertChild(new StubTreeItem("root-2new", rootItem), 1); + + QCOMPARE(model.rowCount(), 4); + QCOMPARE(model.columnCount(), 1); + QCOMPARE(model.headerData(0, Qt::Horizontal).toString(), QString("root")); + + QModelIndex index = model.index(0, 0); + assertItem(index, "root-1", 0, QModelIndex()); + + index = model.index(1, 0); + assertItem(index, "root-2new", 0, QModelIndex()); + + index = model.index(2, 0); + assertItem(index, "root-2", 0, QModelIndex()); + + index = model.index(3, 0); + assertItem(index, "root-3", 0, QModelIndex()); + + QCOMPARE(aboutToSpy.count(), 1); + assertSignal(aboutToSpy, 0, QModelIndex(), 1); + QCOMPARE(insertedSpy.count(), 1); + assertSignal(insertedSpy, 0, QModelIndex(), 1); +} + +void TreeModelTest::testRemoveTopLevelItem() { + TreeItem* rootItem = mSeveralFlatItems; + mSeveralFlatItems = 0; + + TreeModel model(rootItem); + + QSignalSpy aboutToSpy(&model, + SIGNAL(rowsAboutToBeRemoved(QModelIndex, int, int))); + QSignalSpy removedSpy(&model, SIGNAL(rowsRemoved(QModelIndex, int, int))); + + TreeItem* itemToBeRemoved = rootItem->child(1); + rootItem->removeChild(itemToBeRemoved); + delete itemToBeRemoved; + + QCOMPARE(model.rowCount(), 2); + QCOMPARE(model.columnCount(), 1); + QCOMPARE(model.headerData(0, Qt::Horizontal).toString(), QString("root")); + + QModelIndex index = model.index(0, 0); + assertItem(index, "root-1", 0, QModelIndex()); + + index = model.index(1, 0); + assertItem(index, "root-3", 0, QModelIndex()); + + QCOMPARE(aboutToSpy.count(), 1); + assertSignal(aboutToSpy, 0, QModelIndex(), 1); + QCOMPARE(removedSpy.count(), 1); + assertSignal(removedSpy, 0, QModelIndex(), 1); +} + + +void TreeModelTest::testChangeChildrenOfNestedItemAddedBeforeConstructor() { + TreeItem* rootItem = mSingleNestedItem; + mSingleNestedItem = 0; + + TreeModel model(rootItem); + TreeItem* item = rootItem->child(0)->child(0); + + QSignalSpy aboutToInsertSpy(&model, + SIGNAL(rowsAboutToBeInserted(QModelIndex, int, int))); + QSignalSpy insertedSpy(&model, SIGNAL(rowsInserted(QModelIndex, int, int))); + QSignalSpy aboutToRemoveSpy(&model, + SIGNAL(rowsAboutToBeRemoved(QModelIndex, int, int))); + QSignalSpy removedSpy(&model, SIGNAL(rowsRemoved(QModelIndex, int, int))); + + StubTreeItem childToBeRemoved("root-1-1-3", item); + item->appendChild(new StubTreeItem("root-1-1-1", item)); + item->appendChild(&childToBeRemoved); + item->insertChild(new StubTreeItem("root-1-1-2", item), 1); + item->removeChild(&childToBeRemoved); + + QCOMPARE(model.rowCount(), 1); + QCOMPARE(model.columnCount(), 1); + QCOMPARE(model.headerData(0, Qt::Horizontal).toString(), QString("root")); + + QModelIndex index = model.index(0, 0); + assertItem(index, "root-1", 1, QModelIndex()); + + QModelIndex parent = index; + index = model.index(0, 0, parent); + assertItem(index, "root-1-1", 2, parent); + + parent = index; + index = model.index(0, 0, parent); + assertItem(index, "root-1-1-1", 0, parent); + + index = model.index(1, 0, parent); + assertItem(index, "root-1-1-2", 0, parent); + + QCOMPARE(aboutToInsertSpy.count(), 3); + assertSignal(aboutToInsertSpy, 0, parent, 0); + assertSignal(aboutToInsertSpy, 1, parent, 1); + assertSignal(aboutToInsertSpy, 2, parent, 1); + QCOMPARE(insertedSpy.count(), 3); + assertSignal(insertedSpy, 0, parent, 0); + assertSignal(insertedSpy, 1, parent, 1); + assertSignal(insertedSpy, 2, parent, 1); + QCOMPARE(aboutToRemoveSpy.count(), 1); + assertSignal(aboutToRemoveSpy, 0, parent, 2); + QCOMPARE(removedSpy.count(), 1); + assertSignal(removedSpy, 0, parent, 2); +} + +void TreeModelTest::testChangeChildrenOfNestedItemAddedAfterConstructor() { + TreeItem* rootItem = mEmptyRootItem; + mEmptyRootItem = 0; + + TreeModel model(rootItem); + rootItem->appendChild(new StubTreeItem("root-1", rootItem)); + + TreeItem* item = new StubTreeItem("root-1-1", rootItem->child(0)); + rootItem->child(0)->appendChild(item); + + QSignalSpy aboutToInsertSpy(&model, + SIGNAL(rowsAboutToBeInserted(QModelIndex, int, int))); + QSignalSpy insertedSpy(&model, SIGNAL(rowsInserted(QModelIndex, int, int))); + QSignalSpy aboutToRemoveSpy(&model, + SIGNAL(rowsAboutToBeRemoved(QModelIndex, int, int))); + QSignalSpy removedSpy(&model, SIGNAL(rowsRemoved(QModelIndex, int, int))); + + StubTreeItem childToBeRemoved("root-1-1-3", item); + item->appendChild(new StubTreeItem("root-1-1-1", item)); + item->appendChild(&childToBeRemoved); + item->insertChild(new StubTreeItem("root-1-1-2", item), 1); + item->removeChild(&childToBeRemoved); + + QCOMPARE(model.rowCount(), 1); + QCOMPARE(model.columnCount(), 1); + QCOMPARE(model.headerData(0, Qt::Horizontal).toString(), QString("root")); + + QModelIndex index = model.index(0, 0); + assertItem(index, "root-1", 1, QModelIndex()); + + QModelIndex parent = index; + index = model.index(0, 0, parent); + assertItem(index, "root-1-1", 2, parent); + + parent = index; + index = model.index(0, 0, parent); + assertItem(index, "root-1-1-1", 0, parent); + + index = model.index(1, 0, parent); + assertItem(index, "root-1-1-2", 0, parent); + + QCOMPARE(aboutToInsertSpy.count(), 3); + assertSignal(aboutToInsertSpy, 0, parent, 0); + assertSignal(aboutToInsertSpy, 1, parent, 1); + assertSignal(aboutToInsertSpy, 2, parent, 1); + QCOMPARE(insertedSpy.count(), 3); + assertSignal(insertedSpy, 0, parent, 0); + assertSignal(insertedSpy, 1, parent, 1); + assertSignal(insertedSpy, 2, parent, 1); + QCOMPARE(aboutToRemoveSpy.count(), 1); + assertSignal(aboutToRemoveSpy, 0, parent, 2); + QCOMPARE(removedSpy.count(), 1); + assertSignal(removedSpy, 0, parent, 2); +} + +void TreeModelTest::testChangeChildrenOfRemovedItem() { + TreeItem* rootItem = mEmptyRootItem; + mEmptyRootItem = 0; + + TreeModel model(rootItem); + + StubTreeItem item("root-1", rootItem); + rootItem->appendChild(&item); + rootItem->removeChild(&item); + + QSignalSpy aboutToSpy(&model, + SIGNAL(rowsAboutToBeInserted(QModelIndex, int, int))); + QSignalSpy insertedSpy(&model, SIGNAL(rowsInserted(QModelIndex, int, int))); + + item.appendChild(new StubTreeItem("root-1-1", &item)); + + QCOMPARE(model.rowCount(), 0); + QCOMPARE(model.columnCount(), 1); + QCOMPARE(model.headerData(0, Qt::Horizontal).toString(), QString("root")); + + QCOMPARE(aboutToSpy.count(), 0); + QCOMPARE(insertedSpy.count(), 0); +} + +void TreeModelTest::testChangeChildrenOfRemovedNestedItem() { + TreeItem* rootItem = mEmptyRootItem; + mEmptyRootItem = 0; + + TreeModel model(rootItem); + + StubTreeItem item("root-1", rootItem); + TreeItem* nestedItem = new StubTreeItem("root-1-1", &item); + item.appendChild(nestedItem); + rootItem->appendChild(&item); + rootItem->removeChild(&item); + + QSignalSpy aboutToSpy(&model, + SIGNAL(rowsAboutToBeInserted(QModelIndex, int, int))); + QSignalSpy insertedSpy(&model, SIGNAL(rowsInserted(QModelIndex, int, int))); + + nestedItem->appendChild(new StubTreeItem("root-1-1-1", nestedItem)); + + QCOMPARE(model.rowCount(), 0); + QCOMPARE(model.columnCount(), 1); + QCOMPARE(model.headerData(0, Qt::Horizontal).toString(), QString("root")); + + QCOMPARE(aboutToSpy.count(), 0); + QCOMPARE(insertedSpy.count(), 0); +} + +void TreeModelTest::testChangeItemData() { + TreeItem* rootItem = mEmptyRootItem; + mEmptyRootItem = 0; + + StubTreeItem* item = new StubTreeItem("root-1", rootItem); + rootItem->appendChild(item); + TreeModel model(rootItem); + + QSignalSpy dataChangedSpy(&model, + SIGNAL(dataChanged(QModelIndex, QModelIndex))); + + item->setText("root-1modified"); + + QCOMPARE(model.rowCount(), 1); + QCOMPARE(model.columnCount(), 1); + QCOMPARE(model.headerData(0, Qt::Horizontal).toString(), QString("root")); + + QModelIndex index = model.index(0, 0); + assertItem(index, "root-1modified", 0, QModelIndex()); + + QCOMPARE(dataChangedSpy.count(), 1); + QVariant argument = dataChangedSpy.at(0).at(0); + QCOMPARE(argument.userType(), mModelIndexType); + QCOMPARE(qvariant_cast<QModelIndex>(argument), index); + argument = dataChangedSpy.at(0).at(1); + QCOMPARE(argument.userType(), mModelIndexType); + QCOMPARE(qvariant_cast<QModelIndex>(argument), index); +} + void TreeModelTest::testDataWithInvalidIndex() { TreeModel model(mSingleItem); mSingleItem = 0; @@ -290,6 +604,23 @@ QCOMPARE(model->columnCount(index), 1); } +void TreeModelTest::assertSignal(const QSignalSpy& spy, int signalIndex, + const QModelIndex& index, int row) const { + QCOMPARE(spy.at(signalIndex).count(), 3); + + QVariant argument = spy.at(signalIndex).at(0); + QCOMPARE(argument.userType(), mModelIndexType); + QCOMPARE(qvariant_cast<QModelIndex>(argument), index); + + argument = spy.at(signalIndex).at(1); + QCOMPARE(argument.type(), QVariant::Int); + QCOMPARE(argument.toInt(), row); + + argument = spy.at(signalIndex).at(2); + QCOMPARE(argument.type(), QVariant::Int); + QCOMPARE(argument.toInt(), row); +} + QTEST_MAIN(TreeModelTest) #include "TreeModelTest.moc" This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |