LimeReport/limereport/lrpreviewreportwidget.cpp

323 lines
9.8 KiB
C++
Raw Normal View History

2016-03-30 23:04:59 +03:00
#include "lrpreviewreportwidget.h"
#include "ui_lrpreviewreportwidget.h"
#include <QPrinter>
#include <QPrintDialog>
#include <QPrinterInfo>
2016-03-30 23:04:59 +03:00
#include <QScrollBar>
#include <QFileDialog>
#include "lrpagedesignintf.h"
#include "lrreportrender.h"
#include "lrreportengine_p.h"
#include "lrpreviewreportwidget_p.h"
#include "serializators/lrxmlwriter.h"
namespace LimeReport {
bool PreviewReportWidgetPrivate::pageIsVisible(){
QGraphicsView* view = q_ptr->ui->graphicsView;
if ( m_currentPage-1 >= m_reportPages.size() || m_currentPage <= 0 )
return false;
2016-03-30 23:04:59 +03:00
PageItemDesignIntf::Ptr page = m_reportPages.at(m_currentPage-1);
return page->mapToScene(page->rect()).boundingRect().intersects(
view->mapToScene(view->viewport()->geometry()).boundingRect()
);
}
QRectF PreviewReportWidgetPrivate::calcPageShift(){
QGraphicsView *view = q_ptr->ui->graphicsView;
PageItemDesignIntf::Ptr page = m_reportPages.at(m_currentPage-1);
qreal pageHeight = page->mapToScene(page->boundingRect()).boundingRect().height();
qreal viewHeight = view->mapToScene(
0, view->viewport()->height()
).y() - view->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);
}
void PreviewReportWidgetPrivate::setPages(ReportPages pages)
{
m_reportPages = pages;
if (!m_reportPages.isEmpty()){
m_previewPage->setPageItems(m_reportPages);
m_changingPage = true;
m_currentPage = 1;
if (pages.at(0)) pages.at(0)->setSelected(true);
m_changingPage = false;
q_ptr->initPreview();
2016-04-02 03:38:23 +03:00
q_ptr->emitPageSet();
2016-03-30 23:04:59 +03:00
}
}
2016-04-02 03:38:23 +03:00
PageItemDesignIntf::Ptr PreviewReportWidgetPrivate::currentPage()
{
2016-04-05 23:06:11 +03:00
if (m_reportPages.count()>0 && m_reportPages.count()>=m_currentPage)
2016-04-02 03:38:23 +03:00
return m_reportPages.at(m_currentPage-1);
else return PageItemDesignIntf::Ptr(0);
}
PreviewReportWidget::PreviewReportWidget(ReportEngine *report, QWidget *parent) :
2016-03-30 23:04:59 +03:00
QWidget(parent),
ui(new Ui::PreviewReportWidget), d_ptr(new PreviewReportWidgetPrivate(this))
{
ui->setupUi(this);
d_ptr->m_report = report->d_ptr;
d_ptr->m_previewPage = d_ptr->m_report->createPreviewPage();
2016-03-30 23:04:59 +03:00
d_ptr->m_previewPage->setItemMode( LimeReport::PreviewMode );
2016-03-30 23:04:59 +03:00
ui->errorsView->setVisible(false);
connect(ui->graphicsView->verticalScrollBar(),SIGNAL(valueChanged(int)), this, SLOT(slotSliderMoved(int)));
connect(d_ptr->m_report, SIGNAL(destroyed(QObject*)), this, SLOT(reportEngineDestroyed(QObject*)));
d_ptr->m_zoomer = new GraphicsViewZoomer(ui->graphicsView);
2016-04-05 23:06:11 +03:00
connect(d_ptr->m_zoomer, SIGNAL(zoomed(double)), this, SLOT(slotZoomed(double)));
2016-03-30 23:04:59 +03:00
}
PreviewReportWidget::~PreviewReportWidget()
{
delete d_ptr->m_previewPage;
d_ptr->m_previewPage = 0;
delete d_ptr->m_zoomer;
delete d_ptr;
delete ui;
}
void PreviewReportWidget::initPreview()
{
2016-04-02 03:38:23 +03:00
if (ui->graphicsView->scene()!=d_ptr->m_previewPage)
ui->graphicsView->setScene(d_ptr->m_previewPage);
2016-04-01 23:39:07 +03:00
ui->graphicsView->resetMatrix();
2016-03-30 23:04:59 +03:00
ui->graphicsView->centerOn(0, 0);
2016-04-02 03:38:23 +03:00
setScalePercent(d_ptr->m_scalePercent);
2016-03-30 23:04:59 +03:00
}
void PreviewReportWidget::setErrorsMesagesVisible(bool visible)
{
ui->errorsView->setVisible(visible);
}
2016-04-02 03:38:23 +03:00
void PreviewReportWidget::zoomIn()
2016-03-30 23:04:59 +03:00
{
2016-04-05 23:06:11 +03:00
d_ptr->m_scalePercent = (d_ptr->m_scalePercent / 10) * 10 + 10;
2016-04-02 03:38:23 +03:00
setScalePercent(d_ptr->m_scalePercent);
2016-03-30 23:04:59 +03:00
}
2016-04-02 03:38:23 +03:00
void PreviewReportWidget::zoomOut()
2016-03-30 23:04:59 +03:00
{
2016-04-02 03:38:23 +03:00
if (d_ptr->m_scalePercent>0)
2016-04-05 23:06:11 +03:00
d_ptr->m_scalePercent = (d_ptr->m_scalePercent / 10) * 10 - 10;
2016-04-02 03:38:23 +03:00
setScalePercent(d_ptr->m_scalePercent);
2016-03-30 23:04:59 +03:00
}
2016-04-02 03:38:23 +03:00
void PreviewReportWidget::firstPage()
2016-03-30 23:04:59 +03:00
{
d_ptr->m_changingPage=true;
if ((!d_ptr->m_reportPages.isEmpty())&&(d_ptr->m_currentPage>1)){
d_ptr->m_currentPage=1;
ui->graphicsView->ensureVisible(d_ptr->calcPageShift(), 0, 0);
emit pageChanged(d_ptr->m_currentPage);
}
d_ptr->m_changingPage=false;
}
2016-04-02 03:38:23 +03:00
void PreviewReportWidget::priorPage()
2016-03-30 23:04:59 +03:00
{
d_ptr->m_changingPage=true;
if ((!d_ptr->m_reportPages.isEmpty())&&(d_ptr->m_currentPage>1)){
d_ptr->m_currentPage--;
ui->graphicsView->ensureVisible(d_ptr->calcPageShift(), 0, 0);
emit pageChanged(d_ptr->m_currentPage);
}
d_ptr->m_changingPage=false;
}
2016-04-02 03:38:23 +03:00
void PreviewReportWidget::nextPage()
2016-03-30 23:04:59 +03:00
{
d_ptr->m_changingPage=true;
if ((!d_ptr->m_reportPages.isEmpty())&&(d_ptr->m_reportPages.count()>(d_ptr->m_currentPage))){
d_ptr->m_currentPage++;
ui->graphicsView->ensureVisible(d_ptr->calcPageShift(), 0, 0);
emit pageChanged(d_ptr->m_currentPage);
}
d_ptr->m_changingPage=false;
}
2016-04-02 03:38:23 +03:00
void PreviewReportWidget::lastPage()
2016-03-30 23:04:59 +03:00
{
d_ptr->m_changingPage=true;
if ((!d_ptr->m_reportPages.isEmpty())&&(d_ptr->m_reportPages.count()>(d_ptr->m_currentPage))){
d_ptr->m_currentPage=d_ptr->m_reportPages.count();
ui->graphicsView->ensureVisible(d_ptr->calcPageShift(), 0, 0);
emit pageChanged(d_ptr->m_currentPage);
}
d_ptr->m_changingPage=false;
}
2016-04-02 03:38:23 +03:00
void PreviewReportWidget::print()
2016-03-30 23:04:59 +03:00
{
QPrinterInfo pi;
2016-03-30 23:04:59 +03:00
QPrinter printer(QPrinter::HighResolution);
if (!pi.defaultPrinter().isNull())
2017-04-19 01:04:14 +03:00
#ifdef HAVE_QT4
printer.setPrinterName(pi.defaultPrinter().printerName());
#endif
#ifdef HAVE_QT5
printer.setPrinterName(pi.defaultPrinterName());
#endif
2016-03-30 23:04:59 +03:00
QPrintDialog dialog(&printer,QApplication::activeWindow());
if (dialog.exec()==QDialog::Accepted){
if (!d_ptr->m_reportPages.isEmpty())
ReportEnginePrivate::printReport(
d_ptr->m_reportPages,
2017-04-21 23:03:56 +03:00
printer
2016-03-30 23:04:59 +03:00
);
2016-04-02 03:38:23 +03:00
foreach(PageItemDesignIntf::Ptr pageItem, d_ptr->m_reportPages){
d_ptr->m_previewPage->reactivatePageItem(pageItem);
}
2016-03-30 23:04:59 +03:00
}
}
2016-04-02 03:38:23 +03:00
void PreviewReportWidget::printToPDF()
2016-03-30 23:04:59 +03:00
{
2017-02-03 14:18:28 +03:00
QString filter = "PDF (*.pdf)";
QString fileName = QFileDialog::getSaveFileName(this,tr("PDF file name"),"","PDF (*.pdf)");
2016-03-30 23:04:59 +03:00
if (!fileName.isEmpty()){
2017-02-03 14:18:28 +03:00
QFileInfo fi(fileName);
if (fi.suffix().isEmpty())
fileName+=".pdf";
2016-03-30 23:04:59 +03:00
QPrinter printer;
printer.setOutputFileName(fileName);
printer.setOutputFormat(QPrinter::PdfFormat);
if (!d_ptr->m_reportPages.isEmpty()){
2017-04-21 23:03:56 +03:00
ReportEnginePrivate::printReport(d_ptr->m_reportPages,printer);
2016-03-30 23:04:59 +03:00
}
foreach(PageItemDesignIntf::Ptr pageItem, d_ptr->m_reportPages){
d_ptr->m_previewPage->reactivatePageItem(pageItem);
}
}
}
2016-04-02 03:38:23 +03:00
void PreviewReportWidget::pageNavigatorChanged(int value)
2016-03-30 23:04:59 +03:00
{
if (d_ptr->m_changingPage) return;
d_ptr->m_changingPage = true;
if ((!d_ptr->m_reportPages.isEmpty())&&(d_ptr->m_reportPages.count() >= value) && value>0){
d_ptr->m_currentPage = value;
ui->graphicsView->ensureVisible(d_ptr->calcPageShift(), 0, 0);
}
d_ptr->m_changingPage=false;
}
2016-04-02 03:38:23 +03:00
void PreviewReportWidget::saveToFile()
2016-03-30 23:04:59 +03:00
{
QString fileName = QFileDialog::getSaveFileName(this,tr("Report file name"));
if (!fileName.isEmpty()){
QScopedPointer< ItemsWriterIntf > writer(new XMLWriter());
foreach (PageItemDesignIntf::Ptr page, d_ptr->m_reportPages){
writer->putItem(page.data());
}
writer->saveToFile(fileName);
}
}
2016-04-02 03:38:23 +03:00
void PreviewReportWidget::setScalePercent(int percent)
{
ui->graphicsView->resetMatrix();
d_ptr->m_scalePercent = percent;
2016-04-02 03:38:23 +03:00
qreal scaleSize = percent/100.0;
ui->graphicsView->scale(scaleSize, scaleSize);
emit scalePercentChanged(percent);
}
void PreviewReportWidget::fitWidth()
{
if (d_ptr->currentPage()){
qreal scalePercent = ui->graphicsView->viewport()->width() / ui->graphicsView->scene()->width();
setScalePercent(scalePercent*100);
}
}
void PreviewReportWidget::fitPage()
{
if (d_ptr->currentPage()){
qreal vScale = ui->graphicsView->viewport()->width() / ui->graphicsView->scene()->width();
qreal hScale = ui->graphicsView->viewport()->height() / d_ptr->currentPage()->height();
setScalePercent(qMin(vScale,hScale)*100);
}
}
2016-03-30 23:04:59 +03:00
void PreviewReportWidget::setErrorMessages(const QStringList &value)
{
foreach (QString line, value) {
ui->errorsView->append(line);
}
}
2016-04-02 03:38:23 +03:00
void PreviewReportWidget::emitPageSet()
{
emit pagesSet(d_ptr->m_reportPages.count());
}
2016-03-30 23:04:59 +03:00
void PreviewReportWidget::refreshPages()
{
if (d_ptr->m_report){
try{
d_ptr->m_report->dataManager()->setDesignTime(false);
ReportPages pages = d_ptr->m_report->renderToPages();
d_ptr->m_report->dataManager()->setDesignTime(true);
if (pages.count()>0){
2016-04-01 23:39:07 +03:00
d_ptr->setPages(pages);
2016-03-30 23:04:59 +03:00
}
} catch (ReportError &exception){
d_ptr->m_report->saveError(exception.what());
d_ptr->m_report->showError(exception.what());
}
}
}
void PreviewReportWidget::slotSliderMoved(int value)
{
if (ui->graphicsView->verticalScrollBar()->minimum()==value){
d_ptr->m_currentPage = 1;
} else if (ui->graphicsView->verticalScrollBar()->maximum()==value){
d_ptr->m_currentPage = d_ptr->m_reportPages.count();
}
if (!d_ptr->pageIsVisible()){
if (value>d_ptr->m_priorScrolValue){
d_ptr->m_currentPage++;
} else {
d_ptr->m_currentPage--;
}
}
d_ptr->m_changingPage = true;
emit pageChanged(d_ptr->m_currentPage);
d_ptr->m_changingPage = false;
d_ptr->m_priorScrolValue = value;
}
void PreviewReportWidget::reportEngineDestroyed(QObject *object)
{
if (object == d_ptr->m_report){
d_ptr->m_report = 0;
}
}
2016-04-05 23:06:11 +03:00
void PreviewReportWidget::slotZoomed(double )
{
d_ptr->m_scalePercent = ui->graphicsView->matrix().m11()*100;
emit scalePercentChanged(d_ptr->m_scalePercent);
}
2016-03-30 23:04:59 +03:00
}