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

0001 /* This file is part of the KDE project
0002    Copyright Shreya Pandit <shreya@shreyapandit.com>
0003    Copyright 2011 Adam Pigg <adam@piggz.co.uk>
0004 
0005    This library is free software; you can redistribute it and/or
0006    modify it under the terms of the GNU Library General Public
0007    License as published by the Free Software Foundation; either
0008    version 2 of the License, or (at your option) any later version.
0009 
0010    This library is distributed in the hope that it will be useful,
0011    but WITHOUT ANY WARRANTY; without even the implied warranty of
0012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0013    Library General Public License for more details.
0014 
0015    You should have received a copy of the GNU Library General Public License
0016    along with this library; see the file COPYING.LIB.  If not, write to
0017    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0018  * Boston, MA 02110-1301, USA.
0019 */
0020 
0021 #include "KReportItemWeb.h"
0022 #include "KReportUtils.h"
0023 #include "KReportRenderObjects.h"
0024 
0025 #include <KPropertyListData>
0026 #include <KPropertySet>
0027 
0028 #include <QGraphicsRectItem>
0029 #include <QUrl>
0030 #include <QWebPage>
0031 #include <QWebFrame>
0032 #include <QPainter>
0033 
0034 #include "kreportplugin_debug.h"
0035 
0036 KReportItemWeb::KReportItemWeb()
0037     : m_rendering(false), m_targetPage(nullptr), m_targetSection(nullptr)
0038 {
0039     createProperties();
0040     m_webPage = new QWebPage();
0041     connect(m_webPage, SIGNAL(loadFinished(bool)), this, SLOT(loadFinished(bool)));
0042 }
0043 
0044 KReportItemWeb::KReportItemWeb(const QDomNode &element)
0045     : KReportItemWeb()
0046 {
0047     QDomElement e = element.toElement();
0048 
0049     setItemDataSource(e.attribute(QLatin1String("report:item-data-source")));
0050     nameProperty()->setValue(KReportUtils::readNameAttribute(e));
0051     setZ(e.attribute(QLatin1String("report:z-index")).toDouble());
0052     parseReportRect(e);
0053     QDomNodeList nl = element.childNodes();
0054     QString n;
0055     QDomNode node;
0056     for (int i = 0; i < nl.count(); i++) {
0057         node = nl.item(i);
0058         n = node.nodeName();
0059     }
0060 }
0061 
0062 void KReportItemWeb::createProperties()
0063 {
0064     createDataSourceProperty();
0065 }
0066 
0067 KReportItemWeb::~KReportItemWeb()
0068 {
0069 }
0070 
0071 QString KReportItemWeb::typeName() const
0072 {
0073     return QLatin1String("web");
0074 }
0075 
0076 void KReportItemWeb::loadFinished(bool)
0077 {
0078     //kreportpluginDebug() << m_rendering;
0079     if (m_rendering) {
0080         OROPicture * pic = new OROPicture();
0081         m_webPage->setViewportSize(sceneSize(size()).toSize());
0082         m_webPage->mainFrame()->setScrollBarPolicy(Qt::Horizontal, Qt::ScrollBarAlwaysOff);
0083         m_webPage->mainFrame()->setScrollBarPolicy(Qt::Vertical, Qt::ScrollBarAlwaysOff);
0084 
0085         QPainter p(pic->picture());
0086 
0087         m_webPage->mainFrame()->render(&p);
0088 
0089         QPointF pos = scenePosition(position());
0090         QSizeF siz = sceneSize(size());
0091 
0092         pos += m_targetOffset;
0093 
0094         pic->setPosition(pos);
0095         pic->setSize(siz);
0096         if (m_targetPage) m_targetPage->insertPrimitive(pic);
0097 
0098         OROPicture *p2 = dynamic_cast<OROPicture*>(pic->clone());
0099         if (p2) {
0100             p2->setPosition(scenePosition(position()));
0101             if (m_targetSection) {
0102                 m_targetSection->addPrimitive(p2);
0103             }
0104         }
0105 
0106         m_rendering = false;
0107         emit(finishedRendering());
0108     }
0109 }
0110 
0111 int KReportItemWeb::renderSimpleData(OROPage *page, OROSection *section, const QPointF &offset,
0112                                       const QVariant &data, KReportScriptHandler *script)
0113 {
0114     Q_UNUSED(script);
0115 
0116     m_rendering = true;
0117 
0118     //kreportpluginDebug() << data;
0119 
0120     m_targetPage = page;
0121     m_targetSection = section;
0122     m_targetOffset = offset;
0123 
0124     QUrl url = QUrl::fromUserInput(data.toString());
0125     if (url.isValid()) {
0126         m_webPage->mainFrame()->load(url);
0127     } else {
0128         m_webPage->mainFrame()->setHtml(data.toString());
0129     }
0130 
0131     return 0; //Item doesn't stretch the section height
0132 }