Qucs-GUI  0.0.19
/home/travis/build/Qucs/qucs/qucs/qucs/printerwriter.cpp
Go to the documentation of this file.
00001 /*
00002  * printerwriter.cpp - implementation of writer to printer
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 "printerwriter.h"
00024 #include "schematic.h"
00025 #include "textdoc.h"
00026 
00027 #include <QPrinter>
00028 #include <QPainter>
00029 #include <QPrintDialog>
00030 
00031 PrinterWriter::PrinterWriter()
00032 {
00033   //default setting
00034   Printer = new QPrinter(QPrinter::HighResolution);
00035   Printer->setOptionEnabled(QPrinter::PrintSelection, true);
00036   Printer->setOptionEnabled(QPrinter::PrintPageRange, false);
00037   Printer->setOptionEnabled(QPrinter::PrintToFile, true);
00038 
00039   Printer->setPaperSize(QPrinter::A4);
00040   Printer->setColorMode(QPrinter::Color);
00041   Printer->setFullPage(true);
00042 
00043   fitToPage = false;
00044 }
00045 
00046 PrinterWriter::~PrinterWriter()
00047 {
00048   delete Printer;
00049 }
00050 
00051 //allow user pass parameter and print document
00052 void
00053 PrinterWriter::noGuiPrint(QWidget *doc, QString printFile,
00054     QString page, int dpi, QString color, QString orientation)
00055 {
00056   //set property
00057   Printer->setOutputFileName(printFile);
00058 
00059   //page size
00060   if (page == "A3") {
00061     Printer->setPaperSize(QPrinter::A3);
00062   } else if (page == "B4") {
00063     Printer->setPaperSize(QPrinter::B4);
00064   } else if (page == "B5") {
00065     Printer->setPaperSize(QPrinter::B5);
00066   } else {
00067     Printer->setPaperSize(QPrinter::A4);
00068   }
00069   //dpi
00070   Printer->setResolution(dpi);
00071   //color
00072   if (color == "BW") {
00073     Printer->setColorMode(QPrinter::GrayScale);
00074   } else {
00075     Printer->setColorMode(QPrinter::Color);
00076   }
00077   //orientation
00078   if (orientation == "landscape") {
00079     Printer->setOrientation(QPrinter::Landscape);
00080   } else {
00081     Printer->setOrientation(QPrinter::Portrait);
00082   }
00083   QPainter Painter(Printer);
00084   if(!Painter.device()) {      // valid device available ?
00085     return;
00086   }
00087 
00088   static_cast<Schematic *>(doc)->print(Printer, &Painter,
00089     Printer->printRange() == QPrinter::AllPages, fitToPage);
00090 }
00091 
00092 void
00093 PrinterWriter::print(QWidget *doc)
00094 {
00095   QPrintDialog *dialog = new QPrintDialog(Printer, 0);
00096   dialog->setWindowTitle(QObject::tr("Print Document"));
00097   dialog->addEnabledOption(QAbstractPrintDialog::PrintSelection);
00098 
00099   if (doc->inherits("QPlainTextEdit"))
00100   {
00101     if (dialog->exec() == QDialog::Accepted) {
00102        static_cast<QPlainTextEdit *>(doc)->print(Printer);
00103     }
00104   }
00105   else {
00106     Printer->setOrientation(QPrinter::Landscape);
00107 
00108     if (dialog->exec() == QDialog::Accepted)
00109     {
00110       QPainter Painter(Printer);
00111       if(!Painter.device()) {     // valid device available ?
00112         delete dialog;
00113         return;
00114       }
00115       for (int z = Printer->numCopies(); z > 0; --z) {
00116         if (Printer->aborted()) {
00117           break;
00118         }
00119 
00120         static_cast<Schematic *>(doc)->print(Printer, &Painter,
00121                 Printer->printRange() == QPrinter::AllPages, fitToPage);
00122         if (z > 1 && !Printer->newPage()) {
00123           delete dialog;
00124           return;
00125         }
00126       }
00127     }
00128   }
00129   delete dialog;
00130 }
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines