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  CUpperCaseValidator *ucv =
36  new CUpperCaseValidator(true, 0, CAircraftModel::supportedParts().size(), ui->le_Parts);
37  ucv->setAllowedCharacters(CAircraftModel::supportedParts());
38  ui->le_Parts->setValidator(ucv);
39 
40  // "auto checked" disabled
41  // connect(ui->le_Description, &QLineEdit::returnPressed, this, &CModelMappingModifyForm::returnPressed);
42  // connect(ui->le_Name, &QLineEdit::returnPressed, this, &CModelMappingModifyForm::returnPressed);
43  // connect(ui->selector_ModeSelector, &CModelModeSelector::changed, this, &CModelMappingModifyForm::changed);
44  // connect(ui->selector_SimulatorSelector, &CSimulatorSelector::changed, this,
45  // &CModelMappingModifyForm::changed);
46 
47  connect(ui->le_CG, &QLineEdit::editingFinished, this, &CModelMappingModifyForm::onCGEdited);
48  }
49 
51  {
52  // void
53  }
54 
56  {
58 
59  if (ui->cb_Name->isChecked()) { vm.addValue(CAircraftModel::IndexName, ui->le_Name->text().trimmed()); }
60 
61  if (ui->cb_Description->isChecked())
62  {
63  vm.addValue(CAircraftModel::IndexDescription, ui->le_Description->text().trimmed());
64  }
65 
66  if (ui->cb_Mode->isChecked())
67  {
68  vm.addValue(CAircraftModel::IndexModelMode, ui->selector_ModeSelector->getMode());
69  }
70 
71  if (ui->cb_Simulator->isChecked())
72  {
73  vm.addValue(CAircraftModel::IndexSimulatorInfo, ui->selector_SimulatorSelector->getValue());
74  }
75 
76  if (ui->cb_Mode->isChecked())
77  {
78  vm.addValue(CAircraftModel::IndexModelMode, ui->selector_ModeSelector->getMode());
79  }
80 
81  if (ui->cb_Parts->isChecked())
82  {
83  vm.addValue(CAircraftModel::IndexSupportedParts, ui->le_Parts->text().toUpper());
84  }
85 
86  if (ui->cb_CG->isChecked())
87  {
88  const QString cgv = ui->le_CG->text().trimmed();
89  CLength cg = CLength::null();
90  if (!cgv.isEmpty()) { cg.parseFromString(cgv, CPqString::SeparatorBestGuess); }
91  vm.addValue(CAircraftModel::IndexCG, cg);
92  }
93 
94  return vm;
95  }
96 
98  {
99  ui->le_Description->setText(model.getDescription());
100  ui->le_Name->setText(model.getName());
101  ui->selector_SimulatorSelector->setValue(model.getSimulator());
102  ui->selector_ModeSelector->setValue(model);
103  }
104 
106  {
107  // void
108  Q_UNUSED(readOnly);
109  // this->forceStyleSheetUpdate();
110  }
111 
113  {
114  const CAuthenticatedUser user(this->getSwiftDbUser());
115  if (user.hasAdminRole())
116  {
117  ui->selector_ModeSelector->setValue(CAircraftModel::Include);
118  ui->selector_ModeSelector->setReadOnly(false);
119  }
120  else { ui->selector_ModeSelector->setReadOnly(true); }
121 
123  }
124 
125  void CModelMappingModifyForm::returnPressed()
126  {
127  QCheckBox *cb = widgetToCheckbox(sender());
128  if (!cb) { return; }
129  cb->setChecked(true);
130  }
131 
132  void CModelMappingModifyForm::changed()
133  {
134  QCheckBox *cb = widgetToCheckbox(sender());
135  if (!cb) { return; }
136  cb->setChecked(true);
137  }
138 
139  QCheckBox *CModelMappingModifyForm::widgetToCheckbox(QObject *widget) const
140  {
141  if (!widget) { return nullptr; }
142  if (widget == ui->le_Description) { return ui->cb_Description; }
143  if (widget == ui->le_Name) { return ui->cb_Name; }
144  if (widget == ui->selector_ModeSelector) { return ui->cb_Mode; }
145  if (widget == ui->le_CG) { return ui->cb_CG; }
146  if (widget == ui->le_Parts) { return ui->cb_Parts; }
147  if (widget == ui->selector_SimulatorSelector) { return ui->cb_Simulator; }
148  return nullptr;
149  }
150 
151  void CModelMappingModifyForm::onCGEdited()
152  {
153  QString cgv = ui->le_CG->text().trimmed();
154  if (isDigitsOnlyString(cgv)) { cgv += "ft"; }
155  CLength cg = CLength::null();
156  cg.parseFromString(cgv, CPqString::SeparatorBestGuess);
157  ui->le_CG->setText(cg.isNull() ? "" : cg.toQString(true));
158  }
159 } // namespace swift::gui::editors
void setAllowedCharacters(const QString &chars)
Allowed characters.
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.
virtual void userChanged()
User has been changed.
virtual void setReadOnly(bool readOnly)
Set editable.
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:76
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:13
Free functions in swift::misc.
bool isDigitsOnlyString(const QString &testString)
String with digits only.
Definition: stringutils.h:168