OP-1814 Reset consumed energy from PFD
[librepilot.git] / ground / openpilotgcs / src / plugins / pfdqml / pfdqmlgadgetwidget.cpp
blob616328091953c4dda75c981467157c66ad8d6010
1 /*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 3 of the License, or
5 * (at your option) any later version.
7 * This program is distributed in the hope that it will be useful, but
8 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
9 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
10 * for more details.
12 * You should have received a copy of the GNU General Public License along
13 * with this program; if not, write to the Free Software Foundation, Inc.,
14 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 #include "pfdqmlgadgetwidget.h"
18 #include "extensionsystem/pluginmanager.h"
19 #include "uavobjectmanager.h"
20 #include "uavobject.h"
21 #include "flightbatterysettings.h"
22 #include "utils/svgimageprovider.h"
23 #ifdef USE_OSG
24 #include "osgearth.h"
25 #endif
26 #include <QDebug>
27 #include <QSvgRenderer>
28 #include <QtOpenGL/QGLWidget>
29 #include <QtCore/qfileinfo.h>
30 #include <QtCore/qdir.h>
31 #include <QMouseEvent>
33 #include <QQmlEngine>
34 #include <QQmlContext>
36 PfdQmlGadgetWidget::PfdQmlGadgetWidget(QWindow *parent) :
37 QQuickView(parent),
38 m_openGLEnabled(false),
39 m_terrainEnabled(false),
40 m_actualPositionUsed(false),
41 m_latitude(46.671478),
42 m_longitude(10.158932),
43 m_altitude(2000),
44 m_speedUnit("m/s"),
45 m_speedFactor(1.0),
46 m_altitudeUnit("m"),
47 m_altitudeFactor(1.0)
49 setResizeMode(SizeRootObjectToView);
51 // setViewport(new QGLWidget(QGLFormat(QGL::SampleBuffers)));
53 QStringList objectsToExport;
54 objectsToExport <<
55 "VelocityState" <<
56 "PositionState" <<
57 "AttitudeState" <<
58 "AccelState" <<
59 "VelocityDesired" <<
60 "PathDesired" <<
61 "AltitudeHoldDesired" <<
62 "GPSPositionSensor" <<
63 "GCSTelemetryStats" <<
64 "SystemAlarms" <<
65 "NedAccel" <<
66 "FlightBatteryState" <<
67 "ActuatorDesired" <<
68 "TakeOffLocation" <<
69 "PathPlan" <<
70 "WaypointActive" <<
71 "OPLinkStatus" <<
72 "FlightStatus" <<
73 "SystemStats" <<
74 "StabilizationDesired" <<
75 "VtolPathFollowerSettings" <<
76 "HwSettings" <<
77 "ManualControlCommand" <<
78 "SystemSettings" <<
79 "RevoSettings" <<
80 "MagState" <<
81 "FlightBatterySettings";
83 ExtensionSystem::PluginManager *pm = ExtensionSystem::PluginManager::instance();
84 UAVObjectManager *objManager = pm->getObject<UAVObjectManager>();
85 m_uavoManager = pm->getObject<UAVObjectManager>();
86 Q_ASSERT(m_uavoManager);
88 foreach(const QString &objectName, objectsToExport) {
89 UAVObject *object = objManager->getObject(objectName);
91 if (object) {
92 engine()->rootContext()->setContextProperty(objectName, object);
93 } else {
94 qWarning() << "Failed to load object" << objectName;
98 // to expose settings values
99 engine()->rootContext()->setContextProperty("qmlWidget", this);
100 #ifdef USE_OSG
101 qmlRegisterType<OsgEarthItem>("org.OpenPilot", 1, 0, "OsgEarth");
102 #endif
105 PfdQmlGadgetWidget::~PfdQmlGadgetWidget()
108 void PfdQmlGadgetWidget::setQmlFile(QString fn)
110 m_qmlFileName = fn;
112 engine()->removeImageProvider("svg");
113 SvgImageProvider *svgProvider = new SvgImageProvider(fn);
114 engine()->addImageProvider("svg", svgProvider);
116 engine()->clearComponentCache();
118 // it's necessary to allow qml side to query svg element position
119 engine()->rootContext()->setContextProperty("svgRenderer", svgProvider);
120 engine()->setBaseUrl(QUrl::fromLocalFile(fn));
122 qDebug() << Q_FUNC_INFO << fn;
123 setSource(QUrl::fromLocalFile(fn));
125 foreach(const QQmlError &error, errors()) {
126 qDebug() << error.description();
130 void PfdQmlGadgetWidget::resetConsumedEnergy()
132 FlightBatterySettings *mBatterySettings = FlightBatterySettings::GetInstance(m_uavoManager);
134 mBatterySettings->setResetConsumedEnergy(true);
135 mBatterySettings->setData(mBatterySettings->getData());
138 void PfdQmlGadgetWidget::setEarthFile(QString arg)
140 if (m_earthFile != arg) {
141 m_earthFile = arg;
142 emit earthFileChanged(arg);
146 void PfdQmlGadgetWidget::setTerrainEnabled(bool arg)
148 bool wasEnabled = terrainEnabled();
150 m_terrainEnabled = arg;
152 if (wasEnabled != terrainEnabled()) {
153 emit terrainEnabledChanged(terrainEnabled());
157 void PfdQmlGadgetWidget::setSpeedUnit(QString unit)
159 if (m_speedUnit != unit) {
160 m_speedUnit = unit;
161 emit speedUnitChanged(unit);
165 void PfdQmlGadgetWidget::setSpeedFactor(double factor)
167 if (m_speedFactor != factor) {
168 m_speedFactor = factor;
169 emit speedFactorChanged(factor);
173 void PfdQmlGadgetWidget::setAltitudeUnit(QString unit)
175 if (m_altitudeUnit != unit) {
176 m_altitudeUnit = unit;
177 emit altitudeUnitChanged(unit);
181 void PfdQmlGadgetWidget::setAltitudeFactor(double factor)
183 if (m_altitudeFactor != factor) {
184 m_altitudeFactor = factor;
185 emit altitudeFactorChanged(factor);
189 void PfdQmlGadgetWidget::setOpenGLEnabled(bool arg)
191 Q_UNUSED(arg);
192 setTerrainEnabled(m_terrainEnabled);
195 // Switch between PositionState UAVObject position
196 // and pre-defined latitude/longitude/altitude properties
197 void PfdQmlGadgetWidget::setActualPositionUsed(bool arg)
199 if (m_actualPositionUsed != arg) {
200 m_actualPositionUsed = arg;
201 emit actualPositionUsedChanged(arg);
205 void PfdQmlGadgetWidget::mouseReleaseEvent(QMouseEvent *event)
207 // Reload the schene on the middle mouse button click.
208 if (event->button() == Qt::MiddleButton) {
209 setQmlFile(m_qmlFileName);
212 QQuickView::mouseReleaseEvent(event);
215 void PfdQmlGadgetWidget::setLatitude(double arg)
217 // not sure qFuzzyCompare is accurate enough for geo coordinates
218 if (m_latitude != arg) {
219 m_latitude = arg;
220 emit latitudeChanged(arg);
224 void PfdQmlGadgetWidget::setLongitude(double arg)
226 if (m_longitude != arg) {
227 m_longitude = arg;
228 emit longitudeChanged(arg);
232 void PfdQmlGadgetWidget::setAltitude(double arg)
234 if (!qFuzzyCompare(m_altitude, arg)) {
235 m_altitude = arg;
236 emit altitudeChanged(arg);