diff --git a/src/blackgui/components/dbairlineicaoselectorcomponent.cpp b/src/blackgui/components/dbairlineicaoselectorcomponent.cpp new file mode 100644 index 000000000..e84b904c3 --- /dev/null +++ b/src/blackgui/components/dbairlineicaoselectorcomponent.cpp @@ -0,0 +1,198 @@ +/* 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 "dbairlineicaoselectorcomponent.h" +#include "ui_dbairlineicaoselectorcomponent.h" +#include "blackgui/guiutility.h" +#include "blackmisc/datastoreutility.h" +#include + +using namespace BlackGui; +using namespace BlackMisc; +using namespace BlackMisc::Aviation; +using namespace BlackMisc::Network; + +namespace BlackGui +{ + namespace Components + { + CDbAirlineIcaoSelectorComponent::CDbAirlineIcaoSelectorComponent(QWidget *parent) : + QFrame(parent), + ui(new Ui::CDbAirlineIcaoSelectorComponent) + { + ui->setupUi(this); + this->setAcceptDrops(true); + this->setAcceptedMetaTypeIds({qMetaTypeId(), qMetaTypeId()}); + + connect(ui->le_Airline, &QLineEdit::returnPressed, this, &CDbAirlineIcaoSelectorComponent::ps_dataChanged); + } + + CDbAirlineIcaoSelectorComponent::~CDbAirlineIcaoSelectorComponent() + { + gracefulShutdown(); + } + + void CDbAirlineIcaoSelectorComponent::setProvider(Network::IWebDataServicesProvider *webDataReaderProvider) + { + if (!webDataReaderProvider) { return; } + CWebDataServicesAware::setProvider(webDataReaderProvider); + connectSwiftDatabaseSignals( + this, + std::bind(&CDbAirlineIcaoSelectorComponent::ps_codesRead, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3) + ); + int c = this->getAirlineIcaoCodesCount(); + if (c > 0) + { + this->ps_codesRead(CDbFlags::AirlineIcaoEntity, CDbFlags::ReadFinished, c); + } + } + + void CDbAirlineIcaoSelectorComponent::setAirlineIcao(const CAirlineIcaoCode &icao) + { + QString icaoStr(icao.getVDesignator()); + this->ui->le_Airline->setText(icaoStr); + ui->lbl_Description->setText(icao.getName()); + if (icao != m_currentIcao) + { + m_currentIcao = icao; + emit changedAirlineIcao(icao); + } + } + + void CDbAirlineIcaoSelectorComponent::setAirlineIcao(int key) + { + CAirlineIcaoCode icao(getAirlineIcaoCodeForDbKey(key)); + ui->lbl_Description->setText(""); + if (icao.hasCompleteData()) + { + this->setAirlineIcao(icao); + } + } + + CAirlineIcaoCode CDbAirlineIcaoSelectorComponent::getAirlineIcao() const + { + int key = CDatastoreUtility::extractIntegerKey(this->ui->le_Airline->text()); + if (key < 0) { return CAirlineIcaoCode(); } + CAirlineIcaoCode icao(getAirlineIcaoCodeForDbKey(key)); + return icao; + } + + void CDbAirlineIcaoSelectorComponent::setReadOnly(bool readOnly) + { + this->ui->le_Airline->setReadOnly(readOnly); + } + + void CDbAirlineIcaoSelectorComponent::withIcaoDescription(bool description) + { + this->ui->lbl_Description->setVisible(description); + } + + bool CDbAirlineIcaoSelectorComponent::isSet() const + { + return this->getAirlineIcao().hasCompleteData(); + } + + void CDbAirlineIcaoSelectorComponent::clear() + { + this->ui->le_Airline->clear(); + } + + void CDbAirlineIcaoSelectorComponent::dragEnterEvent(QDragEnterEvent *event) + { + if (!event || !acceptDrop(event->mimeData())) { return; } + setBackgroundRole(QPalette::Highlight); + event->acceptProposedAction(); + } + + void CDbAirlineIcaoSelectorComponent::dragMoveEvent(QDragMoveEvent *event) + { + if (!event || !acceptDrop(event->mimeData())) { return; } + event->acceptProposedAction(); + } + + void CDbAirlineIcaoSelectorComponent::dragLeaveEvent(QDragLeaveEvent *event) + { + if (!event) { return; } + event->accept(); + } + + void CDbAirlineIcaoSelectorComponent::dropEvent(QDropEvent *event) + { + if (!event || !acceptDrop(event->mimeData())) { return; } + CVariant valueVariant(toCVariant(event->mimeData())); + if (valueVariant.isValid()) + { + if (valueVariant.canConvert()) + { + CAirlineIcaoCode icao(valueVariant.value()); + if (!icao.hasValidDbKey()) { return; } + this->setAirlineIcao(icao); + } + else if (valueVariant.canConvert()) + { + CAirlineIcaoCodeList icaos(valueVariant.value()); + if (icaos.isEmpty()) { return; } + this->setAirlineIcao(icaos.front()); + } + } + } + + void CDbAirlineIcaoSelectorComponent::ps_codesRead(CDbFlags::Entity entity, CDbFlags::ReadState readState, int count) + { + if (!hasProvider()) { return; } + if (entity.testFlag(CDbFlags::AirlineIcaoEntity) && readState == CDbFlags::ReadFinished) + { + if (count > 0) + { + QCompleter *c = new QCompleter(this->getAirlineIcaoCodes().toCompleterStrings(), this); + c->setCaseSensitivity(Qt::CaseInsensitive); + c->setCompletionMode(QCompleter::PopupCompletion); + c->setMaxVisibleItems(10); + this->connect(c, static_cast(&QCompleter::activated), this, &CDbAirlineIcaoSelectorComponent::ps_completerActivated); + + this->ui->le_Airline->setCompleter(c); + m_completerIcaoDescription.reset(c); // deletes any old completer + this->setReadOnly(false); + } + else + { + this->m_completerIcaoDescription.reset(nullptr); + this->setReadOnly(true); + } + } + } + + void CDbAirlineIcaoSelectorComponent::ps_dataChanged() + { + if (!hasProvider()) { return; } + QString s(this->ui->le_Airline->text()); + if (s.isEmpty()) { return; } + int dbKey = CDatastoreUtility::extractIntegerKey(s); + if (dbKey >= 0) + { + CAirlineIcaoCode icao(getAirlineIcaoCodeForDbKey(dbKey)); + this->setAirlineIcao(icao); + } + else + { + // second choice, first object found by designator + CAirlineIcaoCode icao(getAirlineIcaoCodeForDesignator(s)); + this->setAirlineIcao(icao); + } + } + + void CDbAirlineIcaoSelectorComponent::ps_completerActivated(const QString &icaoString) + { + int dbKey = CDatastoreUtility::extractIntegerKey(icaoString); + if (dbKey < 0) { return; } + this->setAirlineIcao(dbKey); + } + + }// class +} // ns diff --git a/src/blackgui/components/dbairlineicaoselectorcomponent.h b/src/blackgui/components/dbairlineicaoselectorcomponent.h new file mode 100644 index 000000000..a7b94d645 --- /dev/null +++ b/src/blackgui/components/dbairlineicaoselectorcomponent.h @@ -0,0 +1,105 @@ +/* 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_DBAIRLINEICAOSELECTORCOMPONENT_H +#define BLACKGUI_DBAIRLINEICAOSELECTORCOMPONENT_H + +#include "blackgui/blackguiexport.h" +#include "blackcore/webdataservices.h" +#include "blackgui/dropbase.h" +#include "blackmisc/aviation/airlineicaocode.h" +#include +#include +#include + +namespace Ui { class CDbAirlineIcaoSelectorComponent; } + +namespace BlackGui +{ + namespace Components + { + /*! + * Distributor selector + */ + class BLACKGUI_EXPORT CDbAirlineIcaoSelectorComponent : + public QFrame, + public BlackMisc::Network::CWebDataServicesAware, + public BlackGui::CDropBase + { + Q_OBJECT + + public: + //! Constructor + explicit CDbAirlineIcaoSelectorComponent(QWidget *parent = nullptr); + + //! Destructor + ~CDbAirlineIcaoSelectorComponent(); + + //! \copydoc CWebDataReaderAware::setProvider + virtual void setProvider(BlackMisc::Network::IWebDataServicesProvider *webDataReaderProvider) override; + + //! Current airline ICAO + void setAirlineIcao(const BlackMisc::Aviation::CAirlineIcaoCode &icao); + + //! Current airline ICAO + void setAirlineIcao(int key); + + //! Distributor + BlackMisc::Aviation::CAirlineIcaoCode getAirlineIcao() const; + + //! Read only + void setReadOnly(bool readOnly); + + //! Display distributor description + void withIcaoDescription(bool description); + + //! Set with valid Distributor + bool isSet() const; + + //! Clear selection + void clear(); + + signals: + //! ICAO was changed + void changedAirlineIcao(const BlackMisc::Aviation::CAirlineIcaoCode &icao); + + 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: + //! Distributors have been read + void ps_codesRead(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 &icaoString); + + private: + QScopedPointer ui; + QScopedPointer m_completerIcaoDescription; + QList m_signals; + BlackMisc::Aviation::CAirlineIcaoCode m_currentIcao; + }; + } +} +#endif // guard diff --git a/src/blackgui/components/dbairlineicaoselectorcomponent.ui b/src/blackgui/components/dbairlineicaoselectorcomponent.ui new file mode 100644 index 000000000..537d78404 --- /dev/null +++ b/src/blackgui/components/dbairlineicaoselectorcomponent.ui @@ -0,0 +1,62 @@ + + + CDbAirlineIcaoSelectorComponent + + + + 0 + 0 + 189 + 22 + + + + Frame + + + QFrame::StyledPanel + + + QFrame::Raised + + + + 0 + + + 0 + + + 0 + + + 0 + + + 4 + + + + + ICAO + + + + + + + + 50 + 0 + + + + + + + + + + + +