swift
usercomponent.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 <QString>
7 #include <QTabBar>
8 
9 #include "ui_usercomponent.h"
10 
12 #include "gui/guiapplication.h"
13 #include "gui/guiutility.h"
14 #include "gui/views/clientview.h"
15 #include "gui/views/userview.h"
17 #include "misc/network/userlist.h"
18 
19 using namespace swift::misc::network;
20 using namespace swift::gui;
21 using namespace swift::gui::views;
22 using namespace swift::gui::settings;
23 using namespace swift::core;
24 using namespace swift::core::context;
25 
26 namespace swift::gui::components
27 {
28  CUserComponent::CUserComponent(QWidget *parent) : QTabWidget(parent), ui(new Ui::CUserComponent)
29  {
30  ui->setupUi(this);
31  this->setCurrentIndex(0);
32  this->tabBar()->setExpanding(false);
33  this->tabBar()->setUsesScrollButtons(true);
34  connect(ui->tvp_AllUsers, &CUserView::modelDataChangedDigest, this, &CUserComponent::onCountChanged);
35  connect(ui->tvp_AllUsers, &CUserView::requestTextMessageWidget, this,
37  connect(ui->tvp_Clients, &CClientView::modelDataChangedDigest, this, &CUserComponent::onCountChanged);
38  connect(sGui->getIContextNetwork(), &IContextNetwork::connectionStatusChanged, this,
39  &CUserComponent::onConnectionStatusChanged);
40  connect(&m_updateTimer, &QTimer::timeout, this, &CUserComponent::update);
41  this->onSettingsChanged();
42  }
43 
45 
47  {
48  Q_ASSERT(ui->tvp_Clients);
49  return ui->tvp_Clients->rowCount();
50  }
51 
53  {
54  Q_ASSERT(ui->tvp_AllUsers);
55  return ui->tvp_AllUsers->rowCount();
56  }
57 
59  {
60  if (!sGui || sGui->isShuttingDown() || !sGui->getIContextNetwork()) { return; }
61  Q_ASSERT(ui->tvp_AllUsers);
62  Q_ASSERT(ui->tvp_Clients);
63 
65  {
66  bool withData = countUsers() > 0 || countClients() > 0;
67  if (withData && !isVisibleWidget())
68  {
69  // Skip update, invisible
70  return;
71  }
72 
73  // load data
74  const CUserList users = sGui->getIContextNetwork()->getUsers();
75  ui->tvp_AllUsers->updateContainer(users);
76  ui->tvp_Clients->updateContainer(sGui->getIContextNetwork()->getClientsForCallsigns(users.getCallsigns()));
77  }
78  }
79 
80  void CUserComponent::onCountChanged(int count, bool withFilter)
81  {
82  Q_UNUSED(count);
83  Q_UNUSED(withFilter);
84  int iu = this->indexOf(ui->tb_AllUsers);
85  int ic = this->indexOf(ui->tb_Clients);
86  QString u = this->tabBar()->tabText(iu);
87  QString c = this->tabBar()->tabText(ic);
90  this->tabBar()->setTabText(iu, u);
91  this->tabBar()->setTabText(ic, c);
92  }
93 
94  void CUserComponent::onConnectionStatusChanged(const CConnectionStatus &from, const CConnectionStatus &to)
95  {
96  Q_UNUSED(from);
97  if (to.isDisconnected())
98  {
99  ui->tvp_AllUsers->clear();
100  ui->tvp_Clients->clear();
101  m_updateTimer.stop();
102  }
103  else if (to.isConnected()) { m_updateTimer.start(); }
104  }
105 
106  void CUserComponent::onSettingsChanged()
107  {
108  const CViewUpdateSettings settings = m_settings.get();
109  const int ms = settings.getAtcUpdateTime().toMs();
110  m_updateTimer.setInterval(ms);
111  }
112 } // namespace swift::gui::components
const context::IContextNetwork * getIContextNetwork() const
Direct access to contexts if a CCoreFacade has been initialized.
bool isShuttingDown() const
Is application shutting down?
virtual swift::misc::network::CUserList getUsers() const =0
Get all users.
virtual swift::misc::network::CClientList getClientsForCallsigns(const swift::misc::aviation::CCallsignSet &callsigns) const =0
Clients for given callsign, e.g. to test/fetch direct aircraft model.
virtual bool isConnected() const =0
Network connected?
static QString replaceTabCountValue(const QString &oldName, int count)
Replace count in name such as "stations (4)".
Definition: guiutility.cpp:522
User componenet (users, clients)
Definition: usercomponent.h:29
void requestTextMessageWidget(const swift::misc::aviation::CCallsign &callsign)
Request a text message.
int countClients() const
Number of clients.
int countUsers() const
Number of users.
Settings about view update rates.
const swift::misc::physical_quantities::CTime & getAtcUpdateTime() const
Get time.
T get() const
Get a copy of the current value.
Definition: valuecache.h:408
swift::misc::aviation::CCallsignSet getCallsigns() const
All callsigns.
Value object encapsulating information about a connection status.
bool isConnected() const
Query status.
bool isDisconnected() const
Query status.
Value object encapsulating a list of voice rooms.
Definition: userlist.h:26
int toMs() const
Convenience functions when used with timers etc.
Definition: time.cpp:117
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:14
Views, mainly QTableView.
GUI related classes.
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
void setExpanding(bool enabled)
void setTabText(int index, const QString &text)
QString tabText(int index) const const
void setUsesScrollButtons(bool useButtons)
void setCurrentIndex(int index)
int indexOf(const QWidget *w) const const
QTabBar * tabBar() const const
void setInterval(int msec)
void start()
void stop()
void timeout()