From edd2c43545ad9071878b76ec8313246740ee8b00 Mon Sep 17 00:00:00 2001 From: Arin Alexander Date: Tue, 20 Dec 2016 01:33:58 +0300 Subject: [PATCH] beforeRender, afterData, afterRender signals have been added getVariable, getField functions have been added --- limereport/lrbanddesignintf.cpp | 1 + limereport/lrbasedesignintf.cpp | 2 +- limereport/lrbasedesignintf.h | 4 ++ limereport/lrreportrender.cpp | 73 +++++++++++++++++++++++----- limereport/lrreportrender.h | 3 ++ limereport/lrscriptenginemanager.cpp | 24 +++++++++ 6 files changed, 93 insertions(+), 14 deletions(-) diff --git a/limereport/lrbanddesignintf.cpp b/limereport/lrbanddesignintf.cpp index c7b3dcc..2fe29b8 100644 --- a/limereport/lrbanddesignintf.cpp +++ b/limereport/lrbanddesignintf.cpp @@ -357,6 +357,7 @@ bool BandDesignIntf::canBeSplitted(int height) const bool BandDesignIntf::isEmpty() const { + if (!isVisible()) return true; foreach(QGraphicsItem* qgItem,childItems()){ BaseDesignIntf* item = dynamic_cast(qgItem); if ((item)&&(!item->isEmpty())) return false; diff --git a/limereport/lrbasedesignintf.cpp b/limereport/lrbasedesignintf.cpp index 3a3056b..7025a0f 100644 --- a/limereport/lrbasedesignintf.cpp +++ b/limereport/lrbasedesignintf.cpp @@ -818,7 +818,7 @@ void BaseDesignIntf::turnOnSelectionMarker(bool value) QString BaseDesignIntf::patternName() const { - return m_patternName; + return (m_patternName.isEmpty()) ? objectName() : m_patternName; } void BaseDesignIntf::setPatternName(const QString &patternName) diff --git a/limereport/lrbasedesignintf.h b/limereport/lrbasedesignintf.h index 71e2736..d9bdf3f 100644 --- a/limereport/lrbasedesignintf.h +++ b/limereport/lrbasedesignintf.h @@ -387,6 +387,10 @@ signals: void propertyesChanged(QVector propertyNames); void itemAlignChanged(BaseDesignIntf* item, const ItemAlign& oldValue, const ItemAlign& newValue); void itemVisibleHasChanged(BaseDesignIntf* item); + + void beforeRender(); + void afterData(); + void afterRender(); }; } //namespace LimeReport diff --git a/limereport/lrreportrender.cpp b/limereport/lrreportrender.cpp index 196d3ad..1f656eb 100644 --- a/limereport/lrreportrender.cpp +++ b/limereport/lrreportrender.cpp @@ -167,6 +167,7 @@ void ReportRender::setScriptContext(ScriptEngineContext* scriptContext) bool ReportRender::runInitScript(){ if (m_scriptEngineContext){ + ScriptEngineManager::instance().scriptEngine()->pushContext(); QScriptValue res = ScriptEngineManager::instance().scriptEngine()->evaluate(m_scriptEngineContext->initScript()); if (res.isBool()) return res.toBool(); } @@ -213,6 +214,14 @@ void ReportRender::renderPage(PageDesignIntf* patternPage) #ifdef HAVE_UI_LOADER initDialogs(); #endif + + if (m_scriptEngineContext){ + + foreach (BaseDesignIntf* item, patternPage->pageItem()->childBaseItems()){ + baseDesignIntfToScript(item); + } + } + if (runInitScript()){ clearPageMap(); @@ -224,6 +233,7 @@ void ReportRender::renderPage(PageDesignIntf* patternPage) QMessageBox::critical(0,tr("Error"),exception.what()); return; } + clearPageMap(); startNewPage(); @@ -242,12 +252,13 @@ void ReportRender::renderPage(PageDesignIntf* patternPage) if (lastRenderedBand && lastRenderedBand->keepFooterTogether()) closeFooterGroup(lastRenderedBand); - BandDesignIntf* tearOffBand = m_patternPageItem->bandByType(BandDesignIntf::TearOffBand); - if (tearOffBand) - renderBand(tearOffBand, 0, StartNewPageAsNeeded); + BandDesignIntf* tearOffBand = m_patternPageItem->bandByType(BandDesignIntf::TearOffBand); + if (tearOffBand) + renderBand(tearOffBand, 0, StartNewPageAsNeeded); savePage(true); + ScriptEngineManager::instance().scriptEngine()->popContext(); } } @@ -364,14 +375,6 @@ BandDesignIntf* ReportRender::renderBand(BandDesignIntf *patternBand, BandDesign QApplication::processEvents(); if (patternBand){ - if (mode == ForcedStartPage){ - savePage(); - startNewPage(); - } - - if (patternBand->isFooter()) - m_lastRenderedFooter = patternBand; - BandDesignIntf* bandClone = 0; if (bandData){ @@ -380,13 +383,24 @@ BandDesignIntf* ReportRender::renderBand(BandDesignIntf *patternBand, BandDesign bandClone=renderData(patternBand); } + if (mode == ForcedStartPage){ + savePage(); + startNewPage(); + } + + if (patternBand->isFooter()) + m_lastRenderedFooter = patternBand; + + bandClone->setBackgroundColor( (datasources()->variable(QLatin1String("line_")+patternBand->objectName().toLower()).toInt()%2!=0 ? - patternBand->backgroundColor(): - patternBand->alternateBackgroundColor() + bandClone->backgroundColor(): + bandClone->alternateBackgroundColor() ) ); + patternBand->emitBandRendered(bandClone); + emit(patternBand->afterRender()); if ( isLast && bandClone->keepFooterTogether() && bandClone->sliceLastRow() ){ if (m_maxHeightByColumn[m_currentColumn] < (bandClone->height()+m_reportFooterHeight)) @@ -960,10 +974,18 @@ BandDesignIntf *ReportRender::saveUppperPartReturnBottom(BandDesignIntf *band, i BandDesignIntf *ReportRender::renderData(BandDesignIntf *patternBand) { BandDesignIntf* bandClone = dynamic_cast(patternBand->cloneItem(PreviewMode)); + + baseDesignIntfToScript(bandClone); + emit(patternBand->beforeRender()); + if (patternBand->isFooter()){ replaceGroupsFunction(bandClone); } bandClone->updateItemSize(m_datasources); + + baseDesignIntfToScript(bandClone); + emit(patternBand->afterData()); + return bandClone; } @@ -1194,4 +1216,29 @@ 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())); + + QScriptEngine* engine = ScriptEngineManager::instance().scriptEngine(); + QScriptValue sItem = engine->globalObject().property(item->patternName()); + if (sItem.isValid()){ + engine->newQObject(sItem, item); + } else { + sItem = engine->newQObject(item); + engine->globalObject().setProperty(item->patternName(),sItem); + } + foreach(BaseDesignIntf* child, item->childBaseItems()){ + baseDesignIntfToScript(child); + } + } +} + } diff --git a/limereport/lrreportrender.h b/limereport/lrreportrender.h index 89807ca..16e8176 100644 --- a/limereport/lrreportrender.h +++ b/limereport/lrreportrender.h @@ -86,6 +86,9 @@ signals: public slots: void cancelRender(); private: + + void baseDesignIntfToScript(BaseDesignIntf* item); + void renderPage(PageDesignIntf *patternPage); void initDatasources(); void initDatasource(const QString &name); diff --git a/limereport/lrscriptenginemanager.cpp b/limereport/lrscriptenginemanager.cpp index 976aa22..d24de0e 100644 --- a/limereport/lrscriptenginemanager.cpp +++ b/limereport/lrscriptenginemanager.cpp @@ -220,6 +220,28 @@ QScriptValue setVariable(QScriptContext* pcontext, QScriptEngine* /*pengine*/){ 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'; @@ -431,6 +453,8 @@ ScriptEngineManager::ScriptEngineManager() addFunction("currencyUSBasedFormat",currencyUSBasedFormat,"NUMBER","currencyUSBasedFormat(\""+tr("Value")+",\""+tr("CurrencySymbol")+"\")"); #endif addFunction("setVariable", setVariable, "GENERAL", "setVariable(\""+tr("Name")+"\",\""+tr("Value")+"\")"); + addFunction("getVariable", getVariable, "GENERAL", "getVariable(\""+tr("Name")+"\")"); + addFunction("getField", getField, "GENERAL", "getField(\""+tr("Name")+"\")"); QScriptValue colorCtor = m_scriptEngine->newFunction(constructColor); m_scriptEngine->globalObject().setProperty("QColor", colorCtor);