[Ktutorial-commits] SF.net SVN: ktutorial:[290] trunk/ktutorial/ktutorial-library
Status: Alpha
Brought to you by:
danxuliu
|
From: <dan...@us...> - 2011-03-08 02:59:44
|
Revision: 290
http://ktutorial.svn.sourceforge.net/ktutorial/?rev=290&view=rev
Author: danxuliu
Date: 2011-03-08 02:59:37 +0000 (Tue, 08 Mar 2011)
Log Message:
-----------
Add support for object paths instead of just object names in KTutorial::findObject(QString). Now the ancestor names can be included when finding an object to differentiate between several objects with the same name.
Modified Paths:
--------------
trunk/ktutorial/ktutorial-library/src/KTutorial.h
trunk/ktutorial/ktutorial-library/tests/CMakeLists.txt
Added Paths:
-----------
trunk/ktutorial/ktutorial-library/tests/KTutorialTest.cpp
Modified: trunk/ktutorial/ktutorial-library/src/KTutorial.h
===================================================================
--- trunk/ktutorial/ktutorial-library/src/KTutorial.h 2011-03-08 02:47:15 UTC (rev 289)
+++ trunk/ktutorial/ktutorial-library/src/KTutorial.h 2011-03-08 02:59:37 UTC (rev 290)
@@ -1,5 +1,5 @@
/***************************************************************************
- * Copyright (C) 2008-2010 by Daniel Calviño Sánchez *
+ * Copyright (C) 2008-2011 by Daniel Calviño Sánchez *
* dan...@gm... *
* *
* This program is free software; you can redistribute it and/or modify *
@@ -123,6 +123,15 @@
* Returns the object with the specified name, if any.
* Objects are searched in the children of the main window of the
* application.
+ * When the name of the desired object is not unique the name of some
+ * ancestor must be included. Ancestor names are separated using a "/". That
+ * is, "Ancestor name/The object with a repeated name". As many ancestor
+ * names as desired can be included, not just one.
+ * The name of the ancestors does not need to be unique either; what has to
+ * be unique is the full path to the object to find. For example, "Ancestor
+ * name not unique/The object to find" is valid if, among all the objects
+ * called "Ancestor name not unique", there is only one that has a
+ * descendant called "The object to find".
*
* @param name The name of the object to find.
* @return The object with the specified name, or null if there is none.
@@ -131,7 +140,7 @@
*/
template <typename T>
T findObject(const QString& name) const {
- return mParent->findChild<T>(name);
+ return findObject<T>(name, mParent);
}
private:
@@ -167,6 +176,44 @@
mParent = 0;
}
+ /**
+ * Returns the object with the specified name that is descendant of the
+ * given parent, if any.
+ * The name of the object can contain ancestor names separated by "/". The
+ * first object that matches the whole path is returned (note that ancestor
+ * are not necessarily direct parents).
+ *
+ * @param name The name of the object to find.
+ * @param parent The parent to look the object in.
+ * @return The object with the specified name, or null if there is none.
+ */
+ template <typename T>
+ T findObject(const QString& name, const QObject* ancestor) const {
+ if (name.isEmpty() || ancestor == 0) {
+ return T();
+ }
+
+ if (name.indexOf('/') == -1) {
+ return ancestor->findChild<T>(name);
+ }
+
+ QRegExp slashPattern("/+");
+ QString ancestorName = name.left(name.indexOf(slashPattern));
+ QString descendantName = name.mid(ancestorName.length() +
+ slashPattern.matchedLength());
+
+ QList<QObject*> namedAncestors =
+ ancestor->findChildren<QObject*>(ancestorName);
+ foreach (QObject* namedAncestor, namedAncestors) {
+ T object = findObject<T>(descendantName, namedAncestor);
+ if (object) {
+ return object;
+ }
+ }
+
+ return 0;
+ }
+
private slots:
/**
Modified: trunk/ktutorial/ktutorial-library/tests/CMakeLists.txt
===================================================================
--- trunk/ktutorial/ktutorial-library/tests/CMakeLists.txt 2011-03-08 02:47:15 UTC (rev 289)
+++ trunk/ktutorial/ktutorial-library/tests/CMakeLists.txt 2011-03-08 02:59:37 UTC (rev 290)
@@ -26,6 +26,7 @@
ENDMACRO(UNIT_TESTS)
unit_tests(
+ KTutorial
Option
Step
Tutorial
@@ -48,6 +49,7 @@
ENDMACRO(MEM_TESTS)
mem_tests(
+ KTutorial
Option
Step
Tutorial
Added: trunk/ktutorial/ktutorial-library/tests/KTutorialTest.cpp
===================================================================
--- trunk/ktutorial/ktutorial-library/tests/KTutorialTest.cpp (rev 0)
+++ trunk/ktutorial/ktutorial-library/tests/KTutorialTest.cpp 2011-03-08 02:59:37 UTC (rev 290)
@@ -0,0 +1,185 @@
+/***************************************************************************
+ * Copyright (C) 2011 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 <QAction>
+
+#include "KTutorial.h"
+
+class KTutorialTest: public QObject {
+Q_OBJECT
+
+private slots:
+
+ void initTestCase();
+ void cleanupTestCase();
+
+ void testFindObjectSingleName();
+ void testFindObjectSingleNameUnknown();
+ void testFindObjectComplexNameDirectChild();
+ void testFindObjectComplexNameNestedChild();
+ void testFindObjectComplexNameAncestorNameNotUnique();
+ void testFindObjectComplexNameUnknownParent();
+ void testFindObjectEmptyName();
+ void testFindObjectSingleSlash();
+ void testFindObjectSlashEndedName();
+ void testFindObjectSeveralSlashes();
+
+private:
+
+ KXmlGuiWindow* mMainWindow;
+ QObject* mObject1_1_1;
+ QAction* mAction1_1_2;
+ QObject* mObject1_2_1;
+ QObject* mObject2_1_1;
+ QObject* mObject2_1_2;
+ QAction* mAction2_1_3;
+ QAction* mAction2_1_4;
+ QObject* mObject3_1_1;
+ QObject* mObject3_2_1_1;
+ QAction* mAction3_3_1_1;
+
+ void assertFindObject(const QString& objectName, QObject* object) const;
+ void assertFindAction(const QString& objectName, QAction* widget) const;
+
+};
+
+void KTutorialTest::initTestCase() {
+ mMainWindow = new KXmlGuiWindow();
+ KTutorial::self()->setup(mMainWindow);
+
+ QObject* grandParent1 = new QObject(mMainWindow);
+ grandParent1->setObjectName("Grand parent1");
+ QObject* parent1_1 = new QObject(grandParent1);
+ parent1_1->setObjectName("Parent1");
+ mObject1_1_1 = new QObject(parent1_1);
+ mObject1_1_1->setObjectName("The object");
+ mAction1_1_2 = new QAction(parent1_1);
+ mAction1_1_2->setObjectName("The widget");
+
+ QObject* parent1_2 = new QObject(grandParent1);
+ parent1_2->setObjectName("Parent2");
+ mObject1_2_1 = new QObject(parent1_2);
+ mObject1_2_1->setObjectName("The object");
+
+ QObject* grandParent2 = new QObject(mMainWindow);
+ grandParent2->setObjectName("Grand parent2");
+ QObject* parent2_1 = new QObject(grandParent2);
+ parent2_1->setObjectName("Parent1");
+ mObject2_1_1 = new QObject(parent2_1);
+ mObject2_1_1->setObjectName("The object");
+ mObject2_1_2 = new QObject(parent2_1);
+ mObject2_1_2->setObjectName("Another object");
+ mAction2_1_3 = new QAction(parent2_1);
+ mAction2_1_3->setObjectName("The widget");
+ mAction2_1_4 = new QAction(parent2_1);
+ mAction2_1_4->setObjectName("Another widget");
+
+ QObject* grandParent3 = new QObject(mMainWindow);
+ grandParent3->setObjectName("Grand parent3");
+ QObject* parent3_1 = new QObject(grandParent3);
+ parent3_1->setObjectName("Parent1");
+ mObject3_1_1 = new QObject(parent3_1);
+ mObject3_1_1->setObjectName("The object");
+
+ QObject* parent3_2 = new QObject(grandParent3);
+ parent3_2->setObjectName("Nested parent");
+ QObject* parent3_2_1 = new QObject(parent3_2);
+ mObject3_2_1_1 = new QObject(parent3_2_1);
+ mObject3_2_1_1->setObjectName("Another object");
+
+ QTimer* parent3_3 = new QTimer(grandParent3);
+ parent3_3->setObjectName("Nested timer");
+ QTimer* parent3_3_1 = new QTimer(parent3_3);
+ mAction3_3_1_1 = new QAction(parent3_3_1);
+ mAction3_3_1_1->setObjectName("Another widget");
+}
+
+void KTutorialTest::cleanupTestCase() {
+ delete mMainWindow;
+}
+
+void KTutorialTest::testFindObjectSingleName() {
+ assertFindObject("The object", mObject1_1_1);
+}
+
+void KTutorialTest::testFindObjectSingleNameUnknown() {
+ assertFindObject("Unknown object", 0);
+}
+
+void KTutorialTest::testFindObjectComplexNameDirectChild() {
+ assertFindObject("Grand parent1/Parent1/The object", mObject1_1_1);
+ assertFindObject("Grand parent1/Parent2/The object", mObject1_2_1);
+ assertFindObject("Grand parent2/Parent1/The object", mObject2_1_1);
+
+ assertFindAction("Grand parent1/Parent1/The widget", mAction1_1_2);
+ assertFindAction("Grand parent2/Parent1/The widget", mAction2_1_3);
+}
+
+void KTutorialTest::testFindObjectComplexNameNestedChild() {
+ assertFindObject("Grand parent2/The object", mObject2_1_1);
+ assertFindObject("Nested parent/Another object", mObject3_2_1_1);
+
+ assertFindAction("Grand parent2/The widget", mAction2_1_3);
+ assertFindAction("Nested timer/Another widget", mAction3_3_1_1);
+}
+
+void KTutorialTest::testFindObjectComplexNameAncestorNameNotUnique() {
+ //The ancestor name is not unique, but the full path is
+ assertFindObject("Parent1/Another object", mObject2_1_2);
+
+ assertFindAction("Parent1/Another widget", mAction2_1_4);
+}
+
+void KTutorialTest::testFindObjectComplexNameUnknownParent() {
+ assertFindObject("Unknown grand parent/The object", 0);
+ assertFindObject("Grand parent1/Unknown parent/The object", 0);
+}
+
+void KTutorialTest::testFindObjectEmptyName() {
+ assertFindObject("", 0);
+}
+
+void KTutorialTest::testFindObjectSingleSlash() {
+ assertFindObject("/", 0);
+}
+
+void KTutorialTest::testFindObjectSlashEndedName() {
+ assertFindObject("Parent/", 0);
+}
+
+void KTutorialTest::testFindObjectSeveralSlashes() {
+ assertFindObject("Parent1///The object", mObject1_1_1);
+}
+
+/////////////////////////////////Helpers////////////////////////////////////////
+
+void KTutorialTest::assertFindObject(const QString& objectName,
+ QObject* object) const {
+ QCOMPARE(KTutorial::self()->findObject<QObject*>(objectName), object);
+}
+
+void KTutorialTest::assertFindAction(const QString& objectName,
+ QAction* widget) const {
+ QCOMPARE(KTutorial::self()->findObject<QAction*>(objectName), widget);
+}
+
+QTEST_MAIN(KTutorialTest)
+
+#include "KTutorialTest.moc"
Property changes on: trunk/ktutorial/ktutorial-library/tests/KTutorialTest.cpp
___________________________________________________________________
Added: svn:eol-style
+ native
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|