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  bool CDbOwnModelSetFormDialog::event(QEvent *event)
68  {
69  if (CGuiApplication::triggerShowHelp(this, event)) { return true; }
70  return QDialog::event(event);
71  }
72 
73  void CDbOwnModelSetFormDialog::buttonClicked()
74  {
75  const QObject *sender = QObject::sender();
76  if (sender == ui->pb_Cancel) { this->reject(); }
77  else if (sender == ui->pb_Ok)
78  {
79  m_modelSet = this->buildSet(m_simulatorInfo, m_modelSet);
80  this->accept();
81  }
82  }
83 
84  void CDbOwnModelSetFormDialog::simulatorChanged(const CSimulatorInfo &simulator)
85  {
86  Q_ASSERT_X(simulator.isSingleSimulator(), Q_FUNC_INFO, "Need single simulator");
87  this->setSimulator(simulator);
88 
89  // if possible set in mappings component
90  if (this->getMappingComponent())
91  {
92  this->getMappingComponent()->setOwnModelsSimulator(simulator);
93  this->getMappingComponent()->setOwnModelSetSimulator(simulator);
94  this->checkData();
95  }
96  }
97 
98  bool CDbOwnModelSetFormDialog::checkData()
99  {
100  // models
101  Q_ASSERT_X(m_simulatorInfo.isSingleSimulator(), Q_FUNC_INFO, "Need single simulator");
102  const int c = this->getMappingComponent()->getOwnModelsCount();
103  if (c < 1)
104  {
105  const CStatusMessage m = CStatusMessage(this).error(u"No models for '%1'")
106  << m_simulatorInfo.toQString(true);
107  ui->form_OwnModelSet->showOverlayMessage(m);
108  return false;
109  }
110  return true;
111  }
112 
113  void CDbOwnModelSetFormDialog::setSimulator(const CSimulatorInfo &simulator)
114  {
115  // Ref T663, avoid ASSERT in some weird cases
116  if (simulator.isSingleSimulator()) { m_simulatorInfo = simulator; }
117  else
118  {
119  const CSimulatorInfo resetSim =
120  m_simulatorInfo.isSingleSimulator() ? m_simulatorInfo : CSimulatorInfo::guessDefaultSimulator();
121  const QString msg =
122  QStringLiteral("Set invalid simulator, continue to use '%1'").arg(resetSim.toQString(true));
124  m_simulatorInfo = resetSim;
125  }
126  ui->form_OwnModelSet->setSimulator(m_simulatorInfo);
127  this->setWindowTitle("Create model set for " + m_simulatorInfo.toQString(true));
128  }
129 
130  CAircraftModelList CDbOwnModelSetFormDialog::buildSet(const CSimulatorInfo &simulator,
131  const CAircraftModelList &currentSet)
132  {
133  Q_ASSERT_X(this->getMappingComponent(), Q_FUNC_INFO, "missing mapping component");
134  const bool givenDistributorsOnly = !ui->form_OwnModelSet->optionUseAllDistributors();
135  const bool dbDataOnly = ui->form_OwnModelSet->optionDbDataOnly();
136  const bool dbIcaoOnly = ui->form_OwnModelSet->optionDbIcaoCodesOnly();
137  const bool incremnental = ui->form_OwnModelSet->optionIncrementalBuild();
138  const bool sortByDistributor = ui->form_OwnModelSet->optionSortByDistributorPreferences();
139  const bool consolidateWithDb = ui->form_OwnModelSet->optionConsolidateModelSetWithDbData();
140 
141  m_simulatorInfo = this->getMappingComponent()->getOwnModelsSimulator();
142  const CAircraftModelList models = this->getMappingComponent()->getOwnModels();
143  const CDistributorList distributors = ui->form_OwnModelSet->getDistributorsBasedOnOptions();
144 
145  if (givenDistributorsOnly && distributors.isEmpty())
146  {
147  // nothing to do, keep current set
148  return currentSet;
149  }
150 
151  const CModelSetBuilder builder(this);
152  CModelSetBuilder::Builder options =
153  givenDistributorsOnly ? CModelSetBuilder::GivenDistributorsOnly : CModelSetBuilder::NoOptions;
154  if (dbDataOnly) { options |= CModelSetBuilder::OnlyDbData; }
155  if (dbIcaoOnly) { options |= CModelSetBuilder::OnlyDbIcaoCodes; }
156  if (incremnental) { options |= CModelSetBuilder::Incremental; }
157  if (sortByDistributor) { options |= CModelSetBuilder::SortByDistributors; }
158  if (consolidateWithDb) { options |= CModelSetBuilder::ConsolidateWithDb; }
159  return builder.buildModelSet(simulator, models, currentSet, options, distributors);
160  }
161 } // namespace swift::gui::components
Create model set (normally from own models)
static bool triggerShowHelp(const QWidget *widget, QEvent *event)
Static version used with dialogs.
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:76
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.