From d89fcc53a8e230cb0acaffd0fbde83aeb79caf1b Mon Sep 17 00:00:00 2001 From: Arin Alexander Date: Thu, 20 Apr 2017 23:34:32 +0300 Subject: [PATCH] Prevent multi run initscript Prevent multi run initscript Prevent multi run initscript & Build without HAVE_UI_LOADER has been fixed --- limereport/items/lrtextitem.cpp | 2 +- limereport/lrreportdesignwidget.cpp | 3 +- limereport/lrreportdesignwindow.cpp | 2 + limereport/lrreportengine.cpp | 28 ++- limereport/lrreportengine_p.h | 1 - limereport/lrreportrender.cpp | 179 +++---------- limereport/lrreportrender.h | 22 +- limereport/lrscriptenginemanager.cpp | 249 +++++++------------ limereport/lrscriptenginemanager.h | 6 + limereport/scriptbrowser/lrscriptbrowser.cpp | 5 +- limereport/scriptbrowser/lrscriptbrowser.h | 2 +- 11 files changed, 166 insertions(+), 333 deletions(-) diff --git a/limereport/items/lrtextitem.cpp b/limereport/items/lrtextitem.cpp index 4f4d258..d674bf2 100644 --- a/limereport/items/lrtextitem.cpp +++ b/limereport/items/lrtextitem.cpp @@ -503,7 +503,7 @@ TextItem::TextPtr TextItem::textDocument() const } //text->documentLayout(); - if (m_lineSpacing != 1 || m_textIndent !=0 ){ + if (m_lineSpacing != 1 || m_textIndent != 0 ){ for ( QTextBlock block = text->begin(); block.isValid(); block = block.next()) { diff --git a/limereport/lrreportdesignwidget.cpp b/limereport/lrreportdesignwidget.cpp index 6648354..b082273 100644 --- a/limereport/lrreportdesignwidget.cpp +++ b/limereport/lrreportdesignwidget.cpp @@ -77,8 +77,9 @@ ReportDesignWidget::ReportDesignWidget(ReportEngine *report, QMainWindow *mainWi connect(m_report,SIGNAL(pagesLoadFinished()),this,SLOT(slotPagesLoadFinished())); connect(m_report,SIGNAL(cleared()),this,SIGNAL(cleared())); connect(m_tabWidget, SIGNAL(currentChanged(int)), this, SLOT(slotCurrentTabChanged(int))); +#ifdef HAVE_UI_LOADER connect(m_report->scriptContext(), SIGNAL(dialogDeleted(QString)), this, SLOT(slotDialogDeleted(QString))); - +#endif //m_instance=this; m_scriptEditor->setPlainText(m_report->scriptContext()->initScript()); m_zoomer = new GraphicsViewZoomer(activeView()); diff --git a/limereport/lrreportdesignwindow.cpp b/limereport/lrreportdesignwindow.cpp index 2f15315..cee9775 100644 --- a/limereport/lrreportdesignwindow.cpp +++ b/limereport/lrreportdesignwindow.cpp @@ -1277,7 +1277,9 @@ void ReportDesignWindow::slotActivePageChanged() switch (m_editorTabType) { case ReportDesignWidget::Dialog: m_dialogEditorsState = saveState(); +#ifdef HAVE_UI_LOADER m_scriptBrowser->updateDialogsTree(); +#endif break; default: m_pageEditorsState = saveState(); diff --git a/limereport/lrreportengine.cpp b/limereport/lrreportengine.cpp index 8dcd2fc..0729fca 100644 --- a/limereport/lrreportengine.cpp +++ b/limereport/lrreportengine.cpp @@ -795,22 +795,34 @@ ReportPages ReportEnginePrivate::renderToPages() connect(m_reportRender.data(),SIGNAL(pageRendered(int)), this, SIGNAL(renderPageFinished(int))); + if (m_pages.count()){ +#ifdef HAVE_UI_LOADER + m_scriptEngineContext->initDialogs(); +#endif ReportPages result; m_reportRendering = true; - emit renderStarted(); + m_reportRender->setDatasources(dataManager()); m_reportRender->setScriptContext(scriptContext()); - foreach(PageDesignIntf* page , m_pages){ - m_pages.at(0)->setReportSettings(&m_reportSettings); - result.append(m_reportRender->renderPageToPages(page)); + foreach (PageDesignIntf* page, m_pages) { + scriptContext()->baseDesignIntfToScript(page->pageItem()); } - m_reportRender->secondRenderPass(result); - emit renderFinished(); - m_reportRender.clear(); - m_reportRendering = false; + if (m_scriptEngineContext->runInitScript()){ + emit renderStarted(); + + foreach(PageDesignIntf* page , m_pages){ + page->setReportSettings(&m_reportSettings); + result.append(m_reportRender->renderPageToPages(page)); + } + + m_reportRender->secondRenderPass(result); + emit renderFinished(); + m_reportRender.clear(); + m_reportRendering = false; + } return result; } else { return ReportPages(); diff --git a/limereport/lrreportengine_p.h b/limereport/lrreportengine_p.h index f900217..8a989d6 100644 --- a/limereport/lrreportengine_p.h +++ b/limereport/lrreportengine_p.h @@ -181,7 +181,6 @@ private: bool m_reportRendering; bool m_resultIsEditable; QString m_passPhrase; - QFileSystemWatcher *m_fileWatcher; }; diff --git a/limereport/lrreportrender.cpp b/limereport/lrreportrender.cpp index 039a3ba..008b17e 100644 --- a/limereport/lrreportrender.cpp +++ b/limereport/lrreportrender.cpp @@ -165,35 +165,6 @@ void ReportRender::setScriptContext(ScriptEngineContext* scriptContext) m_scriptEngineContext=scriptContext; } -bool ReportRender::runInitScript(){ - if (m_scriptEngineContext){ - ScriptEngineType* engine = ScriptEngineManager::instance().scriptEngine(); -#ifndef USE_QJSENGINE - engine->pushContext(); -#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; -} - void ReportRender::initDatasources(){ try{ datasources()->setAllDatasourcesToFirst(); @@ -222,67 +193,55 @@ void ReportRender::renderPage(PageDesignIntf* patternPage) m_curentNameIndex = 0; m_patternPageItem = patternPage->pageItem(); - if (m_patternPageItem->resetPageNumber() && m_pageCount>0) { resetPageNumber(PageReset); } + m_renderCanceled = false; BandDesignIntf* reportFooter = m_patternPageItem->bandByType(BandDesignIntf::ReportFooter); m_reportFooterHeight = 0; if (reportFooter) m_reportFooterHeight = reportFooter->height(); + initGroups(); -#ifdef HAVE_UI_LOADER - initDialogs(); -#endif + clearPageMap(); - if (m_scriptEngineContext){ - baseDesignIntfToScript(patternPage->pageItem()); - foreach (BaseDesignIntf* item, patternPage->pageItem()->childBaseItems()){ - baseDesignIntfToScript(item); - } + try{ + datasources()->setAllDatasourcesToFirst(); + datasources()->clearGroupFuntionsExpressions(); + } catch(ReportError &exception){ + //TODO possible should thow exeption + QMessageBox::critical(0,tr("Error"),exception.what()); + return; } - if (runInitScript()){ + clearPageMap(); + startNewPage(true); - clearPageMap(); + renderReportHeader(m_patternPageItem, AfterPageHeader); - try{ - datasources()->setAllDatasourcesToFirst(); - datasources()->clearGroupFuntionsExpressions(); - } catch(ReportError &exception){ - //TODO possible should thow exeption - QMessageBox::critical(0,tr("Error"),exception.what()); - return; - } + BandDesignIntf* lastRenderedBand = 0; + for (int i=0;idataBandCount() && !m_renderCanceled;i++){ + lastRenderedBand = m_patternPageItem->dataBandAt(i); + initDatasource(lastRenderedBand->datasourceName()); + renderDataBand(lastRenderedBand); + if (idataBandCount()-1) closeFooterGroup(lastRenderedBand); + } - clearPageMap(); - startNewPage(true); + if (reportFooter) + renderBand(reportFooter, 0, StartNewPageAsNeeded); + if (lastRenderedBand && lastRenderedBand->keepFooterTogether()) + closeFooterGroup(lastRenderedBand); - renderReportHeader(m_patternPageItem, AfterPageHeader); + BandDesignIntf* tearOffBand = m_patternPageItem->bandByType(BandDesignIntf::TearOffBand); + if (tearOffBand) + renderBand(tearOffBand, 0, StartNewPageAsNeeded); - BandDesignIntf* lastRenderedBand = 0; - for (int i=0;idataBandCount() && !m_renderCanceled;i++){ - lastRenderedBand = m_patternPageItem->dataBandAt(i); - initDatasource(lastRenderedBand->datasourceName()); - renderDataBand(lastRenderedBand); - if (idataBandCount()-1) closeFooterGroup(lastRenderedBand); - } - - if (reportFooter) - renderBand(reportFooter, 0, StartNewPageAsNeeded); - if (lastRenderedBand && lastRenderedBand->keepFooterTogether()) - closeFooterGroup(lastRenderedBand); - - BandDesignIntf* tearOffBand = m_patternPageItem->bandByType(BandDesignIntf::TearOffBand); - if (tearOffBand) - renderBand(tearOffBand, 0, StartNewPageAsNeeded); - - savePage(true); + savePage(true); #ifndef USE_QJSENGINE - ScriptEngineManager::instance().scriptEngine()->popContext(); + ScriptEngineManager::instance().scriptEngine()->popContext(); #endif - } + } int ReportRender::pageCount() @@ -325,31 +284,6 @@ void ReportRender::initVariables() m_datasources->setReportVariable("#PAGE_COUNT",0); } -#ifdef HAVE_UI_LOADER - -#ifdef USE_QJSENGINE -void registerChildObjects(ScriptEngineType* se, ScriptValueType* sv){ - foreach(QObject* obj, sv->toQObject()->children()){ - ScriptValueType child = se->newQObject(obj); - sv->setProperty(obj->objectName(),child); - registerChildObjects(se, &child); - } -} -#endif -void ReportRender::initDialogs(){ - if (m_scriptEngineContext){ - ScriptEngineType* se = ScriptEngineManager::instance().scriptEngine(); - foreach(DialogDescriber::Ptr dialog, m_scriptEngineContext->dialogDescribers()){ - ScriptValueType sv = se->newQObject(m_scriptEngineContext->getDialog(dialog->name())); -#ifdef USE_QJSENGINE - registerChildObjects(se,&sv); -#endif - se->globalObject().setProperty(dialog->name(),sv); - } - } -} -#endif - void ReportRender::clearPageMap() { m_renderedPages.clear(); @@ -548,7 +482,6 @@ void ReportRender::renderDataBand(BandDesignIntf *dataBand) if (header && !header->printAlways()) renderDataHeader(header); - //renderChildHeader(dataBand,PrintNotAlwaysPrintable); renderGroupHeader(dataBand, bandDatasource, true); bool firstTime = true; @@ -662,14 +595,10 @@ void ReportRender::renderPageItems(PageItemDesignIntf* patternPage) m_renderPageItem, m_renderPageItem); pageItems.append(cloneItem); - //cloneItem->updateItemSize(m_datasources); } } m_renderPageItem->restoreLinks(); m_renderPageItem->updateSubItemsSize(FirstPass,m_datasources); -// foreach(BaseDesignIntf* item, pageItems){ -// item->updateItemSize(m_datasources); -// } } qreal ReportRender::calcPageFooterHeight(PageItemDesignIntf *patternPage) @@ -1068,9 +997,6 @@ void ReportRender::secondRenderPass(ReportPages renderedPages) foreach(BaseDesignIntf* item, page->childBaseItems()){ item->updateItemSize(m_datasources, SecondPass); } -// foreach(BandDesignIntf* band, page->childBands()){ -// band->updateItemSize(m_datasources, SecondPass); -// } } } @@ -1080,7 +1006,6 @@ BandDesignIntf *ReportRender::saveUppperPartReturnBottom(BandDesignIntf *band, i BandDesignIntf* upperBandPart = dynamic_cast(band->cloneUpperPart(sliceHeight)); BandDesignIntf* bottomBandPart = dynamic_cast(band->cloneBottomPart(sliceHeight)); if (!bottomBandPart->isEmpty()){ - //bottomBandPart->updateItemSize(FirstPass,height); if (patternBand->keepFooterTogether()) closeFooterGroup(patternBand); } @@ -1097,8 +1022,7 @@ BandDesignIntf *ReportRender::saveUppperPartReturnBottom(BandDesignIntf *band, i savePage(); startNewPage(); } -// if (!bottomBandPart->isEmpty() && patternBand->keepFooterTogether()) -// openFooterGroup(patternBand); + delete band; return bottomBandPart; } @@ -1107,7 +1031,7 @@ BandDesignIntf *ReportRender::renderData(BandDesignIntf *patternBand) { BandDesignIntf* bandClone = dynamic_cast(patternBand->cloneItem(PreviewMode)); - baseDesignIntfToScript(bandClone); + m_scriptEngineContext->baseDesignIntfToScript(bandClone); emit(patternBand->beforeRender()); if (patternBand->isFooter()){ @@ -1120,7 +1044,7 @@ BandDesignIntf *ReportRender::renderData(BandDesignIntf *patternBand) bandClone->updateItemSize(m_datasources); - baseDesignIntfToScript(bandClone); + m_scriptEngineContext->baseDesignIntfToScript(bandClone); emit(patternBand->afterData()); return bandClone; @@ -1143,7 +1067,7 @@ void ReportRender::startNewPage(bool isFirst) initColumns(); initRenderPage(); - baseDesignIntfToScript(m_renderPageItem); + m_scriptEngineContext->baseDesignIntfToScript(m_renderPageItem); m_renderPageItem->setObjectName(QLatin1String("ReportPage")+QString::number(m_pageCount)); m_maxHeightByColumn[m_currentColumn]=m_renderPageItem->pageRect().height(); @@ -1360,37 +1284,4 @@ void ReportRender::cancelRender(){ m_renderCanceled = true; } -void ReportRender::baseDesignIntfToScript(BaseDesignIntf *item) -{ - if ( item ) { - - if (item->metaObject()->indexOfSignal("beforeRender()")!=-1) - item->disconnect(SIGNAL(beforeRender())); - if (item->metaObject()->indexOfSignal("afterData()")!=-1) - item->disconnect(SIGNAL(afterData())); - if (item->metaObject()->indexOfSignal("afterRender()")!=-1) - item->disconnect(SIGNAL(afterRender())); - - 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); - } - } - -} - -} +} // namespace LimeReport diff --git a/limereport/lrreportrender.h b/limereport/lrreportrender.h index 21048c9..cb3bca1 100644 --- a/limereport/lrreportrender.h +++ b/limereport/lrreportrender.h @@ -87,29 +87,20 @@ signals: public slots: void cancelRender(); private: - - void baseDesignIntfToScript(BaseDesignIntf* item); - - - void renderPage(PageDesignIntf *patternPage); void initDatasources(); void initDatasource(const QString &name); void initRenderPage(); -#ifdef HAVE_UI_LOADER - void initDialogs(); -#endif void initVariables(); - bool runInitScript(); + void initGroups(); void clearPageMap(); + + void renderPage(PageDesignIntf *patternPage); BandDesignIntf* renderBand(BandDesignIntf *patternBand, BandDesignIntf *bandData, DataRenderMode mode = NotStartNewPage, bool isLast = false); void renderDataBand(BandDesignIntf* dataBand); void renderPageHeader(PageItemDesignIntf* patternPage); void renderReportHeader(PageItemDesignIntf* patternPage, PageRenderStage stage); void renderPageFooter(PageItemDesignIntf* patternPage); - void moveTearOffBand(); void renderPageItems(PageItemDesignIntf* patternPage); - qreal calcPageFooterHeight(PageItemDesignIntf* patternPage); - qreal calcSlicePercent(qreal height); void renderChildHeader(BandDesignIntf* parent, BandPrintMode printMode); void renderChildFooter(BandDesignIntf* parent, BandPrintMode printMode); void renderChildBands(BandDesignIntf* parentBand); @@ -117,8 +108,10 @@ private: void renderDataHeader(BandDesignIntf* header); void renderGroupHeader(BandDesignIntf* parentBand, IDataSource* dataSource, bool firstTime); void renderGroupFooter(BandDesignIntf* parentBand); + void moveTearOffBand(); + qreal calcPageFooterHeight(PageItemDesignIntf* patternPage); + qreal calcSlicePercent(qreal height); - void initGroups(); bool containsGroupsFunction(BandDesignIntf* band); void extractGroupsFunction(BandDesignIntf* band); void replaceGroupsFunction(BandDesignIntf* band); @@ -169,10 +162,7 @@ private: QMultiMap< BandDesignIntf*, GroupBandsHolder* > m_childBands; QList m_reprintableBands; QList m_recalcBands; -// QList m_lastRenderedHeaders; - //int m_maxHeightByColumn[0]; - //int m_currentStartDataPos; int m_currentIndex; int m_pageCount; diff --git a/limereport/lrscriptenginemanager.cpp b/limereport/lrscriptenginemanager.cpp index 785640a..6a781cd 100644 --- a/limereport/lrscriptenginemanager.cpp +++ b/limereport/lrscriptenginemanager.cpp @@ -32,12 +32,14 @@ #include #include #include +#include #ifdef HAVE_UI_LOADER #include #include #include #endif #include "lrdatasourcemanager.h" +#include "lrbasedesignintf.h" Q_DECLARE_METATYPE(QColor) Q_DECLARE_METATYPE(QFont) @@ -192,146 +194,9 @@ void ScriptEngineModel::updateModel() } categ->addChild(funcDesc.name,funcDesc.description,ScriptEngineNode::Function,QIcon(":/report/images/function")); } - //reset(); endResetModel(); } -//QScriptValue line(QScriptContext* pcontext, QScriptEngine* pengine){ -// ScriptEngineManager* sm = qscriptvalue_cast(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*/){ - -// QString name = pcontext->argument(0).toString(); -// QVariant value = pcontext->argument(1).toVariant(); - -// ScriptEngineManager* sm = qscriptvalue_cast(pcontext->callee().data()); -// DataSourceManager* dm = sm->dataManager(); - -// dm->changeVariable(name,value); -// return QScriptValue(); -//} - -//QScriptValue getVariable(QScriptContext* pcontext, QScriptEngine* pengine){ - -// QString name = pcontext->argument(0).toString(); - -// ScriptEngineManager* sm = qscriptvalue_cast(pcontext->callee().data()); -// DataSourceManager* dm = sm->dataManager(); -// QScriptValue res = pengine->newVariant(dm->variable(name)); - -// return res; -//} - -//QScriptValue getField(QScriptContext* pcontext, QScriptEngine* pengine){ - -// QString name = pcontext->argument(0).toString(); - -// ScriptEngineManager* sm = qscriptvalue_cast(pcontext->callee().data()); -// DataSourceManager* dm = sm->dataManager(); -// QScriptValue res = pengine->newVariant(dm->fieldData(name)); - -// 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 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 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 date(QScriptContext* /*pcontext*/, QScriptEngine* pengine){ -// return pengine->newVariant(QDate::currentDate()); -//} - -//QScriptValue callGroupFunction(const QString& functionName, QScriptContext* pcontext, QScriptEngine* pengine){ - -// ScriptEngineManager* sm = qscriptvalue_cast(pcontext->callee().data()); -// DataSourceManager* dm = sm->dataManager(); - -// 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(); -// } - -// 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); -//} - ScriptEngineManager::~ScriptEngineManager() { delete m_model; @@ -896,20 +761,6 @@ ScriptEngineManager::ScriptEngineManager() createSetVariableFunction(); createGetFieldFunction(); createGetVariableFunction(); - -// addFunction("line",line,"SYSTEM", "line(\""+tr("BandName")+"\")"); -// addFunction("numberFormat",numberFormat,"NUMBER", "numberFormat(\""+tr("Value")+"\",\""+tr("Format")+"\",\""+ -// tr("Precision")+"\",\""+ -// tr("Locale")+"\")"); - -// addFunction("dateFormat",dateFormat,"DATE&TIME", "dateFormat(\""+tr("Value")+"\",\""+tr("Format")+"\")"); -// addFunction("timeFormat",timeFormat,"DATE&TIME", "dateFormat(\""+tr("Value")+"\",\""+tr("Format")+"\")"); -// addFunction("dateTimeFormat", dateTimeFormat, "DATE&TIME", "dateTimeFormat(\""+tr("Value")+"\",\""+tr("Format")+"\")"); -// addFunction("date",date,"DATE&TIME","date()"); -// addFunction("now",now,"DATE&TIME","now()"); -// addFunction("currencyFormat",currencyFormat,"NUMBER","currencyFormat(\""+tr("Value")+"\",\""+tr("Locale")+"\")"); -// addFunction("currencyUSBasedFormat",currencyUSBasedFormat,"NUMBER","currencyUSBasedFormat(\""+tr("Value")+",\""+tr("CurrencySymbol")+"\")"); -// addFunction("setVariable", setVariable, "GENERAL", "setVariable(\""+tr("Name")+"\",\""+tr("Value")+"\")"); #ifndef USE_QJSENGINE QScriptValue colorCtor = m_scriptEngine->newFunction(constructColor); m_scriptEngine->globalObject().setProperty("QColor", colorCtor); @@ -919,15 +770,6 @@ ScriptEngineManager::ScriptEngineManager() QScriptValue fontConstructor = m_scriptEngine->newFunction(QFontPrototype::constructorQFont, fontProto); m_scriptEngine->globalObject().setProperty("QFont", fontConstructor); #endif -// foreach(QString func, dataManager()->groupFunctionNames()){ -// 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); -// } - m_model = new ScriptEngineModel(this); } @@ -1275,6 +1117,93 @@ QString ScriptEngineContext::getNewDialogName() } #endif + +void ScriptEngineContext::baseDesignIntfToScript(BaseDesignIntf* item) +{ + if ( item ) { + + if (item->metaObject()->indexOfSignal("beforeRender()")!=-1) + item->disconnect(SIGNAL(beforeRender())); + if (item->metaObject()->indexOfSignal("afterData()")!=-1) + item->disconnect(SIGNAL(afterData())); + if (item->metaObject()->indexOfSignal("afterRender()")!=-1) + item->disconnect(SIGNAL(afterRender())); + + 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); + } + } +} + +#ifdef HAVE_UI_LOADER + +#ifdef USE_QJSENGINE +void registerChildObjects(ScriptEngineType* se, ScriptValueType* sv){ + foreach(QObject* obj, sv->toQObject()->children()){ + ScriptValueType child = se->newQObject(obj); + sv->setProperty(obj->objectName(),child); + registerChildObjects(se, &child); + } +} +#endif + +void ScriptEngineContext::initDialogs(){ + ScriptEngineType* se = ScriptEngineManager::instance().scriptEngine(); + foreach(DialogDescriber::Ptr dialog, dialogDescribers()){ + ScriptValueType sv = se->newQObject(getDialog(dialog->name())); +#ifdef USE_QJSENGINE + registerChildObjects(se,&sv); +#endif + se->globalObject().setProperty(dialog->name(),sv); + } +} + +#endif + + +bool ScriptEngineContext::runInitScript(){ + + ScriptEngineType* engine = ScriptEngineManager::instance().scriptEngine(); +#ifndef USE_QJSENGINE + engine->pushContext(); +#endif + ScriptValueType res = engine->evaluate(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; +} + QString ScriptEngineContext::initScript() const { return m_initScript; diff --git a/limereport/lrscriptenginemanager.h b/limereport/lrscriptenginemanager.h index 235599a..2e5786f 100644 --- a/limereport/lrscriptenginemanager.h +++ b/limereport/lrscriptenginemanager.h @@ -54,6 +54,7 @@ namespace LimeReport{ class DataSourceManager; +class BaseDesignIntf; struct ScriptFunctionDesc{ enum FuncType {Native,Script}; @@ -153,14 +154,19 @@ public: void deleteDialog(const QString& dialogName); QDialog *getDialog(const QString &dialogName); QString getNewDialogName(); + void initDialogs(); #endif + void baseDesignIntfToScript(BaseDesignIntf *item); void clear(); QString initScript() const; void setInitScript(const QString& initScript); + bool runInitScript(); +#ifdef HAVE_UI_LOADER signals: void dialogNameChanged(QString dialogName); void dialogDeleted(QString dialogName); void dialogAdded(QString dialogName); +#endif protected: QObject* createElement(const QString& collectionName,const QString& elementType); int elementsCount(const QString& collectionName); diff --git a/limereport/scriptbrowser/lrscriptbrowser.cpp b/limereport/scriptbrowser/lrscriptbrowser.cpp index 1ef6b73..98d0568 100644 --- a/limereport/scriptbrowser/lrscriptbrowser.cpp +++ b/limereport/scriptbrowser/lrscriptbrowser.cpp @@ -59,7 +59,9 @@ void ScriptBrowser::setReportEditor(ReportDesignWidget* report) m_report=report; connect(m_report,SIGNAL(cleared()),this,SLOT(slotClear())); connect(m_report,SIGNAL(loaded()),this,SLOT(slotUpdate())); +#ifdef HAVE_UI_LOADER connect(m_report->scriptContext(), SIGNAL(dialogAdded(QString)), this, SLOT(slotDialogAdded(QString))); +#endif updateFunctionTree(); } @@ -139,12 +141,13 @@ void ScriptBrowser::slotUpdate() updateFunctionTree(); } +#ifdef HAVE_UI_LOADER + void ScriptBrowser::slotDialogAdded(QString) { updateDialogsTree(); } -#ifdef HAVE_UI_LOADER void ScriptBrowser::on_tbAddDialog_clicked() { QFileDialog fileDialog(this); diff --git a/limereport/scriptbrowser/lrscriptbrowser.h b/limereport/scriptbrowser/lrscriptbrowser.h index dd7d9ae..54bf077 100644 --- a/limereport/scriptbrowser/lrscriptbrowser.h +++ b/limereport/scriptbrowser/lrscriptbrowser.h @@ -62,8 +62,8 @@ protected: private slots: void slotClear(); void slotUpdate(); - void slotDialogAdded(QString); #ifdef HAVE_UI_LOADER + void slotDialogAdded(QString); void on_tbAddDialog_clicked(); void on_tbRunDialog_clicked(); void on_tbDeleteDialog_clicked();