10.12., 9:00 - 11:00: Due to updates GitLab may be unavailable for some minutes between 09:00 and 11:00.

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 {
#ifdef CAMPVIS_HAS_SCRIPTING
const QString dialogCaption = QString::fromStdString("Select File");
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);
if (filename != nullptr && _application->getLuaVmState() != nullptr) {
......@@ -332,31 +332,29 @@ namespace campvis {
#ifdef CAMPVIS_HAS_SCRIPTING
const QString dialogCaption = QString::fromStdString("Save File as");
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);
if (filename != nullptr) {
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++) {
pipeScript += "\n-- Restoring property state for " + _selectedPipeline->getProcessor(i)->getName() + " processor\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());
std::string res = _pcLua->getLuaScript(std::string(""), std::string("proc:"));
pcLua.updatePropCollection(proc, &_selectedPipeline->getDataContainer());
std::string res = pcLua.getLuaScript(std::string(""), std::string("proc:"));
pipeScript += res;
}
if (pipeScript != "pipeline = pipelines[\"" + _selectedPipeline->getName()+"\"]\n\n") {
std::ofstream file;
file.open(filename.toStdString());
file << pipeScript.c_str();
file.close();
}
delete _pcLua;
std::ofstream file;
file.open(filename.toStdString(), std::ofstream::out |std::ofstream::trunc);
file << pipeScript.c_str();
file.close();
}
}
#endif
......
......@@ -465,7 +465,7 @@ namespace campvis {
virtual void addProcessor(AbstractProcessor* processor);
virtual void executePipeline() = 0;
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_deinit;
......
......@@ -287,8 +287,8 @@ namespace campvis {
return nullptr;
}
AbstractProcessor* AbstractPipeline::getProcessor(int index ) const {
if (index < 0 || (size_t) index >= _processors.size())
AbstractProcessor* AbstractPipeline::getProcessor(size_t index) const {
if (index >= _processors.size())
return nullptr;
return _processors[index];
}
......
......@@ -171,7 +171,7 @@ namespace campvis {
* \param index The index of the processor to get
* \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.
......
......@@ -23,6 +23,7 @@
// ================================================================================================
#include "abstractproperty.h"
#include "cgt/logmanager.h"
namespace campvis {
......@@ -49,12 +50,16 @@ namespace campvis {
}
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(typeid(this) == typeid(prop), "Shared property must be of the same type as this property.");
tbb::spin_mutex::scoped_lock lock(_localMutex);
_sharedProperties.insert(prop);
if (typeid(this) == typeid(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) {
......
......@@ -28,7 +28,6 @@
#include "sigslot/sigslot.h"
#include <tbb/atomic.h>
#include <tbb/spin_mutex.h>
#include "cgt/logmanager.h"
#include "core/coreapi.h"
#include "core/pipeline/abstractprocessor.h"
......@@ -94,7 +93,6 @@ namespace campvis {
/**
* Adds the given property \a prop to the set of shared properties.
* 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!
* \param prop Property to add.
*/
......
......@@ -59,8 +59,10 @@ namespace campvis {
/**
* Adds the given property \a prop to the set of shared properties.
* All shared properties will be changed when this property changes.
*
* \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.
* \return true if \a prop was added successfully, false otherwise (e.g. if types do not match).
*/
virtual void addSharedProperty(AbstractProperty* prop);
......@@ -79,6 +81,17 @@ namespace campvis {
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
* buffer and all observers will be notified.
......@@ -110,7 +123,8 @@ namespace campvis {
*/
void setBackValue(const T& value);
const T _defaultValue; ///< default value of the property as set during construction
T _value; ///< value of the property
T _backBuffer; ///< back buffer for values when property is in use
......@@ -126,6 +140,7 @@ namespace campvis {
template<typename T>
campvis::GenericProperty<T>::GenericProperty(const std::string& name, const std::string& title, const T& value)
: AbstractProperty(name, title)
, _defaultValue(value)
, _value(value)
, _backBuffer(value)
{
......@@ -138,8 +153,9 @@ namespace campvis {
template<typename T>
void campvis::GenericProperty<T>::addSharedProperty(AbstractProperty* prop) {
// make type check first, then call base method.
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)) {
AbstractProperty::addSharedProperty(prop);
tmp->setValue(getValue());
......@@ -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>
void campvis::GenericProperty<T>::unlock() {
tbb::spin_mutex::scoped_lock lock(_localMutex);
......
......@@ -36,9 +36,11 @@ namespace campvis {
std::string BoolPropertyLua::getLuaScript(const std::string& propNamePrefix, const std::string& luaProc) {
std::string ret = "";
ret += luaProc;
ret += "getNestedProperty(\"" + propNamePrefix +_property->getName() + "\"):setValue("
+ (static_cast<BoolProperty*>(_property)->getValue() == 1? "true" : "false") + ")";
if (static_cast<BoolProperty*>(_property)->getValue() != static_cast<BoolProperty*>(_property)->getDefaultValue()) {
ret += luaProc;
ret += "getNestedProperty(\"" + propNamePrefix +_property->getName() + "\"):setValue("
+ (static_cast<BoolProperty*>(_property)->getValue() == 1? "true" : "false") + ")";
}
return ret;
}
}
\ No newline at end of file
......@@ -36,12 +36,14 @@ namespace campvis {
}
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 = "";
ret += luaProc;
ret += "getNestedProperty(\"" + propNamePrefix + _property->getName() + "\"):setValue(cgt.vec4("
+ StringUtils::toString(value.x) +", " + StringUtils::toString(value.y) +", "
+ StringUtils::toString(value.z) +", " + StringUtils::toString(value.w) + "))";
if (value != static_cast<ColorProperty*>(_property)->getDefaultValue()) {
ret += luaProc;
ret += "getNestedProperty(\"" + propNamePrefix + _property->getName() + "\"):setValue(cgt.vec4("
+ StringUtils::toString(value.x) +", " + StringUtils::toString(value.y) +", "
+ StringUtils::toString(value.z) +", " + StringUtils::toString(value.w) + "))";
}
return ret;
}
}
......@@ -36,10 +36,12 @@ namespace campvis {
DataNamePropertyLua::~DataNamePropertyLua() {
}
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 = "";
ret += luaProc;
ret += "getNestedProperty(\"" + propNamePrefix + _property->getName() + "\"):setValue(\"" + value + "\")";
if (value != static_cast<DataNameProperty*>(_property)->getDefaultValue()) {
ret += luaProc;
ret += "getNestedProperty(\"" + propNamePrefix + _property->getName() + "\"):setValue(\"" + value + "\")";
}
return ret;
}
}
......@@ -37,9 +37,11 @@ namespace campvis {
std::string FloatPropertyLua::getLuaScript(const std::string& propNamePrefix, const std::string& luaProc) {
std::string ret = "";
ret += luaProc;
ret += "getNestedProperty(\"" + propNamePrefix + _property->getName() + "\"):setValue("
+ StringUtils::toString( static_cast<FloatProperty*>(_property)->getValue() ) + ")";
if (static_cast<FloatProperty*>(_property)->getValue() != static_cast<FloatProperty*>(_property)->getDefaultValue()) {
ret += luaProc;
ret += "getNestedProperty(\"" + propNamePrefix + _property->getName() + "\"):setValue("
+ StringUtils::toString( static_cast<FloatProperty*>(_property)->getValue() ) + ")";
}
return ret;
}
}
......@@ -36,9 +36,11 @@ namespace campvis {
std::string IntPropertyLua::getLuaScript(const std::string& propNamePrefix, const std::string& luaProc) {
std::string ret = "";
ret += luaProc;
ret += "getNestedProperty(\"" + propNamePrefix + _property->getName() + "\"):setValue("
+ StringUtils::toString( static_cast<IntProperty*>(_property)->getValue() ) + ")";
if (static_cast<IntProperty*>(_property)->getValue() != static_cast<IntProperty*>(_property)->getDefaultValue()) {
ret += luaProc;
ret += "getNestedProperty(\"" + propNamePrefix + _property->getName() + "\"):setValue("
+ StringUtils::toString( static_cast<IntProperty*>(_property)->getValue() ) + ")";
}
return ret;
}
......
......@@ -37,9 +37,11 @@ namespace campvis {
std::string OptionPropertyLua::getLuaScript(const std::string& propNamePrefix, const std::string& luaProc) {
std::string ret = "";
ret += luaProc;
ret += "getNestedProperty(\"" + propNamePrefix + _property->getName() + "\"):selectById(\""
+ StringUtils::toString(static_cast<AbstractOptionProperty*>(_property)->getOptionId() ) + "\")";
if (static_cast<AbstractOptionProperty*>(_property)->getValue() != static_cast<AbstractOptionProperty*>(_property)->getDefaultValue()) {
ret += luaProc;
ret += "getNestedProperty(\"" + propNamePrefix + _property->getName() + "\"):selectById(\""
+ StringUtils::toString(static_cast<AbstractOptionProperty*>(_property)->getOptionId() ) + "\")";
}
return ret;
}
......
......@@ -67,7 +67,9 @@ namespace campvis {
std::string PropertyCollectionLuaScriptGenerator::getLuaScript(const std::string& propNamePrefix, const std::string& luaProc) {
std::string ret = "";
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;
}
......
......@@ -39,8 +39,10 @@ namespace campvis {
std::string StringPropertyLua::getLuaScript(const std::string& propNamePrefix, const std::string& luaProc) {
std::string ret = "";
ret += luaProc;
ret += "getNestedProperty(\"" + propNamePrefix + _property->getName() + "\"):setValue(\"" + static_cast<StringProperty*>(_property)->getValue() + "\")";
if (static_cast<StringProperty*>(_property)->getValue() != static_cast<StringProperty*>(_property)->getDefaultValue()) {
ret += luaProc;
ret += "getNestedProperty(\"" + propNamePrefix + _property->getName() + "\"):setValue(\"" + static_cast<StringProperty*>(_property)->getValue() + "\")";
}
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