Weather UI Component

refs #663
This commit is contained in:
Roland Winklmeier
2016-06-07 21:53:10 +02:00
parent 6a79802c1b
commit 3f4422920a
5 changed files with 607 additions and 11 deletions

View File

@@ -39,6 +39,7 @@ namespace BlackGui
class CSimulatorComponent;
class CTextMessageComponent;
class CUserComponent;
class CWeatherComponent;
//! Main info area

View File

@@ -6,8 +6,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>1530</width>
<height>71</height>
<width>1629</width>
<height>79</height>
</rect>
</property>
<property name="windowTitle">
@@ -565,8 +565,8 @@
<widget class="BlackGui::CDockWidgetInfoArea" name="dwp_Weather">
<property name="minimumSize">
<size>
<width>80</width>
<height>50</height>
<width>157</width>
<height>78</height>
</size>
</property>
<property name="allowedAreas">
@@ -613,13 +613,28 @@
<number>0</number>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<property name="spacing">
<number>0</number>
</property>
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>2</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<item>
<widget class="QFrame" name="comp_Weather">
<property name="frameShape">
<enum>QFrame::StyledPanel</enum>
</property>
<property name="frameShadow">
<enum>QFrame::Raised</enum>
<widget class="BlackGui::Components::CWeatherComponent" name="comp_Weather">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
</widget>
</item>
@@ -708,7 +723,7 @@
<widget class="BlackGui::CDockWidgetInfoArea" name="dwp_Log">
<property name="minimumSize">
<size>
<width>80</width>
<width>91</width>
<height>70</height>
</size>
</property>
@@ -928,6 +943,12 @@
<header>blackgui/components/mappingcomponent.h</header>
<container>1</container>
</customwidget>
<customwidget>
<class>BlackGui::Components::CWeatherComponent</class>
<extends>QTabWidget</extends>
<header>blackgui/components/weathercomponent.h</header>
<container>1</container>
</customwidget>
</customwidgets>
<resources>
<include location="../../blackmisc/blackmisc.qrc"/>

View File

