Fix doc path
[opentx.git] / companion / src / comparedialog.cpp
blob4897f74ba0e262abf634a13eaed29851b3a72222
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 "comparedialog.h"
22 #include "ui_comparedialog.h"
23 #include "appdata.h"
24 #include "helpers.h"
25 #include "modelslist.h"
26 #include "styleeditdialog.h"
27 #include <QPrinter>
28 #include <QPrintDialog>
30 //class DragDropHeader {
31 // public:
32 // DragDropHeader():
33 // general_settings(false),
34 // models_count(0)
35 // {
36 // }
37 // bool general_settings;
38 // uint8_t models_count;
39 // uint8_t models[CPN_MAX_MODELS];
40 //};
42 CompareDialog::CompareDialog(QWidget * parent, Firmware * firmware):
43 QDialog(parent, Qt::Window),
44 multimodelprinter(new MultiModelPrinter(firmware)),
45 ui(new Ui::CompareDialog)
47 ui->setupUi(this);
48 setWindowIcon(CompanionIcon("compare.png"));
49 setAcceptDrops(true);
50 if (!g.compareWinGeo().isEmpty()) {
51 restoreGeometry(g.compareWinGeo());
55 CompareDialog::~CompareDialog()
57 delete multimodelprinter;
58 delete ui;
61 void CompareDialog::dragMoveEvent(QDragMoveEvent * event)
63 if (event->mimeData()->hasFormat("application/x-companion-modeldata")) {
64 event->acceptProposedAction();
66 else {
67 event->ignore();
71 void CompareDialog::dragEnterEvent(QDragEnterEvent * event)
73 if (event->mimeData()->hasFormat("application/x-companion-modeldata")) {
74 event->acceptProposedAction();
76 else {
77 event->ignore();
81 void CompareDialog::dragLeaveEvent(QDragLeaveEvent * event)
83 event->accept();
86 bool CompareDialog::handleMimeData(const QMimeData * mimeData)
88 QVector<ModelData> mList;
89 GeneralSettings gs;
90 if (!TreeModel::decodeMimeData(mimeData, &mList, &gs) || mList.isEmpty())
91 return false;
92 for (int i=0; i < mList.size(); ++i) {
93 GMData data;
94 data.model = mList[i];
95 data.gs = gs;
96 modelsList.append(data);
98 return true;
101 void CompareDialog::closeEvent(QCloseEvent * event)
103 g.compareWinGeo(saveGeometry());
106 void CompareDialog::dropEvent(QDropEvent *event)
108 if (handleMimeData(event->mimeData())) {
109 event->accept();
110 compare();
114 void CompareDialog::compare()
116 QLayoutItem *child;
117 while ((child = ui->layout_modelNames->takeAt(0))) {
118 if (child->widget())
119 delete child->widget();
120 delete child;
123 multimodelprinter->clearModels();
124 ui->textEdit->clear();
126 for (int i=0; i < modelsList.size(); ++i) {
127 multimodelprinter->setModel(i, &modelsList[i].model, &modelsList[i].gs);
128 QString name(modelsList.at(i).model.name);
129 if (name.isEmpty())
130 name = tr("Unnamed Model %1").arg(i+1);
132 QWidget * hdr = new QWidget(this);
133 hdr->setLayout(new QHBoxLayout());
134 hdr->layout()->setContentsMargins(0, 0, 0, 0);
135 hdr->layout()->setSpacing(2);
136 QToolButton * btn = new QToolButton(hdr);
137 btn->setIcon(CompanionIcon("clear.png"));
138 btn->setProperty("index", i);
139 btn->setFixedSize(18, 18);
140 btn->setToolTip(tr("Click to remove this model."));
141 hdr->layout()->addWidget(btn);
142 QLabel * lbl = new QLabel(name, this);
143 lbl->setStyleSheet("font-weight: bold;");
144 hdr->layout()->addWidget(lbl);
145 connect(btn, &QToolButton::clicked, this, &CompareDialog::removeModelBtnClicked);
147 ui->layout_modelNames->addWidget(hdr);
149 if (modelsList.size())
150 ui->textEdit->setHtml(multimodelprinter->print(ui->textEdit->document()));
153 void CompareDialog::removeModel(int idx)
155 if (idx < modelsList.size()) {
156 modelsList.remove(idx);
157 compare();
161 void CompareDialog::removeModelBtnClicked()
163 if (sender() && sender()->property("index").isValid())
164 removeModel(sender()->property("index").toInt());
167 void CompareDialog::on_printButton_clicked()
169 QPrinter printer;
170 printer.setPageMargins(10.0, 10.0, 10.0, 10.0, printer.Millimeter);
171 QPrintDialog * dialog = new QPrintDialog(&printer, this);
172 dialog->setWindowTitle(tr("Print Document"));
173 if (dialog->exec() != QDialog::Accepted)
174 return;
175 ui->textEdit->print(&printer);
178 void CompareDialog::on_printFileButton_clicked()
180 QPrinter printer;
181 QString filename = QFileDialog::getSaveFileName(this, tr("Select PDF output file"), QString(), "Pdf File(*.pdf)");
182 printer.setPageMargins(10.0, 10.0, 10.0, 10.0, printer.Millimeter);
183 printer.setOutputFormat(QPrinter::PdfFormat);
184 printer.setOrientation(QPrinter::Landscape);
185 printer.setColorMode(QPrinter::Color);
186 if (!filename.isEmpty()) {
187 if (QFileInfo(filename).suffix().isEmpty())
188 filename.append(".pdf");
189 printer.setOutputFileName(filename);
190 ui->textEdit->print(&printer);
194 void CompareDialog::on_styleButton_clicked()
196 StyleEditDialog *g = new StyleEditDialog(this, MODEL_PRINT_CSS);
197 if (g->exec() == QDialog::Accepted)
198 compare();