File indexing completed on 2024-05-05 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 
0018 #include "KReportPage.h"
0019 
0020 #include "KReportRendererBase.h"
0021 #include "KReportUnit.h"
0022 #include "KReportRenderObjects.h"
0023 #include "KReportUtils_p.h"
0024 #include "kreport_debug.h"
0025 
0026 #include <QPainter>
0027 #include <QPixmap>
0028 #include <QPointer>
0029 #include <QTimer>
0030 #include <QWidget>
0031 
0032 //! @internal
0033 class Q_DECL_HIDDEN KReportPage::Private
0034 {
0035 public:
0036     explicit Private(ORODocument *document)
0037         : reportDocument(document)
0038     {}
0039 
0040     ~Private()
0041     {
0042         delete renderer;
0043     }
0044 
0045     QPointer<ORODocument> reportDocument;
0046     int page;
0047     QPixmap pixmap;
0048     KReportRendererFactory factory;
0049     KReportRendererBase *renderer;
0050     QTimer renderTimer;
0051 };
0052 
0053 
0054 KReportPage::KReportPage(QWidget *parent, ORODocument *document)
0055         : QObject(parent), QGraphicsRectItem()
0056         , d(new Private(document))
0057 {
0058     Q_ASSERT(document);
0059     
0060     int pageWidth;
0061     int pageHeight;
0062 
0063     pageWidth = d->reportDocument->pageLayout().fullRectPixels(KReportPrivate::dpiX()).width();
0064     pageHeight = d->reportDocument->pageLayout().fullRectPixels(KReportPrivate::dpiX()).height();
0065 
0066     setRect(0, 0, pageWidth, pageHeight);
0067     //kreportDebug() << "PAGE IS " << pageWidth << "x" << pageHeight;
0068     d->pixmap = QPixmap(pageWidth, pageHeight);
0069     d->renderer = d->factory.createInstance(QLatin1String("screen"));
0070     connect(d->reportDocument, SIGNAL(updated(int)), this, SLOT(pageUpdated(int)));
0071 
0072     d->renderTimer.setSingleShot(true);
0073     connect(&d->renderTimer, SIGNAL(timeout()), this, SLOT(renderCurrentPage()));
0074 
0075     renderPage(1);
0076 }
0077 
0078 KReportPage::~KReportPage()
0079 {
0080     delete d;
0081 }
0082 
0083 void KReportPage::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
0084 {
0085     Q_UNUSED(option);
0086     Q_UNUSED(widget);
0087     painter->drawPixmap(QPoint(0, 0), d->pixmap);
0088 }
0089 
0090 void KReportPage::renderPage(int page)
0091 {
0092     d->page = page - 1;
0093     d->pixmap.fill();
0094     QPainter qp(&d->pixmap);
0095     if (d->reportDocument) {
0096         KReportRendererContext cxt;
0097         cxt.setPainter(&qp);
0098         (void)d->renderer->render(cxt, d->reportDocument, d->page);
0099     }
0100     update();
0101 }
0102 
0103 void KReportPage::pageUpdated(int pageNo)
0104 {
0105     //kreportDebug() << pageNo << d->page;
0106     //Refresh this page if it changes
0107     if (pageNo == d->page) {
0108         //kreportDebug() << "Current page updated";
0109         d->renderTimer.start(100);
0110     }
0111 }
0112 
0113 void KReportPage::renderCurrentPage()
0114 {
0115     renderPage(d->page + 1);
0116 }