Changeset View
Changeset View
Standalone View
Standalone View
src/renderer/scripting/KReportScriptHandler.cpp
Show All 12 Lines | |||||
13 | * Lesser General Public License for more details. | 13 | * Lesser General Public License for more details. | ||
14 | * | 14 | * | ||
15 | * You should have received a copy of the GNU Lesser General Public | 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/>. | 16 | * License along with this library. If not, see <http://www.gnu.org/licenses/>. | ||
17 | */ | 17 | */ | ||
18 | 18 | | |||
19 | #include "KReportScriptHandler.h" | 19 | #include "KReportScriptHandler.h" | ||
20 | 20 | | |||
21 | #include "KReportDataSource.h" | ||||
22 | #include "KReportScriptSource.h" | ||||
21 | #include "KReportScriptSection.h" | 23 | #include "KReportScriptSection.h" | ||
22 | #include "KReportScriptDebug.h" | 24 | #include "KReportScriptDebug.h" | ||
23 | #include "KReportScriptReport.h" | 25 | #include "KReportScriptReport.h" | ||
24 | #include "KReportScriptDraw.h" | 26 | #include "KReportScriptDraw.h" | ||
25 | #include "KReportScriptConstants.h" | 27 | #include "KReportScriptConstants.h" | ||
26 | #include "KReportSectionData.h" | 28 | #include "KReportSectionData.h" | ||
27 | #include "KReportItemBase.h" | 29 | #include "KReportItemBase.h" | ||
28 | #include "KReportDocument.h" | 30 | #include "KReportDocument.h" | ||
29 | #include "KReportDetailSectionData.h" | 31 | #include "KReportDetailSectionData.h" | ||
30 | #include "KReportRenderObjects.h" | 32 | #include "KReportRenderObjects.h" | ||
31 | #include "kreport_debug.h" | 33 | #include "kreport_debug.h" | ||
32 | 34 | | |||
33 | #include <QMessageBox> | 35 | #include <QMessageBox> | ||
34 | #include <QJSEngine> | 36 | #include <QJSEngine> | ||
35 | #include <QJSValue> | 37 | #include <QJSValue> | ||
36 | 38 | | |||
39 | | ||||
40 | //Note#: this is here to save creating a new file for this interface | ||||
41 | KReportScriptSource::~KReportScriptSource() | ||||
staniek: coding style | |||||
piggz: ? | |||||
staniek: KReportScriptSource::~KReportScriptSource()
{
} | |||||
42 | { | ||||
43 | } | ||||
44 | | ||||
37 | class Q_DECL_HIDDEN KReportScriptHandler::Private | 45 | class Q_DECL_HIDDEN KReportScriptHandler::Private | ||
38 | { | 46 | { | ||
39 | public: | 47 | public: | ||
40 | Private(); | 48 | Private(); | ||
41 | ~Private(); | 49 | ~Private(); | ||
42 | KReportScriptConstants *constants; | 50 | KReportScriptConstants *constants; | ||
43 | KReportScriptDebug *debug; | 51 | KReportScriptDebug *debug; | ||
44 | KReportScriptDraw *draw; | 52 | KReportScriptDraw *draw; | ||
45 | Scripting::Report *report; | 53 | Scripting::Report *report; | ||
46 | const KReportDataSource *reportDataSource; | 54 | const KReportDataSource *reportDataSource; | ||
55 | const KReportScriptSource *scriptSource; | ||||
47 | QString source; | 56 | QString source; | ||
48 | KReportDocument *reportDocument; | 57 | KReportDocument *reportDocument; | ||
49 | QJSEngine engine; | 58 | QJSEngine engine; | ||
50 | QJSValue scriptValue; | 59 | QJSValue scriptValue; | ||
51 | QMap<QString, QVariant> groups; | 60 | QMap<QString, QVariant> groups; | ||
52 | QMap<KReportSectionData*, Scripting::Section*> sectionMap; | 61 | QMap<KReportSectionData*, Scripting::Section*> sectionMap; | ||
62 | bool suppressEvaluateErrors = false; | ||||
53 | }; | 63 | }; | ||
54 | 64 | | |||
55 | KReportScriptHandler::Private::Private() : constants(new KReportScriptConstants), debug(new KReportScriptDebug), draw(new KReportScriptDraw) | 65 | KReportScriptHandler::Private::Private() : constants(new KReportScriptConstants), debug(new KReportScriptDebug), draw(new KReportScriptDraw) | ||
56 | { | 66 | { | ||
57 | } | 67 | } | ||
58 | 68 | | |||
59 | KReportScriptHandler::Private::~Private() | 69 | KReportScriptHandler::Private::~Private() | ||
60 | { | 70 | { | ||
61 | } | 71 | } | ||
62 | 72 | | |||
63 | KReportScriptHandler::KReportScriptHandler(const KReportDataSource* reportDataSource, KReportDocument* reportDocument) : d(new Private()) | 73 | KReportScriptHandler::KReportScriptHandler(const KReportDataSource* reportDataSource, KReportScriptSource* scriptSource, KReportDocument* reportDocument) : d(new Private()) | ||
64 | { | 74 | { | ||
65 | d->reportDocument = reportDocument; | 75 | d->reportDocument = reportDocument; | ||
66 | d->reportDataSource = reportDataSource; | 76 | d->reportDataSource = reportDataSource; | ||
77 | d->scriptSource = scriptSource; | ||||
67 | 78 | | |||
68 | //Add a general report object | 79 | //Add a general report object | ||
69 | d->report = new Scripting::Report(d->reportDocument); | 80 | d->report = new Scripting::Report(d->reportDocument); | ||
70 | 81 | | |||
71 | registerScriptObject(d->constants, QLatin1String("constants")); | 82 | registerScriptObject(d->constants, QLatin1String("constants")); | ||
72 | registerScriptObject(d->debug, QLatin1String("debug")); | 83 | registerScriptObject(d->debug, QLatin1String("debug")); | ||
73 | registerScriptObject(d->draw, QLatin1String("draw")); | 84 | registerScriptObject(d->draw, QLatin1String("draw")); | ||
74 | 85 | | |||
75 | QJSValue r = registerScriptObject(d->report, d->reportDocument->name()); | 86 | QJSValue r = registerScriptObject(d->report, d->reportDocument->name()); | ||
76 | 87 | | |||
77 | //Add the sections | 88 | //Add the sections | ||
78 | QList<KReportSectionData*> secs = d->reportDocument->sections(); | 89 | QList<KReportSectionData*> secs = d->reportDocument->sections(); | ||
79 | foreach(KReportSectionData *sec, secs) { | 90 | foreach(KReportSectionData *sec, secs) { | ||
80 | d->sectionMap[sec] = new Scripting::Section(sec); | 91 | d->sectionMap[sec] = new Scripting::Section(sec); | ||
81 | d->sectionMap[sec]->setParent(d->report); | 92 | d->sectionMap[sec]->setParent(d->report); | ||
82 | d->sectionMap[sec]->setObjectName(sec->name().replace(QLatin1Char('-'), QLatin1Char('_')) | 93 | d->sectionMap[sec]->setObjectName(sec->name().replace(QLatin1Char('-'), QLatin1Char('_')) | ||
83 | .remove(QLatin1String("report:"))); | 94 | .remove(QLatin1String("report:"))); | ||
84 | QJSValue s = d->engine.newQObject(d->sectionMap[sec]); | 95 | QJSValue s = d->engine.newQObject(d->sectionMap[sec]); | ||
85 | r.setProperty(d->sectionMap[sec]->objectName(), s); | 96 | r.setProperty(d->sectionMap[sec]->objectName(), s); | ||
86 | //kreportDebug() << "Added" << d->sectionMap[sec]->objectName() << "to report" << d->reportData->name(); | 97 | //kreportDebug() << "Added" << d->sectionMap[sec]->objectName() << "to report" << d->reportData->name(); | ||
87 | } | 98 | } | ||
88 | | ||||
89 | //kreportDebug() << "Report name is" << d->reportData->name(); | | |||
90 | } | 99 | } | ||
staniek: All this debug not needed in the repo | |||||
91 | 100 | | |||
92 | bool KReportScriptHandler::trigger() | 101 | bool KReportScriptHandler::trigger() | ||
93 | { | 102 | { | ||
94 | QString code = d->reportDataSource->scriptCode(d->reportDocument->script()); | 103 | QString code; | ||
95 | //kreportDebug() << code; | 104 | if (d->scriptSource) { | ||
105 | code = d->scriptSource->scriptCode(d->reportDocument->script()); | ||||
106 | } | ||||
96 | 107 | | |||
97 | if (code.isEmpty()) { | 108 | if (code.isEmpty()) { | ||
staniek: not needed code | |||||
98 | return true; | 109 | return true; | ||
99 | } | 110 | } | ||
100 | 111 | | |||
101 | d->scriptValue = d->engine.evaluate(code, d->reportDocument->script()); | 112 | d->scriptValue = d->engine.evaluate(code, d->reportDocument->script()); | ||
102 | 113 | | |||
103 | if (d->scriptValue.isError()) { | 114 | if (d->scriptValue.isError()) { | ||
104 | return false; | 115 | return false; | ||
105 | }/*TODO else { | 116 | }/*TODO else { | ||
▲ Show 20 Lines • Show All 43 Lines • ▼ Show 20 Line(s) | |||||
149 | 160 | | |||
150 | QVariant KReportScriptHandler::evaluate(const QString &code) | 161 | QVariant KReportScriptHandler::evaluate(const QString &code) | ||
151 | { | 162 | { | ||
152 | if (!d->scriptValue.isError()) { | 163 | if (!d->scriptValue.isError()) { | ||
153 | QJSValue result = d->engine.evaluate(code); | 164 | QJSValue result = d->engine.evaluate(code); | ||
154 | if (!result.isError()) { | 165 | if (!result.isError()) { | ||
155 | return result.toVariant(); | 166 | return result.toVariant(); | ||
156 | } else { | 167 | } else { | ||
157 | QMessageBox::warning(nullptr, tr("Script Error"), d->scriptValue.toString()); | 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)); | ||||
172 | msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No); | ||||
173 | msgBox.setDefaultButton(QMessageBox::No); | ||||
174 | int ret = msgBox.exec(); | ||||
175 | if (ret == QMessageBox::Yes) { | ||||
176 | d->suppressEvaluateErrors = true; | ||||
staniek: Do we want to do this infinitely? Or is there a way for the user to restore? | |||||
piggz: it will be restored the next time the report is opened | |||||
Good. How about adding something like this to the message? "They will be restored the next time the report is opened." staniek: Good. How about adding something like this to the message?
"They will be restored the next… | |||||
177 | } | ||||
178 | } | ||||
Better: QMessageBox::setText: tr("Cannot evaluate script. Error: %1.") QMessageBox::setDetailedText: "Script code:\n%1" staniek: Better:
QMessageBox::setText: tr("Cannot evaluate script. Error: %1.")
QMessageBox… | |||||
158 | } | 179 | } | ||
159 | } | 180 | } | ||
160 | return QVariant(); | 181 | return QVariant(); | ||
161 | } | 182 | } | ||
162 | 183 | | |||
163 | void KReportScriptHandler::displayErrors() | 184 | void KReportScriptHandler::displayErrors() | ||
164 | { | 185 | { | ||
165 | if (d->scriptValue.isError()) { | 186 | if (d->scriptValue.isError()) { | ||
Show All 37 Lines |
coding style