From 5c3c032ea37fb6715170da77c0cd8ad3e1482c61 Mon Sep 17 00:00:00 2001 From: Arin Alexander Date: Fri, 1 Sep 2017 02:02:51 +0300 Subject: [PATCH] Changing the language from the script has been added --- .../change_lang_from_script.lrxml | 127 ++++++++++++++++++ limereport/lrreportdesignwidget.cpp | 1 + limereport/lrreportengine.cpp | 24 +++- limereport/lrreportengine_p.h | 2 + limereport/lrscriptenginemanager.cpp | 97 ++++++++++++- limereport/lrscriptenginemanager.h | 54 +++++++- 6 files changed, 300 insertions(+), 5 deletions(-) create mode 100644 demo_r1/demo_reports/change_lang_from_script.lrxml diff --git a/demo_r1/demo_reports/change_lang_from_script.lrxml b/demo_r1/demo_reports/change_lang_from_script.lrxml new file mode 100644 index 0000000..a5d3704 --- /dev/null +++ b/demo_r1/demo_reports/change_lang_from_script.lrxml @@ -0,0 +1,127 @@ + + + + + + + page1 + + + + + + + + Reportpage1 + + + + TextItem1 + + + + + Reportpage1 + + + + + + + Test + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + datasources + + + + + + + + + + + + Dialog + + + + function OnAccept(){ + engine.setReportTranslation(Dialog.comboBox.currentText); +} +Dialog.accepted.connect(OnAccept); + +var comboBox = LimeReport.createWrapper(Dialog.comboBox); +comboBox.addItems(engine.aviableReportTranslations()); + +Dialog.exec() == 1; + + + + + + + + + + + + + + + + + + + + + diff --git a/limereport/lrreportdesignwidget.cpp b/limereport/lrreportdesignwidget.cpp index 84a9a7e..9472f13 100644 --- a/limereport/lrreportdesignwidget.cpp +++ b/limereport/lrreportdesignwidget.cpp @@ -268,6 +268,7 @@ void ReportDesignWidget::createNewDialogTab(const QString& dialogName, const QBy int pageIndex = m_tabWidget->addTab(dialogDesigner,QIcon(),dialogName); m_tabWidget->setTabWhatsThis(pageIndex,"dialog"); m_tabWidget->setCurrentIndex(pageIndex); + m_dialogDesignerManager->setActiveEditor(dialogDesigner); } DialogDesigner*ReportDesignWidget::activeDialogPage() diff --git a/limereport/lrreportengine.cpp b/limereport/lrreportengine.cpp index e582fc2..bc3f626 100644 --- a/limereport/lrreportengine.cpp +++ b/limereport/lrreportengine.cpp @@ -304,6 +304,24 @@ void ReportEnginePrivate::printReport(ReportPages pages, QPrinter &printer) delete painter; } +QStringList ReportEnginePrivate::aviableReportTranslations() +{ + QStringList result; + foreach (QLocale::Language language, aviableLanguages()){ + result << QLocale::languageToString(language); + } + return result; +} + +void ReportEnginePrivate::setReportTranslation(const QString &languageName) +{ + foreach(QLocale::Language language, aviableLanguages()){ + if (QLocale::languageToString(language).compare(languageName) == 0){ + setReportLanguage(language); + } + } +}; + bool ReportEnginePrivate::printReport(QPrinter* printer) { if (!printer&&!m_printerSelected){ @@ -892,7 +910,7 @@ ReportPages ReportEnginePrivate::renderToPages() dataManager()->connectAllDatabases(); dataManager()->setDesignTime(false); dataManager()->updateDatasourceModel(); - activateLanguage(m_reportLanguage); + connect(m_reportRender.data(),SIGNAL(pageRendered(int)), this, SIGNAL(renderPageFinished(int))); @@ -910,7 +928,11 @@ ReportPages ReportEnginePrivate::renderToPages() scriptContext()->baseDesignIntfToScript(page->pageItem()->objectName(), page->pageItem()); } + scriptContext()->qobjectToScript("engine",this); + if (m_scriptEngineContext->runInitScript()){ + + activateLanguage(m_reportLanguage); emit renderStarted(); foreach(PageDesignIntf* page , m_pages){ diff --git a/limereport/lrreportengine_p.h b/limereport/lrreportengine_p.h index 5167bc6..4bda29d 100644 --- a/limereport/lrreportengine_p.h +++ b/limereport/lrreportengine_p.h @@ -67,6 +67,8 @@ class ReportEnginePrivate : public QObject, public ICollectionContainer, public public: static void printReport(ItemsReaderIntf::Ptr reader, QPrinter &printer); static void printReport(ReportPages pages, QPrinter &printer); + Q_INVOKABLE QStringList aviableReportTranslations(); + Q_INVOKABLE void setReportTranslation(const QString& languageName); public: explicit ReportEnginePrivate(QObject *parent = 0); virtual ~ReportEnginePrivate(); diff --git a/limereport/lrscriptenginemanager.cpp b/limereport/lrscriptenginemanager.cpp index 9fc11f3..6ee0689 100644 --- a/limereport/lrscriptenginemanager.cpp +++ b/limereport/lrscriptenginemanager.cpp @@ -819,6 +819,10 @@ ScriptEngineManager::ScriptEngineManager() m_scriptEngine = new ScriptEngineType; m_functionManager = new ScriptFunctionsManager(this); m_functionManager->setScriptEngineManager(this); +#ifndef USE_QJSENGINE + m_scriptEngine->setDefaultPrototype(qMetaTypeId(), + m_scriptEngine->newQObject(new ComboBoxPrototype())); +#endif createLineFunction(); createNumberFomatFunction(); @@ -1238,7 +1242,6 @@ void ScriptEngineContext::baseDesignIntfToScript(const QString& pageName, BaseDe ScriptEngineType* engine = ScriptEngineManager::instance().scriptEngine(); #ifdef USE_QJSENGINE - //sItem = engine->newQObject(item); ScriptValueType sItem = getCppOwnedJSValue(*engine, item); engine->globalObject().setProperty(pageName+"_"+item->patternName(), sItem); #else @@ -1247,7 +1250,7 @@ void ScriptEngineContext::baseDesignIntfToScript(const QString& pageName, BaseDe engine->newQObject(sItem, item); } else { sItem = engine->newQObject(item); - engine->globalObject().setProperty(item->patternName(),sItem); + engine->globalObject().setProperty(pageName+"_"+item->patternName(),sItem); } #endif foreach(BaseDesignIntf* child, item->childBaseItems()){ @@ -1256,6 +1259,23 @@ void ScriptEngineContext::baseDesignIntfToScript(const QString& pageName, BaseDe } } +void ScriptEngineContext::qobjectToScript(const QString& name, QObject *item) +{ + ScriptEngineType* engine = ScriptEngineManager::instance().scriptEngine(); +#ifdef USE_QJSENGINE + ScriptValueType sItem = getCppOwnedJSValue(*engine, item); + engine->globalObject().setProperty(name, sItem); +#else + ScriptValueType sItem = engine->globalObject().property(name); + if (sItem.isValid()){ + engine->newQObject(sItem, item); + } else { + sItem = engine->newQObject(item); + engine->globalObject().setProperty(name,sItem); + } +#endif +} + #ifdef HAVE_UI_LOADER #ifdef USE_QJSENGINE @@ -1502,7 +1522,10 @@ void ScriptFunctionsManager::clearTableOfContens() scriptEngineManager()->clearTableOfContens(); } + + #ifdef USE_QJSENGINE + QFont ScriptFunctionsManager::font(const QString &family, int pointSize, bool bold, bool italic, bool underLine) { QFont result (family, pointSize); @@ -1511,6 +1534,45 @@ QFont ScriptFunctionsManager::font(const QString &family, int pointSize, bool bo result.setUnderline(underLine); return result; } + +void ScriptFunctionsManager::addItemsToComboBox(QJSValue object, const QStringList &values) +{ + QComboBox* comboBox = dynamic_cast(object.toQObject()); + if (comboBox){ + comboBox->addItems(values); + } +} + +void ScriptFunctionsManager::addItemToComboBox(QJSValue object, const QString &value) +{ + QComboBox* comboBox = dynamic_cast(object.toQObject()); + if (comboBox){ + comboBox->addItem(value); + } +} + +QJSValue ScriptFunctionsManager::createComboBoxWrapper(QJSValue comboBox) +{ + QComboBox* item = dynamic_cast(comboBox.toQObject()); + if (item){ + ComboBoxWrapper* wrapper = new ComboBoxWrapper(item); + return m_scriptEngineManager->scriptEngine()->newQObject(wrapper); + } + return QJSValue(); +} + +QJSValue ScriptFunctionsManager::createWrapper(QJSValue item) +{ + QObject* object = item.toQObject(); + if (object){ + IWrapperCreator* wrapper = m_wrappersFactory.value(object->metaObject()->className()); + if (wrapper){ + return m_scriptEngineManager->scriptEngine()->newQObject(wrapper->createWrapper(item.toQObject())); + } + } + return QJSValue(); +} + #endif QFont ScriptFunctionsManager::font(QVariantMap params){ if (!params.contains("family")){ @@ -1606,5 +1668,36 @@ void LimeReport::TableOfContens::clear(){ } +#ifdef USE_QJSENGINE + +QObject* ComboBoxWrapperCreator::createWrapper(QObject *item) +{ + QComboBox* comboBox = dynamic_cast(item); + if (comboBox){ + return new ComboBoxWrapper(comboBox); + } + return 0; +} + +#endif + +#ifndef USE_QJSENGINE +void ComboBoxPrototype::addItem(const QString &text) +{ + QComboBox* comboBox = qscriptvalue_cast(thisObject()); + if (comboBox){ + comboBox->addItem(text); + } +} + +void ComboBoxPrototype::addItems(const QStringList &texts) +{ + QComboBox* comboBox = qscriptvalue_cast(thisObject()); + if (comboBox){ + comboBox->addItems(texts); + } +} +#endif + } //namespace LimeReport diff --git a/limereport/lrscriptenginemanager.h b/limereport/lrscriptenginemanager.h index 717a693..162a21e 100644 --- a/limereport/lrscriptenginemanager.h +++ b/limereport/lrscriptenginemanager.h @@ -38,7 +38,7 @@ #include #include #include - +#include //#include @@ -183,6 +183,7 @@ public: void initDialogs(); #endif void baseDesignIntfToScript(const QString& pageName, BaseDesignIntf *item); + void qobjectToScript(const QString &name, QObject* item); void clear(); QString initScript() const; void setInitScript(const QString& initScript); @@ -262,10 +263,48 @@ private: QString m_scriptWrapper; }; + +#ifndef USE_QJSENGINE +class ComboBoxPrototype : public QObject, public QScriptable{ + Q_OBJECT +public: + ComboBoxPrototype(QObject* parent = 0):QObject(parent){} +public slots: + void addItem( const QString& text); + void addItems(const QStringList& texts); +}; +#endif + +#ifdef USE_QJSENGINE + +class IWrapperCreator{ +public: + virtual QObject* createWrapper(QObject* item) = 0; + virtual ~IWrapperCreator(){} +}; + +class ComboBoxWrapper : public QObject{ + Q_OBJECT +public: + ComboBoxWrapper(QComboBox* comboBox, QObject* parent = 0) : QObject(parent), m_comboBox(comboBox){} + Q_INVOKABLE void addItems(const QStringList& texts){ m_comboBox->addItems(texts);} + Q_INVOKABLE void addItem(const QString& text){ m_comboBox->addItem(text);} +private: + QComboBox* m_comboBox; +}; + +class ComboBoxWrapperCreator: public IWrapperCreator{ +private: + QObject* createWrapper(QObject* item); +}; + +#endif + class ScriptFunctionsManager : public QObject{ Q_OBJECT public: - explicit ScriptFunctionsManager(QObject* parent = 0):QObject(parent){} + explicit ScriptFunctionsManager(QObject* parent = 0):QObject(parent){ m_wrappersFactory.insert("QComboBox",new ComboBoxWrapperCreator());} + ~ScriptFunctionsManager(){ foreach(IWrapperCreator* wrapper, m_wrappersFactory.values()){ delete wrapper;} m_wrappersFactory.clear();} 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); @@ -283,8 +322,13 @@ public: Q_INVOKABLE QVariant color(const QString& color){ return QColor(color);} Q_INVOKABLE void addTableOfContensItem(const QString& uniqKey, const QString& content, int indent = 0); Q_INVOKABLE void clearTableOfContens(); + #ifdef USE_QJSENGINE Q_INVOKABLE QFont font(const QString& family, int pointSize = -1, bool bold = false, bool italic = false, bool underLine = false); + Q_INVOKABLE void addItemsToComboBox(QJSValue object, const QStringList& values); + Q_INVOKABLE void addItemToComboBox(QJSValue object, const QString& value); + Q_INVOKABLE QJSValue createComboBoxWrapper(QJSValue comboBox); + Q_INVOKABLE QJSValue createWrapper(QJSValue item); #endif Q_INVOKABLE QFont font(QVariantMap params); ScriptEngineManager *scriptEngineManager() const; @@ -292,6 +336,7 @@ public: static QColor createQColor(const QString& color){ return QColor(color);} private: ScriptEngineManager* m_scriptEngineManager; + QMap m_wrappersFactory; }; class ScriptEngineManager : public QObject, public Singleton, public IScriptEngineManager @@ -435,4 +480,9 @@ public: }; } + +#ifndef USE_QJSENGINE +Q_DECLARE_METATYPE(LimeReport::ComboBoxPrototype*); +#endif + #endif // LRSCRIPTENGINEMANAGER_H