diff --git a/src/renderer/scripting/KReportScriptHandler.cpp b/src/renderer/scripting/KReportScriptHandler.cpp index 2a7e0e37..0ba3d117 100644 --- a/src/renderer/scripting/KReportScriptHandler.cpp +++ b/src/renderer/scripting/KReportScriptHandler.cpp @@ -1,183 +1,217 @@ /* This file is part of the KDE project * Copyright (C) 2007-2008 by Adam Pigg * Copyright (C) 2012 Jarosław Staniek * * 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 . */ #include "KReportScriptHandler.h" #include "KReportScriptSection.h" #include "KReportScriptDebug.h" #include "KReportScriptReport.h" #include "KReportScriptDraw.h" #include "KReportScriptConstants.h" #include "KReportSectionData.h" #include "KReportItemBase.h" #include "KReportDocument.h" #include "KReportDetailSectionData.h" #include "KReportRenderObjects.h" #include "kreport_debug.h" #include #include #include -KReportScriptHandler::KReportScriptHandler(const KReportData* kodata, KReportDocument* d) +class Q_DECL_HIDDEN KReportScriptHandler::Private { - m_reportData = d; - m_kreportData = kodata; +public: + Private(); + ~Private(); + KReportScriptConstants *constants; + KReportScriptDebug *debug; + KReportScriptDraw *draw; - m_engine = 0; - m_constants = 0; - m_debug = 0; - m_draw = 0; + Scripting::Report *report; - // Create the script engine instance . - m_engine = new QJSEngine(this); + const KReportData *reportData; + QString source; + KReportDocument *reportDocument; + + QJSEngine engine; + QJSValue scriptValue; + + QMap groups; + QMap sectionMap; +}; + +KReportScriptHandler::Private::Private() +{ + //NOTE these are on the heap so that engine can delete them + //Add constants object - m_constants = new KReportScriptConstants(); - registerScriptObject(m_constants, QLatin1String("constants")); + constants = new KReportScriptConstants(); //A simple debug function to allow printing from functions - m_debug = new KReportScriptDebug(); - registerScriptObject(m_debug, QLatin1String("debug")); + debug = new KReportScriptDebug(); //A simple drawing object - m_draw = new KReportScriptDraw(); - registerScriptObject(m_draw, QLatin1String("draw")); + draw = new KReportScriptDraw(); +} + +KReportScriptHandler::Private::~Private() +{ +} + +KReportScriptHandler::KReportScriptHandler(const KReportData* kodata, KReportDocument* doc) : d(new Private()) +{ + d->reportDocument = doc; + d->reportData = kodata; //Add a general report object - m_report = new Scripting::Report(m_reportData); - QJSValue r = registerScriptObject(m_report, m_reportData->name()); + d->report = new Scripting::Report(d->reportDocument); + + registerScriptObject(d->constants, QLatin1String("constants")); + registerScriptObject(d->debug, QLatin1String("debug")); + registerScriptObject(d->draw, QLatin1String("draw")); + + QJSValue r = registerScriptObject(d->report, d->reportDocument->name()); //Add the sections - QList secs = m_reportData->sections(); + QList secs = d->reportDocument->sections(); foreach(KReportSectionData *sec, secs) { - m_sectionMap[sec] = new Scripting::Section(sec); - m_sectionMap[sec]->setParent(m_report); - m_sectionMap[sec]->setObjectName(sec->name().replace(QLatin1Char('-'), QLatin1Char('_')) + d->sectionMap[sec] = new Scripting::Section(sec); + d->sectionMap[sec]->setParent(d->report); + d->sectionMap[sec]->setObjectName(sec->name().replace(QLatin1Char('-'), QLatin1Char('_')) .remove(QLatin1String("report:"))); - QJSValue s = m_engine->newQObject(m_sectionMap[sec]); - r.setProperty(m_sectionMap[sec]->objectName(), s); - //kreportDebug() << "Added" << m_sectionMap[sec]->objectName() << "to report" << m_reportData->name(); + QJSValue s = d->engine.newQObject(d->sectionMap[sec]); + r.setProperty(d->sectionMap[sec]->objectName(), s); + //kreportDebug() << "Added" << d->sectionMap[sec]->objectName() << "to report" << d->reportData->name(); } - //kreportDebug() << "Report name is" << m_reportData->name(); + //kreportDebug() << "Report name is" << d->reportData->name(); } bool KReportScriptHandler::trigger() { - QString code = m_kreportData->scriptCode(m_reportData->script()); + QString code = d->reportData->scriptCode(d->reportDocument->script()); //kreportDebug() << code; if (code.isEmpty()) { return true; } - m_scriptValue = m_engine->evaluate(code, m_reportData->script()); + d->scriptValue = d->engine.evaluate(code, d->reportDocument->script()); - if (m_scriptValue.isError()) { + if (d->scriptValue.isError()) { return false; }/*TODO else { - kreportDebug() << "Function Names:" << m_engine->functionNames(); + kreportDebug() << "Function Names:" << d->engine->functionNames(); }*/ - m_report->eventOnOpen(); + d->report->eventOnOpen(); return true; } KReportScriptHandler::~KReportScriptHandler() { - delete m_report; - delete m_engine; + delete d; } void KReportScriptHandler::newPage() { - if (m_report) { - m_report->eventOnNewPage(); + if (d->report) { + d->report->eventOnNewPage(); } } void KReportScriptHandler::slotEnteredGroup(const QString &key, const QVariant &value) { //kreportDebug() << key << value; - m_groups[key] = value; - emit(groupChanged(m_groups)); + d->groups[key] = value; + emit(groupChanged(d->groups)); } void KReportScriptHandler::slotExitedGroup(const QString &key, const QVariant &value) { Q_UNUSED(value); //kreportDebug() << key << value; - m_groups.remove(key); - emit(groupChanged(m_groups)); + d->groups.remove(key); + emit(groupChanged(d->groups)); } void KReportScriptHandler::slotEnteredSection(KReportSectionData *section, OROPage* cp, QPointF off) { if (cp) - m_draw->setPage(cp); - m_draw->setOffset(off); + d->draw->setPage(cp); + d->draw->setOffset(off); - Scripting::Section *ss = m_sectionMap[section]; + Scripting::Section *ss = d->sectionMap[section]; if (ss) { ss->eventOnRender(); } } QVariant KReportScriptHandler::evaluate(const QString &code) { - if (!m_scriptValue.isError()) { - QJSValue result = m_engine->evaluate(code); + if (!d->scriptValue.isError()) { + QJSValue result = d->engine.evaluate(code); if (!result.isError()) { return result.toVariant(); } else { - QMessageBox::warning(0, tr("Script Error"), m_scriptValue.toString()); + QMessageBox::warning(0, tr("Script Error"), d->scriptValue.toString()); } } return QVariant(); } void KReportScriptHandler::displayErrors() { - if (m_scriptValue.isError()) { - QMessageBox::warning(0, tr("Script Error"), m_scriptValue.toString()); + if (d->scriptValue.isError()) { + QMessageBox::warning(0, tr("Script Error"), d->scriptValue.toString()); } } //! @todo KEXI3 move to kexi #if 0 QString KReportScriptHandler::where() { QString w; - QMap::const_iterator i = m_groups.constBegin(); - while (i != m_groups.constEnd()) { + QMap::const_iterator i = d->groups.constBegin(); + while (i != d->groups.constEnd()) { w += QLatin1Char('(') + i.key() + QLatin1String(" = '") + i.value().toString() + QLatin1String("') AND "); ++i; } w.chop(4); //kreportDebug() << w; return w; } #endif QJSValue KReportScriptHandler::registerScriptObject(QObject* obj, const QString& name) { QJSValue val; - val = m_engine->newQObject(obj); - m_engine->globalObject().setProperty(name, val); + val = d->engine.newQObject(obj); + d->engine.globalObject().setProperty(name, val); return val; } +void KReportScriptHandler::setPageNumber(int p) +{ + d->constants->setPageNumber(p); +} + +void KReportScriptHandler::setPageTotal(int t) +{ + d->constants->setPageTotal(t); +} diff --git a/src/renderer/scripting/KReportScriptHandler.h b/src/renderer/scripting/KReportScriptHandler.h index 7242b68e..b05a2a72 100644 --- a/src/renderer/scripting/KReportScriptHandler.h +++ b/src/renderer/scripting/KReportScriptHandler.h @@ -1,93 +1,77 @@ /* This file is part of the KDE project * Copyright (C) 2007-2008 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 . */ #ifndef KRSCRIPTHANDLER_H #define KRSCRIPTHANDLER_H #include "kreport_export.h" #include "config-kreport.h" #ifdef KREPORT_SCRIPTING #include "KReportScriptConstants.h" #include "KReportData.h" #include class KReportScriptDebug; class KReportScriptDraw; class KReportSectionData; class QJSEngine; class KReportDocument; class OROPage; namespace Scripting { class Report; class Section; } + class KREPORT_EXPORT KReportScriptHandler : public QObject { Q_OBJECT public: KReportScriptHandler(const KReportData *, KReportDocument*); ~KReportScriptHandler(); QVariant evaluate(const QString&); void displayErrors(); QJSValue registerScriptObject(QObject*, const QString&); bool trigger(); public Q_SLOTS: void slotEnteredSection(KReportSectionData*, OROPage*, QPointF); void slotEnteredGroup(const QString&, const QVariant&); void slotExitedGroup(const QString&, const QVariant&); - void setPageNumber(int p) { - m_constants->setPageNumber(p); - } - void setPageTotal(int t) { - m_constants->setPageTotal(t); - } + void setPageNumber(int p); + void setPageTotal(int t); void newPage(); Q_SIGNALS: void groupChanged(const QMap &groupData); private: - KReportScriptConstants *m_constants; - KReportScriptDebug *m_debug; - KReportScriptDraw *m_draw; - - Scripting::Report *m_report; - - const KReportData *m_kreportData; - - QString m_source; - KReportDocument *m_reportData; - - QJSEngine* m_engine; - QJSValue m_scriptValue; - - QMap m_groups; - QMap m_sectionMap; -//! @todo KEXI3 QString where(); + //! @todo KEXI3 QString where(); + Q_DISABLE_COPY(KReportScriptHandler) + class Private; + Private * const d; }; #else // !KREPORT_SCRIPTING #define KReportScriptHandler void #endif #endif