[prev in list] [next in list] [prev in thread] [next in thread]
List: kde-commits
Subject: [kreport] /: Move script sourcing to its own class, with associated changes
From: Adam Pigg <null () kde ! org>
Date: 2017-06-01 20:44:53
Message-ID: E1dGWxh-00036F-9J () code ! kde ! org
[Download RAW message or body]
Git commit c1f6e2ae244a3d7beaa7c5bb6fd20f6d0d244e21 by Adam Pigg.
Committed on 01/06/2017 at 20:44.
Pushed by piggz into branch 'master'.
Move script sourcing to its own class, with associated changes
Summary: Move script sources out of KReportData, into its own interface
Test Plan: kreportexample still runs. With associated changes to kexi, scirpting \
works in reports
Reviewers: staniek
Reviewed By: staniek
Tags: #kreport
Differential Revision: https://phabricator.kde.org/D6036
M +2 -1 examples/KReportExampleDataSource.h
M +5 -4 src/CMakeLists.txt
M +2 -15 src/common/KReportDataSource.cpp
M +3 -15 src/common/KReportDataSource.h
A +41 -0 src/common/KReportScriptSource.h [License: LGPL (v2.1+)]
M +9 -1 src/renderer/KReportPreRenderer.cpp
M +5 -1 src/renderer/KReportPreRenderer.h
M +5 -0 src/renderer/KReportPreRenderer_p.h
M +28 -7 src/renderer/scripting/KReportScriptHandler.cpp
M +2 -1 src/renderer/scripting/KReportScriptHandler.h
M +16 -3 src/wrtembed/KReportDesigner.cpp
M +14 -3 src/wrtembed/KReportDesigner.h
https://commits.kde.org/kreport/c1f6e2ae244a3d7beaa7c5bb6fd20f6d0d244e21
diff --git a/examples/KReportExampleDataSource.h \
b/examples/KReportExampleDataSource.h index 9c06325..2671cb3 100644
--- a/examples/KReportExampleDataSource.h
+++ b/examples/KReportExampleDataSource.h
@@ -21,11 +21,12 @@
#define KREPORTEXAMPLEDATA_H
#include <KReportDataSource>
+#include <KReportScriptSource>
#include <QVariant>
#include <QStringList>
#include <QList>
-class KReportExampleDataSource : public KReportDataSource
+class KReportExampleDataSource : public KReportDataSource, public \
KReportScriptSource {
public:
KReportExampleDataSource();
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index 90275c5..9d849b0 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -93,7 +93,7 @@ set(kreport_LIB_SRCS
Mainpage.dox
Messages.sh
)
-
+
set(kreport_INCLUDE_DIRS
${CMAKE_CURRENT_SOURCE_DIR}/common
${CMAKE_CURRENT_SOURCE_DIR}/wrtembed
@@ -159,7 +159,7 @@ set_coinstallable_lib_version(KReport)
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 @@ target_link_libraries(KReportUtilsPrivate
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 @@ ecm_generate_headers(kreport_FORWARDING_HEADERS
HEADER_NAMES
KReportPageSize
KReportDataSource
+ KReportScriptSource
KReportItemBase
KReportItemLine
KReportPluginMetaData
diff --git a/src/common/KReportDataSource.cpp b/src/common/KReportDataSource.cpp
index ab5e1ca..9e4f829 100644
--- 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 @@ void KReportDataSource::addCondition(const QString &field, const \
QVariant &value 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/KReportDataSource.h b/src/common/KReportDataSource.h
index 8948471..01504fe 100644
--- a/src/common/KReportDataSource.h
+++ b/src/common/KReportDataSource.h
@@ -47,7 +47,7 @@ public:
void setOrder(Qt::SortOrder order);
QString field() const;
Qt::SortOrder order() const;
-
+
private:
class Private;
Private * const d;
@@ -97,7 +97,7 @@ public:
//! @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
@@ -106,18 +106,6 @@ public:
//! 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 @@ public:
//! @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/KReportScriptSource.h b/src/common/KReportScriptSource.h
new file mode 100644
index 0000000..613111f
--- /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 <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef KREPORTSCRIPTSOURCE_H
+#define KREPORTSCRIPTSOURCE_H
+
+#include <QStringList>
+#include <QString>
+
+#include "kreport_export.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.cpp \
b/src/renderer/KReportPreRenderer.cpp index f9da940..442791d 100644
--- a/src/renderer/KReportPreRenderer.cpp
+++ b/src/renderer/KReportPreRenderer.cpp
@@ -383,7 +383,7 @@ qreal KReportPreRendererPrivate::renderSection(const \
KReportSectionData & sectio void KReportPreRendererPrivate::initEngine()
{
delete m_scriptHandler;
- m_scriptHandler = new KReportScriptHandler(m_dataSource, m_reportDocument);
+ m_scriptHandler = new KReportScriptHandler(m_dataSource, scriptSource, \
m_reportDocument);
connect(this, SIGNAL(enteredGroup(QString,QVariant)), m_scriptHandler, \
SLOT(slotEnteredGroup(QString,QVariant)));
@@ -663,6 +663,14 @@ void KReportPreRenderer::setSourceData(KReportDataSource \
*dataSource) }
}
+void KReportPreRenderer::setScriptSource(KReportScriptSource *source)
+{
+ if (d) {
+ d->scriptSource = source;
+ }
+}
+
+
bool KReportPreRenderer::setDocument(const QDomElement &document)
{
delete d->m_document;
diff --git a/src/renderer/KReportPreRenderer.h b/src/renderer/KReportPreRenderer.h
index 3ec256b..262175c 100644
--- 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 @@ public:
//! 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 @@ public:
Q_SIGNALS:
void groupChanged(const QMap<QString, QVariant> &groupData);
void finishedAllASyncItems();
-
+
private:
bool setDocument(const QDomElement &document);
diff --git a/src/renderer/KReportPreRenderer_p.h \
b/src/renderer/KReportPreRenderer_p.h index b39168e..f616fee 100644
--- a/src/renderer/KReportPreRenderer_p.h
+++ b/src/renderer/KReportPreRenderer_p.h
@@ -27,6 +27,10 @@
#include <QMap>
+#ifdef KREPORT_SCRIPTING
+#include <KReportScriptSource>
+#endif
+
namespace KReportPrivate{
class OneRecordDataSource;
}
@@ -66,6 +70,7 @@ public:
#ifdef KREPORT_SCRIPTING
QMap<QString, QObject*> m_scriptObjects;
+ KReportScriptSource *scriptSource = nullptr;
#endif
void createNewPage();
diff --git a/src/renderer/scripting/KReportScriptHandler.cpp \
b/src/renderer/scripting/KReportScriptHandler.cpp index b7493fd..0d113e9 100644
--- 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"
@@ -34,6 +36,12 @@
#include <QJSEngine>
#include <QJSValue>
+
+//Note#: this is here to save creating a new file for this interface
+KReportScriptSource::~KReportScriptSource()
+{
+}
+
class Q_DECL_HIDDEN KReportScriptHandler::Private
{
public:
@@ -44,12 +52,14 @@ public:
KReportScriptDraw *draw;
Scripting::Report *report;
const KReportDataSource *reportDataSource;
+ const KReportScriptSource *scriptSource;
QString source;
KReportDocument *reportDocument;
QJSEngine engine;
QJSValue scriptValue;
QMap<QString, QVariant> groups;
QMap<KReportSectionData*, Scripting::Section*> sectionMap;
+ bool suppressEvaluateErrors = false;
};
KReportScriptHandler::Private::Private() : constants(new KReportScriptConstants), \
debug(new KReportScriptDebug), draw(new KReportScriptDraw) @@ -60,14 +70,15 @@ \
KReportScriptHandler::Private::~Private() {
}
-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"));
@@ -85,14 +96,14 @@ KReportScriptHandler::KReportScriptHandler(const \
KReportDataSource* reportDataSo r.setProperty(d->sectionMap[sec]->objectName(), s);
//kreportDebug() << "Added" << d->sectionMap[sec]->objectName() << "to \
report" << d->reportData->name(); }
-
- //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());
+ }
if (code.isEmpty()) {
return true;
@@ -154,7 +165,17 @@ QVariant KReportScriptHandler::evaluate(const QString &code)
if (!result.isError()) {
return result.toVariant();
} else {
- QMessageBox::warning(nullptr, tr("Script Error"), \
d->scriptValue.toString()); + if (!d->suppressEvaluateErrors) {
+ QMessageBox msgBox;
+ msgBox.setText(tr("Cannot evaluate script. Error: %1\n\nDo you want \
to suppress further messages?\n(messages will be restored next time the report is \
opened)").arg(d->scriptValue.toString())); + \
msgBox.setDetailedText(tr("Script code:\n%1").arg(code)); + \
msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No); + \
msgBox.setDefaultButton(QMessageBox::No); + int ret = msgBox.exec();
+ if (ret == QMessageBox::Yes) {
+ d->suppressEvaluateErrors = true;
+ }
+ }
}
}
return QVariant();
diff --git a/src/renderer/scripting/KReportScriptHandler.h \
b/src/renderer/scripting/KReportScriptHandler.h index b33efe0..c693473 100644
--- a/src/renderer/scripting/KReportScriptHandler.h
+++ b/src/renderer/scripting/KReportScriptHandler.h
@@ -32,6 +32,7 @@ class KReportSectionData;
class QJSEngine;
class KReportDocument;
class OROPage;
+class KReportScriptSource;
namespace Scripting
{
@@ -43,7 +44,7 @@ class KREPORT_EXPORT KReportScriptHandler : public QObject
{
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/wrtembed/KReportDesigner.cpp b/src/wrtembed/KReportDesigner.cpp
index f70981a..16de778 100644
--- 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 <KStandardShortcut>
#include <KStandardGuiItem>
@@ -180,6 +183,9 @@ public:
QString originalScript; //Value of the script at load time
KReportDataSource *dataSource = nullptr;
+#ifdef KREPORT_SCRIPTING
+ KReportScriptSource *scriptSource = nullptr;
+#endif
};
KReportDesigner::KReportDesigner(QWidget * parent)
@@ -447,6 +453,13 @@ void KReportDesigner::setDataSource(KReportDataSource* source)
emit reportDataChanged();
}
+#ifdef KREPORT_SCRIPTING
+void KReportDesigner::setScriptSource(KReportScriptSource* source)
+{
+ d->scriptSource = source;
+}
+#endif
+
KReportDesignerSection * KReportDesigner::section(KReportSectionData::Section s) \
const {
KReportDesignerSection *sec;
@@ -790,9 +803,9 @@ void KReportDesigner::slotPropertyChanged(KPropertySet &s, \
KProperty &p) void KReportDesigner::slotPageButton_Pressed()
{
#ifdef KREPORT_SCRIPTING
- if (d->dataSource) {
- QStringList sl = d->dataSource->scriptList();
- sl.prepend(QLatin1String(""));
+ if (d->scriptSource) {
+ QStringList sl = d->scriptSource->scriptList();
+ sl.prepend(QString());
d->script->setListData(sl, sl);
}
changeSet(&d->set);
diff --git a/src/wrtembed/KReportDesigner.h b/src/wrtembed/KReportDesigner.h
index d2b31be..fee140b 100644
--- a/src/wrtembed/KReportDesigner.h
+++ b/src/wrtembed/KReportDesigner.h
@@ -41,6 +41,10 @@ class KReportDesignerSectionScene;
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 @@ public:
@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 @@ public Q_SLOTS:
void slotLowerSelected();
private:
- void init();
-
/**
@brief Sets the detail section to the given section
*/
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic