Qucs-GUI
0.0.19
|
00001 /*************************************************************************** 00002 exportdiagramdialog.cpp 00003 ------------------ 00004 begin : Thu Nov 28 2013 00005 copyright : (C) 2013 by Vadim Kuznetzov 00006 email : <ra3xdh@gmail.com> 00007 ***************************************************************************/ 00008 00009 /*************************************************************************** 00010 * * 00011 * This program is free software; you can redistribute it and/or modify * 00012 * it under the terms of the GNU General Public License as published by * 00013 * the Free Software Foundation; either version 2 of the License, or * 00014 * (at your option) any later version. * 00015 * * 00016 ***************************************************************************/ 00017 #include <cmath> 00018 #include "exportdialog.h" 00019 00020 #include <QValidator> 00021 #include <QLabel> 00022 #include <QPushButton> 00023 #include <QLineEdit> 00024 #include <QComboBox> 00025 #include <QCheckBox> 00026 #include <QVBoxLayout> 00027 #include <QHBoxLayout> 00028 #include <QFileInfo> 00029 #include <QFileDialog> 00030 00031 ExportDialog::ExportDialog(int w, int h, int wsel, int hsel, QString filename_, bool nosel_, QWidget *parent) : 00032 QDialog(parent) 00033 { 00034 00035 setWindowTitle(tr("Export graphics")); 00036 dwidth = w; 00037 dheight = h; 00038 dwidthsel = wsel; 00039 dheightsel = hsel; 00040 svg = false; 00041 noselected = nosel_; 00042 00043 filename = filename_; 00044 00045 lblFilename = new QLabel(tr("Save to file (Graphics format by extension)")); 00046 lblResolutionX = new QLabel(tr("Width in pixels")); 00047 lblResolutionY = new QLabel(tr("Height in pixels")); 00048 lblRatio = new QLabel(tr("Scale factor: ")); 00049 lblFormat = new QLabel(tr("Image format:")); 00050 00051 ExportButt = new QPushButton(tr("Export")); 00052 connect(ExportButt,SIGNAL(clicked()),this,SLOT(accept())); 00053 CancelButt = new QPushButton(tr("Cancel")); 00054 connect(CancelButt,SIGNAL(clicked()),this,SLOT(reject())); 00055 SaveButt = new QPushButton(tr("Browse")); 00056 connect(SaveButt,SIGNAL(clicked()),this,SLOT(setFileName())); 00057 00058 editFilename = new QLineEdit(filename); 00059 connect(editFilename,SIGNAL(textChanged(QString)),this,SLOT(setSvg(QString))); 00060 00061 editResolutionX = new QLineEdit(QString::number(dwidth)); 00062 QIntValidator *val = new QIntValidator(0,64000,this); 00063 editResolutionX->setValidator(val); 00064 editResolutionX->setEnabled(false); 00065 editResolutionY = new QLineEdit(QString::number(dheight)); 00066 editResolutionY->setValidator(val); 00067 editResolutionY->setEnabled(false); 00068 editScale = new QLineEdit(QString::number(1.0)); 00069 QDoubleValidator *val1 = new QDoubleValidator(0,20.0,2,this); 00070 editScale->setValidator(val1); 00071 00072 cbxImgType = new QComboBox(this); 00073 QStringList lst; 00074 lst<<tr("Colour")<<tr("Monochrome"); 00075 cbxImgType->addItems(lst); 00076 00077 cbRatio = new QCheckBox(tr("Original width to height ratio")); 00078 cbRatio->setChecked(true); 00079 connect(cbRatio,SIGNAL(toggled(bool)),this,SLOT(recalcRatio())); 00080 00081 cbResolution = new QCheckBox(tr("Original size")); 00082 connect(cbResolution,SIGNAL(toggled(bool)),editResolutionX,SLOT(setDisabled(bool))); 00083 connect(cbResolution,SIGNAL(toggled(bool)),editResolutionY,SLOT(setDisabled(bool))); 00084 connect(cbResolution,SIGNAL(toggled(bool)),cbRatio,SLOT(setDisabled(bool))); 00085 connect(cbResolution,SIGNAL(toggled(bool)),editScale,SLOT(setDisabled(bool))); 00086 connect(cbResolution,SIGNAL(toggled(bool)),this,SLOT(restoreOriginalWtoH())); 00087 cbResolution->setChecked(true); 00088 00089 connect(editResolutionX,SIGNAL(textEdited(QString)),this,SLOT(calcHeight())); 00090 connect(editResolutionY,SIGNAL(textEdited(QString)),this,SLOT(calcWidth())); 00091 connect(editScale,SIGNAL(textChanged(QString)),this,SLOT(recalcScale())); 00092 00093 cbSelected = new QCheckBox(tr("Export selected only")); 00094 connect(cbSelected,SIGNAL(toggled(bool)),this,SLOT(setSelectedWH())); 00095 cbSelected->setChecked(false); 00096 if (noselected) cbSelected->setDisabled(true); 00097 00098 //cbResolution->setEnabled(false); 00099 cbRatio->setEnabled(false); 00100 00101 00102 top = new QVBoxLayout; 00103 lower1 = new QHBoxLayout; 00104 lower2 = new QHBoxLayout; 00105 lower3 = new QHBoxLayout; 00106 lower4 = new QHBoxLayout; 00107 00108 top->addWidget(lblFilename); 00109 lower1->addWidget(editFilename); 00110 lower1->addWidget(SaveButt); 00111 top->addLayout(lower1); 00112 lower4->addWidget(lblFormat); 00113 lower4->addWidget(cbxImgType); 00114 top->addLayout(lower4); 00115 top->addWidget(cbResolution); 00116 //top->addWidget(cbRatio); 00117 lower3->addWidget(lblRatio); 00118 lower3->addWidget(editScale); 00119 top->addLayout(lower3); 00120 top->addWidget(lblResolutionX); 00121 top->addWidget(editResolutionX); 00122 top->addWidget(lblResolutionY); 00123 top->addWidget(editResolutionY); 00124 top->addWidget(cbSelected); 00125 00126 lower2->addWidget(ExportButt); 00127 lower2->addWidget(CancelButt); 00128 top->addLayout(lower2); 00129 this->setLayout(top); 00130 00131 this->layout()->setSizeConstraint(QLayout::SetFixedSize); 00132 this->setWindowTitle(tr("Export schematic to raster or vector image")); 00133 00134 this->setSvg(editFilename->text()); 00135 } 00136 00137 QString ExportDialog::FileToSave() 00138 { 00139 return editFilename->text(); 00140 } 00141 00142 bool ExportDialog::isOriginalSize() 00143 { 00144 return cbResolution->isChecked(); 00145 } 00146 00147 int ExportDialog::Xpixels() 00148 { 00149 return editResolutionX->text().toInt(); 00150 } 00151 00152 int ExportDialog::Ypixels() 00153 { 00154 return editResolutionY->text().toInt(); 00155 } 00156 00157 void ExportDialog::setFileName() 00158 { 00159 QString selectedFilter; 00160 QString currentExtension; 00161 QString filterExtension; 00162 00163 QString fileName = QFileDialog::getSaveFileName(this, tr("Export Schematic to Image"), 00164 editFilename->text(), 00165 "PNG images (*.png);;" 00166 "JPEG images (*.jpg *.jpeg);;" 00167 "SVG vector graphics (*.svg);;" 00168 "PDF (*.pdf);;" 00169 "PDF + LaTeX (*.pdf_tex);;" 00170 "EPS Encapsulated Postscript (*.eps)", 00171 &selectedFilter); 00172 00173 if (fileName.isEmpty()) return; 00174 00175 if (selectedFilter.contains("*.png", Qt::CaseInsensitive)) filterExtension = QString(".png"); 00176 if (selectedFilter.contains("*.jpg", Qt::CaseInsensitive)) filterExtension = QString(".jpg"); 00177 if (selectedFilter.contains("*.svg", Qt::CaseInsensitive)) filterExtension = QString(".svg"); 00178 if (selectedFilter.contains("*.pdf", Qt::CaseInsensitive)) filterExtension = QString(".pdf"); 00179 if (selectedFilter.contains("*.pdf_tex", Qt::CaseInsensitive)) filterExtension = QString(".pdf_tex"); 00180 if (selectedFilter.contains("*.eps", Qt::CaseInsensitive)) filterExtension = QString(".eps"); 00181 00182 QFileInfo fileInfo(fileName); 00183 00184 currentExtension = fileInfo.suffix(); 00185 00186 QString allowedExtensions = "png;jpg;jpeg;svg;pdf;pdf_tex;eps"; 00187 QStringList extensionsList = allowedExtensions.split (';'); 00188 00189 if (currentExtension.isEmpty()) { 00190 fileName.append(filterExtension); 00191 } else { 00192 if (!extensionsList.contains(currentExtension)) { 00193 fileName.append(filterExtension); 00194 } 00195 } 00196 00197 editFilename->setText(fileName); 00198 } 00199 00200 void ExportDialog::calcWidth() 00201 { 00202 if (cbRatio->isChecked()) { 00203 float h = editResolutionY->text().toFloat(); 00204 float w = round((h*dwidth)/dheight); 00205 editResolutionX->setText(QString::number(w)); 00206 } 00207 } 00208 00209 00210 void ExportDialog::calcHeight() 00211 { 00212 if (cbRatio->isChecked()) { 00213 float w = editResolutionX->text().toFloat(); 00214 float h = round((w*dheight)/dwidth); 00215 editResolutionY->setText(QString::number(h)); 00216 } 00217 00218 } 00219 00220 void ExportDialog::recalcRatio() 00221 { 00222 if (cbRatio->isChecked()) { 00223 calcHeight(); 00224 } 00225 } 00226 00227 void ExportDialog::restoreOriginalWtoH() 00228 { 00229 if (cbResolution->isChecked()) { 00230 editScale->setText(QString::number(1.0)); 00231 editResolutionX->setText(QString::number(dwidth)); 00232 editResolutionY->setText(QString::number(dheight)); 00233 } 00234 } 00235 00236 bool ExportDialog::isSvg() 00237 { 00238 return svg; 00239 } 00240 00241 bool ExportDialog::isExportSelected() 00242 { 00243 return cbSelected->isChecked(); 00244 } 00245 00246 void ExportDialog::setSvg(QString filename) 00247 { 00248 QFileInfo graphics_file(filename); 00249 QString ext = graphics_file.suffix(); 00250 ext = ext.toLower(); 00251 if ((ext=="svg")||(ext=="pdf")||(ext=="eps")||(ext=="pdf_tex")) { 00252 svg = true; 00253 cbResolution->setChecked(true); 00254 cbResolution->setDisabled(true); 00255 cbxImgType->setDisabled(true); 00256 cbRatio->setChecked(true); 00257 } else { 00258 svg = false; 00259 cbResolution->setEnabled(true); 00260 cbxImgType->setEnabled(true); 00261 } 00262 } 00263 00264 bool ExportDialog::isValidFilename() 00265 { 00266 QString nam = editFilename->text(); 00267 QStringList filetypes; 00268 QFileInfo inf(nam); 00269 filetypes<<"png"<<"svg"<<"jpeg"<<"jpg"<<"pdf"<<"pdf_tex"<<"eps" 00270 <<"PNG"<<"JPG"<<"SVG"<<"JPEG"<<"PDF" 00271 <<"PDF_TEX"<<"EPS"; 00272 00273 if (filetypes.contains(inf.suffix())) { 00274 return true; 00275 } else { 00276 return false; 00277 } 00278 } 00279 00280 bool ExportDialog::needsInkscape() 00281 { 00282 QString nam = editFilename->text(); 00283 QStringList filetypes; 00284 QFileInfo inf(nam); 00285 filetypes<<"pdf"<<"pdf_tex"<<"eps"<<"PDF"<<"PDF_TEX"<<"EPS"; 00286 00287 if (filetypes.contains(inf.suffix())) { 00288 return true; 00289 } else { 00290 return false; 00291 } 00292 } 00293 00294 bool ExportDialog::isPdf() 00295 { 00296 QFileInfo inf(editFilename->text()); 00297 if (inf.suffix().toLower()=="pdf") return true; 00298 else return false; 00299 } 00300 00301 bool ExportDialog::isPdf_Tex() 00302 { 00303 QFileInfo inf(editFilename->text()); 00304 if (inf.suffix().toLower()=="pdf_tex") return true; 00305 else return false; 00306 } 00307 00308 bool ExportDialog::isEps() 00309 { 00310 QFileInfo inf(editFilename->text()); 00311 if (inf.suffix().toLower()=="eps") return true; 00312 else return false; 00313 } 00314 00315 void ExportDialog::setSelectedWH() 00316 { 00317 if (cbSelected->isChecked()) { 00318 editResolutionX->setText(QString::number(dwidthsel)); 00319 editResolutionY->setText(QString::number(dheightsel)); 00320 } else { 00321 editResolutionX->setText(QString::number(dwidth)); 00322 editResolutionY->setText(QString::number(dheight)); 00323 } 00324 recalcScale(); 00325 } 00326 00327 void ExportDialog::setDiagram() 00328 { 00329 cbSelected->setChecked(true); 00330 cbSelected->setDisabled(true); 00331 this->setWindowTitle(tr("Export diagram to raster or vector image")); 00332 } 00333 00334 float ExportDialog::getScale() 00335 { 00336 scale = editScale->text().toFloat(); 00337 return scale; 00338 } 00339 00340 void ExportDialog::recalcScale() 00341 { 00342 scale = editScale->text().toFloat(); 00343 if (cbSelected->isChecked()) { 00344 editResolutionX->setText(QString::number(scale*dwidthsel)); 00345 editResolutionY->setText(QString::number(scale*dheightsel)); 00346 } else { 00347 editResolutionX->setText(QString::number(scale*dwidth)); 00348 editResolutionY->setText(QString::number(scale*dheight)); 00349 } 00350 00351 } 00352 00353 ExportDialog::ImgFormat ExportDialog::getImgFormat() 00354 { 00355 // default 00356 ExportDialog::ImgFormat ImgFormat = ExportDialog::Coloured; 00357 00358 switch (cbxImgType->currentIndex()) { 00359 case 0 : 00360 ImgFormat = ExportDialog::Coloured; 00361 break; 00362 case 1 : 00363 ImgFormat = ExportDialog::Monochrome; 00364 break; 00365 default : break; 00366 } 00367 00368 return ImgFormat; 00369 }