uncrustification
[librepilot.git] / ground / openpilotgcs / src / plugins / setupwizard / pages / airframeinitialtuningpage.cpp
blobd18c7aa3ebd1b2a15eb53e80e19e30ecc2c0ff5f
1 /**
2 ******************************************************************************
4 * @file airframestabfixedwingpage.cpp
5 * @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2012.
6 * @addtogroup
7 * @{
8 * @addtogroup AirframeStabFixedwingPage
9 * @{
10 * @brief
11 *****************************************************************************/
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 3 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful, but
19 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
20 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 * for more details.
23 * You should have received a copy of the GNU General Public License along
24 * with this program; if not, write to the Free Software Foundation, Inc.,
25 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 #include "airframeinitialtuningpage.h"
29 #include "ui_airframeinitialtuningpage.h"
30 #include <QJsonDocument>
31 #include <QJsonArray>
32 #include <QDir>
33 #include "vehicletemplateexportdialog.h"
35 AirframeInitialTuningPage::AirframeInitialTuningPage(SetupWizard *wizard, QWidget *parent) :
36 AbstractWizardPage(wizard, parent),
37 ui(new Ui::AirframeInitialTuningPage), m_dir(NULL), m_photoItem(NULL)
39 ui->setupUi(this);
40 ui->templateImage->setScene(new QGraphicsScene());
41 connect(ui->templateList, SIGNAL(itemSelectionChanged()), this, SLOT(templateSelectionChanged()));
44 AirframeInitialTuningPage::~AirframeInitialTuningPage()
46 ui->templateList->clear();
47 foreach(QJsonObject * templ, m_templates.values()) {
48 delete templ;
50 m_templates.clear();
52 delete ui;
55 void AirframeInitialTuningPage::initializePage()
57 switch (getWizard()->getVehicleType()) {
58 case VehicleConfigurationSource::VEHICLE_FIXEDWING:
59 m_dir = VehicleTemplateExportDialog::EXPORT_FIXEDWING_NAME;
60 break;
61 case VehicleConfigurationSource::VEHICLE_MULTI:
62 m_dir = VehicleTemplateExportDialog::EXPORT_MULTI_NAME;
63 break;
64 case VehicleConfigurationSource::VEHICLE_HELI:
65 m_dir = VehicleTemplateExportDialog::EXPORT_HELI_NAME;
66 break;
67 case VehicleConfigurationSource::VEHICLE_SURFACE:
68 m_dir = VehicleTemplateExportDialog::EXPORT_SURFACE_NAME;
69 break;
70 default:
71 m_dir = NULL;
72 break;
74 loadValidFiles();
75 setupTemplateList();
78 bool AirframeInitialTuningPage::validatePage()
80 QJsonObject *templ = NULL;
82 if (ui->templateList->currentRow() >= 0) {
83 templ = ui->templateList->item(ui->templateList->currentRow())->data(Qt::UserRole + 1).value<QJsonObject *>();
85 if (getWizard()->getVehicleTemplate() != NULL) {
86 delete getWizard()->getVehicleTemplate();
88 getWizard()->setVehicleTemplate(templ != NULL ? new QJsonObject(*templ) : NULL);
89 return true;
92 bool AirframeInitialTuningPage::isComplete() const
94 return true;
97 void AirframeInitialTuningPage::updatePhoto(QJsonObject *templ)
99 QPixmap photo;
101 if (m_photoItem != NULL) {
102 ui->templateImage->scene()->removeItem(m_photoItem);
104 if (templ != NULL && !templ->value("photo").isUndefined()) {
105 QByteArray imageData = QByteArray::fromBase64(templ->value("photo").toString().toLatin1());
106 photo.loadFromData(imageData, "PNG");
107 } else {
108 photo.load(":/core/images/openpilot_logo_500.png");
110 m_photoItem = ui->templateImage->scene()->addPixmap(photo);
111 ui->templateImage->setSceneRect(ui->templateImage->scene()->itemsBoundingRect());
112 ui->templateImage->fitInView(ui->templateImage->scene()->itemsBoundingRect(), Qt::KeepAspectRatio);
115 void AirframeInitialTuningPage::updateDescription(QJsonObject *templ)
117 if (templ != NULL) {
118 QString description;
119 description.append("<b>").append(tr("Name of Vehicle: ")).append("</b>").append(templ->value("name").toString()).append("<br>");
120 description.append("<b>").append(tr("Name of Owner: ")).append("</b>").append(templ->value("owner").toString());
121 if (templ->value("nick") != "") {
122 description.append(" (").append(templ->value("nick").toString()).append(")");
124 description.append("<br>");
125 description.append("<b>").append(tr("Size: ")).append("</b>").append(templ->value("size").toString()).append("<br>");
126 description.append("<b>").append(tr("Weight: ")).append("</b>").append(templ->value("weight").toString()).append("<br>");
127 description.append("<b>").append(tr("Motor(s): ")).append("</b>").append(templ->value("motor").toString()).append("<br>");
128 description.append("<b>").append(tr("ESC(s): ")).append("</b>").append(templ->value("esc").toString()).append("<br>");
129 description.append("<b>").append(tr("Servo(s): ")).append("</b>").append(templ->value("servo").toString()).append("<br>");
130 description.append("<b>").append(tr("Battery: ")).append("</b>").append(templ->value("battery").toString()).append("<br>");
131 description.append("<b>").append(tr("Propellers(s): ")).append("</b>").append(templ->value("propeller").toString()).append("<br>");
132 description.append("<b>").append(tr("Controller: ")).append("</b>").append(templ->value("controller").toString()).append("<br>");
133 description.append("<b>").append(tr("Comments: ")).append("</b>").append(templ->value("comment").toString());
134 ui->templateDescription->setText(description);
135 } else {
136 ui->templateDescription->setText(tr("This option will use the current tuning settings saved on the controller, if your controller "
137 "is currently unconfigured, then the OpenPilot firmware defaults will be used.\n\n"
138 "It is suggested that if this is a first time configuration of your controller, rather than "
139 "use this option, instead select a tuning set that matches your own airframe as close as "
140 "possible from the list above or if you are not able to fine one, then select the generic item "
141 "from the list."));
145 void AirframeInitialTuningPage::templateSelectionChanged()
147 if (ui->templateList->currentRow() >= 0) {
148 QJsonObject *templ = ui->templateList->item(ui->templateList->currentRow())->data(Qt::UserRole + 1).value<QJsonObject *>();
149 updatePhoto(templ);
150 updateDescription(templ);
154 void AirframeInitialTuningPage::loadValidFiles()
156 ui->templateList->clear();
157 foreach(QJsonObject * templ, m_templates.values()) {
158 delete templ;
160 m_templates.clear();
162 QDir templateDir(QString("%1/%2/").arg(VehicleTemplateExportDialog::EXPORT_BASE_NAME).arg(m_dir));
163 QStringList names;
164 names << "*.optmpl";
165 templateDir.setNameFilters(names);
166 templateDir.setSorting(QDir::Name);
167 QStringList files = templateDir.entryList();
168 foreach(QString fileName, files) {
169 QFile file(QString("%1/%2").arg(templateDir.absolutePath()).arg(fileName));
171 if (file.open(QFile::ReadOnly)) {
172 QByteArray jsonData = file.readAll();
173 QJsonParseError error;
174 QJsonDocument templateDoc = QJsonDocument::fromJson(jsonData, &error);
175 if (error.error == QJsonParseError::NoError) {
176 QJsonObject json = templateDoc.object();
177 if (json["type"].toInt() == getWizard()->getVehicleType() &&
178 json["subtype"].toInt() == getWizard()->getVehicleSubType()) {
179 QString uuid = json["uuid"].toString();
180 if (!m_templates.contains(uuid)) {
181 m_templates[json["uuid"].toString()] = new QJsonObject(json);
184 } else {
185 qDebug() << "Error parsing json file: "
186 << fileName << ". Error was:" << error.errorString();
189 file.close();
193 void AirframeInitialTuningPage::setupTemplateList()
195 QListWidgetItem *item = new QListWidgetItem(tr("Current Tuning"), ui->templateList);
197 item->setData(Qt::UserRole + 1, QVariant::fromValue((QJsonObject *)NULL));
198 foreach(QString templ, m_templates.keys()) {
199 QJsonObject *json = m_templates[templ];
201 item = new QListWidgetItem(json->value("name").toString(), ui->templateList);
202 item->setData(Qt::UserRole + 1, QVariant::fromValue(json));
204 ui->templateList->setCurrentRow(0);
207 QString AirframeInitialTuningPage::getTemplateKey(QJsonObject *templ)
209 return QString(templ->value("name").toString());
212 void AirframeInitialTuningPage::resizeEvent(QResizeEvent *)
214 ui->templateImage->setSceneRect(ui->templateImage->scene()->itemsBoundingRect());
215 ui->templateImage->fitInView(ui->templateImage->scene()->itemsBoundingRect(), Qt::KeepAspectRatio);
218 void AirframeInitialTuningPage::showEvent(QShowEvent *)
220 ui->templateImage->setSceneRect(ui->templateImage->scene()->itemsBoundingRect());
221 ui->templateImage->fitInView(ui->templateImage->scene()->itemsBoundingRect(), Qt::KeepAspectRatio);