Print/Save functions.

--HG--
branch : feature
This commit is contained in:
Roman Telezhynskyi 2015-05-14 15:27:04 +03:00
parent 7a7753e3f1
commit c954c2654c
7 changed files with 863 additions and 21 deletions

View file

@ -73,10 +73,10 @@ Q_LOGGING_CATEGORY(vMainWindow, "v.mainwindow")
* @param parent parent widget.
*/
MainWindow::MainWindow(QWidget *parent)
:MainWindowsNoGUI(parent), ui(new Ui::MainWindow), doc(nullptr), currentTool(Tool::Arrow),
:MainWindowsNoGUI(parent), ui(new Ui::MainWindow), currentTool(Tool::Arrow),
lastUsedTool(Tool::Arrow), sceneDraw(nullptr), sceneDetails(nullptr),
mouseCoordinate(nullptr), helpLabel(nullptr), isInitialized(false), dialogTable(nullptr), dialogTool(nullptr),
dialogHistory(nullptr), comboBoxDraws(nullptr), curFile(QString()), mode(Draw::Calculation), currentDrawIndex(0),
dialogHistory(nullptr), comboBoxDraws(nullptr), mode(Draw::Calculation), currentDrawIndex(0),
currentToolBoxIndex(0), drawMode(true), recentFileActs(),
separatorAct(nullptr), autoSaveTimer(nullptr), guiEnabled(true), gradationHeights(nullptr),
gradationSizes(nullptr),
@ -818,7 +818,7 @@ void MainWindow::ClearLayout()
shadows.clear();
papers.clear();
ui->listWidget->clear();
//EnableActions(false);
SetLayoutModeActions(false);
}
//---------------------------------------------------------------------------------------------------------------------
@ -833,7 +833,7 @@ void MainWindow::PrepareSceneList()
if (not scenes.isEmpty())
{
ui->listWidget->setCurrentRow(0);
//EnableActions(true);
SetLayoutModeActions(true);
}
}
@ -1804,6 +1804,7 @@ void MainWindow::Layout()
{
ui->actionDetails->setEnabled(true);
ui->actionLayout->setEnabled(true);
SetLayoutModeActions(true);
}
else
{
@ -1811,6 +1812,7 @@ void MainWindow::Layout()
ui->actionDetails->setEnabled(false);
ui->actionLayout->setEnabled(false);
ui->actionDraw->setChecked(true);
SetLayoutModeActions(false);
}
}
@ -1970,6 +1972,7 @@ void MainWindow::PatternWasModified(bool saved)
{
setWindowModified(!saved);
ui->actionSave->setEnabled(!saved);
isLayoutStale = true;
}
}
@ -2123,6 +2126,23 @@ void MainWindow::SetEnableTool(bool enable)
ui->toolButtonLayoutSettings->setEnabled(layoutTools);
}
//---------------------------------------------------------------------------------------------------------------------
void MainWindow::SetLayoutModeActions(bool enable)
{
bool value = enable;
if (scenes.isEmpty())
{
value = false;
}
ui->actionExportAs->setEnabled(value);
ui->actionPrintPreview->setEnabled(value);
ui->actionPrintPreviewTailed->setEnabled(value);
ui->actionSaveAsPDF->setEnabled(value);
ui->actionSaveAsTiledPDF->setEnabled(value);
ui->actionPrint->setEnabled(value);
ui->actionPrintTiled->setEnabled(value);
}
//---------------------------------------------------------------------------------------------------------------------
/**
* @brief MinimumScrollBar set scroll bar to minimum.
@ -2524,6 +2544,14 @@ void MainWindow::CreateActions()
connect(ui->actionEdit_pattern_code, &QAction::triggered, this, &MainWindow::EditPatternCode);
connect(ui->actionCloseWindow, &QAction::triggered, this, &MainWindow::ResetWindow);
connect(ui->actionShowCurveDetails, &QAction::triggered, this, &MainWindow::ActionCurveDetailsMode);
connect(ui->actionExportAs, &QAction::triggered, this, &MainWindow::ExportLayoutAs);
connect(ui->actionPrintPreview, &QAction::triggered, this, &MainWindow::PrintPreviewOrigin);
connect(ui->actionPrintPreviewTailed, &QAction::triggered, this, &MainWindow::PrintPreviewTiled);
connect(ui->actionSaveAsPDF, &QAction::triggered, this, &MainWindow::SaveAsPDF);
connect(ui->actionSaveAsTiledPDF, &QAction::triggered, this, &MainWindow::SaveAsTiledPDF);
connect(ui->actionPrint, &QAction::triggered, this, &MainWindow::PrintOrigin);
connect(ui->actionPrintTiled, &QAction::triggered, this, &MainWindow::PrintTiled);
ui->actionEdit_pattern_code->setEnabled(false);
//Actions for recent files loaded by a main window application.
@ -2787,12 +2815,12 @@ void MainWindow::ShowPaper(int index)
if (index < 0 || index >= scenes.size())
{
ui->view->setScene(tempSceneLayout);
//EnableActions(false);
SetLayoutModeActions(false);
}
else
{
ui->view->setScene(scenes.at(index));
//EnableActions(true);
SetLayoutModeActions(true);
}
ui->view->fitInView(ui->view->scene()->sceneRect(), Qt::KeepAspectRatio);

View file

@ -35,8 +35,6 @@
#include "tools/vtooldetail.h"
#include "tools/vtooluniondetails.h"
#include "tools/drawTools/drawtools.h"
#include "xml/vdomdocument.h"
namespace Ui
{
@ -156,8 +154,7 @@ private:
/** @brief ui keeps information about user interface */
Ui::MainWindow *ui;
/** @brief doc dom document container */
VPattern *doc;
/** @brief tool current tool */
Tool currentTool;
@ -187,9 +184,6 @@ private:
/** @brief comboBoxDraws comboc who show name of pattern peaces. */
QComboBox *comboBoxDraws;
/** @brief fileName name current pattern file. */
QString curFile;
/** @brief mode keep current draw mode. */
Draw mode;
@ -222,6 +216,7 @@ private:
void SetEnableWidgets(bool enable);
void SetEnableTool(bool enable);
void SetLayoutModeActions(bool enable);
void SaveCurrentScene();
void RestoreCurrentScene();

