From ec40cfe998fe45f00ebe20f54224610fc3888180 2017-07-04 07:35:27 From: Alexandre Leroux Date: 2017-07-04 07:35:27 Subject: [PATCH] Variable deletion (3) Deletes from model --- diff --git a/core/include/Variable/VariableController.h b/core/include/Variable/VariableController.h index da13ee0..4334755 100644 --- a/core/include/Variable/VariableController.h +++ b/core/include/Variable/VariableController.h @@ -34,6 +34,7 @@ public: * Deletes from the controller the variable passed in parameter. * * Delete a variable includes: + * - the deletion of the model variable * - the deletion of the provider associated with the variable * - removing the cache associated with the variable * diff --git a/core/include/Variable/VariableModel.h b/core/include/Variable/VariableModel.h index 7fa865a..9e78a94 100644 --- a/core/include/Variable/VariableModel.h +++ b/core/include/Variable/VariableModel.h @@ -30,6 +30,12 @@ public: std::shared_ptr createVariable(const QString &name, const SqpDateTime &dateTime) noexcept; + /** + * Deletes a variable from the model, if it exists + * @param variable the variable to delete + */ + void deleteVariable(std::shared_ptr variable) noexcept; + std::shared_ptr variable(int index) const; // /////////////////////////// // diff --git a/core/src/Variable/VariableController.cpp b/core/src/Variable/VariableController.cpp index 22f98ea..b156724 100644 --- a/core/src/Variable/VariableController.cpp +++ b/core/src/Variable/VariableController.cpp @@ -99,6 +99,9 @@ void VariableController::deleteVariable(std::shared_ptr variable) noex // Clears cache impl->m_VariableCacheController->clear(variable); + // Deletes from model + impl->m_VariableModel->deleteVariable(variable); +} void VariableController::deleteVariables( const QVector > &variables) noexcept diff --git a/core/src/Variable/VariableModel.cpp b/core/src/Variable/VariableModel.cpp index 9d6c6f1..b8510dc 100644 --- a/core/src/Variable/VariableModel.cpp +++ b/core/src/Variable/VariableModel.cpp @@ -69,6 +69,32 @@ std::shared_ptr VariableModel::createVariable(const QString &name, return variable; } +void VariableModel::deleteVariable(std::shared_ptr variable) noexcept +{ + if (!variable) { + qCCritical(LOG_Variable()) << "Can't delete a null variable from the model"; + return; + } + + // Finds variable in the model + auto begin = impl->m_Variables.cbegin(); + auto end = impl->m_Variables.cend(); + auto it = std::find(begin, end, variable); + if (it != end) { + auto removeIndex = std::distance(begin, it); + + // Deletes variable + beginRemoveRows({}, removeIndex, removeIndex); + impl->m_Variables.erase(it); + endRemoveRows(); + } + else { + qCritical(LOG_VariableModel()) + << tr("Can't delete variable %1 from the model: the variable is not in the model") + .arg(variable->name()); + } +} + std::shared_ptr VariableModel::variable(int index) const { return (index >= 0 && index < impl->m_Variables.size()) ? impl->m_Variables[index] : nullptr;