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"
25 #include "modelslist.h"
26 #include "styleeditdialog.h"
28 #include <QPrintDialog>
30 //class DragDropHeader {
33 // general_settings(false),
37 // bool general_settings;
38 // uint8_t models_count;
39 // uint8_t models[CPN_MAX_MODELS];
42 CompareDialog::CompareDialog(QWidget
* parent
, Firmware
* firmware
):
43 QDialog(parent
, Qt::Window
),
44 multimodelprinter(new MultiModelPrinter(firmware
)),
45 ui(new Ui::CompareDialog
)
48 setWindowIcon(CompanionIcon("compare.png"));
50 if (!g
.compareWinGeo().isEmpty()) {
51 restoreGeometry(g
.compareWinGeo());
55 CompareDialog::~CompareDialog()
57 delete multimodelprinter
;
61 void CompareDialog::dragMoveEvent(QDragMoveEvent
* event
)
63 if (event
->mimeData()->hasFormat("application/x-companion-modeldata")) {
64 event
->acceptProposedAction();
71 void CompareDialog::dragEnterEvent(QDragEnterEvent
* event
)
73 if (event
->mimeData()->hasFormat("application/x-companion-modeldata")) {
74 event
->acceptProposedAction();
81 void CompareDialog::dragLeaveEvent(QDragLeaveEvent
* event
)
86 bool CompareDialog::handleMimeData(const QMimeData
* mimeData
)
88 QVector
<ModelData
> mList
;
90 if (!TreeModel::decodeMimeData(mimeData
, &mList
, &gs
) || mList
.isEmpty())
92 for (int i
=0; i
< mList
.size(); ++i
) {
94 data
.model
= mList
[i
];
96 modelsList
.append(data
);
101 void CompareDialog::closeEvent(QCloseEvent
* event
)
103 g
.compareWinGeo(saveGeometry());
106 void CompareDialog::dropEvent(QDropEvent
*event
)
108 if (handleMimeData(event
->mimeData())) {
114 void CompareDialog::compare()
117 while ((child
= ui
->layout_modelNames
->takeAt(0))) {
119 delete child
->widget();
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
);
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
);
161 void CompareDialog::removeModelBtnClicked()
163 if (sender() && sender()->property("index").isValid())
164 removeModel(sender()->property("index").toInt());
167 void CompareDialog::on_printButton_clicked()
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
)
175 ui
->textEdit
->print(&printer
);
178 void CompareDialog::on_printFileButton_clicked()
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
)