@@ -0,0 +1,242 @@
/* Copyright (C) 2016
* swift project Community / Contributors
*
* This file is part of swift Project. It is subject to the license terms in the LICENSE file found in the top-level
* directory of this distribution and at http://www.swift-project.org/license.html. No part of swift project,
* including this file, may be copied, modified, propagated, or distributed except according to the terms
* contained in the LICENSE file.
*/
#include "blackgui/components/weathercomponent.h"
#include "blackgui/infoarea.h"
#include "blackgui/views/viewbase.h"
#include "blackgui/guiapplication.h"
#include "blackcore/contextapplication.h"
#include "blackcore/contextsimulator.h"
#include "blackcore/contextownaircraft.h"
#include "blackmisc/pq/length.h"
#include "blackmisc/logmessage.h"
#include "blackmisc/weather/weathergrid.h"
#include "ui_weathercomponent.h"
#include <QCompleter>
#include <QRegExpValidator>
#include <QRegularExpression>
using namespace BlackCore;
using namespace BlackGui;
using namespace BlackGui::Models;
using namespace BlackGui::Views;
using namespace BlackMisc;
using namespace BlackMisc::Geo;
using namespace BlackMisc::PhysicalQuantities;
using namespace BlackMisc::Weather;
namespace BlackGui
{
namespace Components
{
CWeatherComponent::CWeatherComponent(QWidget *parent) :
QWidget(parent),
CIdentifiable(this),
ui(new Ui::CWeatherComponent)
{
ui->setupUi(this);
m_weatherScenarios = CWeatherGrid::getAllScenarios();
for (const auto &scenario : m_weatherScenarios)
{
ui->cb_weatherScenario->addItem(scenario.getName(), QVariant::fromValue(scenario));
}
auto scenario = m_weatherScenarioSetting.get();
ui->cb_weatherScenario->setCurrentIndex(scenario.getIndex());
setupConnections();
setupInputValidators();
setupCompleter();
// Set interval to 5 min
m_weatherUpdateTimer.setInterval(1000 * 60 * 5);
// Call this method deferred in order to have the component fully initialized, e.g. object name set by the parent
QTimer::singleShot(0, this, &CWeatherComponent::updateWeatherInformation);
}
CWeatherComponent::~CWeatherComponent()
{ }
bool CWeatherComponent::setParentDockWidgetInfoArea(CDockWidgetInfoArea *parentDockableWidget)
{
CEnableForDockWidgetInfoArea::setParentDockWidgetInfoArea(parentDockableWidget);
bool c = connect(this->getParentInfoArea(), &CInfoArea::changedInfoAreaTabBarIndex, this, &CWeatherComponent::ps_infoAreaTabBarChanged);
Q_ASSERT_X(c, Q_FUNC_INFO, "failed connect");
Q_ASSERT_X(parentDockableWidget, Q_FUNC_INFO, "missing parent");
return c && parentDockableWidget;
}
void CWeatherComponent::ps_infoAreaTabBarChanged(int index)
{
// ignore in those cases
if (!this->isVisibleWidget()) { return; }
if (this->isParentDockWidgetFloating()) { return; }
// here I know I am the selected widget, update, but keep GUI responsive (-> timer)
//QTimer::singleShot(1000, this, &CWeatherComponent::update);
Q_UNUSED(index);
}
void CWeatherComponent::toggleUseOwnAircraftPosition(bool checked)
{
m_lastOwnAircraftPosition = {};
if (checked)
{
ui->le_LatOrIcao->setReadOnly(true);
ui->le_Lon->setReadOnly(true);
m_weatherUpdateTimer.start();
updateWeatherInformation();
}
else
{
m_weatherUpdateTimer.stop();
ui->le_LatOrIcao->setReadOnly(false);
ui->le_LatOrIcao->setText({});
ui->le_Lon->setReadOnly(false);
ui->le_Lon->setText({});
updateWeatherInformation();
}
}
void CWeatherComponent::setWeatherScenario(int index)
{
if (index == -1) { return; }
m_lastOwnAircraftPosition = {};
auto scenario = m_weatherScenarios[index];
m_weatherScenarioSetting.set(scenario);
updateWeatherInformation();
}
void CWeatherComponent::updateWeatherInformation()
{
setWeatherGrid({});
const bool useOwnAcftPosition = ui->cb_UseOwnAcftPosition->isChecked();
BlackMisc::Geo::CCoordinateGeodetic position;
Q_ASSERT(ui->cb_weatherScenario->currentData().canConvert<CWeatherScenario>());
CWeatherScenario scenario = ui->cb_weatherScenario->currentData().value<CWeatherScenario>();
if (useOwnAcftPosition)
{
Q_ASSERT(sGui->getIContextOwnAircraft());
position = sGui->getIContextOwnAircraft()->getOwnAircraft().getPosition();
if(position == CCoordinateGeodetic())
{
ui->le_LatOrIcao->setText("N/A");
ui->le_Lon->setText("N/A");
return;
}
ui->le_LatOrIcao->setText(position.latitude().toQString());
ui->le_Lon->setText(position.longitude().toQString());
}
else
{
QString latitudeOrIcao = ui->le_LatOrIcao->text();
QString longitude = ui->le_Lon->text();
const QRegularExpression reIcao("^[a-zA-Z]{4}$", QRegularExpression::CaseInsensitiveOption);
auto icaoMatch = reIcao.match(latitudeOrIcao);
if (icaoMatch.hasMatch())
{
CLogMessage(this).warning("Requested weather for ICAO station %1. Unfortunately ICAO support is not yet implemented.") << latitudeOrIcao;
return;
}
const QRegularExpression reDecimalNumber("^-?\\d{1,2}[,.]?\\d+$", QRegularExpression::CaseInsensitiveOption);
auto latitudeMatch = reDecimalNumber.match(latitudeOrIcao);
auto longitudeMatch = reDecimalNumber.match(longitude);
if (!latitudeMatch.hasMatch() || !longitudeMatch.hasMatch())
{
CLogMessage(this).warning("Invalid position - Latitude: %1, Longitude: %2") << latitudeOrIcao << longitude;
return;
}
// Replace ',' with '.'. Both are allowed as input, but QString::toDouble() only accepts '.'
latitudeOrIcao = latitudeOrIcao.replace(',', '.');
longitude = longitude.replace(',', '.');
position = { latitudeOrIcao.toDouble(), longitude.toDouble(), 0 };
}
if (CWeatherScenario::isRealWeatherScenario(scenario))
{
if (!useOwnAcftPosition ||
calculateGreatCircleDistance(position, m_lastOwnAircraftPosition).value(CLengthUnit::km()) > 20 )
{
requestWeatherGrid(position);
m_lastOwnAircraftPosition = position;
}
}
else
{
setWeatherGrid(CWeatherGrid::getByScenario(scenario));
}
}
void CWeatherComponent::weatherGridReceived(const CWeatherGrid &weatherGrid, const CIdentifier &identifier)
{
if(!isMyIdentifier(identifier)) { return; }
ui->lb_Status->setText({});
setWeatherGrid(weatherGrid);
}
void CWeatherComponent::setupConnections()
{
// UI connections
connect(ui->cb_weatherScenario, static_cast<void(QComboBox::*)(int)>(&QComboBox::currentIndexChanged),
this, &CWeatherComponent::setWeatherScenario);
connect(ui->le_LatOrIcao, &QLineEdit::returnPressed, this, &CWeatherComponent::updateWeatherInformation);
connect(ui->le_Lon, &QLineEdit::returnPressed, this, &CWeatherComponent::updateWeatherInformation);
connect(ui->cb_UseOwnAcftPosition, &QCheckBox::toggled, this, &CWeatherComponent::toggleUseOwnAircraftPosition);
connect(&m_weatherUpdateTimer, &QTimer::timeout, this, &CWeatherComponent::updateWeatherInformation);
// Context connections
Q_ASSERT(sGui->getIContextSimulator());
connect(sGui->getIContextSimulator(), &IContextSimulator::weatherGridReceived, this, &CWeatherComponent::weatherGridReceived);
}
void CWeatherComponent::setupInputValidators()
{
QRegExp reIcaoOrLatitude("^[a-zA-Z]{4}|-?\\d{1,2}[,.]?\\d+$", Qt::CaseInsensitive);
ui->le_LatOrIcao->setValidator(new QRegExpValidator(reIcaoOrLatitude, this));
QRegExp reLongitude("^-?\\d{1,2}[,.]?\\d+$", Qt::CaseInsensitive);
ui->le_Lon->setValidator(new QRegExpValidator(reLongitude, this));
}
void CWeatherComponent::setupCompleter()
{
// Temporary list of ICAO airports. Replace with final list, once available
QStringList airports = { "EDDM", "LSZH", "EDMO", "EGLL" };
QCompleter *c = new QCompleter(airports, this);
c->setCaseSensitivity(Qt::CaseInsensitive);
c->setCompletionMode(QCompleter::PopupCompletion);
c->setMaxVisibleItems(5);
ui->le_LatOrIcao->setCompleter(c);
}
void CWeatherComponent::setWeatherGrid(const CWeatherGrid &weatherGrid)
{
auto gridPoint = weatherGrid.frontOrDefault();
ui->tvp_TemperatureLayers->updateContainer(gridPoint.getTemperatureLayers());
ui->tvp_CloudLayers->updateContainer(gridPoint.getCloudLayers());
ui->tvp_WindLayers->updateContainer(gridPoint.getWindLayers());
}
void CWeatherComponent::requestWeatherGrid(const CCoordinateGeodetic &position)
{
ui->lb_Status->setText("Loading...");
CWeatherGrid weatherGrid { { {}, position } };
auto ident = identifier();
sGui->getIContextSimulator()->requestWeatherGrid(weatherGrid, ident);
}
} // namespace
} // namespace

