swift
dbairlineicaocomponent.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 
10 #include "ui_dbairlineicaocomponent.h"
11 
12 #include "core/webdataservices.h"
14 #include "gui/guiapplication.h"
16 #include "gui/views/viewbase.h"
17 
18 using namespace swift::core;
19 using namespace swift::misc::network;
20 using namespace swift::gui;
21 using namespace swift::gui::views;
22 
23 namespace swift::gui::components
24 {
25  CDbAirlineIcaoComponent::CDbAirlineIcaoComponent(QWidget *parent)
26  : COverlayMessagesFrame(parent), ui(new Ui::CDbAirlineIcaoComponent)
27  {
28  Q_ASSERT_X(sGui, Q_FUNC_INFO, "Missing sGui");
29 
30  ui->setupUi(this);
31  this->setViewWithIndicator(ui->tvp_AirlineIcao);
32  ui->tvp_AirlineIcao->setResizeMode(CAirlineIcaoCodeView::ResizingOff);
33  ui->tvp_AirlineIcao->setResizeMode(CAirlineIcaoCodeView::ResizingOff);
34  ui->tvp_AirlineIcao->allowDragDrop(true, false);
35  ui->tvp_AirlineIcao->setFilterWidget(ui->filter_AirlineIcao);
36  ui->tvp_AirlineIcao->menuAddItems(CViewBaseNonTemplate::MenuCopy);
37 
38  connect(ui->tvp_AirlineIcao, &CAirlineIcaoCodeView::requestNewBackendData, this,
39  &CDbAirlineIcaoComponent::onReload);
40  connect(sGui->getWebDataServices(), &CWebDataServices::dataRead, this, &CDbAirlineIcaoComponent::onIcaoRead,
41  Qt::QueuedConnection);
42  connect(sGui->getWebDataServices(), &CWebDataServices::entityDownloadProgress, this,
43  &CDbAirlineIcaoComponent::onEntityDownloadProgress, Qt::QueuedConnection);
44  this->onIcaoRead(CEntityFlags::AirlineIcaoEntity, CEntityFlags::ReadFinished,
46  }
47 
49 
51 
52  void CDbAirlineIcaoComponent::onIcaoRead(CEntityFlags::Entity entity, CEntityFlags::ReadState readState, int count,
53  const QUrl &url)
54  {
55  using namespace std::chrono_literals;
56  Q_UNUSED(count)
57  Q_UNUSED(url)
58 
59  if (!sGui || sGui->isShuttingDown() || !sGui->hasWebDataServices()) { return; }
60  if (!entity.testFlag(CEntityFlags::AirlineIcaoEntity)) { return; }
61 
62  if (CEntityFlags::isFinishedReadState(readState))
63  {
64  this->showOverlayHTMLMessage(QStringLiteral("Updating %1").arg(CEntityFlags::entitiesToString(entity)), 2s);
65  ui->tvp_AirlineIcao->updateContainerMaybeAsync(sGui->getWebDataServices()->getAirlineIcaoCodes());
66  }
67  else
68  {
69  this->showOverlayHTMLMessage(u"Current state: " % CEntityFlags::entitiesToString(entity) % u" " %
70  CEntityFlags::stateToString(readState),
71  10s);
72  }
73  }
74 
75  void CDbAirlineIcaoComponent::onEntityDownloadProgress(CEntityFlags::Entity entity, int logId, int progress,
76  qint64 current, qint64 max, const QUrl &url)
77  {
78  using namespace std::chrono_literals;
79  if (!entity.testFlag(CEntityFlags::AirlineIcaoEntity)) { return; }
80  this->showDownloadProgress(progress, current, max, url, 5s);
81  Q_UNUSED(logId);
82  }
83 
84  void CDbAirlineIcaoComponent::onReload()
85  {
86  if (!sGui || sGui->isShuttingDown() || !sGui->hasWebDataServices()) { return; }
87  sGui->getWebDataServices()->triggerLoadingDirectlyFromDb(CEntityFlags::AirlineIcaoEntity, QDateTime());
88  }
89 } // namespace swift::gui::components
bool hasWebDataServices() const
Web data services available?
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 getAirlineIcaoCodesCount() const
Airline ICAO codes count.
swift::misc::aviation::CAirlineIcaoCodeList getAirlineIcaoCodes() const
Airline 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::CAirlineIcaoCodeView * view() const
Get the view.
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.
GUI related classes.