diff --git a/include/lrscriptenginemanagerintf.h b/include/lrscriptenginemanagerintf.h index f553e00..d7662ff 100644 --- a/include/lrscriptenginemanagerintf.h +++ b/include/lrscriptenginemanagerintf.h @@ -37,9 +37,9 @@ namespace LimeReport{ class IScriptEngineManager{ public: virtual QScriptEngine* scriptEngine() = 0; - virtual QScriptValue addFunction(const QString& name, QScriptEngine::FunctionSignature function, + virtual bool addFunction(const QString& name, QScriptEngine::FunctionSignature function, const QString& category="", const QString& description="") = 0; - virtual QScriptValue addFunction(const QString &name, const QString& script, + virtual bool addFunction(const QString &name, const QString& script, const QString &category="", const QString &description="") = 0; virtual const QString& lastError() const = 0; virtual ~IScriptEngineManager(){} diff --git a/limereport/lrscriptenginemanager.cpp b/limereport/lrscriptenginemanager.cpp index fc7a3c5..b61e819 100644 --- a/limereport/lrscriptenginemanager.cpp +++ b/limereport/lrscriptenginemanager.cpp @@ -328,26 +328,40 @@ void ScriptEngineManager::deleteFunction(const QString &functionsName) } } -QScriptValue ScriptEngineManager::addFunction(const QString& name, +bool ScriptEngineManager::containsFunction(const QString& functionName){ + foreach (ScriptFunctionDesc funct, m_functions) { + if (funct.name.compare(functionName)== 0){ + return true; + } + } + return false; +} + +bool ScriptEngineManager::addFunction(const QString& name, QScriptEngine::FunctionSignature function, const QString& category, const QString& description) { - ScriptFunctionDesc funct; - funct.name = name; - funct.description = description; - funct.category = category; - funct.scriptValue = scriptEngine()->newFunction(function); - funct.scriptValue.setProperty("functionName",name); - funct.scriptValue.setData(m_scriptEngine->toScriptValue(this)); - funct.type = ScriptFunctionDesc::Native; - m_functions.append(funct); - if (m_model) - m_model->updateModel(); - return funct.scriptValue; + if (!containsFunction(name)){ + ScriptFunctionDesc funct; + funct.name = name; + funct.description = description; + funct.category = category; + funct.scriptValue = scriptEngine()->newFunction(function); + funct.scriptValue.setProperty("functionName",name); + funct.scriptValue.setData(m_scriptEngine->toScriptValue(this)); + funct.type = ScriptFunctionDesc::Native; + m_functions.append(funct); + if (m_model) + m_model->updateModel(); + m_scriptEngine->globalObject().setProperty(funct.name,funct.scriptValue); + return true; + } else { + return false; + } } -QScriptValue ScriptEngineManager::addFunction(const QString& name, const QString& script, const QString& category, const QString& description) +bool ScriptEngineManager::addFunction(const QString& name, const QString& script, const QString& category, const QString& description) { QScriptSyntaxCheckResult cr = m_scriptEngine->checkSyntax(script); if (cr.state() == QScriptSyntaxCheckResult::Valid){ @@ -359,11 +373,11 @@ QScriptValue ScriptEngineManager::addFunction(const QString& name, const QString funct.type = ScriptFunctionDesc::Script; funct.scriptValue.setData(m_scriptEngine->toScriptValue(this)); m_functions.append(funct); - m_model->updateModel(); - return funct.scriptValue; + m_model->updateModel(); + return true; } else { m_lastError = cr.errorMessage(); - return QScriptValue(); + return false; } } @@ -428,10 +442,10 @@ ScriptEngineManager::ScriptEngineManager() // addFunction(func, groupFunction,"GROUP FUNCTIONS", func+"(\""+tr("FieldName")+"\",\""+tr("BandName")+"\")"); // } - foreach(ScriptFunctionDesc func, m_functions){ - if (func.type==ScriptFunctionDesc::Native) - m_scriptEngine->globalObject().setProperty(func.name,func.scriptValue); - } +// foreach(ScriptFunctionDesc func, m_functions){ +// if (func.type==ScriptFunctionDesc::Native) +// m_scriptEngine->globalObject().setProperty(func.name,func.scriptValue); +// } m_model = new ScriptEngineModel(this); diff --git a/limereport/lrscriptenginemanager.h b/limereport/lrscriptenginemanager.h index 32ef2c5..0c738d9 100644 --- a/limereport/lrscriptenginemanager.h +++ b/limereport/lrscriptenginemanager.h @@ -178,9 +178,9 @@ public: friend class Singleton; bool isFunctionExists(const QString& functionName) const; void deleteFunction(const QString& functionsName); - QScriptValue addFunction(const QString& name, QScriptEngine::FunctionSignature function, + bool addFunction(const QString& name, QScriptEngine::FunctionSignature function, const QString& category="", const QString& description=""); - QScriptValue addFunction(const QString &name, const QString& script, + bool addFunction(const QString &name, const QString& script, const QString &category="", const QString &description=""); const QString& lastError() const {return m_lastError;} QStringList functionsNames(); @@ -189,8 +189,10 @@ public: void setContext(ScriptEngineContext* context){m_context=context;} DataSourceManager* dataManager() const {return m_dataManager;} void setDataManager(DataSourceManager* dataManager); + protected: void updateModel(); + bool containsFunction(const QString &functionName); private: Q_DISABLE_COPY(ScriptEngineManager) private: diff --git a/limereport/lrscriptenginemanagerintf.h b/limereport/lrscriptenginemanagerintf.h index f553e00..d7662ff 100644 --- a/limereport/lrscriptenginemanagerintf.h +++ b/limereport/lrscriptenginemanagerintf.h @@ -37,9 +37,9 @@ namespace LimeReport{ class IScriptEngineManager{ public: virtual QScriptEngine* scriptEngine() = 0; - virtual QScriptValue addFunction(const QString& name, QScriptEngine::FunctionSignature function, + virtual bool addFunction(const QString& name, QScriptEngine::FunctionSignature function, const QString& category="", const QString& description="") = 0; - virtual QScriptValue addFunction(const QString &name, const QString& script, + virtual bool addFunction(const QString &name, const QString& script, const QString &category="", const QString &description="") = 0; virtual const QString& lastError() const = 0; virtual ~IScriptEngineManager(){}