From 16b49cfa46d6bafd0775ae4b0935d122d7fa4bd4 Mon Sep 17 00:00:00 2001 From: Klaus Basan Date: Fri, 2 Feb 2018 22:48:10 +0100 Subject: [PATCH] Ref T226, completer improvements * a location can home multiple airports * use a combined name for that reason in completers --- src/blackgui/components/airportcompleter.cpp | 2 +- src/blackmisc/aviation/airport.cpp | 50 ++++++++++---------- src/blackmisc/aviation/airport.h | 3 ++ src/blackmisc/aviation/airportlist.cpp | 19 +++++++- src/blackmisc/aviation/airportlist.h | 12 +++-- 5 files changed, 54 insertions(+), 32 deletions(-) diff --git a/src/blackgui/components/airportcompleter.cpp b/src/blackgui/components/airportcompleter.cpp index 4867189af..008c419c5 100644 --- a/src/blackgui/components/airportcompleter.cpp +++ b/src/blackgui/components/airportcompleter.cpp @@ -99,7 +99,7 @@ namespace BlackGui const CAirportList airports = sGui->getWebDataServices()->getAirports(); ui->le_Icao->setCompleter(new QCompleter(airports.allIcaoCodes(true), ui->le_Icao)); ui->le_Name->setCompleter(new QCompleter(airports.allDescriptivesNames(true), ui->le_Name)); - ui->le_Location->setCompleter(new QCompleter(airports.allLocations(true), ui->le_Location)); + ui->le_Location->setCompleter(new QCompleter(airports.allLocationsPlusOptionalDescription(true), ui->le_Location)); if (ui->le_Icao->completer()->popup()) { diff --git a/src/blackmisc/aviation/airport.cpp b/src/blackmisc/aviation/airport.cpp index 0b387546c..60aee4da3 100644 --- a/src/blackmisc/aviation/airport.cpp +++ b/src/blackmisc/aviation/airport.cpp @@ -16,6 +16,7 @@ #include #include #include +#include using namespace BlackMisc; using namespace BlackMisc::PhysicalQuantities; @@ -40,6 +41,14 @@ namespace BlackMisc m_descriptiveName(descriptiveName), m_icao(icao), m_position(position) { } + QString CAirport::getLocationPlusOptionalName() const + { + if (m_location.isEmpty()) { return this->getDescriptiveName(); } + if (m_descriptiveName.isEmpty()) { return this->getLocation(); } + if (this->getDescriptiveName() == this->getLocation()) { return this->getLocation(); } + return this->getLocation() % QStringLiteral(" (") % this->getDescriptiveName() % QStringLiteral(")"); + } + bool CAirport::matchesLocation(const QString &location) const { if (location.isEmpty()) { return false; } @@ -54,8 +63,8 @@ namespace BlackMisc void CAirport::updateMissingParts(const CAirport &airport) { - if (!this->m_country.hasIsoCode() && airport.getCountry().hasIsoCode()) { this->m_country = airport.getCountry(); } - if (this->m_descriptiveName.isEmpty()) { this->m_descriptiveName = airport.getDescriptiveName(); } + if (!m_country.hasIsoCode() && airport.getCountry().hasIsoCode()) { m_country = airport.getCountry(); } + if (m_descriptiveName.isEmpty()) { m_descriptiveName = airport.getDescriptiveName(); } } bool CAirport::isNull() const @@ -66,10 +75,10 @@ namespace BlackMisc QString CAirport::convertToQString(bool i18n) const { QString s = i18n ? QCoreApplication::translate("Aviation", "Airport") : "Airport"; - if (!this->m_icao.isEmpty()) { s.append(' ').append(this->m_icao.toQString(i18n)); } + if (!m_icao.isEmpty()) { s.append(' ').append(m_icao.toQString(i18n)); } // position - s.append(' ').append(this->m_position.toQString(i18n)); + s.append(' ').append(m_position.toQString(i18n)); return s; // force strings for translation in resource files @@ -102,18 +111,12 @@ namespace BlackMisc const ColumnIndex i = index.frontCasted(); switch (i) { - case IndexIcao: - return this->m_icao.propertyByIndex(index.copyFrontRemoved()); - case IndexLocation: - return CVariant(this->m_location); - case IndexDescriptiveName: - return CVariant(this->m_descriptiveName); - case IndexPosition: - return this->m_position.propertyByIndex(index.copyFrontRemoved()); - case IndexElevation: - return this->getElevation().propertyByIndex(index.copyFrontRemoved()); - case IndexOperating: - return CVariant::from(this->isOperating()); + case IndexIcao: return m_icao.propertyByIndex(index.copyFrontRemoved()); + case IndexLocation: return CVariant(m_location); + case IndexDescriptiveName: return CVariant(m_descriptiveName); + case IndexPosition: return m_position.propertyByIndex(index.copyFrontRemoved()); + case IndexElevation: return this->getElevation().propertyByIndex(index.copyFrontRemoved()); + case IndexOperating: return CVariant::from(this->isOperating()); default: return (ICoordinateWithRelativePosition::canHandleIndex(index)) ? ICoordinateWithRelativePosition::propertyByIndex(index) : @@ -128,7 +131,7 @@ namespace BlackMisc switch (i) { case IndexIcao: - this->m_icao.setPropertyByIndex(index.copyFrontRemoved(), variant); + m_icao.setPropertyByIndex(index.copyFrontRemoved(), variant); break; case IndexLocation: this->setLocation(variant.toQString()); @@ -137,7 +140,7 @@ namespace BlackMisc this->setDescriptiveName(variant.toQString()); break; case IndexPosition: - this->m_position.setPropertyByIndex(index.copyFrontRemoved(), variant); + m_position.setPropertyByIndex(index.copyFrontRemoved(), variant); break; case IndexOperating: this->setOperating(variant.toBool()); @@ -157,16 +160,13 @@ namespace BlackMisc int CAirport::comparePropertyByIndex(const CPropertyIndex &index, const CAirport &compareValue) const { - if (index.isMyself()) { return this->m_icao.comparePropertyByIndex(index.copyFrontRemoved(), compareValue.getIcao()); } + if (index.isMyself()) { return m_icao.comparePropertyByIndex(index.copyFrontRemoved(), compareValue.getIcao()); } const ColumnIndex i = index.frontCasted(); switch (i) { - case IndexIcao: - return this->m_icao.comparePropertyByIndex(index.copyFrontRemoved(), compareValue.getIcao()); - case IndexLocation: - return this->m_location.compare(compareValue.getLocation(), Qt::CaseInsensitive); - case IndexDescriptiveName: - return this->m_descriptiveName.compare(compareValue.getDescriptiveName(), Qt::CaseInsensitive); + case IndexIcao: return m_icao.comparePropertyByIndex(index.copyFrontRemoved(), compareValue.getIcao()); + case IndexLocation: return m_location.compare(compareValue.getLocation(), Qt::CaseInsensitive); + case IndexDescriptiveName: return m_descriptiveName.compare(compareValue.getDescriptiveName(), Qt::CaseInsensitive); default: if (ICoordinateWithRelativePosition::canHandleIndex(index)) { diff --git a/src/blackmisc/aviation/airport.h b/src/blackmisc/aviation/airport.h index 74fdf44b0..a0828b693 100644 --- a/src/blackmisc/aviation/airport.h +++ b/src/blackmisc/aviation/airport.h @@ -77,6 +77,9 @@ namespace BlackMisc //! Get location (e.g. "London") const QString &getLocation() const { return m_location; } + //! Location plus optional name (if available and different from location) + QString getLocationPlusOptionalName() const; + //! Set location void setLocation(const QString &location) { this->m_location = location; } diff --git a/src/blackmisc/aviation/airportlist.cpp b/src/blackmisc/aviation/airportlist.cpp index a235a3626..8297a0db6 100644 --- a/src/blackmisc/aviation/airportlist.cpp +++ b/src/blackmisc/aviation/airportlist.cpp @@ -104,11 +104,26 @@ namespace BlackMisc QStringList CAirportList::allLocations(bool sorted) const { - QStringList locations; + QSet locations; for (const CAirport &airport : *this) { if (airport.getLocation().isEmpty()) { continue; } - locations.push_back(airport.getLocation()); + locations.insert(airport.getLocation()); + } + + QStringList locs = locations.toList(); + if (sorted) { locs.sort(); } + return locs; + } + + QStringList CAirportList::allLocationsPlusOptionalDescription(bool sorted) const + { + QStringList locations; + for (const CAirport &airport : *this) + { + const QString l = airport.getLocationPlusOptionalName(); + if (l.isEmpty()) { continue; } + locations.push_back(l); } if (sorted) { locations.sort(); } return locations; diff --git a/src/blackmisc/aviation/airportlist.h b/src/blackmisc/aviation/airportlist.h index a89d2344f..9e91de694 100644 --- a/src/blackmisc/aviation/airportlist.h +++ b/src/blackmisc/aviation/airportlist.h @@ -30,9 +30,9 @@ namespace BlackMisc //! Value object for a list of airports. class BLACKMISC_EXPORT CAirportList : public CSequence, - public BlackMisc::Db::IDatastoreObjectList, - public BlackMisc::Geo::IGeoObjectWithRelativePositionList, - public BlackMisc::Mixin::MetaType + public Db::IDatastoreObjectList, + public Geo::IGeoObjectWithRelativePositionList, + public Mixin::MetaType { public: BLACKMISC_DECLARE_USING_MIXIN_METATYPE(CAirportList) @@ -67,9 +67,13 @@ namespace BlackMisc //! All names QStringList allDescriptivesNames(bool sorted) const; - //! All names + //! All locations + //! \remark less locations than airports, since a location (e.g. New Yorrk) homes multiple airports QStringList allLocations(bool sorted) const; + //! All locations plus optional description + QStringList allLocationsPlusOptionalDescription(bool sorted) const; + //! From our DB JSON static CAirportList fromDatabaseJson(const QJsonArray &array, CAirportList *inconsistent = nullptr); };