0
0
mirror of https://github.com/fralx/LimeReport.git synced 2024-12-24 00:33:02 +03:00

ScriptEngineManager::addFunction has been fixed

This commit is contained in:
Arin Alexander 2016-08-12 22:14:05 +04:00
parent 1320c6f8ee
commit 71fe049ff4
4 changed files with 42 additions and 27 deletions

View File

@ -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(){}

View File

@ -323,26 +323,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){
@ -354,11 +368,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;
}
}
@ -418,10 +432,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);

View File

@ -114,9 +114,9 @@ public:
friend class Singleton<ScriptEngineManager>;
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();
@ -124,6 +124,7 @@ public:
ScriptEngineModel* model(){return m_model;}
DataSourceManager* dataManager() const {return m_dataManager;}
void setDataManager(DataSourceManager* dataManager);
bool containsFunction(const QString &functionName);
private:
Q_DISABLE_COPY(ScriptEngineManager)
private:

View File

@ -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(){}