[Ktutorial-commits] SF.net SVN: ktutorial:[233] trunk/ktutorial/ktutorial-library
Status: Alpha
Brought to you by:
danxuliu
From: <dan...@us...> - 2010-04-25 23:02:01
|
Revision: 233 http://ktutorial.svn.sourceforge.net/ktutorial/?rev=233&view=rev Author: danxuliu Date: 2010-04-25 23:01:54 +0000 (Sun, 25 Apr 2010) Log Message: ----------- Provide more information about classes in editor support module, so the superclass or the signal list of a class can be fetched by the editor. Modified Paths: -------------- trunk/ktutorial/ktutorial-library/src/editorsupport/CMakeLists.txt trunk/ktutorial/ktutorial-library/src/editorsupport/EditorSupport.cpp trunk/ktutorial/ktutorial-library/src/editorsupport/EditorSupport.h trunk/ktutorial/ktutorial-library/src/editorsupport/ObjectRegister.cpp trunk/ktutorial/ktutorial-library/src/editorsupport/ObjectRegister.h trunk/ktutorial/ktutorial-library/src/editorsupport/ObjectRegisterAdaptor.h trunk/ktutorial/ktutorial-library/tests/editorsupport/CMakeLists.txt trunk/ktutorial/ktutorial-library/tests/editorsupport/EditorSupportTest.cpp trunk/ktutorial/ktutorial-library/tests/editorsupport/ObjectRegisterTest.cpp Added Paths: ----------- trunk/ktutorial/ktutorial-library/src/editorsupport/ClassRegisterAdaptor.cpp trunk/ktutorial/ktutorial-library/src/editorsupport/ClassRegisterAdaptor.h trunk/ktutorial/ktutorial-library/tests/editorsupport/ClassRegisterAdaptorTest.cpp Modified: trunk/ktutorial/ktutorial-library/src/editorsupport/CMakeLists.txt =================================================================== --- trunk/ktutorial/ktutorial-library/src/editorsupport/CMakeLists.txt 2010-04-14 01:14:27 UTC (rev 232) +++ trunk/ktutorial/ktutorial-library/src/editorsupport/CMakeLists.txt 2010-04-25 23:01:54 UTC (rev 233) @@ -1,6 +1,7 @@ include_directories(${CMAKE_CURRENT_BINARY_DIR} ${KDE4_INCLUDES}) set(ktutorial_editorsupport_SRCS + ClassRegisterAdaptor.cpp EditorSupport.cpp EditorSupportAdaptor.cpp EventSpy.cpp Added: trunk/ktutorial/ktutorial-library/src/editorsupport/ClassRegisterAdaptor.cpp =================================================================== --- trunk/ktutorial/ktutorial-library/src/editorsupport/ClassRegisterAdaptor.cpp (rev 0) +++ trunk/ktutorial/ktutorial-library/src/editorsupport/ClassRegisterAdaptor.cpp 2010-04-25 23:01:54 UTC (rev 233) @@ -0,0 +1,85 @@ +/*************************************************************************** + * 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 <QMetaClassInfo> + +#include "ClassRegisterAdaptor.h" +#include "ObjectRegister.h" + +namespace editorsupport { + +//public: + +ClassRegisterAdaptor::ClassRegisterAdaptor(ObjectRegister* objectRegister): + QDBusAbstractAdaptor(objectRegister), + mObjectRegister(objectRegister) { +} + +//public slots: + +QString ClassRegisterAdaptor::superClass(const QString& className) const { + const QMetaObject* metaObject = + mObjectRegister->metaObjectForClassName(className); + if (!metaObject) { + return ""; + } + + if (!metaObject->superClass()) { + return ""; + } + + return metaObject->superClass()->className(); +} + +QStringList ClassRegisterAdaptor::signalList(const QString& className) const { + const QMetaObject* metaObject = + mObjectRegister->metaObjectForClassName(className); + if (!metaObject) { + return QStringList(); + } + + QStringList signalList; + for (int i=0; i<metaObject->methodCount(); ++i) { + QMetaMethod method = metaObject->method(i); + if (isSignalDefinedInClass(method, metaObject)) { + signalList.append(method.signature()); + } + } + + return signalList; +} + +//private: + +bool ClassRegisterAdaptor::isSignalDefinedInClass(const QMetaMethod& metaMethod, + const QMetaObject* metaObject) const { + if (metaMethod.methodType() != QMetaMethod::Signal) { + return false; + } + + const QMetaObject* superClass = metaObject; + while ((superClass = superClass->superClass())) { + if (superClass->indexOfSignal(metaMethod.signature()) != -1) { + return false; + } + } + + return true; +} + +} Property changes on: trunk/ktutorial/ktutorial-library/src/editorsupport/ClassRegisterAdaptor.cpp ___________________________________________________________________ Added: svn:eol-style + native Added: trunk/ktutorial/ktutorial-library/src/editorsupport/ClassRegisterAdaptor.h =================================================================== --- trunk/ktutorial/ktutorial-library/src/editorsupport/ClassRegisterAdaptor.h (rev 0) +++ trunk/ktutorial/ktutorial-library/src/editorsupport/ClassRegisterAdaptor.h 2010-04-25 23:01:54 UTC (rev 233) @@ -0,0 +1,98 @@ +/*************************************************************************** + * 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 EDITORSUPPORT_CLASSREGISTERADAPTOR_H +#define EDITORSUPPORT_CLASSREGISTERADAPTOR_H + +#include <QDBusAbstractAdaptor> +#include <QStringList> + +namespace editorsupport { +class ObjectRegister; +} + +namespace editorsupport { + +/** + * Adaptor to expose an ObjectRegister through DBus. + * It provides methods to get information about the registered QMetaObjects. + * + * @see EditorSupport + */ +class ClassRegisterAdaptor: public QDBusAbstractAdaptor { +Q_OBJECT +Q_CLASSINFO("D-Bus Interface", "org.kde.ktutorial.ClassRegister") +public: + + /** + * Creates a new ClassRegisterAdaptor for the given ObjectRegister. + * + * @param objectRegister The ObjectRegister to adapt. + */ + explicit ClassRegisterAdaptor(ObjectRegister* objectRegister); + +public Q_SLOTS: + + /** + * Returns super class name of the class with the given name. + * If the class name is not registered, or it has no super class, an empty + * string is returned. + * + * @param className The name of the class. + * @return The name of the super class. + */ + QString superClass(const QString& className) const; + + /** + * Returns the signals of the class with the given name. + * If the class is not registered, an empty list is returned. + * + * Only the signals defined in the given class are included in the list. + * Signals from parent classes must be got using the parent class name. + * + * @param className The name of the class. + * @return The list of signals. + */ + QStringList signalList(const QString& className) const; + +private: + + /** + * The ObjectRegister to adapt. + */ + ObjectRegister* mObjectRegister; + + /** + * Checks whether the given meta method is a signal defined in the given + * meta object. + * If the signal is inherited in the given meta object instead of defined in + * it, false is also returned. + * + * @param metaMethod The meta method to check. + * @param metaObject The meta object to check the meta method with. + * @return True if the meta method is a signal defined in the meta object, + * false otherwise. + */ + bool isSignalDefinedInClass(const QMetaMethod& metaMethod, + const QMetaObject* metaObject) const; + +}; + +} + +#endif Property changes on: trunk/ktutorial/ktutorial-library/src/editorsupport/ClassRegisterAdaptor.h ___________________________________________________________________ Added: svn:eol-style + native Modified: trunk/ktutorial/ktutorial-library/src/editorsupport/EditorSupport.cpp =================================================================== --- trunk/ktutorial/ktutorial-library/src/editorsupport/EditorSupport.cpp 2010-04-14 01:14:27 UTC (rev 232) +++ trunk/ktutorial/ktutorial-library/src/editorsupport/EditorSupport.cpp 2010-04-25 23:01:54 UTC (rev 233) @@ -22,6 +22,7 @@ #include <KDebug> #include "EditorSupport.h" +#include "ClassRegisterAdaptor.h" #include "EditorSupportAdaptor.h" #include "EventSpy.h" #include "EventSpyAdaptor.h" @@ -53,6 +54,7 @@ QDBusConnection::sessionBus().registerObject("/ktutorial", this); mObjectRegister = new ObjectRegister(this); + new ClassRegisterAdaptor(mObjectRegister); new ObjectRegisterAdaptor(mObjectRegister); QDBusConnection::sessionBus().registerObject("/ktutorial/ObjectRegister", Modified: trunk/ktutorial/ktutorial-library/src/editorsupport/EditorSupport.h =================================================================== --- trunk/ktutorial/ktutorial-library/src/editorsupport/EditorSupport.h 2010-04-14 01:14:27 UTC (rev 232) +++ trunk/ktutorial/ktutorial-library/src/editorsupport/EditorSupport.h 2010-04-25 23:01:54 UTC (rev 233) @@ -45,7 +45,9 @@ * The object register assigns an id to QObjects to be identified by the remote * KTutorial editor. Using that id, KTutorial editor can request further * information about an object to the ObjectRegister (for example, the object - * name or the class name of an object). + * name or the class name of an object). Moreover, the object register also + * provides information about the classes of the registered objects, like the + * super class or the list of signals defined in each class. * * The event spy filters all the events received by the main window and, * recursively, all its children objects. It is used in the remote KTutorial @@ -57,9 +59,10 @@ * The main object is registered at "/ktutorial" path, and provides the * "org.kde.ktutorial.EditorSupport" interface. The object register is * registered at "/ktutorial/ObjectRegister" path and provides the - * "org.kde.ktutorial.ObjectRegister" interface. Finally, when it is enabled, - * the EventSpy is registered at "/ktutorial/EVentSpy" path and provides the - * "org.kde.ktutorial.EventSpy" interface. + * "org.kde.ktutorial.ObjectRegister" and "org.kde.ktutorial.ClassRegister" + * interfaces. Finally, when it is enabled, the EventSpy is registered at + * "/ktutorial/EVentSpy" path and provides the "org.kde.ktutorial.EventSpy" + * interface. */ class EditorSupport: public QObject { Q_OBJECT Modified: trunk/ktutorial/ktutorial-library/src/editorsupport/ObjectRegister.cpp =================================================================== --- trunk/ktutorial/ktutorial-library/src/editorsupport/ObjectRegister.cpp 2010-04-14 01:14:27 UTC (rev 232) +++ trunk/ktutorial/ktutorial-library/src/editorsupport/ObjectRegister.cpp 2010-04-25 23:01:54 UTC (rev 233) @@ -36,6 +36,8 @@ connect(object, SIGNAL(destroyed(QObject*)), this, SLOT(deregister(QObject*))); mNextId++; + + registerMetaObject(object->metaObject()); } return id; @@ -45,11 +47,31 @@ return mRegisteredObjects.value(objectId); } +const QMetaObject* ObjectRegister::metaObjectForClassName( + const QString& className) const { + return mRegisteredMetaObjects.value(className); +} + void ObjectRegister::clear() { mRegisteredIds.clear(); mRegisteredObjects.clear(); + mRegisteredMetaObjects.clear(); } +//private: + +void ObjectRegister::registerMetaObject(const QMetaObject* metaObject) { + if (mRegisteredMetaObjects.contains(metaObject->className())) { + return; + } + + mRegisteredMetaObjects.insert(metaObject->className(), metaObject); + + if (metaObject->superClass()) { + registerMetaObject(metaObject->superClass()); + } +} + //private slots: void ObjectRegister::deregister(QObject* object) { Modified: trunk/ktutorial/ktutorial-library/src/editorsupport/ObjectRegister.h =================================================================== --- trunk/ktutorial/ktutorial-library/src/editorsupport/ObjectRegister.h 2010-04-14 01:14:27 UTC (rev 232) +++ trunk/ktutorial/ktutorial-library/src/editorsupport/ObjectRegister.h 2010-04-25 23:01:54 UTC (rev 233) @@ -31,6 +31,10 @@ * destroyed (if an object is destroyed it is automatically removed from the * register). * + * When an object is registered, its QMetaObject and the QMetaObject of all its + * super classes are also registered. The QMetaObject can be got using the class + * name since then until the register is cleared. + * * Its purpose is assign QObjects an id to allow the remote KTutorial editor to * refer to the objects in the target application. */ @@ -62,6 +66,14 @@ QObject* objectForId(int objectId); /** + * Returns the meta object with the given class name. + * + * @param className The class name to get its meta object. + * @return The meta object with the given class name. + */ + const QMetaObject* metaObjectForClassName(const QString& className) const; + + /** * Removes all the entries in this ObjectRegister. */ void clear(); @@ -83,6 +95,18 @@ */ QHash<int, QObject*> mRegisteredObjects; + /** + * The registered meta objects mapped by their class name. + */ + QHash<QString, const QMetaObject*> mRegisteredMetaObjects; + + /** + * Registers a meta object and the meta objects of all its super classes. + * + * @param metaObject The meta object to register. + */ + void registerMetaObject(const QMetaObject* metaObject); + private Q_SLOTS: /** Modified: trunk/ktutorial/ktutorial-library/src/editorsupport/ObjectRegisterAdaptor.h =================================================================== --- trunk/ktutorial/ktutorial-library/src/editorsupport/ObjectRegisterAdaptor.h 2010-04-14 01:14:27 UTC (rev 232) +++ trunk/ktutorial/ktutorial-library/src/editorsupport/ObjectRegisterAdaptor.h 2010-04-25 23:01:54 UTC (rev 233) @@ -29,6 +29,7 @@ /** * Adaptor to expose an ObjectRegister through DBus. + * It provides methods to get information about the registered QObjects. * * @see EditorSupport */ Modified: trunk/ktutorial/ktutorial-library/tests/editorsupport/CMakeLists.txt =================================================================== --- trunk/ktutorial/ktutorial-library/tests/editorsupport/CMakeLists.txt 2010-04-14 01:14:27 UTC (rev 232) +++ trunk/ktutorial/ktutorial-library/tests/editorsupport/CMakeLists.txt 2010-04-25 23:01:54 UTC (rev 233) @@ -17,6 +17,7 @@ ENDMACRO(UNIT_TESTS) unit_tests( + ClassRegisterAdaptor EditorSupport EditorSupportAdaptor EventSpy @@ -32,6 +33,7 @@ ENDMACRO(MEM_TESTS) mem_tests( + ClassRegisterAdaptor EditorSupport EditorSupportAdaptor EventSpy Added: trunk/ktutorial/ktutorial-library/tests/editorsupport/ClassRegisterAdaptorTest.cpp =================================================================== --- trunk/ktutorial/ktutorial-library/tests/editorsupport/ClassRegisterAdaptorTest.cpp (rev 0) +++ trunk/ktutorial/ktutorial-library/tests/editorsupport/ClassRegisterAdaptorTest.cpp 2010-04-25 23:01:54 UTC (rev 233) @@ -0,0 +1,96 @@ +/*************************************************************************** + * 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 "ClassRegisterAdaptor.h" +#include "ObjectRegister.h" + +namespace editorsupport { + +class ClassRegisterAdaptorTest: public QObject { +Q_OBJECT + +Q_SIGNALS: + + void dummySignal(); + void dummySignal(int argument1, const QString& argument2); + +private slots: + + void testConstructor(); + + void testSuperClass(); + void testSuperClassWithUnknownClassName(); + + void testSignalList(); + void testSignalListWithUnknownClassName(); + +}; + +void ClassRegisterAdaptorTest::testConstructor() { + ObjectRegister objectRegister; + ClassRegisterAdaptor* adaptor = new ClassRegisterAdaptor(&objectRegister); + + QCOMPARE(adaptor->parent(), &objectRegister); +} + +void ClassRegisterAdaptorTest::testSuperClass() { + ObjectRegister objectRegister; + ClassRegisterAdaptor* adaptor = new ClassRegisterAdaptor(&objectRegister); + + objectRegister.idForObject(this); + + QCOMPARE(adaptor->superClass("editorsupport::ClassRegisterAdaptorTest"), + QString("QObject")); + QCOMPARE(adaptor->superClass("QObject"), QString("")); +} + +void ClassRegisterAdaptorTest::testSuperClassWithUnknownClassName() { + ObjectRegister objectRegister; + ClassRegisterAdaptor* adaptor = new ClassRegisterAdaptor(&objectRegister); + + QCOMPARE(adaptor->superClass("UnknownClassName"), QString("")); +} + +void ClassRegisterAdaptorTest::testSignalList() { + ObjectRegister objectRegister; + ClassRegisterAdaptor* adaptor = new ClassRegisterAdaptor(&objectRegister); + + objectRegister.idForObject(this); + + QStringList signalList = + adaptor->signalList("editorsupport::ClassRegisterAdaptorTest"); + QCOMPARE(signalList.count(), 2); + QCOMPARE(signalList[0], QString("dummySignal()")); + QCOMPARE(signalList[1], QString("dummySignal(int,QString)")); +} + +void ClassRegisterAdaptorTest::testSignalListWithUnknownClassName() { + ObjectRegister objectRegister; + ClassRegisterAdaptor* adaptor = new ClassRegisterAdaptor(&objectRegister); + + QStringList signalList = adaptor->signalList("UnknownClassName"); + QCOMPARE(signalList.count(), 0); +} + +} + +QTEST_MAIN(editorsupport::ClassRegisterAdaptorTest) + +#include "ClassRegisterAdaptorTest.moc" Property changes on: trunk/ktutorial/ktutorial-library/tests/editorsupport/ClassRegisterAdaptorTest.cpp ___________________________________________________________________ Added: svn:eol-style + native Modified: trunk/ktutorial/ktutorial-library/tests/editorsupport/EditorSupportTest.cpp =================================================================== --- trunk/ktutorial/ktutorial-library/tests/editorsupport/EditorSupportTest.cpp 2010-04-14 01:14:27 UTC (rev 232) +++ trunk/ktutorial/ktutorial-library/tests/editorsupport/EditorSupportTest.cpp 2010-04-25 23:01:54 UTC (rev 233) @@ -27,8 +27,10 @@ #undef private #undef protected +#include "ClassRegisterAdaptor.h" #include "EventSpy.h" #include "ObjectRegister.h" +#include "ObjectRegisterAdaptor.h" #include "../extendedinformation/WidgetHighlighter.h" using extendedinformation::WidgetHighlighter; @@ -96,7 +98,11 @@ QObject* objectRegisterObject = bus.objectRegisteredAt("/ktutorial/ObjectRegister"); QVERIFY(objectRegisterObject); - QVERIFY(qobject_cast<ObjectRegister*>(objectRegisterObject)); + ObjectRegister* objectRegister = + qobject_cast<ObjectRegister*>(objectRegisterObject); + QVERIFY(objectRegister); + QVERIFY(objectRegister->findChild<ClassRegisterAdaptor*>("")); + QVERIFY(objectRegister->findChild<ObjectRegisterAdaptor*>("")); QVERIFY(!bus.objectRegisteredAt("/ktutorial/EventSpy")); } Modified: trunk/ktutorial/ktutorial-library/tests/editorsupport/ObjectRegisterTest.cpp =================================================================== --- trunk/ktutorial/ktutorial-library/tests/editorsupport/ObjectRegisterTest.cpp 2010-04-14 01:14:27 UTC (rev 232) +++ trunk/ktutorial/ktutorial-library/tests/editorsupport/ObjectRegisterTest.cpp 2010-04-25 23:01:54 UTC (rev 233) @@ -20,6 +20,18 @@ #include "ObjectRegister.h" +class DummyClass: public QObject { +Q_OBJECT +}; + +class DummyChildClass1: public DummyClass { +Q_OBJECT +}; + +class DummyChildClass2: public DummyClass { +Q_OBJECT +}; + namespace editorsupport { class ObjectRegisterTest: public QObject { @@ -48,17 +60,23 @@ void ObjectRegisterTest::testRegisterObject() { ObjectRegister objectRegister; - QObject object; + DummyChildClass1 object; int id = objectRegister.idForObject(&object); QCOMPARE(objectRegister.objectForId(id), &object); + QCOMPARE(objectRegister.metaObjectForClassName("DummyChildClass1"), + &DummyChildClass1::staticMetaObject); + QCOMPARE(objectRegister.metaObjectForClassName("DummyClass"), + &DummyClass::staticMetaObject); + QCOMPARE(objectRegister.metaObjectForClassName("QObject"), + &QObject::staticMetaObject); } void ObjectRegisterTest::testRegisterObjectSeveralObjects() { ObjectRegister objectRegister; - QObject object1; - QObject object2; + DummyChildClass1 object1; + DummyChildClass2 object2; QObject object3; int id1 = objectRegister.idForObject(&object1); @@ -71,6 +89,14 @@ QCOMPARE(objectRegister.objectForId(id1), &object1); QCOMPARE(objectRegister.objectForId(id2), &object2); QCOMPARE(objectRegister.objectForId(id3), &object3); + QCOMPARE(objectRegister.metaObjectForClassName("DummyChildClass1"), + &DummyChildClass1::staticMetaObject); + QCOMPARE(objectRegister.metaObjectForClassName("DummyChildClass2"), + &DummyChildClass2::staticMetaObject); + QCOMPARE(objectRegister.metaObjectForClassName("DummyClass"), + &DummyClass::staticMetaObject); + QCOMPARE(objectRegister.metaObjectForClassName("QObject"), + &QObject::staticMetaObject); } void ObjectRegisterTest::testRegisterObjectTwice() { @@ -82,6 +108,8 @@ QCOMPARE(id2, id1); QCOMPARE(objectRegister.objectForId(id1), &object); + QCOMPARE(objectRegister.metaObjectForClassName("QObject"), + &QObject::staticMetaObject); } void ObjectRegisterTest::testObjectForIdWithDestroyedObject() { @@ -93,6 +121,8 @@ delete object; QCOMPARE(objectRegister.objectForId(id), (QObject*)0); + QCOMPARE(objectRegister.metaObjectForClassName("QObject"), + &QObject::staticMetaObject); } void ObjectRegisterTest::testClear() { @@ -105,6 +135,7 @@ objectRegister.clear(); QCOMPARE(objectRegister.objectForId(id), (QObject*)0); + QCOMPARE(objectRegister.metaObjectForClassName("QObject"), (QMetaObject*)0); } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |