From 2c84c51a93fb991044d11eb04155c0b48856b79b Mon Sep 17 00:00:00 2001 From: Klaus Basan Date: Sun, 13 Dec 2015 19:59:48 +0100 Subject: [PATCH] refs #535, stash componet * allow to assign livery/ICAO to selected objects * allow access to corresponding mapping component --- .../components/dbmappingcomponent.cpp | 59 +++++---- src/blackgui/components/dbmappingcomponent.h | 10 +- src/blackgui/components/dbstashcomponent.cpp | 121 +++++++++++++++++- src/blackgui/components/dbstashcomponent.h | 37 ++++++ src/blackgui/components/dbstashcomponent.ui | 52 +++++++- 5 files changed, 248 insertions(+), 31 deletions(-) diff --git a/src/blackgui/components/dbmappingcomponent.cpp b/src/blackgui/components/dbmappingcomponent.cpp index 83554d509..1a9c077a7 100644 --- a/src/blackgui/components/dbmappingcomponent.cpp +++ b/src/blackgui/components/dbmappingcomponent.cpp @@ -13,7 +13,7 @@ #include "blackmisc/simulation/fscommon/aircraftcfgparser.h" #include "blackmisc/logmessage.h" #include "blackmisc/project.h" - +#include using namespace BlackCore; using namespace BlackMisc; @@ -34,6 +34,7 @@ namespace BlackGui ui(new Ui::CDbMappingComponent) { ui->setupUi(this); + this->ui->comp_StashAircraft->setMappingComponent(this); this->ui->tvp_AircraftModelsForVPilot->setAircraftModelMode(CAircraftModelListModel::VPilotRuleModel); this->ui->tvp_OwnAircraftModels->setAircraftModelMode(CAircraftModelListModel::OwnSimulatorModelMapping); @@ -41,6 +42,7 @@ namespace BlackGui connect(ui->tvp_OwnAircraftModels, &CAircraftModelView::doubleClicked, this, &CDbMappingComponent::ps_onModelRowSelected); connect(ui->tvp_OwnAircraftModels, &CAircraftModelView::rowCountChanged, this, &CDbMappingComponent::ps_onOwnModelsCountChanged); connect(ui->comp_StashAircraft->getView(), &CAircraftModelView::rowCountChanged, this, &CDbMappingComponent::ps_onStashCountChanged); + connect(ui->comp_StashAircraft, &CDbStashComponent::stashedModelChanged, this, &CDbMappingComponent::ps_onStashedModelsChanged); ui->tvp_OwnAircraftModels->setDisplayAutomatically(true); ui->tvp_OwnAircraftModels->setCustomMenu(new CMappingSimulatorModelMenu(this)); @@ -78,7 +80,10 @@ namespace BlackGui connect(&m_vPilotReader, &CVPilotRulesReader::readFinished, this, &CDbMappingComponent::ps_onLoadVPilotDataFinished); this->ui->tvp_AircraftModelsForVPilot->setCustomMenu(new CMappingVPilotMenu(this, true)); this->ui->tvp_AircraftModelsForVPilot->setDisplayAutomatically(true); - this->ui->tvp_AircraftModelsForVPilot->updateContainerMaybeAsync(m_cachedVPilotModels.get()); + const CAircraftModelList cachedModels(m_cachedVPilotModels.get()); + this->ui->tvp_AircraftModelsForVPilot->updateContainerMaybeAsync(cachedModels); + int noModels = cachedModels.size(); + CLogMessage(this).info("%1 cached vPilot models loaded") << noModels; } this->m_vPilot1stInit = false; this->ui->tab_VPilot->setEnabled(withVPilotRights); @@ -123,6 +128,7 @@ namespace BlackGui this->ui->editor_Livery->setProvider(provider); this->ui->editor_Distributor->setProvider(provider); this->ui->editor_AircraftIcao->setProvider(provider); + this->ui->comp_StashAircraft->setProvider(provider); } void CDbMappingComponent::gracefulShutdown() @@ -256,25 +262,15 @@ namespace BlackGui { this->ui->tvp_AircraftModelsForVPilot->updateContainerMaybeAsync(models); } - QTime t; - t.start(); - m_cachedVPilotModels.set(models); - qint64 e = t.elapsed(); - qDebug() << e; - CLogMessage(this).info("Written %1 vPilot rules to cache") << models.size(); - - /** - t.start(); - QString x = models.toJsonString(); - e = t.elapsed(); - - t.start(); - CAircraftModelList ml; - ml.convertFromJson(x); - e = t.elapsed(); - - t.start(); - **/ + CStatusMessage msg = m_cachedVPilotModels.set(models); + if (msg.isWarningOrAbove()) + { + CLogMessage(this).preformatted(msg); + } + else + { + CLogMessage(this).info("Written %1 vPilot rules to cache") << models.size(); + } } else { @@ -283,6 +279,23 @@ namespace BlackGui this->ui->tvp_OwnAircraftModels->hideLoadIndicator(); } + void CDbMappingComponent::ps_onStashedModelsChanged() + { + bool hlvp = this->ui->tvp_AircraftModelsForVPilot->derivedModel()->highlightGivenModelStrings(); + bool hlom = this->ui->tvp_OwnAircraftModels->derivedModel()->highlightGivenModelStrings(); + bool highlight = hlom || hlvp; + if (!highlight) { return; } + const QStringList stashedModels(this->ui->comp_StashAircraft->getStashedModelStrings()); + if (hlvp) + { + this->ui->tvp_AircraftModelsForVPilot->derivedModel()->setHighlightModelsStrings(stashedModels); + } + if (hlom) + { + this->ui->tvp_OwnAircraftModels->derivedModel()->setHighlightModelsStrings(stashedModels); + } + } + void CDbMappingComponent::ps_onVPilotCountChanged(int count, bool withFilter) { if (!m_withVPilot) { return; } @@ -496,7 +509,9 @@ namespace BlackGui Q_ASSERT_X(mapComp, Q_FUNC_INFO, "Cannot access mapping component"); if (mapComp->hasModelsForStash()) { - menu.addAction(CIcons::appMappings16(), "Stash", mapComp, SLOT(stashSelectedModels())); + QAction *a = menu.addAction(CIcons::appMappings16(), "Stash", mapComp, SLOT(stashSelectedModels()), Qt::ALT + Qt::Key_S); + a->setShortcut(Qt::ALT + Qt::Key_S); + } this->nestedCustomMenu(menu); } diff --git a/src/blackgui/components/dbmappingcomponent.h b/src/blackgui/components/dbmappingcomponent.h index b05b673c5..d86dd24f3 100644 --- a/src/blackgui/components/dbmappingcomponent.h +++ b/src/blackgui/components/dbmappingcomponent.h @@ -78,6 +78,10 @@ namespace BlackGui //! Current tab index TabIndex currentTabIndex() const; + //! Unvalidated consolidated aircraft model from the subparts (icao, distributor) + //! \note not guaranteed to be valid, just snapshot of as it state + BlackMisc::Simulation::CAircraftModel getAircraftModel() const; + signals: //! Request to filter by livery void filterByLivery(const BlackMisc::Aviation::CLivery &livery); @@ -111,6 +115,9 @@ namespace BlackGui //! Data for vPilot have been loaded void ps_onLoadVPilotDataFinished(bool success); + //! Stashed models changed + void ps_onStashedModelsChanged(); + //! Row count for vPilot data changed void ps_onVPilotCountChanged(int count, bool withFilter); @@ -145,9 +152,6 @@ namespace BlackGui bool m_vPilot1stInit = true; bool m_withVPilot = false; - //! Consolidated aircraft model - BlackMisc::Simulation::CAircraftModel getAircraftModel() const; - //! Init vPilot if rights and suitable void initVPilotLoading(); diff --git a/src/blackgui/components/dbstashcomponent.cpp b/src/blackgui/components/dbstashcomponent.cpp index 103c83476..7401068b4 100644 --- a/src/blackgui/components/dbstashcomponent.cpp +++ b/src/blackgui/components/dbstashcomponent.cpp @@ -9,12 +9,17 @@ #include "dbstashcomponent.h" #include "ui_dbstashcomponent.h" +#include "dbmappingcomponent.h" +#include "blackgui/views/aircraftmodelview.h" #include "blackmisc/icons.h" +#include "blackmisc/verify.h" using namespace BlackMisc; using namespace BlackMisc::Simulation; +using namespace BlackMisc::Aviation; using namespace BlackMisc::Network; using namespace BlackGui::Models; +using namespace BlackGui::Views; namespace BlackGui { @@ -29,8 +34,23 @@ namespace BlackGui connect(this->ui->pb_Unstash, &QPushButton::pressed, this, &CDbStashComponent::ps_onUnstashPressed); connect(this->ui->pb_Validate, &QPushButton::pressed, this, &CDbStashComponent::ps_onValidatePressed); + connect(this->ui->tvp_StashAircraftModels, &CAircraftModelView::modelChanged, this, &CDbStashComponent::stashedModelChanged); + + // copy over buttons + connect(this->ui->pb_AircraftIcao, &QPushButton::pressed, this, &CDbStashComponent::ps_copyOverValues); + connect(this->ui->pb_AirlineIcao, &QPushButton::pressed, this, &CDbStashComponent::ps_copyOverValues); + connect(this->ui->pb_Livery, &QPushButton::pressed, this, &CDbStashComponent::ps_copyOverValues); + connect(this->ui->pb_Distributor, &QPushButton::pressed, this, &CDbStashComponent::ps_copyOverValues); ui->tvp_StashAircraftModels->setCustomMenu(new CStashModelsMenu(this, true)); + + + // set mapping component reference if it is parent + CDbMappingComponent *mapping = qobject_cast(parent); + if (mapping) + { + m_mappingComponent = mapping; + } } CDbStashComponent::~CDbStashComponent() @@ -84,6 +104,61 @@ namespace BlackGui return ui->tvp_StashAircraftModels; } + bool CDbStashComponent::hasStashedModels() const + { + return !this->ui->tvp_StashAircraftModels->isEmpty(); + } + + QStringList CDbStashComponent::getStashedModelStrings() const + { + return this->ui->tvp_StashAircraftModels->derivedModel()->getModelStrings(false); + } + + void CDbStashComponent::applyToSelected(const CLivery &livery, bool acceptWarnings) + { + if (!this->ui->tvp_StashAircraftModels->hasSelection()) { return; } + CStatusMessageList msgs(livery.validate()); + if (this->showMessages(msgs, acceptWarnings)) { return; } + this->ui->tvp_StashAircraftModels->applyToSelected(livery); + } + + void CDbStashComponent::applyToSelected(const CAircraftIcaoCode &icao, bool acceptWarnings) + { + if (!this->ui->tvp_StashAircraftModels->hasSelection()) { return; } + CStatusMessageList msgs(icao.validate()); + if (this->showMessages(msgs, acceptWarnings)) { return; } + this->ui->tvp_StashAircraftModels->applyToSelected(icao); + } + + void CDbStashComponent::applyToSelected(const CAirlineIcaoCode &icao, bool acceptWarnings) + { + if (!icao.hasValidDesignator()) + { + static const CStatusMessage msg(CStatusMessage::SeverityError, "No valid designator"); + this->showMessage(msg); + return; + } + + // retrieve the std livery + const CLivery stdLivery(this->getStdLiveryForAirlineCode(icao)); + if (!stdLivery.hasValidDbKey()) + { + static const CStatusMessage msg(CStatusMessage::SeverityError, "No valid standard livery for " + icao.getDesignator()); + this->showMessage(msg); + return; + } + + applyToSelected(stdLivery, acceptWarnings); + } + + void CDbStashComponent::applyToSelected(const CDistributor &distributor, bool acceptWarnings) + { + if (!this->ui->tvp_StashAircraftModels->hasSelection()) { return; } + CStatusMessageList msgs(distributor.validate()); + if (this->showMessages(msgs, acceptWarnings)) { return; } + this->ui->tvp_StashAircraftModels->applyToSelected(distributor); + } + void CDbStashComponent::ps_onUnstashPressed() { this->ui->tvp_StashAircraftModels->removeSelectedRows(); @@ -94,11 +169,55 @@ namespace BlackGui if (this->ui->tvp_StashAircraftModels->isEmpty()) { return; } } + void CDbStashComponent::ps_copyOverValues() + { + QObject *sender = QObject::sender(); + BLACK_VERIFY_X(this->m_mappingComponent, Q_FUNC_INFO, "missing mapping component"); + if (!this->m_mappingComponent) { return; } + if (!this->ui->tvp_StashAircraftModels->hasSelection()) { return; } + + CAircraftModel model(this->m_mappingComponent->getAircraftModel()); + if (sender == this->ui->pb_AircraftIcao) + { + this->applyToSelected(model.getAircraftIcaoCode()); + } + else if (sender == this->ui->pb_AirlineIcao) + { + this->applyToSelected(model.getAirlineIcaoCode()); + } + else if (sender == this->ui->pb_Distributor) + { + this->applyToSelected(model.getDistributor()); + } + else if (sender == this->ui->pb_Livery) + { + this->applyToSelected(model.getLivery()); + } + } + + bool CDbStashComponent::showMessages(const CStatusMessageList &msgs, bool onlyErrors) + { + if (msgs.isEmpty()) { return false; } + if (!msgs.hasErrorMessages() && onlyErrors) { return false; } + BLACK_VERIFY_X(this->m_mappingComponent, Q_FUNC_INFO, "missing mapping component"); + if (!this->m_mappingComponent) { return false; } + this->m_mappingComponent->showMessages(msgs); + return true; + } + + bool CDbStashComponent::showMessage(const CStatusMessage &msg) + { + if (msg.isEmpty()) { return false; } + BLACK_VERIFY_X(this->m_mappingComponent, Q_FUNC_INFO, "missing mapping component"); + if (!this->m_mappingComponent) { return false; } + this->m_mappingComponent->showMessage(msg); + return true; + } + void CDbStashComponent::CStashModelsMenu::customMenu(QMenu &menu) const { menu.addAction(CIcons::database16(), "Unstash", this->parent(), SLOT(ps_onUnstashPressed())); nestedCustomMenu(menu); } - } // ns } // ns diff --git a/src/blackgui/components/dbstashcomponent.h b/src/blackgui/components/dbstashcomponent.h index 8f5b353b5..ab9f06e7a 100644 --- a/src/blackgui/components/dbstashcomponent.h +++ b/src/blackgui/components/dbstashcomponent.h @@ -18,6 +18,7 @@ #include "blackmisc/network/webdataservicesprovider.h" #include #include +#include namespace Ui { class CDbStashComponent; } @@ -25,6 +26,8 @@ namespace BlackGui { namespace Components { + class CDbMappingComponent; + /*! * Stashed objects */ @@ -60,10 +63,34 @@ namespace BlackGui //! The embedded view const BlackGui::Views::CAircraftModelView *getView() const; + //! Corresponding mapping component + void setMappingComponent(CDbMappingComponent *mappingComponent) { m_mappingComponent = mappingComponent; } + + //! Has stashed models + bool hasStashedModels() const; + + //! Stashed model strings + QStringList getStashedModelStrings() const; + + //! Apply object to select objects + void applyToSelected(const BlackMisc::Aviation::CLivery &livery, bool acceptWarnings = true); + + //! Apply object to select objects + void applyToSelected(const BlackMisc::Aviation::CAircraftIcaoCode &icao, bool acceptWarnings = true); + + //! Apply object to select objects + void applyToSelected(const BlackMisc::Aviation::CAirlineIcaoCode &icao, bool acceptWarnings = true); + + //! Apply object to select objects + void applyToSelected(const BlackMisc::Simulation::CDistributor &distributor, bool acceptWarnings = true); + signals: //! Unstash void unstashed(BlackMisc::Simulation::CAircraftModel &model); + //! Stashed models have been changed + void stashedModelChanged(); + private slots: //! Unstash pressed void ps_onUnstashPressed(); @@ -71,8 +98,18 @@ namespace BlackGui //! Validate pressed void ps_onValidatePressed(); + //! Copy over values + void ps_copyOverValues(); + private: QScopedPointer ui; + CDbMappingComponent *m_mappingComponent = nullptr; //!< corresponding mapping component + + //! Display messages + bool showMessages(const BlackMisc::CStatusMessageList &msgs, bool onlyErrors = false); + + //! Display message + bool showMessage(const BlackMisc::CStatusMessage &msg); //! Custom menu for the stashed models class CStashModelsMenu : public BlackGui::IMenuDelegate diff --git a/src/blackgui/components/dbstashcomponent.ui b/src/blackgui/components/dbstashcomponent.ui index 2c05c9866..1ca9ecbce 100644 --- a/src/blackgui/components/dbstashcomponent.ui +++ b/src/blackgui/components/dbstashcomponent.ui @@ -6,8 +6,8 @@ 0 0 - 400 - 300 + 616 + 221 @@ -72,21 +72,63 @@ - Unstash + unstash + + + + + + + Qt::Vertical - Validate + validate - Save + save + + + + + + + Qt::Vertical + + + + + + + distributor + + + + + + + aircraft ICAO + + + + + + + livery + + + + + + + airline ICAO