From bb4f0e7a132cb2d78fbf624d28e3f577b40b6eb2 Mon Sep 17 00:00:00 2001 From: Klaus Basan Date: Tue, 8 Aug 2017 23:52:48 +0200 Subject: [PATCH] Ref T111, search for location/name --- src/blackmisc/aviation/airport.cpp | 28 +++++++++++++++++++++++--- src/blackmisc/aviation/airport.h | 17 +++++++++++++++- src/blackmisc/aviation/airportlist.cpp | 17 ++++++++++++++++ src/blackmisc/aviation/airportlist.h | 3 +++ 4 files changed, 61 insertions(+), 4 deletions(-) diff --git a/src/blackmisc/aviation/airport.cpp b/src/blackmisc/aviation/airport.cpp index 4415f6d90..0f55152ba 100644 --- a/src/blackmisc/aviation/airport.cpp +++ b/src/blackmisc/aviation/airport.cpp @@ -11,11 +11,13 @@ #include "blackmisc/pq/angle.h" #include "blackmisc/propertyindex.h" #include "blackmisc/variant.h" +#include "blackmisc/stringutils.h" #include #include #include +using namespace BlackMisc; using namespace BlackMisc::PhysicalQuantities; using namespace BlackMisc::Geo; @@ -38,6 +40,18 @@ namespace BlackMisc m_descriptiveName(descriptiveName), m_icao(icao), m_position(position) { } + bool CAirport::matchesLocation(const QString &location) const + { + if (location.isEmpty()) { return false; } + return caseInsensitiveStringCompare(location, this->getLocation()); + } + + bool CAirport::matchesDescriptiveName(const QString &name) const + { + if (name.isEmpty()) { return false; } + return caseInsensitiveStringCompare(name, this->getDescriptiveName()); + } + void CAirport::updateMissingParts(const CAirport &airport) { if (!this->m_country.hasIsoCode() && airport.getCountry().hasIsoCode()) { this->m_country = airport.getCountry(); } @@ -61,6 +75,7 @@ namespace BlackMisc { CAirport airport(json.value(prefix + "icao").toString()); airport.setDescriptiveName(json.value(prefix + "name").toString()); + airport.setLocation(json.value(prefix + "location").toString()); const CCoordinateGeodetic pos( json.value(prefix + "latitude").toDouble(), json.value(prefix + "longitude").toDouble(), @@ -79,11 +94,13 @@ namespace BlackMisc CVariant CAirport::propertyByIndex(const BlackMisc::CPropertyIndex &index) const { if (index.isMyself()) { return CVariant::from(*this); } - ColumnIndex i = index.frontCasted(); + 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: @@ -102,12 +119,15 @@ namespace BlackMisc void CAirport::setPropertyByIndex(const BlackMisc::CPropertyIndex &index, const CVariant &variant) { if (index.isMyself()) { (*this) = variant.to(); return; } - ColumnIndex i = index.frontCasted(); + const ColumnIndex i = index.frontCasted(); switch (i) { case IndexIcao: this->m_icao.setPropertyByIndex(index.copyFrontRemoved(), variant); break; + case IndexLocation: + this->setLocation(variant.toQString()); + break; case IndexDescriptiveName: this->setDescriptiveName(variant.toQString()); break; @@ -133,11 +153,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()); } - ColumnIndex i = index.frontCasted(); + 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); default: diff --git a/src/blackmisc/aviation/airport.h b/src/blackmisc/aviation/airport.h index aebfc5c9b..2e1c18c93 100644 --- a/src/blackmisc/aviation/airport.h +++ b/src/blackmisc/aviation/airport.h @@ -45,6 +45,7 @@ namespace BlackMisc enum ColumnIndex { IndexIcao = BlackMisc::CPropertyIndex::GlobalIndexCAirport, + IndexLocation, IndexDescriptiveName, IndexPosition, IndexCountry, @@ -73,12 +74,24 @@ namespace BlackMisc //! Set ICAO code. void setIcao(const CAirportIcaoCode &icao) { m_icao = icao; } + //! Get location (e.g. "London") + const QString &getLocation() const { return m_location; } + + //! Set location + void setLocation(const QString &location) { this->m_location = location; } + + //! Matches location? + bool matchesLocation(const QString &location) const; + //! Get descriptive name - QString getDescriptiveName() const { return m_descriptiveName; } + const QString &getDescriptiveName() const { return m_descriptiveName; } //! Set descriptive name void setDescriptiveName(const QString &name) { this->m_descriptiveName = name; } + //! Matches name? + bool matchesDescriptiveName(const QString &name) const; + //! Get the position const BlackMisc::Geo::CCoordinateGeodetic &getPosition() const { return m_position; } @@ -148,6 +161,7 @@ namespace BlackMisc static CAirport fromDatabaseJson(const QJsonObject &json, const QString &prefix = QString()); private: + QString m_location; QString m_descriptiveName; bool m_operating = true; CAirportIcaoCode m_icao; @@ -157,6 +171,7 @@ namespace BlackMisc BLACK_METACLASS( CAirport, BLACK_METAMEMBER(icao), + BLACK_METAMEMBER(location), BLACK_METAMEMBER(descriptiveName), BLACK_METAMEMBER(position), BLACK_METAMEMBER(country), diff --git a/src/blackmisc/aviation/airportlist.cpp b/src/blackmisc/aviation/airportlist.cpp index 37e5f29f4..026ae5844 100644 --- a/src/blackmisc/aviation/airportlist.cpp +++ b/src/blackmisc/aviation/airportlist.cpp @@ -62,6 +62,23 @@ namespace BlackMisc return this->findFirstByOrDefault(&CAirport::getIcao, icao, ifNotFound); } + CAirport CAirportList::findFirstByNameOrLocation(const QString &nameOrLocation, const CAirport &ifNotFound) const + { + if (this->isEmpty() || nameOrLocation.isEmpty()) { return ifNotFound; } + CAirportList airports = this->findBy([&](const CAirport & airport) + { + return airport.matchesDescriptiveName(nameOrLocation); + }); + if (!airports.isEmpty()) { return airports.frontOrDefault(); } + + airports = this->findBy([&](const CAirport & airport) + { + return airport.matchesLocation(nameOrLocation); + }); + if (!airports.isEmpty()) { return airports.frontOrDefault(); } + return ifNotFound; + } + QStringList CAirportList::allIcaoCodes(bool sorted) const { QStringList icaos; diff --git a/src/blackmisc/aviation/airportlist.h b/src/blackmisc/aviation/airportlist.h index 95d966a68..117e1b7fd 100644 --- a/src/blackmisc/aviation/airportlist.h +++ b/src/blackmisc/aviation/airportlist.h @@ -49,6 +49,9 @@ namespace BlackMisc //! Find first station by callsign, if not return given value / default CAirport findFirstByIcao(const CAirportIcaoCode &icao, const CAirport &ifNotFound = CAirport()) const; + //! Find first by name or location, if not return given value / default + CAirport findFirstByNameOrLocation(const QString &nameOrLocation, const CAirport &ifNotFound = CAirport()) const; + //! Containing an airport with given ICAO code? bool containsAirportWithIcaoCode(const CAirportIcaoCode &icao) const;