[Ktutorial-commits] SF.net SVN: ktutorial:[307] trunk/ktutorial/ktutorial-library
Status: Alpha
Brought to you by:
danxuliu
From: <dan...@us...> - 2011-05-16 22:42:39
|
Revision: 307 http://ktutorial.svn.sourceforge.net/ktutorial/?rev=307&view=rev Author: danxuliu Date: 2011-05-16 22:42:33 +0000 (Mon, 16 May 2011) Log Message: ----------- Provide the names of the properties of a class in the editor support module. Modified 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/ClassRegisterAdaptor.cpp =================================================================== --- trunk/ktutorial/ktutorial-library/src/editorsupport/ClassRegisterAdaptor.cpp 2011-05-16 16:46:51 UTC (rev 306) +++ trunk/ktutorial/ktutorial-library/src/editorsupport/ClassRegisterAdaptor.cpp 2011-05-16 22:42:33 UTC (rev 307) @@ -1,5 +1,5 @@ /*************************************************************************** - * Copyright (C) 2010 by Daniel Calviño Sánchez * + * Copyright (C) 2010-2011 by Daniel Calviño Sánchez * * dan...@gm... * * * * This program is free software; you can redistribute it and/or modify * @@ -46,6 +46,24 @@ return metaObject->superClass()->className(); } +QStringList ClassRegisterAdaptor::propertyList(const QString& className) const { + const QMetaObject* metaObject = + mObjectRegister->metaObjectForClassName(className); + if (!metaObject) { + return QStringList(); + } + + QStringList propertyList; + for (int i=0; i<metaObject->propertyCount(); ++i) { + QMetaProperty property = metaObject->property(i); + if (isPropertyDefinedInClass(property, metaObject)) { + propertyList.append(property.name()); + } + } + + return propertyList; +} + QStringList ClassRegisterAdaptor::signalList(const QString& className) const { const QMetaObject* metaObject = mObjectRegister->metaObjectForClassName(className); @@ -66,6 +84,19 @@ //private: +bool ClassRegisterAdaptor::isPropertyDefinedInClass( + const QMetaProperty& metaProperty, + const QMetaObject* metaObject) const { + const QMetaObject* superClass = metaObject; + while ((superClass = superClass->superClass())) { + if (superClass->indexOfProperty(metaProperty.name()) != -1) { + return false; + } + } + + return true; +} + bool ClassRegisterAdaptor::isSignalDefinedInClass(const QMetaMethod& metaMethod, const QMetaObject* metaObject) const { if (metaMethod.methodType() != QMetaMethod::Signal) { Modified: trunk/ktutorial/ktutorial-library/src/editorsupport/ClassRegisterAdaptor.h =================================================================== --- trunk/ktutorial/ktutorial-library/src/editorsupport/ClassRegisterAdaptor.h 2011-05-16 16:46:51 UTC (rev 306) +++ trunk/ktutorial/ktutorial-library/src/editorsupport/ClassRegisterAdaptor.h 2011-05-16 22:42:33 UTC (rev 307) @@ -1,5 +1,5 @@ /*************************************************************************** - * Copyright (C) 2010 by Daniel Calviño Sánchez * + * Copyright (C) 2010-2011 by Daniel Calviño Sánchez * * dan...@gm... * * * * This program is free software; you can redistribute it and/or modify * @@ -59,6 +59,18 @@ QString superClass(const QString& className) const; /** + * Returns the properties of the class with the given name. + * If the class is not registered, an empty list is returned. + * + * Only the properties defined in the given class are included in the list. + * Properties from parent classes must be got using the parent class name. + * + * @param className The name of the class. + * @return The list of properties. + */ + QStringList propertyList(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. * @@ -78,6 +90,20 @@ ObjectRegister* mObjectRegister; /** + * Checks whether the given meta property is a property defined in the given + * meta object. + * If the property is inherited in the given meta object instead of defined + * in it, false is also returned. + * + * @param metaProperty The meta property to check. + * @param metaObject The meta object to check the meta property with. + * @return True if the meta property is a property defined in the meta + * object, false otherwise. + */ + bool isPropertyDefinedInClass(const QMetaProperty& metaProperty, + const QMetaObject* metaObject) const; + + /** * 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 Modified: trunk/ktutorial/ktutorial-library/tests/editorsupport/ClassRegisterAdaptorTest.cpp =================================================================== --- trunk/ktutorial/ktutorial-library/tests/editorsupport/ClassRegisterAdaptorTest.cpp 2011-05-16 16:46:51 UTC (rev 306) +++ trunk/ktutorial/ktutorial-library/tests/editorsupport/ClassRegisterAdaptorTest.cpp 2011-05-16 22:42:33 UTC (rev 307) @@ -1,5 +1,5 @@ /*************************************************************************** - * Copyright (C) 2010 by Daniel Calviño Sánchez * + * Copyright (C) 2010-2011 by Daniel Calviño Sánchez * * dan...@gm... * * * * This program is free software; you can redistribute it and/or modify * @@ -26,6 +26,20 @@ class ClassRegisterAdaptorTest: public QObject { Q_OBJECT +Q_PROPERTY(QString dummyProperty READ dummyProperty) +Q_PROPERTY(QString dummyPropertyWithNotifySignal + READ dummyPropertyWithNotifySignal NOTIFY dummySignal) + +public: + + QString dummyProperty() const { + return mDummyProperty; + } + + QString dummyPropertyWithNotifySignal() const { + return mDummyPropertyWithNotifySignal; + } + Q_SIGNALS: void dummySignal(); @@ -38,9 +52,17 @@ void testSuperClass(); void testSuperClassWithUnknownClassName(); + void testPropertyList(); + void testPropertyListWithUnknownClassName(); + void testSignalList(); void testSignalListWithUnknownClassName(); +private: + + QString mDummyProperty; + QString mDummyPropertyWithNotifySignal; + }; void ClassRegisterAdaptorTest::testConstructor() { @@ -68,6 +90,27 @@ QCOMPARE(adaptor->superClass("UnknownClassName"), QString("")); } +void ClassRegisterAdaptorTest::testPropertyList() { + ObjectRegister objectRegister; + ClassRegisterAdaptor* adaptor = new ClassRegisterAdaptor(&objectRegister); + + objectRegister.idForObject(this); + + QStringList propertyList = + adaptor->propertyList("editorsupport::ClassRegisterAdaptorTest"); + QCOMPARE(propertyList.count(), 2); + QCOMPARE(propertyList[0], QString("dummyProperty")); + QCOMPARE(propertyList[1], QString("dummyPropertyWithNotifySignal")); +} + +void ClassRegisterAdaptorTest::testPropertyListWithUnknownClassName() { + ObjectRegister objectRegister; + ClassRegisterAdaptor* adaptor = new ClassRegisterAdaptor(&objectRegister); + + QStringList propertyList = adaptor->propertyList("UnknownClassName"); + QCOMPARE(propertyList.count(), 0); +} + void ClassRegisterAdaptorTest::testSignalList() { ObjectRegister objectRegister; ClassRegisterAdaptor* adaptor = new ClassRegisterAdaptor(&objectRegister); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |