Fixed issue #494. Printing is not working.

--HG--
branch : release
This commit is contained in:
Roman Telezhynskyi 2016-06-17 17:56:15 +03:00
parent 470ffcf767
commit cdf3d76930
11 changed files with 354 additions and 253 deletions

View File

@ -8,6 +8,7 @@
- [#468] Error - Tape 'Save As' should set 'Read Only' to false. - [#468] Error - Tape 'Save As' should set 'Read Only' to false.
- [#501] Rename Window->Close Window to Window->Close pattern. - [#501] Rename Window->Close Window to Window->Close pattern.
- [#515] Seam allowance wrong way. - [#515] Seam allowance wrong way.
- [#494] Printing is not working.
# Version 0.4.4 April 12, 2016 # Version 0.4.4 April 12, 2016
- Updated measurement templates with all measurements. Added new template Aldrich/Women measurements. - Updated measurement templates with all measurements. Added new template Aldrich/Women measurements.

View File

@ -2900,7 +2900,6 @@ void MainWindow::SetLayoutModeActions(bool enable)
ui->actionExportAs->setEnabled(value); ui->actionExportAs->setEnabled(value);
ui->actionPrintPreview->setEnabled(value); ui->actionPrintPreview->setEnabled(value);
ui->actionPrintPreviewTiled->setEnabled(value); ui->actionPrintPreviewTiled->setEnabled(value);
ui->actionSaveAsPDF->setEnabled(value);
ui->actionSaveAsTiledPDF->setEnabled(value); ui->actionSaveAsTiledPDF->setEnabled(value);
ui->actionPrint->setEnabled(value); ui->actionPrint->setEnabled(value);
ui->actionPrintTiled->setEnabled(value); ui->actionPrintTiled->setEnabled(value);
@ -3347,7 +3346,6 @@ void MainWindow::CreateActions()
connect(ui->actionExportAs, &QAction::triggered, this, &MainWindow::ExportLayoutAs); connect(ui->actionExportAs, &QAction::triggered, this, &MainWindow::ExportLayoutAs);
connect(ui->actionPrintPreview, &QAction::triggered, this, &MainWindow::PrintPreviewOrigin); connect(ui->actionPrintPreview, &QAction::triggered, this, &MainWindow::PrintPreviewOrigin);
connect(ui->actionPrintPreviewTiled, &QAction::triggered, this, &MainWindow::PrintPreviewTiled); connect(ui->actionPrintPreviewTiled, &QAction::triggered, this, &MainWindow::PrintPreviewTiled);
connect(ui->actionSaveAsPDF, &QAction::triggered, this, &MainWindow::SaveAsPDF);
connect(ui->actionSaveAsTiledPDF, &QAction::triggered, this, &MainWindow::SaveAsTiledPDF); connect(ui->actionSaveAsTiledPDF, &QAction::triggered, this, &MainWindow::SaveAsTiledPDF);
connect(ui->actionPrint, &QAction::triggered, this, &MainWindow::PrintOrigin); connect(ui->actionPrint, &QAction::triggered, this, &MainWindow::PrintOrigin);
connect(ui->actionPrintTiled, &QAction::triggered, this, &MainWindow::PrintTiled); connect(ui->actionPrintTiled, &QAction::triggered, this, &MainWindow::PrintTiled);

View File

@ -1040,7 +1040,6 @@
<property name="title"> <property name="title">
<string>Layout</string> <string>Layout</string>
</property> </property>
<addaction name="actionSaveAsPDF"/>
<addaction name="actionPrintPreview"/> <addaction name="actionPrintPreview"/>
<addaction name="actionPrint"/> <addaction name="actionPrint"/>
<addaction name="separator"/> <addaction name="separator"/>
@ -1887,24 +1886,6 @@
<enum>QAction::NoRole</enum> <enum>QAction::NoRole</enum>
</property> </property>
</action> </action>
<action name="actionSaveAsPDF">
<property name="enabled">
<bool>false</bool>
</property>
<property name="icon">
<iconset resource="../../libs/vmisc/share/resources/icon.qrc">
<normaloff>:/icon/32x32/pdf.png</normaloff>:/icon/32x32/pdf.png</iconset>
</property>
<property name="text">
<string>Save as PDF</string>
</property>
<property name="toolTip">
<string>Save original layout</string>
</property>
<property name="shortcut">
<string notr="true"/>
</property>
</action>
<action name="actionSaveAsTiledPDF"> <action name="actionSaveAsTiledPDF">
<property name="enabled"> <property name="enabled">
<bool>false</bool> <bool>false</bool>

View File

@ -269,19 +269,6 @@ void MainWindowsNoGUI::ExportLayout(const DialogSaveLayout &dialog)
} }
} }
//---------------------------------------------------------------------------------------------------------------------
void MainWindowsNoGUI::SaveAsPDF()
{
if (not isPagesUniform())
{
qCritical()<<tr("For saving multipage document all sheet should have the same size. Use export "
"function instead.");
return;
}
isTiled = false;
SaveLayoutAs();
}
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
void MainWindowsNoGUI::SaveAsTiledPDF() void MainWindowsNoGUI::SaveAsTiledPDF()
{ {
@ -292,26 +279,13 @@ void MainWindowsNoGUI::SaveAsTiledPDF()
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
void MainWindowsNoGUI::PrintPages(QPrinter *printer) void MainWindowsNoGUI::PrintPages(QPrinter *printer)
{ {
if (printer == nullptr) // Here we try understand difference between printer's dpi and our.
{ // Get printer rect acording to our dpi.
return; const QRectF printerPageRect(0, 0, ToPixel(printer->pageRect(QPrinter::Millimeter).width(), Unit::Mm),
} ToPixel(printer->pageRect(QPrinter::Millimeter).height(), Unit::Mm));
const double xscale = printer->pageRect().width() / printerPageRect.width();
const QVector<QImage> images = AllSheets(); const double yscale = printer->pageRect().height() / printerPageRect.height();
const double scale = qMin(xscale, yscale);
QVector<QImage> poster;
if (isTiled)
{
VPoster posterazor(printer);
for (int i=0; i < images.size(); i++)
{
poster += posterazor.Generate(images.at(i), i+1, images.size());
}
}
else
{
poster = images;
}
QPainter painter; QPainter painter;
if (not painter.begin(printer)) if (not painter.begin(printer))
@ -320,9 +294,40 @@ void MainWindowsNoGUI::PrintPages(QPrinter *printer)
return; return;
} }
painter.setFont( QFont( "Arial", 8, QFont::Normal ) );
painter.setRenderHint(QPainter::Antialiasing, true);
painter.setPen(QPen(Qt::black, qApp->toPixel(WidthMainLine(*pattern->GetPatternUnit())), Qt::SolidLine,
Qt::RoundCap, Qt::RoundJoin));
painter.setBrush ( QBrush ( Qt::NoBrush ) );
int count = 0;
QSharedPointer<QVector<PosterData>> poster;
QSharedPointer<VPoster> posterazor;
if (isTiled)
{
poster = QSharedPointer<QVector<PosterData>>(new QVector<PosterData>());
posterazor = QSharedPointer<VPoster>(new VPoster(printer));
for (int i=0; i < scenes.size(); ++i)
{
auto *paper = qgraphicsitem_cast<QGraphicsRectItem *>(papers.at(i));
if (paper)
{
*poster += posterazor->Calc(paper->rect().toRect(), i);
}
}
count = poster->size();
}
else
{
count = scenes.size();
}
// Handle the fromPage(), toPage(), supportsMultipleCopies(), and numCopies() values from QPrinter. // Handle the fromPage(), toPage(), supportsMultipleCopies(), and numCopies() values from QPrinter.
int firstPage = printer->fromPage() - 1; int firstPage = printer->fromPage() - 1;
if (firstPage >= poster.size()) if (firstPage >= count)
{ {
return; return;
} }
@ -332,9 +337,9 @@ void MainWindowsNoGUI::PrintPages(QPrinter *printer)
} }
int lastPage = printer->toPage() - 1; int lastPage = printer->toPage() - 1;
if (lastPage == -1 || lastPage >= poster.size()) if (lastPage == -1 || lastPage >= count)
{ {
lastPage = poster.size() - 1; lastPage = count - 1;
} }
const int numPages = lastPage - firstPage + 1; const int numPages = lastPage - firstPage + 1;
@ -365,7 +370,38 @@ void MainWindowsNoGUI::PrintPages(QPrinter *printer)
{ {
index = lastPage - j; index = lastPage - j;
} }
painter.drawImage(QPointF(margins.left(), margins.top()), poster.at(index));
int paperIndex = -1;
isTiled ? paperIndex = poster->at(index).index : paperIndex = index;
auto *paper = qgraphicsitem_cast<QGraphicsRectItem *>(papers.at(paperIndex));
if (paper)
{
QVector<QGraphicsItem *> posterData;
if (isTiled)
{
// Draw borders
posterData = posterazor->Borders(paper, poster->at(index), scenes.size());
}
PreparePaper(paperIndex);
// Render
QRectF source;
isTiled ? source = poster->at(index).rect : source = paper->rect();
QRectF target(0, 0, source.width() * scale, source.height() * scale);
scenes.at(paperIndex)->render(&painter, target, source, Qt::IgnoreAspectRatio);
if (isTiled)
{
// Remove borders
qDeleteAll(posterData);
}
// Restore
RestorePaper(paperIndex);
}
} }
} }
@ -741,47 +777,32 @@ void MainWindowsNoGUI::DxfFile(const QString &name, int i) const
#endif #endif
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
QVector<QImage> MainWindowsNoGUI::AllSheets() const void MainWindowsNoGUI::PreparePaper(int index) const
{ {
QVector<QImage> images; auto *paper = qgraphicsitem_cast<QGraphicsRectItem *>(papers.at(index));
for (int i=0; i < scenes.size(); ++i)
{
QGraphicsRectItem *paper = qgraphicsitem_cast<QGraphicsRectItem *>(papers.at(i));
if (paper) if (paper)
{ {
// Hide shadow and paper border QBrush brush(Qt::white);
QBrush *brush = new QBrush(); scenes.at(index)->setBackgroundBrush(brush);
brush->setColor( QColor( Qt::white ) ); shadows.at(index)->setVisible(false);
scenes[i]->setBackgroundBrush( *brush );
shadows[i]->setVisible(false);
paper->setPen(QPen(Qt::white, 0.1, Qt::NoPen));// border 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<qint32>(r.width()), static_cast<qint32>(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(WidthMainLine(*pattern->GetPatternUnit())), Qt::SolidLine,
Qt::RoundCap, Qt::RoundJoin));
painter.setBrush ( QBrush ( Qt::NoBrush ) );
scenes.at(i)->render(&painter, r, r, Qt::IgnoreAspectRatio);
painter.end();
images.append(image);
// Resore //---------------------------------------------------------------------------------------------------------------------
void MainWindowsNoGUI::RestorePaper(int index) const
{
auto *paper = qgraphicsitem_cast<QGraphicsRectItem *>(papers.at(index));
if (paper)
{
// Restore
paper->setPen(QPen(Qt::black, qApp->toPixel(WidthMainLine(*pattern->GetPatternUnit())))); paper->setPen(QPen(Qt::black, qApp->toPixel(WidthMainLine(*pattern->GetPatternUnit()))));
brush->setColor( QColor( Qt::gray ) ); QBrush brush(Qt::gray);
brush->setStyle( Qt::SolidPattern ); scenes.at(index)->setBackgroundBrush(brush);
scenes[i]->setBackgroundBrush( *brush ); shadows.at(index)->setVisible(true);
shadows[i]->setVisible(true);
delete brush;
} }
} }
return images;
}
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
void MainWindowsNoGUI::SaveLayoutAs() void MainWindowsNoGUI::SaveLayoutAs()
@ -794,8 +815,13 @@ void MainWindowsNoGUI::SaveLayoutAs()
} }
} }
QPrinter printer; QPrinter printer;
SetPrinterSettings(&printer); SetPrinterSettings(&printer, PrintType::PrintPDF);
printer.setOutputFormat(QPrinter::PdfFormat);
// Call IsPagesFit after setting a printer settings and check if pages is not bigger than printer's paper size
if (not isTiled && not IsPagesFit(printer.paperRect().size()))
{
qWarning()<<tr("Pages will be cropped because they do not fit printer paper size.");
}
QString fileName = QFileDialog::getSaveFileName(this, tr("Print to pdf"), QString fileName = QFileDialog::getSaveFileName(this, tr("Print to pdf"),
qApp->ValentinaSettings()->GetPathLayout()+"/"+FileName()+".pdf", qApp->ValentinaSettings()->GetPathLayout()+"/"+FileName()+".pdf",
@ -834,7 +860,8 @@ void MainWindowsNoGUI::PrintPreview()
return; return;
} }
SetPrinterSettings(printer.data(), false); SetPrinterSettings(printer.data(), PrintType::PrintPreview);
printer->setResolution(static_cast<int>(PrintDPI));
// display print preview dialog // display print preview dialog
QPrintPreviewDialog preview(printer.data()); QPrintPreviewDialog preview(printer.data());
connect(&preview, &QPrintPreviewDialog::paintRequested, this, &MainWindowsNoGUI::PrintPages); connect(&preview, &QPrintPreviewDialog::paintRequested, this, &MainWindowsNoGUI::PrintPages);
@ -852,30 +879,33 @@ void MainWindowsNoGUI::LayoutPrint()
} }
} }
// display print dialog and if accepted print // display print dialog and if accepted print
QPrinter printer(QPrinter::HighResolution); QSharedPointer<QPrinter> printer = DefaultPrinter(QPrinter::HighResolution);
SetPrinterSettings(&printer); if (printer.isNull())
QPrintDialog dialog( &printer, this ); {
qCritical("%s\n\n%s", qUtf8Printable(tr("Print error")),
qUtf8Printable(tr("Cannot proceed because there are no available printers in your system.")));
return;
}
SetPrinterSettings(printer.data(), PrintType::PrintNative);
QPrintDialog dialog(printer.data(), this );
// If only user couldn't change page margins we could use method setMinMax(); // If only user couldn't change page margins we could use method setMinMax();
dialog.setOption(QPrintDialog::PrintCurrentPage, false); dialog.setOption(QPrintDialog::PrintCurrentPage, false);
if ( dialog.exec() == QDialog::Accepted ) if ( dialog.exec() == QDialog::Accepted )
{ {
printer.setResolution(static_cast<int>(PrintDPI)); printer->setResolution(static_cast<int>(PrintDPI));
PrintPages( &printer ); PrintPages(printer.data());
} }
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
void MainWindowsNoGUI::SetPrinterSettings(QPrinter *printer, bool prepareForPrinting) void MainWindowsNoGUI::SetPrinterSettings(QPrinter *printer, const PrintType &printType)
{ {
SCASSERT(printer != nullptr) SCASSERT(printer != nullptr)
printer->setCreator(qApp->applicationDisplayName()+" "+qApp->applicationVersion()); printer->setCreator(qApp->applicationDisplayName()+" "+qApp->applicationVersion());
// Set orientation // Set orientation
if (papers.size() > 0) if (paperSize.height() >= paperSize.width())
{
QGraphicsRectItem *paper = qgraphicsitem_cast<QGraphicsRectItem *>(papers.at(0));
SCASSERT(paper != nullptr)
if (paper->rect().height()>= paper->rect().width())
{ {
printer->setOrientation(QPrinter::Portrait); printer->setOrientation(QPrinter::Portrait);
} }
@ -883,9 +913,8 @@ void MainWindowsNoGUI::SetPrinterSettings(QPrinter *printer, bool prepareForPrin
{ {
printer->setOrientation(QPrinter::Landscape); printer->setOrientation(QPrinter::Landscape);
} }
}
if (not isTiled && papers.size() > 0) if (not isTiled)
{ {
const QSizeF size = QSizeF(FromPixel(paperSize.width(), Unit::Mm), FromPixel(paperSize.height(), Unit::Mm)); const QSizeF size = QSizeF(FromPixel(paperSize.width(), Unit::Mm), FromPixel(paperSize.height(), Unit::Mm));
const QPrinter::PageSize pSZ = FindTemplate(size); const QPrinter::PageSize pSZ = FindTemplate(size);
@ -907,14 +936,33 @@ void MainWindowsNoGUI::SetPrinterSettings(QPrinter *printer, bool prepareForPrin
printer->setPageMargins(left, top, right, bottom, QPrinter::Millimeter); printer->setPageMargins(left, top, right, bottom, QPrinter::Millimeter);
} }
if (prepareForPrinting) switch(printType)
{ {
case PrintType::PrintPDF:
{
const QString outputFileName = QDir::homePath() + QDir::separator() + FileName();
#ifdef Q_OS_WIN #ifdef Q_OS_WIN
printer->setOutputFileName(QDir::homePath() + QDir::separator() + FileName()); printer->setOutputFileName(outputFileName);
#else #else
printer->setOutputFileName(QDir::homePath() + QDir::separator() + FileName() + QLatin1Literal(".pdf")); printer->setOutputFileName(outputFileName + QLatin1Literal(".pdf"));
#endif #endif
#ifdef Q_OS_MAC
printer->setOutputFormat(QPrinter::NativeFormat);
#else
printer->setOutputFormat(QPrinter::PdfFormat);
#endif
break;
} }
case PrintType::PrintNative:
printer->setOutputFileName("");//Disable printing to file if was enabled.
printer->setOutputFormat(QPrinter::NativeFormat);
break;
case PrintType::PrintPreview: /*do nothing*/
default:
break;
}
printer->setDocName(FileName()); printer->setDocName(FileName());
IsLayoutGrayscale() ? printer->setColorMode(QPrinter::GrayScale) : printer->setColorMode(QPrinter::Color); IsLayoutGrayscale() ? printer->setColorMode(QPrinter::GrayScale) : printer->setColorMode(QPrinter::Color);
@ -923,15 +971,35 @@ void MainWindowsNoGUI::SetPrinterSettings(QPrinter *printer, bool prepareForPrin
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
bool MainWindowsNoGUI::IsLayoutGrayscale() const bool MainWindowsNoGUI::IsLayoutGrayscale() const
{ {
const QVector<QImage> images = AllSheets(); const QRect target = QRect(0, 0, 100, 100);//Small image less memory need
for(int i=0; i < images.size(); ++i) for (int i=0; i < scenes.size(); ++i)
{ {
if (not images.at(i).isGrayscale()) auto *paper = qgraphicsitem_cast<QGraphicsRectItem *>(papers.at(i));
if (paper)
{
// Hide shadow and paper border
PreparePaper(i);
// Render png
QImage image(target.size(), QImage::Format_RGB32);
image.fill(Qt::white);
QPainter painter(&image);
painter.setPen(QPen(Qt::black, qApp->toPixel(WidthMainLine(*pattern->GetPatternUnit())), Qt::SolidLine,
Qt::RoundCap, Qt::RoundJoin));
painter.setBrush ( QBrush ( Qt::NoBrush ) );
scenes.at(i)->render(&painter, target, paper->rect(), Qt::KeepAspectRatio);
painter.end();
// Restore
RestorePaper(i);
if (not image.isGrayscale())
{ {
return false; return false;
} }
} }
}
return true; return true;
} }
@ -986,11 +1054,11 @@ bool MainWindowsNoGUI::isPagesUniform() const
} }
else else
{ {
QGraphicsRectItem *paper = qgraphicsitem_cast<QGraphicsRectItem *>(papers.at(0)); auto *paper = qgraphicsitem_cast<QGraphicsRectItem *>(papers.at(0));
SCASSERT(paper != nullptr) SCASSERT(paper != nullptr)
for (int i=1; i < papers.size(); ++i) for (int i=1; i < papers.size(); ++i)
{ {
QGraphicsRectItem *p = qgraphicsitem_cast<QGraphicsRectItem *>(papers.at(i)); auto *p = qgraphicsitem_cast<QGraphicsRectItem *>(papers.at(i));
SCASSERT(p != nullptr) SCASSERT(p != nullptr)
if (paper->rect() != p->rect()) if (paper->rect() != p->rect())
{ {
@ -1001,6 +1069,22 @@ bool MainWindowsNoGUI::isPagesUniform() const
return true; return true;
} }
//---------------------------------------------------------------------------------------------------------------------
bool MainWindowsNoGUI::IsPagesFit(const QSizeF &printPaper) const
{
// On previous stage already was checked if pages have uniform size
// Enough will be to check only one page
QGraphicsRectItem *p = qgraphicsitem_cast<QGraphicsRectItem *>(papers.at(0));
SCASSERT(p != nullptr);
const QSizeF pSize = p->rect().size();
if (pSize.height() <= printPaper.height() && pSize.width() <= printPaper.width())
{
return true;
}
return false;
}
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
QString MainWindowsNoGUI::FileName() const QString MainWindowsNoGUI::FileName() const
{ {

View File

@ -41,7 +41,7 @@
class QGraphicsScene; class QGraphicsScene;
class QPrinter; class QPrinter;
class PosterData;
class MainWindowsNoGUI : public QMainWindow class MainWindowsNoGUI : public QMainWindow
{ {
@ -53,7 +53,6 @@ public:
public slots: public slots:
void ToolLayoutSettings(bool checked); void ToolLayoutSettings(bool checked);
void ExportLayoutAs(); void ExportLayoutAs();
void SaveAsPDF();
void SaveAsTiledPDF(); void SaveAsTiledPDF();
void PrintPages (QPrinter *printer); void PrintPages (QPrinter *printer);
void PrintPreviewOrigin(); void PrintPreviewOrigin();
@ -116,17 +115,21 @@ private:
void ObjFile(const QString &name, int i)const; void ObjFile(const QString &name, int i)const;
void DxfFile(const QString &name, int i)const; void DxfFile(const QString &name, int i)const;
QVector<QImage> AllSheets() const; void PreparePaper(int index) const;
void RestorePaper(int index) const;
void SaveLayoutAs(); void SaveLayoutAs();
void PrintPreview(); void PrintPreview();
void LayoutPrint(); void LayoutPrint();
void SetPrinterSettings(QPrinter *printer, bool prepareForPrinting = true); enum class PrintType : char {PrintPDF, PrintPreview, PrintNative};
void SetPrinterSettings(QPrinter *printer, const PrintType &printType);
bool IsLayoutGrayscale() const; bool IsLayoutGrayscale() const;
QPrinter::PaperSize FindTemplate(const QSizeF &size) const; QPrinter::PaperSize FindTemplate(const QSizeF &size) const;
bool isPagesUniform() const; bool isPagesUniform() const;
bool IsPagesFit(const QSizeF &printPaper) const;
QString FileName() const; QString FileName() const;
int ContinueIfLayoutStale(); int ContinueIfLayoutStale();

View File

@ -27,8 +27,9 @@
*************************************************************************/ *************************************************************************/
#include "vposter.h" #include "vposter.h"
#include <QPainter>
#include <QPrinter> #include <QPrinter>
#include <QGraphicsLineItem>
#include <QPen>
#if QT_VERSION < QT_VERSION_CHECK(5, 1, 0) #if QT_VERSION < QT_VERSION_CHECK(5, 1, 0)
# include "../vmisc/vmath.h" # include "../vmisc/vmath.h"
@ -40,36 +41,126 @@
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
VPoster::VPoster(const QPrinter *printer) VPoster::VPoster(const QPrinter *printer)
:printer(printer), allowence(static_cast<quint32>(qRound(10./25.4*printer->resolution())))//1 cm :printer(printer), allowence(static_cast<quint32>(qRound(10./25.4*PrintDPI)))//1 cm
{ {
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
QVector<QImage> VPoster::Generate(const QImage &image, int page, int sheets) const QVector<PosterData> VPoster::Calc(const QRect &imageRect, int page) const
{ {
QVector<QImage> poster; QVector<PosterData> poster;
if (printer == nullptr) if (printer == nullptr)
{ {
return poster; return poster;
} }
const int rows = CountRows(image.rect().height()); const int rows = CountRows(imageRect.height());
const int colomns = CountColomns(image.rect().width()); const int columns = CountColumns(imageRect.width());
for (int i=0; i < rows; i++) for (int i=0; i < rows; i++)
{ {
for (int j=0; j< colomns; j++) for (int j=0; j< columns; j++)
{ {
QImage img = Cut(i, j, image); PosterData data = Cut(i, j, imageRect);
img = Borders(rows, colomns, i, j, img, page, sheets); data.index = page;
poster.append(img); data.rows = rows;
data.columns = columns;
poster.append(data);
} }
} }
return poster; return poster;
} }
//---------------------------------------------------------------------------------------------------------------------
QVector<QGraphicsItem *> VPoster::Borders(QGraphicsItem *parent, const PosterData &img, int sheets) const
{
QVector<QGraphicsItem *> data;
QPen pen(Qt::NoBrush, 1, Qt::DashLine);
pen.setColor(Qt::black);
const QRect rec = img.rect;
if (img.column != 0)
{// Left border
auto *line = new QGraphicsLineItem(parent);
line->setPen(pen);
line->setLine(rec.x(), rec.y(), rec.x(), rec.y() + rec.height());
data.append(line);
auto *scissors = new QGraphicsPixmapItem(QPixmap("://scissors_vertical.png"), parent);
scissors->setPos(rec.x(), rec.y() + rec.height()-static_cast<int>(allowence));
data.append(scissors);
}
if (img.column != img.columns-1)
{// Right border
auto *line = new QGraphicsLineItem(parent);
line->setPen(pen);
line->setLine(rec.x() + rec.width()-static_cast<int>(allowence), rec.y(),
rec.x() + rec.width()-static_cast<int>(allowence), rec.y() + rec.height());
data.append(line);
}
if (img.row != 0)
{// Top border
auto *line = new QGraphicsLineItem(parent);
line->setPen(pen);
line->setLine(rec.x(), rec.y(), rec.x() + rec.width(), rec.y());
data.append(line);
auto *scissors = new QGraphicsPixmapItem(QPixmap("://scissors_horizontal.png"), parent);
scissors->setPos(rec.x() + rec.width()-static_cast<int>(allowence), rec.y());
data.append(scissors);
}
if (img.rows*img.columns > 1)
{ // Don't show bottom border if only one page need
// Bottom border (mandatory)
auto *line = new QGraphicsLineItem(parent);
line->setPen(pen);
line->setLine(rec.x(), rec.y() + rec.height()-static_cast<int>(allowence),
rec.x() + rec.width(), rec.y() + rec.height()-static_cast<int>(allowence));
data.append(line);
if (img.row == img.rows-1)
{
auto *scissors = new QGraphicsPixmapItem(QPixmap("://scissors_horizontal.png"), parent);
scissors->setPos(rec.x() + rec.width()-static_cast<int>(allowence),
rec.y() + rec.height()-static_cast<int>(allowence));
data.append(scissors);
}
}
// Labels
auto *labels = new QGraphicsTextItem(parent);
const int layoutX = 15;
const int layoutY = 5;
labels->setPos(rec.x() + layoutX, rec.y() + rec.height()-static_cast<int>(allowence)+layoutY);
labels->setTextWidth(rec.width()-(static_cast<int>(allowence)+layoutX));
const QString grid = tr("Grid ( %1 , %2 )").arg(img.row+1).arg(img.column+1);
const QString page = tr("Page %1 of %2").arg(img.row*(img.columns)+img.column+1).arg(img.rows*img.columns);
QString sheet;
if (sheets > 1)
{
sheet = tr("Sheet %1 of %2").arg(img.index+1).arg(sheets);
}
labels->setHtml(QString("<table width='100%'>"
"<tr>"
"<td>%1</td><td align='center'>%2</td><td align='right'>%3</td>"
"</tr>"
"</table>")
.arg(grid, page, sheet));
data.append(labels);
return data;
}
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
int VPoster::CountRows(int height) const int VPoster::CountRows(int height) const
{ {
@ -120,7 +211,7 @@ int VPoster::CountRows(int height) const
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
int VPoster::CountColomns(int width) const int VPoster::CountColumns(int width) const
{ {
const qreal imgLength = width; const qreal imgLength = width;
const qreal pageLength = PageRect().width(); const qreal pageLength = PageRect().width();
@ -148,96 +239,20 @@ int VPoster::CountColomns(int width) const
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
QImage VPoster::Cut(int i, int j, const QImage &image) const PosterData VPoster::Cut(int i, int j, const QRect &imageRect) const
{ {
const int x = j*PageRect().width() - j*static_cast<int>(allowence); const int x = j*PageRect().width() - j*static_cast<int>(allowence);
const int y = i*PageRect().height() - i*static_cast<int>(allowence); const int y = i*PageRect().height() - i*static_cast<int>(allowence);
SCASSERT(x <= image.rect().width()); SCASSERT(x <= imageRect.width());
SCASSERT(y <= image.rect().height()); SCASSERT(y <= imageRect.height());
QRect copyRect(x, y, PageRect().width(), PageRect().height()); PosterData data;
data.row = i;
data.column = j;
data.rect = QRect(x, y, PageRect().width(), PageRect().height());
if (not image.rect().contains(copyRect)) return data;
{
// Create full page with white background
QImage fullPage(copyRect.size(), image.format());
fullPage.fill(Qt::white);
// Real size that we can copy from image.
// Because in areas beyond the image, pixels are set to 0 by copy() method.
// For 32-bit RGB images, this means black.
copyRect = image.rect().intersected(copyRect);
QPainter painter(&fullPage);
painter.drawImage(QPointF(), image.copy( copyRect));
painter.end();
return fullPage;
}
else
{
return image.copy(copyRect);
}
}
//---------------------------------------------------------------------------------------------------------------------
QImage VPoster::Borders(int rows, int colomns, int i, int j, QImage &image, int page, int sheets) const
{
QPainter painter(&image);
QPen pen = QPen(Qt::NoBrush, 1, Qt::DashLine);
pen.setColor(Qt::black);
painter.setPen(pen);
const QRect rec = image.rect();
if (j != 0 && PageRect().x() > 0)
{// Left border
painter.drawLine(QLine(0, 0, 0, rec.height()));
painter.drawImage(QPoint(0, rec.height()-static_cast<int>(allowence)),
QImage("://scissors_vertical.png"));
}
if (j != colomns-1)
{// Right border
painter.drawLine(QLine(rec.width()-static_cast<int>(allowence), 0,
rec.width()-static_cast<int>(allowence), rec.height()));
}
if (i != 0 && PageRect().y() > 0)
{// Top border
painter.drawLine(QLine(0, 0, rec.width(), 0));
painter.drawImage(QPoint(rec.width()-static_cast<int>(allowence), 0),
QImage("://scissors_horizontal.png"));
}
if (rows*colomns > 1)
{ // Don't show bottom border if only one page need
// Bottom border (mandatory)
painter.drawLine(QLine(0, rec.height()-static_cast<int>(allowence),
rec.width(), rec.height()-static_cast<int>(allowence)));
if (i == rows-1)
{
painter.drawImage(QPoint(rec.width()-static_cast<int>(allowence),
rec.height()-static_cast<int>(allowence)),
QImage("://scissors_horizontal.png"));
}
}
// Labels
const int layoutX = 15;
const int layoutY = 5;
QRect labels(layoutX, rec.height()-static_cast<int>(allowence)+layoutY,
rec.width()-(static_cast<int>(allowence)+layoutX), static_cast<int>(allowence)-layoutY);
painter.drawText(labels, Qt::AlignLeft, tr("Grid ( %1 , %2 )").arg(i+1).arg(j+1));
painter.drawText(labels, Qt::AlignHCenter, tr("Page %1 of %2").arg(i*(colomns)+j+1).arg(rows*colomns));
if (sheets > 1)
{
painter.drawText(labels, Qt::AlignRight, tr("Sheet %1 of %2").arg(page).arg(sheets));
}
painter.end();
return image;
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
@ -247,13 +262,12 @@ QRect VPoster::PageRect() const
// we can't use method pageRect(QPrinter::Point). Our dpi value can be different. // we can't use method pageRect(QPrinter::Point). Our dpi value can be different.
// We convert value yourself to pixels. // We convert value yourself to pixels.
const QRectF rect = printer->pageRect(QPrinter::Millimeter); const QRectF rect = printer->pageRect(QPrinter::Millimeter);
const QRect pageRect(qFloor(ToPixel(rect.x())), qFloor(ToPixel(rect.y())), qFloor(ToPixel(rect.width())), const QRect pageRect(0, 0, qFloor(ToPixel(rect.width())), qFloor(ToPixel(rect.height())));
qFloor(ToPixel(rect.height())));
return pageRect; return pageRect;
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
qreal VPoster::ToPixel(qreal val) const qreal VPoster::ToPixel(qreal val)
{ {
return val / 25.4 * printer->resolution(); // Mm to pixels with current dpi. return val / 25.4 * PrintDPI; // Mm to pixels with current dpi.
} }

View File

@ -29,11 +29,29 @@
#ifndef VPOSTER_H #ifndef VPOSTER_H
#define VPOSTER_H #define VPOSTER_H
#include <QImage>
#include <QRect> #include <QRect>
#include <QCoreApplication> #include <QCoreApplication>
class QPrinter; class QPrinter;
class QGraphicsItem;
struct PosterData
{
PosterData()
: index(0),
row(0),
column(0),
rows(0),
columns(0),
rect(){}
quint32 index; // paper index
quint32 row; // positions in the greed
quint32 column;
quint32 rows;
quint32 columns;
QRect rect; // rect section
};
class VPoster class VPoster
{ {
@ -41,20 +59,21 @@ class VPoster
public: public:
explicit VPoster(const QPrinter *printer); explicit VPoster(const QPrinter *printer);
QVector<QImage> Generate(const QImage &image, int page, int sheets = 1) const; QVector<PosterData> Calc(const QRect &imageRect, int page) const;
QVector<QGraphicsItem *> Borders(QGraphicsItem *parent, const PosterData &img, int sheets) const;
private: private:
const QPrinter *printer; const QPrinter *printer;
quint32 allowence; quint32 allowence;
int CountRows(int height) const; int CountRows(int height) const;
int CountColomns(int width) const; int CountColumns(int width) const;
QImage Cut(int i, int j, const QImage &image) const; PosterData Cut(int i, int j, const QRect &imageRect) const;
QImage Borders(int rows, int colomns, int i, int j, QImage &image, int page, int sheets) const;
QRect PageRect() const; QRect PageRect() const;
qreal ToPixel(qreal val) const; static qreal ToPixel(qreal val);
}; };
#endif // VPOSTER_H #endif // VPOSTER_H

View File

@ -1746,7 +1746,7 @@ QString AbsoluteMPath(const QString &patternPath, const QString &relativeMPath)
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
QSharedPointer<QPrinter> DefaultPrinter() QSharedPointer<QPrinter> DefaultPrinter(QPrinter::PrinterMode mode)
{ {
QPrinterInfo def = QPrinterInfo::defaultPrinter(); QPrinterInfo def = QPrinterInfo::defaultPrinter();
@ -1763,7 +1763,7 @@ QSharedPointer<QPrinter> DefaultPrinter()
} }
} }
QSharedPointer<QPrinter> printer = QSharedPointer<QPrinter>(new QPrinter(def, QPrinter::ScreenResolution)); auto printer = QSharedPointer<QPrinter>(new QPrinter(def, mode));
printer->setResolution(static_cast<int>(PrintDPI)); printer->setResolution(static_cast<int>(PrintDPI));
return printer; return printer;
} }

View File

@ -33,6 +33,7 @@
#include <QtGlobal> #include <QtGlobal>
#include <QStringList> #include <QStringList>
#include <QSharedPointer> #include <QSharedPointer>
#include <QtPrintSupport/QPrinter>
#ifdef Q_OS_WIN #ifdef Q_OS_WIN
#include <windows.h> #include <windows.h>
#endif /* Q_OS_WIN */ #endif /* Q_OS_WIN */
@ -44,7 +45,6 @@
#endif /* Q_CC_MSVC */ #endif /* Q_CC_MSVC */
class QComboBox; class QComboBox;
class QPrinter;
#define SceneSize 50000 #define SceneSize 50000
#define DefPointRadius 1.5//mm #define DefPointRadius 1.5//mm
@ -598,7 +598,7 @@ QString StrippedName(const QString &fullFileName) Q_REQUIRED_RESULT;
QString RelativeMPath(const QString &patternPath, const QString &absoluteMPath) Q_REQUIRED_RESULT; QString RelativeMPath(const QString &patternPath, const QString &absoluteMPath) Q_REQUIRED_RESULT;
QString AbsoluteMPath(const QString &patternPath, const QString &relativeMPath) Q_REQUIRED_RESULT; QString AbsoluteMPath(const QString &patternPath, const QString &relativeMPath) Q_REQUIRED_RESULT;
QSharedPointer<QPrinter> DefaultPrinter() Q_REQUIRED_RESULT; QSharedPointer<QPrinter> DefaultPrinter(QPrinter::PrinterMode mode = QPrinter::ScreenResolution) Q_REQUIRED_RESULT;
QPixmap darkenPixmap(const QPixmap &pixmap) Q_REQUIRED_RESULT; QPixmap darkenPixmap(const QPixmap &pixmap) Q_REQUIRED_RESULT;

View File

@ -28,6 +28,7 @@
#include "tst_vposter.h" #include "tst_vposter.h"
#include "../vlayout/vposter.h" #include "../vlayout/vposter.h"
#include "../vmisc/def.h"
#include <QImage> #include <QImage>
#include <QPrinter> #include <QPrinter>
@ -47,15 +48,15 @@ void TST_VPoster::BigPoster()
printer.setResolution(96);// By default printer.setResolution(96);// By default
printer.setPaperSize(QPrinter::A4); printer.setPaperSize(QPrinter::A4);
const QImage image(2622, 3178, QImage::Format_RGB32); // Little bit bigger than A1 const QRect image(0, 0, 2622, 3178); // Little bit bigger than A1
VPoster posterazor(&printer); VPoster posterazor(&printer);
const QVector<QImage> poster = posterazor.Generate(image, 1, 1); const QVector<PosterData> poster = posterazor.Calc(image, 0);
QCOMPARE(poster.size(), 16); QCOMPARE(poster.size(), 16);
for (int i=0; i < poster.size(); i++) for (int i=0; i < poster.size(); i++)
{ {
QCOMPARE(poster.at(i).rect().size(), PageRect(printer).size()); QCOMPARE(poster.at(i).rect.size(), PageRect(printer).size());
} }
} }
@ -67,13 +68,13 @@ void TST_VPoster::SmallPoster()
printer.setResolution(96);// By default printer.setResolution(96);// By default
printer.setPaperSize(QPrinter::A4); printer.setPaperSize(QPrinter::A4);
const QImage image(700, 1000, QImage::Format_RGB32); // Little bit less than A4 const QRect image(0, 0, 700, 1000); // Little bit less than A4
VPoster posterazor(&printer); VPoster posterazor(&printer);
const QVector<QImage> poster = posterazor.Generate(image, 1, 1); const QVector<PosterData> poster = posterazor.Calc(image, 0);
QCOMPARE(poster.size(), 1); QCOMPARE(poster.size(), 1);
QCOMPARE(poster.at(0).rect().size(), PageRect(printer).size()); QCOMPARE(poster.at(0).rect.size(), PageRect(printer).size());
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
@ -83,13 +84,13 @@ QRect TST_VPoster::PageRect(const QPrinter &printer) const
// we can't use method pageRect(QPrinter::Point). Our dpi different can be different. // we can't use method pageRect(QPrinter::Point). Our dpi different can be different.
// We convert value yourself to pixels. // We convert value yourself to pixels.
const QRectF rect = printer.pageRect(QPrinter::Millimeter); const QRectF rect = printer.pageRect(QPrinter::Millimeter);
QRect pageRect(qFloor(ToPixel(rect.x(), printer)), qFloor(ToPixel(rect.y(), printer)), QRect pageRect(qFloor(ToPixel(rect.x())), qFloor(ToPixel(rect.y())),
qFloor(ToPixel(rect.width(), printer)), qFloor(ToPixel(rect.height(), printer))); qFloor(ToPixel(rect.width())), qFloor(ToPixel(rect.height())));
return pageRect; return pageRect;
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
qreal TST_VPoster::ToPixel(qreal val, const QPrinter &printer) const qreal TST_VPoster::ToPixel(qreal val) const
{ {
return val / 25.4 * printer.resolution(); // Mm to pixels with current dpi. return val / 25.4 * PrintDPI; // Mm to pixels with current dpi.
} }

View File

@ -47,7 +47,7 @@ private slots:
void SmallPoster(); void SmallPoster();
private: private:
qreal ToPixel(qreal val, const QPrinter &printer) const; qreal ToPixel(qreal val) const;
QRect PageRect(const QPrinter &printer) const; QRect PageRect(const QPrinter &printer) const;
}; };