From 9aa695e50c87b4b08f1082ae824615b24de78cea 2017-09-07 09:33:14 From: Alexandre Leroux Date: 2017-09-07 09:33:14 Subject: [PATCH] Adds variable to model --- diff --git a/core/include/Variable/VariableModel.h b/core/include/Variable/VariableModel.h index f4d30fa..2e891ee 100644 --- a/core/include/Variable/VariableModel.h +++ b/core/include/Variable/VariableModel.h @@ -28,6 +28,14 @@ public: explicit VariableModel(QObject *parent = nullptr); /** + * Adds an existing variable in the model. + * @param variable the variable to add. + * @remarks the variable's name is modified to avoid name duplicates + * @remarks this method does nothing if the variable already exists in the model + */ + void addVariable(std::shared_ptr variable) noexcept; + + /** * Creates a new variable in the model * @param name the name of the new variable * @param dateTime the dateTime of the new variable diff --git a/core/src/Variable/VariableController.cpp b/core/src/Variable/VariableController.cpp index 4b3293b..5951f28 100644 --- a/core/src/Variable/VariableController.cpp +++ b/core/src/Variable/VariableController.cpp @@ -194,6 +194,9 @@ VariableController::cloneVariable(std::shared_ptr variable) noexcept // Clones variable auto duplicate = variable->clone(); + // Adds clone to model + impl->m_VariableModel->addVariable(duplicate); + return duplicate; } diff --git a/core/src/Variable/VariableModel.cpp b/core/src/Variable/VariableModel.cpp index 94f0a7f..f0212ab 100644 --- a/core/src/Variable/VariableModel.cpp +++ b/core/src/Variable/VariableModel.cpp @@ -62,19 +62,23 @@ VariableModel::VariableModel(QObject *parent) { } -std::shared_ptr VariableModel::createVariable(const QString &name, - const SqpRange &dateTime, - const QVariantHash &metadata) noexcept +void VariableModel::addVariable(std::shared_ptr variable) noexcept { auto insertIndex = rowCount(); beginInsertRows({}, insertIndex, insertIndex); - auto variable = std::make_shared(name, dateTime, metadata); - impl->m_Variables.push_back(variable); connect(variable.get(), &Variable::updated, this, &VariableModel::onVariableUpdated); endInsertRows(); +} + +std::shared_ptr VariableModel::createVariable(const QString &name, + const SqpRange &dateTime, + const QVariantHash &metadata) noexcept +{ + auto variable = std::make_shared(name, dateTime, metadata); + addVariable(variable); return variable; }