swift
Loading...
Searching...
No Matches
aircraftcomponent.cpp
1// SPDX-FileCopyrightText: Copyright (C) 2013 swift Project Community / Contributors
2// SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-swift-pilot-client-1
3
5
6#include <QPointer>
7#include <QString>
8#include <QTabBar>
9#include <QTimer>
10
11#include "ui_aircraftcomponent.h"
12
16#include "gui/guiapplication.h"
17#include "gui/guiutility.h"
18#include "gui/infoarea.h"
21#include "gui/views/viewbase.h"
23#include "misc/network/server.h"
24
25using namespace swift::gui;
26using namespace swift::gui::views;
27using namespace swift::gui::models;
28using namespace swift::gui::settings;
29using namespace swift::core;
30using namespace swift::core::context;
31using namespace swift::misc::network;
32using namespace swift::misc::simulation;
33using namespace swift::misc::physical_quantities;
34
36{
38 {
39 ui->setupUi(this);
40
41 Q_ASSERT(sGui->getIContextNetwork());
42 Q_ASSERT(sGui->getIContextSimulator());
43 Q_ASSERT(sGui->getIContextOwnAircraft());
44
45 this->setCurrentIndex(0);
46 this->tabBar()->setExpanding(false);
47 this->tabBar()->setUsesScrollButtons(true);
48 ui->tvp_AircraftInRange->setAircraftMode(CSimulatedAircraftListModel::NetworkMode);
49 ui->tvp_AircraftInRange->configureMenu(true, true, false, true, true, true);
50
51 connect(ui->tvp_AircraftInRange, &CSimulatedAircraftView::modelDataChangedDigest, this,
52 &CAircraftComponent::onRowCountChanged);
53 connect(ui->tvp_AircraftInRange, &CSimulatedAircraftView::requestTextMessageWidget, this,
55 connect(sGui->getIContextNetwork(), &IContextNetwork::connectionStatusChanged, this,
56 &CAircraftComponent::onConnectionStatusChanged, Qt::QueuedConnection);
57 connect(sGui->getIContextOwnAircraft(), &IContextOwnAircraft::movedAircraft, this,
58 &CAircraftComponent::onOwnAircraftMoved, Qt::QueuedConnection);
59 connect(&m_updateTimer, &QTimer::timeout, this, &CAircraftComponent::update);
60
61 this->onSettingsChanged();
62 m_updateTimer.start();
63 }
64
66
68 {
69 Q_ASSERT(ui->tvp_AircraftInRange);
70 return ui->tvp_AircraftInRange->rowCount();
71 }
72
74 {
77 &CAircraftComponent::onInfoAreaTabBarChanged, Qt::QueuedConnection);
78 Q_ASSERT_X(c, Q_FUNC_INFO, "failed connect");
79 Q_ASSERT_X(parentDockableWidget, Q_FUNC_INFO, "missing parent");
80 return c && parentDockableWidget;
81 }
82
84 {
85 if (!sGui || sGui->isShuttingDown())
86 {
87 return;
88 }
89
90 // count < 1 checks if view already has been updated
91 if (sGui->getIContextNetwork()->isConnected())
92 {
93 const bool visible = (this->isVisibleWidget() && this->currentWidget() == ui->tb_AircraftInRange);
94 if (this->countAircraftInView() < 1 || visible)
95 {
96 ui->tvp_AircraftInRange->updateContainerMaybeAsync(sGui->getIContextNetwork()->getAircraftInRange());
97 }
98 }
99
100 m_updateCounter++;
101 }
102
104 {
105 const int tabIndex = static_cast<int>(tab);
106 this->setCurrentIndex(tabIndex);
107 }
108
109 void CAircraftComponent::updateViews()
110 {
111 if (!sGui || sGui->isShuttingDown() || !sGui->getIContextNetwork() || !sGui->getIContextSimulator())
112 {
113 return;
114 }
115 ui->tvp_AircraftInRange->updateContainerMaybeAsync(sGui->getIContextNetwork()->getAircraftInRange());
116 }
117
118 void CAircraftComponent::onInfoAreaTabBarChanged(int index)
119 {
120 // ignore in those cases
121 if (!this->isVisibleWidget()) return;
122 if (this->isParentDockWidgetFloating()) return;
123 if (!sGui->getIContextNetwork()->isConnected()) return;
124
125 // here I know I am the selected widget, update, but keep GUI responsive (hence I use a timer)
126 QPointer<CAircraftComponent> myself(this);
127 QTimer::singleShot(1000, this, [=] {
128 if (!myself)
129 {
130 return;
131 }
132 myself->update();
133 });
134 Q_UNUSED(index)
135 }
136
137 void CAircraftComponent::onRowCountChanged(int count, bool withFilter)
138 {
139 Q_UNUSED(count)
140 Q_UNUSED(withFilter)
141 const int ac = this->indexOf(ui->tb_AircraftInRange);
142 QString acs = this->tabBar()->tabText(ac);
144 }
145
146 void CAircraftComponent::onConnectionStatusChanged(const CConnectionStatus &from, const CConnectionStatus &to)
147 {
148 Q_UNUSED(from)
149 if (to.isDisconnected())
150 {
151 ui->tvp_AircraftInRange->clear();
152 }
153 else if (to.isConnected())
154 {
155 if (sGui && sGui->getIContextNetwork())
156 {
157 const CServer server = sGui->getIContextNetwork()->getConnectedServer();
158 const bool sendFast = server.getFsdSetup().sendInterimPositions();
159 ui->tvp_AircraftInRange->configureMenuFastPositionUpdates(sendFast);
160 }
161 }
162 }
163
164 void CAircraftComponent::onSettingsChanged()
165 {
166 const CViewUpdateSettings settings = m_settings.get();
167 const int ms = settings.getAircraftUpdateTime().toMs();
168 m_updateTimer.setInterval(ms);
169 }
170
171 void CAircraftComponent::onOwnAircraftMoved(const CLength &distance)
172 {
173 Q_UNUSED(distance)
174 this->updateViews();
175 }
176} // namespace swift::gui::components
const context::IContextNetwork * getIContextNetwork() const
Direct access to contexts if a CCoreFacade has been initialized.
void connectionStatusChanged(const swift::misc::network::CConnectionStatus &from, const swift::misc::network::CConnectionStatus &to)
Connection status changed.
virtual swift::misc::network::CServer getConnectedServer() const =0
Server which is connected, if not connected empty default object.
void movedAircraft(const swift::misc::physical_quantities::CLength &distance)
Aircraft has been moved from one location to another (changed scenery).
Specialized class for dock widgets serving as info area.
CInfoArea * getParentInfoArea() const
The parent info area.
bool isParentDockWidgetFloating() const
Is the parent dockable widget floating?
virtual bool setParentDockWidgetInfoArea(CDockWidgetInfoArea *parentDockableWidget)
Corresponding dockable widget in info area.
static QString replaceTabCountValue(const QString &oldName, int count)
Replace count in name such as "stations (4)".
void changedInfoAreaTabBarIndex(int index)
Tab bar changed.
bool setParentDockWidgetInfoArea(swift::gui::CDockWidgetInfoArea *parentDockableWidget)
Corresponding dockable widget in info area.
void requestTextMessageWidget(const swift::misc::aviation::CCallsign &callsign)
Request a text message.
CAircraftComponent(QWidget *parent=nullptr)
Constructor.
void update()
Update aircraft/airport view.
int countAircraftInView() const
Aircraft in range.
const swift::misc::physical_quantities::CTime & getAircraftUpdateTime() const
Get time.
void requestTextMessageWidget(const swift::misc::aviation::CCallsign &callsign)
Request a text message.
void modelDataChangedDigest(int count, bool withFilter)
Model data changed,.
bool isDisconnected() const
Query status.
bool sendInterimPositions() const
FSD setup flags.
Definition fsdsetup.h:103
const CFsdSetup & getFsdSetup() const
Get FSD setup.
Definition server.h:117
int toMs() const
Convenience functions when used with timers etc.
Definition time.cpp:150
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.
Models to be used with views, mainly QTableView.
Views, mainly QTableView.
GUI related classes.
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
QObject * parent() const const
QueuedConnection
void setExpanding(bool enabled)
QString tabText(int index) const const
void setUsesScrollButtons(bool useButtons)
QTabWidget(QWidget *parent)
void setCurrentIndex(int index)
QWidget * currentWidget() const const
int indexOf(const QWidget *w) const const
QTabBar * tabBar() const const
void timeout()
QWidget(QWidget *parent, Qt::WindowFlags f)