mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-03-22 23:05:36 +08:00
refs #618, allow to modify and validate data when loaded for a view
* allows to reject data considered incorrect * data can be modified and adjusted after loading before displaying
This commit is contained in:
@@ -289,6 +289,19 @@ namespace BlackGui
|
||||
CViewWithDbObjects::customMenu(menu);
|
||||
}
|
||||
|
||||
CStatusMessage CAircraftModelView::validateLoadedData(const CAircraftModelList &models) const
|
||||
{
|
||||
static const CStatusMessage ok(this, CStatusMessage::SeverityInfo, "model validation passed", true);
|
||||
if (models.isEmpty()) { return CStatusMessage(this, CStatusMessage::SeverityInfo, "no data", true); }
|
||||
if (this->m_validation == AllowOnlySingeSimulator)
|
||||
{
|
||||
const CSimulatorInfo sim = models.simulatorsSupported();
|
||||
if (sim.isSingleSimulator()) { return ok; }
|
||||
return CStatusMessage(this, CStatusMessage::SeverityError, "data need to be from one simulator");
|
||||
}
|
||||
return CViewWithDbObjects::validateLoadedData(models);
|
||||
}
|
||||
|
||||
void CAircraftModelView::ps_toggleHighlightStashedModels()
|
||||
{
|
||||
bool h = derivedModel()->highlightModelStrings();
|
||||
|
||||
@@ -28,6 +28,14 @@ namespace BlackGui
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
//! How to validate loaded JSON data
|
||||
enum LoadValidationFlag
|
||||
{
|
||||
NoValidation,
|
||||
AllowOnlySingeSimulator
|
||||
};
|
||||
Q_DECLARE_FLAGS(LoadValidation, LoadValidationFlag)
|
||||
|
||||
//! Constructor
|
||||
explicit CAircraftModelView(QWidget *parent = nullptr);
|
||||
|
||||
@@ -79,6 +87,9 @@ namespace BlackGui
|
||||
//! \copydoc BlackGui::Models::CAircraftModelListModel::highlightModelStrings
|
||||
bool highlightModelStrings() const;
|
||||
|
||||
//! Load validation
|
||||
void setLoadValidation(LoadValidation validation) { m_validation = validation; }
|
||||
|
||||
signals:
|
||||
//! Request to stash if applicable
|
||||
void requestStash(const BlackMisc::Simulation::CAircraftModelList &models);
|
||||
@@ -93,8 +104,11 @@ namespace BlackGui
|
||||
//! \copydoc QTableView::dropEvent
|
||||
virtual void dropEvent(QDropEvent *event) override;
|
||||
|
||||
//! \copydoc CViewBaseNonTemplate::customMenu
|
||||
//! \name View base class overrides
|
||||
//! @{
|
||||
virtual void customMenu(QMenu &menu) const override;
|
||||
virtual BlackMisc::CStatusMessage validateLoadedData(const BlackMisc::Simulation::CAircraftModelList &models) const override;
|
||||
//! @}
|
||||
|
||||
private slots:
|
||||
//! Highlight stashed models
|
||||
@@ -110,8 +124,14 @@ namespace BlackGui
|
||||
void ps_requestStash();
|
||||
|
||||
private:
|
||||
bool m_stashingClearsSelection = true; //!< stashing unselects
|
||||
bool m_stashingClearsSelection = true; //!< stashing unselects
|
||||
LoadValidation m_validation = NoValidation; //!< Loaded JSON validation
|
||||
};
|
||||
} // ns
|
||||
} // ns
|
||||
|
||||
Q_DECLARE_METATYPE(BlackGui::Views::CAircraftModelView::LoadValidation)
|
||||
Q_DECLARE_METATYPE(BlackGui::Views::CAircraftModelView::LoadValidationFlag)
|
||||
Q_DECLARE_OPERATORS_FOR_FLAGS(BlackGui::Views::CAircraftModelView::LoadValidation)
|
||||
|
||||
#endif // guard
|
||||
|
||||
@@ -955,42 +955,58 @@ namespace BlackGui
|
||||
return this->updateContainer(c, sort, resize);
|
||||
}
|
||||
|
||||
template <class ModelClass, class ContainerType, class ObjectType>
|
||||
CStatusMessage CViewBase<ModelClass, ContainerType, ObjectType>::modifyLoadedData(ContainerType &data) const
|
||||
{
|
||||
Q_UNUSED(data);
|
||||
static const CStatusMessage e(this, CStatusMessage::SeverityInfo, "no modification", true);
|
||||
return e;
|
||||
}
|
||||
|
||||
template <class ModelClass, class ContainerType, class ObjectType>
|
||||
CStatusMessage CViewBase<ModelClass, ContainerType, ObjectType>::validateLoadedData(const ContainerType &data) const
|
||||
{
|
||||
Q_UNUSED(data);
|
||||
static const CStatusMessage e(this, CStatusMessage::SeverityInfo, "no validation", true);
|
||||
return e;
|
||||
}
|
||||
|
||||
template <class ModelClass, class ContainerType, class ObjectType>
|
||||
CStatusMessage CViewBase<ModelClass, ContainerType, ObjectType>::ps_loadJson()
|
||||
{
|
||||
static const CLogCategoryList cats(CLogCategoryList(this).join({ CLogCategory::validation()}));
|
||||
const QString fileName = QFileDialog::getOpenFileName(nullptr,
|
||||
tr("Load data file"), getDefaultFilename(true),
|
||||
tr("swift (*.json *.txt)"));
|
||||
if (fileName.isEmpty()) { return CStatusMessage(cats, CStatusMessage::SeverityDebug, "Load canceled"); }
|
||||
if (fileName.isEmpty()) { return CStatusMessage(this, CStatusMessage::SeverityDebug, "Load canceled", true); }
|
||||
QString json(CFileUtils::readFileToString(fileName));
|
||||
if (json.isEmpty())
|
||||
{
|
||||
return CStatusMessage(cats, CStatusMessage::SeverityWarning, "Reading " + fileName + " yields no data");
|
||||
return CStatusMessage(this, CStatusMessage::SeverityWarning, "Reading " + fileName + " yields no data", true);
|
||||
}
|
||||
ContainerType container;
|
||||
container.convertFromJson(json);
|
||||
const CStatusMessage s = this->validateLoadedData(container);
|
||||
if (s.getSeverity() == CStatusMessage::SeverityError) { return s; }
|
||||
this->updateContainerMaybeAsync(container);
|
||||
return CStatusMessage(cats, CStatusMessage::SeverityInfo, "Reading " + fileName + " completed");
|
||||
return CStatusMessage(this, CStatusMessage::SeverityInfo, "Reading " + fileName + " completed", true);
|
||||
}
|
||||
|
||||
template <class ModelClass, class ContainerType, class ObjectType>
|
||||
CStatusMessage CViewBase<ModelClass, ContainerType, ObjectType>::ps_saveJson() const
|
||||
{
|
||||
static const CLogCategoryList cats(CLogCategoryList(this).join({ CLogCategory::validation()}));
|
||||
const QString fileName = QFileDialog::getSaveFileName(nullptr,
|
||||
tr("Save data file"), getDefaultFilename(false),
|
||||
tr("swift (*.json *.txt)"));
|
||||
if (fileName.isEmpty()) { return CStatusMessage(cats, CStatusMessage::SeverityDebug, "Save canceled"); }
|
||||
if (fileName.isEmpty()) { return CStatusMessage(this, CStatusMessage::SeverityDebug, "Save canceled", true); }
|
||||
const QString json(this->toJsonString());
|
||||
bool ok = CFileUtils::writeStringToFileInBackground(json, fileName);
|
||||
if (ok)
|
||||
{
|
||||
return CStatusMessage(cats, CStatusMessage::SeverityInfo, "Writing " + fileName + " in progress");
|
||||
return CStatusMessage(this, CStatusMessage::SeverityInfo, "Writing " + fileName + " in progress", true);
|
||||
}
|
||||
else
|
||||
{
|
||||
return CStatusMessage(cats, CStatusMessage::SeverityError, "Writing " + fileName + " failed");
|
||||
return CStatusMessage(this, CStatusMessage::SeverityError, "Writing " + fileName + " failed", true);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -528,6 +528,12 @@ namespace BlackGui
|
||||
//! \copydoc BlackGui::Views::CViewBaseNonTemplate::performUpdateContainer
|
||||
virtual int performUpdateContainer(const BlackMisc::CVariant &variant, bool sort, bool resize) override;
|
||||
|
||||
//! Modify JSON data loaded in BlackGui::Views::CViewBaseNonTemplate::ps_loadJson
|
||||
virtual BlackMisc::CStatusMessage modifyLoadedData(ContainerType &data) const;
|
||||
|
||||
//! Verify JSON data loaded in BlackGui::Views::CViewBaseNonTemplate::ps_loadJson
|
||||
virtual BlackMisc::CStatusMessage validateLoadedData(const ContainerType &data) const;
|
||||
|
||||
// --------------------------------------------- SLOTS start here -----------------------------------------
|
||||
|
||||
//! \copydoc BlackGui::Views::CViewBaseNonTemplate::ps_filterDialogFinished
|
||||
|
||||
Reference in New Issue
Block a user