swift
ownmodelsetform.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 <QPointer>
7 #include <QRadioButton>
8 #include <QtGlobal>
9 
10 #include "ui_ownmodelsetform.h"
11 
12 #include "core/webdataservices.h"
13 #include "gui/guiapplication.h"
14 #include "gui/guiutility.h"
18 
19 using namespace swift::misc;
20 using namespace swift::misc::simulation;
21 using namespace swift::gui::models;
22 using namespace swift::gui::components;
23 
24 namespace swift::gui::editors
25 {
26  COwnModelSetForm::COwnModelSetForm(QWidget *parent) : CForm(parent), ui(new Ui::COwnModelSetForm)
27  {
28  ui->setupUi(this);
29  ui->tvp_Distributors->setDistributorMode(CDistributorListModel::Minimal);
30  ui->comp_SimulatorSelector->setMode(CSimulatorSelector::RadioButtons);
31  ui->comp_SimulatorSelector->setLeftMargin(0);
32 
33  connect(ui->comp_SimulatorSelector, &CSimulatorSelector::changed, this, &COwnModelSetForm::onSimulatorChanged,
34  Qt::QueuedConnection);
35  connect(ui->rb_DisplayAllDistributors, &QRadioButton::clicked, this,
36  &COwnModelSetForm::changeDistributorDisplay);
37  connect(ui->rb_DisplayPreferencesDistributors, &QRadioButton::clicked, this,
38  &COwnModelSetForm::changeDistributorDisplay);
39 
40  QPointer<COwnModelSetForm> myself(this);
41  QTimer::singleShot(1250, [=] {
42  if (!myself) { return; }
43  this->onSimulatorChanged(ui->comp_SimulatorSelector->getValue());
44  });
45  }
46 
48  {
49  // void
50  }
51 
53  {
54  const bool hasPreferences = this->hasDistributorPreferences();
55  ui->cb_SortByPreferences->setChecked(hasPreferences);
56  CGuiUtility::checkBoxReadOnly(ui->cb_SortByPreferences, !hasPreferences);
57  ui->comp_SimulatorSelector->setValue(m_simulator);
58  this->setDistributorView(hasPreferences);
59  this->initDistributorDisplay();
60  }
61 
62  bool COwnModelSetForm::optionUseSelectedDistributors() const { return ui->rb_DistributorsSelected->isChecked(); }
63 
64  bool COwnModelSetForm::optionUseAllDistributors() const { return ui->rb_DistributorsAll->isChecked(); }
65 
66  bool COwnModelSetForm::optionDbDataOnly() const { return ui->rb_DbDataOnly->isChecked(); }
67 
68  bool COwnModelSetForm::optionIncrementalBuild() const { return ui->rb_Incremental->isChecked(); }
69 
71  {
72  return ui->cb_SortByPreferences->isChecked() && this->hasDistributorPreferences();
73  }
74 
76  {
77  return ui->cb_ConsolidateModelSet->isChecked();
78  }
79 
80  void COwnModelSetForm::onPreferencesChanged()
81  {
82  // void
83  }
84 
85  void COwnModelSetForm::onSimulatorChanged(const CSimulatorInfo &simulator)
86  {
87  Q_ASSERT_X(simulator.isSingleSimulator(), Q_FUNC_INFO, "Expect single simulator");
88  this->setSimulator(simulator);
89  this->reloadData();
90  emit this->simulatorChanged(simulator);
91  }
92 
93  void COwnModelSetForm::changeDistributorDisplay()
94  {
95  if (ui->rb_DisplayAllDistributors->isChecked())
96  {
97  ui->tvp_Distributors->updateContainerMaybeAsync(this->getAllDistributors());
98  ui->cb_SortByPreferences->setChecked(false);
99  CGuiUtility::checkBoxReadOnly(ui->cb_SortByPreferences, true);
100  this->setDistributorView(false);
101  }
102  else
103  {
104  ui->tvp_Distributors->updateContainerMaybeAsync(this->getDistributorsFromPreferences());
105  ui->cb_SortByPreferences->setChecked(true);
106  CGuiUtility::checkBoxReadOnly(ui->cb_SortByPreferences, false);
107  this->setDistributorView(true);
108  }
109  }
110 
111  void COwnModelSetForm::initDistributorDisplay()
112  {
113  if (this->hasDistributorPreferences()) { ui->rb_DisplayPreferencesDistributors->setChecked(true); }
114  else { ui->rb_DisplayAllDistributors->setChecked(true); }
115  this->changeDistributorDisplay();
116  }
117 
118  void COwnModelSetForm::setDistributorView(bool hasPreferences)
119  {
120  ui->tvp_Distributors->setDistributorMode(hasPreferences ? CDistributorListModel::MinimalWithOrder :
121  CDistributorListModel::Minimal);
122  ui->tvp_Distributors->fullResizeToContents();
123  const CPropertyIndex i =
124  hasPreferences ? CPropertyIndex(CDistributor::IndexOrder) : CPropertyIndex(CDistributor::IndexDbStringKey);
125  ui->tvp_Distributors->sortByPropertyIndex(i);
126  }
127 
129  {
130  Q_ASSERT_X(m_simulator.isSingleSimulator(), Q_FUNC_INFO, "Need single simulator");
131  const CDistributorListPreferences prefs(m_distributorPreferences.get());
132  const CDistributorList distributors(prefs.getDistributors(m_simulator));
133  return distributors;
134  }
135 
137  {
138  Q_ASSERT_X(m_simulator.isSingleSimulator(), Q_FUNC_INFO, "Need single simulator");
139  Q_ASSERT_X(sGui && sGui->hasWebDataServices(), Q_FUNC_INFO, "Missing web data services");
140  return sGui->getWebDataServices()->getDistributors().matchesSimulator(m_simulator);
141  }
142 
144  {
145  if (ui->rb_DistributorsAll->isChecked()) { return this->getAllDistributors(); }
146  if (ui->rb_DistributorsSelected->isChecked()) { return this->getSelectedDistributors(); }
147  if (ui->rb_DistributorsFromBelow->isChecked()) { return this->getShownDistributors(); }
148  Q_ASSERT_X(false, Q_FUNC_INFO, "Wrong option");
149  return CDistributorList();
150  }
151 
152  bool COwnModelSetForm::optionDbIcaoCodesOnly() const { return ui->rb_DbIcaoCodesOnly->isChecked(); }
153 
155  {
156  return ui->tvp_Distributors->selectedObjects();
157  }
158 
160  {
161  return ui->tvp_Distributors->containerOrFilteredContainer();
162  }
163 
165  {
166  // Ref T663, avoid ASSERT in some weird cases
167  if (simulator.isSingleSimulator()) { m_simulator = simulator; }
168  else
169  {
170  const CSimulatorInfo resetSim =
171  m_simulator.isSingleSimulator() ? m_simulator : CSimulatorInfo::guessDefaultSimulator();
172  const QString msg =
173  QStringLiteral("Set invalid simulator, continue to use '%1'").arg(resetSim.toQString(true));
174  this->showOverlayHTMLMessage(msg);
175  m_simulator = resetSim;
176  }
177  }
178 
180  {
181  const CDistributorListPreferences prefs(m_distributorPreferences.get());
182  return !prefs.getDistributors(m_simulator).isEmpty();
183  }
184 } // namespace swift::gui::editors
bool hasWebDataServices() const
Web data services available?
CWebDataServices * getWebDataServices() const
Get the web data services.
swift::misc::simulation::CDistributorList getDistributors() const
Distributors.
static void checkBoxReadOnly(QCheckBox *checkBox, bool readOnly)
Pseudo readonly state for checkbox.
Definition: guiutility.cpp:450
bool showOverlayHTMLMessage(const QString &htmlMessage, std::chrono::milliseconds timeout=std::chrono::milliseconds(0))
HTML message.
Form base class.
Definition: form.h:27
Selection for own model set.
bool optionConsolidateModelSetWithDbData() const
Consolidate with DB data?
void simulatorChanged(const swift::misc::simulation::CSimulatorInfo &simulator)
Simulator changed.
bool optionDbDataOnly() const
Only DB data.
swift::misc::simulation::CDistributorList getAllDistributors() const
All distributors.
bool hasDistributorPreferences() const
Preferences for given simulator?
swift::misc::simulation::CDistributorList getSelectedDistributors() const
Get selected distributors.
swift::misc::simulation::CDistributorList getDistributorsBasedOnOptions() const
Get distributors based on options.
swift::misc::simulation::CDistributorList getShownDistributors() const
Get shown distributors.
bool optionSortByDistributorPreferences() const
Sort by distributor preferences.
bool optionUseSelectedDistributors() const
Selected distributors?
bool optionUseAllDistributors() const
All distributors?
swift::misc::simulation::CDistributorList getDistributorsFromPreferences() const
Distributors from preferences.
bool optionIncrementalBuild() const
Request incremental build.
bool optionDbIcaoCodesOnly() const
DB ICAO codes.
void setSimulator(const swift::misc::simulation::CSimulatorInfo &simulator)
Current simulator.
T get() const
Get a copy of the current value.
Definition: valuecache.h:408
bool isEmpty() const
Synonym for empty.
Definition: sequence.h:285
QString toQString(bool i18n=false) const
Cast as QString.
Definition: mixinstring.h:76
Value object encapsulating information of software distributor.
Definition: distributor.h:33
Value object encapsulating a list of distributors.
CDistributorList matchesSimulator(const CSimulatorInfo &simulator) const
Find for given simulator.
const CDistributorList & getDistributors(const CSimulatorInfo &simulator) const
Get distributors.
Simple hardcoded info about the corresponding simulator.
Definition: simulatorinfo.h:41
bool isSingleSimulator() const
Single simulator selected.
SWIFT_GUI_EXPORT swift::gui::CGuiApplication * sGui
Single instance of GUI application object.
High level reusable GUI components.
Definition: aboutdialog.cpp:13
Models to be used with views, mainly QTableView.
Free functions in swift::misc.
auto singleShot(int msec, QObject *target, F &&task)
Starts a single-shot timer which will call a task in the thread of the given object when it times out...
Definition: threadutils.h:30