swift
modelmappingmodifyform.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 <QCheckBox>
7 #include <QLineEdit>
8 #include <QString>
9 
10 #include "ui_modelmappingmodifyform.h"
11 
14 #include "gui/uppercasevalidator.h"
18 #include "misc/stringutils.h"
19 
20 using namespace swift::misc;
21 using namespace swift::misc::network;
22 using namespace swift::misc::physical_quantities;
23 using namespace swift::misc::simulation;
24 using namespace swift::gui::components;
25 
26 namespace swift::gui::editors
27 {
28  CModelMappingModifyForm::CModelMappingModifyForm(QWidget *parent)
29  : CFormDbUser(parent), ui(new Ui::CModelMappingModifyForm)
30  {
31  ui->setupUi(this);
32  ui->le_Parts->setPlaceholderText("Parts " + CAircraftModel::supportedParts());
33  this->userChanged();
34 
35  auto *ucv = new CUpperCaseValidator(true, 0, CAircraftModel::supportedParts().size(), ui->le_Parts);
36  ucv->setAllowedCharacters(CAircraftModel::supportedParts());
37  ui->le_Parts->setValidator(ucv);
38 
39  // "auto checked" disabled
40  // connect(ui->le_Description, &QLineEdit::returnPressed, this, &CModelMappingModifyForm::returnPressed);
41  // connect(ui->le_Name, &QLineEdit::returnPressed, this, &CModelMappingModifyForm::returnPressed);
42  // connect(ui->selector_ModeSelector, &CModelModeSelector::changed, this, &CModelMappingModifyForm::changed);
43  // connect(ui->selector_SimulatorSelector, &CSimulatorSelector::changed, this,
44  // &CModelMappingModifyForm::changed);
45 
46  connect(ui->le_CG, &QLineEdit::editingFinished, this, &CModelMappingModifyForm::onCGEdited);
47  }
48 
50 
52  {
54 
55  if (ui->cb_Name->isChecked()) { vm.addValue(CAircraftModel::IndexName, ui->le_Name->text().trimmed()); }
56 
57  if (ui->cb_Description->isChecked())
58  {
59  vm.addValue(CAircraftModel::IndexDescription, ui->le_Description->text().trimmed());
60  }
61 
62  if (ui->cb_Mode->isChecked())
63  {
64  vm.addValue(CAircraftModel::IndexModelMode, ui->selector_ModeSelector->getMode());
65  }
66 
67  if (ui->cb_Simulator->isChecked())
68  {
69  vm.addValue(CAircraftModel::IndexSimulatorInfo, ui->selector_SimulatorSelector->getValue());
70  }
71 
72  if (ui->cb_Mode->isChecked())
73  {
74  vm.addValue(CAircraftModel::IndexModelMode, ui->selector_ModeSelector->getMode());
75  }
76 
77  if (ui->cb_Parts->isChecked())
78  {
79  vm.addValue(CAircraftModel::IndexSupportedParts, ui->le_Parts->text().toUpper());
80  }
81 
82  if (ui->cb_CG->isChecked())
83  {
84  const QString cgv = ui->le_CG->text().trimmed();
85  CLength cg = CLength::null();
86  if (!cgv.isEmpty()) { cg.parseFromString(cgv, CPqString::SeparatorBestGuess); }
87  vm.addValue(CAircraftModel::IndexCG, cg);
88  }
89 
90  return vm;
91  }
92 
94  {
95  ui->le_Description->setText(model.getDescription());
96  ui->le_Name->setText(model.getName());
97  ui->selector_SimulatorSelector->setValue(model.getSimulator());
98  ui->selector_ModeSelector->setValue(model);
99  }
100 
102  {
103  // void
104  Q_UNUSED(readOnly);
105  // this->forceStyleSheetUpdate();
106  }
107 
109  {
110  const CAuthenticatedUser user(this->getSwiftDbUser());
111  if (user.hasAdminRole())
112  {
113  ui->selector_ModeSelector->setValue(CAircraftModel::Include);
114  ui->selector_ModeSelector->setReadOnly(false);
115  }
116  else { ui->selector_ModeSelector->setReadOnly(true); }
117 
119  }
120 
121  void CModelMappingModifyForm::returnPressed()
122  {
123  QCheckBox *cb = widgetToCheckbox(sender());
124  if (!cb) { return; }
125  cb->setChecked(true);
126  }
127 
128  void CModelMappingModifyForm::changed()
129  {
130  QCheckBox *cb = widgetToCheckbox(sender());
131  if (!cb) { return; }
132  cb->setChecked(true);
133  }
134 
135  QCheckBox *CModelMappingModifyForm::widgetToCheckbox(QObject *widget) const
136  {
137  if (!widget) { return nullptr; }
138  if (widget == ui->le_Description) { return ui->cb_Description; }
139  if (widget == ui->le_Name) { return ui->cb_Name; }
140  if (widget == ui->selector_ModeSelector) { return ui->cb_Mode; }
141  if (widget == ui->le_CG) { return ui->cb_CG; }
142  if (widget == ui->le_Parts) { return ui->cb_Parts; }
143  if (widget == ui->selector_SimulatorSelector) { return ui->cb_Simulator; }
144  return nullptr;
145  }
146 
147  void CModelMappingModifyForm::onCGEdited()
148  {
149  QString cgv = ui->le_CG->text().trimmed();
150  if (isDigitsOnlyString(cgv)) { cgv += "ft"; }
151  CLength cg = CLength::null();
152  cg.parseFromString(cgv, CPqString::SeparatorBestGuess);
153  ui->le_CG->setText(cg.isNull() ? "" : cg.toQString(true));
154  }
155 } // namespace swift::gui::editors
Form base class.
Definition: form.h:72
swift::misc::network::CAuthenticatedUser getSwiftDbUser() const
Authenticated DB user.
Definition: form.cpp:53
virtual void userChanged()
User has been changed.
Definition: form.cpp:55
Allows to modify individual fields of the model form.
swift::misc::CPropertyIndexVariantMap getValues() const
Get the values.
void setValue(const swift::misc::simulation::CAircraftModel &model)
Set value.
Specialized value object compliant map for variants, based on indexes.
void addValue(const CPropertyIndex &index, const CVariant &value)
Add a value.
QString toQString(bool i18n=false) const
Cast as QString.
Definition: mixinstring.h:74
Value object encapsulating information of an authentiated user.
Physical unit length (length)
Definition: length.h:18
void parseFromString(const QString &value)
Parse value from string.
Aircraft model (used by another pilot, my models on disk)
Definition: aircraftmodel.h:71
CSimulatorInfo getSimulator() const
Simulator info.
const QString & getName() const
Name.
const QString & getDescription() const
Descriptive text.
High level reusable GUI components.
Definition: aboutdialog.cpp:14
Free functions in swift::misc.
bool isDigitsOnlyString(const QString &testString)
String with digits only.
Definition: stringutils.h:167
void setChecked(bool)
void editingFinished()
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
QObject * sender() const const
bool isEmpty() const const