Projectkk2glider/issue 4086 usb and serial (#4097)
[opentx.git] / companion / src / printdialog.cpp
blob8d846f1b882270981f1d8c9a67097714d018e7c1
1 /*
2 * Copyright (C) OpenTX
4 * Based on code named
5 * th9x - http://code.google.com/p/th9x
6 * er9x - http://code.google.com/p/er9x
7 * gruvin9x - http://code.google.com/p/gruvin9x
9 * License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 as
13 * published by the Free Software Foundation.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
21 #include "printdialog.h"
22 #include "ui_printdialog.h"
23 #include "helpers.h"
24 #include <QPrinter>
25 #include <QPrintDialog>
27 // #if !defined WIN32 && defined __GNUC__
28 // #include <unistd.h>
29 // #endif
31 PrintDialog::PrintDialog(QWidget *parent, Firmware * firmware, GeneralSettings & generalSettings, ModelData & model, const QString & filename) :
32 QDialog(parent, Qt::WindowTitleHint | Qt::WindowSystemMenuHint),
33 firmware(firmware),
34 generalSettings(generalSettings),
35 model(model),
36 printfilename(filename),
37 ui(new Ui::PrintDialog),
38 multimodelprinter(firmware)
40 ui->setupUi(this);
41 setWindowIcon(CompanionIcon("print.png"));
42 setWindowTitle(model.name);
43 multimodelprinter.setModel(0, model);
44 ui->textEdit->setHtml(multimodelprinter.print(ui->textEdit->document()));
45 if (!printfilename.isEmpty()) {
46 printToFile();
47 QTimer::singleShot(0, this, SLOT(autoClose()));
51 void PrintDialog::closeEvent(QCloseEvent *event)
55 PrintDialog::~PrintDialog()
57 delete ui;
60 void PrintDialog::on_printButton_clicked()
62 QPrinter printer;
63 printer.setPageMargins(10.0,10.0,10.0,10.0,printer.Millimeter);
64 QPrintDialog *dialog = new QPrintDialog(&printer, this);
65 dialog->setWindowTitle(tr("Print Document"));
66 if (dialog->exec() != QDialog::Accepted)
67 return;
68 ui->textEdit->print(&printer);
71 void PrintDialog::on_printFileButton_clicked()
73 QString fn = QFileDialog::getSaveFileName(this,tr("Select PDF output file"),QString(),tr("ODF files (*.odt);;PDF Files(*.pdf);;HTML-Files (*.htm *.html);;All Files (*)"));
74 if (fn.isEmpty())
75 return;
76 if (! (fn.endsWith(".odt", Qt::CaseInsensitive) || fn.endsWith(".pdf", Qt::CaseInsensitive) || fn.endsWith(".htm", Qt::CaseInsensitive) || fn.endsWith(".html", Qt::CaseInsensitive)) )
77 fn += ".pdf"; // default
78 if (fn.endsWith(".pdf", Qt::CaseInsensitive)) {
79 QPrinter printer;
80 printer.setPageMargins(10.0,10.0,10.0,10.0,printer.Millimeter);
81 printer.setOutputFormat(QPrinter::PdfFormat);
82 printer.setColorMode(QPrinter::Color);
83 printer.setOutputFileName(fn);
84 ui->textEdit->print(&printer);
86 else {
87 QTextDocumentWriter writer(fn);
88 writer.write(ui->textEdit->document());
92 void PrintDialog::printToFile()
94 if (printfilename.isEmpty())
95 return;
96 if (! (printfilename.endsWith(".odt", Qt::CaseInsensitive) || printfilename.endsWith(".pdf", Qt::CaseInsensitive) || printfilename.endsWith(".htm", Qt::CaseInsensitive) || printfilename.endsWith(".html", Qt::CaseInsensitive)) )
97 printfilename += ".pdf"; // default
98 if (printfilename.endsWith(".pdf", Qt::CaseInsensitive)) {
99 QPrinter printer;
100 printer.setPageMargins(10.0,10.0,10.0,10.0,printer.Millimeter);
101 printer.setOutputFormat(QPrinter::PdfFormat);
102 printer.setColorMode(QPrinter::Color);
103 printer.setOutputFileName(printfilename);
104 ui->textEdit->print(&printer);
106 else {
107 QTextDocumentWriter writer(printfilename);
108 writer.write(ui->textEdit->document());
112 void PrintDialog::autoClose()
114 this->close();