swift
dbcountryselectorcomponent.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 <QCompleter>
7 #include <QDragEnterEvent>
8 #include <QDragLeaveEvent>
9 #include <QDragMoveEvent>
10 #include <QDropEvent>
11 #include <QLabel>
12 #include <QLineEdit>
13 #include <QMetaType>
14 #include <QPalette>
15 #include <QPixmap>
16 #include <Qt>
17 
18 #include "ui_dbcountryselectorcomponent.h"
19 
20 #include "core/webdataservices.h"
21 #include "gui/guiapplication.h"
22 #include "gui/uppercasevalidator.h"
23 #include "misc/countrylist.h"
25 #include "misc/variant.h"
26 
27 using namespace swift::gui;
28 using namespace swift::core;
29 using namespace swift::misc;
30 using namespace swift::misc::network;
31 
32 namespace swift::gui::components
33 {
34  CDbCountrySelectorComponent::CDbCountrySelectorComponent(QWidget *parent)
35  : QFrame(parent), ui(new Ui::CDbCountrySelectorComponent)
36  {
37  ui->setupUi(this);
38  this->setFocusProxy(ui->le_CountryIso);
39  this->setAcceptDrops(true);
40  this->setAcceptedMetaTypeIds({ qMetaTypeId<CCountry>(), qMetaTypeId<CCountryList>() });
41 
42  connect(ui->le_CountryIso, &QLineEdit::returnPressed, this, &CDbCountrySelectorComponent::onDataChanged);
43  connect(ui->le_CountryName, &QLineEdit::returnPressed, this, &CDbCountrySelectorComponent::onDataChanged);
44  connect(ui->le_CountryIso, &QLineEdit::editingFinished, this, &CDbCountrySelectorComponent::onDataChanged);
45  connect(ui->le_CountryName, &QLineEdit::returnPressed, this, &CDbCountrySelectorComponent::onDataChanged);
46 
47  ui->le_CountryIso->setValidator(new CUpperCaseValidator(this));
48  connect(sGui->getWebDataServices(), &CWebDataServices::dataRead, this,
49  &CDbCountrySelectorComponent::onCountriesRead, Qt::QueuedConnection);
50  this->onCountriesRead(CEntityFlags::DistributorEntity, CEntityFlags::ReadFinished,
52  }
53 
55 
57  {
58  ui->le_CountryIso->setText(country.getIsoCode());
59  ui->le_CountryName->setText(country.getName());
60  ui->lbl_CountryIcon->setPixmap(CIcon(country.toIcon()));
61  if (country != m_currentCountry)
62  {
63  m_currentCountry = country;
64  emit countryChanged(country);
65  }
66  }
67 
68  void CDbCountrySelectorComponent::setCountry(const QString &isoCode)
69  {
71  this->setCountry(c);
72  }
73 
75  {
76  if (!sGui) { return CCountry(); }
77  const QString iso(ui->le_CountryIso->text().trimmed().toUpper());
78  const QString name(ui->le_CountryName->text().trimmed());
79  if (CCountry::isValidIsoCode(iso)) { return sGui->getWebDataServices()->getCountryForIsoCode(iso); }
80  else
81  {
82  if (name.isEmpty()) { return CCountry(); }
83  return sGui->getWebDataServices()->getCountryForName(name);
84  }
85  }
86 
88  {
89  ui->le_CountryIso->setReadOnly(readOnly);
90  ui->le_CountryName->setReadOnly(readOnly);
91  ui->le_CountryName->setEnabled((!readOnly));
92  ui->lbl_CountryIcon->setVisible(!readOnly);
93  this->setEnabled(!readOnly);
94  }
95 
96  bool CDbCountrySelectorComponent::isSet() const { return this->getCountry().isValid(); }
97 
99  {
100  ui->le_CountryIso->clear();
101  ui->le_CountryName->clear();
102  ui->lbl_CountryIcon->setPixmap(QPixmap());
103  }
104 
105  void CDbCountrySelectorComponent::dragEnterEvent(QDragEnterEvent *event)
106  {
107  if (!event || !acceptDrop(event->mimeData())) { return; }
108  setBackgroundRole(QPalette::Highlight);
109  event->acceptProposedAction();
110  }
111 
112  void CDbCountrySelectorComponent::dragMoveEvent(QDragMoveEvent *event)
113  {
114  if (!event || !acceptDrop(event->mimeData())) { return; }
115  event->acceptProposedAction();
116  }
117 
118  void CDbCountrySelectorComponent::dragLeaveEvent(QDragLeaveEvent *event)
119  {
120  if (!event) { return; }
121  event->accept();
122  }
123 
125  {
126  if (!event || !acceptDrop(event->mimeData())) { return; }
127  const CVariant valueVariant(toCVariant(event->mimeData()));
128  if (valueVariant.isValid())
129  {
130  if (valueVariant.canConvert<CCountry>())
131  {
132  const CCountry country(valueVariant.value<CCountry>());
133  if (!country.hasIsoCode()) { return; }
134  this->setCountry(country);
135  }
136  else if (valueVariant.canConvert<CCountryList>())
137  {
138  const CCountryList countries(valueVariant.value<CCountryList>());
139  if (countries.isEmpty()) { return; }
140  this->setCountry(countries.front());
141  }
142  }
143  }
144 
145  void CDbCountrySelectorComponent::onCountriesRead(CEntityFlags::Entity entity, CEntityFlags::ReadState readState,
146  int count)
147  {
148  if (!sGui || sGui->isShuttingDown() || !sGui->hasWebDataServices()) { return; }
149  if (entity.testFlag(CEntityFlags::CountryEntity) && CEntityFlags::isFinishedReadState(readState))
150  {
151  if (count > 0)
152  {
153  QCompleter *c = new QCompleter(sGui->getWebDataServices()->getCountries().toNameList(), this);
154  c->setCaseSensitivity(Qt::CaseInsensitive);
155  c->setCompletionMode(QCompleter::PopupCompletion);
156  c->setMaxVisibleItems(10);
157  connect(c, qOverload<const QString &>(&QCompleter::activated), this,
158  &CDbCountrySelectorComponent::onCompleterActivated);
159 
160  ui->le_CountryName->setCompleter(c);
161  m_completerCountryNames.reset(c); // deletes any old completer
162  }
163  else { m_completerCountryNames.reset(nullptr); }
164  }
165  }
166 
167  void CDbCountrySelectorComponent::onDataChanged()
168  {
169  if (!sGui || sGui->isShuttingDown() || !sGui->hasWebDataServices()) { return; }
170  QObject *sender = this->sender();
171  if (sender == ui->le_CountryIso)
172  {
173  QString iso(ui->le_CountryIso->text().trimmed().toUpper());
174  if (CCountry::isValidIsoCode(iso))
175  {
177  }
178  }
179  else if (sender == ui->le_CountryName)
180  {
181  QString name(ui->le_CountryName->text().trimmed());
182  if (!name.isEmpty()) { this->setCountry(sGui->getWebDataServices()->getCountryForName(name)); }
183  }
184  }
185 
186  void CDbCountrySelectorComponent::onCompleterActivated(const QString &countryName)
187  {
188  ui->le_CountryName->setText(countryName);
189  this->setCountry(sGui->getWebDataServices()->getCountryForName(countryName));
190  }
191 
192 } // namespace swift::gui::components
bool hasWebDataServices() const
Web data services available?
bool isShuttingDown() const
Is application shutting down?
CWebDataServices * getWebDataServices() const
Get the web data services.
swift::misc::CCountryList getCountries() const
Countries.
int getCountriesCount() const
Country count.
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 ..)
bool acceptDrop(const QMimeData *mime) const
Accept drop?
Definition: dropbase.cpp:24
swift::misc::CVariant toCVariant(const QMimeData *mime) const
Mime data to CVariant (normally encapsulating a value object)
Definition: dropbase.cpp:43
void setAcceptedMetaTypeIds(const QList< int > &ids)
Accepted ids.
Definition: dropbase.cpp:20
void countryChanged(const swift::misc::CCountry &country)
Country has been changed.
void setCountry(const swift::misc::CCountry &country)
Current country.
const QString & getName() const
Country name.
Definition: country.h:73
bool isValid() const
Valid?
Definition: country.cpp:98
bool hasIsoCode() const
ISO code?
Definition: country.cpp:49
const QString & getIsoCode() const
DB ISO code.
Definition: country.h:55
CIcons::IconIndex toIcon() const
Representing icon.
Definition: country.cpp:24
Value object encapsulating a list of countries.
Definition: countrylist.h:32
QStringList toNameList(bool sorted=false) const
Name string list.
Definition: countrylist.cpp:85
Value object for icons. An icon is stored in the global icon repository and identified by its index....
Definition: icon.h:39
reference front()
Access the first element.
Definition: sequence.h:225
bool isEmpty() const
Synonym for empty.
Definition: sequence.h:285
Wrapper around QVariant which provides transparent access to CValueObject methods of the contained ob...
Definition: variant.h:66
T value() const
Return the value converted to the type T.
Definition: variant.h:169
bool isValid() const
True if this variant is valid.
Definition: variant.h:252
bool canConvert(int typeId) const
True if this variant can be converted to the type with the given metatype ID.
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
GUI related classes.
Free functions in swift::misc.