From 5f56f36a4925d9fa71f8c83083f67f6e6f2d4c98 Mon Sep 17 00:00:00 2001 From: Arin Alexander Date: Wed, 6 Mar 2019 22:16:30 +0300 Subject: [PATCH] addBookmark() && findPageIndexByBookmark() have been added to the script --- limereport/lrreportengine.cpp | 1 + limereport/lrscriptenginemanager.cpp | 79 ++++++++++++++++++++++++++-- limereport/lrscriptenginemanager.h | 14 ++++- 3 files changed, 90 insertions(+), 4 deletions(-) diff --git a/limereport/lrreportengine.cpp b/limereport/lrreportengine.cpp index b07c607..7cd13a4 100644 --- a/limereport/lrreportengine.cpp +++ b/limereport/lrreportengine.cpp @@ -1191,6 +1191,7 @@ ReportPages ReportEnginePrivate::renderToPages() activateLanguage(m_reportLanguage); emit renderStarted(); + m_scriptEngineContext->setReportPages(&result); for(int i = 0; i < m_renderingPages.count(); ++i){ PageItemDesignIntf* page = m_renderingPages.at(i); diff --git a/limereport/lrscriptenginemanager.cpp b/limereport/lrscriptenginemanager.cpp index 0f3aa1a..8a8b1d1 100644 --- a/limereport/lrscriptenginemanager.cpp +++ b/limereport/lrscriptenginemanager.cpp @@ -545,18 +545,39 @@ QVariant ScriptEngineManager::evaluateScript(const QString& script){ return QVariant(); } -void ScriptEngineManager::addTableOfContentsItem(const QString& uniqKey, const QString& content, int indent) -{ +void ScriptEngineManager::addBookMark(const QString& uniqKey, const QString& content){ Q_ASSERT(m_context != 0); if (m_context){ BandDesignIntf* currentBand = m_context->getCurrentBand(); - m_context->tableOfContents()->setItem(uniqKey, content, 0, indent); if (currentBand) currentBand->addBookmark(uniqKey, content); else if (m_context->getCurrentPage()) { m_context->getCurrentPage()->addBookmark(uniqKey, content); } } + +} + +int ScriptEngineManager::findPageIndexByBookmark(const QString &uniqKey) +{ + for (int i=0; i < m_context->reportPages()->size(); ++i){ + if (m_context->reportPages()->at(i)->bookmarks().contains(uniqKey)) + return i+1; + foreach(BandDesignIntf* band, m_context->reportPages()->at(i)->bands()){ + if (band->bookmarks().contains(uniqKey)) + return i+1; + } + } + return -1; +} + +void ScriptEngineManager::addTableOfContentsItem(const QString& uniqKey, const QString& content, int indent) +{ + Q_ASSERT(m_context != 0); + if (m_context){ + m_context->tableOfContents()->setItem(uniqKey, content, 0, indent); + addBookMark(uniqKey, content); + } } void ScriptEngineManager::clearTableOfContents(){ @@ -835,6 +856,36 @@ bool ScriptEngineManager::createGetFieldByRowIndex() return addFunction(fd); } +bool ScriptEngineManager::createAddBookmarkFunction() +{ + JSFunctionDesc fd; + fd.setManager(m_functionManager); + fd.setManagerName(LimeReport::Const::FUNCTION_MANAGER_NAME); + fd.setCategory(tr("GENERAL")); + fd.setName("addBookmark"); + fd.setDescription("addBookmark(\""+tr("Unique identifier")+" \""+tr("Content")+"\")"); + fd.setScriptWrapper(QString("function addBookmark(uniqKey, content){" + "return %1.addBookmark(uniqKey, content);}" + ).arg(LimeReport::Const::FUNCTION_MANAGER_NAME) + ); + return addFunction(fd); +} + +bool ScriptEngineManager::createFindPageIndexByBookmark() +{ + JSFunctionDesc fd; + fd.setManager(m_functionManager); + fd.setManagerName(LimeReport::Const::FUNCTION_MANAGER_NAME); + fd.setCategory(tr("GENERAL")); + fd.setName("findPageIndexByBookmark"); + fd.setDescription("findPageIndexByBookmark(\""+tr("Unique identifier")+"\")"); + fd.setScriptWrapper(QString("function findPageIndexByBookmark(uniqKey){" + "return %1.findPageIndexByBookmark(uniqKey);}" + ).arg(LimeReport::Const::FUNCTION_MANAGER_NAME) + ); + return addFunction(fd); +} + bool ScriptEngineManager::createAddTableOfContentsItemFunction() { JSFunctionDesc fd; @@ -916,6 +967,8 @@ ScriptEngineManager::ScriptEngineManager() QScriptValue fontConstructor = m_scriptEngine->newFunction(QFontPrototype::constructorQFont, fontProto); m_scriptEngine->globalObject().setProperty("QFont", fontConstructor); #endif + createAddBookmarkFunction(); + createFindPageIndexByBookmark(); createAddTableOfContentsItemFunction(); createClearTableOfContentsFunction(); createReopenDatasourceFunction(); @@ -1204,6 +1257,16 @@ void ScriptEngineContext::collectionLoadFinished(const QString& collectionName) Q_UNUSED(collectionName); } +ReportPages* ScriptEngineContext::reportPages() const +{ + return m_reportPages; +} + +void ScriptEngineContext::setReportPages(ReportPages *value) +{ + m_reportPages = value; +} + #ifdef HAVE_UI_LOADER QDialog* ScriptEngineContext::createDialog(DialogDescriber* cont) { @@ -1622,6 +1685,16 @@ void ScriptFunctionsManager::reopenDatasource(const QString& datasourceName) return dm->reopenDatasource(datasourceName); } +void ScriptFunctionsManager::addBookmark(const QString &uniqKey, const QString &content) +{ + scriptEngineManager()->addBookMark(uniqKey, content); +} + +int ScriptFunctionsManager::findPageIndexByBookmark(const QString &uniqKey) +{ + return scriptEngineManager()->findPageIndexByBookmark(uniqKey); +} + void ScriptFunctionsManager::addTableOfContentsItem(const QString& uniqKey, const QString& content, int indent) { scriptEngineManager()->addTableOfContentsItem(uniqKey, content, indent); diff --git a/limereport/lrscriptenginemanager.h b/limereport/lrscriptenginemanager.h index bcddffb..760ffc0 100644 --- a/limereport/lrscriptenginemanager.h +++ b/limereport/lrscriptenginemanager.h @@ -165,6 +165,8 @@ private : QByteArray m_description; }; +typedef QList< QSharedPointer > ReportPages; + class ScriptEngineContext : public QObject, public ICollectionContainer { Q_OBJECT @@ -204,7 +206,9 @@ public: void setTableOfContents(TableOfContents* tableOfContents); void dropChanges(){ m_hasChanges = false;} bool hasChanges(){ return m_hasChanges;} -#ifdef HAVE_UI_LOADER + ReportPages* reportPages() const; + void setReportPages(ReportPages* value); +#ifdef HAVE_UI_LOADER signals: void dialogNameChanged(QString dialogName); void dialogDeleted(QString dialogName); @@ -231,6 +235,7 @@ private: PageItemDesignIntf* m_currentPage; TableOfContents* m_tableOfContents; bool m_hasChanges; + ReportPages* m_reportPages; }; class JSFunctionDesc{ @@ -366,6 +371,8 @@ public: Q_INVOKABLE QVariant getFieldByRowIndex(const QString& fieldName, int rowIndex); Q_INVOKABLE void reopenDatasource(const QString& datasourceName); Q_INVOKABLE QVariant color(const QString& color){ return QColor(color);} + Q_INVOKABLE void addBookmark(const QString& uniqKey, const QString& content); + Q_INVOKABLE int findPageIndexByBookmark(const QString &uniqKey); Q_INVOKABLE void addTableOfContentsItem(const QString& uniqKey, const QString& content, int indent = 0); Q_INVOKABLE void clearTableOfContents(); Q_INVOKABLE QFont font(const QString& family, int pointSize = -1, bool bold = false, bool italic = false, bool underLine = false); @@ -417,9 +424,12 @@ public: QString expandDataFields(QString context, ExpandType expandType, QVariant &varValue, QObject* reportItem); QString expandScripts(QString context, QVariant &varValue, QObject* reportItem); QVariant evaluateScript(const QString &script); + void addBookMark(const QString &uniqKey, const QString &content); + int findPageIndexByBookmark(const QString& uniqKey); void addTableOfContentsItem(const QString& uniqKey, const QString& content, int indent); void clearTableOfContents(); ScriptValueType moveQObjectToScript(QObject* object, const QString objectName); + protected: void updateModel(); bool containsFunction(const QString &functionName); @@ -440,6 +450,8 @@ private: bool createGetFieldFunction(); bool createGetFieldByKeyFunction(); bool createGetFieldByRowIndex(); + bool createAddBookmarkFunction(); + bool createFindPageIndexByBookmark(); bool createAddTableOfContentsItemFunction(); bool createClearTableOfContentsFunction(); bool createReopenDatasourceFunction();