swift
dbmodelcomponent.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 <QWidget>
9 #include <QtGlobal>
10 
11 #include "ui_dbmodelcomponent.h"
12 
13 #include "core/application.h"
14 #include "core/webdataservices.h"
16 #include "gui/guiapplication.h"
19 #include "gui/views/viewbase.h"
21 
22 using namespace swift::misc;
23 using namespace swift::misc::network;
24 using namespace swift::misc::simulation;
25 using namespace swift::core;
26 using namespace swift::gui::views;
27 using namespace swift::gui::models;
28 
29 namespace swift::gui::components
30 {
31  CDbModelComponent::CDbModelComponent(QWidget *parent)
33  {
34  Q_ASSERT_X(sGui, Q_FUNC_INFO, "Missing sGui");
35  Q_ASSERT_X(sGui->hasWebDataServices(), Q_FUNC_INFO, "Missing web services");
36 
37  ui->setupUi(this);
38  this->setViewWithIndicator(ui->tvp_AircraftModel);
39  ui->tvp_AircraftModel->setAircraftModelMode(CAircraftModelListModel::Database);
40  ui->tvp_AircraftModel->menuAddItems(CAircraftModelView::MenuStashing);
41  ui->tvp_AircraftModel->menuAddItems(CViewBaseNonTemplate::MenuCopy);
42  ui->tvp_AircraftModel->menuRemoveItems(CAircraftModelView::MenuHighlightStashed); // not supported here
43 
44  // configure view
45  ui->tvp_AircraftModel->setFilterWidget(ui->filter_AircraftModelFilter);
46  ui->tvp_AircraftModel->allowDragDrop(true, false);
47 
48  connect(ui->tvp_AircraftModel, &CAircraftModelView::requestNewBackendData, this, &CDbModelComponent::onReload);
49  connect(ui->tvp_AircraftModel, &CAircraftModelView::requestStash, this, &CDbModelComponent::requestStash);
50  connect(sGui, &CGuiApplication::styleSheetsChanged, this, &CDbModelComponent::onStyleSheetChanged,
51  Qt::QueuedConnection);
52  connect(sGui->getWebDataServices(), &CWebDataServices::dataRead, this, &CDbModelComponent::onModelsRead);
53  connect(sGui->getWebDataServices(), &CWebDataServices::entityDownloadProgress, this,
54  &CDbModelComponent::onEntityDownloadProgress, Qt::QueuedConnection);
55  this->onModelsRead(CEntityFlags::ModelEntity, CEntityFlags::ReadFinished,
57  }
58 
60  {
61  // void
62  }
63 
64  bool CDbModelComponent::hasModels() const { return !ui->tvp_AircraftModel->isEmpty(); }
65 
67  {
68  if (!sGui || sGui->isShuttingDown() || !sGui->getWebDataServices()) { return; }
69 
70  QDateTime ts;
71  if (!ui->tvp_AircraftModel->isEmpty())
72  {
73  CAircraftModel model(ui->tvp_AircraftModel->container().latestObject());
74  ts = model.getUtcTimestamp();
75  }
76  sGui->getWebDataServices()->triggerLoadingDirectlyFromDb(CEntityFlags::ModelEntity, ts);
77  }
78 
79  void CDbModelComponent::onModelsRead(CEntityFlags::Entity entity, CEntityFlags::ReadState readState, int count,
80  const QUrl &url)
81  {
82  using namespace std::chrono_literals;
83  Q_UNUSED(count)
84  Q_UNUSED(url)
85 
86  if (!sGui || sGui->isShuttingDown() || !sGui->getWebDataServices()) { return; }
87  if (!entity.testFlag(CEntityFlags::ModelEntity)) { return; }
88 
89  if (CEntityFlags::isFinishedReadState(readState))
90  {
91  this->showOverlayHTMLMessage(QStringLiteral("Updating %1").arg(CEntityFlags::entitiesToString(entity)), 2s);
92  ui->tvp_AircraftModel->updateContainerMaybeAsync(sGui->getWebDataServices()->getModels());
93  }
94  else
95  {
96  this->showOverlayHTMLMessage(u"Current state: " % CEntityFlags::entitiesToString(entity) % u" " %
97  CEntityFlags::stateToString(readState),
98  2s);
99  }
100  }
101 
102  void CDbModelComponent::onReload()
103  {
104  if (!sGui || sGui->isShuttingDown() || !sGui->getWebDataServices()) { return; }
105  sGui->getWebDataServices()->triggerLoadingDirectlyFromDb(CEntityFlags::ModelEntity);
106  }
107 
108  void CDbModelComponent::onStyleSheetChanged()
109  {
110  // code goes here
111  }
112 
113  void CDbModelComponent::onEntityDownloadProgress(CEntityFlags::Entity entity, int logId, int progress,
114  qint64 current, qint64 max, const QUrl &url)
115  {
116  using namespace std::chrono_literals;
117  if (!entity.testFlag(CEntityFlags::ModelEntity)) { return; }
118  this->showDownloadProgress(progress, current, max, url, 5s);
119  Q_UNUSED(logId)
120  }
121 } // namespace swift::gui::components
SWIFT_CORE_EXPORT swift::core::CApplication * sApp
Single instance of application object.
Definition: application.cpp:71
bool hasWebDataServices() const
Web data services available?
bool isShuttingDown() const
Is application shutting down?
CWebDataServices * getWebDataServices() const
Get the web data services.
int getModelsCount() const
Models count.
swift::misc::simulation::CAircraftModelList getModels() const
Models.
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.
void setViewWithIndicator(swift::gui::views::CViewBaseNonTemplate *viewWithIndicator)
Set the corresponding view.
void styleSheetsChanged()
Style sheet changed.
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.
Allows subcomponents to gain access to model component.
void requestStash(const swift::misc::simulation::CAircraftModelList &models)
Request to stash the selected models.
QDateTime getUtcTimestamp() const
Get timestamp.
Aircraft model (used by another pilot, my models on disk)
Definition: aircraftmodel.h:71
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
Models to be used with views, mainly QTableView.
Views, mainly QTableView.
Free functions in swift::misc.