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 CompareDialog::CompareDialog(QWidget
* parent
, Firmware
* firmware
):
31 QDialog(parent
, Qt::Window
),
32 multimodelprinter(new MultiModelPrinter(firmware
)),
33 ui(new Ui::CompareDialog
)
36 setWindowIcon(CompanionIcon("compare.png"));
38 if (!g
.compareWinGeo().isEmpty()) {
39 restoreGeometry(g
.compareWinGeo());
43 CompareDialog::~CompareDialog()
45 delete multimodelprinter
;
49 void CompareDialog::dragMoveEvent(QDragMoveEvent
* event
)
51 if (event
->mimeData()->hasFormat("application/x-companion-modeldata")) {
52 event
->acceptProposedAction();
59 void CompareDialog::dragEnterEvent(QDragEnterEvent
* event
)
61 if (event
->mimeData()->hasFormat("application/x-companion-modeldata")) {
62 event
->acceptProposedAction();
69 void CompareDialog::dragLeaveEvent(QDragLeaveEvent
* event
)
74 bool CompareDialog::handleMimeData(const QMimeData
* mimeData
)
76 QVector
<ModelData
> mList
;
78 if (!TreeModel::decodeMimeData(mimeData
, &mList
, &gs
) || mList
.isEmpty())
80 for (int i
=0; i
< mList
.size(); ++i
) {
82 data
.model
= mList
[i
];
84 modelsList
.append(data
);
89 void CompareDialog::closeEvent(QCloseEvent
* event
)
91 g
.compareWinGeo(saveGeometry());
94 void CompareDialog::dropEvent(QDropEvent
*event
)
96 if (handleMimeData(event
->mimeData())) {
102 void CompareDialog::compare()
105 while ((child
= ui
->layout_modelNames
->takeAt(0))) {
107 delete child
->widget();
111 multimodelprinter
->clearModels();
112 ui
->textEdit
->clear();
114 for (int i
=0; i
< modelsList
.size(); ++i
) {
115 multimodelprinter
->setModel(i
, &modelsList
[i
].model
, &modelsList
[i
].gs
);
116 QString
name(modelsList
.at(i
).model
.name
);
118 name
= tr("Unnamed Model %1").arg(i
+1);
120 QWidget
* hdr
= new QWidget(this);
121 hdr
->setLayout(new QHBoxLayout());
122 hdr
->layout()->setContentsMargins(0, 0, 0, 0);
123 hdr
->layout()->setSpacing(2);
124 QToolButton
* btn
= new QToolButton(hdr
);
125 btn
->setIcon(CompanionIcon("clear.png"));
126 btn
->setProperty("index", i
);
127 btn
->setFixedSize(18, 18);
128 btn
->setToolTip(tr("Click to remove this model."));
129 hdr
->layout()->addWidget(btn
);
130 QLabel
* lbl
= new QLabel(name
, this);
131 lbl
->setStyleSheet("font-weight: bold;");
132 hdr
->layout()->addWidget(lbl
);
133 connect(btn
, &QToolButton::clicked
, this, &CompareDialog::removeModelBtnClicked
);
135 ui
->layout_modelNames
->addWidget(hdr
);
137 if (modelsList
.size())
138 ui
->textEdit
->setHtml(multimodelprinter
->print(ui
->textEdit
->document()));
141 void CompareDialog::removeModel(int idx
)
143 if (idx
< modelsList
.size()) {
144 modelsList
.remove(idx
);
149 void CompareDialog::removeModelBtnClicked()
151 if (sender() && sender()->property("index").isValid())
152 removeModel(sender()->property("index").toInt());
155 void CompareDialog::on_printButton_clicked()
158 printer
.setPageMargins(10.0, 10.0, 10.0, 10.0, printer
.Millimeter
);
159 QPrintDialog
* dialog
= new QPrintDialog(&printer
, this);
160 dialog
->setWindowTitle(tr("Print Document"));
161 if (dialog
->exec() != QDialog::Accepted
)
163 ui
->textEdit
->print(&printer
);
166 void CompareDialog::on_printFileButton_clicked()
169 QString filename
= QFileDialog::getSaveFileName(this, tr("Select PDF output file"), QString(), "Pdf File(*.pdf)");
170 printer
.setPageMargins(10.0, 10.0, 10.0, 10.0, printer
.Millimeter
);
171 printer
.setOutputFormat(QPrinter::PdfFormat
);
172 printer
.setOrientation(QPrinter::Landscape
);
173 printer
.setColorMode(QPrinter::Color
);
174 if (!filename
.isEmpty()) {
175 if (QFileInfo(filename
).suffix().isEmpty())
176 filename
.append(".pdf");
177 printer
.setOutputFileName(filename
);
178 ui
->textEdit
->print(&printer
);
182 void CompareDialog::on_styleButton_clicked()
184 StyleEditDialog
*g
= new StyleEditDialog(this, MODEL_PRINT_CSS
);
185 if (g
->exec() == QDialog::Accepted
)