From 7981815ddff22bf67b0f88ed637e45eaac99a0e1 Mon Sep 17 00:00:00 2001 From: Klaus Basan Date: Wed, 3 Jun 2020 04:46:32 +0200 Subject: [PATCH] Improvements on livery selector and form, also airline form * use id first to find data * use id in selector and fields * based on issue https://discordapp.com/channels/539048679160676382/717493722392297493/717511173146411061 --- .../components/dbliveryselectorcomponent.cpp | 79 +++++++++++-------- .../components/dbliveryselectorcomponent.h | 2 +- src/blackgui/components/dbstashcomponent.cpp | 8 +- src/blackgui/editors/airlineicaoform.cpp | 20 ++++- src/blackgui/editors/liveryform.cpp | 25 +++++- 5 files changed, 92 insertions(+), 42 deletions(-) diff --git a/src/blackgui/components/dbliveryselectorcomponent.cpp b/src/blackgui/components/dbliveryselectorcomponent.cpp index 5cea69aea..aebdc2b1a 100644 --- a/src/blackgui/components/dbliveryselectorcomponent.cpp +++ b/src/blackgui/components/dbliveryselectorcomponent.cpp @@ -12,6 +12,7 @@ #include "blackgui/guiapplication.h" #include "blackgui/uppercasevalidator.h" #include "blackmisc/aviation/liverylist.h" +#include "blackmisc/db/datastoreutility.h" #include "blackmisc/compare.h" #include "blackmisc/stringutils.h" #include "blackmisc/variant.h" @@ -34,6 +35,7 @@ using namespace BlackGui; using namespace BlackCore; using namespace BlackMisc; using namespace BlackMisc::Aviation; +using namespace BlackMisc::Db; using namespace BlackMisc::Network; namespace BlackGui @@ -50,8 +52,8 @@ namespace BlackGui ui->le_Livery->setValidator(new CUpperCaseValidator(this)); - connect(ui->le_Livery, &QLineEdit::returnPressed, this, &CDbLiverySelectorComponent::onDataChanged); - connect(ui->le_Livery, &QLineEdit::returnPressed, this, &CDbLiverySelectorComponent::onDataChanged); + connect(ui->le_Livery, &QLineEdit::returnPressed, this, &CDbLiverySelectorComponent::onDataChanged); + connect(ui->le_Livery, &QLineEdit::editingFinished, this, &CDbLiverySelectorComponent::onDataChanged); connect(sGui->getWebDataServices(), &CWebDataServices::dataRead, this, &CDbLiverySelectorComponent::onLiveriesRead, Qt::QueuedConnection); this->onLiveriesRead(CEntityFlags::LiveryEntity, CEntityFlags::ReadFinished, sGui->getWebDataServices()->getLiveriesCount(), {}); @@ -64,33 +66,41 @@ namespace BlackGui void CDbLiverySelectorComponent::setLivery(const CLivery &livery) { - QString code(livery.getCombinedCode()); - if (code.isEmpty()) + if (!livery.hasCombinedCode()) { ui->le_Livery->clear(); return; } + if (livery != m_currentLivery) { - ui->le_Livery->setText(code); + ui->le_Livery->setText(livery.getCombinedCodePlusId()); m_currentLivery = livery; emit changedLivery(livery); } } - void CDbLiverySelectorComponent::setlivery(const QString &code) + void CDbLiverySelectorComponent::setLivery(const QString &code) { - QString liveryCode(code.toUpper().trimmed()); - int s = liveryCode.indexOf(' '); - if (s >= 1) { liveryCode = liveryCode.left(s); } - s = liveryCode.indexOf('('); - if (s >= 1) { liveryCode = liveryCode.left(s).trimmed(); } + if (!sGui || sGui->isShuttingDown() || !sGui->hasWebDataServices()) { return; } + const int dbKey = CDatastoreUtility::extractIntegerKey(code); + CLivery livery; - if (m_currentLivery.matchesCombinedCode(liveryCode)) { return; } - CLivery d(sApp->getWebDataServices()->getLiveries().findByCombinedCode(liveryCode)); - if (d.hasCompleteData()) + if (dbKey >= 0) { - this->setLivery(d); + livery = sGui->getWebDataServices()->getLiveryForDbKey(dbKey); + } + + if (!livery.hasValidDbKey()) + { + const QString liveryCode = this->stripExtraInfo(code.toUpper().trimmed()); + if (m_currentLivery.matchesCombinedCode(liveryCode)) { return; } + livery = sGui->getWebDataServices()->getLiveries().findByCombinedCode(liveryCode); + } + + if (livery.hasCompleteData()) + { + this->setLivery(livery); } else { @@ -101,20 +111,29 @@ namespace BlackGui CLivery CDbLiverySelectorComponent::getLivery() const { - if (!sApp) { return CLivery(); } - const QString liveryCode( - this->stripExtraInfo(ui->le_Livery->text()) - ); - const CLivery liv(sApp->getWebDataServices()->getLiveries().findByCombinedCode(liveryCode)); - if (liv.hasCompleteData() && liv.hasValidDbKey()) + if (!sGui || sGui->isShuttingDown()) { return CLivery(); } + + const QString raw = ui->le_Livery->text(); + const int dbKey = CDatastoreUtility::extractIntegerKey(raw); + + CLivery livery; + if (dbKey >= 0) { - // full data fetched - return liv; + livery = sGui->getWebDataServices()->getLiveryForDbKey(dbKey); } else { - return m_currentLivery; + const QString liveryCode(this->stripExtraInfo(ui->le_Livery->text())); + livery = sGui->getWebDataServices()->getLiveries().findByCombinedCode(liveryCode); } + + if (livery.hasCompleteData() && livery.hasValidDbKey()) + { + // full data fetched + return livery; + } + + return m_currentLivery; } QString CDbLiverySelectorComponent::getRawCombinedCode() const @@ -192,7 +211,7 @@ namespace BlackGui { if (count > 0) { - const QStringList codes(sApp->getWebDataServices()->getLiveries().getCombinedCodesPlusInfo(true)); + const QStringList codes(sGui->getWebDataServices()->getLiveries().getCombinedCodesPlusInfoAndId(true)); QCompleter *c = new QCompleter(codes, this); c->setCaseSensitivity(Qt::CaseInsensitive); c->setCompletionMode(QCompleter::PopupCompletion); @@ -212,17 +231,13 @@ namespace BlackGui void CDbLiverySelectorComponent::onDataChanged() { if (!sGui || sGui->isShuttingDown() || !sGui->hasWebDataServices()) { return; } - const QString code( - this->stripExtraInfo(ui->le_Livery->text()) - ); - if (code.isEmpty()) { return; } - const CLivery livery(sApp->getWebDataServices()->getLiveries().findByCombinedCode(code)); - this->setLivery(livery); + const QString raw = ui->le_Livery->text(); + this->setLivery(raw); } void CDbLiverySelectorComponent::onCompleterActivated(const QString &liveryCode) { - this->setlivery(liveryCode); + this->setLivery(liveryCode); } QString CDbLiverySelectorComponent::stripExtraInfo(const QString &liveryCode) const diff --git a/src/blackgui/components/dbliveryselectorcomponent.h b/src/blackgui/components/dbliveryselectorcomponent.h index c1da54ceb..b4712cebe 100644 --- a/src/blackgui/components/dbliveryselectorcomponent.h +++ b/src/blackgui/components/dbliveryselectorcomponent.h @@ -55,7 +55,7 @@ namespace BlackGui void setLivery(const BlackMisc::Aviation::CLivery &livery); //! Current livery - void setlivery(const QString &code); + void setLivery(const QString &code); //! Livery BlackMisc::Aviation::CLivery getLivery() const; diff --git a/src/blackgui/components/dbstashcomponent.cpp b/src/blackgui/components/dbstashcomponent.cpp index 64743a23a..6b01e3198 100644 --- a/src/blackgui/components/dbstashcomponent.cpp +++ b/src/blackgui/components/dbstashcomponent.cpp @@ -65,10 +65,10 @@ namespace BlackGui // copy over buttons connect(ui->pb_AircraftIcao, &QPushButton::pressed, this, &CDbStashComponent::copyOverValuesToSelectedModels); - connect(ui->pb_AirlineIcao, &QPushButton::pressed, this, &CDbStashComponent::copyOverValuesToSelectedModels); - connect(ui->pb_Livery, &QPushButton::pressed, this, &CDbStashComponent::copyOverValuesToSelectedModels); - connect(ui->pb_Distributor, &QPushButton::pressed, this, &CDbStashComponent::copyOverValuesToSelectedModels); - connect(ui->pb_Model, &QPushButton::pressed, this, &CDbStashComponent::modifyModelDialog); + connect(ui->pb_AirlineIcao, &QPushButton::pressed, this, &CDbStashComponent::copyOverValuesToSelectedModels); + connect(ui->pb_Livery, &QPushButton::pressed, this, &CDbStashComponent::copyOverValuesToSelectedModels); + connect(ui->pb_Distributor, &QPushButton::pressed, this, &CDbStashComponent::copyOverValuesToSelectedModels); + connect(ui->pb_Model, &QPushButton::pressed, this, &CDbStashComponent::modifyModelDialog); ui->tvp_StashAircraftModels->setAircraftModelMode(CAircraftModelListModel::StashModel); ui->tvp_StashAircraftModels->allowDragDrop(false, true, true); diff --git a/src/blackgui/editors/airlineicaoform.cpp b/src/blackgui/editors/airlineicaoform.cpp index 9e5283ded..3d7f77c78 100644 --- a/src/blackgui/editors/airlineicaoform.cpp +++ b/src/blackgui/editors/airlineicaoform.cpp @@ -96,7 +96,20 @@ namespace BlackGui CAirlineIcaoCode CAirlineIcaoForm::getValue() const { - CAirlineIcaoCode code(m_currentCode); + CAirlineIcaoCode code; + const QString id = ui->le_Id->text(); + if (sGui && !sGui->isShuttingDown() && sGui->hasWebDataServices()) + { + bool ok; + const int dbKey = id.toInt(&ok); + if (ok) + { + code = sGui->getWebDataServices()->getAirlineIcaoCodeForDbKey(dbKey); + } + } + + if (code.hasValidDbKey()) { return code; } + code = m_currentCode; code.setVirtualAirline(ui->cb_Va->isChecked()); code.setMilitary(ui->cb_Military->isChecked()); code.setCountry(ui->country_Selector->getCountry()); @@ -210,9 +223,10 @@ namespace BlackGui { if (!sGui || sGui->isShuttingDown() || !sGui->hasWebDataServices()) { return; } - const int id = ui->le_Id->text().toInt(); + bool ok; + const int id = ui->le_Id->text().toInt(&ok); const CAirlineIcaoCode icao = sGui->getWebDataServices()->getAirlineIcaoCodeForDbKey(id); - if (!icao.isLoadedFromDb()) + if (ok && !icao.isLoadedFromDb()) { ui->le_Id->undo(); return; diff --git a/src/blackgui/editors/liveryform.cpp b/src/blackgui/editors/liveryform.cpp index 960395966..13f2ac338 100644 --- a/src/blackgui/editors/liveryform.cpp +++ b/src/blackgui/editors/liveryform.cpp @@ -69,7 +69,24 @@ namespace BlackGui CLivery CLiveryForm::getValue() const { - CLivery livery(ui->comp_LiverySelector->getLivery()); + CLivery livery; + const QString id = ui->le_Id->text(); + if (!id.isEmpty() && sGui && !sGui->isShuttingDown() && sGui->hasWebDataServices()) + { + bool ok; + const int dbKey = id.toInt(&ok); + if (ok) + { + livery = sGui->getWebDataServices()->getLiveryForDbKey(dbKey); + } + } + + // fallback + if (!livery.hasValidDbKey()) + { + livery = ui->comp_LiverySelector->getLivery(); + } + if (livery.hasCompleteData() && livery.hasValidDbKey()) { // already complete data from selector @@ -229,7 +246,11 @@ namespace BlackGui { if (!sGui || sGui->isShuttingDown() || !sGui->getWebDataServices()) { return; } if (!code.hasCompleteData()) { return; } - if (!code.hasValidDbKey()) { return; } + if (!code.hasValidDbKey()) { return; } + + // only replace with STD livery if airline does not match + const CLivery currentLivery = this->getValue(); + if (currentLivery.getAirlineIcaoCode() == code) { return; } const CLivery stdLivery(sGui->getWebDataServices()->getLiveries().findStdLiveryByAirlineIcaoVDesignator(code)); if (stdLivery.hasValidDbKey())