KoReportPage.cpp 3.25 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
 * Kexi Report Plugin
 * 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 <http://www.gnu.org/licenses/>.
 */

#include "KoReportPage.h"
20
#include <QWidget>
21
#include <kdebug.h>
22 23
#include <QColor>
#include <QPixmap>
24 25 26 27 28 29
#include <KoPageFormat.h>
#include <KoUnit.h>
#include <KoGlobal.h>

#include <renderobjects.h>
#include <QPainter>
30
#include <QTimer>
31 32

KoReportPage::KoReportPage(QWidget *parent, ORODocument *document)
Adam Pigg's avatar
Adam Pigg committed
33
        : QObject(parent), QGraphicsRectItem()
34
{
Adam Pigg's avatar
Adam Pigg committed
35
    //TODO setAttribute(Qt::WA_NoBackground);
36 37
    kDebug() << "CREATED PAGE";
    m_reportDocument = document;
38
    m_page = 0;
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
    int pageWidth = 0;
    int pageHeight = 0;

    if (m_reportDocument) {
        QString pageSize = m_reportDocument->pageOptions().getPageSize();


        if (pageSize == "Custom") {
            // if this is custom sized sheet of paper we will just use those values
            pageWidth = (int)(m_reportDocument->pageOptions().getCustomWidth());
            pageHeight = (int)(m_reportDocument->pageOptions().getCustomHeight());
        } else {
            // lookup the correct size information for the specified size paper
            pageWidth = m_reportDocument->pageOptions().widthPx();
            pageHeight = m_reportDocument->pageOptions().heightPx();
        }
    }

Adam Pigg's avatar
Adam Pigg committed
57
    setRect(0,0,pageWidth, pageHeight);
58 59

    kDebug() << "PAGE IS " << pageWidth << "x" << pageHeight;
60

61 62 63
    m_pixmap = new QPixmap(pageWidth, pageHeight);

    m_renderer = m_factory.createInstance("screen");
64 65
    
    connect(m_reportDocument, SIGNAL(updated(int)), this, SLOT(pageUpdated(int)));
66

67 68 69 70
    m_renderTimer = new QTimer();
    m_renderTimer->setSingleShot(true);
    connect(m_renderTimer, SIGNAL(timeout()), this, SLOT(renderCurrentPage()));
    
71 72 73 74 75 76
    renderPage(1);
}

KoReportPage::~KoReportPage()
{
    delete m_renderer;
77
    delete m_pixmap;
78
    delete m_renderTimer;
79 80
}

Adam Pigg's avatar
Adam Pigg committed
81
void KoReportPage::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
82
{
Jarosław Staniek's avatar
Jarosław Staniek committed
83 84
    Q_UNUSED(option);
    Q_UNUSED(widget);
Adam Pigg's avatar
Adam Pigg committed
85
    painter->drawPixmap(QPoint(0, 0), *m_pixmap);
86 87 88 89
}

void KoReportPage::renderPage(int page)
{
90
    m_page = page - 1;
91 92 93 94 95
    m_pixmap->fill();
    QPainter qp(m_pixmap);
    if (m_reportDocument) {
        KoReportRendererContext cxt;
        cxt.painter = &qp;
96
        m_renderer->render(cxt, m_reportDocument, m_page);
97
    }
98
    update();
99 100
}

101 102
void KoReportPage::pageUpdated(int pageNo)
{
103 104 105 106 107
    kDebug() << pageNo << m_page;
    //Refresh this page if it changes
    if (pageNo == m_page) {
        kDebug() << "Current page updated";
        m_renderTimer->start(100);
108 109 110
    }
}

111 112 113 114 115 116
void KoReportPage::renderCurrentPage()
{
    renderPage(m_page + 1);
}


117
#include "KoReportPage.moc"