Qucs-GUI  0.0.18
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
exportdialog.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  exportdiagramdialog.cpp
3  ------------------
4  begin : Thu Nov 28 2013
5  copyright : (C) 2013 by Vadim Kuznetzov
6  email : <ra3xdh@gmail.com>
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 #include <math.h>
18 #include "exportdialog.h"
19 
20 ExportDialog::ExportDialog(int w, int h, int wsel, int hsel, QString filename_, bool nosel_, QWidget *parent) :
21  QDialog(parent)
22 {
23 
24  setCaption(tr("Export graphics"));
25  dwidth = w;
26  dheight = h;
27  dwidthsel = wsel;
28  dheightsel = hsel;
29  svg = false;
30  noselected = nosel_;
31 
32  filename = filename_;
33 
34  lblFilename = new QLabel(tr("Save to file (Graphics format by extension)"));
35  lblResolutionX = new QLabel(tr("Width in pixels"));
36  lblResolutionY = new QLabel(tr("Height in pixels"));
37  lblRatio = new QLabel(tr("Scale factor: "));
38  lblFormat = new QLabel(tr("Image format:"));
39 
40  ExportButt = new QPushButton(tr("Export"));
41  connect(ExportButt,SIGNAL(clicked()),this,SLOT(accept()));
42  CancelButt = new QPushButton(tr("Cancel"));
43  connect(CancelButt,SIGNAL(clicked()),this,SLOT(reject()));
44  SaveButt = new QPushButton(tr("File"));
45  connect(SaveButt,SIGNAL(clicked()),this,SLOT(setFileName()));
46 
47  editFilename = new QLineEdit(filename);
48  connect(editFilename,SIGNAL(textChanged(QString)),this,SLOT(setSvg(QString)));
49 
50  editResolutionX = new QLineEdit(QString::number(dwidth));
51  QIntValidator *val = new QIntValidator(0,64000,this);
52  editResolutionX->setValidator(val);
53  editResolutionX->setEnabled(false);
54  editResolutionY = new QLineEdit(QString::number(dheight));
55  editResolutionY->setValidator(val);
56  editResolutionY->setEnabled(false);
57  editScale = new QLineEdit(QString::number(1.0));
58  QDoubleValidator *val1 = new QDoubleValidator(0,20.0,2,this);
59  editScale->setValidator(val1);
60 
61  cbxImgType = new QComboBox(this);
62  QStringList lst;
63  lst<<tr("Colour")<<tr("Monochrome");
64  cbxImgType->addItems(lst);
65 
66  cbRatio = new QCheckBox(tr("Original width to height ratio"));
67  cbRatio->setChecked(true);
68  connect(cbRatio,SIGNAL(toggled(bool)),this,SLOT(recalcRatio()));
69 
70  cbResolution = new QCheckBox(tr("Original size"));
71  connect(cbResolution,SIGNAL(toggled(bool)),editResolutionX,SLOT(setDisabled(bool)));
72  connect(cbResolution,SIGNAL(toggled(bool)),editResolutionY,SLOT(setDisabled(bool)));
73  connect(cbResolution,SIGNAL(toggled(bool)),cbRatio,SLOT(setDisabled(bool)));
74  connect(cbResolution,SIGNAL(toggled(bool)),editScale,SLOT(setDisabled(bool)));
75  connect(cbResolution,SIGNAL(toggled(bool)),this,SLOT(restoreOriginalWtoH()));
76  cbResolution->setChecked(true);
77 
78  connect(editResolutionX,SIGNAL(textEdited(QString)),this,SLOT(calcHeight()));
79  connect(editResolutionY,SIGNAL(textEdited(QString)),this,SLOT(calcWidth()));
80  connect(editScale,SIGNAL(textChanged(QString)),this,SLOT(recalcScale()));
81 
82  cbSelected = new QCheckBox(tr("Export selected only"));
83  connect(cbSelected,SIGNAL(toggled(bool)),this,SLOT(setSelectedWH()));
84  cbSelected->setChecked(false);
85  if (noselected) cbSelected->setDisabled(true);
86 
87  //cbResolution->setEnabled(false);
88  cbRatio->setEnabled(false);
89 
90 
91  top = new QVBoxLayout;
92  lower1 = new QHBoxLayout;
93  lower2 = new QHBoxLayout;
94  lower3 = new QHBoxLayout;
95  lower4 = new QHBoxLayout;
96 
97  top->addWidget(lblFilename);
98  lower1->addWidget(editFilename);
99  lower1->addWidget(SaveButt);
100  top->addLayout(lower1);
101  lower4->addWidget(lblFormat);
102  lower4->addWidget(cbxImgType);
103  top->addLayout(lower4);
104  top->addWidget(cbResolution);
105  //top->addWidget(cbRatio);
106  lower3->addWidget(lblRatio);
107  lower3->addWidget(editScale);
108  top->addLayout(lower3);
109  top->addWidget(lblResolutionX);
110  top->addWidget(editResolutionX);
111  top->addWidget(lblResolutionY);
112  top->addWidget(editResolutionY);
113  top->addWidget(cbSelected);
114 
115  lower2->addWidget(ExportButt);
116  lower2->addWidget(CancelButt);
117  top->addLayout(lower2);
118  this->setLayout(top);
119 
120  this->layout()->setSizeConstraint(QLayout::SetFixedSize);
121  this->setWindowTitle(tr("Export schematic to raster or vector image"));
122 
123  this->setSvg(editFilename->text());
124 }
125 
127 {
128  return editFilename->text();
129 }
130 
132 {
133  return cbResolution->isChecked();
134 }
135 
137 {
138  return editResolutionX->text().toInt();
139 }
140 
142 {
143  return editResolutionY->text().toInt();
144 }
145 
147 {
148  /*QString nam = dialog.getSaveFileName(this,tr("Export diagram to file"),QDir::homeDirPath(),
149  "SVG vector graphics (*.svg) ;;"
150  "PNG images (*.png) ;;"
151  "JPEG images (*.jpg *.jpeg)");
152  */
153  //QFileInfo inf(filename);
154  QFileDialog dialog(this, tr("Export to image"), editFilename->text(),
155  "SVG vector graphics (*.svg) ;;"
156  "PNG images (*.png) ;;"
157  "JPEG images (*.jpg *.jpeg) ;;"
158  "PDF (*.pdf) ;;"
159  "PDF + LaTeX (*.pdf_tex) ;;"
160  "EPS Encapsulated Postscript (*.eps)");
161  dialog.setAcceptMode(QFileDialog::AcceptSave);
162  if(dialog.exec())
163  {
164  QString nam = dialog.selectedFile();
165  QString extension;
166  if(dialog.selectedNameFilter().contains("*.png")) extension=QString(".png");
167  if(dialog.selectedNameFilter().contains("*.jpg")) extension=QString(".jpg");
168  if(dialog.selectedNameFilter().contains("*.svg")) extension=QString(".svg");
169  if(dialog.selectedNameFilter().contains("*.pdf")) extension=QString(".pdf");
170  if(dialog.selectedNameFilter().contains("*.pdf_tex")) extension=QString(".pdf_tex");
171  if(dialog.selectedNameFilter().contains("*.eps")) extension=QString(".eps");
172  if(nam.toLower().section("/",-1,-1).contains(".")) //has the user specified an extension?
173  editFilename->setText(nam); //yes, just leave unchanged
174  else
175  editFilename->setText(nam+extension); //no, add extension
176  }
177 }
178 
180 {
181  if (cbRatio->isChecked()) {
182  float h = editResolutionY->text().toFloat();
183  float w = round((h*dwidth)/dheight);
184  editResolutionX->setText(QString::number(w));
185  }
186 }
187 
188 
190 {
191  if (cbRatio->isChecked()) {
192  float w = editResolutionX->text().toFloat();
193  float h = round((w*dheight)/dwidth);
194  editResolutionY->setText(QString::number(h));
195  }
196 
197 }
198 
200 {
201  if (cbRatio->isChecked()) {
202  calcHeight();
203  }
204 }
205 
207 {
208  if (cbResolution->isChecked()) {
209  editScale->setText(QString::number(1.0));
210  editResolutionX->setText(QString::number(dwidth));
211  editResolutionY->setText(QString::number(dheight));
212  }
213 }
214 
216 {
217  return svg;
218 }
219 
221 {
222  return cbSelected->isChecked();
223 }
224 
225 void ExportDialog::setSvg(QString filename)
226 {
227  QFileInfo graphics_file(filename);
228  QString ext = graphics_file.suffix();
229  ext = ext.toLower();
230  if ((ext=="svg")||(ext=="pdf")||(ext=="eps")||(ext=="pdf_tex")) {
231  svg = true;
232  cbResolution->setChecked(true);
233  cbResolution->setDisabled(true);
234  cbxImgType->setDisabled(true);
235  cbRatio->setChecked(true);
236  } else {
237  svg = false;
238  cbResolution->setEnabled(true);
239  cbxImgType->setEnabled(true);
240  }
241 }
242 
244 {
245  QString nam = editFilename->text();
246  QStringList filetypes;
247  QFileInfo inf(nam);
248  filetypes<<"png"<<"svg"<<"jpeg"<<"jpg"<<"pdf"<<"pdf_tex"<<"eps"
249  <<"PNG"<<"JPG"<<"SVG"<<"JPEG"<<"PDF"
250  <<"PDF_TEX"<<"EPS";
251 
252  if (filetypes.contains(inf.suffix())) {
253  return true;
254  } else {
255  return false;
256  }
257 }
258 
260 {
261  QString nam = editFilename->text();
262  QStringList filetypes;
263  QFileInfo inf(nam);
264  filetypes<<"pdf"<<"pdf_tex"<<"eps"<<"PDF"<<"PDF_TEX"<<"EPS";
265 
266  if (filetypes.contains(inf.suffix())) {
267  return true;
268  } else {
269  return false;
270  }
271 }
272 
274 {
275  QFileInfo inf(editFilename->text());
276  if (inf.suffix().toLower()=="pdf") return true;
277  else return false;
278 }
279 
281 {
282  QFileInfo inf(editFilename->text());
283  if (inf.suffix().toLower()=="pdf_tex") return true;
284  else return false;
285 }
286 
288 {
289  QFileInfo inf(editFilename->text());
290  if (inf.suffix().toLower()=="eps") return true;
291  else return false;
292 }
293 
295 {
296  if (cbSelected->isChecked()) {
297  editResolutionX->setText(QString::number(dwidthsel));
298  editResolutionY->setText(QString::number(dheightsel));
299  } else {
300  editResolutionX->setText(QString::number(dwidth));
301  editResolutionY->setText(QString::number(dheight));
302  }
303  recalcScale();
304 }
305 
307 {
308  cbSelected->setChecked(true);
309  cbSelected->setDisabled(true);
310  this->setWindowTitle(tr("Export diagram to raster or vector image"));
311 }
312 
314 {
315  scale = editScale->text().toFloat();
316  return scale;
317 }
318 
320 {
321  scale = editScale->text().toFloat();
322  if (cbSelected->isChecked()) {
323  editResolutionX->setText(QString::number(scale*dwidthsel));
324  editResolutionY->setText(QString::number(scale*dheightsel));
325  } else {
326  editResolutionX->setText(QString::number(scale*dwidth));
327  editResolutionY->setText(QString::number(scale*dheight));
328  }
329 
330 }
331 
333 {
334  switch (cbxImgType->currentIndex()) {
335  case 0 : return ExportDialog::Coloured;
336  break;
337  case 1 : return ExportDialog::Monochrome;
338  break;
339  default : break;
340  }
341 }
QLabel * lblRatio
Definition: exportdialog.h:39
QLineEdit * editResolutionX
Definition: exportdialog.h:47
QCheckBox * cbResolution
Definition: exportdialog.h:42
void setFileName()
QPushButton * ExportButt
Definition: exportdialog.h:32
float getScale()
QLineEdit * editScale
Definition: exportdialog.h:49
bool needsInkscape()
QString FileToSave()
QLineEdit * editFilename
Definition: exportdialog.h:46
QComboBox * cbxImgType
Definition: exportdialog.h:51
QVBoxLayout * top
Definition: exportdialog.h:57
ExportDialog::ImgFormat getImgFormat()
void restoreOriginalWtoH()
QLabel * lblFilename
Definition: exportdialog.h:36
QPushButton * SaveButt
Definition: exportdialog.h:34
void recalcScale()
QLineEdit * editResolutionY
Definition: exportdialog.h:48
QCheckBox * cbSelected
Definition: exportdialog.h:44
QHBoxLayout * lower4
Definition: exportdialog.h:56
void setSelectedWH()
void recalcRatio()
ExportDialog(int w, int h, int wsel, int hsel, QString filename_, bool nosel_=true, QWidget *parent=0)
QLabel * lblFormat
Definition: exportdialog.h:40
void setSvg(QString filename)
bool isOriginalSize()
QString filename
Definition: exportdialog.h:67
QLabel * lblResolutionX
Definition: exportdialog.h:37
bool isValidFilename()
bool isExportSelected()
QCheckBox * cbRatio
Definition: exportdialog.h:43
QHBoxLayout * lower2
Definition: exportdialog.h:54
QPushButton * CancelButt
Definition: exportdialog.h:33
QHBoxLayout * lower1
Definition: exportdialog.h:53
QLabel * lblResolutionY
Definition: exportdialog.h:38
QHBoxLayout * lower3
Definition: exportdialog.h:55