File indexing completed on 2024-05-12 04:43:22

0001 /* This file is part of the KDE project
0002  * Copyright (C) 2007-2008 by Adam Pigg (adam@piggz.co.uk)
0003  *
0004  * This library is free software; you can redistribute it and/or
0005  * modify it under the terms of the GNU Lesser General Public
0006  * License as published by the Free Software Foundation; either
0007  * version 2.1 of the License, or (at your option) any later version.
0008  *
0009  * This library is distributed in the hope that it will be useful,
0010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
0012  * Lesser General Public License for more details.
0013  *
0014  * You should have received a copy of the GNU Lesser General Public
0015  * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
0016  */
0017 #include "KReportScriptSection.h"
0018 
0019 #include "KReportScriptLine.h"
0020 #include "KReportItemBase.h"
0021 #include "KReportPluginManager.h"
0022 #include "KReportPluginInterface.h"
0023 #include "KReportItemLine.h"
0024 #include "KReportSectionData.h"
0025 #include "kreport_debug.h"
0026 
0027 namespace Scripting
0028 {
0029 Section::Section(KReportSectionData* sec)
0030 {
0031     m_section = sec;
0032     m_scriptObject = 0;
0033 }
0034 
0035 
0036 Section::~Section()
0037 {
0038 }
0039 
0040 QColor Section::backgroundColor() const
0041 {
0042     return m_section->backgroundColor();
0043 }
0044 
0045 void Section::setBackgroundColor(const QColor &c)
0046 {
0047     m_section->setBackgroundColor(c);
0048 }
0049 
0050 qreal Section::height() const
0051 {
0052     return m_section->height();
0053 }
0054 
0055 void Section::setHeight(qreal h)
0056 {
0057     m_section->setHeight(h);
0058 }
0059 
0060 QString Section::name() const
0061 {
0062     return m_section->objectName();
0063 }
0064 
0065 QObject* Section::objectByNumber(int i)
0066 {
0067     if (m_section->object(i)->typeName() == QLatin1String("line")) {
0068                 return new Scripting::Line(dynamic_cast<KReportItemLine*>(m_section->object(i)));
0069     }
0070     else {
0071         KReportPluginManager* manager = KReportPluginManager::self();
0072         KReportPluginInterface *plugin = manager->plugin(m_section->object(i)->typeName());
0073         if (plugin) {
0074             QObject *obj = plugin->createScriptInstance(m_section->object(i));
0075             if (obj) {
0076                 return obj;
0077             }
0078         }
0079         else {
0080             kreportWarning() << "Encountered unknown node while parsing section: " << m_section->object(i)->typeName();
0081         }
0082     }
0083 
0084     return new QObject();
0085 }
0086 
0087 QObject* Section::objectByName(const QString& n)
0088 {
0089     for (int i = 0; i < m_section->objects().count(); ++i) {
0090         if (m_section->object(i)->entityName() == n) {
0091             return objectByNumber(i);
0092         }
0093     }
0094     return nullptr;
0095 }
0096 
0097 void Section::initialize(const QJSValue &s)
0098 {
0099     m_scriptObject = s;
0100 }
0101 
0102 void Section::eventOnRender()
0103 {
0104     if (m_scriptObject.isObject() && m_scriptObject.hasProperty(QLatin1String("OnRender")))
0105         m_scriptObject.property(QLatin1String("OnRender")).call();
0106 }
0107 }