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

QJSEngine -> QQmlEngine

This commit is contained in:
Arin Alexander 2017-02-11 00:21:03 +03:00
parent c37a310918
commit 7406789cbb
5 changed files with 247 additions and 132 deletions

View File

@ -43,7 +43,8 @@
#endif
#ifdef USE_QJSENGINE
#include <QJSEngine>
//#include <QJSEngine>
#include <QQmlEngine>
#else
#include <QScriptEngine>
#endif
@ -138,8 +139,15 @@ namespace Const{
#endif
#ifdef USE_QJSENGINE
typedef QJSEngine ScriptEngineType;
typedef QQmlEngine ScriptEngineType;
typedef QJSValue ScriptValueType;
template <typename T>
static inline QJSValue getCppOwnedJSValue(QJSEngine &e, T *p)
{
QJSValue res = e.newQObject(p);
QQmlEngine::setObjectOwnership(p, QQmlEngine::CppOwnership);
return res;
}
#else
typedef QScriptEngine ScriptEngineType;
typedef QScriptValue ScriptValueType;

View File

@ -43,7 +43,8 @@
#endif
#ifdef USE_QJSENGINE
#include <QJSEngine>
//#include <QJSEngine>
#include <QQmlEngine>
#else
#include <QScriptEngine>
#endif
@ -138,8 +139,15 @@ namespace Const{
#endif
#ifdef USE_QJSENGINE
typedef QJSEngine ScriptEngineType;
typedef QQmlEngine ScriptEngineType;
typedef QJSValue ScriptValueType;
template <typename T>
static inline QJSValue getCppOwnedJSValue(QJSEngine &e, T *p)
{
QJSValue res = e.newQObject(p);
QQmlEngine::setObjectOwnership(p, QQmlEngine::CppOwnership);
return res;
}
#else
typedef QScriptEngine ScriptEngineType;
typedef QScriptValue ScriptValueType;

View File

@ -167,17 +167,29 @@ void ReportRender::setScriptContext(ScriptEngineContext* scriptContext)
bool ReportRender::runInitScript(){
if (m_scriptEngineContext){
QScriptEngine* engine = ScriptEngineManager::instance().scriptEngine();
ScriptEngineType* engine = ScriptEngineManager::instance().scriptEngine();
#ifndef USE_QJSENGINE
engine->pushContext();
QScriptValue res = engine->evaluate(m_scriptEngineContext->initScript());
#endif
ScriptValueType res = engine->evaluate(m_scriptEngineContext->initScript());
if (res.isBool()) return res.toBool();
#ifdef USE_QJSENGINE
if (res.isError()){
QMessageBox::critical(0,tr("Error"),
QString("Line %1: %2 ").arg(res.property("lineNumber").toString())
.arg(res.toString())
);
return false;
}
#else
if (engine->hasUncaughtException()) {
QMessageBox::critical(0,tr("Error"),
QString("Line %1: %2 ").arg(engine->uncaughtExceptionLineNumber())
.arg(engine->uncaughtException().toString())
);
return false;
}
}
#endif
}
return true;
}
@ -269,8 +281,9 @@ void ReportRender::renderPage(PageDesignIntf* patternPage)
renderBand(tearOffBand, 0, StartNewPageAsNeeded);
savePage(true);
#ifndef USE_QJSENGINE
ScriptEngineManager::instance().scriptEngine()->popContext();
#endif
}
}
@ -1262,18 +1275,26 @@ void ReportRender::baseDesignIntfToScript(BaseDesignIntf *item)
if (item->metaObject()->indexOfSignal("afterRender()")!=-1)
item->disconnect(SIGNAL(afterRender()));
QScriptEngine* engine = ScriptEngineManager::instance().scriptEngine();
QScriptValue sItem = engine->globalObject().property(item->patternName());
ScriptEngineType* engine = ScriptEngineManager::instance().scriptEngine();
#ifdef USE_QJSENGINE
//sItem = engine->newQObject(item);
ScriptValueType sItem = getCppOwnedJSValue(*engine, item);
engine->globalObject().setProperty(item->patternName(), sItem);
#else
ScriptValueType sItem = engine->globalObject().property(item->patternName());
if (sItem.isValid()){
engine->newQObject(sItem, item);
} else {
sItem = engine->newQObject(item);
engine->globalObject().setProperty(item->patternName(),sItem);
}
#endif
foreach(BaseDesignIntf* child, item->childBaseItems()){
baseDesignIntfToScript(child);
}
}
}
}