View file

@ -50,7 +50,7 @@
<rect>
<x>0</x>
<y>0</y>
<width>100</width>
<width>105</width>
<height>272</height>
</rect>
</property>
@ -337,7 +337,7 @@
<rect>
<x>0</x>
<y>0</y>
<width>100</width>
<width>105</width>
<height>58</height>
</rect>
</property>
@ -413,7 +413,7 @@
<rect>
<x>0</x>
<y>0</y>
<width>100</width>
<width>105</width>
<height>156</height>
</rect>
</property>
@ -567,7 +567,7 @@
<rect>
<x>0</x>
<y>0</y>
<width>100</width>
<width>105</width>
<height>104</height>
</rect>
</property>
@ -745,8 +745,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>87</width>
<height>58</height>
<width>105</width>
<height>380</height>
</rect>
</property>
<attribute name="label">
@ -815,11 +815,27 @@
<property name="title">
<string>&amp;File</string>
</property>
<widget class="QMenu" name="menuLayout">
<property name="title">
<string>Layout</string>
</property>
<addaction name="actionSaveAsPDF"/>
<addaction name="actionPrintPreview"/>
<addaction name="actionPrint"/>
<addaction name="separator"/>
<addaction name="actionSaveAsTiledPDF"/>
<addaction name="actionPrintPreviewTailed"/>
<addaction name="actionPrintTiled"/>
<addaction name="separator"/>
<addaction name="actionExportAs"/>
</widget>
<addaction name="actionNew"/>
<addaction name="actionOpen"/>
<addaction name="actionSave"/>
<addaction name="actionSaveAs"/>
<addaction name="separator"/>
<addaction name="menuLayout"/>
<addaction name="separator"/>
<addaction name="actionPreferences"/>
<addaction name="separator"/>
<addaction name="actionExit"/>
@ -1514,6 +1530,115 @@
<string>F2</string>
</property>
</action>
<action name="actionSaveAsPDF">
<property name="enabled">
<bool>false</bool>
</property>
<property name="icon">
<iconset resource="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>
</action>
<action name="actionSaveAsTiledPDF">
<property name="enabled">
<bool>false</bool>
</property>
<property name="icon">
<iconset resource="share/resources/icon.qrc">
<normaloff>:/icon/32x32/pdf.png</normaloff>:/icon/32x32/pdf.png</iconset>
</property>
<property name="text">
<string>Save as tiled PDF</string>
</property>
<property name="toolTip">
<string>Split and save a layout into smaller pages</string>
</property>
</action>
<action name="actionPrint">
<property name="enabled">
<bool>false</bool>
</property>
<property name="icon">
<iconset theme="document-print">
<normaloff/>
</iconset>
</property>
<property name="text">
<string>Print</string>
</property>
<property name="toolTip">
<string>Print original a layout</string>
</property>
</action>
<action name="actionPrintTiled">
<property name="enabled">
<bool>false</bool>
</property>
<property name="icon">
<iconset theme="document-print">
<normaloff/>
</iconset>
</property>
<property name="text">
<string>Print tiled</string>
</property>
<property name="toolTip">
<string>Split and print a layout into smaller pages (for regular printers)</string>
</property>
</action>
<action name="actionPrintPreview">
<property name="enabled">
<bool>false</bool>
</property>
<property name="icon">
<iconset theme="document-print-preview">
<normaloff/>
</iconset>
</property>
<property name="text">
<string>Print preview</string>
</property>
<property name="toolTip">
<string>Print preview original layout</string>
</property>
</action>
<action name="actionPrintPreviewTailed">
<property name="enabled">
<bool>false</bool>
</property>
<property name="icon">
<iconset theme="document-print-preview">
<normaloff/>
</iconset>
</property>
<property name="text">
<string>Print preview tailed</string>
</property>
<property name="toolTip">
<string>Print preview tailed layout</string>
</property>
</action>
<action name="actionExportAs">
<property name="enabled">
<bool>false</bool>
</property>
<property name="icon">
<iconset resource="share/resources/icon.qrc">
<normaloff>:/icon/32x32/export_to_picture_document.png</normaloff>:/icon/32x32/export_to_picture_document.png</iconset>
</property>
<property name="text">
<string>Export As...</string>
</property>
<property name="toolTip">
<string>Export original layout</string>
</property>
</action>
</widget>
<layoutdefault spacing="6" margin="11"/>
<customwidgets>

