mirror of
https://github.com/fralx/LimeReport.git
synced 2024-12-23 16:22:58 +03:00
Multi printers print process has been added
This commit is contained in:
parent
d85b8bbd87
commit
362c7217f3
@ -37,6 +37,7 @@
|
||||
#include <LRCallbackDS>
|
||||
#include <QDebug>
|
||||
#include <QStringListModel>
|
||||
#include <QPrinter>
|
||||
|
||||
#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<QString, QPrinter*> printers;
|
||||
// printers.insert("default",printer);
|
||||
// report->printReport(printers);
|
||||
// }
|
||||
report->previewReport();
|
||||
}
|
||||
}
|
||||
|
@ -80,6 +80,7 @@ public:
|
||||
explicit ReportEngine(QObject *parent = 0);
|
||||
~ReportEngine();
|
||||
bool printReport(QPrinter *printer=0);
|
||||
bool printReport(QMap<QString, QPrinter*> printers);
|
||||
bool printPages(ReportPages pages, QPrinter *printer);
|
||||
void printToFile(const QString& fileName);
|
||||
PageDesignIntf *createPreviewScene(QObject *parent = 0);
|
||||
|
@ -342,6 +342,16 @@ void PageItemDesignIntf::initColumnsPos(QVector<qreal> &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;
|
||||
|
@ -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<PageItemDesignIntf::Ptr> ReportPages;
|
||||
|
@ -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<QString, QSharedPointer<PrintProcessor>> printProcessors;
|
||||
printProcessors.insert("default",QSharedPointer<PrintProcessor>(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<QPrinter::Orientation>(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<QPrinter::Orientation>(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<QPrinter::PageSize>(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<QPrinter::PageSize>(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<QString, QPrinter*> printers)
|
||||
{
|
||||
if (printers.values().isEmpty()) return;
|
||||
int currenPage = 1;
|
||||
QMap<QString, QSharedPointer<PrintProcessor>> printProcessors;
|
||||
for (int i = 0; i < printers.keys().count(); ++i) {
|
||||
printProcessors.insert(printers.keys()[i],QSharedPointer<PrintProcessor>(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<QString, QPrinter*> 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<QString, QPrinter*> 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<QPrinter::PageSize>(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<QPrinter::Orientation>(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<QPrinter::Orientation>(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<QPrinter::PageSize>(page->pageSize()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}// namespace LimeReport
|
||||
|
||||
|
@ -80,6 +80,7 @@ public:
|
||||
explicit ReportEngine(QObject *parent = 0);
|
||||
~ReportEngine();
|
||||
bool printReport(QPrinter *printer=0);
|
||||
bool printReport(QMap<QString, QPrinter*> printers);
|
||||
bool printPages(ReportPages pages, QPrinter *printer);
|
||||
void printToFile(const QString& fileName);
|
||||
PageDesignIntf *createPreviewScene(QObject *parent = 0);
|
||||
|
@ -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, QMap<QString,QPrinter*>printers);
|
||||
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(QMap<QString, QPrinter*>printers);
|
||||
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<QLocale::Language>* languages);
|
||||
void currentDefaulLanguageChanged(QLocale::Language);
|
||||
QLocale::Language getCurrentDefaultLanguage();
|
||||
|
||||
void externalPaint(const QString& objectName, QPainter* painter, const QStyleOptionGraphicsItem*);
|
||||
|
||||
public slots:
|
||||
|
Loading…
Reference in New Issue
Block a user