From fc54866019bc1215de60dd707273cafefef4d07d Mon Sep 17 00:00:00 2001 From: Roman Telezhynskyi Date: Sat, 11 Apr 2015 17:30:49 +0300 Subject: [PATCH] Action print preview. --HG-- branch : feature --- src/app/main.cpp | 1 + src/app/tablewindow.cpp | 134 ++++++++++++++++++++++++++++++++++++++-- src/app/tablewindow.h | 31 ++++++---- src/app/tablewindow.ui | 25 +++++++- 4 files changed, 173 insertions(+), 18 deletions(-) diff --git a/src/app/main.cpp b/src/app/main.cpp index 1bf5555fb..d5f8dcaa5 100644 --- a/src/app/main.cpp +++ b/src/app/main.cpp @@ -51,6 +51,7 @@ int main(int argc, char *argv[]) Q_INIT_RESOURCE(schema); Q_INIT_RESOURCE(theme); Q_INIT_RESOURCE(flags); + Q_INIT_RESOURCE(icons); QT_REQUIRE_VERSION(argc, argv, "5.0.0"); diff --git a/src/app/tablewindow.cpp b/src/app/tablewindow.cpp index 8d0d2d6ed..c7dc9c2f3 100644 --- a/src/app/tablewindow.cpp +++ b/src/app/tablewindow.cpp @@ -36,10 +36,12 @@ #include "../../libs/vlayout/vlayoutgenerator.h" #include "../dialogs/app/dialoglayoutprogress.h" #include "../dialogs/app/dialogsavelayout.h" +#include "../../libs/vlayout/vposter.h" #include #include #include +#include #include #ifdef Q_OS_WIN @@ -78,6 +80,7 @@ TableWindow::TableWindow(QWidget *parent) connect(ui->actionSave, &QAction::triggered, this, &TableWindow::SaveLayout); connect(ui->actionLayout, &QAction::triggered, this, &TableWindow::Layout); connect(ui->listWidget, &QListWidget::currentRowChanged, this, &TableWindow::ShowPaper); + connect(ui->actionPrint_pre_view, &QAction::triggered, this, &TableWindow::PrintPreview); } //--------------------------------------------------------------------------------------------------------------------- @@ -238,7 +241,7 @@ void TableWindow::ShowPaper(int index) if (index < 0 || index > scenes.size()) { ui->view->setScene(tempScene); - ui->actionSave->setEnabled(false); + EnableActions(false); } else { @@ -248,6 +251,76 @@ void TableWindow::ShowPaper(int index) ui->view->fitInView(ui->view->scene()->sceneRect(), Qt::KeepAspectRatio); } +//--------------------------------------------------------------------------------------------------------------------- +void TableWindow::PrintPreview() +{ + QPrinterInfo def = QPrinterInfo::defaultPrinter(); + + //if there is no default printer set the print preview won't show + if(def.isNull() || def.printerName().isEmpty()) + { + if(QPrinterInfo::availablePrinters().isEmpty()) + { + QMessageBox::critical(this, tr("Print error"), + tr("Cannot proceed because there are no available printers in your system."), + QMessageBox::Ok); + return; + } + else + { + def = QPrinterInfo::availablePrinters().first(); + } + } + + QPrinter printer(def, QPrinter::ScreenResolution); + printer.setResolution(static_cast(VApplication::PrintDPI)); + + QPrintPreviewDialog preview(&printer); + connect(&preview, &QPrintPreviewDialog::paintRequested, this, &TableWindow::Print); + preview.exec(); +} + +//--------------------------------------------------------------------------------------------------------------------- +void TableWindow::Print(QPrinter *printer) +{ + if (printer == nullptr) + { + return; + } + + const QVector images = AllSheets(); + + VPoster posterazor(printer); + QVector poster; + for (int i=0; i < images.size(); i++) + { + poster += posterazor.Generate(images.at(i), i+1, images.size()); + } + + QPainter painter; + if (not painter.begin(printer)) + { // failed to open file + qWarning("failed to open file, is it writable?"); + return; + } + + for (int i=0; i < poster.size(); i++) + { + painter.drawImage(QPointF(), poster.at(i)); + + if (i+1 < poster.size()) + { + if (not printer->newPage()) + { + qWarning("failed in flushing page to disk, disk full?"); + return; + } + } + } + + painter.end(); +} + //--------------------------------------------------------------------------------------------------------------------- void TableWindow::Layout() { @@ -281,8 +354,8 @@ void TableWindow::Layout() { case LayoutErrors::NoError: ClearLayout(); - papers = lGenerator.GetPapersItems(); - details = lGenerator.GetAllDetails(); + papers = lGenerator.GetPapersItems();// Blank sheets + details = lGenerator.GetAllDetails();// All details CreateShadows(); CreateScenes(); PrepareSceneList(); @@ -462,6 +535,49 @@ void TableWindow::ObjFile(const QString &name, int i) const } } +//--------------------------------------------------------------------------------------------------------------------- +QVector TableWindow::AllSheets() +{ + QVector images; + for (int i=0; i < scenes.size(); ++i) + { + QGraphicsRectItem *paper = qgraphicsitem_cast(papers.at(i)); + if (paper) + { + // Hide shadow and paper border + QBrush *brush = new QBrush(); + brush->setColor( QColor( Qt::white ) ); + scenes[i]->setBackgroundBrush( *brush ); + shadows[i]->setVisible(false); + paper->setPen(QPen(Qt::white, 0.1, Qt::NoPen));// border + + // Render png + const QRectF r = paper->rect(); + // Create the image with the exact size of the shrunk scene + QImage image(QSize(static_cast(r.width()), static_cast(r.height())), QImage::Format_RGB32); + image.fill(Qt::white); + QPainter painter(&image); + painter.setFont( QFont( "Arial", 8, QFont::Normal ) ); + painter.setRenderHint(QPainter::Antialiasing, true); + painter.setPen(QPen(Qt::black, qApp->toPixel(qApp->widthMainLine()), Qt::SolidLine, Qt::RoundCap, + Qt::RoundJoin)); + painter.setBrush ( QBrush ( Qt::NoBrush ) ); + scenes.at(i)->render(&painter); + painter.end(); + images.append(image); + + // Resore + paper->setPen(QPen(Qt::black, qApp->toPixel(qApp->widthMainLine()))); + brush->setColor( QColor( Qt::gray ) ); + brush->setStyle( Qt::SolidPattern ); + scenes[i]->setBackgroundBrush( *brush ); + shadows[i]->setVisible(true); + delete brush; + } + } + return images; +} + //--------------------------------------------------------------------------------------------------------------------- void TableWindow::ClearLayout() { @@ -470,6 +586,7 @@ void TableWindow::ClearLayout() shadows.clear(); papers.clear(); ui->listWidget->clear(); + EnableActions(false); } //--------------------------------------------------------------------------------------------------------------------- @@ -529,7 +646,7 @@ void TableWindow::PrepareSceneList() if (scenes.isEmpty() == false) { ui->listWidget->setCurrentRow(0); - ui->actionSave->setEnabled(true); + EnableActions(true); } } @@ -587,3 +704,12 @@ QMap TableWindow::InitFormates() const } return extByMessage; } + +//--------------------------------------------------------------------------------------------------------------------- +void TableWindow::EnableActions(bool enable) +{ + ui->actionSave->setEnabled(enable); + ui->actionSave_to_p_df->setEnabled(enable); + ui->actionPrint_pre_view->setEnabled(enable); + ui->action_Print->setEnabled(enable); +} diff --git a/src/app/tablewindow.h b/src/app/tablewindow.h index 171d3495f..0a55e4f76 100644 --- a/src/app/tablewindow.h +++ b/src/app/tablewindow.h @@ -54,12 +54,13 @@ public: ~TableWindow(); public slots: - void ModelChosen(QVector listDetails, const QString &fileName, - const QString &description); - void Layout(); - void StopTable(); - void SaveLayout(); - void ShowPaper(int index); + void ModelChosen(QVector listDetails, const QString &fileName, const QString &description); + void Layout(); + void StopTable(); + void SaveLayout(); + void ShowPaper(int index); + void PrintPreview(); + void Print (QPrinter *printer); signals: /** @brief closed emit if window is closing. */ @@ -91,13 +92,15 @@ private: QGraphicsScene* tempScene; - void SvgFile(const QString &name, int i)const; - void PngFile(const QString &name, int i)const; - void PdfFile(const QString &name, int i)const; - void EpsFile(const QString &name, int i)const; - void PsFile(const QString &name, int i)const; - void PdfToPs(const QStringList ¶ms)const; - void ObjFile(const QString &name, int i)const; + void SvgFile(const QString &name, int i)const; + void PngFile(const QString &name, int i)const; + void PdfFile(const QString &name, int i)const; + void EpsFile(const QString &name, int i)const; + void PsFile(const QString &name, int i)const; + void PdfToPs(const QStringList ¶ms)const; + void ObjFile(const QString &name, int i)const; + + QVector AllSheets(); void ClearLayout(); void CreateShadows(); @@ -105,6 +108,8 @@ private: void PrepareSceneList(); QIcon ScenePreview(int i) const; QMap InitFormates() const; + + void EnableActions(bool enable); }; #endif // TABLEWINDOW_H diff --git a/src/app/tablewindow.ui b/src/app/tablewindow.ui index 32a551248..a3fdd5d31 100644 --- a/src/app/tablewindow.ui +++ b/src/app/tablewindow.ui @@ -39,7 +39,7 @@ - toolBar + Main toolbar @@ -169,6 +169,20 @@ + + + Toolbar print + + + TopToolBarArea + + + false + + + + + false @@ -234,6 +248,9 @@ + + false + @@ -242,6 +259,9 @@ + + false + @@ -250,6 +270,9 @@ + + false + Print to p&df