View File

@ -196,141 +196,141 @@ void ScriptEngineModel::updateModel()
endResetModel();
}
QScriptValue line(QScriptContext* pcontext, QScriptEngine* pengine){
ScriptEngineManager* sm = qscriptvalue_cast<ScriptEngineManager*>(pcontext->callee().data());
DataSourceManager* dm = sm->dataManager();
QString band = pcontext->argument(0).toString();
QScriptValue res;
QString varName = QLatin1String("line_")+band.toLower();
if (dm->variable(varName).isValid()){
res=pengine->newVariant(dm->variable(varName));
} else res=pengine->newVariant(QString("Variable line for band %1 not found").arg(band));
return res;
}
//QScriptValue line(QScriptContext* pcontext, QScriptEngine* pengine){
// ScriptEngineManager* sm = qscriptvalue_cast<ScriptEngineManager*>(pcontext->callee().data());
// DataSourceManager* dm = sm->dataManager();
// QString band = pcontext->argument(0).toString();
// QScriptValue res;
// QString varName = QLatin1String("line_")+band.toLower();
// if (dm->variable(varName).isValid()){
// res=pengine->newVariant(dm->variable(varName));
// } else res=pengine->newVariant(QString("Variable line for band %1 not found").arg(band));
// return res;
//}
QScriptValue setVariable(QScriptContext* pcontext, QScriptEngine* /*pengine*/){
//QScriptValue setVariable(QScriptContext* pcontext, QScriptEngine* /*pengine*/){
QString name = pcontext->argument(0).toString();
QVariant value = pcontext->argument(1).toVariant();
// QString name = pcontext->argument(0).toString();
// QVariant value = pcontext->argument(1).toVariant();
ScriptEngineManager* sm = qscriptvalue_cast<ScriptEngineManager*>(pcontext->callee().data());
DataSourceManager* dm = sm->dataManager();
// ScriptEngineManager* sm = qscriptvalue_cast<ScriptEngineManager*>(pcontext->callee().data());
// DataSourceManager* dm = sm->dataManager();
dm->changeVariable(name,value);
return QScriptValue();
}
// dm->changeVariable(name,value);
// return QScriptValue();
//}
QScriptValue getVariable(QScriptContext* pcontext, QScriptEngine* pengine){
//QScriptValue getVariable(QScriptContext* pcontext, QScriptEngine* pengine){
QString name = pcontext->argument(0).toString();
// QString name = pcontext->argument(0).toString();
ScriptEngineManager* sm = qscriptvalue_cast<ScriptEngineManager*>(pcontext->callee().data());
DataSourceManager* dm = sm->dataManager();
QScriptValue res = pengine->newVariant(dm->variable(name));
// ScriptEngineManager* sm = qscriptvalue_cast<ScriptEngineManager*>(pcontext->callee().data());
// DataSourceManager* dm = sm->dataManager();
// QScriptValue res = pengine->newVariant(dm->variable(name));
return res;
}
// return res;
//}
QScriptValue getField(QScriptContext* pcontext, QScriptEngine* pengine){
//QScriptValue getField(QScriptContext* pcontext, QScriptEngine* pengine){
QString name = pcontext->argument(0).toString();
// QString name = pcontext->argument(0).toString();
ScriptEngineManager* sm = qscriptvalue_cast<ScriptEngineManager*>(pcontext->callee().data());
DataSourceManager* dm = sm->dataManager();
QScriptValue res = pengine->newVariant(dm->fieldData(name));
// ScriptEngineManager* sm = qscriptvalue_cast<ScriptEngineManager*>(pcontext->callee().data());
// DataSourceManager* dm = sm->dataManager();
// QScriptValue res = pengine->newVariant(dm->fieldData(name));
return res;
}
// return res;
//}
QScriptValue numberFormat(QScriptContext* pcontext, QScriptEngine* pengine){
QVariant value = pcontext->argument(0).toVariant();
char format = (pcontext->argumentCount()>1)?pcontext->argument(1).toString()[0].toLatin1():'f';
int precision = (pcontext->argumentCount()>2)?pcontext->argument(2).toInt32():2;
QString locale = (pcontext->argumentCount()>3)?pcontext->argument(3).toString():"";
QScriptValue res = (locale.isEmpty())?pengine->newVariant(QString::number(value.toDouble(),format,precision)):
pengine->newVariant(QLocale(locale).toString(value.toDouble(),format,precision));
return res;
}
#if QT_VERSION>0x040800
QScriptValue currencyFormat(QScriptContext* pcontext, QScriptEngine* pengine){
QVariant value = pcontext->argument(0).toVariant();
QString locale = (pcontext->argumentCount()>1)?pcontext->argument(1).toString():QLocale::system().name();
return pengine->newVariant(QLocale(locale).toCurrencyString(value.toDouble()));
}
//QScriptValue numberFormat(QScriptContext* pcontext, QScriptEngine* pengine){
// QVariant value = pcontext->argument(0).toVariant();
// char format = (pcontext->argumentCount()>1)?pcontext->argument(1).toString()[0].toLatin1():'f';
// int precision = (pcontext->argumentCount()>2)?pcontext->argument(2).toInt32():2;
// QString locale = (pcontext->argumentCount()>3)?pcontext->argument(3).toString():"";
// QScriptValue res = (locale.isEmpty())?pengine->newVariant(QString::number(value.toDouble(),format,precision)):
// pengine->newVariant(QLocale(locale).toString(value.toDouble(),format,precision));
// return res;
//}
//#if QT_VERSION>0x040800
//QScriptValue currencyFormat(QScriptContext* pcontext, QScriptEngine* pengine){
// QVariant value = pcontext->argument(0).toVariant();
// QString locale = (pcontext->argumentCount()>1)?pcontext->argument(1).toString():QLocale::system().name();
// return pengine->newVariant(QLocale(locale).toCurrencyString(value.toDouble()));
//}
QScriptValue currencyUSBasedFormat(QScriptContext* pcontext, QScriptEngine* pengine){
QVariant value = pcontext->argument(0).toVariant();
QString CurrencySymbol = (pcontext->argumentCount()>1)?pcontext->argument(1).toString():QLocale::system().currencySymbol();
// Format it using USA locale
QString vTempStr=QLocale(QLocale::English, QLocale::UnitedStates).toCurrencyString(value.toDouble());
// Replace currency symbol if necesarry
if (CurrencySymbol!="") vTempStr.replace("$", CurrencySymbol);
return pengine->newVariant(vTempStr);
}
#endif
QScriptValue dateFormat(QScriptContext* pcontext, QScriptEngine* pengine){
QVariant value = pcontext->argument(0).toVariant();
QString format = (pcontext->argumentCount()>1)?pcontext->argument(1).toString().toLatin1():"dd.MM.yyyy";
QScriptValue res = pengine->newVariant(QLocale().toString(value.toDate(),format));
return res;
}
//QScriptValue currencyUSBasedFormat(QScriptContext* pcontext, QScriptEngine* pengine){
// QVariant value = pcontext->argument(0).toVariant();
// QString CurrencySymbol = (pcontext->argumentCount()>1)?pcontext->argument(1).toString():QLocale::system().currencySymbol();
// // Format it using USA locale
// QString vTempStr=QLocale(QLocale::English, QLocale::UnitedStates).toCurrencyString(value.toDouble());
// // Replace currency symbol if necesarry
// if (CurrencySymbol!="") vTempStr.replace("$", CurrencySymbol);
// return pengine->newVariant(vTempStr);
//}
//#endif
//QScriptValue dateFormat(QScriptContext* pcontext, QScriptEngine* pengine){
// QVariant value = pcontext->argument(0).toVariant();
// QString format = (pcontext->argumentCount()>1)?pcontext->argument(1).toString().toLatin1():"dd.MM.yyyy";
// QScriptValue res = pengine->newVariant(QLocale().toString(value.toDate(),format));
// return res;
//}
QScriptValue timeFormat(QScriptContext* pcontext, QScriptEngine* pengine){
QVariant value = pcontext->argument(0).toVariant();
QString format = (pcontext->argumentCount()>1)?pcontext->argument(1).toString().toLatin1():"hh:mm";
QScriptValue res = pengine->newVariant(QLocale().toString(value.toTime(),format));
return res;
}
//QScriptValue timeFormat(QScriptContext* pcontext, QScriptEngine* pengine){
// QVariant value = pcontext->argument(0).toVariant();
// QString format = (pcontext->argumentCount()>1)?pcontext->argument(1).toString().toLatin1():"hh:mm";
// QScriptValue res = pengine->newVariant(QLocale().toString(value.toTime(),format));
// return res;
//}
QScriptValue dateTimeFormat(QScriptContext* pcontext, QScriptEngine* pengine){
QVariant value = pcontext->argument(0).toVariant();
QString format = (pcontext->argumentCount()>1)?pcontext->argument(1).toString().toLatin1():"dd.MM.yyyy hh:mm";
QScriptValue res = pengine->newVariant(QLocale().toString(value.toDateTime(),format));
return res;
}
//QScriptValue dateTimeFormat(QScriptContext* pcontext, QScriptEngine* pengine){
// QVariant value = pcontext->argument(0).toVariant();
// QString format = (pcontext->argumentCount()>1)?pcontext->argument(1).toString().toLatin1():"dd.MM.yyyy hh:mm";
// QScriptValue res = pengine->newVariant(QLocale().toString(value.toDateTime(),format));
// return res;
//}
QScriptValue now(QScriptContext* /*pcontext*/, QScriptEngine* pengine){
return pengine->newVariant(QDateTime::currentDateTime());
}
//QScriptValue now(QScriptContext* /*pcontext*/, QScriptEngine* pengine){
// return pengine->newVariant(QDateTime::currentDateTime());
//}
QScriptValue date(QScriptContext* /*pcontext*/, QScriptEngine* pengine){
return pengine->newVariant(QDate::currentDate());
}
//QScriptValue date(QScriptContext* /*pcontext*/, QScriptEngine* pengine){
// return pengine->newVariant(QDate::currentDate());
//}
QScriptValue callGroupFunction(const QString& functionName, QScriptContext* pcontext, QScriptEngine* pengine){
//QScriptValue callGroupFunction(const QString& functionName, QScriptContext* pcontext, QScriptEngine* pengine){
ScriptEngineManager* sm = qscriptvalue_cast<ScriptEngineManager*>(pcontext->callee().data());
DataSourceManager* dm = sm->dataManager();
// ScriptEngineManager* sm = qscriptvalue_cast<ScriptEngineManager*>(pcontext->callee().data());
// DataSourceManager* dm = sm->dataManager();
QString expression;
QString band;
// QString expression;
// QString band;
if (functionName.compare("COUNT",Qt::CaseInsensitive) == 0 && pcontext->argumentCount()==1){
expression = " ";
band = pcontext->argument(0).toString();
} else {
expression = dm->getExpression(pcontext->argument(0).toString());
band = pcontext->argument(1).toString();
}
// if (functionName.compare("COUNT",Qt::CaseInsensitive) == 0 && pcontext->argumentCount()==1){
// expression = " ";
// band = pcontext->argument(0).toString();
// } else {
// expression = dm->getExpression(pcontext->argument(0).toString());
// band = pcontext->argument(1).toString();
// }
QScriptValue res;
GroupFunction* gf = dm->groupFunction(functionName,expression,band);
if (gf){
if (gf->isValid()){
res=pengine->newVariant(gf->calculate());
}else{
res=pengine->newVariant(gf->error());
}
}
else {
res=pengine->newVariant(QString(QObject::tr("Function %1 not found or have wrong arguments").arg(functionName)));
}
return res;
}
// QScriptValue res;
// GroupFunction* gf = dm->groupFunction(functionName,expression,band);
// if (gf){
// if (gf->isValid()){
// res=pengine->newVariant(gf->calculate());
// }else{
// res=pengine->newVariant(gf->error());
// }
// }
// else {
// res=pengine->newVariant(QString(QObject::tr("Function %1 not found or have wrong arguments").arg(functionName)));
// }
// return res;
//}
QScriptValue groupFunction(QScriptContext* pcontext, QScriptEngine* pengine){
return callGroupFunction(pcontext->callee().property("functionName").toString(),pcontext,pengine);
}
//QScriptValue groupFunction(QScriptContext* pcontext, QScriptEngine* pengine){
// return callGroupFunction(pcontext->callee().property("functionName").toString(),pcontext,pengine);
//}
ScriptEngineManager::~ScriptEngineManager()
{
@ -589,16 +589,24 @@ QString ScriptEngineManager::expandScripts(QString context, QVariant& varValue,
if (ScriptEngineManager::instance().dataManager()!=dataManager())
ScriptEngineManager::instance().setDataManager(dataManager());
QScriptEngine* se = ScriptEngineManager::instance().scriptEngine();
ScriptEngineType* se = ScriptEngineManager::instance().scriptEngine();
if (reportItem){
QScriptValue svThis = se->globalObject().property("THIS");
ScriptValueType svThis;
#ifdef USE_QJSENGINE
svThis = getCppOwnedJSValue(*se, reportItem);
se->globalObject().setProperty("THIS",svThis);
#else
svThis = se->globalObject().property("THIS");
if (svThis.isValid()){
se->newQObject(svThis, this);
se->newQObject(svThis, reportItem);
} else {
svThis = se->newQObject(this);
svThis = se->newQObject(reportItem);
se->globalObject().setProperty("THIS",svThis);
}
#endif
}
ScriptExtractor scriptExtractor(context);
@ -606,16 +614,26 @@ QString ScriptEngineManager::expandScripts(QString context, QVariant& varValue,
for(int i=0; i<scriptExtractor.count();++i){
QString scriptBody = expandDataFields(scriptExtractor.bodyAt(i),EscapeSymbols, varValue, reportItem);
scriptBody = expandUserVariables(scriptBody, FirstPass, EscapeSymbols, varValue);
QScriptValue value = se->evaluate(scriptBody);
ScriptValueType value = se->evaluate(scriptBody);
#ifdef USE_QJSENGINE
if (!value.isError()){
varValue = value.toVariant();
context.replace(scriptExtractor.scriptAt(i),value.toString());
} else {
context.replace(scriptExtractor.scriptAt(i),value.toString());
}
#else
if (!se->hasUncaughtException()) {
varValue = value.toVariant();
context.replace(scriptExtractor.scriptAt(i),value.toString());
} else {
context.replace(scriptExtractor.scriptAt(i),se->uncaughtException().toString());
}
#endif
}
}
}
return context;
}
@ -629,14 +647,18 @@ QVariant ScriptEngineManager::evaluateScript(const QString& script){
if (ScriptEngineManager::instance().dataManager()!=dataManager())
ScriptEngineManager::instance().setDataManager(dataManager());
QScriptEngine* se = ScriptEngineManager::instance().scriptEngine();
ScriptEngineType* se = ScriptEngineManager::instance().scriptEngine();
ScriptExtractor scriptExtractor(script);
if (scriptExtractor.parse()){
QString scriptBody = expandDataFields(scriptExtractor.bodyAt(0),EscapeSymbols, varValue, 0);
scriptBody = expandUserVariables(scriptBody, FirstPass, EscapeSymbols, varValue);
QScriptValue value = se->evaluate(scriptBody);
ScriptValueType value = se->evaluate(scriptBody);
#ifdef USE_QJSENGINE
if (!value.isError()){
#else
if (!se->hasUncaughtException()) {
#endif
return value.toVariant();
}
}
@ -820,6 +842,36 @@ bool ScriptEngineManager::createSetVariableFunction(){
return addFunction(fd);
}
bool ScriptEngineManager::createGetVariableFunction()
{
JSFunctionDesc fd;
fd.setManager(m_functionManager);
fd.setManagerName(LimeReport::Const::FUNCTION_MANAGER_NAME);
fd.setCategory("GENERAL");
fd.setName("getVariable");
fd.setDescription("getVariable(\""+tr("Name")+"\")");
fd.setScriptWrapper(QString("function getVariable(name){"
"return %1.getVariable(name);}"
).arg(LimeReport::Const::FUNCTION_MANAGER_NAME)
);
return addFunction(fd);
}
bool ScriptEngineManager::createGetFieldFunction()
{
JSFunctionDesc fd;
fd.setManager(m_functionManager);
fd.setManagerName(LimeReport::Const::FUNCTION_MANAGER_NAME);
fd.setCategory("GENERAL");
fd.setName("getField");
fd.setDescription("getField(\""+tr("Name")+"\")");
fd.setScriptWrapper(QString("function getField(name){"
"return %1.getField(name);}"
).arg(LimeReport::Const::FUNCTION_MANAGER_NAME)
);
return addFunction(fd);
}
ScriptEngineManager::ScriptEngineManager()
:m_model(0), m_dataManager(0)
{
@ -839,6 +891,8 @@ ScriptEngineManager::ScriptEngineManager()
createCurrencyUSBasedFormatFunction();
#endif
createSetVariableFunction();
createGetFieldFunction();
createGetVariableFunction();
// addFunction("line",line,"SYSTEM", "line(\""+tr("BandName")+"\")");
// addFunction("numberFormat",numberFormat,"NUMBER", "numberFormat(\""+tr("Value")+"\",\""+tr("Format")+"\",\""+
@ -1235,10 +1289,17 @@ void JSFunctionDesc::setScriptWrapper(const QString &scriptWrapper)
m_scriptWrapper = scriptWrapper;
}
QVariant ScriptFunctionsManager::calcGroupFunction(const QString &name, const QString &fieldName, const QString &bandName)
QVariant ScriptFunctionsManager::calcGroupFunction(const QString &name, const QString &expressionID, const QString &bandName)
{
if (m_scriptEngineManager->dataManager()){
GroupFunction* gf = m_scriptEngineManager->dataManager()->groupFunction(name,fieldName,bandName);
QString expression = "";
if (name.compare("COUNT",Qt::CaseInsensitive) == 0){
expression = " ";
} else {
expression = m_scriptEngineManager->dataManager()->getExpression(expressionID);
}
GroupFunction* gf = m_scriptEngineManager->dataManager()->groupFunction(name,expression,bandName);
if (gf){
if (gf->isValid()){
return gf->calculate();
@ -1316,6 +1377,19 @@ void ScriptFunctionsManager::setVariable(const QString &name, QVariant value)
DataSourceManager* dm = scriptEngineManager()->dataManager();
dm->changeVariable(name,value);
}
QVariant ScriptFunctionsManager::getVariable(const QString &name)
{
DataSourceManager* dm = scriptEngineManager()->dataManager();
return dm->variable(name);
}
QVariant ScriptFunctionsManager::getField(const QString &field)
{
DataSourceManager* dm = scriptEngineManager()->dataManager();
return dm->fieldData(field);
}
#ifdef USE_QJSENGINE
QFont ScriptFunctionsManager::font(const QString &family, int pointSize, bool bold, bool italic, bool underLine)
{

View File

@ -216,7 +216,7 @@ class ScriptFunctionsManager : public QObject{
Q_OBJECT
public:
explicit ScriptFunctionsManager(QObject* parent = 0):QObject(parent){}
Q_INVOKABLE QVariant calcGroupFunction(const QString& name, const QString& fieldName, const QString& bandName);
Q_INVOKABLE QVariant calcGroupFunction(const QString& name, const QString& expressionID, const QString& bandName);
Q_INVOKABLE QVariant line(const QString& bandName);
Q_INVOKABLE QVariant numberFormat(QVariant value, const char &format, int precision, const QString &locale);
Q_INVOKABLE QVariant dateFormat(QVariant value, const QString& format);
@ -226,7 +226,9 @@ public:
Q_INVOKABLE QVariant now();
Q_INVOKABLE QVariant currencyFormat(QVariant value, const QString& locale);
Q_INVOKABLE QVariant currencyUSBasedFormat(QVariant value, const QString& currencySymbol);
Q_INVOKABLE void setVariable(const QString& name, QVariant value);
Q_INVOKABLE void setVariable(const QString& name, QVariant value);
Q_INVOKABLE QVariant getVariable(const QString& name);
Q_INVOKABLE QVariant getField(const QString& field);
Q_INVOKABLE QVariant color(const QString& color){ return QColor(color);}
#ifdef USE_QJSENGINE
Q_INVOKABLE QFont font(const QString& family, int pointSize = -1, bool bold = false, bool italic = false, bool underLine = false);
@ -283,6 +285,8 @@ private:
bool createCurrencyFormatFunction();
bool createCurrencyUSBasedFormatFunction();
bool createSetVariableFunction();
bool createGetVariableFunction();
bool createGetFieldFunction();
private:
ScriptEngineManager();
ScriptEngineType* m_scriptEngine;