diff --git a/examples/KReportExampleDataSource.h b/examples/KReportExampleDataSource.h --- a/examples/KReportExampleDataSource.h +++ b/examples/KReportExampleDataSource.h @@ -21,11 +21,12 @@ #define KREPORTEXAMPLEDATA_H #include +#include #include #include #include -class KReportExampleDataSource : public KReportDataSource +class KReportExampleDataSource : public KReportDataSource, public KReportScriptSource { public: KReportExampleDataSource(); diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -93,7 +93,7 @@ Mainpage.dox Messages.sh ) - + set(kreport_INCLUDE_DIRS ${CMAKE_CURRENT_SOURCE_DIR}/common ${CMAKE_CURRENT_SOURCE_DIR}/wrtembed @@ -159,7 +159,7 @@ add_library(KReportUtilsPrivate STATIC common/KReportUtils_p.cpp ) - + kdb_create_shared_data_classes( kreport_GENERATED_SHARED_DATA_CLASS_HEADERS # output variable with list of headers NO_PREFIX # subdirectory in which the headers should be generated @@ -200,9 +200,9 @@ Qt5::Widgets PRIVATE KF5::WidgetsAddons - KF5::ConfigGui + KF5::ConfigGui ) - + ecm_setup_version(${PROJECT_VERSION} VARIABLE_PREFIX KREPORT VERSION_HEADER "${CMAKE_CURRENT_BINARY_DIR}/kreport_version.h" @@ -255,6 +255,7 @@ HEADER_NAMES KReportPageSize KReportDataSource + KReportScriptSource KReportItemBase KReportItemLine KReportPluginMetaData diff --git a/src/common/KReportDataSource.h b/src/common/KReportDataSource.h --- a/src/common/KReportDataSource.h +++ b/src/common/KReportDataSource.h @@ -47,7 +47,7 @@ void setOrder(Qt::SortOrder order); QString field() const; Qt::SortOrder order() const; - + private: class Private; Private * const d; @@ -97,27 +97,15 @@ //! @return the class name of this source virtual QString sourceClass() const; - + //! Sets the sorting for the data //! Should be called before open() so that the data source can be edited accordingly //! Default impl does nothing virtual void setSorting(const QList &sorting); //! Adds a condition to the data source virtual void addCondition(const QString &field, const QVariant &value, const QString& relation = QLatin1String("=")); - //! Utility Functions - //! @todo These are probably eligable to be moved into a new class - -#ifdef KREPORT_SCRIPTING - //! Allow the reportdata implementation to return a list of possible scripts - virtual QStringList scriptList() const; - - //! Allow the reportdata implementation to return some script code based on a specific script name - //! as set in the report - virtual QString scriptCode(const QString& script) const; -#endif - //! Return a list of data source names available for this data source //! Works after the source is opened virtual QStringList dataSourceNames() const = 0; @@ -131,7 +119,7 @@ //! @a source is implementation-specific identifier. //! Owner of the returned pointer is the caller. virtual KReportDataSource* create(const QString &source) const Q_REQUIRED_RESULT; - + private: Q_DISABLE_COPY(KReportDataSource) class Private; diff --git a/src/common/KReportDataSource.cpp b/src/common/KReportDataSource.cpp --- a/src/common/KReportDataSource.cpp +++ b/src/common/KReportDataSource.cpp @@ -20,9 +20,9 @@ #define KReportDataSortedFieldPrivateArgs(o) std::tie(o.field, o.order) -class KReportDataSource::SortedField::Private +class KReportDataSource::SortedField::Private { - + public: Private() {} Private(const Private& other) { @@ -133,19 +133,6 @@ Q_UNUSED(relation); } -#ifdef KREPORT_SCRIPTING -QStringList KReportDataSource::scriptList() const -{ - return QStringList(); -} - -QString KReportDataSource::scriptCode(const QString &script) const -{ - Q_UNUSED(script); - return QString(); -} -#endif - QStringList KReportDataSource::dataSourceNames() const { return QStringList(); diff --git a/src/common/KReportScriptSource.h b/src/common/KReportScriptSource.h new file mode 100644 --- /dev/null +++ b/src/common/KReportScriptSource.h @@ -0,0 +1,41 @@ +/* This file is part of the KDE project + * Copyright (C) 2017 by Adam Pigg (adam@piggz.co.uk) + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library. If not, see . + */ +#ifndef KREPORTSCRIPTSOURCE_H +#define KREPORTSCRIPTSOURCE_H + +#include +#include + +#include "kreport_export.h" +#include "config-kreport.h" + +/** @brief Abstraction of report script source +*/ +class KREPORT_EXPORT KReportScriptSource +{ +public: + virtual ~KReportScriptSource(){}; + + //! Allow the implementation to return a list of possible scripts + virtual QStringList scriptList() const = 0; + + //! Allow the implementation to return some script code based on a specific script name + //! as set in the report + virtual QString scriptCode(const QString& script) const = 0; +}; + +#endif diff --git a/src/renderer/KReportPreRenderer.h b/src/renderer/KReportPreRenderer.h --- a/src/renderer/KReportPreRenderer.h +++ b/src/renderer/KReportPreRenderer.h @@ -26,6 +26,7 @@ #ifdef KREPORT_SCRIPTING class KReportScriptHandler; +class KReportScriptSource; #else #define KReportScriptHandler void #endif @@ -51,6 +52,9 @@ //! Sets source data to @a data, takes ownership void setSourceData(KReportDataSource* dataSource); + //!Sets the script source to @a source, does NOT take ownership as it may be an application window + void setScriptSource(KReportScriptSource* source); + #ifdef KREPORT_SCRIPTING KReportScriptHandler *scriptHandler(); void registerScriptObject(QObject *obj, const QString &name); @@ -72,7 +76,7 @@ Q_SIGNALS: void groupChanged(const QMap &groupData); void finishedAllASyncItems(); - + private: bool setDocument(const QDomElement &document); diff --git a/src/renderer/KReportPreRenderer.cpp b/src/renderer/KReportPreRenderer.cpp --- a/src/renderer/KReportPreRenderer.cpp +++ b/src/renderer/KReportPreRenderer.cpp @@ -383,7 +383,7 @@ void KReportPreRendererPrivate::initEngine() { delete m_scriptHandler; - m_scriptHandler = new KReportScriptHandler(m_dataSource, m_reportDocument); + m_scriptHandler = new KReportScriptHandler(m_dataSource, m_scriptSource, m_reportDocument); connect(this, SIGNAL(enteredGroup(QString,QVariant)), m_scriptHandler, SLOT(slotEnteredGroup(QString,QVariant))); @@ -663,6 +663,14 @@ } } +void KReportPreRenderer::setScriptSource(KReportScriptSource *source) +{ + if (d) { + d->m_scriptSource = source; + } +} + + bool KReportPreRenderer::setDocument(const QDomElement &document) { delete d->m_document; diff --git a/src/renderer/KReportPreRenderer_p.h b/src/renderer/KReportPreRenderer_p.h --- a/src/renderer/KReportPreRenderer_p.h +++ b/src/renderer/KReportPreRenderer_p.h @@ -27,6 +27,10 @@ #include +#ifdef KREPORT_SCRIPTING +#include +#endif + namespace KReportPrivate{ class OneRecordDataSource; } @@ -66,6 +70,7 @@ #ifdef KREPORT_SCRIPTING QMap m_scriptObjects; + KReportScriptSource *m_scriptSource = nullptr; #endif void createNewPage(); diff --git a/src/renderer/scripting/KReportScriptHandler.h b/src/renderer/scripting/KReportScriptHandler.h --- a/src/renderer/scripting/KReportScriptHandler.h +++ b/src/renderer/scripting/KReportScriptHandler.h @@ -32,6 +32,7 @@ class QJSEngine; class KReportDocument; class OROPage; +class KReportScriptSource; namespace Scripting { @@ -43,7 +44,7 @@ { Q_OBJECT public: - KReportScriptHandler(const KReportDataSource *reportDataSource, KReportDocument* reportDocument); + KReportScriptHandler(const KReportDataSource *reportDataSource, KReportScriptSource *scriptSource, KReportDocument* reportDocument); ~KReportScriptHandler() override; QVariant evaluate(const QString&); diff --git a/src/renderer/scripting/KReportScriptHandler.cpp b/src/renderer/scripting/KReportScriptHandler.cpp --- a/src/renderer/scripting/KReportScriptHandler.cpp +++ b/src/renderer/scripting/KReportScriptHandler.cpp @@ -18,6 +18,8 @@ #include "KReportScriptHandler.h" +#include "KReportDataSource.h" +#include "KReportScriptSource.h" #include "KReportScriptSection.h" #include "KReportScriptDebug.h" #include "KReportScriptReport.h" @@ -33,6 +35,7 @@ #include #include #include +#include class Q_DECL_HIDDEN KReportScriptHandler::Private { @@ -44,6 +47,7 @@ KReportScriptDraw *draw; Scripting::Report *report; const KReportDataSource *reportDataSource; + const KReportScriptSource *scriptSource; QString source; KReportDocument *reportDocument; QJSEngine engine; @@ -60,14 +64,15 @@ { } -KReportScriptHandler::KReportScriptHandler(const KReportDataSource* reportDataSource, KReportDocument* reportDocument) : d(new Private()) +KReportScriptHandler::KReportScriptHandler(const KReportDataSource* reportDataSource, KReportScriptSource* scriptSource, KReportDocument* reportDocument) : d(new Private()) { d->reportDocument = reportDocument; d->reportDataSource = reportDataSource; + d->scriptSource = scriptSource; //Add a general report object d->report = new Scripting::Report(d->reportDocument); - + registerScriptObject(d->constants, QLatin1String("constants")); registerScriptObject(d->debug, QLatin1String("debug")); registerScriptObject(d->draw, QLatin1String("draw")); @@ -86,13 +91,25 @@ //kreportDebug() << "Added" << d->sectionMap[sec]->objectName() << "to report" << d->reportData->name(); } + QJSValueIterator it(d->engine.globalObject()); + qDebug() << "Script properties:"; + while (it.hasNext()) { + it.next(); + qDebug() << it.name() << ": " << it.value().toString(); + } + + //kreportDebug() << "Report name is" << d->reportData->name(); } bool KReportScriptHandler::trigger() { - QString code = d->reportDataSource->scriptCode(d->reportDocument->script()); - //kreportDebug() << code; + QString code; + if (d->scriptSource) { + code = d->scriptSource->scriptCode(d->reportDocument->script()); + } + + kreportDebug() << "Code:" << code; if (code.isEmpty()) { return true; @@ -154,7 +171,7 @@ if (!result.isError()) { return result.toVariant(); } else { - QMessageBox::warning(nullptr, tr("Script Error"), d->scriptValue.toString()); + QMessageBox::warning(nullptr, tr("Script Error"), tr("Cannot evaluate:\n%1\n\nError:\n%2").arg(code,d->scriptValue.toString())); } } return QVariant(); diff --git a/src/wrtembed/KReportDesigner.h b/src/wrtembed/KReportDesigner.h --- a/src/wrtembed/KReportDesigner.h +++ b/src/wrtembed/KReportDesigner.h @@ -41,6 +41,10 @@ class KReportDesignerSectionView; class QAction; +#ifdef KREPORT_SCRIPTING +class KReportScriptSource; +#endif + // // Class ReportDesigner // The ReportDesigner is the main widget for designing a report @@ -71,10 +75,19 @@ @brief Sets the report data The report data interface contains functions to retrieve data and information about the fields. - @param kodata Pointer to KReportDataSource instance, ownership is transferred + @param source Pointer to KReportDataSource instance, ownership is transferred */ void setDataSource(KReportDataSource* source); +#ifdef KREPORT_SCRIPTING + /** + @brief Sets the script source for the designer + The script source contains function to return scripts supplied by the parent application + @param source Pointer to KReportScriptSource instance, ownership is NOT transferred as it may be an application window + */ + void setScriptSource(KReportScriptSource *source); +#endif + /** @brief Return a pointer to the reports data @return Pointer to report data @@ -299,8 +312,6 @@ void slotLowerSelected(); private: - void init(); - /** @brief Sets the detail section to the given section */ diff --git a/src/wrtembed/KReportDesigner.cpp b/src/wrtembed/KReportDesigner.cpp --- a/src/wrtembed/KReportDesigner.cpp +++ b/src/wrtembed/KReportDesigner.cpp @@ -36,6 +36,9 @@ #include "KReportSection.h" #include "KReportPluginMetaData.h" #include "kreport_debug.h" +#ifdef KREPORT_SCRIPTING +#include "KReportScriptSource.h" +#endif #include #include @@ -180,6 +183,9 @@ QString originalScript; //Value of the script at load time KReportDataSource *dataSource = nullptr; +#ifdef KREPORT_SCRIPTING + KReportScriptSource *scriptSource = nullptr; +#endif }; KReportDesigner::KReportDesigner(QWidget * parent) @@ -436,6 +442,7 @@ void KReportDesigner::setDataSource(KReportDataSource* source) { + qDebug() << "Data source" << source; if (d->dataSource == source) { return; } @@ -447,6 +454,13 @@ emit reportDataChanged(); } +#ifdef KREPORT_SCRIPTING +void KReportDesigner::setScriptSource(KReportScriptSource* source) +{ + d->scriptSource = source; +} +#endif + KReportDesignerSection * KReportDesigner::section(KReportSectionData::Section s) const { KReportDesignerSection *sec; @@ -789,9 +803,11 @@ void KReportDesigner::slotPageButton_Pressed() { + qDebug() << "Page button pressed"; #ifdef KREPORT_SCRIPTING - if (d->dataSource) { - QStringList sl = d->dataSource->scriptList(); + if (d->scriptSource) { + qDebug() << "Script source is set"; + QStringList sl = d->scriptSource->scriptList(); sl.prepend(QLatin1String("")); d->script->setListData(sl, sl); }