2 ******************************************************************************
4 * @file vehicletemplateexportdialog.cpp
5 * @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2012.
8 * @addtogroup VehicleTemplateExportDialog
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
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"
33 #include <QFileDialog>
34 #include <QJsonDocument>
35 #include <QJsonObject>
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_FIXEDWING_NAME
= "fixedwing";
52 const char *VehicleTemplateExportDialog::EXPORT_MULTI_NAME
= "multirotor";
53 const char *VehicleTemplateExportDialog::EXPORT_HELI_NAME
= "helicopter";
54 const char *VehicleTemplateExportDialog::EXPORT_SURFACE_NAME
= "surface";
55 const char *VehicleTemplateExportDialog::EXPORT_CUSTOM_NAME
= "custom";
57 VehicleTemplateExportDialog::VehicleTemplateExportDialog(QWidget
*parent
) :
59 ui(new Ui::VehicleTemplateExportDialog
), m_autopilotConnected(false)
62 connect(ui
->ImportButton
, SIGNAL(clicked()), this, SLOT(importImage()));
63 ExtensionSystem::PluginManager
*pm
= ExtensionSystem::PluginManager::instance();
64 m_uavoManager
= pm
->getObject
<UAVObjectManager
>();
65 ui
->Photo
->setScene(new QGraphicsScene(this));
66 ui
->Type
->setText(setupVehicleType());
67 ui
->selectionWidget
->setTemplateInfo(m_type
, m_subType
, true);
69 connect(ui
->Name
, SIGNAL(textChanged(QString
)), this, SLOT(updateStatus()));
70 connect(ui
->Owner
, SIGNAL(textChanged(QString
)), this, SLOT(updateStatus()));
71 connect(ui
->ForumNick
, SIGNAL(textChanged(QString
)), this, SLOT(updateStatus()));
72 connect(ui
->Size
, SIGNAL(textChanged(QString
)), this, SLOT(updateStatus()));
73 connect(ui
->Weight
, SIGNAL(textChanged(QString
)), this, SLOT(updateStatus()));
75 connect(ui
->exportBtn
, SIGNAL(clicked()), this, SLOT(exportTemplate()));
76 connect(ui
->saveAsBtn
, SIGNAL(clicked()), this, SLOT(saveAsTemplate()));
77 connect(ui
->importBtn
, SIGNAL(clicked()), this, SLOT(importTemplate()));
78 connect(ui
->cancelBtn
, SIGNAL(clicked()), this, SLOT(reject()));
79 connect(ui
->cancelBtn_2
, SIGNAL(clicked()), this, SLOT(reject()));
81 TelemetryManager
*telemManager
= pm
->getObject
<TelemetryManager
>();
82 if (telemManager
->isConnected()) {
85 onAutoPilotDisconnect();
88 connect(telemManager
, SIGNAL(connected()), this, SLOT(onAutoPilotConnect()));
89 connect(telemManager
, SIGNAL(disconnected()), this, SLOT(onAutoPilotDisconnect()));
92 VehicleTemplateExportDialog::~VehicleTemplateExportDialog()
97 QString
VehicleTemplateExportDialog::setupVehicleType()
99 SystemSettings
*systemSettings
= SystemSettings::GetInstance(m_uavoManager
);
101 Q_ASSERT(systemSettings
);
102 SystemSettings::DataFields systemSettingsData
= systemSettings
->getData();
104 switch (systemSettingsData
.AirframeType
) {
105 case SystemSettings::AIRFRAMETYPE_FIXEDWING
:
106 m_type
= VehicleConfigurationSource::VEHICLE_FIXEDWING
;
107 m_subType
= VehicleConfigurationSource::FIXED_WING_AILERON
;
108 return tr("Fixed Wing - Aileron");
110 case SystemSettings::AIRFRAMETYPE_FIXEDWINGELEVON
:
111 m_type
= VehicleConfigurationSource::VEHICLE_FIXEDWING
;
112 m_subType
= VehicleConfigurationSource::FIXED_WING_ELEVON
;
113 return tr("Fixed Wing - Elevon");
115 case SystemSettings::AIRFRAMETYPE_FIXEDWINGVTAIL
:
116 m_type
= VehicleConfigurationSource::VEHICLE_FIXEDWING
;
117 m_subType
= VehicleConfigurationSource::FIXED_WING_VTAIL
;
118 return tr("Fixed Wing - V-Tail");
120 case SystemSettings::AIRFRAMETYPE_HELICP
:
121 m_type
= VehicleConfigurationSource::VEHICLE_HELI
;
122 m_subType
= VehicleConfigurationSource::HELI_CCPM
;
123 return tr("Helicopter");
125 case SystemSettings::AIRFRAMETYPE_TRI
:
126 m_type
= VehicleConfigurationSource::VEHICLE_MULTI
;
127 m_subType
= VehicleConfigurationSource::MULTI_ROTOR_TRI_Y
;
128 return tr("Multirotor - Tricopter");
130 case SystemSettings::AIRFRAMETYPE_QUADX
:
131 m_type
= VehicleConfigurationSource::VEHICLE_MULTI
;
132 m_subType
= VehicleConfigurationSource::MULTI_ROTOR_QUAD_X
;
133 return tr("Multirotor - Quadrocopter X");
135 case SystemSettings::AIRFRAMETYPE_QUADP
:
136 m_type
= VehicleConfigurationSource::VEHICLE_MULTI
;
137 m_subType
= VehicleConfigurationSource::MULTI_ROTOR_QUAD_PLUS
;
138 return tr("Multirotor - Quadrocopter +");
140 case SystemSettings::AIRFRAMETYPE_OCTOV
:
141 m_type
= VehicleConfigurationSource::VEHICLE_MULTI
;
142 m_subType
= VehicleConfigurationSource::MULTI_ROTOR_OCTO_V
;
143 return tr("Multirotor - Octocopter V");
145 case SystemSettings::AIRFRAMETYPE_OCTOCOAXX
:
146 m_type
= VehicleConfigurationSource::VEHICLE_MULTI
;
147 m_subType
= VehicleConfigurationSource::MULTI_ROTOR_OCTO_COAX_X
;
148 return tr("Multirotor - Octocopter X8X");
150 case SystemSettings::AIRFRAMETYPE_OCTOCOAXP
:
151 m_type
= VehicleConfigurationSource::VEHICLE_MULTI
;
152 m_subType
= VehicleConfigurationSource::MULTI_ROTOR_OCTO_COAX_PLUS
;
153 return tr("Multirotor - Octocopter X8+");
155 case SystemSettings::AIRFRAMETYPE_OCTO
:
156 m_type
= VehicleConfigurationSource::VEHICLE_MULTI
;
157 m_subType
= VehicleConfigurationSource::MULTI_ROTOR_OCTO
;
158 return tr("Multirotor - Octocopter +");
160 case SystemSettings::AIRFRAMETYPE_OCTOX
:
161 m_type
= VehicleConfigurationSource::VEHICLE_MULTI
;
162 m_subType
= VehicleConfigurationSource::MULTI_ROTOR_OCTO_X
;
163 return tr("Multirotor - Octocopter X");
165 case SystemSettings::AIRFRAMETYPE_HEXAX
:
166 m_type
= VehicleConfigurationSource::VEHICLE_MULTI
;
167 m_subType
= VehicleConfigurationSource::MULTI_ROTOR_HEXA_X
;
168 return tr("Multirotor - Hexacopter X");
170 case SystemSettings::AIRFRAMETYPE_HEXAH
:
171 m_type
= VehicleConfigurationSource::VEHICLE_MULTI
;
172 m_subType
= VehicleConfigurationSource::MULTI_ROTOR_HEXA_H
;
173 return tr("Multirotor - Hexacopter H");
175 case SystemSettings::AIRFRAMETYPE_HEXACOAX
:
176 m_type
= VehicleConfigurationSource::VEHICLE_MULTI
;
177 m_subType
= VehicleConfigurationSource::MULTI_ROTOR_HEXA_COAX_Y
;
178 return tr("Multirotor - Hexacopter Y6");
180 case SystemSettings::AIRFRAMETYPE_HEXA
:
181 m_type
= VehicleConfigurationSource::VEHICLE_MULTI
;
182 m_subType
= VehicleConfigurationSource::MULTI_ROTOR_HEXA
;
183 return tr("Multirotor - Hexacopter +");
186 m_type
= VehicleConfigurationSource::VEHICLE_UNKNOWN
;
187 return tr("Unsupported");
191 void VehicleTemplateExportDialog::saveTemplate(QString path
)
193 QJsonObject exportObject
;
195 QList
<UAVObject
*> objectsToExport
;
196 objectsToExport
<< StabilizationSettings::GetInstance(m_uavoManager
);
197 objectsToExport
<< StabilizationSettingsBank1::GetInstance(m_uavoManager
);
198 objectsToExport
<< StabilizationSettingsBank2::GetInstance(m_uavoManager
);
199 objectsToExport
<< StabilizationSettingsBank3::GetInstance(m_uavoManager
);
200 objectsToExport
<< MixerSettings::GetInstance(m_uavoManager
);
201 objectsToExport
<< EKFConfiguration::GetInstance(m_uavoManager
);
202 m_uavoManager
->toJson(exportObject
, objectsToExport
);
204 exportObject
["type"] = m_type
;
205 exportObject
["subtype"] = m_subType
;
206 exportObject
["name"] = ui
->Name
->text();
207 exportObject
["owner"] = ui
->Owner
->text();
208 exportObject
["nick"] = ui
->ForumNick
->text();
209 exportObject
["size"] = ui
->Size
->text();
210 exportObject
["weight"] = ui
->Weight
->text();
211 exportObject
["motor"] = ui
->Motor
->text();
212 exportObject
["esc"] = ui
->Esc
->text();
213 exportObject
["servo"] = ui
->Servo
->text();
214 exportObject
["battery"] = ui
->Battery
->text();
215 exportObject
["propeller"] = ui
->Propeller
->text();
216 exportObject
["controller"] = ui
->Controllers
->currentText();
217 exportObject
["comment"] = ui
->Comment
->document()->toPlainText();
218 QUuid uuid
= QUuid::createUuid();
219 exportObject
["uuid"] = uuid
.toString();
221 if (!m_image
.isNull()) {
223 QBuffer
buffer(&bytes
);
224 buffer
.open(QIODevice::WriteOnly
);
225 m_image
.scaled(IMAGE_SCALE_WIDTH
, IMAGE_SCALE_HEIGHT
, Qt::KeepAspectRatio
,
226 Qt::SmoothTransformation
).save(&buffer
, "PNG");
227 exportObject
["photo"] = QString::fromLatin1(bytes
.toBase64().data());
230 QJsonDocument
saveDoc(exportObject
);
232 const char *fileType
= ".vtmpl";
234 QString fileName
= QString("%1-%2-%3%4")
235 .arg(fixFilenameString(ui
->Name
->text(), 20))
236 .arg(fixFilenameString(ui
->Type
->text(), 30))
237 .arg(fixFilenameString(uuid
.toString().right(12)))
241 if (path
.isEmpty()) {
242 fullPath
= QString("%1%2%3").arg(QDir::homePath()).arg(QDir::separator()).arg(fileName
);
243 fullPath
= QFileDialog::getSaveFileName(this, tr("Export settings"), fullPath
, QString("%1 (*%2)").arg(tr("VehicleTemplates"), fileType
));
245 fullPath
= QString("%1%2%3").arg(path
).arg(QDir::separator()).arg(fileName
);
248 if (!fullPath
.isEmpty()) {
249 if (!fullPath
.endsWith(fileType
)) {
250 fullPath
.append(fileType
);
252 QFile
saveFile(fullPath
);
253 if (saveFile
.open(QIODevice::WriteOnly
)) {
254 saveFile
.write(saveDoc
.toJson());
257 QMessageBox::information(this, "Export", tr("Settings could not be exported to \n%1(%2).\nPlease try again.")
258 .arg(QFileInfo(saveFile
).absoluteFilePath(), saveFile
.error()), QMessageBox::Ok
);
263 QString
VehicleTemplateExportDialog::fixFilenameString(QString input
, int truncate
)
265 return input
.replace(' ', "").replace('|', "").replace('/', "")
266 .replace('\\', "").replace(':', "").replace('"', "")
267 .replace('\'', "").replace('?', "").replace('*', "")
268 .replace('>', "").replace('<', "")
269 .replace('}', "").replace('{', "")
273 void VehicleTemplateExportDialog::exportTemplate()
275 QString path
= QString("%1%2%3%4").arg(Utils::InsertStoragePath("%%STOREPATH%%vehicletemplates"))
276 .arg(QDir::separator()).arg(getTypeDirectory()).arg(QDir::separator());
283 void VehicleTemplateExportDialog::saveAsTemplate()
285 saveTemplate(QString(""));
288 void VehicleTemplateExportDialog::importTemplate()
290 QJsonObject
*tmpl
= ui
->selectionWidget
->selectedTemplate();
293 QList
<UAVObject
*> updatedObjects
;
294 m_uavoManager
->fromJson(*tmpl
, &updatedObjects
);
295 UAVObjectUpdaterHelper helper
;
296 foreach(UAVObject
* object
, updatedObjects
) {
297 UAVDataObject
*dataObj
= dynamic_cast<UAVDataObject
*>(object
);
299 if (dataObj
!= NULL
&& dataObj
->isKnown()) {
300 helper
.doObjectAndWait(dataObj
);
302 ObjectPersistence
*objper
= ObjectPersistence::GetInstance(m_uavoManager
);
303 ObjectPersistence::DataFields data
;
304 data
.Operation
= ObjectPersistence::OPERATION_SAVE
;
305 data
.Selection
= ObjectPersistence::SELECTION_SINGLEOBJECT
;
306 data
.ObjectID
= dataObj
->getObjID();
307 data
.InstanceID
= dataObj
->getInstID();
308 objper
->setData(data
);
310 helper
.doObjectAndWait(objper
);
316 void VehicleTemplateExportDialog::importImage()
318 QString fileName
= QFileDialog::getOpenFileName(this, tr("Import Image"), "", tr("Images (*.png *.jpg)"));
320 if (!fileName
.isEmpty()) {
321 m_image
.load(fileName
);
322 ui
->Photo
->scene()->addPixmap(m_image
);
323 ui
->Photo
->setSceneRect(ui
->Photo
->scene()->itemsBoundingRect());
324 ui
->Photo
->fitInView(ui
->Photo
->scene()->itemsBoundingRect(), Qt::KeepAspectRatio
);
328 void VehicleTemplateExportDialog::onAutoPilotConnect()
330 ui
->importBtn
->setEnabled(true);
331 m_autopilotConnected
= true;
335 void VehicleTemplateExportDialog::onAutoPilotDisconnect()
337 ui
->importBtn
->setEnabled(false);
338 m_autopilotConnected
= false;
342 QString
VehicleTemplateExportDialog::getTypeDirectory()
345 case VehicleConfigurationSource::VEHICLE_FIXEDWING
:
346 return EXPORT_FIXEDWING_NAME
;
348 case VehicleConfigurationSource::VEHICLE_MULTI
:
349 return EXPORT_MULTI_NAME
;
351 case VehicleConfigurationSource::VEHICLE_HELI
:
352 return EXPORT_HELI_NAME
;
354 case VehicleConfigurationSource::VEHICLE_SURFACE
:
355 return EXPORT_SURFACE_NAME
;
358 return EXPORT_CUSTOM_NAME
;
362 void VehicleTemplateExportDialog::updateStatus()
364 bool enabled
= m_autopilotConnected
&& ui
->Name
->text().length() > 2 && ui
->Owner
->text().length() > 2 &&
365 ui
->ForumNick
->text().length() > 2 && ui
->Size
->text().length() > 0 &&
366 ui
->Weight
->text().length() > 0;
368 ui
->Motor
->setEnabled(enabled
);
369 ui
->Esc
->setEnabled(enabled
);
370 ui
->Servo
->setEnabled(enabled
);
371 ui
->Battery
->setEnabled(enabled
);
372 ui
->Propeller
->setEnabled(enabled
);
373 ui
->Controllers
->setEnabled(enabled
);
374 ui
->Comment
->setEnabled(enabled
);
375 ui
->ImportButton
->setEnabled(enabled
);
377 ui
->exportBtn
->setEnabled(enabled
);
378 ui
->saveAsBtn
->setEnabled(enabled
);