Commit 7b8a4d36 authored by Christian Schulte zu Berge's avatar Christian Schulte zu Berge
Browse files

Improved Lua-based persistence API. Written files are now more clean

* Added default values to GenericProperty<T>
* Lua export now checks properties for default values and only sets the property if its value is different from the default value.
parent 1c5e0eae
...@@ -319,7 +319,7 @@ namespace campvis { ...@@ -319,7 +319,7 @@ namespace campvis {
#ifdef CAMPVIS_HAS_SCRIPTING #ifdef CAMPVIS_HAS_SCRIPTING
const QString dialogCaption = QString::fromStdString("Select File"); const QString dialogCaption = QString::fromStdString("Select File");
const QString directory = QString::fromStdString("."); const QString directory = QString::fromStdString(".");
const QString fileFilter = tr("All files (*)"); const QString fileFilter = tr("Lua Scripts (*.lua)");
QString filename = QFileDialog::getOpenFileName(QWidget::parentWidget(), dialogCaption, directory, fileFilter); QString filename = QFileDialog::getOpenFileName(QWidget::parentWidget(), dialogCaption, directory, fileFilter);
if (filename != nullptr && _application->getLuaVmState() != nullptr) { if (filename != nullptr && _application->getLuaVmState() != nullptr) {
...@@ -332,31 +332,29 @@ namespace campvis { ...@@ -332,31 +332,29 @@ namespace campvis {
#ifdef CAMPVIS_HAS_SCRIPTING #ifdef CAMPVIS_HAS_SCRIPTING
const QString dialogCaption = QString::fromStdString("Save File as"); const QString dialogCaption = QString::fromStdString("Save File as");
const QString directory = QString::fromStdString("."); const QString directory = QString::fromStdString(".");
const QString fileFilter = tr("All files (*)"); const QString fileFilter = tr("Lua Scripts (*.lua)");
QString filename = QFileDialog::getSaveFileName(QWidget::parentWidget(), dialogCaption, directory, fileFilter); QString filename = QFileDialog::getSaveFileName(QWidget::parentWidget(), dialogCaption, directory, fileFilter);
if (filename != nullptr) { if (filename != nullptr) {
if (_selectedProcessor != 0 && _selectedPipeline != 0) { if (_selectedProcessor != 0 && _selectedPipeline != 0) {
PropertyCollectionLuaScriptGenerator* _pcLua = new PropertyCollectionLuaScriptGenerator(); PropertyCollectionLuaScriptGenerator pcLua;
std::string pipeScript = "pipeline = pipelines[\"" + _selectedPipeline->getName()+"\"]\n\n"; std::string pipeScript = "pipeline = pipelines[\"" + _selectedPipeline->getName()+"\"]\n";
for (size_t i = 0; i < _selectedPipeline->getProcessors().size(); i++) { for (size_t i = 0; i < _selectedPipeline->getProcessors().size(); i++) {
pipeScript += "\n-- Restoring property state for " + _selectedPipeline->getProcessor(i)->getName() + " processor\n";
pipeScript += "proc = pipeline:getProcessor(" + StringUtils::toString(i) + ")\n"; pipeScript += "proc = pipeline:getProcessor(" + StringUtils::toString(i) + ")\n";
AbstractProcessor* proc = _selectedPipeline->getProcessor(int(i)); AbstractProcessor* proc = _selectedPipeline->getProcessor(i);
_pcLua->updatePropCollection(proc, &_selectedPipeline->getDataContainer()); pcLua.updatePropCollection(proc, &_selectedPipeline->getDataContainer());
std::string res = _pcLua->getLuaScript(std::string(""), std::string("proc:")); std::string res = pcLua.getLuaScript(std::string(""), std::string("proc:"));
pipeScript += res; pipeScript += res;
} }
if (pipeScript != "pipeline = pipelines[\"" + _selectedPipeline->getName()+"\"]\n\n") {
std::ofstream file; std::ofstream file;
file.open(filename.toStdString()); file.open(filename.toStdString(), std::ofstream::out |std::ofstream::trunc);
file << pipeScript.c_str(); file << pipeScript.c_str();
file.close(); file.close();
}
delete _pcLua;
} }
} }
#endif #endif
......
...@@ -465,7 +465,7 @@ namespace campvis { ...@@ -465,7 +465,7 @@ namespace campvis {
virtual void addProcessor(AbstractProcessor* processor); virtual void addProcessor(AbstractProcessor* processor);
virtual void executePipeline() = 0; virtual void executePipeline() = 0;
AbstractProcessor* getProcessor(const std::string& name) const; AbstractProcessor* getProcessor(const std::string& name) const;
AbstractProcessor* getProcessor(int index) const; AbstractProcessor* getProcessor(size_t index) const;
sigslot::signal0 s_init; sigslot::signal0 s_init;
sigslot::signal0 s_deinit; sigslot::signal0 s_deinit;
......
...@@ -287,8 +287,8 @@ namespace campvis { ...@@ -287,8 +287,8 @@ namespace campvis {
return nullptr; return nullptr;
} }
AbstractProcessor* AbstractPipeline::getProcessor(int index ) const { AbstractProcessor* AbstractPipeline::getProcessor(size_t index) const {
if (index < 0 || (size_t) index >= _processors.size()) if (index >= _processors.size())
return nullptr; return nullptr;
return _processors[index]; return _processors[index];
} }
......
...@@ -171,7 +171,7 @@ namespace campvis { ...@@ -171,7 +171,7 @@ namespace campvis {
* \param index The index of the processor to get * \param index The index of the processor to get
* \return The first processor whose name matches \a name, 0 if no such processor exists. * \return The first processor whose name matches \a name, 0 if no such processor exists.
*/ */
AbstractProcessor* getProcessor(int index) const; AbstractProcessor* getProcessor(size_t index) const;
/** /**
* Gets the flag whether this pipeline is currently enabled. * Gets the flag whether this pipeline is currently enabled.
......
...@@ -23,6 +23,7 @@ ...@@ -23,6 +23,7 @@
// ================================================================================================ // ================================================================================================
#include "abstractproperty.h" #include "abstractproperty.h"
#include "cgt/logmanager.h"
namespace campvis { namespace campvis {
...@@ -49,12 +50,16 @@ namespace campvis { ...@@ -49,12 +50,16 @@ namespace campvis {
} }
void AbstractProperty::addSharedProperty(AbstractProperty* prop) { void AbstractProperty::addSharedProperty(AbstractProperty* prop) {
cgtAssert(prop != 0, "Shared property must not be 0!"); cgtAssert(prop != nullptr, "Shared property must not be 0!");
cgtAssert(prop != this, "Shared property must not be this!"); cgtAssert(prop != this, "Shared property must not be this!");
cgtAssert(typeid(this) == typeid(prop), "Shared property must be of the same type as this property.");
tbb::spin_mutex::scoped_lock lock(_localMutex); if (typeid(this) == typeid(prop)) {
_sharedProperties.insert(prop); tbb::spin_mutex::scoped_lock lock(_localMutex);
_sharedProperties.insert(prop);
}
else {
LERROR("Could not add " << prop->getName() << " as shared property of " << getName() << " since they are not of the same type.");
}
} }
void AbstractProperty::removeSharedProperty(AbstractProperty* prop) { void AbstractProperty::removeSharedProperty(AbstractProperty* prop) {
......
...@@ -28,7 +28,6 @@ ...@@ -28,7 +28,6 @@
#include "sigslot/sigslot.h" #include "sigslot/sigslot.h"
#include <tbb/atomic.h> #include <tbb/atomic.h>
#include <tbb/spin_mutex.h> #include <tbb/spin_mutex.h>
#include "cgt/logmanager.h"
#include "core/coreapi.h" #include "core/coreapi.h"
#include "core/pipeline/abstractprocessor.h" #include "core/pipeline/abstractprocessor.h"
...@@ -94,7 +93,6 @@ namespace campvis { ...@@ -94,7 +93,6 @@ namespace campvis {
/** /**
* Adds the given property \a prop to the set of shared properties. * Adds the given property \a prop to the set of shared properties.
* All shared properties will be changed when this property changes. * All shared properties will be changed when this property changes.
* Overload in subclasses to make sure that shared properties are of the same type.
* \note Make sure not to build circular sharing or you will encounter endless loops! * \note Make sure not to build circular sharing or you will encounter endless loops!
* \param prop Property to add. * \param prop Property to add.
*/ */
......
...@@ -59,8 +59,10 @@ namespace campvis { ...@@ -59,8 +59,10 @@ namespace campvis {
/** /**
* Adds the given property \a prop to the set of shared properties. * Adds the given property \a prop to the set of shared properties.
* All shared properties will be changed when this property changes. * All shared properties will be changed when this property changes.
*
* \note Make sure not to build circular sharing or you will encounter endless loops! * \note Make sure not to build circular sharing or you will encounter endless loops!
* \param prop Property to add, must be of the same type as this property. * \param prop Property to add, must be of the same type as this property.
* \return true if \a prop was added successfully, false otherwise (e.g. if types do not match).
*/ */
virtual void addSharedProperty(AbstractProperty* prop); virtual void addSharedProperty(AbstractProperty* prop);
...@@ -79,6 +81,17 @@ namespace campvis { ...@@ -79,6 +81,17 @@ namespace campvis {
virtual void setValue(const T& value); virtual void setValue(const T& value);
/**
* Returns the default value of this property as it was set during construction.
* \return _defaultValue
*/
const T& getDefaultValue() const;
/**
* Resets this property to the default value of this property as it was set during construction.
*/
void resetToDefaultValue();
/** /**
* Unlocks the property. If the back buffer has changed, the changes will be written to the front * Unlocks the property. If the back buffer has changed, the changes will be written to the front
* buffer and all observers will be notified. * buffer and all observers will be notified.
...@@ -110,7 +123,8 @@ namespace campvis { ...@@ -110,7 +123,8 @@ namespace campvis {
*/ */
void setBackValue(const T& value); void setBackValue(const T& value);
const T _defaultValue; ///< default value of the property as set during construction
T _value; ///< value of the property T _value; ///< value of the property
T _backBuffer; ///< back buffer for values when property is in use T _backBuffer; ///< back buffer for values when property is in use
...@@ -126,6 +140,7 @@ namespace campvis { ...@@ -126,6 +140,7 @@ namespace campvis {
template<typename T> template<typename T>
campvis::GenericProperty<T>::GenericProperty(const std::string& name, const std::string& title, const T& value) campvis::GenericProperty<T>::GenericProperty(const std::string& name, const std::string& title, const T& value)
: AbstractProperty(name, title) : AbstractProperty(name, title)
, _defaultValue(value)
, _value(value) , _value(value)
, _backBuffer(value) , _backBuffer(value)
{ {
...@@ -138,8 +153,9 @@ namespace campvis { ...@@ -138,8 +153,9 @@ namespace campvis {
template<typename T> template<typename T>
void campvis::GenericProperty<T>::addSharedProperty(AbstractProperty* prop) { void campvis::GenericProperty<T>::addSharedProperty(AbstractProperty* prop) {
// make type check first, then call base method.
cgtAssert(prop != 0, "Shared property must not be 0!"); cgtAssert(prop != 0, "Shared property must not be 0!");
// make type check first, then call base method.
if (GenericProperty<T>* tmp = dynamic_cast< GenericProperty<T>* >(prop)) { if (GenericProperty<T>* tmp = dynamic_cast< GenericProperty<T>* >(prop)) {
AbstractProperty::addSharedProperty(prop); AbstractProperty::addSharedProperty(prop);
tmp->setValue(getValue()); tmp->setValue(getValue());
...@@ -166,6 +182,16 @@ namespace campvis { ...@@ -166,6 +182,16 @@ namespace campvis {
} }
} }
template<typename T>
const T& campvis::GenericProperty<T>::getDefaultValue() const {
return _defaultValue;
}
template<typename T>
void campvis::GenericProperty<T>::resetToDefaultValue() {
setValue(_defaultValue);
}
template<typename T> template<typename T>
void campvis::GenericProperty<T>::unlock() { void campvis::GenericProperty<T>::unlock() {
tbb::spin_mutex::scoped_lock lock(_localMutex); tbb::spin_mutex::scoped_lock lock(_localMutex);
......
...@@ -36,9 +36,11 @@ namespace campvis { ...@@ -36,9 +36,11 @@ namespace campvis {
std::string BoolPropertyLua::getLuaScript(const std::string& propNamePrefix, const std::string& luaProc) { std::string BoolPropertyLua::getLuaScript(const std::string& propNamePrefix, const std::string& luaProc) {
std::string ret = ""; std::string ret = "";
ret += luaProc; if (static_cast<BoolProperty*>(_property)->getValue() != static_cast<BoolProperty*>(_property)->getDefaultValue()) {
ret += "getNestedProperty(\"" + propNamePrefix +_property->getName() + "\"):setValue(" ret += luaProc;
+ (static_cast<BoolProperty*>(_property)->getValue() == 1? "true" : "false") + ")"; ret += "getNestedProperty(\"" + propNamePrefix +_property->getName() + "\"):setValue("
+ (static_cast<BoolProperty*>(_property)->getValue() == 1? "true" : "false") + ")";
}
return ret; return ret;
} }
} }
\ No newline at end of file
...@@ -36,12 +36,14 @@ namespace campvis { ...@@ -36,12 +36,14 @@ namespace campvis {
} }
std::string ColorPropertyLua::getLuaScript(const std::string& propNamePrefix, const std::string& luaProc) { std::string ColorPropertyLua::getLuaScript(const std::string& propNamePrefix, const std::string& luaProc) {
cgt::vec4 value = static_cast<ColorProperty*>(_property)->getValue(); const cgt::vec4& value = static_cast<ColorProperty*>(_property)->getValue();
std::string ret = ""; std::string ret = "";
ret += luaProc; if (value != static_cast<ColorProperty*>(_property)->getDefaultValue()) {
ret += "getNestedProperty(\"" + propNamePrefix + _property->getName() + "\"):setValue(cgt.vec4(" ret += luaProc;
+ StringUtils::toString(value.x) +", " + StringUtils::toString(value.y) +", " ret += "getNestedProperty(\"" + propNamePrefix + _property->getName() + "\"):setValue(cgt.vec4("
+ StringUtils::toString(value.z) +", " + StringUtils::toString(value.w) + "))"; + StringUtils::toString(value.x) +", " + StringUtils::toString(value.y) +", "
+ StringUtils::toString(value.z) +", " + StringUtils::toString(value.w) + "))";
}
return ret; return ret;
} }
} }
...@@ -36,10 +36,12 @@ namespace campvis { ...@@ -36,10 +36,12 @@ namespace campvis {
DataNamePropertyLua::~DataNamePropertyLua() { DataNamePropertyLua::~DataNamePropertyLua() {
} }
std::string DataNamePropertyLua::getLuaScript(const std::string& propNamePrefix, const std::string& luaProc) { std::string DataNamePropertyLua::getLuaScript(const std::string& propNamePrefix, const std::string& luaProc) {
const std::string value = static_cast<DataNameProperty*>(_property)->getValue(); const std::string& value = static_cast<DataNameProperty*>(_property)->getValue();
std::string ret = ""; std::string ret = "";
ret += luaProc; if (value != static_cast<DataNameProperty*>(_property)->getDefaultValue()) {
ret += "getNestedProperty(\"" + propNamePrefix + _property->getName() + "\"):setValue(\"" + value + "\")"; ret += luaProc;
ret += "getNestedProperty(\"" + propNamePrefix + _property->getName() + "\"):setValue(\"" + value + "\")";
}
return ret; return ret;
} }
} }
...@@ -37,9 +37,11 @@ namespace campvis { ...@@ -37,9 +37,11 @@ namespace campvis {
std::string FloatPropertyLua::getLuaScript(const std::string& propNamePrefix, const std::string& luaProc) { std::string FloatPropertyLua::getLuaScript(const std::string& propNamePrefix, const std::string& luaProc) {
std::string ret = ""; std::string ret = "";
ret += luaProc; if (static_cast<FloatProperty*>(_property)->getValue() != static_cast<FloatProperty*>(_property)->getDefaultValue()) {
ret += "getNestedProperty(\"" + propNamePrefix + _property->getName() + "\"):setValue(" ret += luaProc;
+ StringUtils::toString( static_cast<FloatProperty*>(_property)->getValue() ) + ")"; ret += "getNestedProperty(\"" + propNamePrefix + _property->getName() + "\"):setValue("
+ StringUtils::toString( static_cast<FloatProperty*>(_property)->getValue() ) + ")";
}
return ret; return ret;
} }
} }
...@@ -36,9 +36,11 @@ namespace campvis { ...@@ -36,9 +36,11 @@ namespace campvis {
std::string IntPropertyLua::getLuaScript(const std::string& propNamePrefix, const std::string& luaProc) { std::string IntPropertyLua::getLuaScript(const std::string& propNamePrefix, const std::string& luaProc) {
std::string ret = ""; std::string ret = "";
ret += luaProc; if (static_cast<IntProperty*>(_property)->getValue() != static_cast<IntProperty*>(_property)->getDefaultValue()) {
ret += "getNestedProperty(\"" + propNamePrefix + _property->getName() + "\"):setValue(" ret += luaProc;
+ StringUtils::toString( static_cast<IntProperty*>(_property)->getValue() ) + ")"; ret += "getNestedProperty(\"" + propNamePrefix + _property->getName() + "\"):setValue("
+ StringUtils::toString( static_cast<IntProperty*>(_property)->getValue() ) + ")";
}
return ret; return ret;
} }
......
...@@ -37,9 +37,11 @@ namespace campvis { ...@@ -37,9 +37,11 @@ namespace campvis {
std::string OptionPropertyLua::getLuaScript(const std::string& propNamePrefix, const std::string& luaProc) { std::string OptionPropertyLua::getLuaScript(const std::string& propNamePrefix, const std::string& luaProc) {
std::string ret = ""; std::string ret = "";
ret += luaProc; if (static_cast<AbstractOptionProperty*>(_property)->getValue() != static_cast<AbstractOptionProperty*>(_property)->getDefaultValue()) {
ret += "getNestedProperty(\"" + propNamePrefix + _property->getName() + "\"):selectById(\"" ret += luaProc;
+ StringUtils::toString(static_cast<AbstractOptionProperty*>(_property)->getOptionId() ) + "\")"; ret += "getNestedProperty(\"" + propNamePrefix + _property->getName() + "\"):selectById(\""
+ StringUtils::toString(static_cast<AbstractOptionProperty*>(_property)->getOptionId() ) + "\")";
}
return ret; return ret;
} }
......
...@@ -67,7 +67,9 @@ namespace campvis { ...@@ -67,7 +67,9 @@ namespace campvis {
std::string PropertyCollectionLuaScriptGenerator::getLuaScript(const std::string& propNamePrefix, const std::string& luaProc) { std::string PropertyCollectionLuaScriptGenerator::getLuaScript(const std::string& propNamePrefix, const std::string& luaProc) {
std::string ret = ""; std::string ret = "";
for (std::map<AbstractProperty*, AbstractPropertyLua*>::iterator it = _luaMap.begin(); it != _luaMap.end(); ++it) { for (std::map<AbstractProperty*, AbstractPropertyLua*>::iterator it = _luaMap.begin(); it != _luaMap.end(); ++it) {
ret += it->second->getLuaScript(propNamePrefix, luaProc) + "\n"; const std::string line = it->second->getLuaScript(propNamePrefix, luaProc);
if (!line.empty())
ret += line + "\n";
} }
return ret; return ret;
} }
......
...@@ -39,8 +39,10 @@ namespace campvis { ...@@ -39,8 +39,10 @@ namespace campvis {
std::string StringPropertyLua::getLuaScript(const std::string& propNamePrefix, const std::string& luaProc) { std::string StringPropertyLua::getLuaScript(const std::string& propNamePrefix, const std::string& luaProc) {
std::string ret = ""; std::string ret = "";
ret += luaProc; if (static_cast<StringProperty*>(_property)->getValue() != static_cast<StringProperty*>(_property)->getDefaultValue()) {
ret += "getNestedProperty(\"" + propNamePrefix + _property->getName() + "\"):setValue(\"" + static_cast<StringProperty*>(_property)->getValue() + "\")"; ret += luaProc;
ret += "getNestedProperty(\"" + propNamePrefix + _property->getName() + "\"):setValue(\"" + static_cast<StringProperty*>(_property)->getValue() + "\")";
}
return ret; return ret;
} }
} }
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment