swift
matchingform.cpp
1 // SPDX-FileCopyrightText: Copyright (C) 2018 swift Project Community / Contributors
2 // SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-swift-pilot-client-1
3 
4 #include "matchingform.h"
5 
6 #include <QFileDialog>
7 #include <QPushButton>
8 
9 #include "ui_matchingform.h"
10 
11 #include "gui/guiutility.h"
12 
13 using namespace swift::misc;
14 using namespace swift::misc::simulation;
15 using namespace swift::core;
16 
17 namespace swift::gui::editors
18 {
19  CMatchingForm::CMatchingForm(QWidget *parent) : CForm(parent), ui(new Ui::CMatchingForm)
20  {
21  ui->setupUi(this);
22  connect(ui->rb_Reduction, &QRadioButton::released, this, &CMatchingForm::onAlgorithmChanged,
23  Qt::QueuedConnection);
24 
25  connect(ui->pb_ResetAlgorithm, &QPushButton::released, this, &CMatchingForm::resetByAlgorithm,
26  Qt::QueuedConnection);
27  connect(ui->pb_ResetAll, &QPushButton::released, this, &CMatchingForm::resetAll, Qt::QueuedConnection);
28  connect(ui->pb_MsReverseLookup, &QPushButton::released, this, &CMatchingForm::fileDialog, Qt::QueuedConnection);
29  connect(ui->pb_MsMatching, &QPushButton::released, this, &CMatchingForm::fileDialog, Qt::QueuedConnection);
30 
31  connect(ui->rb_ScoreAndReduction, &QRadioButton::released, this, &CMatchingForm::onAlgorithmChanged,
32  Qt::QueuedConnection);
33  connect(ui->rb_ScoreOnly, &QRadioButton::released, this, &CMatchingForm::onAlgorithmChanged,
34  Qt::QueuedConnection);
35 
36  connect(ui->rb_AirlineGroupIfNoAirline, &QRadioButton::released, this, &CMatchingForm::onAirlineGroupChanged,
37  Qt::QueuedConnection);
38  connect(ui->rb_AirlineGroupAsAirline, &QRadioButton::released, this, &CMatchingForm::onAirlineGroupChanged,
39  Qt::QueuedConnection);
40  connect(ui->rb_AirlineGroupNo, &QRadioButton::released, this, &CMatchingForm::onAirlineGroupChanged,
41  Qt::QueuedConnection);
42  }
43 
45 
46  void CMatchingForm::setReadOnly(bool readonly)
47  {
48  CGuiUtility::checkBoxReadOnly(ui->cb_ByModelString, readonly);
49  CGuiUtility::checkBoxReadOnly(ui->cb_ByFamily, readonly);
50  CGuiUtility::checkBoxReadOnly(ui->cb_ByLivery, readonly);
51  CGuiUtility::checkBoxReadOnly(ui->cb_ByCombinedCode, readonly);
52  CGuiUtility::checkBoxReadOnly(ui->cb_ByVtol, readonly);
53  CGuiUtility::checkBoxReadOnly(ui->cb_ByMilitary, readonly);
54  CGuiUtility::checkBoxReadOnly(ui->cb_ByCivilian, readonly);
55  CGuiUtility::checkBoxReadOnly(ui->cb_ScoreIgnoreZeros, readonly);
56  CGuiUtility::checkBoxReadOnly(ui->cb_ScorePreferColorLiveries, readonly);
57  CGuiUtility::checkBoxReadOnly(ui->cb_ModelSetRemoveFailed, readonly);
58  CGuiUtility::checkBoxReadOnly(ui->cb_ModelFailedFailover, readonly);
59  CGuiUtility::checkBoxReadOnly(ui->cb_ModelSetVerification, readonly);
60  CGuiUtility::checkBoxReadOnly(ui->cb_ModelSetVerificationOnlyErrorWarning, readonly);
61  CGuiUtility::checkBoxReadOnly(ui->cb_CategoryGlider, readonly);
62  CGuiUtility::checkBoxReadOnly(ui->cb_CategoryMilitaryAircraft, readonly);
63  CGuiUtility::checkBoxReadOnly(ui->cb_ReverseUseModelString, readonly);
64  CGuiUtility::checkBoxReadOnly(ui->cb_ReverseUseSwiftLiveryIds, readonly);
65  CGuiUtility::checkBoxReadOnly(ui->cb_MsReverseLookup, readonly);
66  CGuiUtility::checkBoxReadOnly(ui->cb_MsMatching, readonly);
67 
68  const bool enabled = !readonly;
69  ui->rb_Reduction->setEnabled(enabled);
70  ui->rb_ScoreAndReduction->setEnabled(enabled);
71  ui->rb_ScoreOnly->setEnabled(enabled);
72  ui->rb_ByIcaoDataAircraft1st->setEnabled(enabled);
73  ui->rb_ByIcaoDataAirline1st->setEnabled(enabled);
74  ui->rb_AirlineGroupAsAirline->setEnabled(enabled);
75  ui->rb_AirlineGroupNo->setEnabled(enabled);
76  ui->rb_AirlineGroupIfNoAirline->setEnabled(enabled);
77 
78  ui->rb_PickFirst->setEnabled(enabled);
79  ui->rb_PickByOrder->setEnabled(enabled);
80  ui->rb_PickRandom->setEnabled(enabled);
81 
82  ui->le_MsReverseLookup->setEnabled(enabled);
83  ui->le_MsMatching->setEnabled(enabled);
84  }
85 
86  CStatusMessageList CMatchingForm::validate(bool withNestedForms) const
87  {
88  Q_UNUSED(withNestedForms)
89  return CStatusMessageList();
90  }
91 
93  {
94  const CAircraftMatcherSetup::MatchingMode mode = setup.getMatchingMode();
95  ui->cb_ByModelString->setChecked(mode.testFlag(CAircraftMatcherSetup::ByModelString));
96  ui->cb_ByCombinedCode->setChecked(mode.testFlag(CAircraftMatcherSetup::ByCombinedType));
97 
98  ui->rb_ByIcaoDataAircraft1st->setChecked(mode.testFlag(CAircraftMatcherSetup::ByIcaoOrderAircraftFirst));
99  ui->rb_ByIcaoDataAirline1st->setChecked(mode.testFlag(CAircraftMatcherSetup::ByIcaoOrderAirlineFirst));
100 
101  const bool nag = !mode.testFlag(CAircraftMatcherSetup::ByAirlineGroupSameAsAirline) &&
102  !mode.testFlag(CAircraftMatcherSetup::ByAirlineGroupIfNoAirline);
103  if (nag) { ui->rb_AirlineGroupNo->setChecked(nag); }
104  else
105  {
106  ui->rb_AirlineGroupAsAirline->setChecked(mode.testFlag(CAircraftMatcherSetup::ByAirlineGroupSameAsAirline));
107  ui->rb_AirlineGroupIfNoAirline->setChecked(
108  mode.testFlag(CAircraftMatcherSetup::CAircraftMatcherSetup::ByAirlineGroupIfNoAirline));
109  }
110 
111  ui->cb_ByLivery->setChecked(mode.testFlag(CAircraftMatcherSetup::ByLivery));
112  ui->cb_ByFamily->setChecked(mode.testFlag(CAircraftMatcherSetup::ByFamily));
113  ui->cb_ByMilitary->setChecked(mode.testFlag(CAircraftMatcherSetup::ByMilitary));
114  ui->cb_ByCivilian->setChecked(mode.testFlag(CAircraftMatcherSetup::ByCivilian));
115  ui->cb_CategoryGlider->setChecked(mode.testFlag(CAircraftMatcherSetup::ByCategoryGlider));
116  ui->cb_CategoryMilitaryAircraft->setChecked(mode.testFlag(CAircraftMatcherSetup::ByCategoryMilitary));
117  ui->cb_CategorySmallAircraft->setChecked(mode.testFlag(CAircraftMatcherSetup::ByCategorySmallAircraft));
118  ui->cb_ByVtol->setChecked(mode.testFlag(CAircraftMatcherSetup::ByVtol));
119  ui->cb_ScoreIgnoreZeros->setChecked(mode.testFlag(CAircraftMatcherSetup::ScoreIgnoreZeros));
120  ui->cb_ScorePreferColorLiveries->setChecked(mode.testFlag(CAircraftMatcherSetup::ScorePreferColorLiveries));
121  ui->cb_ExclNoExcludedModels->setChecked(mode.testFlag(CAircraftMatcherSetup::ExcludeNoExcluded));
122  ui->cb_ExclNoDbData->setChecked(mode.testFlag(CAircraftMatcherSetup::ExcludeNoDbData));
123  ui->cb_ModelSetRemoveFailed->setChecked(mode.testFlag(CAircraftMatcherSetup::ModelSetRemoveFailedModel) ||
124  mode.testFlag(CAircraftMatcherSetup::ModelFailoverIfNoModelCanBeAdded));
125  ui->cb_ModelFailedFailover->setChecked(mode.testFlag(CAircraftMatcherSetup::ModelFailoverIfNoModelCanBeAdded));
126  ui->cb_ModelSetVerification->setChecked(mode.testFlag(CAircraftMatcherSetup::ModelVerificationAtStartup));
127  ui->cb_ModelSetVerificationOnlyErrorWarning->setChecked(
128  mode.testFlag(CAircraftMatcherSetup::ModelVerificationOnlyWarnError));
129  ui->cb_ReverseUseModelString->setChecked(mode.testFlag(CAircraftMatcherSetup::ReverseLookupModelString));
130  ui->cb_ReverseUseSwiftLiveryIds->setChecked(mode.testFlag(CAircraftMatcherSetup::ReverseLookupSwiftLiveryIds));
131 
132  this->setMatchingAlgorithm(setup);
133  this->setPickStrategy(setup);
134 
135  ui->cb_MsReverseLookup->setChecked(setup.isMsReverseLookupEnabled());
136  ui->cb_MsMatching->setChecked(setup.isMsMatchingStageEnabled());
137  ui->le_MsReverseLookup->setText(setup.getMsReverseLookupFile());
138  ui->le_MsMatching->setText(setup.getMsMatchingStageFile());
139  }
140 
142  {
143  CAircraftMatcherSetup setup(algorithm(), matchingMode(), pickStrategy());
144  setup.setAirlineGroupBehaviour(ui->rb_AirlineGroupIfNoAirline->isChecked(),
145  ui->rb_AirlineGroupAsAirline->isChecked());
146  setup.setMsReverseLookupFile(ui->le_MsReverseLookup->text());
147  setup.setMsMatchingStageFile(ui->le_MsMatching->text());
148  setup.setMsReverseLookupEnabled(ui->cb_MsReverseLookup->isChecked());
149  setup.setMsMatchingStageEnabled(ui->cb_MsMatching->isChecked());
150  return setup;
151  }
152 
153  void CMatchingForm::clear() { this->resetAll(); }
154 
155  void CMatchingForm::resetByAlgorithm()
156  {
157  const CAircraftMatcherSetup s(this->algorithm());
158  this->setValue(s);
159  }
160 
161  void CMatchingForm::resetAll()
162  {
163  const CAircraftMatcherSetup s;
164  this->setValue(s);
165  }
166 
167  void CMatchingForm::fileDialog()
168  {
169  const bool nw = (QObject::sender() == ui->pb_MsReverseLookup);
170  QString fn = nw ? ui->le_MsReverseLookup->text() : ui->le_MsMatching->text();
171  CDirectories swiftDirs = m_directories.get();
172 
173  fn = QFileDialog::getOpenFileName(nullptr, tr("Matching script"),
174  fn.isEmpty() ? swiftDirs.getMatchingScriptDirectoryOrDefault() : fn,
175  "Matching script (*.js)");
176  const QFileInfo fi(fn);
177  if (!fi.exists()) { return; }
178  if (nw) { ui->le_MsReverseLookup->setText(fi.absoluteFilePath()); }
179  else { ui->le_MsMatching->setText(fi.absoluteFilePath()); }
180 
181  swiftDirs.setMatchingScriptDirectory(fi.absolutePath());
182  m_directories.setAndSave(swiftDirs);
183  }
184 
185  CAircraftMatcherSetup::MatchingAlgorithm CMatchingForm::algorithm() const
186  {
187  if (ui->rb_Reduction->isChecked()) { return CAircraftMatcherSetup::MatchingStepwiseReduce; }
188  if (ui->rb_ScoreOnly->isChecked()) { return CAircraftMatcherSetup::MatchingScoreBased; }
189  return CAircraftMatcherSetup::MatchingStepwiseReducePlusScoreBased;
190  }
191 
192  CAircraftMatcherSetup::MatchingMode CMatchingForm::matchingMode() const
193  {
194  return CAircraftMatcherSetup::matchingMode(
195  ui->cb_ReverseUseModelString->isChecked(), ui->cb_ReverseUseSwiftLiveryIds->isChecked(),
196  ui->cb_ByModelString->isChecked(), ui->rb_ByIcaoDataAircraft1st->isChecked(),
197  ui->rb_ByIcaoDataAirline1st->isChecked(), ui->cb_ByFamily->isChecked(), ui->cb_ByLivery->isChecked(),
198  ui->cb_ByCombinedCode->isChecked(), ui->cb_ByMilitary->isChecked(), ui->cb_ByCivilian->isChecked(),
199  ui->cb_ByVtol->isChecked(), ui->cb_CategoryGlider->isChecked(),
200  ui->cb_CategoryMilitaryAircraft->isChecked(), ui->cb_CategorySmallAircraft->isChecked(),
201  ui->cb_ScoreIgnoreZeros->isChecked(), ui->cb_ScorePreferColorLiveries->isChecked(),
202  ui->cb_ExclNoDbData->isChecked(), ui->cb_ExclNoExcludedModels->isChecked(),
203  ui->cb_ModelSetVerification->isChecked(), ui->cb_ModelSetVerificationOnlyErrorWarning->isChecked(),
204  ui->cb_ModelSetRemoveFailed->isChecked(), ui->cb_ModelFailedFailover->isChecked());
205  }
206 
207  CAircraftMatcherSetup::PickSimilarStrategy CMatchingForm::pickStrategy() const
208  {
209  if (ui->rb_PickRandom->isChecked()) { return CAircraftMatcherSetup::PickRandom; }
210  if (ui->rb_PickByOrder->isChecked()) { return CAircraftMatcherSetup::PickByOrder; }
211  return CAircraftMatcherSetup::PickFirst;
212  }
213 
214  void CMatchingForm::setPickStrategy(const CAircraftMatcherSetup &setup)
215  {
216  switch (setup.getPickStrategy())
217  {
218  case CAircraftMatcherSetup::PickByOrder: ui->rb_PickByOrder->setChecked(true); break;
219  case CAircraftMatcherSetup::PickRandom: ui->rb_PickRandom->setChecked(true); break;
220  case CAircraftMatcherSetup::PickFirst:
221  default: ui->rb_PickFirst->setChecked(true); break;
222  }
223  }
224 
225  void CMatchingForm::setMatchingAlgorithm(const CAircraftMatcherSetup &setup)
226  {
227  switch (setup.getMatchingAlgorithm())
228  {
229  case CAircraftMatcherSetup::MatchingStepwiseReduce:
230  ui->rb_Reduction->setChecked(true);
231  ui->gb_Reduction->setEnabled(true);
232  ui->gb_Scoring->setEnabled(false);
233  break;
234  case CAircraftMatcherSetup::MatchingScoreBased:
235  ui->rb_ScoreOnly->setChecked(true);
236  ui->gb_Reduction->setEnabled(false);
237  ui->gb_Scoring->setEnabled(true);
238  break;
239  case CAircraftMatcherSetup::MatchingStepwiseReducePlusScoreBased:
240  default:
241  ui->rb_ScoreAndReduction->setChecked(true);
242  ui->gb_Reduction->setEnabled(true);
243  ui->gb_Scoring->setEnabled(true);
244  break;
245  }
246  }
247 
248  void CMatchingForm::onAlgorithmChanged()
249  {
250  const CAircraftMatcherSetup setup = this->value();
251  this->setValue(setup);
252  }
253 
254  void CMatchingForm::onAirlineGroupChanged()
255  {
256  const CAircraftMatcherSetup setup = this->value();
257  this->setValue(setup);
258  }
259 } // namespace swift::gui::editors
static void checkBoxReadOnly(QCheckBox *checkBox, bool readOnly)
Pseudo readonly state for checkbox.
Definition: guiutility.cpp:450
Form base class.
Definition: form.h:27
void setValue(const swift::misc::simulation::CAircraftMatcherSetup &setup)
Set valued.
virtual swift::misc::CStatusMessageList validate(bool withNestedForms) const
Validate, empty list means OK.
virtual ~CMatchingForm()
Destructor.
swift::misc::simulation::CAircraftMatcherSetup value() const
Value.
virtual void setReadOnly(bool readonly)
Set editable.
CStatusMessage setAndSave(const T &value, qint64 timestamp=0)
Write and save in the same step. Must be called from the thread in which the owner lives.
Definition: valuecache.h:417
T get() const
Get a copy of the current value.
Definition: valuecache.h:408
Directories (swift data directories)
Definition: directories.h:25
void setMatchingScriptDirectory(const QString &dir)
Matching script directory.
Definition: directories.h:90
QString getMatchingScriptDirectoryOrDefault() const
Matching script directory.
Definition: directories.cpp:50
Status messages, e.g. from Core -> GUI.
void setMsMatchingStageEnabled(bool enabled)
Is matching script enabled.
void setMsReverseLookupFile(const QString &file)
Set matching files.
MatchingMode getMatchingMode() const
Matching mode.
void setMsReverseLookupEnabled(bool enabled)
Is matching script enabled.
PickSimilarStrategy
How to pick among similar candiates.
const QString & getMsMatchingStageFile() const
Get matching files.
void setAirlineGroupBehaviour(bool ifNoAirline, bool sameAsAirline)
Airline group behaviour.
void setMsMatchingStageFile(const QString &file)
Set matching files.
bool isMsMatchingStageEnabled() const
Is matching script enabled.
MatchingAlgorithm getMatchingAlgorithm() const
Algorithm.
PickSimilarStrategy getPickStrategy() const
Strategy among equally suitable models.
const QString & getMsReverseLookupFile() const
Get matching files.
bool isMsReverseLookupEnabled() const
Is matching script enabled.
Backend services of the swift project, like dealing with the network or the simulators.
Definition: actionbind.cpp:7
Free functions in swift::misc.