OP-1900 have path_progress updated correctly for leg_remaining and error_below end...
[librepilot.git] / ground / openpilotgcs / src / plugins / setupwizard / vehicletemplateexportdialog.cpp
blob12cc625bb518d1c8b495d774a94da0b6afb4888a
1 /**
2 ******************************************************************************
4 * @file vehicletemplateexportdialog.cpp
5 * @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2012.
6 * @addtogroup [Group]
7 * @{
8 * @addtogroup VehicleTemplateExportDialog
9 * @{
10 * @brief [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 "vehicletemplateexportdialog.h"
29 #include "ui_vehicletemplateexportdialog.h"
30 #include <extensionsystem/pluginmanager.h>
31 #include "systemsettings.h"
32 #include <QBuffer>
33 #include <QFileDialog>
34 #include <QJsonDocument>
35 #include <QJsonObject>
36 #include <QJsonArray>
37 #include <QUuid>
38 #include <QDebug>
39 #include <QMessageBox>
40 #include <uavobjecthelper.h>
41 #include <objectpersistence.h>
42 #include "stabilizationsettings.h"
43 #include "stabilizationsettingsbank1.h"
44 #include "stabilizationsettingsbank2.h"
45 #include "stabilizationsettingsbank3.h"
46 #include "mixersettings.h"
47 #include "ekfconfiguration.h"
48 #include <uavtalk/telemetrymanager.h>
49 #include <utils/pathutils.h>
51 const char *VehicleTemplateExportDialog::EXPORT_BASE_NAME = "../share/openpilotgcs/cloudconfig";
52 const char *VehicleTemplateExportDialog::EXPORT_FIXEDWING_NAME = "fixedwing";
53 const char *VehicleTemplateExportDialog::EXPORT_MULTI_NAME = "multirotor";
54 const char *VehicleTemplateExportDialog::EXPORT_HELI_NAME = "helicopter";
55 const char *VehicleTemplateExportDialog::EXPORT_SURFACE_NAME = "surface";
56 const char *VehicleTemplateExportDialog::EXPORT_CUSTOM_NAME = "custom";
58 VehicleTemplateExportDialog::VehicleTemplateExportDialog(QWidget *parent) :
59 QDialog(parent),
60 ui(new Ui::VehicleTemplateExportDialog), m_autopilotConnected(false)
62 ui->setupUi(this);
63 connect(ui->ImportButton, SIGNAL(clicked()), this, SLOT(importImage()));
64 ExtensionSystem::PluginManager *pm = ExtensionSystem::PluginManager::instance();
65 m_uavoManager = pm->getObject<UAVObjectManager>();
66 ui->Photo->setScene(new QGraphicsScene(this));
67 ui->Type->setText(setupVehicleType());
68 ui->selectionWidget->setTemplateInfo(m_type, m_subType);
70 connect(ui->Name, SIGNAL(textChanged(QString)), this, SLOT(updateStatus()));
71 connect(ui->Owner, SIGNAL(textChanged(QString)), this, SLOT(updateStatus()));
72 connect(ui->ForumNick, SIGNAL(textChanged(QString)), this, SLOT(updateStatus()));
73 connect(ui->Size, SIGNAL(textChanged(QString)), this, SLOT(updateStatus()));
74 connect(ui->Weight, SIGNAL(textChanged(QString)), this, SLOT(updateStatus()));
76 connect(ui->exportBtn, SIGNAL(clicked()), this, SLOT(exportTemplate()));
77 connect(ui->saveAsBtn, SIGNAL(clicked()), this, SLOT(saveAsTemplate()));
78 connect(ui->importBtn, SIGNAL(clicked()), this, SLOT(importTemplate()));
79 connect(ui->cancelBtn, SIGNAL(clicked()), this, SLOT(reject()));
80 connect(ui->cancelBtn_2, SIGNAL(clicked()), this, SLOT(reject()));
82 TelemetryManager *telemManager = pm->getObject<TelemetryManager>();
83 if (telemManager->isConnected()) {
84 onAutoPilotConnect();
85 } else {
86 onAutoPilotDisconnect();
89 connect(telemManager, SIGNAL(connected()), this, SLOT(onAutoPilotConnect()));
90 connect(telemManager, SIGNAL(disconnected()), this, SLOT(onAutoPilotDisconnect()));
93 VehicleTemplateExportDialog::~VehicleTemplateExportDialog()
95 delete ui;
98 QString VehicleTemplateExportDialog::setupVehicleType()
100 SystemSettings *systemSettings = SystemSettings::GetInstance(m_uavoManager);
102 Q_ASSERT(systemSettings);
103 SystemSettings::DataFields systemSettingsData = systemSettings->getData();
105 switch (systemSettingsData.AirframeType) {
106 case SystemSettings::AIRFRAMETYPE_FIXEDWING:
107 m_type = VehicleConfigurationSource::VEHICLE_FIXEDWING;
108 m_subType = VehicleConfigurationSource::FIXED_WING_AILERON;
109 return tr("Fixed Wing - Aileron");
111 case SystemSettings::AIRFRAMETYPE_FIXEDWINGELEVON:
112 m_type = VehicleConfigurationSource::VEHICLE_FIXEDWING;
113 m_subType = VehicleConfigurationSource::FIXED_WING_ELEVON;
114 return tr("Fixed Wing - Elevon");
116 case SystemSettings::AIRFRAMETYPE_FIXEDWINGVTAIL:
117 m_type = VehicleConfigurationSource::VEHICLE_FIXEDWING;
118 m_subType = VehicleConfigurationSource::FIXED_WING_VTAIL;
119 return tr("Fixed Wing - V-Tail");
121 case SystemSettings::AIRFRAMETYPE_HELICP:
122 m_type = VehicleConfigurationSource::VEHICLE_HELI;
123 m_subType = VehicleConfigurationSource::HELI_CCPM;
124 return tr("Helicopter");
126 case SystemSettings::AIRFRAMETYPE_TRI:
127 m_type = VehicleConfigurationSource::VEHICLE_MULTI;
128 m_subType = VehicleConfigurationSource::MULTI_ROTOR_TRI_Y;
129 return tr("Multirotor - Tricopter");
131 case SystemSettings::AIRFRAMETYPE_QUADX:
132 m_type = VehicleConfigurationSource::VEHICLE_MULTI;
133 m_subType = VehicleConfigurationSource::MULTI_ROTOR_QUAD_X;
134 return tr("Multirotor - Quadrocopter X");
136 case SystemSettings::AIRFRAMETYPE_QUADP:
137 m_type = VehicleConfigurationSource::VEHICLE_MULTI;
138 m_subType = VehicleConfigurationSource::MULTI_ROTOR_QUAD_PLUS;
139 return tr("Multirotor - Quadrocopter +");
141 case SystemSettings::AIRFRAMETYPE_OCTOV:
142 m_type = VehicleConfigurationSource::VEHICLE_MULTI;
143 m_subType = VehicleConfigurationSource::MULTI_ROTOR_OCTO_V;
144 return tr("Multirotor - Octocopter V");
146 case SystemSettings::AIRFRAMETYPE_OCTOCOAXX:
147 m_type = VehicleConfigurationSource::VEHICLE_MULTI;
148 m_subType = VehicleConfigurationSource::MULTI_ROTOR_OCTO_COAX_X;
149 return tr("Multirotor - Octocopter X8X");
151 case SystemSettings::AIRFRAMETYPE_OCTOCOAXP:
152 m_type = VehicleConfigurationSource::VEHICLE_MULTI;
153 m_subType = VehicleConfigurationSource::MULTI_ROTOR_OCTO_COAX_PLUS;
154 return tr("Multirotor - Octocopter X8+");
156 case SystemSettings::AIRFRAMETYPE_OCTO:
157 m_type = VehicleConfigurationSource::VEHICLE_MULTI;
158 m_subType = VehicleConfigurationSource::MULTI_ROTOR_OCTO;
159 return tr("Multirotor - Octocopter +");
161 case SystemSettings::AIRFRAMETYPE_OCTOX:
162 m_type = VehicleConfigurationSource::VEHICLE_MULTI;
163 m_subType = VehicleConfigurationSource::MULTI_ROTOR_OCTO_X;
164 return tr("Multirotor - Octocopter X");
166 case SystemSettings::AIRFRAMETYPE_HEXAX:
167 m_type = VehicleConfigurationSource::VEHICLE_MULTI;
168 m_subType = VehicleConfigurationSource::MULTI_ROTOR_HEXA_X;
169 return tr("Multirotor - Hexacopter X");
171 case SystemSettings::AIRFRAMETYPE_HEXAH:
172 m_type = VehicleConfigurationSource::VEHICLE_MULTI;
173 m_subType = VehicleConfigurationSource::MULTI_ROTOR_HEXA_H;
174 return tr("Multirotor - Hexacopter H");
176 case SystemSettings::AIRFRAMETYPE_HEXACOAX:
177 m_type = VehicleConfigurationSource::VEHICLE_MULTI;
178 m_subType = VehicleConfigurationSource::MULTI_ROTOR_HEXA_COAX_Y;
179 return tr("Multirotor - Hexacopter Y6");
181 case SystemSettings::AIRFRAMETYPE_HEXA:
182 m_type = VehicleConfigurationSource::VEHICLE_MULTI;
183 m_subType = VehicleConfigurationSource::MULTI_ROTOR_HEXA;
184 return tr("Multirotor - Hexacopter +");
186 default:
187 m_type = VehicleConfigurationSource::VEHICLE_UNKNOWN;
188 return tr("Unsupported");
192 void VehicleTemplateExportDialog::saveTemplate(QString path)
194 QJsonObject exportObject;
196 QList<UAVObject *> objectsToExport;
197 objectsToExport << StabilizationSettings::GetInstance(m_uavoManager);
198 objectsToExport << StabilizationSettingsBank1::GetInstance(m_uavoManager);
199 objectsToExport << StabilizationSettingsBank2::GetInstance(m_uavoManager);
200 objectsToExport << StabilizationSettingsBank3::GetInstance(m_uavoManager);
201 objectsToExport << MixerSettings::GetInstance(m_uavoManager);
202 objectsToExport << EKFConfiguration::GetInstance(m_uavoManager);
203 m_uavoManager->toJson(exportObject, objectsToExport);
205 exportObject["type"] = m_type;
206 exportObject["subtype"] = m_subType;
207 exportObject["name"] = ui->Name->text();
208 exportObject["owner"] = ui->Owner->text();
209 exportObject["nick"] = ui->ForumNick->text();
210 exportObject["size"] = ui->Size->text();
211 exportObject["weight"] = ui->Weight->text();
212 exportObject["motor"] = ui->Motor->text();
213 exportObject["esc"] = ui->Esc->text();
214 exportObject["servo"] = ui->Servo->text();
215 exportObject["battery"] = ui->Battery->text();
216 exportObject["propeller"] = ui->Propeller->text();
217 exportObject["controller"] = ui->Controllers->currentText();
218 exportObject["comment"] = ui->Comment->document()->toPlainText();
219 QUuid uuid = QUuid::createUuid();
220 exportObject["uuid"] = uuid.toString();
222 if (!m_image.isNull()) {
223 QByteArray bytes;
224 QBuffer buffer(&bytes);
225 buffer.open(QIODevice::WriteOnly);
226 m_image.scaled(IMAGE_SCALE_WIDTH, IMAGE_SCALE_HEIGHT, Qt::KeepAspectRatio,
227 Qt::SmoothTransformation).save(&buffer, "PNG");
228 exportObject["photo"] = QString::fromLatin1(bytes.toBase64().data());
231 QJsonDocument saveDoc(exportObject);
233 const char *fileType = ".optmpl";
235 QString fileName = QString("%1-%2-%3%4")
236 .arg(fixFilenameString(ui->Name->text(), 20))
237 .arg(fixFilenameString(ui->Type->text(), 30))
238 .arg(fixFilenameString(uuid.toString().right(12)))
239 .arg(fileType);
241 QString fullPath;
242 if (path.isEmpty()) {
243 fullPath = QString("%1%2%3").arg(QDir::homePath()).arg(QDir::separator()).arg(fileName);
244 fullPath = QFileDialog::getSaveFileName(this, tr("Export settings"), fullPath, QString("%1 (*%2)").arg(tr("OPTemplates"), fileType));
245 } else {
246 fullPath = QString("%1%2%3").arg(path).arg(QDir::separator()).arg(fileName);
249 if (!fullPath.isEmpty()) {
250 if (!fullPath.endsWith(fileType)) {
251 fullPath.append(fileType);
253 QFile saveFile(fullPath);
254 if (saveFile.open(QIODevice::WriteOnly)) {
255 saveFile.write(saveDoc.toJson());
256 saveFile.close();
257 } else {
258 QMessageBox::information(this, "Export", tr("Settings could not be exported to \n%1(%2).\nPlease try again.")
259 .arg(QFileInfo(saveFile).absoluteFilePath(), saveFile.error()), QMessageBox::Ok);
264 QString VehicleTemplateExportDialog::fixFilenameString(QString input, int truncate)
266 return input.replace(' ', "").replace('|', "").replace('/', "")
267 .replace('\\', "").replace(':', "").replace('"', "")
268 .replace('\'', "").replace('?', "").replace('*', "")
269 .replace('>', "").replace('<', "")
270 .replace('}', "").replace('{', "")
271 .left(truncate);
274 void VehicleTemplateExportDialog::exportTemplate()
276 QString path = QString("%1%2%3%4").arg(Utils::PathUtils().InsertStoragePath("%%STOREPATH%%cloudconfig"))
277 .arg(QDir::separator()).arg(getTypeDirectory()).arg(QDir::separator());
278 QDir dir;
280 dir.mkpath(path);
281 saveTemplate(path);
284 void VehicleTemplateExportDialog::saveAsTemplate()
286 saveTemplate(QString(""));
289 void VehicleTemplateExportDialog::importTemplate()
291 QJsonObject *tmpl = ui->selectionWidget->selectedTemplate();
293 if (tmpl != NULL) {
294 QList<UAVObject *> updatedObjects;
295 m_uavoManager->fromJson(*tmpl, &updatedObjects);
296 UAVObjectUpdaterHelper helper;
297 foreach(UAVObject * object, updatedObjects) {
298 UAVDataObject *dataObj = dynamic_cast<UAVDataObject *>(object);
300 if (dataObj != NULL && dataObj->isKnown()) {
301 helper.doObjectAndWait(dataObj);
303 ObjectPersistence *objper = ObjectPersistence::GetInstance(m_uavoManager);
304 ObjectPersistence::DataFields data;
305 data.Operation = ObjectPersistence::OPERATION_SAVE;
306 data.Selection = ObjectPersistence::SELECTION_SINGLEOBJECT;
307 data.ObjectID = dataObj->getObjID();
308 data.InstanceID = dataObj->getInstID();
309 objper->setData(data);
311 helper.doObjectAndWait(objper);
317 void VehicleTemplateExportDialog::importImage()
319 QString fileName = QFileDialog::getOpenFileName(this, tr("Import Image"), "", tr("Images (*.png *.jpg)"));
321 if (!fileName.isEmpty()) {
322 m_image.load(fileName);
323 ui->Photo->scene()->addPixmap(m_image);
324 ui->Photo->setSceneRect(ui->Photo->scene()->itemsBoundingRect());
325 ui->Photo->fitInView(ui->Photo->scene()->itemsBoundingRect(), Qt::KeepAspectRatio);
329 void VehicleTemplateExportDialog::onAutoPilotConnect()
331 ui->importBtn->setEnabled(true);
332 m_autopilotConnected = true;
333 updateStatus();
336 void VehicleTemplateExportDialog::onAutoPilotDisconnect()
338 ui->importBtn->setEnabled(false);
339 m_autopilotConnected = false;
340 updateStatus();
343 QString VehicleTemplateExportDialog::getTypeDirectory()
345 switch (m_type) {
346 case VehicleConfigurationSource::VEHICLE_FIXEDWING:
347 return EXPORT_FIXEDWING_NAME;
349 case VehicleConfigurationSource::VEHICLE_MULTI:
350 return EXPORT_MULTI_NAME;
352 case VehicleConfigurationSource::VEHICLE_HELI:
353 return EXPORT_HELI_NAME;
355 case VehicleConfigurationSource::VEHICLE_SURFACE:
356 return EXPORT_SURFACE_NAME;
358 default:
359 return EXPORT_CUSTOM_NAME;
363 void VehicleTemplateExportDialog::updateStatus()
365 bool enabled = m_autopilotConnected && ui->Name->text().length() > 3 && ui->Owner->text().length() > 2 &&
366 ui->ForumNick->text().length() > 2 && ui->Size->text().length() > 0 &&
367 ui->Weight->text().length() > 0;
369 ui->exportBtn->setEnabled(enabled);
370 ui->saveAsBtn->setEnabled(enabled);