From ee146ad3ae57320a2654eb7ffbc8d9cbbdaf32b5 Mon Sep 17 00:00:00 2001 From: Guido Ranzuglia granzuglia Date: Mon, 21 Mar 2011 16:54:16 +0000 Subject: [PATCH] updated script system: now you can specify which mesh will be affected by the filters. --- src/common/pluginmanager.cpp | 2 +- src/common/pluginmanager.h | 2 -- src/common/scriptinterface.cpp | 45 +++++++++++++++++++++++++++++----- src/common/scriptinterface.h | 4 ++- src/common/xmlfilterinfo.h | 7 ++++++ 5 files changed, 50 insertions(+), 10 deletions(-) diff --git a/src/common/pluginmanager.cpp b/src/common/pluginmanager.cpp index b837852a6..bcf0b4893 100644 --- a/src/common/pluginmanager.cpp +++ b/src/common/pluginmanager.cpp @@ -301,5 +301,5 @@ void PluginManager::updateDocumentScriptBindings(MeshDocument& doc ) //all the currentDocInterface created will be destroyed by QT when the MeshDocument destructor has been called currentDocInterface = new MeshDocumentScriptInterface(&doc); QScriptValue val = env.newQObject(currentDocInterface); - env.globalObject().setProperty(meshDocVarName(),val); + env.globalObject().setProperty(ScriptAdapterGenerator::meshDocVarName(),val); } diff --git a/src/common/pluginmanager.h b/src/common/pluginmanager.h index 5a56a256a..2b6200a5a 100644 --- a/src/common/pluginmanager.h +++ b/src/common/pluginmanager.h @@ -80,8 +80,6 @@ public: QStringList pluginsLoaded; Env env; - - inline static const QString meshDocVarName() {return QString("meshDoc");} void updateDocumentScriptBindings(MeshDocument& doc); }; diff --git a/src/common/scriptinterface.cpp b/src/common/scriptinterface.cpp index 45b633465..cf50e0954 100644 --- a/src/common/scriptinterface.cpp +++ b/src/common/scriptinterface.cpp @@ -19,7 +19,7 @@ QString ScriptAdapterGenerator::parNames(const RichParameterSet& set) const QString ScriptAdapterGenerator::parNames( const QString& filterName,const XMLFilterInfo& xmlInfo ) const { QString names; - //it's important the order!!! + //the order is important !!! XMLFilterInfo::XMLMapList params = xmlInfo.filterParametersExtendedInfo(filterName); int ii; bool optional = false; @@ -60,13 +60,25 @@ QString ScriptAdapterGenerator::funCodeGenerator( const QString& filterName,cons { QString code; QString names = parNames(filterName,xmlInfo); + QString ariet = xmlInfo.filterAttribute(filterName,MLXMLElNames::filterAriety); + + bool isSingle = (ariet == MLXMLElNames::singleMeshAriety); + QString mid("meshID"); + if ((names.isEmpty()) && isSingle) + names = mid; + else + if (isSingle) + names = mid + ", " + names; + code += "function (" + names + ")\n"; code += "{\n"; if (names.indexOf(optName()) != -1) code += "\t" + optName() + " = " + optName() + " || {};\n"; XMLFilterInfo::XMLMapList mplist = xmlInfo.filterParametersExtendedInfo(filterName); code += "\tvar environ = new Env;\n"; - int arg = 0; + + //if is singleMeshAriety i have to jump the first argument because is the meshID + int arg = (int) isSingle; for(int ii = 0; ii < mplist.size();++ii) { if (mplist[ii][MLXMLElNames::paramIsImportant] == "true") @@ -81,7 +93,15 @@ QString ScriptAdapterGenerator::funCodeGenerator( const QString& filterName,cons } } code += "\tvar environWrap = new EnvWrap(environ);\n"; - code += "\treturn _applyFilter(\"" + filterName + "\",environWrap);\n"; + if (isSingle) + { + code += "\tvar oldInd=" + meshDocVarName() + ".setCurrent(" + mid + ");\n"; + code += "\tif (oldInd == -1) return false;\n"; + } + code += "\tvar result = _applyFilter(\"" + filterName + "\",environWrap);\n"; + if (isSingle) + code += "\t" +meshDocVarName() + ".setCurrent(oldInd);\n"; + code += "\treturn result;\n"; code += "};\n"; return code; } @@ -102,7 +122,7 @@ QScriptValue PluginInterfaceInit(QScriptContext *context, QScriptEngine *engine, return false; } - MeshDocumentScriptInterface* md = qscriptvalue_cast(engine->globalObject().property(PluginManager::meshDocVarName())); + MeshDocumentScriptInterface* md = qscriptvalue_cast(engine->globalObject().property(ScriptAdapterGenerator::meshDocVarName())); RichParameterSet* rps = qscriptvalue_cast(context->argument(1)); MeshFilterInterface * mi = it.value(); @@ -122,7 +142,7 @@ QScriptValue PluginInterfaceApply(QScriptContext *context, QScriptEngine *engine return false; } - MeshDocumentScriptInterface* md = qscriptvalue_cast(engine->globalObject().property(PluginManager::meshDocVarName())); + MeshDocumentScriptInterface* md = qscriptvalue_cast(engine->globalObject().property(ScriptAdapterGenerator::meshDocVarName())); RichParameterSet* rps = qscriptvalue_cast(context->argument(1)); MeshFilterInterface * mi = it.value(); @@ -140,7 +160,7 @@ QScriptValue PluginInterfaceApplyXML(QScriptContext *context, QScriptEngine *eng if (it == pm->stringXMLFilterMap.end()) return false; - MeshDocumentScriptInterface* md = qscriptvalue_cast(engine->globalObject().property(PluginManager::meshDocVarName())); + MeshDocumentScriptInterface* md = qscriptvalue_cast(engine->globalObject().property(ScriptAdapterGenerator::meshDocVarName())); EnvWrap* envWrap = qscriptvalue_cast(context->argument(1)); MeshLabFilterInterface * mi = it->filterInterface; @@ -292,6 +312,19 @@ Q_INVOKABLE MeshModelScriptInterface* MeshDocumentScriptInterface::current() return NULL; } +Q_INVOKABLE int MeshDocumentScriptInterface::setCurrent(const int meshId) +{ + int id = md->mm()->id(); + if (md->getMesh(meshId) != NULL) + { + md->setCurrentMesh(meshId); + return id; + } + else + return -1; +} + + EnvWrap::EnvWrap(Env& envir) :env(&envir) { diff --git a/src/common/scriptinterface.h b/src/common/scriptinterface.h index 040abaed7..8a4e5ea54 100644 --- a/src/common/scriptinterface.h +++ b/src/common/scriptinterface.h @@ -37,6 +37,8 @@ private: QString parNames(const QString& filterName,const XMLFilterInfo& xmlInfo) const; static inline QString optName() {return QString("optional");} public: + inline static const QString meshDocVarName() {return QString("meshDoc");} + QString funCodeGenerator(const QString& filtername,const RichParameterSet& set) const; QString funCodeGenerator(const QString& filtername,const XMLFilterInfo& xmlInfo) const; }; @@ -68,7 +70,7 @@ public: Q_INVOKABLE MeshModelScriptInterface* getMesh(const int meshId); Q_INVOKABLE MeshModelScriptInterface* current(); - + Q_INVOKABLE int setCurrent(const int meshId); MeshDocument* md; }; diff --git a/src/common/xmlfilterinfo.h b/src/common/xmlfilterinfo.h index 8b8ad1535..9d2dcb936 100644 --- a/src/common/xmlfilterinfo.h +++ b/src/common/xmlfilterinfo.h @@ -70,6 +70,8 @@ namespace MLXMLElNames const QString filterClass("filterClass"); const QString filterPreCond("filterPre"); const QString filterPostCond("filterPost"); + const QString filterAriety("filterAriety"); + //filterHelp == name to access to the value of FILTER_HELP inside the Map produced by the XMLFilterInfo //const QString filterHelp("f_help"); @@ -90,6 +92,11 @@ namespace MLXMLElNames const QString boolType("Boolean"); const QString realType("Real"); const QString intType("Integer"); + + //ariety values + const QString singleMeshAriety("SingleMesh"); + const QString fixedAriety("Fixed"); + const QString variableAriety("Variable"); } //Query Exception should be managed by the XMLFilterInfo class (XMLFilterInfo is the class devoted to compose queries)