LibrePCB Developers Documentation
graphicsexport.h
Go to the documentation of this file.
1 /*
2  * LibrePCB - Professional EDA for everyone!
3  * Copyright (C) 2013 LibrePCB Developers, see AUTHORS.md for contributors.
4  * https://librepcb.org/
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #ifndef LIBREPCB_CORE_GRAPHICSEXPORT_H
21 #define LIBREPCB_CORE_GRAPHICSEXPORT_H
22 
23 /*******************************************************************************
24  * Includes
25  ******************************************************************************/
26 #include "../fileio/filepath.h"
27 #include "../types/length.h"
28 #include "graphicsexportsettings.h"
29 
30 #include <optional/tl/optional.hpp>
31 
32 #include <QtCore>
33 #include <QtGui>
34 #include <QtPrintSupport>
35 
36 #include <memory>
37 
38 /*******************************************************************************
39  * Namespace / Forward Declarations
40  ******************************************************************************/
41 namespace librepcb {
42 
43 /*******************************************************************************
44  * Class GraphicsPagePainter
45  ******************************************************************************/
46 
58 public:
76  virtual void paint(QPainter& painter,
77  const GraphicsExportSettings& settings) const noexcept = 0;
78 };
79 
80 /*******************************************************************************
81  * Class GraphicsExport
82  ******************************************************************************/
83 
90 class GraphicsExport final : public QObject {
91  Q_OBJECT
92 
93 public:
94  // Types
95  typedef std::pair<std::shared_ptr<GraphicsPagePainter>,
96  std::shared_ptr<GraphicsExportSettings>>
98  typedef QList<Page> Pages;
99 
100  struct Result {
101  QVector<FilePath> writtenFiles;
102  QString errorMsg;
103  };
104 
105  // Constructors / Destructor
106  GraphicsExport(QObject* parent = nullptr) noexcept;
107  GraphicsExport(const GraphicsExport& other) = delete;
108  ~GraphicsExport() noexcept;
109 
110  // General Methods
111 
117  void setDocumentName(const QString& name) noexcept { mDocumentName = name; }
118 
127  void startPreview(const Pages& pages) noexcept;
128 
144  void startExport(const Pages& pages, const FilePath& filePath) noexcept;
145 
154  void startPrint(const Pages& pages, const QString& printerName,
155  QPrinter::DuplexMode duplex, int copies) noexcept;
156 
162  Result waitForFinished() noexcept;
163 
167  void cancel() noexcept;
168 
169  // Operator Overloadings
170  GraphicsExport& operator=(const GraphicsExport& rhs) = delete;
171 
172  // Static Methods.
173 
179  static QStringList getSupportedExtensions() noexcept;
180 
186  static QStringList getSupportedImageExtensions() noexcept;
187 
188 signals:
189  void previewReady(int index, const QSize& pageSize, const QRectF margins,
190  std::shared_ptr<QPicture> picture);
191  void savingFile(const librepcb::FilePath& filePath);
192  void progress(int percent, int completed, int total);
193  void succeeded();
194  void failed(const QString& error);
195  void imageCopiedToClipboard(const QImage& image, QClipboard::Mode mode);
196 
197 private: // Types
198  struct RunArgs {
199  bool preview;
200  Pages pages;
202  QString printerName;
203  QPrinter::DuplexMode duplex;
204  int copies;
205  };
206 
207 private: // Methods
208  Result run(RunArgs args) noexcept;
209  static QTransform getSourceTransformation(
210  const GraphicsExportSettings& settings) noexcept;
211  static QRectF calcSourceRect(const GraphicsPagePainter& page,
212  const GraphicsExportSettings& settings) noexcept;
213  static QPageLayout::Orientation getOrientation(const QSizeF& size) noexcept;
214 
215 private: // Data
216  QString mCreator;
217  QString mDocumentName;
218  QFuture<Result> mFuture;
219  bool mAbort;
220 };
221 
222 /*******************************************************************************
223  * End of File
224  ******************************************************************************/
225 
226 } // namespace librepcb
227 
228 #endif
bool preview
Definition: graphicsexport.h:199
std::pair< std::shared_ptr< GraphicsPagePainter >, std::shared_ptr< GraphicsExportSettings > > Page
Definition: graphicsexport.h:97
Asynchronously exports graphics to a QPainter.
Definition: graphicsexport.h:90
Definition: occmodel.cpp:77
QList< Page > Pages
Definition: graphicsexport.h:98
Definition: graphicsexport.h:198
QString mCreator
Definition: graphicsexport.h:216
QString printerName
Definition: graphicsexport.h:202
Pages pages
Definition: graphicsexport.h:200
QFuture< Result > mFuture
Definition: graphicsexport.h:218
Settings for librepcb::GraphicsExport.
Definition: graphicsexportsettings.h:51
QVector< FilePath > writtenFiles
Definition: graphicsexport.h:101
This class represents absolute, well-formatted paths to files or directories.
Definition: filepath.h:129
int copies
Definition: graphicsexport.h:204
virtual void paint(QPainter &painter, const GraphicsExportSettings &settings) const noexcept=0
Draw page content on a QPainter.
bool mAbort
Definition: graphicsexport.h:219
FilePath filePath
Definition: graphicsexport.h:201
QString errorMsg
Definition: graphicsexport.h:102
QString mDocumentName
Definition: graphicsexport.h:217
QPrinter::DuplexMode duplex
Definition: graphicsexport.h:203
Definition: graphicsexport.h:100
Base class for printing a page for librepcb::GraphicsExport.
Definition: graphicsexport.h:57