From 42508619ee3ed48fa071961b64eba78ca78660cb Mon Sep 17 00:00:00 2001 From: James Turner Date: Thu, 18 May 2017 09:17:27 +0100 Subject: [PATCH] Validation of METAR in the launcher --- src/GUI/LauncherMainWindow.cxx | 20 ++++++++++++++++++++ src/GUI/LauncherMainWindow.hxx | 3 +++ src/GUI/Weather.qml | 21 ++++++++++++++++++++- 3 files changed, 43 insertions(+), 1 deletion(-) diff --git a/src/GUI/LauncherMainWindow.cxx b/src/GUI/LauncherMainWindow.cxx index a1f8a7f2a..2c6acb966 100644 --- a/src/GUI/LauncherMainWindow.cxx +++ b/src/GUI/LauncherMainWindow.cxx @@ -14,6 +14,8 @@ // simgear headers #include +#include +#include // FlightGear headers #include @@ -348,6 +350,8 @@ void LauncherMainWindow::buildEnvironmentSections() LauncherMainWindow::~LauncherMainWindow() { + m_qmlEngine->collectGarbage(); + delete m_qmlEngine; } bool LauncherMainWindow::execInApp() @@ -1097,3 +1101,19 @@ void LauncherMainWindow::onSettingsSearchChanged() ss->setSearchTerm(m_ui->settingsSearchEdit->text()); } } + +bool LauncherMainWindow::validateMetarString(QString metar) +{ + if (metar.isEmpty()) { + return true; + } + + try { + std::string s = metar.toStdString(); + SGMetar theMetar(s); + } catch (sg_io_exception&) { + return false; + } + + return true; +} diff --git a/src/GUI/LauncherMainWindow.hxx b/src/GUI/LauncherMainWindow.hxx index 4cf254b47..fc5746c4e 100644 --- a/src/GUI/LauncherMainWindow.hxx +++ b/src/GUI/LauncherMainWindow.hxx @@ -57,6 +57,9 @@ public: bool execInApp(); bool wasRejected(); + + Q_INVOKABLE bool validateMetarString(QString metar); + protected: virtual void closeEvent(QCloseEvent *event) override; diff --git a/src/GUI/Weather.qml b/src/GUI/Weather.qml index 7cdf50032..f7f0598bd 100644 --- a/src/GUI/Weather.qml +++ b/src/GUI/Weather.qml @@ -1,4 +1,5 @@ import FlightGear.Launcher 1.0 +import QtQml 2.2 Section { id: weatherSettings @@ -23,11 +24,29 @@ Section { LineEdit { id: customMETAR + + property bool __cachedValid: true + + function revalidate() { + __cachedValid = _launcher.validateMetarString(value); + } + visible: weatherScenario.isCustomMETAR enabled: !fetchMetar.checked label: "METAR" placeholder: "XXXX 012345Z 28035G50KT 250V300 9999 TSRA SCT022CB BKN030 13/09 Q1005" - description: "Enter a custom METAR string" + description: __cachedValid ? "Enter a custom METAR string" + : "The entered METAR string doesn't seem to be valid." + + onValueChanged: { + validateTimeout.restart() + } + } + + Timer { + id: validateTimeout + interval: 200 + onTriggered: customMETAR.revalidate(); } onApply: {