swift
Loading...
Searching...
No Matches
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"
16#include "gui/guiapplication.h"
19#include "gui/views/viewbase.h"
21
22using namespace swift::misc;
23using namespace swift::misc::network;
24using namespace swift::misc::simulation;
25using namespace swift::core;
26using namespace swift::gui::views;
27using namespace swift::gui::models;
28
30{
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);
50 connect(sGui, &CGuiApplication::styleSheetsChanged, this, &CDbModelComponent::onStyleSheetChanged,
52 connect(sGui->getWebDataServices(), &CWebDataServices::dataRead, this, &CDbModelComponent::onModelsRead);
53 connect(sGui->getWebDataServices(), &CWebDataServices::entityDownloadProgress, this,
54 &CDbModelComponent::onEntityDownloadProgress, Qt::QueuedConnection);
56 sApp->getWebDataServices()->getModelsCount(), {});
57 }
58
60
61 bool CDbModelComponent::hasModels() const { return !ui->tvp_AircraftModel->isEmpty(); }
62
64 {
65 if (!sGui || sGui->isShuttingDown() || !sGui->getWebDataServices())
66 {
67 return;
68 }
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())
87 {
88 return;
89 }
90 if (!entity.testFlag(CEntityFlags::ModelEntity))
91 {
92 return;
93 }
94
96 {
97 this->showOverlayHTMLMessage(QStringLiteral("Updating %1").arg(CEntityFlags::entitiesToString(entity)), 2s);
98 ui->tvp_AircraftModel->updateContainerMaybeAsync(sGui->getWebDataServices()->getModels());
99 }
100 else
101 {
102 this->showOverlayHTMLMessage(u"Current state: " % CEntityFlags::entitiesToString(entity) % u" " %
104 2s);
105 }
106 }
107
108 void CDbModelComponent::onReload()
109 {
111 {
112 return;
113 }
115 }
116
117 void CDbModelComponent::onStyleSheetChanged()
118 {
119 // code goes here
120 }
121
122 void CDbModelComponent::onEntityDownloadProgress(CEntityFlags::Entity entity, int logId, int progress,
123 qint64 current, qint64 max, const QUrl &url)
124 {
125 using namespace std::chrono_literals;
126 if (!entity.testFlag(CEntityFlags::ModelEntity))
127 {
128 return;
129 }
130 this->showDownloadProgress(progress, current, max, url, 5s);
131 Q_UNUSED(logId)
132 }
133} // namespace swift::gui::components
bool isShuttingDown() const
Is application shutting down?
CWebDataServices * getWebDataServices() const
Get the web data services.
void entityDownloadProgress(swift::misc::network::CEntityFlags::Entity entity, int logId, int progress, qint64 current, qint64 max, const QUrl &url)
Download progress for an entity.
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 dataRead(swift::misc::network::CEntityFlags::Entity entity, swift::misc::network::CEntityFlags::ReadState state, int number, const QUrl &url)
Combined read signal.
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))
void showDownloadProgress(int progress, qint64 current, qint64 max, const QUrl &url, std::chrono::milliseconds timeout=std::chrono::milliseconds(0))
COverlayMessagesFrame(QWidget *parent=nullptr, Qt::WindowFlags f=Qt::WindowFlags())
Constructor.
CDbModelComponent(QWidget *parent=nullptr)
Constructor.
void requestStash(const swift::misc::simulation::CAircraftModelList &models)
Request to stash the selected models.
void requestStash(const swift::misc::simulation::CAircraftModelList &models)
Request to stash if applicable.
void requestNewBackendData()
Load data from backend (where it makes sense).
@ MenuHighlightStashed
highlight stashed models
Definition viewbase.h:142
@ MenuCopy
copy (for copy/paste)
Definition viewbase.h:132
QDateTime getUtcTimestamp() const
Get timestamp.
static bool isFinishedReadState(ReadState state)
Any finished state.
static const QString & stateToString(ReadState flag)
Convert to string.
static QString entitiesToString(CEntityFlags::Entity entities, const QString &separator=", ")
Entities to string list.
Aircraft model (used by another pilot, my models on disk).
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.
Free functions in swift::misc.
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
QObject * parent() const const
QueuedConnection
QWidget(QWidget *parent, Qt::WindowFlags f)