swift
modelbrowsercomponent.cpp
1 // SPDX-FileCopyrightText: Copyright (C) 2019 swift Project Community / Contributors
2 // SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-swift-pilot-client-1
3 
5 
6 #include "ui_modelbrowsercomponent.h"
7 
10 #include "gui/guiapplication.h"
11 #include "misc/aviation/heading.h"
12 #include "misc/pq/angle.h"
13 #include "misc/pq/speed.h"
14 
15 using namespace swift::misc;
16 using namespace swift::misc::aviation;
17 using namespace swift::misc::simulation;
18 using namespace swift::misc::physical_quantities;
19 using namespace swift::misc::geo;
20 using namespace swift::gui::editors;
21 using namespace swift::gui::views;
22 using namespace swift::core;
23 using namespace swift::core::context;
24 
25 namespace swift::gui::components
26 {
27  CModelBrowserComponent::CModelBrowserComponent(QWidget *parent) : QFrame(parent), ui(new Ui::CModelBrowserComponent)
28  {
29  ui->setupUi(this);
30  ui->tw_Tab->setCurrentIndex(0);
31  ui->tvp_AircraftModels->acceptClickSelection(false);
32  ui->tvp_AircraftModels->acceptDoubleClickSelection(true);
33 
34  const CLength relDistance(40.0, CLengthUnit::m());
35  ui->editor_RelativePosition->setDistance(relDistance);
36 
37  connect(ui->pb_SetRelativePosition, &QPushButton::released, this,
38  &CModelBrowserComponent::onSetRelativePosition, Qt::QueuedConnection);
39  connect(ui->pb_LoadModelSet, &QPushButton::released, this, &CModelBrowserComponent::loadModelSet,
40  Qt::QueuedConnection);
41  connect(ui->pb_Display, &QPushButton::released, this, &CModelBrowserComponent::display, Qt::QueuedConnection);
42  connect(ui->pb_Remove, &QPushButton::released, this, &CModelBrowserComponent::remove, Qt::QueuedConnection);
43  connect(ui->editor_Coordinate, &CCoordinateForm::changedCoordinate, this,
44  &CModelBrowserComponent::onSetAbsolutePosition, Qt::QueuedConnection);
45  connect(ui->editor_Pbh, &CPbhsForm::changeValues, this, &CModelBrowserComponent::onSetPBH,
46  Qt::QueuedConnection);
47  connect(ui->editor_AircraftParts, &CAircraftPartsForm::changeAircraftParts, this,
48  &CModelBrowserComponent::onSetParts, Qt::QueuedConnection);
49  connect(ui->tvp_AircraftModels, &CAircraftModelView::objectDoubleClicked, this,
50  &CModelBrowserComponent::onModelDblClicked, Qt::QueuedConnection);
51 
52  connect(ui->cb_OverrideCG, &QCheckBox::clicked, this, &CModelBrowserComponent::onCGChecked);
53  connect(ui->cb_UseCG, &QCheckBox::clicked, this, &CModelBrowserComponent::onCGChecked);
54 
55  this->loadModelSet();
56  this->onSetRelativePosition();
57  }
58 
60  {
61  // void
62  }
63 
64  void CModelBrowserComponent::close() { this->remove(); }
65 
66  void CModelBrowserComponent::onSetRelativePosition()
67  {
68  if (!this->hasContexts()) { return; }
70  ui->editor_RelativePosition->setOriginCoordinate(s);
71  const CCoordinateGeodetic rel = ui->editor_RelativePosition->getRelativeCoordinate(s.getHeading());
72  ui->editor_RelativePosition->displayInfo(rel);
73  m_situation.setPosition(rel);
74  }
75 
76  void CModelBrowserComponent::onSetAbsolutePosition()
77  {
78  m_situation.setPosition(ui->editor_Coordinate->getCoordinate());
79  }
80 
81  void CModelBrowserComponent::onSetPBH()
82  {
83  ui->cb_UsePBH->setChecked(true);
84  if (!this->hasContexts()) { return; }
85  this->updatePartsAndPBH(true, false);
86  }
87 
88  void CModelBrowserComponent::onSetParts()
89  {
90  ui->cb_UseParts->setChecked(true);
91  if (!this->hasContexts()) { return; }
92  this->updatePartsAndPBH(false, true);
93  }
94 
95  CAircraftParts CModelBrowserComponent::getParts() const
96  {
97  return ui->editor_AircraftParts->getAircraftPartsFromGui();
98  }
99 
100  void CModelBrowserComponent::updatePartsAndPBH(bool setPbh, bool setParts)
101  {
102  if (!this->hasContexts()) { return; }
103  CAircraftParts parts = CAircraftParts::null();
104  if (setParts || ui->cb_UseParts->isChecked()) { parts = this->getParts(); }
105 
106  if (setPbh || ui->cb_UsePBH->isChecked()) { ui->editor_Pbh->updateSituation(m_situation); }
107  else { m_situation.setZeroPBHandGs(); }
108 
109  sGui->getISimulator()->testSendSituationAndParts(ISimulator::getTestCallsign(), m_situation, parts);
110  }
111 
112  void CModelBrowserComponent::fetchSimulatorValues()
113  {
114  if (!this->hasContexts()) { return; }
115  }
116 
117  void CModelBrowserComponent::onModelDblClicked(const CVariant &object)
118  {
119  Q_UNUSED(object);
120  this->display();
121  }
122 
123  void CModelBrowserComponent::display()
124  {
125  if (!this->hasContexts()) { return; }
126  CAircraftModel model = ui->tvp_AircraftModels->selectedObject();
127  if (!model.hasModelString()) { return; }
128 
129  this->remove(); // only 1 model at a time
130 
131  if (ui->cb_OverrideCG->isChecked())
132  {
133  CLength cg;
134  cg.parseFromString(ui->le_CG->text(), CPqString::SeparatorBestGuess);
135  if (!cg.isNull()) { model.setCG(cg); }
136  }
137  else { ui->le_CG->setText(model.getCG().valueRoundedWithUnit(CLengthUnit::ft(), 1)); }
138 
139  m_situation.setZeroPBHandGs();
140  if (ui->cb_UsePBH->isChecked()) { ui->editor_Pbh->updateSituation(m_situation); }
141 
142  if (ui->cb_UseParts->isChecked())
143  {
144  const CAircraftParts parts = ui->editor_AircraftParts->getAircraftPartsFromGui();
145  if (!parts.isNull()) { m_aircraft.setParts(parts); }
146  }
147 
148  m_aircraft.setModel(model);
149  m_aircraft.setCallsign(ISimulator::getTestCallsign());
150  m_aircraft.setSituation(m_situation);
151 
152  ui->le_ModelInfo->setText(model.getModelStringAndDbKey());
153  ui->le_Info->setText(m_situation.toQString(true));
154 
156  sGui->getIContextSimulator()->testRemoteAircraft(m_aircraft, true);
157  }
158 
159  void CModelBrowserComponent::remove()
160  {
161  ui->le_ModelInfo->clear();
162  ui->le_Info->clear();
163 
164  if (!this->hasContexts()) { return; }
165  if (!m_aircraft.hasCallsign()) { return; }
166  sGui->getIContextSimulator()->testRemoteAircraft(m_aircraft, false);
167  m_aircraft = CSimulatedAircraft(); // reset
168  }
169 
170  void CModelBrowserComponent::loadModelSet()
171  {
172  if (!this->hasContexts()) { return; }
173 
175  ui->tvp_AircraftModels->updateContainerMaybeAsync(modelSet);
177  ui->lbl_ModelSetInfo->setText(QStringLiteral("'%1' model set with %2 models").arg(sim).arg(modelSet.sizeInt()));
178  }
179 
180  bool CModelBrowserComponent::hasContexts() const
181  {
182  if (!sGui || sGui->isShuttingDown()) { return false; }
183  if (!sGui->getIContextOwnAircraft()) { return false; }
184  if (!sGui->getIContextSimulator()) { return false; }
185  return true;
186  }
187 
188  void CModelBrowserComponent::onCGChecked(bool checked)
189  {
190  if (ui->cb_OverrideCG->isChecked() != checked) { ui->cb_OverrideCG->setChecked(true); }
191  if (ui->cb_UseCG->isChecked() != checked) { ui->cb_UseCG->setChecked(true); }
192  }
193 } // namespace swift::gui::components
const context::IContextOwnAircraft * getIContextOwnAircraft() const
Direct access to contexts if a CCoreFacade has been initialized.
QPointer< ISimulator > getISimulator() const
The simulator plugin, if available.
bool isShuttingDown() const
Is application shutting down?
const context::IContextSimulator * getIContextSimulator() const
Direct access to contexts if a CCoreFacade has been initialized.
virtual swift::misc::aviation::CAircraftSituation getOwnAircraftSituation() const =0
Get own aircraft.
virtual swift::misc::simulation::CSimulatorPluginInfo getSimulatorPluginInfo() const =0
Simulator info, currently loaded plugin.
virtual swift::misc::simulation::CAircraftModelList getModelSet() const =0
Installed models in simulator eco system.
virtual bool testRemoteAircraft(const swift::misc::simulation::CSimulatedAircraft &aircraft, bool add)=0
Test a remote aircraft.
virtual bool requestElevationBySituation(const swift::misc::aviation::CAircraftSituation &situation)=0
Request elevation, there is no guarantee the requested elevation will be available in the provider.
int sizeInt() const
Avoid compiler warnings when using with int.
Definition: sequence.h:276
Wrapper around QVariant which provides transparent access to CValueObject methods of the contained ob...
Definition: variant.h:66
Value object encapsulating information of aircraft's parts.
Definition: aircraftparts.h:26
bool isNull() const
NULL parts object?
Value object encapsulating information of an aircraft's situation.
const CHeading & getHeading() const
Get heading.
void setZeroPBHandGs()
Set PBH and GS values to 0 (zero)
void setPosition(const geo::CCoordinateGeodetic &position)
Set position.
QString toQString(bool i18n=false) const
Cast as QString.
Definition: mixinstring.h:76
Physical unit length (length)
Definition: length.h:18
void parseFromString(const QString &value)
Parse value from string.
QString valueRoundedWithUnit(const MU &unit, int digits=-1, bool withGroupSeparator=false, bool i18n=false) const
Value to QString with the given unit, e.g. "5.00m".
Aircraft model (used by another pilot, my models on disk)
Definition: aircraftmodel.h:71
const physical_quantities::CLength & getCG() const
Get center of gravity.
void setCG(const physical_quantities::CLength &cg)
Get center of gravity.
QString getModelStringAndDbKey() const
Model string and DB key (if available)
bool hasModelString() const
Non empty model string?
Value object encapsulating a list of aircraft models.
Comprehensive information of an aircraft.
void setCallsign(const aviation::CCallsign &callsign)
Set callsign.
void setModel(const CAircraftModel &model)
Set model.
void setSituation(const aviation::CAircraftSituation &situation)
Set situation. Won't overwrite the velocity unless it held the default value.
bool hasCallsign() const
Callsign not empty, no further checks.
void setParts(const aviation::CAircraftParts &parts)
Set aircraft parts.
const CSimulatorInfo & getSimulatorInfo() const
Simulator info object.
SWIFT_GUI_EXPORT swift::gui::CGuiApplication * sGui
Single instance of GUI application object.
Backend services of the swift project, like dealing with the network or the simulators.
Definition: actionbind.cpp:7
High level reusable GUI components.
Definition: aboutdialog.cpp:13
Views, mainly QTableView.
Free functions in swift::misc.