Changeset View
Changeset View
Standalone View
Standalone View
src/renderer/scripting/KReportScriptHandler.cpp
Show All 28 Lines | |||||
29 | #include "KReportDetailSectionData.h" | 29 | #include "KReportDetailSectionData.h" | ||
30 | #include "KReportRenderObjects.h" | 30 | #include "KReportRenderObjects.h" | ||
31 | #include "kreport_debug.h" | 31 | #include "kreport_debug.h" | ||
32 | 32 | | |||
33 | #include <QMessageBox> | 33 | #include <QMessageBox> | ||
34 | #include <QJSEngine> | 34 | #include <QJSEngine> | ||
35 | #include <QJSValue> | 35 | #include <QJSValue> | ||
36 | 36 | | |||
37 | KReportScriptHandler::KReportScriptHandler(const KReportData* kodata, KReportDocument* d) | 37 | class Q_DECL_HIDDEN KReportScriptHandler::Private | ||
38 | { | 38 | { | ||
39 | m_reportData = d; | 39 | public: | ||
40 | m_kreportData = kodata; | 40 | Private(); | ||
41 | ~Private(); | ||||
42 | KReportScriptConstants *constants; | ||||
43 | KReportScriptDebug *debug; | ||||
44 | KReportScriptDraw *draw; | ||||
41 | 45 | | |||
42 | m_engine = 0; | 46 | Scripting::Report *report; | ||
43 | m_constants = 0; | | |||
44 | m_debug = 0; | | |||
45 | m_draw = 0; | | |||
46 | 47 | | |||
47 | // Create the script engine instance . | 48 | const KReportData *reportData; | ||
staniek: How about putting all these 4 variables on stack in Private to simplify the code? Now they're… | |||||
48 | m_engine = new QJSEngine(this); | 49 | | ||
50 | QString source; | ||||
51 | KReportDocument *reportDocument; | ||||
52 | | ||||
53 | QJSEngine engine; | ||||
54 | QJSValue scriptValue; | ||||
55 | | ||||
56 | QMap<QString, QVariant> groups; | ||||
57 | QMap<KReportSectionData*, Scripting::Section*> sectionMap; | ||||
58 | }; | ||||
59 | | ||||
60 | KReportScriptHandler::Private::Private() | ||||
61 | { | ||||
62 | //NOTE these are on the heap so that engine can delete them | ||||
49 | 63 | | |||
50 | //Add constants object | 64 | //Add constants object | ||
51 | m_constants = new KReportScriptConstants(); | 65 | constants = new KReportScriptConstants(); | ||
52 | registerScriptObject(m_constants, QLatin1String("constants")); | | |||
53 | 66 | | |||
54 | //A simple debug function to allow printing from functions | 67 | //A simple debug function to allow printing from functions | ||
55 | m_debug = new KReportScriptDebug(); | 68 | debug = new KReportScriptDebug(); | ||
56 | registerScriptObject(m_debug, QLatin1String("debug")); | | |||
57 | 69 | | |||
58 | //A simple drawing object | 70 | //A simple drawing object | ||
59 | m_draw = new KReportScriptDraw(); | 71 | draw = new KReportScriptDraw(); | ||
60 | registerScriptObject(m_draw, QLatin1String("draw")); | 72 | } | ||
73 | | ||||
74 | KReportScriptHandler::Private::~Private() | ||||
75 | { | ||||
76 | } | ||||
Sorry, this note is not needed, I meant only the one from line 62. Thanks. staniek: Sorry, this note is not needed, I meant only the one from line 62. Thanks. | |||||
77 | | ||||
78 | KReportScriptHandler::KReportScriptHandler(const KReportData* kodata, KReportDocument* doc) : d(new Private()) | ||||
79 | { | ||||
80 | d->reportDocument = doc; | ||||
81 | d->reportData = kodata; | ||||
61 | 82 | | |||
62 | //Add a general report object | 83 | //Add a general report object | ||
63 | m_report = new Scripting::Report(m_reportData); | 84 | d->report = new Scripting::Report(d->reportDocument); | ||
64 | QJSValue r = registerScriptObject(m_report, m_reportData->name()); | 85 | | ||
86 | registerScriptObject(d->constants, QLatin1String("constants")); | ||||
87 | registerScriptObject(d->debug, QLatin1String("debug")); | ||||
88 | registerScriptObject(d->draw, QLatin1String("draw")); | ||||
89 | | ||||
90 | QJSValue r = registerScriptObject(d->report, d->reportDocument->name()); | ||||
65 | 91 | | |||
66 | //Add the sections | 92 | //Add the sections | ||
67 | QList<KReportSectionData*> secs = m_reportData->sections(); | 93 | QList<KReportSectionData*> secs = d->reportDocument->sections(); | ||
68 | foreach(KReportSectionData *sec, secs) { | 94 | foreach(KReportSectionData *sec, secs) { | ||
69 | m_sectionMap[sec] = new Scripting::Section(sec); | 95 | d->sectionMap[sec] = new Scripting::Section(sec); | ||
70 | m_sectionMap[sec]->setParent(m_report); | 96 | d->sectionMap[sec]->setParent(d->report); | ||
71 | m_sectionMap[sec]->setObjectName(sec->name().replace(QLatin1Char('-'), QLatin1Char('_')) | 97 | d->sectionMap[sec]->setObjectName(sec->name().replace(QLatin1Char('-'), QLatin1Char('_')) | ||
72 | .remove(QLatin1String("report:"))); | 98 | .remove(QLatin1String("report:"))); | ||
73 | QJSValue s = m_engine->newQObject(m_sectionMap[sec]); | 99 | QJSValue s = d->engine.newQObject(d->sectionMap[sec]); | ||
74 | r.setProperty(m_sectionMap[sec]->objectName(), s); | 100 | r.setProperty(d->sectionMap[sec]->objectName(), s); | ||
75 | //kreportDebug() << "Added" << m_sectionMap[sec]->objectName() << "to report" << m_reportData->name(); | 101 | //kreportDebug() << "Added" << d->sectionMap[sec]->objectName() << "to report" << d->reportData->name(); | ||
76 | } | 102 | } | ||
77 | 103 | | |||
78 | //kreportDebug() << "Report name is" << m_reportData->name(); | 104 | //kreportDebug() << "Report name is" << d->reportData->name(); | ||
79 | } | 105 | } | ||
80 | 106 | | |||
81 | bool KReportScriptHandler::trigger() | 107 | bool KReportScriptHandler::trigger() | ||
82 | { | 108 | { | ||
83 | QString code = m_kreportData->scriptCode(m_reportData->script()); | 109 | QString code = d->reportData->scriptCode(d->reportDocument->script()); | ||
84 | //kreportDebug() << code; | 110 | //kreportDebug() << code; | ||
85 | 111 | | |||
86 | if (code.isEmpty()) { | 112 | if (code.isEmpty()) { | ||
87 | return true; | 113 | return true; | ||
88 | } | 114 | } | ||
89 | 115 | | |||
90 | m_scriptValue = m_engine->evaluate(code, m_reportData->script()); | 116 | d->scriptValue = d->engine.evaluate(code, d->reportDocument->script()); | ||
91 | 117 | | |||
92 | if (m_scriptValue.isError()) { | 118 | if (d->scriptValue.isError()) { | ||
93 | return false; | 119 | return false; | ||
94 | }/*TODO else { | 120 | }/*TODO else { | ||
95 | kreportDebug() << "Function Names:" << m_engine->functionNames(); | 121 | kreportDebug() << "Function Names:" << d->engine->functionNames(); | ||
96 | }*/ | 122 | }*/ | ||
97 | m_report->eventOnOpen(); | 123 | d->report->eventOnOpen(); | ||
98 | return true; | 124 | return true; | ||
99 | } | 125 | } | ||
100 | 126 | | |||
101 | KReportScriptHandler::~KReportScriptHandler() | 127 | KReportScriptHandler::~KReportScriptHandler() | ||
102 | { | 128 | { | ||
103 | delete m_report; | 129 | delete d; | ||
104 | delete m_engine; | | |||
105 | } | 130 | } | ||
106 | 131 | | |||
107 | void KReportScriptHandler::newPage() | 132 | void KReportScriptHandler::newPage() | ||
108 | { | 133 | { | ||
109 | if (m_report) { | 134 | if (d->report) { | ||
110 | m_report->eventOnNewPage(); | 135 | d->report->eventOnNewPage(); | ||
111 | } | 136 | } | ||
112 | } | 137 | } | ||
113 | 138 | | |||
114 | void KReportScriptHandler::slotEnteredGroup(const QString &key, const QVariant &value) | 139 | void KReportScriptHandler::slotEnteredGroup(const QString &key, const QVariant &value) | ||
115 | { | 140 | { | ||
116 | //kreportDebug() << key << value; | 141 | //kreportDebug() << key << value; | ||
117 | m_groups[key] = value; | 142 | d->groups[key] = value; | ||
118 | emit(groupChanged(m_groups)); | 143 | emit(groupChanged(d->groups)); | ||
119 | } | 144 | } | ||
120 | void KReportScriptHandler::slotExitedGroup(const QString &key, const QVariant &value) | 145 | void KReportScriptHandler::slotExitedGroup(const QString &key, const QVariant &value) | ||
121 | { | 146 | { | ||
122 | Q_UNUSED(value); | 147 | Q_UNUSED(value); | ||
123 | //kreportDebug() << key << value; | 148 | //kreportDebug() << key << value; | ||
124 | m_groups.remove(key); | 149 | d->groups.remove(key); | ||
125 | emit(groupChanged(m_groups)); | 150 | emit(groupChanged(d->groups)); | ||
126 | } | 151 | } | ||
127 | 152 | | |||
128 | void KReportScriptHandler::slotEnteredSection(KReportSectionData *section, OROPage* cp, QPointF off) | 153 | void KReportScriptHandler::slotEnteredSection(KReportSectionData *section, OROPage* cp, QPointF off) | ||
129 | { | 154 | { | ||
130 | if (cp) | 155 | if (cp) | ||
131 | m_draw->setPage(cp); | 156 | d->draw->setPage(cp); | ||
132 | m_draw->setOffset(off); | 157 | d->draw->setOffset(off); | ||
133 | 158 | | |||
134 | Scripting::Section *ss = m_sectionMap[section]; | 159 | Scripting::Section *ss = d->sectionMap[section]; | ||
135 | if (ss) { | 160 | if (ss) { | ||
136 | ss->eventOnRender(); | 161 | ss->eventOnRender(); | ||
137 | } | 162 | } | ||
138 | } | 163 | } | ||
139 | 164 | | |||
140 | QVariant KReportScriptHandler::evaluate(const QString &code) | 165 | QVariant KReportScriptHandler::evaluate(const QString &code) | ||
141 | { | 166 | { | ||
142 | if (!m_scriptValue.isError()) { | 167 | if (!d->scriptValue.isError()) { | ||
143 | QJSValue result = m_engine->evaluate(code); | 168 | QJSValue result = d->engine.evaluate(code); | ||
144 | if (!result.isError()) { | 169 | if (!result.isError()) { | ||
145 | return result.toVariant(); | 170 | return result.toVariant(); | ||
146 | } else { | 171 | } else { | ||
147 | QMessageBox::warning(0, tr("Script Error"), m_scriptValue.toString()); | 172 | QMessageBox::warning(0, tr("Script Error"), d->scriptValue.toString()); | ||
148 | } | 173 | } | ||
149 | } | 174 | } | ||
150 | return QVariant(); | 175 | return QVariant(); | ||
151 | } | 176 | } | ||
152 | 177 | | |||
153 | void KReportScriptHandler::displayErrors() | 178 | void KReportScriptHandler::displayErrors() | ||
154 | { | 179 | { | ||
155 | if (m_scriptValue.isError()) { | 180 | if (d->scriptValue.isError()) { | ||
156 | QMessageBox::warning(0, tr("Script Error"), m_scriptValue.toString()); | 181 | QMessageBox::warning(0, tr("Script Error"), d->scriptValue.toString()); | ||
157 | } | 182 | } | ||
158 | } | 183 | } | ||
159 | 184 | | |||
160 | //! @todo KEXI3 move to kexi | 185 | //! @todo KEXI3 move to kexi | ||
161 | #if 0 | 186 | #if 0 | ||
162 | QString KReportScriptHandler::where() | 187 | QString KReportScriptHandler::where() | ||
163 | { | 188 | { | ||
164 | QString w; | 189 | QString w; | ||
165 | QMap<QString, QVariant>::const_iterator i = m_groups.constBegin(); | 190 | QMap<QString, QVariant>::const_iterator i = d->groups.constBegin(); | ||
166 | while (i != m_groups.constEnd()) { | 191 | while (i != d->groups.constEnd()) { | ||
167 | w += QLatin1Char('(') + i.key() + QLatin1String(" = '") + i.value().toString() + QLatin1String("') AND "); | 192 | w += QLatin1Char('(') + i.key() + QLatin1String(" = '") + i.value().toString() + QLatin1String("') AND "); | ||
168 | ++i; | 193 | ++i; | ||
169 | } | 194 | } | ||
170 | w.chop(4); | 195 | w.chop(4); | ||
171 | //kreportDebug() << w; | 196 | //kreportDebug() << w; | ||
172 | return w; | 197 | return w; | ||
173 | } | 198 | } | ||
174 | #endif | 199 | #endif | ||
175 | 200 | | |||
176 | QJSValue KReportScriptHandler::registerScriptObject(QObject* obj, const QString& name) | 201 | QJSValue KReportScriptHandler::registerScriptObject(QObject* obj, const QString& name) | ||
177 | { | 202 | { | ||
178 | QJSValue val; | 203 | QJSValue val; | ||
179 | val = m_engine->newQObject(obj); | 204 | val = d->engine.newQObject(obj); | ||
180 | m_engine->globalObject().setProperty(name, val); | 205 | d->engine.globalObject().setProperty(name, val); | ||
181 | return val; | 206 | return val; | ||
182 | } | 207 | } | ||
183 | 208 | | |||
209 | void KReportScriptHandler::setPageNumber(int p) | ||||
210 | { | ||||
211 | d->constants->setPageNumber(p); | ||||
212 | } | ||||
213 | | ||||
214 | void KReportScriptHandler::setPageTotal(int t) | ||||
215 | { | ||||
216 | d->constants->setPageTotal(t); | ||||
217 | } |
How about putting all these 4 variables on stack in Private to simplify the code? Now they're leaked BTW.