From a1357f0bd79b12b6f1176988dd8533a0484cb9c2 Mon Sep 17 00:00:00 2001 From: Klaus Basan Date: Wed, 26 Jul 2017 14:09:14 +0200 Subject: [PATCH] Ref T111, UI element for model key completion --- .../components/dbmodelkeycompleter.cpp | 56 +++++++++++++++++++ src/blackgui/components/dbmodelkeycompleter.h | 50 +++++++++++++++++ 2 files changed, 106 insertions(+) create mode 100644 src/blackgui/components/dbmodelkeycompleter.cpp create mode 100644 src/blackgui/components/dbmodelkeycompleter.h diff --git a/src/blackgui/components/dbmodelkeycompleter.cpp b/src/blackgui/components/dbmodelkeycompleter.cpp new file mode 100644 index 000000000..1bd40f563 --- /dev/null +++ b/src/blackgui/components/dbmodelkeycompleter.cpp @@ -0,0 +1,56 @@ +/* Copyright (C) 2017 + * 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 "dbmodelkeycompleter.h" +#include "blackgui/guiapplication.h" +#include "blackgui/uppercasevalidator.h" +#include "blackcore/webdataservices.h" +#include + +using namespace BlackCore; + +namespace BlackGui +{ + namespace Components + { + CDbModelKeyCompleter::CDbModelKeyCompleter(QWidget *parent) : QLineEdit(parent) + { + Q_ASSERT_X(sGui, Q_FUNC_INFO, "Need sGui"); + Q_ASSERT_X(sGui->hasWebDataServices(), Q_FUNC_INFO, "Need web services"); + + this->setValidator(new CUpperCaseValidator(this)); + this->initCompleter(); + connect(sGui->getWebDataServices(), &CWebDataServices::swiftDbModelsRead, this, &CDbModelKeyCompleter::onModelsRead); + } + + void CDbModelKeyCompleter::setSimulator(const BlackMisc::Simulation::CSimulatorInfo &simulator) + { + if (m_simulator == simulator) { return; } + m_simulator = simulator; + this->initCompleter(); + } + + BlackMisc::Simulation::CAircraftModel CDbModelKeyCompleter::getAircraftModel() const + { + return sGui->getWebDataServices()->getModelForModelString(this->text().toUpper().trimmed()); + } + + void CDbModelKeyCompleter::onModelsRead() + { + this->initCompleter(); + } + + void CDbModelKeyCompleter::initCompleter() + { + const QStringList models(sGui->getWebDataServices()->getModelCompleterStrings(true, m_simulator)); + if (models.isEmpty()) { return; } + this->setCompleter(new QCompleter(models, this)); + } + } // ns +} // ns diff --git a/src/blackgui/components/dbmodelkeycompleter.h b/src/blackgui/components/dbmodelkeycompleter.h new file mode 100644 index 000000000..898f982d0 --- /dev/null +++ b/src/blackgui/components/dbmodelkeycompleter.h @@ -0,0 +1,50 @@ +/* Copyright (C) 2017 + * 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_COMPONENTS_DBMODELKEYCOMPLETER_H +#define BLACKGUI_COMPONENTS_DBMODELKEYCOMPLETER_H + +#include "blackmisc/simulation/simulatorinfo.h" +#include "blackmisc/simulation/aircraftmodel.h" +#include + +namespace BlackGui +{ + namespace Components + { + /** + * Completer for model keys + */ + class CDbModelKeyCompleter : public QLineEdit + { + public: + //! Ctor + CDbModelKeyCompleter(QWidget *parent = nullptr); + + //! Limit models to simulator, or add unspecified + void setSimulator(const BlackMisc::Simulation::CSimulatorInfo &simulator); + + //! Aircraft model for current string (searched in DB data) + BlackMisc::Simulation::CAircraftModel getAircraftModel() const; + + private: + //! Models have been read + void onModelsRead(); + + //! Init completer + void initCompleter(); + + BlackMisc::Simulation::CSimulatorInfo m_simulator { BlackMisc::Simulation::CSimulatorInfo::All }; + }; + } // ns +} // ns + +#endif // guard