swift
selcalcodeselector.cpp
1 // SPDX-FileCopyrightText: Copyright (C) 2013 swift Project Community / Contributors
2 // SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-swift-pilot-client-1
3 
5 
6 #include <QComboBox>
7 #include <QStringList>
8 #include <QtGlobal>
9 
10 #include "ui_selcalcodeselector.h"
11 
12 #include "gui/ticklabel.h"
13 #include "misc/aviation/selcal.h"
14 
15 using namespace swift::misc;
16 using namespace swift::misc::aviation;
17 
18 namespace swift::gui::components
19 {
20  CSelcalCodeSelector::CSelcalCodeSelector(QWidget *parent) : QFrame(parent), ui(new Ui::CSelcalCodeSelector)
21  {
22  ui->setupUi(this);
23  this->resetSelcalCodes(true);
24  this->setValidityHint();
25  ui->lblp_ValidCodeIcon->setToolTips("valid SELCAL", "invalid SELCAL");
26 
27  // limit number of elements: https://forum.qt.io/topic/11315/limit-the-number-of-visible-items-on-qcombobox/6
28  ui->cb_SelcalPairs1->setStyleSheet("combobox-popup: 0;");
29  ui->cb_SelcalPairs2->setStyleSheet("combobox-popup: 0;");
30 
31  connect(ui->cb_SelcalPairs1, qOverload<int>(&QComboBox::currentIndexChanged), this,
32  &CSelcalCodeSelector::selcalIndexChanged);
33  connect(ui->cb_SelcalPairs2, qOverload<int>(&QComboBox::currentIndexChanged), this,
34  &CSelcalCodeSelector::selcalIndexChanged);
35  }
36 
38 
40  {
41  QString selcal = ui->cb_SelcalPairs1->currentText();
42  selcal.append(ui->cb_SelcalPairs2->currentText());
43  return selcal;
44  }
45 
47  {
48  CSelcal selcal(getSelcalCode());
49  return selcal;
50  }
51 
52  void CSelcalCodeSelector::resetSelcalCodes(bool allowEmptyValue)
53  {
54  ui->cb_SelcalPairs1->clear();
55  if (allowEmptyValue) ui->cb_SelcalPairs1->addItem(" ");
56  ui->cb_SelcalPairs1->addItems(swift::misc::aviation::CSelcal::codePairs());
57  ui->cb_SelcalPairs2->clear();
58  if (allowEmptyValue) ui->cb_SelcalPairs2->addItem(" ");
59  ui->cb_SelcalPairs2->addItems(swift::misc::aviation::CSelcal::codePairs());
60  }
61 
62  void CSelcalCodeSelector::setSelcalCode(const QString &selcal)
63  {
64  const QString s = selcal.isEmpty() ? " " : selcal.toUpper().trimmed();
65  if (s.length() != 4) { return; } // still incomplete code
66  if (this->getSelcalCode() == s) { return; } // avoid unintended signals
67  const QString s1 = s.left(2);
68  const QString s2 = s.right(2);
69  if (swift::misc::aviation::CSelcal::codePairs().contains(s1)) { ui->cb_SelcalPairs1->setCurrentText(s1); }
70  if (swift::misc::aviation::CSelcal::codePairs().contains(s2)) { ui->cb_SelcalPairs2->setCurrentText(s2); }
71  }
72 
74  {
75  this->setSelcalCode(selcal.getCode());
76  }
77 
79  {
80  const QString s = this->getSelcalCode();
81  if (s.length() != 4) return false;
83  }
84 
86  {
87  if (ui->cb_SelcalPairs1->count() < 1) { this->resetSelcalCodes(true); }
88  ui->cb_SelcalPairs1->setCurrentIndex(0);
89  ui->cb_SelcalPairs2->setCurrentIndex(0);
90  }
91 
92  int CSelcalCodeSelector::getComboBoxHeight() const { return ui->cb_SelcalPairs1->height(); }
93 
95  {
96  ui->cb_SelcalPairs1->setMinimumHeight(h);
97  ui->cb_SelcalPairs2->setMinimumHeight(h);
98  }
99 
100  void CSelcalCodeSelector::selcalIndexChanged(int index)
101  {
102  Q_UNUSED(index);
103  this->setValidityHint();
104  emit valueChanged();
105  }
106 
107  void CSelcalCodeSelector::setValidityHint() { ui->lblp_ValidCodeIcon->setTicked(this->hasValidCode()); }
108 } // namespace swift::gui::components
void valueChanged()
Value has been changed.
void setComboBoxMinimumHeight(int h)
Set the combobox height.
swift::misc::aviation::CSelcal getSelcal() const
SELCAL.
int getComboBoxHeight() const
The height of the combobox.
void resetSelcalCodes(bool allowEmptyValue=false)
Reset the SELCAL code.
void setSelcalCode(const QString &selcal)
Set the SELCAL code.
void setSelcal(const swift::misc::aviation::CSelcal &selcal)
Set the SELCAL code.
Value object for SELCAL.
Definition: selcal.h:31
const QString & getCode() const
Get SELCAL code.
Definition: selcal.h:46
static const QStringList & codePairs()
All valid code pairs: AB, AC, AD ...
Definition: selcal.cpp:109
static bool isValidCode(const QString &code)
Valid SELCAL code?
Definition: selcal.cpp:54
High level reusable GUI components.
Definition: aboutdialog.cpp:13
Free functions in swift::misc.