Second try. Issue #494.

--HG--
branch : develop
This commit is contained in:
Roman Telezhynskyi 2016-06-02 13:25:34 +03:00
parent 6d18c71956
commit 6c6edf3728
2 changed files with 32 additions and 12 deletions

View File

@ -791,8 +791,7 @@ void MainWindowsNoGUI::SaveLayoutAs()
} }
} }
QPrinter printer; QPrinter printer;
SetPrinterSettings(&printer); SetPrinterSettings(&printer, PrintType::PrintPDF);
printer.setOutputFormat(QPrinter::PdfFormat);
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",
@ -831,7 +830,7 @@ void MainWindowsNoGUI::PrintPreview()
return; return;
} }
SetPrinterSettings(printer.data(), false); SetPrinterSettings(printer.data(), PrintType::PrintPreview);
// 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);
@ -857,7 +856,7 @@ void MainWindowsNoGUI::LayoutPrint()
return; return;
} }
SetPrinterSettings(printer.data()); SetPrinterSettings(printer.data(), PrintType::PrintNative);
QPrintDialog dialog(printer.data(), this ); 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);
@ -869,7 +868,7 @@ void MainWindowsNoGUI::LayoutPrint()
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
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());
@ -911,14 +910,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)
{ {
#ifdef Q_OS_WIN case PrintType::PrintPDF:
printer->setOutputFileName(QDir::homePath() + QDir::separator() + FileName()); {
#else const QString outputFileName = QDir::homePath() + QDir::separator() + FileName();
printer->setOutputFileName(QDir::homePath() + QDir::separator() + FileName() + QLatin1Literal(".pdf")); #ifdef Q_OS_WIN
#endif printer->setOutputFileName(outputFileName);
#else
printer->setOutputFileName(outputFileName + QLatin1Literal(".pdf"));
#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);

View File

@ -123,7 +123,9 @@ private:
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;