Files
pilotclient/src/blackgui/components/dbautosimulatorstashingcomponent.cpp

170 lines
6.9 KiB
C++

/* 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 <QIntValidator>
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<CDbMappingComponent * >(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<CSimulatorInfo> 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