Ref T225, use coordinate dialog in weather component

Remark: There was a redundant, also unfinished code in weather UI component for coordinate entry. This is now encapsulated in coordinate form/dialog.
This commit is contained in:
Klaus Basan
2018-01-18 14:42:55 +01:00
parent a764ea7534
commit 0916c90038
4 changed files with 89 additions and 149 deletions

View File

@@ -44,6 +44,9 @@ namespace BlackGui
ui(new Ui::CWeatherComponent)
{
ui->setupUi(this);
m_coordinateDialog->showElevation(false);
m_coordinateDialog->setReadOnly(ui->cb_UseOwnAcftPosition->isChecked());
connect(ui->pb_Coordinate, &QPushButton::clicked, this, &CWeatherComponent::showCoordinateDialog);
m_weatherScenarios = CWeatherGrid::getAllScenarios();
for (const auto &scenario : as_const(m_weatherScenarios))
@@ -56,18 +59,18 @@ namespace BlackGui
ui->pb_ActivateWeather->setIcon(CIcons::metar());
setupConnections();
setupInputValidators();
setupCompleter();
ui->lbl_Status->setText({});
// hotkeys
const QString swift(CGuiActionBindHandler::pathSwiftPilotClient());
m_hotkeyBindings.append(CGuiActionBindHandler::bindButton(ui->pb_ActivateWeather, swift + "Weather/Toggle weather", true));
m_hotkeyBindings.append(CGuiActionBindHandler::bindButton(ui->pb_ActivateWeather, swift + "Weather/Force CAVOK", true));
// 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);
QTimer::singleShot(1000, this, &CWeatherComponent::updateWeatherInformation);
}
CWeatherComponent::~CWeatherComponent()
@@ -76,13 +79,13 @@ namespace BlackGui
bool CWeatherComponent::setParentDockWidgetInfoArea(CDockWidgetInfoArea *parentDockableWidget)
{
CEnableForDockWidgetInfoArea::setParentDockWidgetInfoArea(parentDockableWidget);
bool c = connect(this->getParentInfoArea(), &CInfoArea::changedInfoAreaTabBarIndex, this, &CWeatherComponent::ps_infoAreaTabBarChanged);
bool c = connect(this->getParentInfoArea(), &CInfoArea::changedInfoAreaTabBarIndex, this, &CWeatherComponent::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)
void CWeatherComponent::infoAreaTabBarChanged(int index)
{
// ignore in those cases
if (!this->isVisibleWidget()) { return; }
@@ -93,25 +96,19 @@ namespace BlackGui
Q_UNUSED(index);
}
void CWeatherComponent::toggleUseOwnAircraftPosition(bool checked)
void CWeatherComponent::toggleUseOwnAircraftPosition(bool useOwnAircraftPosition)
{
m_lastOwnAircraftPosition = {};
if (checked)
m_coordinateDialog->setReadOnly(useOwnAircraftPosition);
if (useOwnAircraftPosition)
{
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();
}
updateWeatherInformation();
}
void CWeatherComponent::toggleWeatherActivation()
@@ -129,16 +126,34 @@ namespace BlackGui
sGui->getIContextSimulator()->setWeatherActivated(m_isWeatherActivated);
}
void CWeatherComponent::showCoordinateDialog()
{
m_coordinateDialog->show();
}
void CWeatherComponent::setWeatherScenario(int index)
{
if (index == -1) { return; }
m_lastOwnAircraftPosition = {};
auto scenario = m_weatherScenarios[index];
CWeatherScenario scenario = m_weatherScenarios[index];
m_weatherScenarioSetting.set(scenario);
updateWeatherInformation();
}
void CWeatherComponent::setCavok()
{
for (int index = 0; index < m_weatherScenarios.size(); index++)
{
if (m_weatherScenarios[index].getIndex() == CWeatherScenario::ClearSky)
{
// call queued
QTimer::singleShot(0, this, [ = ] { this->setWeatherScenario(index); });
break;
}
}
}
void CWeatherComponent::updateWeatherInformation()
{
setWeatherGrid({});
@@ -151,42 +166,10 @@ namespace BlackGui
{
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 };
position = m_coordinateDialog->getCoordinate();
}
if (CWeatherScenario::isRealWeatherScenario(scenario))
@@ -207,7 +190,7 @@ namespace BlackGui
void CWeatherComponent::weatherGridReceived(const CWeatherGrid &weatherGrid, const CIdentifier &identifier)
{
if (!isMyIdentifier(identifier)) { return; }
ui->lb_Status->setText({});
ui->lbl_Status->setText({});
setWeatherGrid(weatherGrid);
}
@@ -216,8 +199,7 @@ namespace BlackGui
// 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(m_coordinateDialog.data(), &CCoordinateDialog::changedCoordinate, this, &CWeatherComponent::updateWeatherInformation);
connect(ui->cb_UseOwnAcftPosition, &QCheckBox::toggled, this, &CWeatherComponent::toggleUseOwnAircraftPosition);
connect(&m_weatherUpdateTimer, &QTimer::timeout, this, &CWeatherComponent::updateWeatherInformation);
connect(ui->pb_ActivateWeather, &QPushButton::clicked, this, &CWeatherComponent::toggleWeatherActivation);
@@ -227,25 +209,6 @@ namespace BlackGui
connect(sGui->getIContextSimulator(), &IContextSimulator::weatherGridReceived, this, &CWeatherComponent::weatherGridReceived);
}
void CWeatherComponent::setupInputValidators()
{
QRegularExpression reIcaoOrLatitude("^[a-zA-Z]{4}|-?\\d{1,2}[,.]?\\d+$", QRegularExpression::CaseInsensitiveOption);
ui->le_LatOrIcao->setValidator(new QRegularExpressionValidator(reIcaoOrLatitude, this));
QRegularExpression reLongitude("^-?\\d{1,2}[,.]?\\d+$", QRegularExpression::CaseInsensitiveOption);
ui->le_Lon->setValidator(new QRegularExpressionValidator(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();
@@ -256,11 +219,10 @@ namespace BlackGui
void CWeatherComponent::requestWeatherGrid(const CCoordinateGeodetic &position)
{
ui->lb_Status->setText("Loading...");
ui->lbl_Status->setText("Loading...");
CWeatherGrid weatherGrid { { {}, position } };
auto ident = identifier();
sGui->getIContextSimulator()->requestWeatherGrid(weatherGrid, ident);
}
} // namespace
} // namespace

View File

@@ -12,7 +12,8 @@
#ifndef BLACKGUI_WEATHERCOMPONENT_H
#define BLACKGUI_WEATHERCOMPONENT_H
#include "blackgui/components/enablefordockwidgetinfoarea.h"
#include "enablefordockwidgetinfoarea.h"
#include "coordinatedialog.h"
#include "blackgui/blackguiexport.h"
#include "blackcore/actionbind.h"
#include "blackmisc/geo/coordinategeodetic.h"
@@ -35,6 +36,7 @@ namespace Ui { class CWeatherComponent; }
namespace BlackGui
{
class CDockWidgetInfoArea;
namespace Components
{
//! Weather component
@@ -55,30 +57,30 @@ namespace BlackGui
//! \copydoc CEnableForDockWidgetInfoArea::setParentDockWidgetInfoArea
virtual bool setParentDockWidgetInfoArea(BlackGui::CDockWidgetInfoArea *parentDockableWidget) override;
private slots:
void ps_infoAreaTabBarChanged(int index);
private:
void toggleUseOwnAircraftPosition(bool checked);
void infoAreaTabBarChanged(int index);
void toggleUseOwnAircraftPosition(bool useOwnAircraftPosition);
void toggleWeatherActivation();
void showCoordinateDialog();
void setWeatherScenario(int index);
void setCavok();
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;
QScopedPointer<CCoordinateDialog> m_coordinateDialog { new CCoordinateDialog(this) };
QVector<BlackMisc::Weather::CWeatherScenario> m_weatherScenarios;
QTimer m_weatherUpdateTimer { this };
BlackMisc::Geo::CCoordinateGeodetic m_lastOwnAircraftPosition;
BlackMisc::CSetting<BlackMisc::Simulation::Settings::TSelectedWeatherScenario> m_weatherScenarioSetting { this };
BlackCore::CActionBindings m_hotkeyBindings;
BlackCore::CActionBindings m_hotkeyBindings; //!< allow binding of hotkey
bool m_isWeatherActivated = false;
};
} // namespace

View File

@@ -6,8 +6,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>556</height>
<width>280</width>
<height>396</height>
</rect>
</property>
<property name="windowTitle">
@@ -21,7 +21,7 @@
<number>3</number>
</property>
<property name="topMargin">
<number>7</number>
<number>3</number>
</property>
<property name="rightMargin">
<number>3</number>
@@ -53,74 +53,51 @@
<property name="title">
<string>Weather Display</string>
</property>
<layout class="QGridLayout" name="gl_weatherDisplay">
<item row="0" column="0" colspan="2">
<widget class="QCheckBox" name="cb_UseOwnAcftPosition">
<layout class="QVBoxLayout" name="vl_WeatherDisplay">
<property name="leftMargin">
<number>4</number>
</property>
<property name="topMargin">
<number>4</number>
</property>
<property name="rightMargin">
<number>4</number>
</property>
<property name="bottomMargin">
<number>4</number>
</property>
<item>
<widget class="QFrame" name="fr_Misc">
<layout class="QHBoxLayout" name="hl_Misc">
<item>
<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 alignment="Qt::AlignRight">
<widget class="QPushButton" name="pb_Coordinate">
<property name="text">
<string>coordinate</string>
</property>
</widget>
</item>
</layout>
</widget>
</item>
<item>
<widget class="QLabel" name="lbl_Status">
<property name="text">
<string>Use own aircraft position</string>
</property>
<property name="checked">
<bool>true</bool>
<string>&lt;status will go here&gt;</string>
</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>latitude 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>longitude</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">
<item>
<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>

View File

@@ -39,7 +39,7 @@ namespace BlackMisc
//! Properties by index
enum ColumnIndex
{
IndexScenarioIndex = BlackMisc::CPropertyIndex::GlobalIndexCWeatherScenario,
IndexScenarioIndex = CPropertyIndex::GlobalIndexCWeatherScenario,
IndexScenarioName,
IndexScenarioDescription
};
@@ -95,7 +95,6 @@ namespace BlackMisc
BLACK_METAMEMBER(scenarioDescription)
);
};
} //namespace
} // namespace