swift
dbaircrafticaocomponent.cpp
1 // SPDX-FileCopyrightText: Copyright (C) 2015 swift Project Community / Contributors
2 // SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-swift-pilot-client-1
3 
5 
6 #include <QDateTime>
7 #include <QStringBuilder>
8 #include <QtGlobal>
9 
11 #include "ui_dbaircrafticaocomponent.h"
12 
13 #include "core/webdataservices.h"
15 #include "gui/guiapplication.h"
17 #include "gui/views/viewbase.h"
18 
19 using namespace swift::core;
20 using namespace swift::misc::network;
21 using namespace swift::misc::aviation;
22 using namespace swift::gui::views;
23 
24 namespace swift::gui::components
25 {
26  CDbAircraftIcaoComponent::CDbAircraftIcaoComponent(QWidget *parent)
27  : COverlayMessagesFrame(parent), ui(new Ui::CDbAircraftIcaoComponent)
28  {
29  ui->setupUi(this);
30  this->setViewWithIndicator(ui->tvp_AircraftIcao);
31  ui->tvp_AircraftIcao->setResizeMode(CAircraftIcaoCodeView::ResizingOff);
32 
33  ui->tvp_AircraftIcao->allowDragDrop(true, false);
34  ui->tvp_AircraftIcao->setFilterWidget(ui->filter_AircraftIcao);
35  ui->tvp_AircraftIcao->menuAddItems(CViewBaseNonTemplate::MenuCopy);
36 
37  connect(ui->tvp_AircraftIcao, &CAircraftIcaoCodeView::requestNewBackendData, this,
38  &CDbAircraftIcaoComponent::onReload);
39  connect(sGui->getWebDataServices(), &CWebDataServices::dataRead, this, &CDbAircraftIcaoComponent::onIcaoRead,
40  Qt::QueuedConnection);
41  connect(sGui->getWebDataServices(), &CWebDataServices::entityDownloadProgress, this,
42  &CDbAircraftIcaoComponent::onEntityDownloadProgress, Qt::QueuedConnection);
43  this->onIcaoRead(CEntityFlags::AircraftIcaoEntity, CEntityFlags::ReadFinished,
45  }
46 
48 
50 
51  void CDbAircraftIcaoComponent::filter(const CAircraftIcaoCode &icao) { ui->filter_AircraftIcao->filter(icao); }
52 
54  {
55  if (!icao.isLoadedFromDb()) { return false; }
56  return ui->tvp_AircraftIcao->selectDbKey(icao.getDbKey());
57  }
58 
59  void CDbAircraftIcaoComponent::onIcaoRead(CEntityFlags::Entity entity, CEntityFlags::ReadState readState, int count)
60  {
61  using namespace std::chrono_literals;
62 
63  Q_UNUSED(count);
64  if (!sGui || sGui->isShuttingDown() || !sGui->getWebDataServices()) { return; }
65  if (!entity.testFlag(CEntityFlags::AircraftIcaoEntity)) { return; }
66 
67  if (CEntityFlags::isFinishedReadState(readState))
68  {
69  this->showOverlayHTMLMessage(QStringLiteral("Updating %1").arg(CEntityFlags::entitiesToString(entity)), 2s);
70  ui->tvp_AircraftIcao->updateContainerMaybeAsync(sGui->getWebDataServices()->getAircraftIcaoCodes());
71  }
72  else
73  {
74  this->showOverlayHTMLMessage(u"Current state: " % CEntityFlags::entitiesToString(entity) % u" " %
75  CEntityFlags::stateToString(readState),
76  10s);
77  }
78  }
79 
80  void CDbAircraftIcaoComponent::onEntityDownloadProgress(CEntityFlags::Entity entity, int logId, int progress,
81  qint64 current, qint64 max, const QUrl &url)
82  {
83  using namespace std::chrono_literals;
84 
85  if (CEntityFlags::AircraftIcaoEntity != entity) { return; }
86  this->showDownloadProgress(progress, current, max, url, 5s);
87  Q_UNUSED(logId);
88  }
89 
90  void CDbAircraftIcaoComponent::onReload()
91  {
92  if (!sGui || sGui->isShuttingDown()) { return; }
93  sGui->getWebDataServices()->triggerLoadingDirectlyFromDb(CEntityFlags::AircraftIcaoEntity, QDateTime());
94  }
95 } // namespace swift::gui::components
bool isShuttingDown() const
Is application shutting down?
CWebDataServices * getWebDataServices() const
Get the web data services.
swift::misc::network::CEntityFlags::Entity triggerLoadingDirectlyFromDb(swift::misc::network::CEntityFlags::Entity whatToRead, const QDateTime &newerThan=QDateTime())
Trigger reload from DB, loads the DB data and bypasses the caches checks and info objects.
int getAircraftIcaoCodesCount() const
Aircraft ICAO codes count.
swift::misc::aviation::CAircraftIcaoCodeList getAircraftIcaoCodes() const
Aircraft ICAO codes.
void setViewWithIndicator(swift::gui::views::CViewBaseNonTemplate *viewWithIndicator)
Set the corresponding view.
bool showOverlayHTMLMessage(const QString &htmlMessage, std::chrono::milliseconds timeout=std::chrono::milliseconds(0))
HTML message.
void showDownloadProgress(int progress, qint64 current, qint64 max, const QUrl &url, std::chrono::milliseconds timeout=std::chrono::milliseconds(0))
Download progress.
Using this class provides a QFrame with the overlay functionality already integrated.
swift::gui::views::CAircraftIcaoCodeView * view() const
Get the view.
bool selectAircraftIcao(const swift::misc::aviation::CAircraftIcaoCode &icao)
Select aircraft ICAO object.
void filter(const swift::misc::aviation::CAircraftIcaoCode &icao)
Filter by ICAO as default.
Value object for ICAO classification.
bool isLoadedFromDb() const
Loaded from DB.
Definition: datastore.cpp:49
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.