KReport

KReportScriptHandler.cpp
1/* This file is part of the KDE project
2 * Copyright (C) 2007-2008 by Adam Pigg <adam@piggz.co.uk>
3 * Copyright (C) 2012 Jarosław Staniek <staniek@kde.org>
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19#include "KReportScriptHandler.h"
20
21#include "KReportDataSource.h"
22#include "KReportScriptSource.h"
23#include "KReportScriptSection.h"
24#include "KReportScriptDebug.h"
25#include "KReportScriptReport.h"
26#include "KReportScriptDraw.h"
27#include "KReportScriptConstants.h"
28#include "KReportSectionData.h"
29#include "KReportItemBase.h"
30#include "KReportDocument.h"
31#include "KReportDetailSectionData.h"
32#include "KReportRenderObjects.h"
33#include "kreport_debug.h"
34
35#include <QMessageBox>
36#include <QJSEngine>
37#include <QJSValue>
38
39
40//Note#: this is here to save creating a new file for this interface
41KReportScriptSource::~KReportScriptSource()
42{
43}
44
45class Q_DECL_HIDDEN KReportScriptHandler::Private
46{
47public:
48 Private();
49 ~Private();
50 KReportScriptConstants *constants;
51 KReportScriptDebug *debug;
53 Scripting::Report *report;
54 const KReportDataSource *reportDataSource;
55 const KReportScriptSource *scriptSource;
56 QString source;
57 KReportDocument *reportDocument;
58 QJSEngine engine;
59 QJSValue scriptValue;
62 bool suppressEvaluateErrors = false;
63};
64
65KReportScriptHandler::Private::Private() : constants(new KReportScriptConstants), debug(new KReportScriptDebug), draw(new KReportScriptDraw)
66{
67}
68
69KReportScriptHandler::Private::~Private()
70{
71}
72
73KReportScriptHandler::KReportScriptHandler(const KReportDataSource* reportDataSource, KReportScriptSource* scriptSource, KReportDocument* reportDocument) : d(new Private())
74{
75 d->reportDocument = reportDocument;
76 d->reportDataSource = reportDataSource;
77 d->scriptSource = scriptSource;
78
79 //Add a general report object
80 d->report = new Scripting::Report(d->reportDocument);
81
82 registerScriptObject(d->constants, QLatin1String("constants"));
83 registerScriptObject(d->debug, QLatin1String("debug"));
84 registerScriptObject(d->draw, QLatin1String("draw"));
85
86 QJSValue r = registerScriptObject(d->report, d->reportDocument->name());
87
88 //Add the sections
89 QList<KReportSectionData*> secs = d->reportDocument->sections();
90 foreach(KReportSectionData *sec, secs) {
91 d->sectionMap[sec] = new Scripting::Section(sec);
92 d->sectionMap[sec]->setParent(d->report);
93 d->sectionMap[sec]->setObjectName(sec->name().replace(QLatin1Char('-'), QLatin1Char('_'))
94 .remove(QLatin1String("report:")));
95 QJSValue s = d->engine.newQObject(d->sectionMap[sec]);
96 r.setProperty(d->sectionMap[sec]->objectName(), s);
97 //kreportDebug() << "Added" << d->sectionMap[sec]->objectName() << "to report" << d->reportData->name();
98 }
99}
100
101bool KReportScriptHandler::trigger()
102{
103 QString code;
104 if (d->scriptSource) {
105 code = d->scriptSource->scriptCode(d->reportDocument->script());
106 }
107
108 if (code.isEmpty()) {
109 return true;
110 }
111
112 d->scriptValue = d->engine.evaluate(code, d->reportDocument->script());
113
114 if (d->scriptValue.isError()) {
115 return false;
116 }/*TODO else {
117 kreportDebug() << "Function Names:" << d->engine->functionNames();
118 }*/
119 d->report->eventOnOpen();
120 return true;
121}
122
123KReportScriptHandler::~KReportScriptHandler()
124{
125 delete d;
126}
127
128void KReportScriptHandler::newPage()
129{
130 if (d->report) {
131 d->report->eventOnNewPage();
132 }
133}
134
135void KReportScriptHandler::slotEnteredGroup(const QString &key, const QVariant &value)
136{
137 //kreportDebug() << key << value;
138 d->groups[key] = value;
139 emit(groupChanged(d->groups));
140}
141void KReportScriptHandler::slotExitedGroup(const QString &key, const QVariant &value)
142{
143 Q_UNUSED(value);
144 //kreportDebug() << key << value;
145 d->groups.remove(key);
146 emit(groupChanged(d->groups));
147}
148
149void KReportScriptHandler::slotEnteredSection(KReportSectionData *section, OROPage* cp, QPointF off)
150{
151 if (cp)
152 d->draw->setPage(cp);
153 d->draw->setOffset(off);
154
155 Scripting::Section *ss = d->sectionMap[section];
156 if (ss) {
157 ss->eventOnRender();
158 }
159}
160
161QVariant KReportScriptHandler::evaluate(const QString &code)
162{
163 if (!d->scriptValue.isError()) {
164 QJSValue result = d->engine.evaluate(code);
165 if (!result.isError()) {
166 return result.toVariant();
167 } else {
168 if (!d->suppressEvaluateErrors) {
169 QMessageBox msgBox;
170 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()));
171 msgBox.setDetailedText(tr("Script code:\n%1").arg(code));
174 int ret = msgBox.exec();
175 if (ret == QMessageBox::Yes) {
176 d->suppressEvaluateErrors = true;
177 }
178 }
179 }
180 }
181 return QVariant();
182}
183
184void KReportScriptHandler::displayErrors()
185{
186 if (d->scriptValue.isError()) {
187 QMessageBox::warning(nullptr, tr("Script Error"), d->scriptValue.toString());
188 }
189}
190
191//! @todo KEXI3 move to kexi
192#if 0
193QString KReportScriptHandler::where()
194{
195 QString w;
196 QMap<QString, QVariant>::const_iterator i = d->groups.constBegin();
197 while (i != d->groups.constEnd()) {
198 w += QLatin1Char('(') + i.key() + QLatin1String(" = '") + i.value().toString() + QLatin1String("') AND ");
199 ++i;
200 }
201 w.chop(4);
202 //kreportDebug() << w;
203 return w;
204}
205#endif
206
207QJSValue KReportScriptHandler::registerScriptObject(QObject* obj, const QString& name)
208{
209 QJSValue val;
210 val = d->engine.newQObject(obj);
211 d->engine.globalObject().setProperty(name, val);
212 return val;
213}
214
215void KReportScriptHandler::setPageNumber(int p)
216{
217 d->constants->setPageNumber(p);
218}
219
220void KReportScriptHandler::setPageTotal(int t)
221{
222 d->constants->setPageTotal(t);
223}
Abstraction of report data source.
Top level report document definition. A KReportDocment defines the design of a document,...
Helper giving access to various scripting constants.
Helper for the scripting API to display user messages.
Helper giving access to drawing functions.
Abstraction of report script source.
KReportSectionData is used to store the information about a specific report section.
Represents a single page in a document and may contain zero or more OROPrimitive objects all of which...
Report object user scripting API.
KGuiItem remove()
bool isError() const const
void setProperty(const QString &name, const QJSValue &value)
QVariant toVariant() const const
void setDetailedText(const QString &text)
virtual int exec() override
void setDefaultButton(QPushButton *button)
void setStandardButtons(StandardButtons buttons)
void setText(const QString &text)
StandardButton warning(QWidget *parent, const QString &title, const QString &text, StandardButtons buttons, StandardButton defaultButton)
void chop(qsizetype n)
bool isEmpty() const const
QString & replace(QChar before, QChar after, Qt::CaseSensitivity cs)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Jun 14 2024 11:56:05 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.