swift
dbownmodelsetformdialog.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 <QPushButton>
7 #include <QString>
8 #include <QWidget>
9 #include <QtGlobal>
10 
11 #include "ui_dbownmodelsetformdialog.h"
12 
13 #include "core/modelsetbuilder.h"
16 #include "gui/guiapplication.h"
17 #include "misc/logcategories.h"
19 
20 using namespace swift::misc;
21 using namespace swift::misc::simulation;
22 using namespace swift::core;
23 using namespace swift::gui::editors;
24 
25 namespace swift::gui::components
26 {
27  const QStringList &CDbOwnModelSetFormDialog::getLogCategories()
28  {
29  static const QStringList cats({ "swift.ownmodelset", CLogCategories::guiComponent() });
30  return cats;
31  }
32 
33  CDbOwnModelSetFormDialog::CDbOwnModelSetFormDialog(QWidget *parent)
34  : QDialog(parent), CDbMappingComponentAware(parent), ui(new Ui::CDbOwnModelSetFormDialog)
35  {
36  ui->setupUi(this);
37  connect(ui->pb_Cancel, &QPushButton::clicked, this, &CDbOwnModelSetFormDialog::buttonClicked);
38  connect(ui->pb_Ok, &QPushButton::clicked, this, &CDbOwnModelSetFormDialog::buttonClicked);
39  connect(ui->form_OwnModelSet, &COwnModelSetForm::simulatorChanged, this,
40  &CDbOwnModelSetFormDialog::simulatorChanged);
41  }
42 
44 
46  {
47  m_simulatorInfo = this->getMappingComponent()->getOwnModelsSimulator();
48  Q_ASSERT_X(m_simulatorInfo.isSingleSimulator(), Q_FUNC_INFO, "Need single simulator");
49  ui->form_OwnModelSet->setSimulator(m_simulatorInfo);
50  ui->form_OwnModelSet->reloadData();
51  m_modelSet = this->getMappingComponent()->getOwnModelSet();
52  }
53 
55  {
56  Q_ASSERT_X(this->getMappingComponent(), Q_FUNC_INFO, "missing mapping component");
57  const CSimulatorInfo sim(this->getMappingComponent()->getOwnModelsSimulator());
58  Q_ASSERT_X(sim.isSingleSimulator(), Q_FUNC_INFO, "need single simulator");
59  this->setSimulator(sim);
60  this->checkData();
61  return QDialog::exec();
62  }
63 
64  void CDbOwnModelSetFormDialog::buttonClicked()
65  {
66  const QObject *sender = QObject::sender();
67  if (sender == ui->pb_Cancel) { this->reject(); }
68  else if (sender == ui->pb_Ok)
69  {
70  m_modelSet = this->buildSet(m_simulatorInfo, m_modelSet);
71  this->accept();
72  }
73  }
74 
75  void CDbOwnModelSetFormDialog::simulatorChanged(const CSimulatorInfo &simulator)
76  {
77  Q_ASSERT_X(simulator.isSingleSimulator(), Q_FUNC_INFO, "Need single simulator");
78  this->setSimulator(simulator);
79 
80  // if possible set in mappings component
81  if (this->getMappingComponent())
82  {
83  this->getMappingComponent()->setOwnModelsSimulator(simulator);
84  this->getMappingComponent()->setOwnModelSetSimulator(simulator);
85  this->checkData();
86  }
87  }
88 
89  bool CDbOwnModelSetFormDialog::checkData()
90  {
91  // models
92  Q_ASSERT_X(m_simulatorInfo.isSingleSimulator(), Q_FUNC_INFO, "Need single simulator");
93  const int c = this->getMappingComponent()->getOwnModelsCount();
94  if (c < 1)
95  {
96  const CStatusMessage m = CStatusMessage(this).error(u"No models for '%1'")
97  << m_simulatorInfo.toQString(true);
98  ui->form_OwnModelSet->showOverlayMessage(m);
99  return false;
100  }
101  return true;
102  }
103 
104  void CDbOwnModelSetFormDialog::setSimulator(const CSimulatorInfo &simulator)
105  {
106  // Ref T663, avoid ASSERT in some weird cases
107  if (simulator.isSingleSimulator()) { m_simulatorInfo = simulator; }
108  else
109  {
110  const CSimulatorInfo resetSim =
111  m_simulatorInfo.isSingleSimulator() ? m_simulatorInfo : CSimulatorInfo::guessDefaultSimulator();
112  const QString msg =
113  QStringLiteral("Set invalid simulator, continue to use '%1'").arg(resetSim.toQString(true));
115  m_simulatorInfo = resetSim;
116  }
117  ui->form_OwnModelSet->setSimulator(m_simulatorInfo);
118  this->setWindowTitle("Create model set for " + m_simulatorInfo.toQString(true));
119  }
120 
121  CAircraftModelList CDbOwnModelSetFormDialog::buildSet(const CSimulatorInfo &simulator,
122  const CAircraftModelList &currentSet)
123  {
124  Q_ASSERT_X(this->getMappingComponent(), Q_FUNC_INFO, "missing mapping component");
125  const bool givenDistributorsOnly = !ui->form_OwnModelSet->optionUseAllDistributors();
126  const bool dbDataOnly = ui->form_OwnModelSet->optionDbDataOnly();
127  const bool dbIcaoOnly = ui->form_OwnModelSet->optionDbIcaoCodesOnly();
128  const bool incremnental = ui->form_OwnModelSet->optionIncrementalBuild();
129  const bool sortByDistributor = ui->form_OwnModelSet->optionSortByDistributorPreferences();
130  const bool consolidateWithDb = ui->form_OwnModelSet->optionConsolidateModelSetWithDbData();
131 
132  m_simulatorInfo = this->getMappingComponent()->getOwnModelsSimulator();
133  const CAircraftModelList models = this->getMappingComponent()->getOwnModels();
134  const CDistributorList distributors = ui->form_OwnModelSet->getDistributorsBasedOnOptions();
135 
136  if (givenDistributorsOnly && distributors.isEmpty())
137  {
138  // nothing to do, keep current set
139  return currentSet;
140  }
141 
142  const CModelSetBuilder builder(this);
143  CModelSetBuilder::Builder options =
144  givenDistributorsOnly ? CModelSetBuilder::GivenDistributorsOnly : CModelSetBuilder::NoOptions;
145  if (dbDataOnly) { options |= CModelSetBuilder::OnlyDbData; }
146  if (dbIcaoOnly) { options |= CModelSetBuilder::OnlyDbIcaoCodes; }
147  if (incremnental) { options |= CModelSetBuilder::Incremental; }
148  if (sortByDistributor) { options |= CModelSetBuilder::SortByDistributors; }
149  if (consolidateWithDb) { options |= CModelSetBuilder::ConsolidateWithDb; }
150  return builder.buildModelSet(simulator, models, currentSet, options, distributors);
151  }
152 } // namespace swift::gui::components
Create model set (normally from own models)
Allows subcomponents to gain access to model component.
bool showMappingComponentOverlayHtmlMessage(const swift::misc::CStatusMessage &message, std::chrono::milliseconds timeout=std::chrono::milliseconds(0))
Overlay messages.
CDbMappingComponent * getMappingComponent() const
Get the mapping component.
void setOwnModelSetSimulator(const swift::misc::simulation::CSimulatorInfo &simulator)
Set simulator for own models.
int getOwnModelsCount() const
Number of own models.
swift::misc::simulation::CSimulatorInfo getOwnModelsSimulator() const
Own models for simulator.
swift::misc::simulation::CAircraftModelList getOwnModelSet() const
Own model set.
swift::misc::simulation::CAircraftModelList getOwnModels() const
Own cached models.
void setOwnModelsSimulator(const swift::misc::simulation::CSimulatorInfo &simulator)
Set simulator for own models.
void reloadData()
Reload data e.g. current model set and simulator.
static const QString & guiComponent()
GUI components.
Definition: logcategories.h:94
Derived & error(const char16_t(&format)[N])
Set the severity to error, providing a format string.
bool isEmpty() const
Synonym for empty.
Definition: sequence.h:285
Streamable status message, e.g.
QString toQString(bool i18n=false) const
Cast as QString.
Definition: mixinstring.h:74
Value object encapsulating a list of aircraft models.
Value object encapsulating a list of distributors.
Simple hardcoded info about the corresponding simulator.
Definition: simulatorinfo.h:41
bool isSingleSimulator() const
Single simulator selected.
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:14
Free functions in swift::misc.
void clicked(bool checked)
virtual void accept()
virtual int exec()
virtual void reject()
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
QObject * sender() const const
QString arg(Args &&... args) const const
void setWindowTitle(const QString &)