From f29929111bf5c1d7a92e82207ee2f0bbed7c1665 Mon Sep 17 00:00:00 2001 From: Klaus Basan Date: Thu, 24 Sep 2015 00:08:50 +0200 Subject: [PATCH] refs #452 country selector component UI widget --- .../components/dbcountryselectorcomponent.cpp | 204 ++++++++++++++++++ .../components/dbcountryselectorcomponent.h | 101 +++++++++ .../components/dbcountryselectorcomponent.ui | 87 ++++++++ 3 files changed, 392 insertions(+) create mode 100644 src/blackgui/components/dbcountryselectorcomponent.cpp create mode 100644 src/blackgui/components/dbcountryselectorcomponent.h create mode 100644 src/blackgui/components/dbcountryselectorcomponent.ui diff --git a/src/blackgui/components/dbcountryselectorcomponent.cpp b/src/blackgui/components/dbcountryselectorcomponent.cpp new file mode 100644 index 000000000..5265748b1 --- /dev/null +++ b/src/blackgui/components/dbcountryselectorcomponent.cpp @@ -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 + +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(), qMetaTypeId()}); + + 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 country(valueVariant.value()); + if (!country.hasIsoCode()) { return; } + this->setCountry(country); + } + else if (valueVariant.canConvert()) + { + CCountryList countries(valueVariant.value()); + 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(&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 diff --git a/src/blackgui/components/dbcountryselectorcomponent.h b/src/blackgui/components/dbcountryselectorcomponent.h new file mode 100644 index 000000000..ee30f0179 --- /dev/null +++ b/src/blackgui/components/dbcountryselectorcomponent.h @@ -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 +#include +#include + +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; + QScopedPointer m_completerCountryNames; + BlackMisc::CCountry m_currentCountry; + }; + } +} +#endif // guard diff --git a/src/blackgui/components/dbcountryselectorcomponent.ui b/src/blackgui/components/dbcountryselectorcomponent.ui new file mode 100644 index 000000000..1e9ffb43c --- /dev/null +++ b/src/blackgui/components/dbcountryselectorcomponent.ui @@ -0,0 +1,87 @@ + + + CDbCountrySelectorComponent + + + + 0 + 0 + 135 + 22 + + + + Frame + + + QFrame::StyledPanel + + + QFrame::Raised + + + + 4 + + + 0 + + + 0 + + + 0 + + + 0 + + + + + + 50 + 0 + + + + + 75 + 16777215 + + + + ISO, "AU", "US" + + + + + + + Name, "Russia", "France" + + + + + + + + 16 + 0 + + + + + 20 + 16777215 + + + + + + + + + + + +