Seamly2D
Code documentation
export_format_combobox.cpp
Go to the documentation of this file.
1  /******************************************************************************
2  * @file export_format_combobox.cpp
3  ** @author DS Caskey
4  ** @date Mar 15, 2022
5  **
6  ** @brief
7  ** @copyright
8  **
9  ** Seamly2D is free software: you can redistribute it and/or modify
10  ** it under the terms of the GNU General Public License as published by
11  ** the Free Software Foundation, either version 3 of the License, or
12  ** (at your option) any later version.
13  **
14  ** Seamly2D is distributed in the hope that it will be useful,
15  ** but WITHOUT ANY WARRANTY; without even the implied warranty of
16  ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  ** GNU General Public License for more details.
18  **
19  ** You should have received a copy of the GNU General Public License
20  ** along with Seamly2D. If not, see <http://www.gnu.org/licenses/>.
21  **
22  *****************************************************************************/
23 
24 #include <QAbstractItemView>
25 #include <QPen>
26 #include <Qt>
27 #include <QPainter>
28 #include <QPixmap>
29 #include <QProcess>
30 #include <QDebug>
31 #include <QVariant>
32 
33 #include "export_format_combobox.h"
34 #include "../vmisc/vabstractapplication.h"
35 #include "../vmisc/vcommonsettings.h"
36 
37 #ifdef Q_OS_WIN
38 # define PDFTOPS "pdftops.exe"
39 #else
40 # define PDFTOPS "pdftops"
41 #endif
42 
44 bool ExportFormatCombobox::tested = false;
45 
46 /**
47  * Constructor with name.
48  */
49 ExportFormatCombobox::ExportFormatCombobox(QWidget *parent, const char *name)
50  : QComboBox(parent)
51  , m_currentFormat()
52 {
53  setObjectName(name);
54  setEditable ( false );
55  init();
56 }
57 
58 /**
59  * Destructor
60  */
62 
63 /**
64  * Initialisation called from constructor or manually but only once.
65  */
67 {
68  this->blockSignals(true);
69 
70  int count = 0;
71  foreach (auto& v, initFormats())
72  {
73  addItem(v.first, QVariant(static_cast<int>(v.second)));
74  count++;
75  }
76  setMaxVisibleItems(count);
77 
78  this->view()->setTextElideMode(Qt::ElideNone);
79 
80  this->blockSignals(false);
81  connect(this, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &ExportFormatCombobox::updateExportFormat);
82 
84  updateExportFormat(currentIndex());
85 }
86 
88 {
89  return m_currentFormat;
90 }
91 
92 /**
93  * Sets the currently selected format item to the given format.
94  */
96 {
97  m_currentFormat = format;
98 
99  setCurrentIndex(findData(QVariant(static_cast<int>(format))));
100 
101  if (currentIndex()!= count() -1 )
102  {
103  updateExportFormat(currentIndex());
104  }
105 }
106 
107 //---------------------------------------------------------------------------------------------------------------------
109 {
111 
112  auto InitFormat = [&list](LayoutExportFormat format)
113  {
114  list.append(std::make_pair(exportFormatDescription(format), format));
115  };
116 
117  InitFormat(LayoutExportFormat::SVG);
118  InitFormat(LayoutExportFormat::PDF);
119  InitFormat(LayoutExportFormat::PDFTiled);
120  InitFormat(LayoutExportFormat::PNG);
121  InitFormat(LayoutExportFormat::JPG);
122  InitFormat(LayoutExportFormat::BMP);
123  InitFormat(LayoutExportFormat::TIF);
124  InitFormat(LayoutExportFormat::PPM);
125  InitFormat(LayoutExportFormat::OBJ);
126  if (supportPSTest())
127  {
128  InitFormat(LayoutExportFormat::PS);
129  InitFormat(LayoutExportFormat::EPS);
130  }
149  // We will support them anyway
150 // InitFormat(LayoutExportFormat::DXF_AC1006_ASTM);
151 // InitFormat(LayoutExportFormat::DXF_AC1009_ASTM);
152 // InitFormat(LayoutExportFormat::DXF_AC1012_ASTM);
153 // InitFormat(LayoutExportFormat::DXF_AC1014_ASTM);
154 // InitFormat(LayoutExportFormat::DXF_AC1015_ASTM);
155 // InitFormat(LayoutExportFormat::DXF_AC1018_ASTM);
156 // InitFormat(LayoutExportFormat::DXF_AC1021_ASTM);
157 // InitFormat(LayoutExportFormat::DXF_AC1024_ASTM);
158 // InitFormat(LayoutExportFormat::DXF_AC1027_ASTM);
159 
160  return list;
161 }
162 
163 //---------------------------------------------------------------------------------------------------------------------
165 {
166  if (!tested)
167  {
168  havePdf = testPdf();
169  tested = true;
170  }
171  return havePdf;
172 }
173 
174 //---------------------------------------------------------------------------------------------------------------------
176 {
177  bool res = false;
178 
179  QProcess proc;
180  QStringList args;
181 
182 #if defined(Q_OS_WIN) || defined(Q_OS_OSX)
183  // Seek pdftops in app bundle or near valentin.exe
184  proc.start(qApp->applicationDirPath() + QLatin1String("/")+ PDFTOPS, QStringList());
185 #else
186  proc.start(PDFTOPS, QStringList()); // Seek pdftops in standard path
187 #endif
188 
189  if (proc.waitForStarted(15000) && (proc.waitForFinished(15000) || proc.state() == QProcess::NotRunning))
190  {
191  res = true;
192  }
193  else
194  {
195  qDebug()<<PDFTOPS<<"error"<<proc.error()<<proc.errorString();
196  }
197  return res;
198 }
199 
200 //---------------------------------------------------------------------------------------------------------------------
202 {
203  const QString dxfSuffix = QStringLiteral("(*.dxf)");
204  const QString dxfFlatFilesStr = tr("(flat) files");
205  const QString filesStr = tr("files");
206 
207  switch(format)
208  {
210  return QString("SVG %1 (*.svg)").arg(filesStr);
212  return QString("PDF %1 (*.pdf)").arg(filesStr);
214  return QString("PNG %1 (*.png)").arg(filesStr);
216  return QString("JPG %1 (*.jpg)").arg(filesStr);
218  return QString("BMP %1 (*.bmp)").arg(filesStr);
220  return QString("TIF %1 (*.tif)").arg(filesStr);
222  return QString("PPM %1 (*.ppm)").arg(filesStr);
224  return "Wavefront OBJ (*.obj)";
226  return QString("PS %1 (*.ps)").arg(filesStr);
228  return QString("EPS %1 (*.eps)").arg(filesStr);
230  return QString("AutoCAD DXF R10 %1 %2").arg(dxfFlatFilesStr, dxfSuffix);
232  return QString("AutoCAD DXF R11/12 %1 %2").arg(dxfFlatFilesStr, dxfSuffix);
234  return QString("AutoCAD DXF R13 %1 %2").arg(dxfFlatFilesStr, dxfSuffix);
236  return QString("AutoCAD DXF R14 %1 %2").arg(dxfFlatFilesStr, dxfSuffix);
238  return QString("AutoCAD DXF 2000 %1 %2").arg(dxfFlatFilesStr, dxfSuffix);
240  return QString("AutoCAD DXF 2004 %1 %2").arg(dxfFlatFilesStr, dxfSuffix);
242  return QString("AutoCAD DXF 2007 %1 %2").arg(dxfFlatFilesStr, dxfSuffix);
244  return QString("AutoCAD DXF 2010 %1 %2").arg(dxfFlatFilesStr, dxfSuffix);
246  return QString("AutoCAD DXF 2013 %1 %2").arg(dxfFlatFilesStr, dxfSuffix);
248  return QString("AutoCAD DXF R10 AAMA %1 %2").arg(filesStr, dxfSuffix);
250  return QString("AutoCAD DXF R11/12 AAMA %1 %2").arg(filesStr, dxfSuffix);
252  return QString("AutoCAD DXF R13 AAMA %1 %2").arg(filesStr, dxfSuffix);
254  return QString("AutoCAD DXF R14 AAMA %1 %2").arg(filesStr, dxfSuffix);
256  return QString("AutoCAD DXF 2000 AAMA %1 %2").arg(filesStr, dxfSuffix);
258  return QString("AutoCAD DXF 2004 AAMA %1 %2").arg(filesStr, dxfSuffix);
260  return QString("AutoCAD DXF 2007 AAMA %1 %2").arg(filesStr, dxfSuffix);
262  return QString("AutoCAD DXF 2010 AAMA %1 %2").arg(filesStr, dxfSuffix);
264  return QString("AutoCAD DXF 2013 AAMA %1 %2").arg(filesStr, dxfSuffix);
266  return QString("AutoCAD DXF R10 ASTM %1 %2").arg(filesStr, dxfSuffix);
268  return QString("AutoCAD DXF R11/12 ASTM %1 %2").arg(filesStr, dxfSuffix);
270  return QString("AutoCAD DXF R13 ASTM %1 %2").arg(filesStr, dxfSuffix);
272  return QString("AutoCAD DXF R14 ASTM %1 %2").arg(filesStr, dxfSuffix);
274  return QString("AutoCAD DXF 2000 ASTM %1 %2").arg(filesStr, dxfSuffix);
276  return QString("AutoCAD DXF 2004 ASTM %1 %2").arg(filesStr, dxfSuffix);
278  return QString("AutoCAD DXF 2007 ASTM %1 %2").arg(filesStr, dxfSuffix);
280  return QString("AutoCAD DXF 2010 ASTM %1 %2").arg(filesStr, dxfSuffix);
282  return QString("AutoCAD DXF 2013 ASTM %1 %2").arg(filesStr, dxfSuffix);
284  return QString("PDF tiled %1 (*.pdf)").arg(filesStr);
285  default:
286  return QString();
287  }
288 }
289 
290 //---------------------------------------------------------------------------------------------------------------------
292 {
293  QString out("\n");
294  foreach(auto& v, initFormats())
295  {
296  out += QLatin1String("\t") + v.first + QLatin1String(" = ") + QString::number(static_cast<int>(v.second))
297  + QLatin1String("\n");
298  }
299  return out;
300 }
301 
302 /**
303  * Called when the width has changed. This method sets the current width to the value chosen or even
304  * offers a dialog to the user that allows him/ her to choose an individual width.
305  */
307 {
308  QVariant format = itemData(index);
309  if(format != QVariant::Invalid )
310  {
311  m_currentFormat = static_cast<LayoutExportFormat>(this->currentData().toInt());
312  }
313 
315 }
316 
318 {
319  QString format = qApp->Settings()->getExportFormat();
320 
321  int index = findText(format);
322  if (index != -1)
323  {
324  setCurrentIndex(index);
325  }
326  else
327  {
328  setCurrentIndex(0);
329  }
330 }
static QVector< std::pair< QString, LayoutExportFormat > > initFormats()
ExportFormatCombobox(QWidget *parent=nullptr, const char *name=nullptr)
static QString exportFormatDescription(LayoutExportFormat format)
void exportFormatChanged(const LayoutExportFormat &format)
LayoutExportFormat getExportFormat() const
LayoutExportFormat m_currentFormat
void setExportFormat(LayoutExportFormat &format)
static QString makeHelpFormatList()
LayoutExportFormat
Definition: def.h:60
#define PDFTOPS
#define qApp
Definition: vapplication.h:67