swift
modelmappingform.cpp
1 // SPDX-FileCopyrightText: Copyright (C) 2015 swift Project Community / Contributors
2 // SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-swift-pilot-client-1
3 
5 
6 #include <QLineEdit>
7 #include <QPushButton>
8 
9 #include "ui_modelmappingform.h"
10 
14 #include "gui/labelandicon.h"
15 #include "gui/uppercasevalidator.h"
16 #include "misc/icons.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  CModelMappingForm::CModelMappingForm(QWidget *parent) : CFormDbUser(parent), ui(new Ui::CModelMappingForm)
29  {
30  ui->setupUi(this);
31  ui->le_LastUpdated->setReadOnly(true);
32  ui->le_Id->setReadOnly(true);
33  ui->le_Parts->setPlaceholderText("Allowed: " + CAircraftModel::supportedParts());
34  ui->lai_Id->set(CIcons::appMappings16(), "Id:");
35  CUpperCaseValidator *uc = new CUpperCaseValidator(0, 5, ui->le_Parts);
36  uc->setAllowedCharacters(CAircraftModel::supportedParts());
37  ui->le_Parts->setValidator(uc);
38 
39  connect(ui->le_CG, &QLineEdit::editingFinished, this, &CModelMappingForm::onCgEditFinished);
40  connect(ui->pb_Stash, &QPushButton::clicked, this, &CModelMappingForm::requestStash);
41 
42  // for setting mode (include/exclude)
43  this->userChanged();
44  }
45 
47 
49  {
50  CAircraftModel model(m_originalModel);
51  model.setSimulator(ui->selector_Simulator->getValue());
52  model.setDescription(ui->le_Description->text());
53  model.setModelString(ui->le_ModelKey->text());
54  model.setName(ui->le_Name->text());
55  model.setModelMode(ui->selector_ModelMode->getMode());
56  model.setCG(this->getCGFromUI());
57  model.setSupportedParts(ui->le_Parts->text().trimmed().toUpper());
58  return model;
59  }
60 
61  CStatusMessageList CModelMappingForm::validate(bool withNestedObjects) const
62  {
63  const CAircraftModel model(getValue());
64  const CStatusMessageList msgs(model.validate(withNestedObjects));
65  ui->val_Indicator->setState(msgs);
66  return msgs;
67  }
68 
69  void CModelMappingForm::setReadOnly(bool readOnly)
70  {
71  ui->le_Description->setReadOnly(readOnly);
72  ui->le_ModelKey->setReadOnly(readOnly);
73  ui->le_Name->setReadOnly(readOnly);
74  this->forceStyleSheetUpdate();
75  }
76 
78 
80  {
81  ui->le_ModelKey->setText(model.getModelString());
82  ui->le_LastUpdated->setText(model.getFormattedUtcTimestampYmdhms());
83  ui->le_Id->setText(model.getDbKeyAsString());
84  ui->le_Description->setText(model.getDescription());
85  ui->le_Name->setText(model.getName());
86  ui->le_Parts->setText(model.getSupportedParts());
87  ui->le_FileName->setText(model.getFileName());
88  ui->selector_ModelMode->setValue(model.getModelMode());
89  ui->selector_Simulator->setValue(model.getSimulator());
90  this->setCGtoUI(model.getCG());
91  m_originalModel = model;
92  }
93 
95  {
96  const CAuthenticatedUser user(this->getSwiftDbUser());
97  if (user.hasAdminRole())
98  {
99  ui->selector_ModelMode->setValue(CAircraftModel::Include);
100  ui->selector_ModelMode->setReadOnly(false);
101  }
102  else { ui->selector_ModelMode->setReadOnly(true); }
103 
105  }
106 
107  CLength CModelMappingForm::getCGFromUI() const
108  {
109  if (ui->le_CG->text().isEmpty()) { return CLength::null(); }
110  const QString v = ui->le_CG->text();
111 
112  // without unit we assume ft
113  if (isDigitsOnlyString(v))
114  {
115  bool ok;
116  const double cgv = v.toDouble(&ok);
117  if (!ok) { return CLength::null(); }
118  return CLength(cgv, CLengthUnit::ft());
119  }
120 
121  CLength cg;
122  cg.parseFromString(v, CPqString::SeparatorBestGuess);
123  return cg;
124  }
125 
126  void CModelMappingForm::setCGtoUI(const CLength &cg)
127  {
128  if (cg.isNull()) { ui->le_CG->clear(); }
129  else { ui->le_CG->setText(cg.valueRoundedWithUnit(CLengthUnit::ft(), 1)); }
130  }
131 
132  void CModelMappingForm::onCgEditFinished()
133  {
134  const CLength cg = this->getCGFromUI();
135  this->setCGtoUI(cg);
136  }
137 } // 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
void forceStyleSheetUpdate()
Forces a stylesheet update.
Definition: form.cpp:47
void setValue(swift::misc::simulation::CAircraftModel &model)
Set model.
virtual swift::misc::CStatusMessageList validate(bool withNestedObjects) const
Validate, empty list means OK.
swift::misc::simulation::CAircraftModel getValue() const
Value.
void requestStash()
Request stashing for model.
virtual void userChanged()
User has been changed.
virtual void setSelectOnly()
Read only, but entity can be selected (normally used in mapping). Use setReadOnly to reset this very ...
virtual void setReadOnly(bool readonly)
Set editable.
Status messages, e.g. from Core -> GUI.
QString getFormattedUtcTimestampYmdhms() const
As yyyy MM dd HH mm ss.
QString getDbKeyAsString() const
DB key as string.
Definition: datastore.cpp:30
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.
QString valueRoundedWithUnit(const MU &unit, int digits=-1, bool withGroupSeparator=false, bool i18n=false) const
Value to QString with the given unit, e.g. "5.00m".
Aircraft model (used by another pilot, my models on disk)
Definition: aircraftmodel.h:71
void setName(const QString &name)
Name.
ModelMode getModelMode() const
Model mode.
const QString & getSupportedParts() const
Supported parts.
void setModelString(const QString &modelString)
Model string.
CSimulatorInfo getSimulator() const
Simulator info.
const QString & getModelString() const
Model key, either queried or loaded from simulator model.
void setSupportedParts(const QString &supportedParts)
Supported parts.
const physical_quantities::CLength & getCG() const
Get center of gravity.
const QString & getName() const
Name.
const QString & getDescription() const
Descriptive text.
void setDescription(const QString &description)
Descriptive text.
void setSimulator(const CSimulatorInfo &simulator)
Set simulator info.
void setCG(const physical_quantities::CLength &cg)
Get center of gravity.
CStatusMessageList validate(bool withNestedObjects) const
Validate.
void setModelMode(ModelMode mode)
Set model mode.
const QString & getFileName() const
File name (corresponding data for simulator, only available if representing simulator model.
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