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  }
53  QDialog::accept();
54  break;
55  }
56  default:
57  {
58  this->tryToStash();
59  break;
60  }
61  }
62  }
63 
65 
67  {
68  if (percent > 100) { percent = 100; }
69  if (percent < 0) { percent = 0; }
70  ui->pb_StashingProgress->setValue(percent);
71  }
72 
73  views::CAircraftModelView *CDbAutoSimulatorStashingComponent::currentModelView() const
74  {
75  return this->getMappingComponent()->currentModelView();
76  }
77 
78  void CDbAutoSimulatorStashingComponent::initGui()
79  {
80  ui->bb_OkCancel->setStandardButtons(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
81  ui->tvp_StatusMessages->clear();
82  m_state = Idle;
83  this->updateProgressIndicator(0);
84 
85  const QString infoAll = this->getMappingComponent()->getOwnModelsInfoStringFsFamily();
86  ui->le_AllSets->setText(infoAll);
87 
88  if (!this->currentModelView())
89  {
90  const CStatusMessage m(this, CStatusMessage::SeverityError, u"No data for simulator updating");
91  this->addStatusMessage(m);
92  }
93  else
94  {
95  const int selected = this->currentModelView()->selectedRowCount();
96  ui->le_Selected->setText(QString::number(selected));
97  }
98  }
99 
100  void CDbAutoSimulatorStashingComponent::addStatusMessage(const CStatusMessage &msg)
101  {
102  if (msg.isEmpty()) { return; }
103  ui->tvp_StatusMessages->insert(msg);
104  }
105 
106  void CDbAutoSimulatorStashingComponent::addStatusMessages(const CStatusMessageList &msgs)
107  {
108  if (msgs.isEmpty()) { return; }
109  for (const CStatusMessage &msg : msgs) { this->addStatusMessage(msg); }
110  }
111 
112  void CDbAutoSimulatorStashingComponent::addStatusMessage(const CStatusMessage &msg, const CAircraftModel &model)
113  {
114  if (msg.isEmpty()) { return; }
115  if (model.hasModelString())
116  {
117  CStatusMessage prefixMessage(msg);
118  prefixMessage.prependMessage(QString(model.getModelString() + ", " + model.getMembersDbStatus() + ": "));
119  ui->tvp_StatusMessages->insert(prefixMessage);
120  }
121  else { ui->tvp_StatusMessages->insert(msg); }
122  }
123 
124  void CDbAutoSimulatorStashingComponent::tryToStash()
125  {
126  Q_ASSERT_X(this->getMappingComponent(), Q_FUNC_INFO, "Missing mapping component");
127 
128  if (!this->currentModelView()) { return; }
129  m_state = Running;
130  int maxObjectsStashed = -1;
131  if (!ui->le_MaxModelsStashed->text().isEmpty())
132  {
133  bool ok;
134  ui->le_MaxModelsStashed->text().toInt(&ok);
135  if (!ok) { maxObjectsStashed = 100; }
136  }
137 
138  const bool selected = ui->rb_SelectedOnly->isChecked();
139  int ownModelsCount = 0;
140  CStatusMessageList info;
141  if (selected)
142  {
143  static const QString intro("Checking %1 selected models");
144  const CAircraftModelList selectedModels(this->currentModelView()->selectedObjects());
145  ownModelsCount = selectedModels.size();
146  this->addStatusMessage(CStatusMessage(this, CStatusMessage::SeverityInfo, intro.arg(ownModelsCount)));
147  m_modelsToStash =
148  CDatabaseUtils::updateSimulatorForFsFamily(selectedModels, &info, maxObjectsStashed, this, true);
149  }
150  else
151  {
152  const CDbMappingComponent *mappincComponent = this->getMappingComponent();
153  const QSet<CSimulatorInfo> fsFamilySims(CSimulatorInfo::allFsFamilySimulators().asSingleSimulatorSet());
154  static const QString intro("Checking %1 models for %2");
155 
156  // check all own models
157  for (const CSimulatorInfo &simulator : fsFamilySims)
158  {
159  const CAircraftModelList ownModels = mappincComponent->getOwnCachedModels(simulator);
160  const QString sim = simulator.toQString();
161  ownModelsCount += ownModels.size();
162  this->addStatusMessage(
163  CStatusMessage(this, CStatusMessage::SeverityInfo, intro.arg(ownModels.size()).arg(sim)));
164  m_modelsToStash.push_back(
165  CDatabaseUtils::updateSimulatorForFsFamily(ownModels, &info, maxObjectsStashed, this, true));
166  }
167  }
168 
169  const QString result("Tested %1 own models, %2 models should be updated in DB");
170  this->addStatusMessages(info);
171  this->addStatusMessage(
172  CStatusMessage(this, CStatusMessage::SeverityInfo, result.arg(ownModelsCount).arg(m_modelsToStash.size())));
173  m_state = Completed;
174  }
175 } // namespace swift::gui::components
Allows to automatically update models if found in own model set, but already existing for a sibling s...
virtual 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.
QString toQString(bool i18n=false) const
Cast as QString.
Definition: mixinstring.h:76
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:13
GUI related classes.
Free functions in swift::misc.