diff --git a/src/blackgui/components/dbautosimulatorstashingcomponent.cpp b/src/blackgui/components/dbautosimulatorstashingcomponent.cpp new file mode 100644 index 000000000..48d5639ce --- /dev/null +++ b/src/blackgui/components/dbautosimulatorstashingcomponent.cpp @@ -0,0 +1,169 @@ +/* Copyright (C) 2016 + * 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 "blackcore/db/databaseutils.h" +#include "dbautosimulatorstashingcomponent.h" +#include "dbmappingcomponent.h" +#include "ui_dbautosimulatorstashingcomponent.h" +#include + +using namespace BlackGui; +using namespace BlackCore::Db; +using namespace BlackMisc; +using namespace BlackMisc::Simulation; + +namespace BlackGui +{ + namespace Components + { + CDbAutoSimulatorStashingComponent::CDbAutoSimulatorStashingComponent(QWidget *parent) : + QDialog(parent, Qt::WindowSystemMenuHint | Qt::WindowTitleHint), + CDbMappingComponentAware(qobject_cast(parent)), + ui(new Ui::CDbAutoSimulatorStashingComponent) + { + ui->setupUi(this); + ui->le_MaxModelsStashed->setValidator(new QIntValidator(this)); + ui->tvp_StatusMessages->setMode(BlackGui::Models::CStatusMessageListModel::Simplified); + ui->le_MaxModelsStashed->setText("100"); + } + + int CDbAutoSimulatorStashingComponent::exec() + { + this->initGui(); + return QDialog::exec(); + } + + void CDbAutoSimulatorStashingComponent::accept() + { + switch (this->m_state) + { + case Running: return; + case Completed: + { + if (!this->m_modelsToStash.isEmpty()) + { + // this removes previously stashed models + this->getMappingComponent()->replaceStashedModelsUnvalidated(this->m_modelsToStash); + const CStatusMessage stashedMsg(this, CStatusMessage::SeverityInfo, QString("Stashed %1 models").arg(this->m_modelsToStash.size())); + this->addStatusMessage(stashedMsg); + this->m_modelsToStash.clear(); + } + QDialog::accept(); + break; + } + default: + { + this->tryToStash(); + break; + } + } + } + + CDbAutoSimulatorStashingComponent::~CDbAutoSimulatorStashingComponent() + { } + + void CDbAutoSimulatorStashingComponent::updateProgressIndicator(int percent) + { + if (percent > 100) { percent = 100; } + if (percent < 0) { percent = 0; } + ui->pb_StashingProgress->setValue(percent); + } + + Views::CAircraftModelView *CDbAutoSimulatorStashingComponent::currentModelView() const + { + return this->getMappingComponent()->currentModelView(); + } + + void CDbAutoSimulatorStashingComponent::initGui() + { + ui->bb_OkCancel->setStandardButtons(QDialogButtonBox::Ok | QDialogButtonBox::Cancel); + ui->tvp_StatusMessages->clear(); + this->m_state = Idle; + this->updateProgressIndicator(0); + + const QString infoAll = this->getMappingComponent()->getOwnModelsInfoStringFsFamily(); + ui->le_AllSets->setText(infoAll); + + if (!this->currentModelView()) + { + const CStatusMessage m(this, CStatusMessage::SeverityError, "No data for simulator updating"); + this->addStatusMessage(m); + } + else + { + int selected = this->currentModelView()->selectedRowCount(); + ui->le_Selected->setText(QString::number(selected)); + } + } + + void CDbAutoSimulatorStashingComponent::addStatusMessage(const CStatusMessage &msg) + { + if (msg.isEmpty()) { return; } + ui->tvp_StatusMessages->insert(msg); + } + + void CDbAutoSimulatorStashingComponent::addStatusMessage(const CStatusMessage &msg, const CAircraftModel &model) + { + if (msg.isEmpty()) { return; } + if (model.hasModelString()) + { + CStatusMessage prefixMessage(msg); + prefixMessage.prependMessage(QString(model.getModelString() + ", " + model.getMembersDbStatus() + ": ")); + ui->tvp_StatusMessages->insert(prefixMessage); + } + else + { + ui->tvp_StatusMessages->insert(msg); + } + } + + void CDbAutoSimulatorStashingComponent::tryToStash() + { + Q_ASSERT_X(this->getMappingComponent(), Q_FUNC_INFO, "Missing mapping component"); + + if (!this->currentModelView()) { return; } + this->m_state = Running; + int maxObjectsStashed = -1; + if (!ui->le_MaxModelsStashed->text().isEmpty()) + { + bool ok; + ui->le_MaxModelsStashed->text().toInt(&ok); + if (!ok) { maxObjectsStashed = 100; } + } + + const bool selected = ui->rb_SelectedOnly->isChecked(); + int ownModelsCount = 0; + if (selected) + { + const QString intro("Checking %1 selected models"); + const CAircraftModelList selectedModels(this->currentModelView()->selectedObjects()); + ownModelsCount = selectedModels.size(); + this->addStatusMessage(CStatusMessage(this, CStatusMessage::SeverityInfo, intro.arg(ownModelsCount))); + this->m_modelsToStash = CDatabaseUtils::updateSimulatorForFsFamily(selectedModels, maxObjectsStashed, this, true); + } + else + { + const CDbMappingComponent *mappincComponent = this->getMappingComponent(); + const QSet fsFamilySims(CSimulatorInfo::allFsFamilySimulators().asSingleSimulatorSet()); + const QString intro("Checking %1 models for %2"); + for (const CSimulatorInfo &simulator : fsFamilySims) + { + const CAircraftModelList ownModels = mappincComponent->getOwnCachedModels(simulator); + const QString sim = simulator.toQString(); + ownModelsCount += ownModels.size(); + this->addStatusMessage(CStatusMessage(this, CStatusMessage::SeverityInfo, intro.arg(ownModels.size()).arg(sim))); + this->m_modelsToStash.push_back(CDatabaseUtils::updateSimulatorForFsFamily(ownModels, maxObjectsStashed, this, true)); + } + } + const QString result("Tested %1 own models, %2 models should be updated in DB"); + this->addStatusMessage(CStatusMessage(this, CStatusMessage::SeverityInfo, result.arg(ownModelsCount).arg(this->m_modelsToStash.size()))); + this->m_state = Completed; + } + } // ns +} // ns diff --git a/src/blackgui/components/dbautosimulatorstashingcomponent.h b/src/blackgui/components/dbautosimulatorstashingcomponent.h new file mode 100644 index 000000000..a8777be45 --- /dev/null +++ b/src/blackgui/components/dbautosimulatorstashingcomponent.h @@ -0,0 +1,91 @@ +/* Copyright (C) 2016 + * 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_DBAUTOSIMULATORSTASHINGCOMPONENT_H +#define BLACKGUI_COMPONENTS_DBAUTOSIMULATORSTASHINGCOMPONENT_H + +#include "blackgui/blackguiexport.h" +#include "blackgui/components/dbmappingcomponentaware.h" +#include "blackgui/views/aircraftmodelview.h" +#include "blackcore/progress.h" +#include "blackmisc/simulation/aircraftmodel.h" +#include "blackmisc/statusmessage.h" +#include +#include + +namespace Ui { class CDbAutoSimulatorStashingComponent; } +namespace BlackGui +{ + namespace Components + { + /** + * Allows to automatically updated models if found in own model set, but already existing + * for a sibling simulator (ie. FSX/P3D/FS9) + */ + class BLACKGUI_EXPORT CDbAutoSimulatorStashingComponent : + public QDialog, + public BlackGui::Components::CDbMappingComponentAware, + public BlackCore::IProgressIndicator + { + Q_OBJECT + + public: + //! Current state of this component + enum State + { + Idle, + Running, + Completed + }; + + //! Constructor + explicit CDbAutoSimulatorStashingComponent(QWidget *parent = nullptr); + + //! Destructor + ~CDbAutoSimulatorStashingComponent(); + + //! At least run once and completed + bool isCompleted() const { return m_state == Completed; } + + //! \copydoc BlackCore::IProgressIndicator::updateProgressIndicator + virtual void updateProgressIndicator(int percent) override; + + public slots: + //! \copydoc QDialog::accept + virtual void accept() override; + + //! \copydoc QDialog::exec + virtual int exec() override; + + private: + //! Init the GUI + void initGui(); + + //! Model view to take models from + BlackGui::Views::CAircraftModelView *currentModelView() const; + + //! Add a status message + void addStatusMessage(const BlackMisc::CStatusMessage &msg); + + //! Add a status message for a given model (prefixed) + void addStatusMessage(const BlackMisc::CStatusMessage &msg, const BlackMisc::Simulation::CAircraftModel &model); + + //! Try to stash updated models + void tryToStash(); + + QScopedPointer ui; + State m_state = Idle; //!< modus + BlackMisc::Simulation::CAircraftModelList m_modelsToStash; //!< Models about to be stashed + }; + } // ns +} // ns + +#endif // guard diff --git a/src/blackgui/components/dbautosimulatorstashingcomponent.ui b/src/blackgui/components/dbautosimulatorstashingcomponent.ui new file mode 100644 index 000000000..f01dbeab8 --- /dev/null +++ b/src/blackgui/components/dbautosimulatorstashingcomponent.ui @@ -0,0 +1,148 @@ + + + CDbAutoSimulatorStashingComponent + + + + 0 + 0 + 600 + 400 + + + + + 600 + 400 + + + + Cross simulator updating (FSX-P3D-FS9) + + + + + + 24 + + + + + + + QAbstractItemView::NoSelection + + + false + + + + + + + true + + + + + + + selected + + + bg_ScanAllOrSelected + + + + + + + Qt::Horizontal + + + QDialogButtonBox::Cancel|QDialogButtonBox::Ok + + + + + + + scan all models + + + true + + + bg_ScanAllOrSelected + + + + + + + true + + + + + + + Max.stashed: + + + + + + + max.models to be stashed + + + + + + + + BlackGui::Views::CStatusMessageView + QTableView +
blackgui/views/statusmessageview.h
+
+
+ + + + bb_OkCancel + accepted() + CDbAutoSimulatorStashingComponent + accept() + + + 248 + 254 + + + 157 + 274 + + + + + bb_OkCancel + rejected() + CDbAutoSimulatorStashingComponent + reject() + + + 316 + 260 + + + 286 + 274 + + + + + + + +