diff --git a/demo_r1/mainwindow.cpp b/demo_r1/mainwindow.cpp index 7f35de3..05488ce 100644 --- a/demo_r1/mainwindow.cpp +++ b/demo_r1/mainwindow.cpp @@ -37,6 +37,7 @@ #include #include #include +#include #ifdef BUILD_WITH_EASY_PROFILER #include "easy/profiler.h" @@ -148,6 +149,13 @@ void MainWindow::on_pushButton_2_clicked() #ifdef BUILD_WITH_EASY_PROFILER profiler::dumpBlocksToFile("test.prof"); #endif +// QPrinter* printer = new QPrinter; +// QPrintDialog dialog(printer); +// if (dialog.exec()){ +// QMap printers; +// printers.insert("default",printer); +// report->printReport(printers); +// } report->previewReport(); } } diff --git a/include/lrreportengine.h b/include/lrreportengine.h index 2abf45b..aa8bd6a 100644 --- a/include/lrreportengine.h +++ b/include/lrreportengine.h @@ -80,6 +80,7 @@ public: explicit ReportEngine(QObject *parent = 0); ~ReportEngine(); bool printReport(QPrinter *printer=0); + bool printReport(QMap printers); bool printPages(ReportPages pages, QPrinter *printer); void printToFile(const QString& fileName); PageDesignIntf *createPreviewScene(QObject *parent = 0); diff --git a/limereport/lrpageitemdesignintf.cpp b/limereport/lrpageitemdesignintf.cpp index d6e993c..0f8d98a 100644 --- a/limereport/lrpageitemdesignintf.cpp +++ b/limereport/lrpageitemdesignintf.cpp @@ -342,6 +342,16 @@ void PageItemDesignIntf::initColumnsPos(QVector &posByColumns, qreal pos, } } +QString PageItemDesignIntf::printerName() const +{ + return m_printerName; +} + +void PageItemDesignIntf::setPrinterName(const QString& printerName) +{ + m_printerName = printerName; +} + bool PageItemDesignIntf::isPrintable() const { return m_printable; diff --git a/limereport/lrpageitemdesignintf.h b/limereport/lrpageitemdesignintf.h index 24ab7ae..39c0fe3 100644 --- a/limereport/lrpageitemdesignintf.h +++ b/limereport/lrpageitemdesignintf.h @@ -60,6 +60,7 @@ class PageItemDesignIntf : public LimeReport::ItemsContainerDesignInft Q_PROPERTY(bool setPageSizeToPrinter READ getSetPageSizeToPrinter WRITE setSetPageSizeToPrinter ) Q_PROPERTY(bool endlessHeight READ endlessHeight WRITE setEndlessHeight) Q_PROPERTY(bool printable READ isPrintable WRITE setPrintable) + Q_PROPERTY(QString printerName READ printerName WRITE setPrinterName) friend class ReportRender; public: enum Orientation { Portrait = QPrinter::Portrait, Landscape = QPrinter::Landscape }; @@ -151,6 +152,9 @@ public: bool isPrintable() const; void setPrintable(bool printable); + QString printerName() const; + void setPrinterName(const QString& printerName); + signals: void beforeFirstPageRendered(); void afterLastPageRendered(); @@ -190,6 +194,7 @@ private: bool m_setPageSizeToPrinter; bool m_endlessHeight; bool m_printable; + QString m_printerName; }; typedef QList ReportPages; diff --git a/limereport/lrreportengine.cpp b/limereport/lrreportengine.cpp index 6c62d4a..c7a1233 100644 --- a/limereport/lrreportengine.cpp +++ b/limereport/lrreportengine.cpp @@ -289,19 +289,10 @@ void ReportEnginePrivate::printReport(ItemsReaderIntf::Ptr reader, QPrinter& pri void ReportEnginePrivate::printReport(ReportPages pages, QPrinter &printer) { - LimeReport::PageDesignIntf renderPage; - renderPage.setItemMode(PrintMode); - QPainter* painter=0; - - bool isFirst = true; int currenPage = 1; - - - qreal leftMargin, topMargin, rightMargin, bottomMargin; - printer.getPageMargins(&leftMargin, &topMargin, &rightMargin, &bottomMargin, QPrinter::Millimeter); - + QMap> printProcessors; + printProcessors.insert("default",QSharedPointer(new PrintProcessor(&printer))); foreach(PageItemDesignIntf::Ptr page, pages){ - if ( (printer.printRange() == QPrinter::AllPages) || ( (printer.printRange()==QPrinter::PageRange) && @@ -310,76 +301,32 @@ void ReportEnginePrivate::printReport(ReportPages pages, QPrinter &printer) ) ) { - - QPointF pagePos = page->pos(); - - page->setPos(0,0); - renderPage.setPageItem(page); - renderPage.setSceneRect(renderPage.pageItem()->mapToScene(renderPage.pageItem()->rect()).boundingRect()); - if (renderPage.pageItem()->oldPrintMode()){ - printer.setPageMargins(renderPage.pageItem()->leftMargin(), - renderPage.pageItem()->topMargin(), - renderPage.pageItem()->rightMargin(), - renderPage.pageItem()->bottomMargin(), - QPrinter::Millimeter); - printer.setOrientation(static_cast(renderPage.pageItem()->pageOrientation())); - QSizeF pageSize = (renderPage.pageItem()->pageOrientation()==PageItemDesignIntf::Landscape)? - QSizeF(renderPage.pageItem()->sizeMM().height(),renderPage.pageItem()->sizeMM().width()): - renderPage.pageItem()->sizeMM(); - printer.setPaperSize(pageSize,QPrinter::Millimeter); - } else { - printer.setFullPage(renderPage.pageItem()->fullPage()); - printer.setOrientation(static_cast(renderPage.pageItem()->pageOrientation())); - if (renderPage.pageItem()->pageSize()==PageItemDesignIntf::Custom){ - QSizeF pageSize = (renderPage.pageItem()->pageOrientation()==PageItemDesignIntf::Landscape)? - QSizeF(renderPage.pageItem()->sizeMM().height(),renderPage.pageItem()->sizeMM().width()): - renderPage.pageItem()->sizeMM(); - if (page->getSetPageSizeToPrinter() || printer.outputFormat() == QPrinter::PdfFormat) - printer.setPaperSize(pageSize,QPrinter::Millimeter); - } else { - if (page->getSetPageSizeToPrinter() || printer.outputFormat() == QPrinter::PdfFormat) - printer.setPaperSize(static_cast(renderPage.pageItem()->pageSize())); - } - } - - if (!isFirst){ - printer.newPage(); - } else { - isFirst=false; - painter = new QPainter(&printer); - if (!painter->isActive()){ - delete painter; - return; - } - } - - QRectF printerPageRect = printer.pageRect(QPrinter::Millimeter); - printerPageRect = QRectF(0,0,(printerPageRect.size().width() + rightMargin + leftMargin) * Const::mmFACTOR, - (printerPageRect.size().height() + bottomMargin +topMargin) * Const::mmFACTOR); - - if (printer.pageSize() != static_cast(page->pageSize()) && - printerPageRect.width() < page->geometry().width()) - { - qreal pageWidth = page->geometry().width(); - QRectF currentPrintingRect = printerPageRect; - while (pageWidth>0){ - renderPage.render(painter, printer.pageRect(), currentPrintingRect); - currentPrintingRect.adjust(printerPageRect.size().width(),0,printerPageRect.size().width(),0); - pageWidth -= printerPageRect.size().width(); - if (pageWidth>0) printer.newPage(); - } - - } else { - renderPage.render(painter); - } - - - page->setPos(pagePos); + printProcessors["default"]->printPage(page); } + currenPage++; + } +} + +void ReportEnginePrivate::printReport(ReportPages pages, QMap printers) +{ + if (printers.values().isEmpty()) return; + int currenPage = 1; + QMap> printProcessors; + for (int i = 0; i < printers.keys().count(); ++i) { + printProcessors.insert(printers.keys()[i],QSharedPointer(new PrintProcessor(printers[printers.keys()[i]]))); + } + + PrintProcessor* defaultProcessor = 0; + if (printProcessors.contains("default")) defaultProcessor = printProcessors["default"].data(); + else defaultProcessor = printProcessors.values().at(0).data(); + foreach(PageItemDesignIntf::Ptr page, pages){ + + if (printProcessors.contains(page->printerName())) + printProcessors[page->printerName()]->printPage(page); + else defaultProcessor->printPage(page); currenPage++; } - delete painter; } QStringList ReportEnginePrivate::aviableReportTranslations() @@ -437,6 +384,23 @@ bool ReportEnginePrivate::printReport(QPrinter* printer) } else return false; } +bool ReportEnginePrivate::printReport(QMap printers) +{ + try{ + bool designTime = dataManager()->designTime(); + dataManager()->setDesignTime(false); + ReportPages pages = renderToPages(); + dataManager()->setDesignTime(designTime); + if (pages.count()>0){ + printReport(pages,printers); + } + } catch(ReportError &exception){ + saveError(exception.what()); + return false; + } + return true; +} + bool ReportEnginePrivate::printPages(ReportPages pages, QPrinter *printer) { if (!printer&&!m_printerSelected){ @@ -1299,6 +1263,12 @@ bool ReportEngine::printReport(QPrinter *printer) return d->printReport(printer); } +bool ReportEngine::printReport(QMap printers) +{ + Q_D(ReportEngine); + return d->printReport(printers); +} + bool ReportEngine::printPages(ReportPages pages, QPrinter *printer){ Q_D(ReportEngine); return d->printPages(pages,printer); @@ -1553,5 +1523,84 @@ ScriptEngineManager*LimeReport::ReportEnginePrivate::scriptManager(){ return &ScriptEngineManager::instance(); } +PrintProcessor::PrintProcessor(QPrinter* printer) + : m_printer(printer), m_painter(new QPainter(m_printer)), m_firstPage(true) +{} + + +bool PrintProcessor::printPage(PageItemDesignIntf::Ptr page) +{ + if (m_painter && !m_painter->isActive()) return false; + LimeReport::PageDesignIntf renderPage; + + renderPage.setItemMode(PrintMode); + initPrinter(renderPage.pageItem()); + + QPointF backupPagePos = page->pos(); + page->setPos(0,0); + renderPage.setPageItem(page); + renderPage.setSceneRect(renderPage.pageItem()->mapToScene(renderPage.pageItem()->rect()).boundingRect()); + + if (!m_firstPage){ + m_printer->newPage(); + } else { + m_firstPage = false; + } + + qreal leftMargin, topMargin, rightMargin, bottomMargin; + m_printer->getPageMargins(&leftMargin, &topMargin, &rightMargin, &bottomMargin, QPrinter::Millimeter); + + QRectF printerPageRect = m_printer->pageRect(QPrinter::Millimeter); + printerPageRect = QRectF(0,0,(printerPageRect.size().width() + rightMargin + leftMargin) * Const::mmFACTOR, + (printerPageRect.size().height() + bottomMargin +topMargin) * Const::mmFACTOR); + + if (m_printer->pageSize() != static_cast(page->pageSize()) && + printerPageRect.width() < page->geometry().width()) + { + qreal pageWidth = page->geometry().width(); + QRectF currentPrintingRect = printerPageRect; + while (pageWidth>0){ + renderPage.render(m_painter, m_printer->pageRect(), currentPrintingRect); + currentPrintingRect.adjust(printerPageRect.size().width(),0,printerPageRect.size().width(),0); + pageWidth -= printerPageRect.size().width(); + if (pageWidth>0) m_printer->newPage(); + } + + } else { + renderPage.render(m_painter); + } + page->setPos(backupPagePos); + return true; +} + +void PrintProcessor::initPrinter(PageItemDesignIntf* page) +{ + if (page->oldPrintMode()){ + m_printer->setPageMargins(page->leftMargin(), + page->topMargin(), + page->rightMargin(), + page->bottomMargin(), + QPrinter::Millimeter); + m_printer->setOrientation(static_cast(page->pageOrientation())); + QSizeF pageSize = (page->pageOrientation()==PageItemDesignIntf::Landscape)? + QSizeF(page->sizeMM().height(),page->sizeMM().width()): + page->sizeMM(); + m_printer->setPaperSize(pageSize,QPrinter::Millimeter); + } else { + m_printer->setFullPage(page->fullPage()); + m_printer->setOrientation(static_cast(page->pageOrientation())); + if (page->pageSize()==PageItemDesignIntf::Custom){ + QSizeF pageSize = (page->pageOrientation()==PageItemDesignIntf::Landscape)? + QSizeF(page->sizeMM().height(),page->sizeMM().width()): + page->sizeMM(); + if (page->getSetPageSizeToPrinter() || m_printer->outputFormat() == QPrinter::PdfFormat) + m_printer->setPaperSize(pageSize,QPrinter::Millimeter); + } else { + if (page->getSetPageSizeToPrinter() || m_printer->outputFormat() == QPrinter::PdfFormat) + m_printer->setPaperSize(static_cast(page->pageSize())); + } + } +} + }// namespace LimeReport diff --git a/limereport/lrreportengine.h b/limereport/lrreportengine.h index 2abf45b..aa8bd6a 100644 --- a/limereport/lrreportengine.h +++ b/limereport/lrreportengine.h @@ -80,6 +80,7 @@ public: explicit ReportEngine(QObject *parent = 0); ~ReportEngine(); bool printReport(QPrinter *printer=0); + bool printReport(QMap printers); bool printPages(ReportPages pages, QPrinter *printer); void printToFile(const QString& fileName); PageDesignIntf *createPreviewScene(QObject *parent = 0); diff --git a/limereport/lrreportengine_p.h b/limereport/lrreportengine_p.h index 461fd69..93acb9b 100644 --- a/limereport/lrreportengine_p.h +++ b/limereport/lrreportengine_p.h @@ -93,6 +93,19 @@ public: virtual void setCurrentDesignerLanguage(QLocale::Language language) = 0; }; +class PrintProcessor{ +public: + explicit PrintProcessor(QPrinter* printer); + ~PrintProcessor(){ if (m_painter) delete m_painter;} + bool printPage(LimeReport::PageItemDesignIntf::Ptr page); +private: + void initPrinter(PageItemDesignIntf* page); +private: + QPrinter* m_printer; + QPainter* m_painter; + bool m_firstPage; +}; + class ReportEnginePrivate : public QObject, public ICollectionContainer, public ITranslationContainer, @@ -111,6 +124,7 @@ class ReportEnginePrivate : public QObject, public: static void printReport(ItemsReaderIntf::Ptr reader, QPrinter &printer); static void printReport(ReportPages pages, QPrinter &printer); + static void printReport(ReportPages pages, QMapprinters); Q_INVOKABLE QStringList aviableReportTranslations(); Q_INVOKABLE void setReportTranslation(const QString& languageName); public: @@ -134,6 +148,7 @@ public: void clearReport(); bool printReport(QPrinter *printer=0); + bool printReport(QMapprinters); bool printPages(ReportPages pages, QPrinter *printer); void printToFile(const QString& fileName); bool printToPDF(const QString& fileName); @@ -198,7 +213,7 @@ public: void setCurrentDesignerLanguage(QLocale::Language language); ScaleType previewScaleType(); int previewScalePercent(); - void setPreviewScaleType(const ScaleType &previewScaleType, int percent = 0); + void setPreviewScaleType(const ScaleType &previewScaleType, int percent = 0); signals: void pagesLoadFinished(); void datasourceCollectionLoadFinished(const QString& collectionName); @@ -216,7 +231,6 @@ signals: void getAviableLanguages(QList* languages); void currentDefaulLanguageChanged(QLocale::Language); QLocale::Language getCurrentDefaultLanguage(); - void externalPaint(const QString& objectName, QPainter* painter, const QStyleOptionGraphicsItem*); public slots: