Added FloatPropertyBind class

This allows to easily bind a Qml property to a FloatNode,
synchronizing values both ways.
This commit is contained in:
Michaël Lemaire 2015-08-19 01:17:49 +02:00
parent 9a177352ba
commit 9d077d78f5
12 changed files with 137 additions and 58 deletions

View file

@ -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);
}
}
}

View file

@ -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;
}

View file

@ -3,24 +3,17 @@
#include "modeler_global.h"
#include <QObject>
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;
};
}

View file

@ -0,0 +1,40 @@
#include "FloatPropertyBind.h"
#include "MainModelerWindow.h"
#include "FloatNode.h"
#include "Logs.h"
#include <cassert>
#include <cmath>
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);
}
}

View file

@ -0,0 +1,37 @@
#ifndef FLOATPROPERTYBIND_H
#define FLOATPROPERTYBIND_H
#include "modeler_global.h"
#include <QObject>
#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

View file

@ -0,0 +1,7 @@
#include "PropertyBind.h"
PropertyBind::PropertyBind(QObject *parent) : QObject(parent)
{
}

View file

@ -0,0 +1,17 @@
#ifndef PROPERTYBIND_H
#define PROPERTYBIND_H
#include <QObject>
class PropertyBind : public QObject
{
Q_OBJECT
public:
explicit PropertyBind(QObject *parent = 0);
signals:
public slots:
};
#endif // PROPERTYBIND_H

View file

@ -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;
}

View file

@ -3,23 +3,17 @@
#include "modeler_global.h"
#include <QObject>
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;
};
}

View file

@ -17,6 +17,8 @@ namespace modeler {
class RenderProcess;
class ModelerCameras;
class FloatPropertyBind;
}
}

View file

@ -4,7 +4,9 @@ BasePanel {
width: 20
BaseSlider {
objectName: "water_level"
objectName: "water_height"
minimumValue: -1
maximumValue: 1
orientation: Qt.Vertical
anchors.fill: parent
}

View file

@ -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