#include "lrpreviewreportwidget.h"
#include "ui_lrpreviewreportwidget.h"
#include "lrpagedesignintf.h"
#include "serializators/lrstorageintf.h"
#include "serializators/lrxmlreader.h"
#include "serializators/lrxmlreader.h"
#include "serializators/lrxmlwriter.h"
#include "lrreportengine_p.h"
#include <QPrinter>
#include <QPrintDialog>
#include <QFileDialog>
#include <QScrollBar>
#include <QDesktopWidget>
PreviewReportWidget::PreviewReportWidget(ReportEnginePrivate *report, QWidget *parent) :
QWidget(parent),
ui(new Ui::PreviewReportWidget)
{
ui->setupUi(this);
m_currentPage = 1;
m_changingPage = false;
m_priorScrolValue = 0;
m_report = report;
m_previewPage = m_report->createPreviewPage();
m_previewPage->setItemMode( LimeReport::PreviewMode );
connect(ui->graphicsView->verticalScrollBar(),SIGNAL(valueChanged(int)), this, SLOT(slotSliderMoved(int)));
}
PreviewReportWidget::~PreviewReportWidget()
{
delete ui;
}
void PreviewReportWidget::actualiza()
{
//QTime start = QTime::currentTime();
m_report->dataManager()->setDesignTime(false);
m_reportPages = m_report->renderToPages();
m_report->dataManager()->setDesignTime(true);
if (!m_reportPages.isEmpty()){
//m_previewPage->setPageItem(m_reportPages.at(0));
m_previewPage->setPageItems(m_reportPages);
m_changingPage = true;
ui->graphicsView->setScene(m_previewPage);
ui->graphicsView->centerOn(0, 0);
ui->graphicsView->scale(0.5,0.5);
m_currentPage = 1;
emit pageChanged(m_currentPage);
if (m_reportPages.at(0)) m_reportPages.at(0)->setSelected(true);
m_changingPage = false;
//qDebug()<<"render time ="<<start.msecsTo(QTime::currentTime());
}
}
void PreviewReportWidget::slotZoomIn()
{
ui->graphicsView->scale(1.2,1.2);
}
void PreviewReportWidget::slotZoomOut()
{
ui->graphicsView->scale(1/1.2,1/1.2);
}
void PreviewReportWidget::slotFirstPage()
{
m_changingPage=true;
if ((!m_reportPages.isEmpty())&&(m_currentPage>1)){
m_currentPage=1;
ui->graphicsView->ensureVisible(
calcPageShift(m_reportPages.at(m_currentPage-1)), 0, 0
);
emit pageChanged(m_currentPage);
}
m_changingPage=false;
}
void PreviewReportWidget::slotPriorPage()
{
m_changingPage=true;
if ((!m_reportPages.isEmpty())&&(m_currentPage>1)){
m_currentPage--;
ui->graphicsView->ensureVisible(
calcPageShift(m_reportPages.at(m_currentPage-1)), 0, 0
);
emit pageChanged(m_currentPage);
}
m_changingPage=false;
}
void PreviewReportWidget::slotNextPage()
{
m_changingPage=true;
if ((!m_reportPages.isEmpty())&&(m_reportPages.count()>(m_currentPage))){
m_currentPage++;
ui->graphicsView->ensureVisible(
calcPageShift(m_reportPages.at(m_currentPage-1)), 0, 0
);
emit pageChanged(m_currentPage);
}
m_changingPage=false;
}
void PreviewReportWidget::slotLastPage()
{
m_changingPage=true;
if ((!m_reportPages.isEmpty())&&(m_reportPages.count()>(m_currentPage))){
m_currentPage=m_reportPages.count();
ui->graphicsView->ensureVisible(
calcPageShift(m_reportPages.at(m_currentPage-1)), 0, 0
);
emit pageChanged(m_currentPage);
}
m_changingPage=false;
}
void PreviewReportWidget::slotPrint()
{
QPrinter printer(QPrinter::HighResolution);
QPrintDialog dialog(&printer,QApplication::activeWindow());
if (dialog.exec()==QDialog::Accepted){
if (!m_reportPages.isEmpty())
ReportEnginePrivate::printReport(
m_reportPages,
printer,
PrintRange(dialog.printRange(),dialog.fromPage(),dialog.toPage())
);
/*
else ReportEnginePrivate::printReport(m_reader,printer);
*/
foreach(PageItemDesignIntf::Ptr pageItem, m_reportPages){
m_previewPage->reactivatePageItem(pageItem);
}
}
}
void PreviewReportWidget::slotPrintToPDF()
{
QString fileName = QFileDialog::getSaveFileName(this,tr("PDF file name"),"","PDF(*.pdf)" );
//qDebug()<<fileName;
if (!fileName.isEmpty()){
QPrinter printer;
printer.setOutputFileName(fileName);
printer.setOutputFormat(QPrinter::PdfFormat);
if (!m_reportPages.isEmpty()){
ReportEnginePrivate::printReport(m_reportPages,printer,PrintRange());
}
/*
else {
ReportEnginePrivate::printReport(m_reader,printer);
}
*/
foreach(PageItemDesignIntf::Ptr pageItem, m_reportPages){
m_previewPage->reactivatePageItem(pageItem);
}
}
}
void PreviewReportWidget::slotPageNavigatorChanged(int value)
{
if (m_changingPage) return;
m_changingPage = true;
if ((!m_reportPages.isEmpty())&&(m_reportPages.count() >= value) && value>0){
m_currentPage = value;
//m_previewPage->setPageItem(m_reportPages.at(m_currentPage-1));
ui->graphicsView->ensureVisible(
calcPageShift(m_reportPages.at(m_currentPage-1)), 0, 0
);
}
m_changingPage=false;
}
void PreviewReportWidget::slotSliderMoved(int value)
{
if (ui->graphicsView->verticalScrollBar()->minimum()==value){
m_currentPage = 1;
} else if (ui->graphicsView->verticalScrollBar()->maximum()==value){
m_currentPage = m_reportPages.count();
}
if (!pageIsVisible(m_reportPages.at(m_currentPage-1))){
if (value>m_priorScrolValue){
m_currentPage++;
} else {
m_currentPage--;
}
}
m_changingPage = true;
emit pageChanged(m_currentPage);
m_changingPage = false;
m_priorScrolValue = value;
}
QRectF PreviewReportWidget::calcPageShift(PageItemDesignIntf::Ptr page)
{
qreal pageHeight = page->mapToScene(page->boundingRect()).boundingRect().height();
qreal viewHeight = ui->graphicsView->mapToScene(
0,ui->graphicsView->viewport()->height()
).y()-ui->graphicsView->mapToScene(0,0).y();
viewHeight = (pageHeight<viewHeight)?pageHeight:viewHeight;
QRectF pageStartPos = m_reportPages.at(m_currentPage-1)->mapRectToScene(
m_reportPages.at(m_currentPage-1)->rect()
);
return QRectF(0,pageStartPos.y(),0,viewHeight);
}
bool PreviewReportWidget::pageIsVisible(PageItemDesignIntf::Ptr page)
{
return page->mapToScene(page->rect()).boundingRect().intersects(
ui->graphicsView->mapToScene(ui->graphicsView->viewport()->geometry()).boundingRect()
);
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#ifndef LRPREVIEWREPORTWIDGET_H
#define LRPREVIEWREPORTWIDGET_H
#include <QWidget>
#include "lrreportrender.h"
namespace Ui {
class PreviewReportWidget;
}
using namespace LimeReport;
class LIMEREPORT_EXPORT PreviewReportWidget : public QWidget
{
Q_OBJECT
public:
explicit PreviewReportWidget(ReportEnginePrivate *report, QWidget *parent=0);
~PreviewReportWidget();
void actualiza();
public slots:
void slotZoomIn();
void slotZoomOut();
void slotFirstPage();
void slotPriorPage();
void slotNextPage();
void slotLastPage();
void slotPrint();
void slotPrintToPDF();
void slotPageNavigatorChanged(int value);
private slots:
void slotSliderMoved(int value);
QRectF calcPageShift(PageItemDesignIntf::Ptr page);
private:
bool pageIsVisible(PageItemDesignIntf::Ptr page);
private:
Ui::PreviewReportWidget *ui;
ReportEnginePrivate *m_report;
PageDesignIntf* m_previewPage;
ReportPages m_reportPages;
bool m_changingPage;
int m_currentPage;
int m_priorScrolValue;
signals:
void pageChanged(int page);
};
#endif // LRPREVIEWREPORTWIDGET_H
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////