View File

@@ -0,0 +1,84 @@
/* Copyright (C) 2016
* swift project Community / Contributors
*
* This file is part of swift Project. It is subject to the license terms in the LICENSE file found in the top-level
* directory of this distribution and at http://www.swift-project.org/license.html. No part of swift project,
* including this file, may be copied, modified, propagated, or distributed except according to the terms
* contained in the LICENSE file.
*/
//! \file
#ifndef BLACKGUI_WEATHERCOMPONENT_H
#define BLACKGUI_WEATHERCOMPONENT_H
#include "blackgui/blackguiexport.h"
#include "blackgui/components/enablefordockwidgetinfoarea.h"
#include "blackgui/components/updatetimer.h"
#include "blackcore/settings/simulator.h"
#include "blackmisc/geo/coordinategeodetic.h"
#include "blackmisc/identifiable.h"
#include "blackmisc/weather/weatherscenario.h"
#include <QDateTime>
#include <QModelIndex>
#include <QObject>
#include <QScopedPointer>
#include <QString>
#include <QTimer>
#include <QWidget>
#include <QtGlobal>
namespace BlackMisc { namespace Weather { class CWeatherGrid; } }
namespace Ui { class CWeatherComponent; }
namespace BlackGui
{
class CDockWidgetInfoArea;
namespace Components
{
//! Weather component
class BLACKGUI_EXPORT CWeatherComponent :
public QWidget,
public CEnableForDockWidgetInfoArea,
public BlackMisc::CIdentifiable
{
Q_OBJECT
public:
//! Constructor
explicit CWeatherComponent(QWidget *parent = nullptr);
//! Destructor
virtual ~CWeatherComponent();
//! \copydoc CEnableForDockWidgetInfoArea::setParentDockWidgetInfoArea
virtual bool setParentDockWidgetInfoArea(BlackGui::CDockWidgetInfoArea *parentDockableWidget) override;
private slots:
void ps_infoAreaTabBarChanged(int index);
private:
void toggleUseOwnAircraftPosition(bool checked);
void setWeatherScenario(int index);
void updateWeatherInformation();
void weatherGridReceived(const BlackMisc::Weather::CWeatherGrid &weatherGrid, const BlackMisc::CIdentifier &identifier);
void setupConnections();
void setupInputValidators();
void setupCompleter();
void setWeatherGrid(const BlackMisc::Weather::CWeatherGrid &weatherGrid);
void requestWeatherGrid(const BlackMisc::Geo::CCoordinateGeodetic &position);
QScopedPointer<Ui::CWeatherComponent> ui;
QVector<BlackMisc::Weather::CWeatherScenario> m_weatherScenarios;
QTimer m_weatherUpdateTimer { this };
BlackMisc::Geo::CCoordinateGeodetic m_lastOwnAircraftPosition;
BlackMisc::CSetting<BlackCore::Settings::Simulator::SelectedWeatherScenario> m_weatherScenarioSetting { this };
};
} // namespace
} // namespace
#endif // guard

