Make TX volatge for simu more flexible (#7124)
[opentx.git] / companion / src / printdialog.cpp
blobdecd83ae4245db1568fe025eac22309622e25226
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 "styleeditdialog.h"
25 #include <QPrinter>
26 #include <QPrintDialog>
28 PrintDialog::PrintDialog(QWidget *parent, Firmware * firmware, GeneralSettings & generalSettings, ModelData & model, const QString & filename) :
29 QDialog(parent),
30 firmware(firmware),
31 generalSettings(generalSettings),
32 model(model),
33 printfilename(filename),
34 ui(new Ui::PrintDialog),
35 multiModelPrinter(firmware)
37 ui->setupUi(this);
38 setWindowIcon(CompanionIcon("print.png"));
39 setWindowTitle(model.name);
40 multiModelPrinter.setModel(0, &model, &generalSettings);
41 ui->textEdit->setHtml(multiModelPrinter.print(ui->textEdit->document()));
42 if (!printfilename.isEmpty()) {
43 printToFile();
44 QTimer::singleShot(0, this, SLOT(autoClose()));
48 void PrintDialog::closeEvent(QCloseEvent *event)
52 PrintDialog::~PrintDialog()
54 delete ui;
57 void PrintDialog::on_printButton_clicked()
59 QPrinter printer;
60 printer.setPageMargins(10.0,10.0,10.0,10.0,printer.Millimeter);
61 QPrintDialog *dialog = new QPrintDialog(&printer, this);
62 dialog->setWindowTitle(tr("Print Document"));
63 if (dialog->exec() != QDialog::Accepted)
64 return;
65 ui->textEdit->print(&printer);
68 void PrintDialog::on_printFileButton_clicked()
70 QString fn = QFileDialog::getSaveFileName(this,tr("Select PDF output file"),QString(),tr("ODF files (*.odt);;PDF Files(*.pdf);;HTML-Files (*.htm *.html);;All Files (*)"));
71 if (fn.isEmpty())
72 return;
73 if (! (fn.endsWith(".odt", Qt::CaseInsensitive) || fn.endsWith(".pdf", Qt::CaseInsensitive) || fn.endsWith(".htm", Qt::CaseInsensitive) || fn.endsWith(".html", Qt::CaseInsensitive)) )
74 fn += ".pdf"; // default
75 if (fn.endsWith(".pdf", Qt::CaseInsensitive)) {
76 QPrinter printer;
77 printer.setPageMargins(10.0,10.0,10.0,10.0,printer.Millimeter);
78 printer.setOutputFormat(QPrinter::PdfFormat);
79 printer.setColorMode(QPrinter::Color);
80 printer.setOutputFileName(fn);
81 ui->textEdit->print(&printer);
83 else {
84 QTextDocumentWriter writer(fn);
85 writer.write(ui->textEdit->document());
89 void PrintDialog::printToFile()
91 if (printfilename.isEmpty())
92 return;
93 if (! (printfilename.endsWith(".odt", Qt::CaseInsensitive) || printfilename.endsWith(".pdf", Qt::CaseInsensitive) || printfilename.endsWith(".htm", Qt::CaseInsensitive) || printfilename.endsWith(".html", Qt::CaseInsensitive)) )
94 printfilename += ".pdf"; // default
95 if (printfilename.endsWith(".pdf", Qt::CaseInsensitive)) {
96 QPrinter printer;
97 printer.setPageMargins(10.0,10.0,10.0,10.0,printer.Millimeter);
98 printer.setOutputFormat(QPrinter::PdfFormat);
99 printer.setColorMode(QPrinter::Color);
100 printer.setOutputFileName(printfilename);
101 ui->textEdit->print(&printer);
103 else {
104 QTextDocumentWriter writer(printfilename);
105 writer.write(ui->textEdit->document());
109 void PrintDialog::autoClose()
111 this->close();
114 void PrintDialog::on_styleButton_clicked()
116 StyleEditDialog *g = new StyleEditDialog(this, MODEL_PRINT_CSS);
117 if (g->exec() == QDialog::Accepted)
118 ui->textEdit->setHtml(multiModelPrinter.print(ui->textEdit->document()));