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  {
45  // void
46  }
47 
49  {
50  m_simulatorInfo = this->getMappingComponent()->getOwnModelsSimulator();
51  Q_ASSERT_X(m_simulatorInfo.isSingleSimulator(), Q_FUNC_INFO, "Need single simulator");
52  ui->form_OwnModelSet->setSimulator(m_simulatorInfo);
53  ui->form_OwnModelSet->reloadData();
54  m_modelSet = this->getMappingComponent()->getOwnModelSet();
55  }
56 
58  {
59  Q_ASSERT_X(this->getMappingComponent(), Q_FUNC_INFO, "missing mapping component");
60  const CSimulatorInfo sim(this->getMappingComponent()->getOwnModelsSimulator());
61  Q_ASSERT_X(sim.isSingleSimulator(), Q_FUNC_INFO, "need single simulator");
62  this->setSimulator(sim);
63  this->checkData();
64  return QDialog::exec();
65  }
66 
67  void CDbOwnModelSetFormDialog::buttonClicked()
68  {
69  const QObject *sender = QObject::sender();
70  if (sender == ui->pb_Cancel) { this->reject(); }
71  else if (sender == ui->pb_Ok)
72  {
73  m_modelSet = this->buildSet(m_simulatorInfo, m_modelSet);
74  this->accept();
75  }
76  }
77 
78  void CDbOwnModelSetFormDialog::simulatorChanged(const CSimulatorInfo &simulator)
79  {
80  Q_ASSERT_X(simulator.isSingleSimulator(), Q_FUNC_INFO, "Need single simulator");
81  this->setSimulator(simulator);
82 
83  // if possible set in mappings component
84  if (this->getMappingComponent())
85  {
86  this->getMappingComponent()->setOwnModelsSimulator(simulator);
87  this->getMappingComponent()->setOwnModelSetSimulator(simulator);
88  this->checkData();
89  }
90  }
91 
92  bool CDbOwnModelSetFormDialog::checkData()
93  {
94  // models
95  Q_ASSERT_X(m_simulatorInfo.isSingleSimulator(), Q_FUNC_INFO, "Need single simulator");
96  const int c = this->getMappingComponent()->getOwnModelsCount();
97  if (c < 1)
98  {
99  const CStatusMessage m = CStatusMessage(this).error(u"No models for '%1'")
100  << m_simulatorInfo.toQString(true);
101  ui->form_OwnModelSet->showOverlayMessage(m);
102  return false;
103  }
104  return true;
105  }
106 
107  void CDbOwnModelSetFormDialog::setSimulator(const CSimulatorInfo &simulator)
108  {
109  // Ref T663, avoid ASSERT in some weird cases
110  if (simulator.isSingleSimulator()) { m_simulatorInfo = simulator; }
111  else
112  {
113  const CSimulatorInfo resetSim =
114  m_simulatorInfo.isSingleSimulator() ? m_simulatorInfo : CSimulatorInfo::guessDefaultSimulator();
115  const QString msg =
116  QStringLiteral("Set invalid simulator, continue to use '%1'").arg(resetSim.toQString(true));
118  m_simulatorInfo = resetSim;
119  }
120  ui->form_OwnModelSet->setSimulator(m_simulatorInfo);
121  this->setWindowTitle("Create model set for " + m_simulatorInfo.toQString(true));
122  }
123 
124  CAircraftModelList CDbOwnModelSetFormDialog::buildSet(const CSimulatorInfo &simulator,
125  const CAircraftModelList &currentSet)
126  {
127  Q_ASSERT_X(this->getMappingComponent(), Q_FUNC_INFO, "missing mapping component");
128  const bool givenDistributorsOnly = !ui->form_OwnModelSet->optionUseAllDistributors();
129  const bool dbDataOnly = ui->form_OwnModelSet->optionDbDataOnly();
130  const bool dbIcaoOnly = ui->form_OwnModelSet->optionDbIcaoCodesOnly();
131  const bool incremnental = ui->form_OwnModelSet->optionIncrementalBuild();
132  const bool sortByDistributor = ui->form_OwnModelSet->optionSortByDistributorPreferences();
133  const bool consolidateWithDb = ui->form_OwnModelSet->optionConsolidateModelSetWithDbData();
134 
135  m_simulatorInfo = this->getMappingComponent()->getOwnModelsSimulator();
136  const CAircraftModelList models = this->getMappingComponent()->getOwnModels();
137  const CDistributorList distributors = ui->form_OwnModelSet->getDistributorsBasedOnOptions();
138 
139  if (givenDistributorsOnly && distributors.isEmpty())
140  {
141  // nothing to do, keep current set
142  return currentSet;
143  }
144 
145  const CModelSetBuilder builder(this);
146  CModelSetBuilder::Builder options =
147  givenDistributorsOnly ? CModelSetBuilder::GivenDistributorsOnly : CModelSetBuilder::NoOptions;
148  if (dbDataOnly) { options |= CModelSetBuilder::OnlyDbData; }
149  if (dbIcaoOnly) { options |= CModelSetBuilder::OnlyDbIcaoCodes; }
150  if (incremnental) { options |= CModelSetBuilder::Incremental; }
151  if (sortByDistributor) { options |= CModelSetBuilder::SortByDistributors; }
152  if (consolidateWithDb) { options |= CModelSetBuilder::ConsolidateWithDb; }
153  return builder.buildModelSet(simulator, models, currentSet, options, distributors);
154  }
155 } // 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.
virtual int exec()
Exec and display 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:13
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 &)