From bbe2bfb7bee058b0ca9be5382430e3ba05022f97 Mon Sep 17 00:00:00 2001 From: Guido Ranzuglia granzuglia Date: Fri, 5 Nov 2010 15:21:25 +0000 Subject: [PATCH] updated version of XML-oriented MeshLab --- src/common/mlparameter.cpp | 29 ++++++++++++++++++++++++++--- src/common/mlparameter.h | 21 ++++++++++++++------- src/common/pluginmanager.cpp | 35 +++++++++++++++++++++++++++++------ src/common/pluginmanager.h | 7 ++++++- 4 files changed, 75 insertions(+), 17 deletions(-) diff --git a/src/common/mlparameter.cpp b/src/common/mlparameter.cpp index 1a56e7689..4f88191b5 100644 --- a/src/common/mlparameter.cpp +++ b/src/common/mlparameter.cpp @@ -17,7 +17,7 @@ const QString& Expression::expression() const QScriptValue Expression::evaluate(Env* env ) const { - QScriptValue val = env->eng->evaluate(expression()); + QScriptValue val = env->evaluate(expression()); if(val.isError()) throw ParsingException(QString(val.toString())); return val; @@ -59,6 +59,23 @@ Value* FloatExpression::eval(const QString& floatExp,Env* env ) return new FloatValue(evaluate(env).toNumber()); } +IntExpression::IntExpression() : Expression() {} +IntExpression::IntExpression(const QString& ex) : Expression(ex) {} + +IntExpression::~IntExpression() {} + +Value* IntExpression::eval( Env* env ) const +{ + return new IntValue(evaluate(env).toInt32()); +} + +Value* IntExpression::eval(const QString& intExp,Env* env ) +{ + expression() = intExp; + return new IntValue(evaluate(env).toInt32()); +} + + //FilterEnv::FilterEnv(const QMap& evalExpress) //:evaluatedExpressions(evalExpress) //{ @@ -76,9 +93,15 @@ Value* FloatExpression::eval(const QString& floatExp,Env* env ) // return evaluatedExpressions->find(nm)->getFloat(); //} // -Env::Env( QScriptEngine* scriptEng ) +Env::Env() +:QScriptEngine() { - eng = scriptEng; +} + +Value* Env::insertNewFieldToVariable(const QString& var,const QString& field,Expression* exp) +{ + QString decl(var + "." + field + " = " + exp->expression() + ";"); + return exp->eval(decl,this); } Value* Env::insertLocalExpressionBinding( const QString& nm,Expression* exp ) diff --git a/src/common/mlparameter.h b/src/common/mlparameter.h index 2b1bc887e..55569d01b 100644 --- a/src/common/mlparameter.h +++ b/src/common/mlparameter.h @@ -44,6 +44,16 @@ public: Value* eval(const QString& floatExp,Env* env); }; +class IntExpression : public Expression +{ +public: + IntExpression(); + IntExpression(const QString& ex); + ~IntExpression(); + Value* eval(Env* env) const; + Value* eval(const QString& intExp,Env* env); +}; + class FilterEnv { private: @@ -92,21 +102,18 @@ public: -class Env +class Env : public QScriptEngine { public: - Env(QScriptEngine* scriptEng); + Env(); //could throw a ParsingException: you should catch it in the calling code Value* insertLocalExpressionBinding(const QString& nm,Expression* val); + Value* insertNewFieldToVariable(const QString& var,const QString& field,Expression* exp); + //Value* removeLocalValueBinding(const QString& nm); //Value* insertGlobalValueBinding(const QString& nm,Value* val); //Value* removeGlobalValueBinding(const QString& nm); - inline void pushFrame() {eng->pushContext();}; - inline void popFrame() {eng->popContext();}; - - - QScriptEngine* eng; }; #endif \ No newline at end of file diff --git a/src/common/pluginmanager.cpp b/src/common/pluginmanager.cpp index 3657c14b7..1468d5127 100644 --- a/src/common/pluginmanager.cpp +++ b/src/common/pluginmanager.cpp @@ -7,7 +7,7 @@ PluginManager::PluginManager() -:eng(),env(&eng) +:env() { //pluginsDir=QDir(getPluginDirPath()); // without adding the correct library path in the mac the loading of jpg (done via qt plugins) fails @@ -137,13 +137,13 @@ void PluginManager::loadPlugins(RichParameterSet& defaultGlobal) } } - QScriptValue initFun = eng.newFunction(PluginInterfaceInit, this); - eng.globalObject().setProperty("_initParameterSet", initFun); + QScriptValue initFun = env.newFunction(PluginInterfaceInit, this); + env.globalObject().setProperty("_initParameterSet", initFun); - QScriptValue applyFun = eng.newFunction(PluginInterfaceApply, this); - eng.globalObject().setProperty("_applyFilter", applyFun); + QScriptValue applyFun = env.newFunction(PluginInterfaceApply, this); + env.globalObject().setProperty("_applyFilter", applyFun); - eng.evaluate(code); + env.evaluate(code); qDebug("Code:\n %s",qPrintable(code)); } /* @@ -250,3 +250,26 @@ void PluginManager::LoadFormats(QStringList &filters, QHashid()); + +} + +void PluginManager::updateMeshScriptBindings(MeshDocument& doc,const int id) +{ + IntExpression meshInd(QString::number(id)); + env.insertNewFieldToVariable(meshDocVarName(),currentMeshVarName(),&meshInd); + FloatExpression bboxDiag(QString::number(0.0f)); + MeshModel* model = doc.getMesh(id); + if (model != NULL) + bboxDiag.expression() = QString::number(model->cm.bbox.Diag()); + + Value* val = env.insertNewFieldToVariable(meshDocVarName() + "." + currentMeshVarName(),QString("bboxDiag"),&bboxDiag); + QString st(meshDocVarName() + "." + currentMeshVarName() + ".bboxDiag"); + env.evaluate("print(" + st + ");"); +} \ No newline at end of file diff --git a/src/common/pluginmanager.h b/src/common/pluginmanager.h index 105aaf84b..566202014 100644 --- a/src/common/pluginmanager.h +++ b/src/common/pluginmanager.h @@ -73,8 +73,13 @@ public: QStringList pluginsLoaded; - QScriptEngine eng; Env env; + + inline static const QString meshDocVarName() {return QString("meshDocument");} + inline static const QString currentMeshVarName() {return QString("current");} + + void updateDocumentScriptBindings(MeshDocument& doc); + void updateMeshScriptBindings(MeshDocument& doc,const int id); }; #endif // PLUGINMANAGER_H