swift
dbautosimulatorstashingcomponent.cpp
1 // SPDX-FileCopyrightText: Copyright (C) 2016 swift Project Community / Contributors
2 // SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-swift-pilot-client-1
3 
5 
6 #include <QIntValidator>
7 
8 #include "ui_dbautosimulatorstashingcomponent.h"
9 
10 #include "core/db/databaseutils.h"
12 
13 using namespace swift::gui;
14 using namespace swift::core::db;
15 using namespace swift::misc;
16 using namespace swift::misc::simulation;
17 
18 namespace swift::gui::components
19 {
20  CDbAutoSimulatorStashingComponent::CDbAutoSimulatorStashingComponent(QWidget *parent)
21  : QDialog(parent, Qt::WindowSystemMenuHint | Qt::WindowTitleHint),
22  CDbMappingComponentAware(qobject_cast<CDbMappingComponent *>(parent)),
24  {
25  ui->setupUi(this);
26  ui->le_MaxModelsStashed->setValidator(new QIntValidator(this));
27  ui->tvp_StatusMessages->setMode(swift::gui::models::CStatusMessageListModel::Simplified);
28  ui->le_MaxModelsStashed->setText("100");
29  }
30 
32  {
33  this->initGui();
34  return QDialog::exec();
35  }
36 
38  {
39  switch (m_state)
40  {
41  case Running: return;
42  case Completed:
43  {
44  if (!m_modelsToStash.isEmpty())
45  {
46  // this removes previously stashed models
47  this->getMappingComponent()->replaceStashedModelsUnvalidated(m_modelsToStash);
48  const CStatusMessage stashedMsg(this, CStatusMessage::SeverityInfo,
49  QStringLiteral("Stashed %1 models").arg(m_modelsToStash.size()));
50  this->addStatusMessage(stashedMsg);
51  m_modelsToStash.clear();
52  }
54  break;
55  }
56  default:
57  {
58  this->tryToStash();
59  break;
60  }
61  }
62  }
63 
65 
67  {
68  percent = std::clamp(percent, 0, 100);
69  ui->pb_StashingProgress->setValue(percent);
70  }
71 
72  views::CAircraftModelView *CDbAutoSimulatorStashingComponent::currentModelView() const
73  {
74  return this->getMappingComponent()->currentModelView();
75  }
76 
77  void CDbAutoSimulatorStashingComponent::initGui()
78  {
79  ui->bb_OkCancel->setStandardButtons(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
80  ui->tvp_StatusMessages->clear();
81  m_state = Idle;
82  this->updateProgressIndicator(0);
83 
85  ui->le_AllSets->setText(infoAll);
86 
87  if (!this->currentModelView())
88  {
89  const CStatusMessage m(this, CStatusMessage::SeverityError, u"No data for simulator updating");
90  this->addStatusMessage(m);
91  }
92  else
93  {
94  const int selected = this->currentModelView()->selectedRowCount();
95  ui->le_Selected->setText(QString::number(selected));
96  }
97  }
98 
99  void CDbAutoSimulatorStashingComponent::addStatusMessage(const CStatusMessage &msg)
100  {
101  if (msg.isEmpty()) { return; }
102  ui->tvp_StatusMessages->insert(msg);
103  }
104 
105  void CDbAutoSimulatorStashingComponent::addStatusMessages(const CStatusMessageList &msgs)
106  {
107  if (msgs.isEmpty()) { return; }
108  for (const CStatusMessage &msg : msgs) { this->addStatusMessage(msg); }
109  }
110 
111  void CDbAutoSimulatorStashingComponent::addStatusMessage(const CStatusMessage &msg, const CAircraftModel &model)
112  {
113  if (msg.isEmpty()) { return; }
114  if (model.hasModelString())
115  {
116  CStatusMessage prefixMessage(msg);
117  prefixMessage.prependMessage(QString(model.getModelString() + ", " + model.getMembersDbStatus() + ": "));
118  ui->tvp_StatusMessages->insert(prefixMessage);
119  }
120  else { ui->tvp_StatusMessages->insert(msg); }
121  }
122 
123  void CDbAutoSimulatorStashingComponent::tryToStash()
124  {
125  Q_ASSERT_X(this->getMappingComponent(), Q_FUNC_INFO, "Missing mapping component");
126 
127  if (!this->currentModelView()) { return; }
128  m_state = Running;
129  int maxObjectsStashed = -1;
130  if (!ui->le_MaxModelsStashed->text().isEmpty())
131  {
132  bool ok {};
133  ui->le_MaxModelsStashed->text().toInt(&ok);
134  if (!ok) { maxObjectsStashed = 100; }
135  }
136 
137  const bool selected = ui->rb_SelectedOnly->isChecked();
138  int ownModelsCount = 0;
139  CStatusMessageList info;
140  if (selected)
141  {
142  static const QString intro("Checking %1 selected models");
143  const CAircraftModelList selectedModels(this->currentModelView()->selectedObjects());
144  ownModelsCount = selectedModels.size();
145  this->addStatusMessage(CStatusMessage(this, CStatusMessage::SeverityInfo, intro.arg(ownModelsCount)));
146  m_modelsToStash =
147  CDatabaseUtils::updateSimulatorForFsFamily(selectedModels, &info, maxObjectsStashed, this, true);
148  }
149  else
150  {
151  const CDbMappingComponent *mappincComponent = this->getMappingComponent();
152  const QSet<CSimulatorInfo> fsFamilySims(CSimulatorInfo::allFsFamilySimulators().asSingleSimulatorSet());
153  static const QString intro("Checking %1 models for %2");
154 
155  // check all own models
156  for (const CSimulatorInfo &simulator : fsFamilySims)
157  {
158  const CAircraftModelList ownModels = mappincComponent->getOwnCachedModels(simulator);
159  const QString sim = simulator.toQString();
160  ownModelsCount += ownModels.size();
161  this->addStatusMessage(
162  CStatusMessage(this, CStatusMessage::SeverityInfo, intro.arg(ownModels.size()).arg(sim)));
163  m_modelsToStash.push_back(
164  CDatabaseUtils::updateSimulatorForFsFamily(ownModels, &info, maxObjectsStashed, this, true));
165  }
166  }
167 
168  const QString result("Tested %1 own models, %2 models should be updated in DB");
169  this->addStatusMessages(info);
170  this->addStatusMessage(
171  CStatusMessage(this, CStatusMessage::SeverityInfo, result.arg(ownModelsCount).arg(m_modelsToStash.size())));
172  m_state = Completed;
173  }
174 } // namespace swift::gui::components
Allows to automatically update models if found in own model set, but already existing for a sibling s...
void updateProgressIndicator(int percent)
Update the progress indicator 0..100.
Allows subcomponents to gain access to model component.
CDbMappingComponent * getMappingComponent() const
Get the mapping component.
void replaceStashedModelsUnvalidated(const swift::misc::simulation::CAircraftModelList &models) const
Replace models, no validation.
views::CAircraftModelView * currentModelView() const
Current model view.
QString getOwnModelsInfoStringFsFamily() const
Info string without XPlane (FSX,P3D, FS9)
int selectedRowCount() const
Number of selected rows.
bool isEmpty() const
Message empty.
size_type size() const
Returns number of elements in the sequence.
Definition: sequence.h:273
void push_back(const T &value)
Appends an element at the end of the sequence.
Definition: sequence.h:305
void clear()
Removes all elements in the sequence.
Definition: sequence.h:288
bool isEmpty() const
Synonym for empty.
Definition: sequence.h:285
Streamable status message, e.g.
Status messages, e.g. from Core -> GUI.
Aircraft model (used by another pilot, my models on disk)
Definition: aircraftmodel.h:71
QString getMembersDbStatus() const
Info, which members (Livery, Aircraft ICAO, ...) are already based on DB data.
const QString & getModelString() const
Model key, either queried or loaded from simulator model.
bool hasModelString() const
Non empty model string?
Value object encapsulating a list of aircraft models.
Simple hardcoded info about the corresponding simulator.
Definition: simulatorinfo.h:41
Classes interacting with the swift database (aka "datastore").
High level reusable GUI components.
Definition: aboutdialog.cpp:14
GUI related classes.
Free functions in swift::misc.
virtual void accept()
virtual int exec()
int result() const const
QString number(double n, char format, int precision)