mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-04-11 23:05:34 +08:00
refs #452 country selector component UI widget
This commit is contained in:
committed by
Mathew Sutcliffe
parent
cd259259c8
commit
f29929111b
204
src/blackgui/components/dbcountryselectorcomponent.cpp
Normal file
204
src/blackgui/components/dbcountryselectorcomponent.cpp
Normal file
@@ -0,0 +1,204 @@
|
||||
/* Copyright (C) 2015
|
||||
* 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 "dbcountryselectorcomponent.h"
|
||||
#include "ui_dbcountryselectorcomponent.h"
|
||||
#include "blackgui/guiutility.h"
|
||||
#include <QMimeData>
|
||||
|
||||
using namespace BlackGui;
|
||||
using namespace BlackMisc;
|
||||
using namespace BlackMisc::Network;
|
||||
|
||||
namespace BlackGui
|
||||
{
|
||||
namespace Components
|
||||
{
|
||||
CDbCountrySelectorComponent::CDbCountrySelectorComponent(QWidget *parent) :
|
||||
QFrame(parent),
|
||||
ui(new Ui::CDbCountrySelectorComponent)
|
||||
{
|
||||
ui->setupUi(this);
|
||||
this->setAcceptDrops(true);
|
||||
this->setAcceptedMetaTypeIds({qMetaTypeId<CCountry>(), qMetaTypeId<CCountryList>()});
|
||||
|
||||
connect(ui->le_CountryIso, &QLineEdit::returnPressed, this, &CDbCountrySelectorComponent::ps_dataChanged);
|
||||
connect(ui->le_CountryName, &QLineEdit::returnPressed, this, &CDbCountrySelectorComponent::ps_dataChanged);
|
||||
connect(ui->le_CountryIso, &QLineEdit::editingFinished, this, &CDbCountrySelectorComponent::ps_dataChanged);
|
||||
connect(ui->le_CountryName, &QLineEdit::returnPressed, this, &CDbCountrySelectorComponent::ps_dataChanged);
|
||||
}
|
||||
|
||||
CDbCountrySelectorComponent::~CDbCountrySelectorComponent()
|
||||
{
|
||||
gracefulShutdown();
|
||||
}
|
||||
|
||||
void CDbCountrySelectorComponent::setProvider(Network::IWebDataServicesProvider *webDataReaderProvider)
|
||||
{
|
||||
if (!webDataReaderProvider) { return; }
|
||||
CWebDataServicesAware::setProvider(webDataReaderProvider);
|
||||
webDataReaderProvider->connectSwiftDatabaseSignals(
|
||||
this,
|
||||
std::bind(&CDbCountrySelectorComponent::ps_CountriesRead, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3)
|
||||
);
|
||||
int c = this->getCountriesCount();
|
||||
if (c > 0)
|
||||
{
|
||||
this->ps_CountriesRead(CDbFlags::CountryEntity, CDbFlags::ReadFinished, c);
|
||||
}
|
||||
}
|
||||
|
||||
void CDbCountrySelectorComponent::setCountry(const BlackMisc::CCountry &country)
|
||||
{
|
||||
this->ui->le_CountryIso->setText(country.getIsoCode());
|
||||
this->ui->le_CountryName->setText(country.getName());
|
||||
this->ui->lbl_CountryIcon->setPixmap(country.toPixmap());
|
||||
if (country != m_currentCountry)
|
||||
{
|
||||
m_currentCountry = country;
|
||||
emit countryChanged(country);
|
||||
}
|
||||
}
|
||||
|
||||
void CDbCountrySelectorComponent::setCountry(const QString &isoCode)
|
||||
{
|
||||
CCountry c(this->getCountryForIsoCode(isoCode));
|
||||
this->setCountry(c);
|
||||
}
|
||||
|
||||
BlackMisc::CCountry CDbCountrySelectorComponent::getCountry() const
|
||||
{
|
||||
if (!hasProvider()) { return CCountry(); }
|
||||
QString iso(this->ui->le_CountryIso->text().trimmed().toUpper());
|
||||
QString name(this->ui->le_CountryName->text().trimmed());
|
||||
if (CCountry::isValidIsoCode(iso))
|
||||
{
|
||||
return this->getCountryForIsoCode(iso);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (name.isEmpty()) { return CCountry(); }
|
||||
return this->getCountryForName(name);
|
||||
}
|
||||
}
|
||||
|
||||
void CDbCountrySelectorComponent::setReadOnly(bool readOnly)
|
||||
{
|
||||
this->ui->le_CountryIso->setReadOnly(readOnly);
|
||||
this->ui->le_CountryName->setReadOnly(readOnly);
|
||||
this->ui->lbl_CountryIcon->setVisible(!readOnly);
|
||||
}
|
||||
|
||||
bool CDbCountrySelectorComponent::isSet() const
|
||||
{
|
||||
return this->getCountry().isValid();
|
||||
}
|
||||
|
||||
void CDbCountrySelectorComponent::clear()
|
||||
{
|
||||
this->ui->le_CountryIso->clear();
|
||||
this->ui->le_CountryName->clear();
|
||||
this->ui->lbl_CountryIcon->setPixmap(QPixmap());
|
||||
}
|
||||
|
||||
void CDbCountrySelectorComponent::dragEnterEvent(QDragEnterEvent *event)
|
||||
{
|
||||
if (!event || !acceptDrop(event->mimeData())) { return; }
|
||||
setBackgroundRole(QPalette::Highlight);
|
||||
event->acceptProposedAction();
|
||||
}
|
||||
|
||||
void CDbCountrySelectorComponent::dragMoveEvent(QDragMoveEvent *event)
|
||||
{
|
||||
if (!event || !acceptDrop(event->mimeData())) { return; }
|
||||
event->acceptProposedAction();
|
||||
}
|
||||
|
||||
void CDbCountrySelectorComponent::dragLeaveEvent(QDragLeaveEvent *event)
|
||||
{
|
||||
if (!event) { return; }
|
||||
event->accept();
|
||||
}
|
||||
|
||||
void CDbCountrySelectorComponent::dropEvent(QDropEvent *event)
|
||||
{
|
||||
if (!event || !acceptDrop(event->mimeData())) { return; }
|
||||
CVariant valueVariant(toCVariant(event->mimeData()));
|
||||
if (valueVariant.isValid())
|
||||
{
|
||||
if (valueVariant.canConvert<CCountry>())
|
||||
{
|
||||
CCountry country(valueVariant.value<CCountry>());
|
||||
if (!country.hasIsoCode()) { return; }
|
||||
this->setCountry(country);
|
||||
}
|
||||
else if (valueVariant.canConvert<CCountryList>())
|
||||
{
|
||||
CCountryList countries(valueVariant.value<CCountryList>());
|
||||
if (countries.isEmpty()) { return; }
|
||||
this->setCountry(countries.front());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void CDbCountrySelectorComponent::ps_CountriesRead(CDbFlags::Entity entity, CDbFlags::ReadState readState, int count)
|
||||
{
|
||||
if (!hasProvider()) { return; }
|
||||
if (entity.testFlag(CDbFlags::DistributorEntity) && readState == CDbFlags::ReadFinished)
|
||||
{
|
||||
if (count > 0)
|
||||
{
|
||||
QCompleter *c = new QCompleter(this->getCountries().toNameList(), this);
|
||||
c->setCaseSensitivity(Qt::CaseInsensitive);
|
||||
c->setCompletionMode(QCompleter::PopupCompletion);
|
||||
c->setMaxVisibleItems(10);
|
||||
this->connect(c, static_cast<void (QCompleter::*)(const QString &)>(&QCompleter::activated), this, &CDbCountrySelectorComponent::ps_completerActivated);
|
||||
|
||||
this->ui->le_CountryName->setCompleter(c);
|
||||
m_completerCountryNames.reset(c); // deletes any old completer
|
||||
this->setReadOnly(false);
|
||||
}
|
||||
else
|
||||
{
|
||||
this->m_completerCountryNames.reset(nullptr);
|
||||
this->setReadOnly(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void CDbCountrySelectorComponent::ps_dataChanged()
|
||||
{
|
||||
if (!hasProvider()) { return; }
|
||||
QObject *sender = this->sender();
|
||||
if (sender == this->ui->le_CountryIso)
|
||||
{
|
||||
QString iso(this->ui->le_CountryIso->text().trimmed().toUpper());
|
||||
if (CCountry::isValidIsoCode(iso))
|
||||
{
|
||||
this->setCountry(getCountryForIsoCode(iso));
|
||||
}
|
||||
}
|
||||
else if (sender == this->ui->le_CountryName)
|
||||
{
|
||||
QString name(this->ui->le_CountryName->text().trimmed());
|
||||
if (!name.isEmpty())
|
||||
{
|
||||
this->setCountry(getCountryForName(name));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void CDbCountrySelectorComponent::ps_completerActivated(const QString &countryName)
|
||||
{
|
||||
this->ui->le_CountryName->setText(countryName);
|
||||
this->setCountry(getCountryForName(countryName));
|
||||
}
|
||||
|
||||
}// class
|
||||
} // ns
|
||||
101
src/blackgui/components/dbcountryselectorcomponent.h
Normal file
101
src/blackgui/components/dbcountryselectorcomponent.h
Normal file
@@ -0,0 +1,101 @@
|
||||
/* Copyright (C) 2015
|
||||
* 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_DBCOUNTRYSELECTORCOMPONENT_H
|
||||
#define BLACKGUI_DBCOUNTRYSELECTORCOMPONENT_H
|
||||
|
||||
#include "blackgui/blackguiexport.h"
|
||||
#include "blackcore/webdataservices.h"
|
||||
#include "blackgui/dropbase.h"
|
||||
#include "blackmisc/country.h"
|
||||
#include <QFrame>
|
||||
#include <QScopedPointer>
|
||||
#include <QCompleter>
|
||||
|
||||
namespace Ui { class CDbCountrySelectorComponent; }
|
||||
|
||||
namespace BlackGui
|
||||
{
|
||||
namespace Components
|
||||
{
|
||||
/*!
|
||||
* Country selector
|
||||
*/
|
||||
class BLACKGUI_EXPORT CDbCountrySelectorComponent :
|
||||
public QFrame,
|
||||
public BlackMisc::Network::CWebDataServicesAware,
|
||||
public BlackGui::CDropBase
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
//! Constructor
|
||||
explicit CDbCountrySelectorComponent(QWidget *parent = nullptr);
|
||||
|
||||
//! Destructor
|
||||
~CDbCountrySelectorComponent();
|
||||
|
||||
//! \copydoc CWebDataReaderAware::setProvider
|
||||
virtual void setProvider(BlackMisc::Network::IWebDataServicesProvider *webDataReaderProvider) override;
|
||||
|
||||
//! Current country
|
||||
void setCountry(const BlackMisc::CCountry &country);
|
||||
|
||||
//! Current country
|
||||
void setCountry(const QString &isoCode);
|
||||
|
||||
//! Country
|
||||
BlackMisc::CCountry getCountry() const;
|
||||
|
||||
//! Read only
|
||||
void setReadOnly(bool readOnly);
|
||||
|
||||
//! Set with valid country
|
||||
bool isSet() const;
|
||||
|
||||
//! Clear selection
|
||||
void clear();
|
||||
|
||||
signals:
|
||||
//! Country has been changed
|
||||
void countryChanged(const BlackMisc::CCountry &country);
|
||||
|
||||
protected:
|
||||
//! \copydoc QWidget::dragEnterEvent
|
||||
virtual void dragEnterEvent(QDragEnterEvent *event) override;
|
||||
|
||||
//! \copydoc QWidget::dragMoveEvent
|
||||
virtual void dragMoveEvent(QDragMoveEvent *event) override;
|
||||
|
||||
//! \copydoc QWidget::dragLeaveEvent
|
||||
virtual void dragLeaveEvent(QDragLeaveEvent *event) override;
|
||||
|
||||
//! \copydoc QWidget::dropEvent
|
||||
virtual void dropEvent(QDropEvent *event) override;
|
||||
|
||||
private slots:
|
||||
//! Countries have been read
|
||||
void ps_CountriesRead(BlackMisc::Network::CDbFlags::Entity entity, BlackMisc::Network::CDbFlags::ReadState readState, int count);
|
||||
|
||||
//! Data have been changed
|
||||
void ps_dataChanged();
|
||||
|
||||
//! Data have been changed
|
||||
void ps_completerActivated(const QString &countryName);
|
||||
|
||||
private:
|
||||
QScopedPointer<Ui::CDbCountrySelectorComponent> ui;
|
||||
QScopedPointer<QCompleter> m_completerCountryNames;
|
||||
BlackMisc::CCountry m_currentCountry;
|
||||
};
|
||||
}
|
||||
}
|
||||
#endif // guard
|
||||
87
src/blackgui/components/dbcountryselectorcomponent.ui
Normal file
87
src/blackgui/components/dbcountryselectorcomponent.ui
Normal file
@@ -0,0 +1,87 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<ui version="4.0">
|
||||
<class>CDbCountrySelectorComponent</class>
|
||||
<widget class="QFrame" name="CDbCountrySelectorComponent">
|
||||
<property name="geometry">
|
||||
<rect>
|
||||
<x>0</x>
|
||||
<y>0</y>
|
||||
<width>135</width>
|
||||
<height>22</height>
|
||||
</rect>
|
||||
</property>
|
||||
<property name="windowTitle">
|
||||
<string>Frame</string>
|
||||
</property>
|
||||
<property name="frameShape">
|
||||
<enum>QFrame::StyledPanel</enum>
|
||||
</property>
|
||||
<property name="frameShadow">
|
||||
<enum>QFrame::Raised</enum>
|
||||
</property>
|
||||
<layout class="QHBoxLayout" name="hl_Country">
|
||||
<property name="spacing">
|
||||
<number>4</number>
|
||||
</property>
|
||||
<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_CountryIso">
|
||||
<property name="minimumSize">
|
||||
<size>
|
||||
<width>50</width>
|
||||
<height>0</height>
|
||||
</size>
|
||||
</property>
|
||||
<property name="maximumSize">
|
||||
<size>
|
||||
<width>75</width>
|
||||
<height>16777215</height>
|
||||
</size>
|
||||
</property>
|
||||
<property name="placeholderText">
|
||||
<string>ISO, "AU", "US"</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QLineEdit" name="le_CountryName">
|
||||
<property name="placeholderText">
|
||||
<string>Name, "Russia", "France"</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QLabel" name="lbl_CountryIcon">
|
||||
<property name="minimumSize">
|
||||
<size>
|
||||
<width>16</width>
|
||||
<height>0</height>
|
||||
</size>
|
||||
</property>
|
||||
<property name="maximumSize">
|
||||
<size>
|
||||
<width>20</width>
|
||||
<height>16777215</height>
|
||||
</size>
|
||||
</property>
|
||||
<property name="text">
|
||||
<string/>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
</layout>
|
||||
</widget>
|
||||
<resources/>
|
||||
<connections/>
|
||||
</ui>
|
||||
Reference in New Issue
Block a user