Qucs-GUI  0.0.19
/home/travis/build/Qucs/qucs/qucs/qucs/imagewriter.cpp
Go to the documentation of this file.
00001 /*
00002  * imagewriter.cpp - implementation of writer to image
00003  *
00004  * Copyright (C) 2014, Yodalee, lc85301@gmail.com
00005  *
00006  * This file is part of Qucs
00007  *
00008  * Qucs is free software; you can redistribute it and/or modify
00009  * it under the terms of the GNU General Public License as published by
00010  * the Free Software Foundation; either version 2, or (at your option)
00011  * any later version.
00012  *
00013  * This software is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016  * GNU General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU General Public License
00019  * along with Qucs.  If not, see <http://www.gnu.org/licenses/>.
00020  *
00021  */
00022 
00023 #include "schematic.h"
00024 #include "imagewriter.h"
00025 #include "dialogs/exportdialog.h"
00026 
00027 #include <QtSvg>
00028 
00029 
00030 ImageWriter::ImageWriter(QString lastfile)
00031 {
00032   onlyDiagram = false;
00033   lastExportFilename = lastfile;
00034 }
00035 
00036 ImageWriter::~ImageWriter()
00037 {
00038 }
00039 
00040 void
00041 ImageWriter::noGuiPrint(QWidget *doc, QString printFile, QString color)
00042 {
00043   Schematic *sch = static_cast<Schematic*>(doc);
00044   const int bourder = 30;
00045   int w,h,wsel,hsel,
00046       xmin, ymin, xmin_sel, ymin_sel;
00047   getSchWidthAndHeight(sch, w,h,xmin,ymin);
00048   sch->getSelAreaWidthAndHeight(wsel, hsel, xmin_sel, ymin_sel);
00049   w += bourder;
00050   h += bourder;
00051   wsel += bourder;
00052   hsel += bourder;
00053   float scal = 1.0;
00054 
00055   if (printFile.endsWith(".svg") || printFile.endsWith(".eps")) {
00056     QSvgGenerator* svg1 = new QSvgGenerator();
00057 
00058     QString tempfile = printFile + ".tmp.svg";
00059     if (!printFile.endsWith(".svg")) {
00060         svg1->setFileName(tempfile);
00061     } else {
00062         svg1->setFileName(printFile);
00063     }
00064 
00065     svg1->setSize(QSize(1.12*w, h));
00066     QPainter *p = new QPainter(svg1);
00067     p->fillRect(0, 0, svg1->size().width(), svg1->size().height(), Qt::white);
00068     ViewPainter *vp = new ViewPainter(p);
00069     vp->init(p, 1.0, 0, 0, xmin-bourder/2, ymin-bourder/2, 1.0, 1.0);
00070 
00071     sch->paintSchToViewpainter(vp,true,true);
00072 
00073     delete vp;
00074     delete p;
00075     delete svg1;
00076 
00077     if (!printFile.endsWith(".svg")) {
00078         QString cmd = "inkscape -z -D --file=";
00079         cmd += tempfile + " ";
00080 
00081         if (printFile.endsWith(".eps")) {
00082           cmd += "--export-eps=" + printFile;
00083         }
00084 
00085         int result = QProcess::execute(cmd);
00086 
00087         if (result!=0) {
00088             QMessageBox* msg =  new QMessageBox(QMessageBox::Critical,"Export to image", "Inkscape start error!", QMessageBox::Ok);
00089             msg->exec();
00090             delete msg;
00091         }
00092         QFile::remove(tempfile);
00093     }
00094 
00095   } else if (printFile.endsWith(".png")) {
00096     QImage* img;
00097     if (color == "BW") {
00098       img = new QImage(w, h, QImage::Format_Mono);
00099     } else {
00100       img = new QImage(w, h, QImage::Format_RGB888);
00101     }
00102 
00103     QPainter* p = new QPainter(img);
00104     p->fillRect(0, 0, w, h, Qt::white);
00105     ViewPainter* vp = new ViewPainter(p);
00106     vp->init(p, scal, 0, 0, xmin*scal-bourder/2, ymin*scal-bourder/2, scal,scal);
00107 
00108     sch->paintSchToViewpainter(vp,true,true);
00109 
00110     img->save(printFile);
00111 
00112     delete vp;
00113     delete p;
00114     delete img;
00115   } else {
00116     fprintf(stderr, "Unsupported format of output file. \n"
00117         "Use PNG, SVG or PDF format!\n");
00118     return;
00119   }
00120 }
00121 
00122 QString ImageWriter::getLastSavedFile()
00123 {
00124     return lastExportFilename;
00125 }
00126 
00127 // FIXME: should check if filename exists and not silently overwrite
00128 int ImageWriter::print(QWidget *doc)
00129 {
00130   Schematic *sch = static_cast<Schematic*>(doc);
00131   const int border = 30;
00132 
00133   int w,h,wsel,hsel,
00134       xmin, ymin, xmin_sel, ymin_sel;
00135   int status = -1;
00136 
00137   getSchWidthAndHeight(sch, w, h, xmin, ymin);
00138   sch->getSelAreaWidthAndHeight(wsel, hsel, xmin_sel, ymin_sel);
00139   w += border;
00140   h += border;
00141   wsel += border;
00142   hsel += border;
00143 
00144   bool noselect = false;
00145   if ((wsel == (border+1)) && (hsel == (border+1))) {
00146     noselect = true;
00147   }
00148 
00149   ExportDialog *dlg = new ExportDialog(
00150       w, h, wsel, hsel, lastExportFilename, noselect, 0);
00151   
00152   if (onlyDiagram) {
00153     dlg->setDiagram();
00154   }
00155 
00156   if (dlg->exec()) {
00157     QString filename = dlg->FileToSave();
00158     lastExportFilename = filename;
00159 
00160     bool exportAll;
00161     if (dlg->isExportSelected()) {
00162       exportAll = false;
00163       w = wsel;
00164       h = hsel;
00165       xmin = xmin_sel;
00166       ymin = ymin_sel;
00167     } else {
00168       exportAll = true;
00169     }
00170 
00171     float scal;
00172     if (dlg->isOriginalSize()) {
00173       scal = 1.0;
00174     } else {
00175       scal = (float) dlg->Xpixels()/w;
00176       w = round(w*scal);
00177       h = round(h*scal);
00178     }
00179 
00180     if (dlg->isValidFilename()) {
00181       if (!dlg->isSvg()) {
00182         QImage* img;
00183 
00184         switch (dlg->getImgFormat()) {
00185           case ExportDialog::Coloured : 
00186             img = new QImage(w,h,QImage::Format_RGB888);
00187             break;
00188           case ExportDialog::Monochrome : 
00189             img = new QImage(w,h,QImage::Format_Mono);
00190             break;
00191           default : 
00192             break;
00193         }
00194 
00195         QPainter* p = new QPainter(img);
00196         p->fillRect(0, 0, w, h, Qt::white);
00197         ViewPainter* vp = new ViewPainter(p);
00198         vp->init(p, scal, 0, 0, 
00199             xmin*scal-border/2, ymin*scal-border/2, scal, scal);
00200 
00201         sch->paintSchToViewpainter(vp, exportAll, true);
00202 
00203         img->save(filename);
00204 
00205         delete vp;
00206         delete p;
00207         delete img;
00208       } 
00209       else {
00210         QSvgGenerator* svgwriter = new QSvgGenerator();
00211 
00212         if (dlg->needsInkscape()) {
00213           svgwriter->setFileName(filename+".tmp.svg");
00214         } else {
00215           svgwriter->setFileName(filename);
00216         }
00217 
00218         //svgwriter->setSize(QSize(1.12*w,1.1*h));
00219         svgwriter->setSize(QSize(1.12*w,h));
00220         QPainter *p = new QPainter(svgwriter);
00221         p->fillRect(0, 0, svgwriter->size().width(), svgwriter->size().height(), Qt::white);
00222 
00223         ViewPainter *vp = new ViewPainter(p);
00224         vp->init(p, 1.0, 0, 0, xmin-border/2, ymin-border/2, 1.0, 1.0);
00225         sch->paintSchToViewpainter(vp,exportAll,true);
00226 
00227         delete vp;
00228         delete p;
00229         delete svgwriter;
00230 
00231         if (dlg->needsInkscape()) {
00232             QString cmd = "inkscape -z -D --file=";
00233             cmd += filename+".tmp.svg ";
00234 
00235             if (dlg->isPdf_Tex()) {
00236                 QString tmp = filename;
00237                 tmp.chop(4);
00238                 cmd = cmd + "--export-pdf="+ tmp + " --export-latex";
00239             }
00240 
00241             if (dlg->isPdf()) {
00242                 cmd = cmd + "--export-pdf=" + filename;
00243             }
00244 
00245             if (dlg->isEps()) {
00246                 cmd = cmd + "--export-eps=" + filename;
00247             }
00248 
00249             int result = QProcess::execute(cmd);
00250 
00251             if (result!=0) {
00252                 QMessageBox::critical(0, QObject::tr("Export to image"), 
00253                     QObject::tr("Inkscape start error!"), QMessageBox::Ok);
00254             }
00255             QFile::remove(filename+".tmp.svg");
00256         }
00257       }
00258 
00259       if (QFile::exists(filename)) {
00260         //QMessageBox::information(0, QObject::tr("Export to image"),
00261         //    QObject::tr("Successfully exported"), QMessageBox::Ok);
00262   status = 0;
00263       } 
00264       else {
00265         QMessageBox::information(0, QObject::tr("Export to image"),
00266             QObject::tr("Disk write error!"), QMessageBox::Ok);
00267   status = -1;
00268       }
00269     } else {
00270         QMessageBox::critical(0, QObject::tr("Export to image"), 
00271             QObject::tr("Unsupported format of graphics file. \n"
00272             "Use PNG, JPEG or SVG graphics!"), QMessageBox::Ok);
00273   status = -1;
00274     }
00275   }
00276   delete dlg;
00277   return status;
00278 }
00279 
00280 void ImageWriter::getSchWidthAndHeight(Schematic *sch, int &w, int &h, int &xmin, int &ymin)
00281 {
00282     xmin = sch->UsedX1;
00283     ymin = sch->UsedY1;
00284     w = abs(sch->UsedX2 - xmin);
00285     h = abs(sch->UsedY2 - ymin);
00286 
00287     int f_w, f_h;
00288     if (sch->sizeOfFrame(f_w,f_h)) {
00289         xmin = std::min(0,xmin); // For components
00290         ymin = std::min(0,ymin); // that fall out of frame
00291         w = abs(std::max(f_w,sch->UsedX2) - xmin);
00292         h = abs(std::max(f_h,sch->UsedY2) - ymin);
00293         //w = std::max(w,f_w);
00294         //h = std::max(h,f_h);
00295     }
00296 }
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines