mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-04-03 15:45:46 +08:00
Ref T226, country selector
This commit is contained in:
78
src/blackgui/components/countryselector.cpp
Normal file
78
src/blackgui/components/countryselector.cpp
Normal file
@@ -0,0 +1,78 @@
|
||||
/* Copyright (C) 2018
|
||||
* swift project Community / Contributors
|
||||
*
|
||||
* This file is part of swift project. It is subject to the license terms in the LICENSE file found in the top-level
|
||||
* directory of this distribution and at http://www.swift-project.org/license.html. No part of swift project,
|
||||
* including this file, may be copied, modified, propagated, or distributed except according to the terms
|
||||
* contained in the LICENSE file.
|
||||
*/
|
||||
|
||||
#include "countryselector.h"
|
||||
#include "ui_countryselector.h"
|
||||
|
||||
#include "blackgui/guiapplication.h"
|
||||
#include "blackgui/uppercasevalidator.h"
|
||||
#include "blackcore/webdataservices.h"
|
||||
#include "blackmisc/countrylist.h"
|
||||
#include <QCompleter>
|
||||
|
||||
using namespace BlackMisc;
|
||||
using namespace BlackCore;
|
||||
|
||||
namespace BlackGui
|
||||
{
|
||||
namespace Components
|
||||
{
|
||||
CCountrySelector::CCountrySelector(QWidget *parent) :
|
||||
QFrame(parent),
|
||||
ui(new Ui::CCountrySelector)
|
||||
{
|
||||
Q_ASSERT_X(sGui, Q_FUNC_INFO, "Missing sGui");
|
||||
ui->setupUi(this);
|
||||
ui->le_Iso->setValidator(new CUpperCaseValidator(ui->le_Iso));
|
||||
|
||||
connect(sGui->getWebDataServices(), &CWebDataServices::swiftDbAllDataRead, this, &CCountrySelector::onCountriesLoaded);
|
||||
connect(ui->cb_Country, &QComboBox::currentTextChanged, this, &CCountrySelector::onCountryNameChanged);
|
||||
connect(ui->le_Iso, &QLineEdit::editingFinished, this, &CCountrySelector::onIsoChanged);
|
||||
|
||||
this->onCountriesLoaded();
|
||||
}
|
||||
|
||||
CCountrySelector::~CCountrySelector()
|
||||
{ }
|
||||
|
||||
void CCountrySelector::setCountry(const CCountry &country)
|
||||
{
|
||||
if (country == m_current) { return; }
|
||||
m_current = country;
|
||||
if (country.getIsoCode() != ui->le_Iso->text()) { ui->le_Iso->setText(country.getIsoCode()); }
|
||||
if (country.getName() != ui->cb_Country->currentText()) { ui->cb_Country->setCurrentText(country.getName()); }
|
||||
emit this->countryChanged(m_current);
|
||||
}
|
||||
|
||||
void CCountrySelector::onCountriesLoaded()
|
||||
{
|
||||
if (!sGui || !sGui->hasWebDataServices()) { return; }
|
||||
const CCountryList countries = sGui->getWebDataServices()->getCountries();
|
||||
|
||||
ui->le_Iso->setCompleter(new QCompleter(countries.toIsoList(true)));
|
||||
ui->cb_Country->clear();
|
||||
ui->cb_Country->addItems(countries.toNameList(true));
|
||||
}
|
||||
|
||||
void CCountrySelector::onIsoChanged()
|
||||
{
|
||||
if (!sGui || !sGui->hasWebDataServices()) { return; }
|
||||
const QString iso(ui->le_Iso->text().trimmed().toUpper());
|
||||
if (m_current.getIsoCode() == iso) { return; }
|
||||
this->setCountry(sGui->getWebDataServices()->getCountryForIsoCode(iso));
|
||||
}
|
||||
|
||||
void CCountrySelector::onCountryNameChanged(const QString &name)
|
||||
{
|
||||
if (!sGui || !sGui->hasWebDataServices()) { return; }
|
||||
if (m_current.getName() == name) { return; }
|
||||
this->setCountry(sGui->getWebDataServices()->getCountryForName(name));
|
||||
}
|
||||
} // ns
|
||||
} // ns
|
||||
63
src/blackgui/components/countryselector.h
Normal file
63
src/blackgui/components/countryselector.h
Normal file
@@ -0,0 +1,63 @@
|
||||
/* Copyright (C) 2018
|
||||
* swift project Community / Contributors
|
||||
*
|
||||
* This file is part of swift project. It is subject to the license terms in the LICENSE file found in the top-level
|
||||
* directory of this distribution and at http://www.swift-project.org/license.html. No part of swift project,
|
||||
* including this file, may be copied, modified, propagated, or distributed except according to the terms
|
||||
* contained in the LICENSE file.
|
||||
*/
|
||||
|
||||
//! \file
|
||||
|
||||
#ifndef BLACKGUI_COMPONENTS_COUNTRYSELECTOR_H
|
||||
#define BLACKGUI_COMPONENTS_COUNTRYSELECTOR_H
|
||||
|
||||
#include "blackmisc/country.h"
|
||||
#include <QFrame>
|
||||
#include <QScopedPointer>
|
||||
|
||||
namespace Ui { class CCountrySelector; }
|
||||
namespace BlackGui
|
||||
{
|
||||
namespace Components
|
||||
{
|
||||
/**
|
||||
* @brief Select a valid country
|
||||
*/
|
||||
class CCountrySelector : public QFrame
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
//! Constructor
|
||||
explicit CCountrySelector(QWidget *parent = nullptr);
|
||||
|
||||
//! Destructor
|
||||
virtual ~CCountrySelector();
|
||||
|
||||
//! Set country
|
||||
void setCountry(const BlackMisc::CCountry &country);
|
||||
|
||||
//! Get country
|
||||
const BlackMisc::CCountry &getCountry() const { return m_current; }
|
||||
|
||||
signals:
|
||||
//! Changed country
|
||||
void countryChanged(const BlackMisc::CCountry &country);
|
||||
|
||||
private:
|
||||
//! Countries loaded
|
||||
void onCountriesLoaded();
|
||||
|
||||
//! ISO has been changed
|
||||
void onIsoChanged();
|
||||
|
||||
//! Country name has been changed
|
||||
void onCountryNameChanged(const QString &name);
|
||||
|
||||
BlackMisc::CCountry m_current;
|
||||
QScopedPointer<Ui::CCountrySelector> ui;
|
||||
};
|
||||
} // ns
|
||||
} // ns
|
||||
#endif // guard
|
||||
50
src/blackgui/components/countryselector.ui
Normal file
50
src/blackgui/components/countryselector.ui
Normal file
@@ -0,0 +1,50 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<ui version="4.0">
|
||||
<class>CCountrySelector</class>
|
||||
<widget class="QFrame" name="CCountrySelector">
|
||||
<property name="geometry">
|
||||
<rect>
|
||||
<x>0</x>
|
||||
<y>0</y>
|
||||
<width>134</width>
|
||||
<height>22</height>
|
||||
</rect>
|
||||
</property>
|
||||
<property name="windowTitle">
|
||||
<string>Frame</string>
|
||||
</property>
|
||||
<layout class="QHBoxLayout" name="hl_CountrySelector" stretch="1,3">
|
||||
<property name="leftMargin">
|
||||
<number>0</number>
|
||||
</property>
|
||||
<property name="topMargin">
|
||||
<number>0</number>
|
||||
</property>
|
||||
<property name="rightMargin">
|
||||
<number>0</number>
|
||||
</property>
|
||||
<property name="bottomMargin">
|
||||
<number>0</number>
|
||||
</property>
|
||||
<item>
|
||||
<widget class="QLineEdit" name="le_Iso">
|
||||
<property name="maxLength">
|
||||
<number>5</number>
|
||||
</property>
|
||||
<property name="placeholderText">
|
||||
<string>ISO, e.g. "DE"</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QComboBox" name="cb_Country">
|
||||
<property name="editable">
|
||||
<bool>true</bool>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
</layout>
|
||||
</widget>
|
||||
<resources/>
|
||||
<connections/>
|
||||
</ui>
|
||||
Reference in New Issue
Block a user