swift
countryselector.cpp
1 // SPDX-FileCopyrightText: Copyright (C) 2018 swift Project Community / Contributors
2 // SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-swift-pilot-client-1
3 
4 #include "countryselector.h"
5 
6 #include <QCompleter>
7 
8 #include "ui_countryselector.h"
9 
10 #include "core/webdataservices.h"
11 #include "gui/guiapplication.h"
12 #include "gui/uppercasevalidator.h"
13 #include "misc/countrylist.h"
14 
15 using namespace swift::misc;
16 using namespace swift::core;
17 
18 namespace swift::gui::components
19 {
20  CCountrySelector::CCountrySelector(QWidget *parent) : QFrame(parent), ui(new Ui::CCountrySelector)
21  {
22  Q_ASSERT_X(sGui, Q_FUNC_INFO, "Missing sGui");
23  ui->setupUi(this);
24  ui->le_Iso->setValidator(new CUpperCaseValidator(ui->le_Iso));
25 
26  connect(sGui->getWebDataServices(), &CWebDataServices::swiftDbAllDataRead, this,
27  &CCountrySelector::onCountriesLoaded);
28  connect(ui->cb_Country, &QComboBox::currentTextChanged, this, &CCountrySelector::onCountryNameChanged);
29  connect(ui->le_Iso, &QLineEdit::editingFinished, this, &CCountrySelector::onIsoChanged);
30 
31  this->onCountriesLoaded();
32  }
33 
35 
37  {
38  if (country == m_current) { return; }
39  m_current = country;
40  if (country.getIsoCode() != ui->le_Iso->text()) { ui->le_Iso->setText(country.getIsoCode()); }
41  if (country.getName() != ui->cb_Country->currentText()) { ui->cb_Country->setCurrentText(country.getName()); }
42  emit this->countryChanged(m_current);
43  }
44 
45  void CCountrySelector::onCountriesLoaded()
46  {
47  if (!sGui || !sGui->hasWebDataServices()) { return; }
48  const CCountryList countries = sGui->getWebDataServices()->getCountries();
49 
50  ui->le_Iso->setCompleter(new QCompleter(countries.toIsoList(true)));
51  ui->cb_Country->clear();
52  ui->cb_Country->addItems(countries.toNameList(true));
53  }
54 
55  void CCountrySelector::onIsoChanged()
56  {
57  if (!sGui || !sGui->hasWebDataServices()) { return; }
58  const QString iso(ui->le_Iso->text().trimmed().toUpper());
59  if (m_current.getIsoCode() == iso) { return; }
61  }
62 
63  void CCountrySelector::onCountryNameChanged(const QString &name)
64  {
65  if (!sGui || !sGui->hasWebDataServices()) { return; }
66  if (m_current.getName() == name) { return; }
68  }
69 } // namespace swift::gui::components
bool hasWebDataServices() const
Web data services available?
CWebDataServices * getWebDataServices() const
Get the web data services.
swift::misc::CCountryList getCountries() const
Countries.
swift::misc::CCountry getCountryForIsoCode(const QString &iso) const
Country by ISO code (GB, US...)
swift::misc::CCountry getCountryForName(const QString &name) const
Country by name (France, China ..)
void countryChanged(const swift::misc::CCountry &country)
Changed country.
void setCountry(const swift::misc::CCountry &country)
Set country.
const QString & getName() const
Country name.
Definition: country.h:73
const QString & getIsoCode() const
DB ISO code.
Definition: country.h:55
Value object encapsulating a list of countries.
Definition: countrylist.h:32
QStringList toNameList(bool sorted=false) const
Name string list.
Definition: countrylist.cpp:85
QStringList toIsoList(bool sorted=false) const
All ISO codes.
Definition: countrylist.cpp:98
SWIFT_GUI_EXPORT swift::gui::CGuiApplication * sGui
Single instance of GUI application object.
Backend services of the swift project, like dealing with the network or the simulators.
Definition: actionbind.cpp:7
High level reusable GUI components.
Definition: aboutdialog.cpp:13
Free functions in swift::misc.