diff --git a/include/lrpreviewreportwidget.h b/include/lrpreviewreportwidget.h new file mode 100644 index 0000000..94e9936 --- /dev/null +++ b/include/lrpreviewreportwidget.h @@ -0,0 +1,53 @@ +#ifndef LRPREVIEWREPORTWIDGET_H +#define LRPREVIEWREPORTWIDGET_H + +#include +#include "lrglobal.h" + +namespace LimeReport { + +namespace Ui { +class PreviewReportWidget; +} + +class PreviewReportWidgetPrivate; +class ReportEnginePrivate; + +class LIMEREPORT_EXPORT PreviewReportWidget : public QWidget +{ + Q_OBJECT + friend class ReportEnginePrivate; + friend class PreviewReportWindow; + friend class PreviewReportWidgetPrivate; +public: + explicit PreviewReportWidget(ReportEnginePrivate *report, QWidget *parent = 0); + ~PreviewReportWidget(); + void initPreview(); + void setErrorsMesagesVisible(bool visible); + void setErrorMessages(const QStringList &value); + void refreshPages(); +public slots: + void slotZoomIn(); + void slotZoomOut(); + + void slotFirstPage(); + void slotPriorPage(); + void slotNextPage(); + void slotLastPage(); + + void slotPrint(); + void slotPrintToPDF(); + void slotPageNavigatorChanged(int value); + void slotSaveToFile(); +signals: + void pageChanged(int page); +private slots: + void slotSliderMoved(int value); + void reportEngineDestroyed(QObject* object); +private: + Ui::PreviewReportWidget *ui; + PreviewReportWidgetPrivate* d_ptr; +}; + +} // namespace LimeReport +#endif // LRPREVIEWREPORTWIDGET_H diff --git a/limereport/limereport.pri b/limereport/limereport.pri index e47b1c9..8b82707 100644 --- a/limereport/limereport.pri +++ b/limereport/limereport.pri @@ -81,6 +81,8 @@ SOURCES += \ $$REPORT_PATH/lrreportrender.cpp \ $$REPORT_PATH/lrscriptenginemanager.cpp \ $$REPORT_PATH/lrpreviewreportwindow.cpp \ + $$REPORT_PATH/lrpreviewreportwidget.cpp \ + $$REPORT_PATH/lrgraphicsviewzoom.cpp \ $$REPORT_PATH/lrvariablesholder.cpp \ $$REPORT_PATH/lrgroupfunctions.cpp \ $$REPORT_PATH/lrsimplecrypt.cpp \ @@ -157,6 +159,8 @@ HEADERS += \ $$REPORT_PATH/bands/lrsubdetailband.h \ $$REPORT_PATH/lrreportrender.h \ $$REPORT_PATH/lrpreviewreportwindow.h \ + $$REPORT_PATH/lrpreviewreportwidget.h \ + $$REPORT_PATH/lrgraphicsviewzoom.h \ $$REPORT_PATH/objectinspector/propertyItems/lrfontpropitem.h \ $$REPORT_PATH/objectinspector/editors/lrfonteditor.h \ $$REPORT_PATH/items/lrimageitem.h \ @@ -178,7 +182,8 @@ HEADERS += \ $$REPORT_PATH/lrsimplecrypt.h \ $$REPORT_PATH/lraboutdialog.h \ $$REPORT_PATH/lrcallbackdatasourceintf.h \ - $$REPORT_PATH/lrsettingdialog.h + $$REPORT_PATH/lrsettingdialog.h \ + $$PWD/lrpreviewreportwidget_p.h contains(CONFIG,zint){ HEADERS += $$REPORT_PATH/items/lrbarcodeitem.h @@ -191,6 +196,7 @@ FORMS += \ $$REPORT_PATH/databrowser/lrvariabledialog.ui \ $$REPORT_PATH/objectinspector/editors/ltextitempropertyeditor.ui \ $$REPORT_PATH/lrpreviewreportwindow.ui \ + $$REPORT_PATH/lrpreviewreportwidget.ui \ $$REPORT_PATH/items/lrtextitemeditor.ui \ $$REPORT_PATH/lraboutdialog.ui \ $$REPORT_PATH/lrsettingdialog.ui diff --git a/limereport/lrpreviewreportwidget.cpp b/limereport/lrpreviewreportwidget.cpp new file mode 100644 index 0000000..7becc8d --- /dev/null +++ b/limereport/lrpreviewreportwidget.cpp @@ -0,0 +1,259 @@ +#include "lrpreviewreportwidget.h" +#include "ui_lrpreviewreportwidget.h" + +#include +#include +#include +#include + +#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; + 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 = (pageHeightmapRectToScene( + 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(); + } +} + +PreviewReportWidget::PreviewReportWidget(ReportEnginePrivate *report, QWidget *parent) : + QWidget(parent), + ui(new Ui::PreviewReportWidget), d_ptr(new PreviewReportWidgetPrivate(this)) +{ + ui->setupUi(this); + d_ptr->m_previewPage = report->createPreviewPage(); + d_ptr->m_previewPage->setItemMode( LimeReport::PreviewMode ); + d_ptr->m_report = report; + + 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); +} + +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() +{ + ui->graphicsView->setScene(d_ptr->m_previewPage); + ui->graphicsView->centerOn(0, 0); + ui->graphicsView->scale(0.5,0.5); +} + +void PreviewReportWidget::setErrorsMesagesVisible(bool visible) +{ + ui->errorsView->setVisible(visible); +} + +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() +{ + 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; +} + +void PreviewReportWidget::slotPriorPage() +{ + 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; +} + +void PreviewReportWidget::slotNextPage() +{ + 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; +} + +void PreviewReportWidget::slotLastPage() +{ + 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; +} + +void PreviewReportWidget::slotPrint() +{ + QPrinter printer(QPrinter::HighResolution); + QPrintDialog dialog(&printer,QApplication::activeWindow()); + if (dialog.exec()==QDialog::Accepted){ + if (!d_ptr->m_reportPages.isEmpty()) + ReportEnginePrivate::printReport( + d_ptr->m_reportPages, + printer, + PrintRange(dialog.printRange(),dialog.fromPage(),dialog.toPage()) + ); + } +} + +void PreviewReportWidget::slotPrintToPDF() +{ + QString fileName = QFileDialog::getSaveFileName(this,tr("PDF file name"),"","PDF(*.pdf)" ); + if (!fileName.isEmpty()){ + QPrinter printer; + printer.setOutputFileName(fileName); + printer.setOutputFormat(QPrinter::PdfFormat); + if (!d_ptr->m_reportPages.isEmpty()){ + ReportEnginePrivate::printReport(d_ptr->m_reportPages,printer,PrintRange()); + } + foreach(PageItemDesignIntf::Ptr pageItem, d_ptr->m_reportPages){ + d_ptr->m_previewPage->reactivatePageItem(pageItem); + } + } +} + +void PreviewReportWidget::slotPageNavigatorChanged(int value) +{ + 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; +} + +void PreviewReportWidget::slotSaveToFile() +{ + 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); + } +} + +void PreviewReportWidget::setErrorMessages(const QStringList &value) +{ + foreach (QString line, value) { + ui->errorsView->append(line); + } +} + +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){ + d_ptr->m_reportPages = pages; + if (!d_ptr->m_reportPages.isEmpty()){ + d_ptr->m_previewPage->setPageItems(d_ptr->m_reportPages); + d_ptr->m_changingPage = true; + d_ptr->m_currentPage = 1; + if (pages.at(0)) pages.at(0)->setSelected(true); + d_ptr->m_changingPage = false; + } + ui->graphicsView->centerOn(0, 0); + } + } 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; + } +} + + + +} diff --git a/limereport/lrpreviewreportwidget.h b/limereport/lrpreviewreportwidget.h new file mode 100644 index 0000000..220843b --- /dev/null +++ b/limereport/lrpreviewreportwidget.h @@ -0,0 +1,54 @@ +#ifndef LRPREVIEWREPORTWIDGET_H +#define LRPREVIEWREPORTWIDGET_H + +#include +#include "lrglobal.h" + +namespace LimeReport { + +namespace Ui { +class PreviewReportWidget; +} + +class PreviewReportWidgetPrivate; +class ReportEnginePrivate; + +class LIMEREPORT_EXPORT PreviewReportWidget : public QWidget +{ + Q_OBJECT + friend class ReportEnginePrivate; + friend class PreviewReportWindow; + friend class PreviewReportWidgetPrivate; +public: + explicit PreviewReportWidget(ReportEnginePrivate *report, QWidget *parent = 0); + ~PreviewReportWidget(); +public slots: + void refreshPages(); + void slotZoomIn(); + void slotZoomOut(); + + void slotFirstPage(); + void slotPriorPage(); + void slotNextPage(); + void slotLastPage(); + + void slotPrint(); + void slotPrintToPDF(); + void slotPageNavigatorChanged(int value); + void slotSaveToFile(); +signals: + void pageChanged(int page); +private slots: + void slotSliderMoved(int value); + void reportEngineDestroyed(QObject* object); +private: + void initPreview(); + void setErrorsMesagesVisible(bool visible); + void setErrorMessages(const QStringList &value); +private: + Ui::PreviewReportWidget *ui; + PreviewReportWidgetPrivate* d_ptr; +}; + +} // namespace LimeReport +#endif // LRPREVIEWREPORTWIDGET_H diff --git a/limereport/lrpreviewreportwidget.ui b/limereport/lrpreviewreportwidget.ui new file mode 100644 index 0000000..9580fc9 --- /dev/null +++ b/limereport/lrpreviewreportwidget.ui @@ -0,0 +1,39 @@ + + + LimeReport::PreviewReportWidget + + + + 0 + 0 + 740 + 418 + + + + Form + + + + 2 + + + 2 + + + 2 + + + 2 + + + + + + + + + + + + diff --git a/limereport/lrpreviewreportwidget_p.h b/limereport/lrpreviewreportwidget_p.h new file mode 100644 index 0000000..6d2a8b9 --- /dev/null +++ b/limereport/lrpreviewreportwidget_p.h @@ -0,0 +1,33 @@ +#ifndef LRPREVIEWREPORTWIDGET_P_H +#define LRPREVIEWREPORTWIDGET_P_H + +#include "lrpagedesignintf.h" +#include "lrreportrender.h" +#include "lrgraphicsviewzoom.h" + +namespace LimeReport{ + +class PreviewReportWidget; + +class PreviewReportWidgetPrivate +{ +public: + PreviewReportWidgetPrivate(PreviewReportWidget* previewReportWidget): q_ptr(previewReportWidget), + m_currentPage(1), m_changingPage(false), m_priorScrolValue(0){} + bool pageIsVisible(); + QRectF calcPageShift(); + void setPages( ReportPages pages); +public: + PageDesignIntf* m_previewPage; + ReportPages m_reportPages; + ReportEnginePrivate* m_report; + GraphicsViewZoomer* m_zoomer; + int m_currentPage; + bool m_changingPage; + int m_priorScrolValue; + PreviewReportWidget* q_ptr; + +}; + +} +#endif // LRPREVIEWREPORTWIDGET_P_H