From 92f5d171db4e92b4e54dfe20d6750a9adf9002d4 Mon Sep 17 00:00:00 2001 From: Vasilis Liaskovitis Date: Mon, 23 Dec 2013 02:20:49 +0200 Subject: [PATCH] Controller: initial commit This adds a QVariant/QMetaObject based controller for object and properties manipulation. Object types and objects are stored in std::map structures for now. The API is based on the prototype controller. The get/set properties only handle a single QVariant argument instead of a QVariantList argument as in the prototype but this can be changed. --- Controller.cpp | 123 +++++++++++++++++++++++++++++++++++++++++++++++ Controller.h | 47 ++++++++++++++++++ libremapping.pro | 2 +- 3 files changed, 171 insertions(+), 1 deletion(-) create mode 100644 Controller.cpp create mode 100644 Controller.h diff --git a/Controller.cpp b/Controller.cpp new file mode 100644 index 0000000..c7863fd --- /dev/null +++ b/Controller.cpp @@ -0,0 +1,123 @@ +/* + * Controller.cpp + * + * (c) 2013 Sofian Audry -- info(@)sofianaudry(.)com + * (c) 2013 Alexandre Quessy -- alexandre(@)quessy(.)net + * (c) 2013 Vasilis Liaskovitis -- vliaskov@gmail.com + * + * 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 . + */ +#include "Controller.h" +#include +#include +#include + +template void Controller::registerObjType(const char* objType) +{ + _mControllerTypes[objType] = &(T::staticMetaObject); +} + +bool Controller::createObject(const char* objType, const char* objName) +{ + const QMetaObject *meta = _mControllerTypes[objType]; + + if (meta) + { + _mControllerObjects[objName] = meta->newInstance(); + return true; + } + //std::cout << "object creation " << objName << " failed\n"; + return false; +} + +bool Controller::setObjectProperty(const char* objName, const char* propName, + const QVariant &value) +{ + QObject *obj = _mControllerObjects[objName]; + if (obj) + { + if (obj->setProperty(propName, value)) + { + return true; + } + else + { + //std::cout << "setproperty failed " << propName << "\n"; + return false; + } + } + //std::cout << "object " << objName << " could not be found\n"; + return false; +} + +bool Controller::getObjectProperty(const char *objName, const char *propName, + QVariant &value) +{ + QObject *obj = _mControllerObjects[objName]; + if (obj) + { + value = obj->property(propName); + return value.isValid(); + } + //std::cout << "Failed to get property " << propName << "\n"; + return false; +} + +bool Controller::listObjectProperties(const char *objName, QList + &names, QVariantList &values) +{ + QObject *obj = _mControllerObjects[objName]; + if (obj) + { + const QMetaObject *metaobject = obj->metaObject(); + int count = metaobject->propertyCount(); + for (int i = 0; i < count; ++i) + { + QMetaProperty metaproperty = metaobject->property(i); + const char *propname = metaproperty.name(); + names.append(QString(propname)); + values.append(obj->property(propname)); + } + return true; + } + return false; +} + +bool Controller::listObjects(const char *objType, QList &objNames) +{ + const QMetaObject *meta = _mControllerTypes[objType]; + for (std::map::iterator i = _mControllerObjects.begin(); + i != _mControllerObjects.end(); i++) + { + const QObject *obj = i->second; + const char *name = i->first; + const QMetaObject *metaobject = obj->metaObject(); + if (meta == metaobject) + { + //std::cout << "found object " << name << " of type " << objType << " \n"; + objNames.append(name); + } + } + return true; +} + +Controller::Controller(MainWindow *owner) +{ + _owner = owner; + /* Register common types for properties + * FIXME, we can provide a method for other classes to register types of their + * properties instead of doing it in the controller constructor */ + qRegisterMetaType("int"); + qRegisterMetaType("double"); +} diff --git a/Controller.h b/Controller.h new file mode 100644 index 0000000..c0ed2b3 --- /dev/null +++ b/Controller.h @@ -0,0 +1,47 @@ +#include +#include +#include +#include +#include +#include +#include "MainWindow.h" + +/** + * Facade to control the application. + * + * TODO: implement it. + * TODO: include it in the project + * TODO: add signals + * TODO: the whole app should use actual QObject for properties of everything + */ +class Controller +{ + public: + Controller(MainWindow *owner); + // CRUD + template void registerObjType(const char* objType); + bool createObject(const char *objType, const char *objName); + bool listObjects(const char *objType, QList &objNames); + bool destroyObject(const char *objName); + //FIXME: The original prototype functions for these had QVariantList + // arguments. FOr now these 2 functions set/get a single QVariant for a + // single property, but we can change this easily. + bool setObjectProperty(const char* objName, const char* propName, + const QVariant &value); + bool getObjectProperty(const char *objName, const char *propName, + QVariant &value); + bool listObjectProperties(const char *objName, QList &names, + QVariantList &values); + bool saveProject(const char *fileName); + bool loadProject(const char *fileName); + bool quit(); + struct strCmp { + bool operator()( const char* s1, const char* s2 ) const { + return strcmp( s1, s2 ) < 0; + } + }; + private: + MainWindow *_owner; + std::map _mControllerTypes; + std::map _mControllerObjects; +}; diff --git a/libremapping.pro b/libremapping.pro index ffad944..d3b0141 100644 --- a/libremapping.pro +++ b/libremapping.pro @@ -1,7 +1,7 @@ CONFIG += qt debug TEMPLATE = app HEADERS = MainWindow.h Util.h MapperGLCanvas.h SourceGLCanvas.h DestinationGLCanvas.h Mapper.h Mapping.h Shape.h Paint.h MappingManager.h ProjectWriter.h NameAllocator.h ProjectReader.h -SOURCES = main.cpp MainWindow.cpp Util.cpp Mapper.cpp MapperGLCanvas.cpp SourceGLCanvas.cpp DestinationGLCanvas.cpp MappingManager.cpp ProjectWriter.cpp NameAllocator.cpp ProjectReader.cpp +SOURCES = main.cpp MainWindow.cpp Util.cpp Mapper.cpp MapperGLCanvas.cpp SourceGLCanvas.cpp DestinationGLCanvas.cpp MappingManager.cpp ProjectWriter.cpp NameAllocator.cpp ProjectReader.cpp Controller.cpp QT += gui opengl xml RESOURCES = libremapping.qrc