From 2c7333935d523cc1da2f00ca9356bf41872407a6 Mon Sep 17 00:00:00 2001 From: Philippe Renon Date: Sat, 12 Mar 2016 18:48:45 +0100 Subject: [PATCH] LP-29 add ability to change vehicle model using up and down arrow key wip : needs a proper UI to let user change model --- .../gcs/src/plugins/pfdqml/pfdqmlcontext.cpp | 60 ++++++++++++++++--- ground/gcs/src/plugins/pfdqml/pfdqmlcontext.h | 19 +++++- .../src/share/qml/model/ModelTerrainView.qml | 8 +++ ground/gcs/src/share/qml/model/ModelView.qml | 8 +++ 4 files changed, 87 insertions(+), 8 deletions(-) diff --git a/ground/gcs/src/plugins/pfdqml/pfdqmlcontext.cpp b/ground/gcs/src/plugins/pfdqml/pfdqmlcontext.cpp index 9f3331c00..22aa9bf81 100644 --- a/ground/gcs/src/plugins/pfdqml/pfdqmlcontext.cpp +++ b/ground/gcs/src/plugins/pfdqml/pfdqmlcontext.cpp @@ -28,13 +28,18 @@ #include "pfdqmlcontext.h" #include "extensionsystem/pluginmanager.h" -#include "uavobjectmanager.h" #include "uavobject.h" +#include "uavobjectmanager.h" #include "utils/stringutils.h" +#include "utils/pathutils.h" + #include "flightbatterysettings.h" #include #include +#include + +const QString PfdQmlContext::CONTEXT_PROPERTY_NAME = "pfdContext"; PfdQmlContext::PfdQmlContext(QObject *parent) : QObject(parent), m_speedUnit("m/s"), @@ -50,8 +55,13 @@ PfdQmlContext::PfdQmlContext(QObject *parent) : QObject(parent), m_dateTime(QDateTime()), m_minAmbientLight(0.03), m_modelFile(""), + m_modelIndex(0), m_backgroundImageFile("") -{} +{ + addModelDir("helis"); + addModelDir("multi"); + addModelDir("planes"); +} PfdQmlContext::~PfdQmlContext() {} @@ -220,11 +230,32 @@ QString PfdQmlContext::modelFile() const void PfdQmlContext::setModelFile(const QString &arg) { if (m_modelFile != arg) { - m_modelFile = arg; + m_modelFile = arg; + m_modelIndex = m_modelFileList.indexOf(m_modelFile); + if (m_modelIndex == -1) { + m_modelIndex = 0; + } emit modelFileChanged(modelFile()); } } +void PfdQmlContext::nextModel() +{ + m_modelIndex = (m_modelIndex + 1) % m_modelFileList.length(); + setModelFile(m_modelFileList[m_modelIndex]); +} + +void PfdQmlContext::previousModel() +{ + m_modelIndex = (m_modelFileList.length() + m_modelIndex - 1) % m_modelFileList.length(); + setModelFile(m_modelFileList[m_modelIndex]); +} + +QStringList PfdQmlContext::modelFileList() const +{ + return m_modelFileList; +} + QString PfdQmlContext::backgroundImageFile() const { return m_backgroundImageFile; @@ -281,12 +312,16 @@ void PfdQmlContext::loadConfiguration(PfdQmlGadgetConfiguration *config) void PfdQmlContext::saveState(QSettings *settings) { - Q_UNUSED(settings); + settings->setValue("modelFile", modelFile()); } void PfdQmlContext::restoreState(QSettings *settings) { - Q_UNUSED(settings); + QString file = settings->value("modelFile").toString(); + + if (!file.isEmpty()) { + setModelFile(file); + } } void PfdQmlContext::apply(QQmlContext *context) @@ -339,6 +374,17 @@ void PfdQmlContext::apply(QQmlContext *context) } } - // to expose settings values - context->setContextProperty("pfdContext", this); + // expose this context to Qml + context->setContextProperty(CONTEXT_PROPERTY_NAME, this); +} + +void PfdQmlContext::addModelDir(QString dir) +{ + QDirIterator it(Utils::GetDataPath() + "models/" + dir, QStringList("*.3ds"), QDir::NoFilter, QDirIterator::Subdirectories); + + while (it.hasNext()) { + QString file = QDir::toNativeSeparators(it.next()); + // qDebug() << file; + m_modelFileList.append(file); + } } diff --git a/ground/gcs/src/plugins/pfdqml/pfdqmlcontext.h b/ground/gcs/src/plugins/pfdqml/pfdqmlcontext.h index e26a4d9d1..ca0e88730 100644 --- a/ground/gcs/src/plugins/pfdqml/pfdqmlcontext.h +++ b/ground/gcs/src/plugins/pfdqml/pfdqmlcontext.h @@ -52,7 +52,11 @@ class PfdQmlContext : public QObject { Q_PROPERTY(QDateTime dateTime READ dateTime WRITE setDateTime NOTIFY dateTimeChanged) Q_PROPERTY(double minimumAmbientLight READ minimumAmbientLight WRITE setMinimumAmbientLight NOTIFY minimumAmbientLightChanged) - Q_PROPERTY(QString modelFile READ modelFile WRITE setModelFile NOTIFY modelFileChanged) + // model + Q_PROPERTY(QString modelFile READ modelFile NOTIFY modelFileChanged) + Q_PROPERTY(QStringList modelFileList READ modelFileList CONSTANT FINAL) + + // background Q_PROPERTY(QString backgroundImageFile READ backgroundImageFile WRITE setBackgroundImageFile NOTIFY backgroundImageFileChanged) public: @@ -87,8 +91,14 @@ public: double minimumAmbientLight() const; void setMinimumAmbientLight(double arg); + // model QString modelFile() const; void setModelFile(const QString &arg); + QStringList modelFileList() const; + Q_INVOKABLE void nextModel(); + Q_INVOKABLE void previousModel(); + + // background QString backgroundImageFile() const; void setBackgroundImageFile(const QString &arg); @@ -121,6 +131,9 @@ signals: void backgroundImageFileChanged(QString arg); private: + // constants + static const QString CONTEXT_PROPERTY_NAME; + QString m_speedUnit; double m_speedFactor; QString m_altitudeUnit; @@ -138,7 +151,11 @@ private: double m_minAmbientLight; QString m_modelFile; + int m_modelIndex; + QStringList m_modelFileList; QString m_backgroundImageFile; + + void addModelDir(QString dir); }; #endif /* PFDQMLCONTEXT_H_ */ diff --git a/ground/gcs/src/share/qml/model/ModelTerrainView.qml b/ground/gcs/src/share/qml/model/ModelTerrainView.qml index 9f3fd0536..c6d15e86d 100644 --- a/ground/gcs/src/share/qml/model/ModelTerrainView.qml +++ b/ground/gcs/src/share/qml/model/ModelTerrainView.qml @@ -89,4 +89,12 @@ OSGViewport { // model will be tracked trackNode: modelTransformNode } + + Keys.onUpPressed: { + pfdContext.nextModel(); + } + + Keys.onDownPressed: { + pfdContext.previousModel(); + } } diff --git a/ground/gcs/src/share/qml/model/ModelView.qml b/ground/gcs/src/share/qml/model/ModelView.qml index 68ea35ad5..9bb15ad7a 100644 --- a/ground/gcs/src/share/qml/model/ModelView.qml +++ b/ground/gcs/src/share/qml/model/ModelView.qml @@ -62,6 +62,14 @@ Item { fieldOfView: 90 sceneNode: transformNode } + + Keys.onUpPressed: { + pfdContext.nextModel(); + } + + Keys.onDownPressed: { + pfdContext.previousModel(); + } } }