View file

@ -29,21 +29,36 @@
#include "mainwindowsnogui.h"
#include "../core/vapplication.h"
#include "../container/vcontainer.h"
#include "../../libs/vobj/vobjpaintdevice.h"
#include "../dialogs/app/dialoglayoutsettings.h"
#include "../../libs/vlayout/vlayoutgenerator.h"
#include "../dialogs/app/dialoglayoutprogress.h"
#include "../dialogs/app/dialogsavelayout.h"
#include "../../libs/vlayout/vposter.h"
#include <QFileDialog>
#include <QFileInfo>
#include <QGraphicsScene>
#include <QMessageBox>
#include <QProcess>
#include <QToolButton>
#include <QtSvg>
#include <QPrinter>
#include <QPrintPreviewDialog>
#include <QPrintDialog>
#ifdef Q_OS_WIN
# define PDFTOPS "pdftops.exe"
#else
# define PDFTOPS "pdftops"
#endif
//---------------------------------------------------------------------------------------------------------------------
MainWindowsNoGUI::MainWindowsNoGUI(QWidget *parent)
: QMainWindow(parent), listDetails(QVector<VLayoutDetail>()), currentScene(nullptr), tempSceneLayout(nullptr),
pattern(new VContainer()), papers(QList<QGraphicsItem *>()), shadows(QList<QGraphicsItem *>()),
pattern(new VContainer()), doc(nullptr), papers(QList<QGraphicsItem *>()), shadows(QList<QGraphicsItem *>()),
scenes(QList<QGraphicsScene *>()), details(QList<QList<QGraphicsItem *> >()), undoAction(nullptr),
redoAction(nullptr), actionDockWidgetToolOptions(nullptr)
redoAction(nullptr), actionDockWidgetToolOptions(nullptr), curFile(QString()), isLayoutStale(true), isTiled(false)
{
InitTempLayoutScene();
}
@ -92,6 +107,7 @@ void MainWindowsNoGUI::ToolLayoutSettings(bool checked)
CreateShadows();
CreateScenes();
PrepareSceneList();
isLayoutStale = false;
break;
case LayoutErrors::ProcessStoped:
break;
@ -111,6 +127,224 @@ void MainWindowsNoGUI::ToolLayoutSettings(bool checked)
}
}
//---------------------------------------------------------------------------------------------------------------------
void MainWindowsNoGUI::ExportLayoutAs()
{
if (isLayoutStale)
{
if (ContinueIfLayoutStale() == QMessageBox::No)
{
return;
}
}
QMap<QString, QString> extByMessage = InitFormates();
DialogSaveLayout dialog(extByMessage, scenes.size(), FileName(), this);
if (dialog.exec() == QDialog::Rejected)
{
return;
}
QString suf = dialog.Formate();
suf.replace(".", "");
const QString path = dialog.Path();
qApp->getSettings()->SetPathLayout(path);
const QString mask = dialog.FileName();
for (int i=0; i < scenes.size(); ++i)
{
QGraphicsRectItem *paper = qgraphicsitem_cast<QGraphicsRectItem *>(papers.at(i));
if (paper)
{
const QString name = path + "/" + mask+QString::number(i+1) + dialog.Formate();
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));
const QStringList suffix = QStringList() << "svg" << "png" << "pdf" << "eps" << "ps" << "obj";
switch (suffix.indexOf(suf))
{
case 0: //svg
paper->setVisible(false);
SvgFile(name, i);
paper->setVisible(true);
break;
case 1: //png
PngFile(name, i);
break;
case 2: //pdf
PdfFile(name, i);
break;
case 3: //eps
EpsFile(name, i);
break;
case 4: //ps
PsFile(name, i);
break;
case 5: //obj
paper->setVisible(false);
ObjFile(name, i);
paper->setVisible(true);
break;
default:
qDebug() << "Can't recognize file suffix." << Q_FUNC_INFO;
break;
}
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;
}
}
}
//---------------------------------------------------------------------------------------------------------------------
void MainWindowsNoGUI::SaveAsPDF()
{
if (not isPagesUniform())
{
qCritical()<<tr("For saving multypage document all sheet should have the same size. Use export "
"function instead.");
return;
}
isTiled = false;
SaveLayoutAs();
}
//---------------------------------------------------------------------------------------------------------------------
void MainWindowsNoGUI::SaveAsTiledPDF()
{
isTiled = true;
SaveLayoutAs();
}
//---------------------------------------------------------------------------------------------------------------------
void MainWindowsNoGUI::PrintPages(QPrinter *printer)
{
if (printer == nullptr)
{
return;
}
const QVector<QImage> images = AllSheets();
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;
if (not painter.begin(printer))
{ // failed to open file
qWarning("failed to open file, is it writable?");
return;
}
// Handle the fromPage(), toPage(), supportsMultipleCopies(), and numCopies() values from QPrinter.
int firstPage = printer->fromPage() - 1;
if (firstPage >= poster.size())
{
return;
}
if (firstPage == -1)
{
firstPage = 0;
}
int lastPage = printer->toPage() - 1;
if (lastPage == -1 || lastPage >= poster.size())
{
lastPage = poster.size() - 1;
}
const int numPages = lastPage - firstPage + 1;
int copyCount = 1;
if (not printer->supportsMultipleCopies())
{
copyCount = printer->copyCount();
}
for (int i = 0; i < copyCount; ++i)
{
for (int j = 0; j < numPages; ++j)
{
if (i != 0 || j != 0)
{
if (not printer->newPage())
{
qWarning("failed in flushing page to disk, disk full?");
return;
}
}
int index;
if (printer->pageOrder() == QPrinter::FirstPageFirst)
{
index = firstPage + j;
}
else
{
index = lastPage - j;
}
painter.drawImage(QPointF(), poster.at(index));
}
}
painter.end();
}
//---------------------------------------------------------------------------------------------------------------------
void MainWindowsNoGUI::PrintPreviewOrigin()
{
if (not isPagesUniform())
{
qCritical()<<tr("For previewing multypage document all sheet should have the same size.");
return;
}
isTiled = false;
PrintPreview();
}
//---------------------------------------------------------------------------------------------------------------------
void MainWindowsNoGUI::PrintPreviewTiled()
{
isTiled = true;
PrintPreview();
}
//---------------------------------------------------------------------------------------------------------------------
void MainWindowsNoGUI::PrintOrigin()
{
if (not isPagesUniform())
{
qCritical()<<tr("For printing multypages document all sheet should have the same size.");
return;
}
isTiled = false;
LayoutPrint();
}
//---------------------------------------------------------------------------------------------------------------------
void MainWindowsNoGUI::PrintTiled()
{
isTiled = false;
LayoutPrint();
}
//---------------------------------------------------------------------------------------------------------------------
void MainWindowsNoGUI::PrepareDetailsForLayout(const QHash<quint32, VDetail> *details)
{
@ -220,3 +454,420 @@ void MainWindowsNoGUI::CreateScenes()
scenes.append(scene);
}
}
//---------------------------------------------------------------------------------------------------------------------
QMap<QString, QString> MainWindowsNoGUI::InitFormates() const
{
QMap<QString, QString> extByMessage;
extByMessage[ tr("Svg files (*.svg)") ] = ".svg";
extByMessage[ tr("PDF files (*.pdf)") ] = ".pdf";
extByMessage[ tr("Images (*.png)") ] = ".png";
extByMessage[ tr("Wavefront OBJ (*.obj)") ] = ".obj";
QProcess proc;
#if defined(Q_OS_WIN) || defined(Q_OS_OSX)
proc.start(qApp->applicationDirPath()+"/"+PDFTOPS); // Seek pdftops in app bundle or near valentin.exe
#else
proc.start(PDFTOPS); // Seek pdftops in standard path
#endif
if (proc.waitForFinished(15000))
{
extByMessage[ tr("PS files (*.ps)") ] = ".ps";
extByMessage[ tr("EPS files (*.eps)") ] = ".eps";
}
else
{
qDebug()<<PDFTOPS<<"error"<<proc.error()<<proc.errorString();
}
return extByMessage;
}
//---------------------------------------------------------------------------------------------------------------------
/**
* @brief SvgFile save layout to svg file.
* @param name name layout file.
*/
void MainWindowsNoGUI::SvgFile(const QString &name, int i) const
{
QGraphicsRectItem *paper = qgraphicsitem_cast<QGraphicsRectItem *>(papers.at(i));
if (paper)
{
QSvgGenerator generator;
generator.setFileName(name);
generator.setSize(paper->rect().size().toSize());
generator.setViewBox(paper->rect());
generator.setTitle("Valentina. Pattern layout");
generator.setDescription(doc->GetDescription());
generator.setResolution(static_cast<int>(qApp->PrintDPI));
QPainter painter;
painter.begin(&generator);
painter.setFont( QFont( "Arial", 8, QFont::Normal ) );
painter.setRenderHint(QPainter::Antialiasing, true);
painter.setPen(QPen(Qt::black, qApp->toPixel(qApp->widthHairLine()), Qt::SolidLine, Qt::RoundCap,
Qt::RoundJoin));
painter.setBrush ( QBrush ( Qt::NoBrush ) );
scenes.at(i)->render(&painter);
painter.end();
}
}
//---------------------------------------------------------------------------------------------------------------------
/**
* @brief PngFile save layout to png file.
* @param name name layout file.
*/
void MainWindowsNoGUI::PngFile(const QString &name, int i) const
{
QGraphicsRectItem *paper = qgraphicsitem_cast<QGraphicsRectItem *>(papers.at(i));
if (paper)
{
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_ARGB32);
image.fill(Qt::transparent); // Start all pixels transparent
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, r, r);
image.save(name);
}
}
//---------------------------------------------------------------------------------------------------------------------
/**
* @brief PdfFile save layout to pdf file.
* @param name name layout file.
*/
void MainWindowsNoGUI::PdfFile(const QString &name, int i) const
{
QGraphicsRectItem *paper = qgraphicsitem_cast<QGraphicsRectItem *>(papers.at(i));
if (paper)
{
QPrinter printer;
printer.setCreator(qApp->applicationDisplayName()+" "+qApp->applicationVersion());
printer.setOutputFormat(QPrinter::PdfFormat);
printer.setOutputFileName(name);
printer.setDocName(FileName());
const QRectF r = paper->rect();
printer.setResolution(static_cast<int>(qApp->PrintDPI));
// Set orientation
if (paper->rect().height()>= paper->rect().width())
{
printer.setOrientation(QPrinter::Portrait);
}
else
{
printer.setOrientation(QPrinter::Landscape);
}
printer.setPaperSize ( QSizeF(qApp->fromPixel(r.width(), Unit::Mm), qApp->fromPixel(r.height(), Unit::Mm)),
QPrinter::Millimeter );
QPainter painter;
if (painter.begin( &printer ) == false)
{ // failed to open file
qCritical("Can't open printer %s", qPrintable(name));
return;
}
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();
}
}
//---------------------------------------------------------------------------------------------------------------------
/**
* @brief EpsFile save layout to eps file.
* @param name name layout file.
*/
void MainWindowsNoGUI::EpsFile(const QString &name, int i) const
{
QTemporaryFile tmp;
if (tmp.open())
{
PdfFile(tmp.fileName(), i);
QStringList params = QStringList() << "-eps" << tmp.fileName() << name;
PdfToPs(params);
}
}
//---------------------------------------------------------------------------------------------------------------------
/**
* @brief PsFile save layout to ps file.
* @param name name layout file.
*/
void MainWindowsNoGUI::PsFile(const QString &name, int i) const
{
QTemporaryFile tmp;
if (tmp.open())
{
PdfFile(tmp.fileName(), i);
QStringList params = QStringList() << tmp.fileName() << name;
PdfToPs(params);
}
}
//---------------------------------------------------------------------------------------------------------------------
/**
* @brief PdfToPs use external tool "pdftops" for converting pdf too eps or ps format.
* @param params string with parameter for tool. Parameters have format: "-eps input_file out_file". Use -eps when
* need create eps file.
*/
void MainWindowsNoGUI::PdfToPs(const QStringList &params) const
{
#ifndef QT_NO_CURSOR
QApplication::setOverrideCursor(Qt::WaitCursor);
#endif
QProcess proc;
proc.start(PDFTOPS, params);
proc.waitForFinished(15000);
#ifndef QT_NO_CURSOR
QApplication::restoreOverrideCursor();
#endif
QFile f(params.last());
if (f.exists() == false)
{
QString msg = QString(tr("Creating file '%1' failed! %2")).arg(params.last()).arg(proc.errorString());
QMessageBox msgBox(QMessageBox::Critical, tr("Critical error!"), msg, QMessageBox::Ok | QMessageBox::Default);
msgBox.exec();
}
}
//---------------------------------------------------------------------------------------------------------------------
void MainWindowsNoGUI::ObjFile(const QString &name, int i) const
{
QGraphicsRectItem *paper = qgraphicsitem_cast<QGraphicsRectItem *>(papers.at(i));
if (paper)
{
VObjPaintDevice generator;
generator.setFileName(name);
generator.setSize(paper->rect().size().toSize());
generator.setResolution(static_cast<int>(qApp->PrintDPI));
QPainter painter;
painter.begin(&generator);
scenes.at(i)->render(&painter);
painter.end();
}
}
//---------------------------------------------------------------------------------------------------------------------
QVector<QImage> MainWindowsNoGUI::AllSheets()
{
QVector<QImage> images;
for (int i=0; i < scenes.size(); ++i)
{
QGraphicsRectItem *paper = qgraphicsitem_cast<QGraphicsRectItem *>(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<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(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 MainWindowsNoGUI::SaveLayoutAs()
{
if (isLayoutStale)
{
if (ContinueIfLayoutStale() == QMessageBox::No)
{
return;
}
}
QPrinter printer;
SetPrinterSettings(&printer);
printer.setOutputFormat(QPrinter::PdfFormat);
QString fileName = QFileDialog::getSaveFileName(this, tr("Print to pdf"),
qApp->getSettings()->GetPathLayout()+"/"+FileName()+".pdf",
tr("PDF file (*.pdf)"));
if (not fileName.isEmpty())
{
QFileInfo f( fileName );
if(f.suffix().isEmpty())
{
fileName.append(".pdf");
}
qApp->getSettings()->SetPathLayout(f.absolutePath());
printer.setOutputFileName(fileName);
printer.setResolution(static_cast<int>(VApplication::PrintDPI));
PrintPages( &printer );
}
}
//---------------------------------------------------------------------------------------------------------------------
void MainWindowsNoGUI::PrintPreview()
{
if (isLayoutStale)
{
if (ContinueIfLayoutStale() == QMessageBox::No)
{
return;
}
}
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<int>(VApplication::PrintDPI));
SetPrinterSettings(&printer);
// display print preview dialog
QPrintPreviewDialog preview(&printer);
connect(&preview, &QPrintPreviewDialog::paintRequested, this, &MainWindowsNoGUI::PrintPages);
preview.exec();
}
//---------------------------------------------------------------------------------------------------------------------
void MainWindowsNoGUI::LayoutPrint()
{
if (isLayoutStale)
{
if (ContinueIfLayoutStale() == QMessageBox::No)
{
return;
}
}
// display print dialog and if accepted print
QPrinter printer(QPrinter::HighResolution);
SetPrinterSettings(&printer);
QPrintDialog dialog( &printer, this );
// If only user couldn't change page margins we could use method setMinMax();
dialog.setOption(QPrintDialog::PrintCurrentPage, false);
if ( dialog.exec() == QDialog::Accepted )
{
printer.setResolution(static_cast<int>(VApplication::PrintDPI));
PrintPages( &printer );
}
}
//---------------------------------------------------------------------------------------------------------------------
void MainWindowsNoGUI::SetPrinterSettings(QPrinter *printer)
{
SCASSERT(printer != nullptr)
printer->setCreator(qApp->applicationDisplayName()+" "+qApp->applicationVersion());
// Set orientation
if (papers.size() > 0)
{
QGraphicsRectItem *paper = qgraphicsitem_cast<QGraphicsRectItem *>(papers.at(0));
SCASSERT(paper != nullptr)
if (paper->rect().height()>= paper->rect().width())
{
printer->setOrientation(QPrinter::Portrait);
}
else
{
printer->setOrientation(QPrinter::Landscape);
}
}
if (not isTiled && papers.size() > 0)
{
QGraphicsRectItem *paper = qgraphicsitem_cast<QGraphicsRectItem *>(papers.at(0));
SCASSERT(paper != nullptr)
printer->setPaperSize ( QSizeF(qApp->fromPixel(paper->rect().width(), Unit::Mm),
qApp->fromPixel(paper->rect().height(), Unit::Mm)), QPrinter::Millimeter );
}
printer->setDocName(FileName());
}
//---------------------------------------------------------------------------------------------------------------------
bool MainWindowsNoGUI::isPagesUniform() const
{
if (papers.size() < 2)
{
return true;
}
else
{
QGraphicsRectItem *paper = qgraphicsitem_cast<QGraphicsRectItem *>(papers.at(0));
SCASSERT(paper != nullptr)
for (int i=1; i < papers.size(); ++i)
{
QGraphicsRectItem *p = qgraphicsitem_cast<QGraphicsRectItem *>(papers.at(i));
SCASSERT(p != nullptr)
if (paper->rect() != p->rect())
{
return false;
}
}
}
return true;
}
//---------------------------------------------------------------------------------------------------------------------
QString MainWindowsNoGUI::FileName() const
{
QString fileName;
curFile.isEmpty() ? fileName = tr("unnamed") : fileName = curFile;
return QFileInfo(fileName).baseName();
}
//---------------------------------------------------------------------------------------------------------------------
int MainWindowsNoGUI::ContinueIfLayoutStale()
{
QMessageBox msgBox(this);
msgBox.setIcon(QMessageBox::Question);
msgBox.setWindowTitle(tr("The layout is stale."));
msgBox.setText(tr("The layout was not updated since last pattern modification. Do you want to continue?"));
msgBox.setStandardButtons(QMessageBox::Yes|QMessageBox::No);
msgBox.setDefaultButton(QMessageBox::No);
QSpacerItem* horizontalSpacer = new QSpacerItem(500, 0, QSizePolicy::Minimum, QSizePolicy::Expanding);
QGridLayout* layout = static_cast<QGridLayout*>(msgBox.layout());
SCASSERT(layout != nullptr);
layout->addItem(horizontalSpacer, layout->rowCount(), 0, 1, layout->columnCount());
msgBox.exec();
return msgBox.result();
}

View file

@ -33,8 +33,10 @@
#include "../geometry/vdetail.h"
#include "../libs/vlayout/vlayoutdetail.h"
#include "xml/vpattern.h"
class QGraphicsScene;
class QPrinter;
class MainWindowsNoGUI : public QMainWindow
{
@ -45,6 +47,14 @@ public:
public slots:
void ToolLayoutSettings(bool checked);
void ExportLayoutAs();
void SaveAsPDF();
void SaveAsTiledPDF();
void PrintPages (QPrinter *printer);
void PrintPreviewOrigin();
void PrintPreviewTiled();
void PrintOrigin();
void PrintTiled();
protected:
QVector<VLayoutDetail> listDetails;
@ -57,6 +67,9 @@ protected:
/** @brief pattern container with data (points, arcs, splines, spline paths, variables) */
VContainer *pattern;
/** @brief doc dom document container */
VPattern *doc;
QList<QGraphicsItem *> papers;
QList<QGraphicsItem *> shadows;
QList<QGraphicsScene *> scenes;
@ -66,6 +79,11 @@ protected:
QAction *redoAction;
QAction *actionDockWidgetToolOptions;
/** @brief fileName name current pattern file. */
QString curFile;
bool isLayoutStale;
void PrepareDetailsForLayout(const QHash<quint32, VDetail> *details);
void InitTempLayoutScene();
@ -76,9 +94,33 @@ protected:
private:
Q_DISABLE_COPY(MainWindowsNoGUI)
bool isTiled;
void CreateShadows();
void CreateScenes();
QMap<QString, QString> InitFormates() 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 &params)const;
void ObjFile(const QString &name, int i)const;
QVector<QImage> AllSheets();
void SaveLayoutAs();
void PrintPreview();
void LayoutPrint();
void SetPrinterSettings(QPrinter *printer);
bool isPagesUniform() const;
QString FileName() const;
int ContinueIfLayoutStale();
};
#endif // MAINWINDOWSNOGUI_H

View file

@ -60,5 +60,6 @@
<file>icon/16x16/fx.png</file>
<file>icon/16x16/roll.png</file>
<file>icon/16x16/progress.gif</file>
<file>icon/32x32/export_to_picture_document.png</file>
</qresource>
</RCC>

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.1 KiB