View File

@@ -0,0 +1,248 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>CWeatherComponent</class>
<widget class="QWidget" name="CWeatherComponent">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>556</height>
</rect>
</property>
<property name="windowTitle">
<string>Weather</string>
</property>
<property name="currentIndex" stdset="0">
<number>0</number>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>7</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<item>
<widget class="QGroupBox" name="gb_weatherControl">
<property name="title">
<string>Weather Control</string>
</property>
<layout class="QGridLayout" name="gridLayout">
<property name="leftMargin">
<number>1</number>
</property>
<property name="topMargin">
<number>7</number>
</property>
<property name="rightMargin">
<number>1</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<property name="horizontalSpacing">
<number>1</number>
</property>
<item row="0" column="0">
<widget class="QComboBox" name="cb_weatherScenario"/>
</item>
</layout>
</widget>
</item>
<item>
<widget class="QGroupBox" name="gb_weatherDisplay">
<property name="title">
<string>Weather Display</string>
</property>
<layout class="QGridLayout" name="gridLayout_2">
<property name="leftMargin">
<number>6</number>
</property>
<property name="topMargin">
<number>6</number>
</property>
<property name="rightMargin">
<number>6</number>
</property>
<property name="bottomMargin">
<number>6</number>
</property>
<property name="spacing">
<number>6</number>
</property>
<item row="0" column="0" colspan="2">
<widget class="QCheckBox" name="cb_UseOwnAcftPosition">
<property name="text">
<string>Use Own Aircraft Position</string>
</property>
<property name="checked">
<bool>true</bool>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QLineEdit" name="le_LatOrIcao">
<property name="minimumSize">
<size>
<width>85</width>
<height>0</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>100</width>
<height>16777215</height>
</size>
</property>
<property name="placeholderText">
<string>Lat or ICAO</string>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QLineEdit" name="le_Lon">
<property name="minimumSize">
<size>
<width>85</width>
<height>0</height>
</size>
</property>
<property name="placeholderText">
<string>Lon</string>
</property>
</widget>
</item>
<item row="1" column="2">
<widget class="QLabel" name="lb_Status">
<property name="text">
<string/>
</property>
</widget>
</item>
<item row="1" column="3">
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item row="2" column="0" colspan="4">
<widget class="QTabWidget" name="tw_weatherGrid">
<property name="currentIndex">
<number>0</number>
</property>
<widget class="QWidget" name="tb_temperatureLayers">
<attribute name="title">
<string>Temperature</string>
</attribute>
<layout class="QVBoxLayout" name="verticalLayout_2">
<property name="spacing">
<number>0</number>
</property>
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>1</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<item>
<widget class="BlackGui::Views::CTemperatureLayerView" name="tvp_TemperatureLayers"/>
</item>
</layout>
</widget>
<widget class="QWidget" name="tb_cloudLayers">
<attribute name="title">
<string>Clouds</string>
</attribute>
<layout class="QVBoxLayout" name="verticalLayout_3">
<property name="spacing">
<number>0</number>
</property>
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>1</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<item>
<widget class="BlackGui::Views::CCloudLayerView" name="tvp_CloudLayers"/>
</item>
</layout>
</widget>
<widget class="QWidget" name="tb_windLayers">
<attribute name="title">
<string>Winds</string>
</attribute>
<layout class="QVBoxLayout" name="verticalLayout_4">
<property name="spacing">
<number>0</number>
</property>
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>1</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<item>
<widget class="BlackGui::Views::CWindLayerView" name="tvp_WindLayers"/>
</item>
</layout>
</widget>
</widget>
</item>
</layout>
</widget>
</item>
</layout>
</widget>
<customwidgets>
<customwidget>
<class>BlackGui::Views::CTemperatureLayerView</class>
<extends>QTableView</extends>
<header>blackgui/views/temperaturelayerview.h</header>
</customwidget>
<customwidget>
<class>BlackGui::Views::CCloudLayerView</class>
<extends>QTableView</extends>
<header>blackgui/views/cloudlayerview.h</header>
</customwidget>
<customwidget>
<class>BlackGui::Views::CWindLayerView</class>
<extends>QTableView</extends>
<header>blackgui/views/windlayerview.h</header>
</customwidget>
</customwidgets>
<resources/>
<connections/>
</ui>