KReport

KReportScriptHandler.cpp
1 /* This file is part of the KDE project
2  * Copyright (C) 2007-2008 by Adam Pigg <[email protected]>
3  * Copyright (C) 2012 JarosÅ‚aw Staniek <[email protected]>
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
41 KReportScriptSource::~KReportScriptSource()
42 {
43 }
44 
45 class Q_DECL_HIDDEN KReportScriptHandler::Private
46 {
47 public:
48  Private();
49  ~Private();
50  KReportScriptConstants *constants;
51  KReportScriptDebug *debug;
52  KReportScriptDraw *draw;
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 
65 KReportScriptHandler::Private::Private() : constants(new KReportScriptConstants), debug(new KReportScriptDebug), draw(new KReportScriptDraw)
66 {
67 }
68 
69 KReportScriptHandler::Private::~Private()
70 {
71 }
72 
73 KReportScriptHandler::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 
101 bool 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 
123 KReportScriptHandler::~KReportScriptHandler()
124 {
125  delete d;
126 }
127 
128 void KReportScriptHandler::newPage()
129 {
130  if (d->report) {
131  d->report->eventOnNewPage();
132  }
133 }
134 
135 void KReportScriptHandler::slotEnteredGroup(const QString &key, const QVariant &value)
136 {
137  //kreportDebug() << key << value;
138  d->groups[key] = value;
139  emit(groupChanged(d->groups));
140 }
141 void 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 
149 void 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 
161 QVariant 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 
184 void 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
193 QString KReportScriptHandler::where()
194 {
195  QString w;
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 
207 QJSValue 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 
215 void KReportScriptHandler::setPageNumber(int p)
216 {
217  d->constants->setPageNumber(p);
218 }
219 
220 void KReportScriptHandler::setPageTotal(int t)
221 {
222  d->constants->setPageTotal(t);
223 }
QMap::const_iterator constBegin() const const
void setDefaultButton(QPushButton *button)
QJSValue newQObject(QObject *object)
const T value(const Key &key, const T &defaultValue) const const
Helper for the scripting API to display user messages.
void chop(int n)
Helper giving access to various scripting constants.
QMessageBox::StandardButton warning(QWidget *parent, const QString &title, const QString &text, QMessageBox::StandardButtons buttons, QMessageBox::StandardButton defaultButton)
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...
void setDetailedText(const QString &text)
void setStandardButtons(QMessageBox::StandardButtons buttons)
bool isError() const const
void setProperty(const QString &name, const QJSValue &value)
bool isEmpty() const const
Abstraction of report data source.
QJSEngine * engine() const const
KGuiItem remove()
QJSValue evaluate(const QString &program, const QString &fileName, int lineNumber)
const Key key(const T &value, const Key &defaultKey) const const
QString & replace(int position, int n, QChar after)
QVariant toVariant() const const
virtual int exec() override
void setText(const QString &text)
Helper giving access to drawing functions.
Abstraction of report script source.
QJSValue globalObject() const const
Report object user scripting API.
Top level report document definition. A KReportDocment defines the design of a document,...
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Tue Feb 7 2023 04:17:37 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.