From 9d077d78f5f5773989e1241a49b5c9ddc3af4c07 Mon Sep 17 00:00:00 2001 From: Michael Lemaire Date: Wed, 19 Aug 2015 01:17:49 +0200 Subject: [PATCH] Added FloatPropertyBind class This allows to easily bind a Qml property to a FloatNode, synchronizing values both ways. --- src/definition/DiffManager.cpp | 11 +++++ .../modeler/quickapp/AtmosphereModeler.cpp | 24 +++-------- .../modeler/quickapp/AtmosphereModeler.h | 13 ++---- .../modeler/quickapp/FloatPropertyBind.cpp | 40 +++++++++++++++++++ .../modeler/quickapp/FloatPropertyBind.h | 37 +++++++++++++++++ .../modeler/quickapp/PropertyBind.cpp | 7 ++++ src/interface/modeler/quickapp/PropertyBind.h | 17 ++++++++ .../modeler/quickapp/WaterModeler.cpp | 22 +++------- src/interface/modeler/quickapp/WaterModeler.h | 12 ++---- .../modeler/quickapp/modeler_global.h | 2 + .../modeler/quickapp/qml/PanelWaterLevel.qml | 4 +- src/interface/modeler/quickapp/quickapp.pro | 6 ++- 12 files changed, 137 insertions(+), 58 deletions(-) create mode 100644 src/interface/modeler/quickapp/FloatPropertyBind.cpp create mode 100644 src/interface/modeler/quickapp/FloatPropertyBind.h create mode 100644 src/interface/modeler/quickapp/PropertyBind.cpp create mode 100644 src/interface/modeler/quickapp/PropertyBind.h diff --git a/src/definition/DiffManager.cpp b/src/definition/DiffManager.cpp index ea8ce25..9cf1c0c 100644 --- a/src/definition/DiffManager.cpp +++ b/src/definition/DiffManager.cpp @@ -55,6 +55,12 @@ void DiffManager::undo() // Obtain the node by path and reverse apply diff on it DefinitionNode *node = tree->findByPath(diff->getPath()); node->applyDiff(diff, true); + + for (auto watcher: watchers[node]) + { + // FIXME Reverse diff + watcher->nodeChanged(node, diff); + } } } @@ -68,6 +74,11 @@ void DiffManager::redo() // Obtain the node by path and re-apply diff on it DefinitionNode *node = tree->findByPath(diff->getPath()); node->applyDiff(diff); + + for (auto watcher: watchers[node]) + { + watcher->nodeChanged(node, diff); + } } } diff --git a/src/interface/modeler/quickapp/AtmosphereModeler.cpp b/src/interface/modeler/quickapp/AtmosphereModeler.cpp index 091171a..a5ec004 100644 --- a/src/interface/modeler/quickapp/AtmosphereModeler.cpp +++ b/src/interface/modeler/quickapp/AtmosphereModeler.cpp @@ -3,28 +3,14 @@ #include "MainModelerWindow.h" #include "Scenery.h" #include "AtmosphereDefinition.h" -#include "OpenGLRenderer.h" -#include "OpenGLSkybox.h" -#include "OpenGLTerrain.h" -#include "FloatNode.h" +#include "FloatPropertyBind.h" -AtmosphereModeler::AtmosphereModeler(MainModelerWindow *main): - main(main) +AtmosphereModeler::AtmosphereModeler(MainModelerWindow *main) { - QObject *item = main->findQmlObject("atmosphere_daytime"); - if (item) - { - item->setProperty("value", propDayTime()->getValue()); - connect(item, SIGNAL(changed(double)), this, SLOT(daytimeChanged(double))); - } + prop_daytime = new FloatPropertyBind(main, "atmosphere_daytime", "value", main->getScenery()->getAtmosphere()->propDayTime()); } -void AtmosphereModeler::daytimeChanged(double value) +AtmosphereModeler::~AtmosphereModeler() { - propDayTime()->setValue(value); -} - -FloatNode *AtmosphereModeler::propDayTime() const -{ - return main->getScenery()->getAtmosphere()->propDayTime(); + delete prop_daytime; } diff --git a/src/interface/modeler/quickapp/AtmosphereModeler.h b/src/interface/modeler/quickapp/AtmosphereModeler.h index 25d6aa6..3881d40 100644 --- a/src/interface/modeler/quickapp/AtmosphereModeler.h +++ b/src/interface/modeler/quickapp/AtmosphereModeler.h @@ -3,24 +3,17 @@ #include "modeler_global.h" -#include - namespace paysages { namespace modeler { -class AtmosphereModeler : public QObject +class AtmosphereModeler { - Q_OBJECT public: AtmosphereModeler(MainModelerWindow *main); - -public slots: - void daytimeChanged(double value); + ~AtmosphereModeler(); private: - FloatNode *propDayTime() const; - - MainModelerWindow *main; + FloatPropertyBind *prop_daytime; }; } diff --git a/src/interface/modeler/quickapp/FloatPropertyBind.cpp b/src/interface/modeler/quickapp/FloatPropertyBind.cpp new file mode 100644 index 0000000..bc7d572 --- /dev/null +++ b/src/interface/modeler/quickapp/FloatPropertyBind.cpp @@ -0,0 +1,40 @@ +#include "FloatPropertyBind.h" + +#include "MainModelerWindow.h" +#include "FloatNode.h" +#include "Logs.h" + +#include +#include + +FloatPropertyBind::FloatPropertyBind(MainModelerWindow *window, const QString &object_name, const QString &property_name, FloatNode *node): + QObject(window), node(node), property(property_name) +{ + item = window->findQmlObject(object_name); + if (item) + { + node->addWatcher(this, true); + connect(item, SIGNAL(changed(double)), this, SLOT(propertyChanged(double))); + } + else + { + item = NULL; + Logs::error() << "Can't find object :" << object_name.toStdString() << std::endl; + } +} + +void FloatPropertyBind::nodeChanged(const DefinitionNode *, const DefinitionDiff *) +{ + if (item) + { + item->setProperty(property.toLocal8Bit(), node->getValue()); + } +} + +void FloatPropertyBind::propertyChanged(double value) +{ + if (fabs(value - node->getValue()) > 0.00000001) + { + node->setValue(value); + } +} diff --git a/src/interface/modeler/quickapp/FloatPropertyBind.h b/src/interface/modeler/quickapp/FloatPropertyBind.h new file mode 100644 index 0000000..10c5420 --- /dev/null +++ b/src/interface/modeler/quickapp/FloatPropertyBind.h @@ -0,0 +1,37 @@ +#ifndef FLOATPROPERTYBIND_H +#define FLOATPROPERTYBIND_H + +#include "modeler_global.h" + +#include +#include "DefinitionWatcher.h" + +namespace paysages { +namespace modeler { + +/** + * Bind a float Qml property to a FloatNode. + * + * The FloatNode must exist through this object lifetime. + */ +class FloatPropertyBind : public QObject, public DefinitionWatcher +{ + Q_OBJECT +public: + FloatPropertyBind(MainModelerWindow *window, const QString &object_name, const QString &property_name, FloatNode *node); + + virtual void nodeChanged(const DefinitionNode *node, const DefinitionDiff *diff) override; + +private slots: + void propertyChanged(double value); + +private: + FloatNode *node; + QString property; + QObject *item; +}; + +} +} + +#endif // FLOATPROPERTYBIND_H diff --git a/src/interface/modeler/quickapp/PropertyBind.cpp b/src/interface/modeler/quickapp/PropertyBind.cpp new file mode 100644 index 0000000..6853613 --- /dev/null +++ b/src/interface/modeler/quickapp/PropertyBind.cpp @@ -0,0 +1,7 @@ +#include "PropertyBind.h" + +PropertyBind::PropertyBind(QObject *parent) : QObject(parent) +{ + +} + diff --git a/src/interface/modeler/quickapp/PropertyBind.h b/src/interface/modeler/quickapp/PropertyBind.h new file mode 100644 index 0000000..e06a560 --- /dev/null +++ b/src/interface/modeler/quickapp/PropertyBind.h @@ -0,0 +1,17 @@ +#ifndef PROPERTYBIND_H +#define PROPERTYBIND_H + +#include + +class PropertyBind : public QObject +{ + Q_OBJECT +public: + explicit PropertyBind(QObject *parent = 0); + +signals: + +public slots: +}; + +#endif // PROPERTYBIND_H diff --git a/src/interface/modeler/quickapp/WaterModeler.cpp b/src/interface/modeler/quickapp/WaterModeler.cpp index 4a497a0..f5a1af9 100644 --- a/src/interface/modeler/quickapp/WaterModeler.cpp +++ b/src/interface/modeler/quickapp/WaterModeler.cpp @@ -3,26 +3,14 @@ #include "MainModelerWindow.h" #include "Scenery.h" #include "TerrainDefinition.h" -#include "FloatNode.h" -#include "Logs.h" +#include "FloatPropertyBind.h" -WaterModeler::WaterModeler(MainModelerWindow *main): - main(main) +WaterModeler::WaterModeler(MainModelerWindow *main) { - QObject *item = main->findQmlObject("water_level"); - if (item) - { - item->setProperty("value", propWaterHeight()->getValue() * 0.5 + 0.5); - connect(item, SIGNAL(changed(double)), this, SLOT(waterLevelChanged(double))); - } + prop_water_height = new FloatPropertyBind(main, "water_height", "value", main->getScenery()->getTerrain()->propWaterHeight()); } -void WaterModeler::waterLevelChanged(double value) +WaterModeler::~WaterModeler() { - propWaterHeight()->setValue(value * 2.0 - 1.0); -} - -FloatNode *WaterModeler::propWaterHeight() const -{ - return main->getScenery()->getTerrain()->propWaterHeight(); + delete prop_water_height; } diff --git a/src/interface/modeler/quickapp/WaterModeler.h b/src/interface/modeler/quickapp/WaterModeler.h index ceb2b10..b2b169e 100644 --- a/src/interface/modeler/quickapp/WaterModeler.h +++ b/src/interface/modeler/quickapp/WaterModeler.h @@ -3,23 +3,17 @@ #include "modeler_global.h" -#include - namespace paysages { namespace modeler { -class WaterModeler: public QObject +class WaterModeler { - Q_OBJECT public: WaterModeler(MainModelerWindow *main); - -public slots: - void waterLevelChanged(double value); + ~WaterModeler(); private: - FloatNode *propWaterHeight() const; - MainModelerWindow *main; + FloatPropertyBind *prop_water_height; }; } diff --git a/src/interface/modeler/quickapp/modeler_global.h b/src/interface/modeler/quickapp/modeler_global.h index 135e12b..717e7c5 100644 --- a/src/interface/modeler/quickapp/modeler_global.h +++ b/src/interface/modeler/quickapp/modeler_global.h @@ -17,6 +17,8 @@ namespace modeler { class RenderProcess; class ModelerCameras; + + class FloatPropertyBind; } } diff --git a/src/interface/modeler/quickapp/qml/PanelWaterLevel.qml b/src/interface/modeler/quickapp/qml/PanelWaterLevel.qml index 444ae97..90d4be0 100644 --- a/src/interface/modeler/quickapp/qml/PanelWaterLevel.qml +++ b/src/interface/modeler/quickapp/qml/PanelWaterLevel.qml @@ -4,7 +4,9 @@ BasePanel { width: 20 BaseSlider { - objectName: "water_level" + objectName: "water_height" + minimumValue: -1 + maximumValue: 1 orientation: Qt.Vertical anchors.fill: parent } diff --git a/src/interface/modeler/quickapp/quickapp.pro b/src/interface/modeler/quickapp/quickapp.pro index 9257aecc..132e58e 100644 --- a/src/interface/modeler/quickapp/quickapp.pro +++ b/src/interface/modeler/quickapp/quickapp.pro @@ -11,7 +11,8 @@ SOURCES += main.cpp \ AtmosphereModeler.cpp \ RenderPreviewProvider.cpp \ RenderProcess.cpp \ - ModelerCameras.cpp + ModelerCameras.cpp \ + FloatPropertyBind.cpp RESOURCES += \ qml/app.qrc @@ -32,7 +33,8 @@ HEADERS += \ AtmosphereModeler.h \ RenderPreviewProvider.h \ RenderProcess.h \ - ModelerCameras.h + ModelerCameras.h \ + FloatPropertyBind.h win32:CONFIG(release, debug|release): LIBS += -L$$OUT_PWD/../../../system/release/ -lpaysages_system else:win32:CONFIG(debug, debug|release): LIBS += -L$$OUT_PWD/../../../system/debug/ -lpaysages_system