From fc12ad933c3bd0694923ee707124e092afe79fbe 2017-07-07 09:26:52 From: Alexandre Leroux Date: 2017-07-07 09:26:52 Subject: [PATCH] Fixes refresh problem in Variable widget --- diff --git a/core/include/Variable/VariableModel.h b/core/include/Variable/VariableModel.h index 9e78a94..3a89ad2 100644 --- a/core/include/Variable/VariableModel.h +++ b/core/include/Variable/VariableModel.h @@ -7,6 +7,7 @@ #include #include +#include #include Q_DECLARE_LOGGING_CATEGORY(LOG_VariableModel) @@ -50,6 +51,13 @@ public: private: class VariableModelPrivate; spimpl::unique_impl_ptr impl; + +private slots: + /// Slot called when data of a variable has been updated + void onVariableUpdated() noexcept; }; +// Registers QVector metatype so it can be used in VariableModel::dataChanged() signal +SCIQLOP_REGISTER_META_TYPE(QVECTOR_INT_REGISTRY, QVector) + #endif // SCIQLOP_VARIABLEMODEL_H diff --git a/core/src/Variable/VariableModel.cpp b/core/src/Variable/VariableModel.cpp index b8510dc..e9a7ea0 100644 --- a/core/src/Variable/VariableModel.cpp +++ b/core/src/Variable/VariableModel.cpp @@ -63,6 +63,7 @@ std::shared_ptr VariableModel::createVariable(const QString &name, QStringLiteral("mission"), dateTime); impl->m_Variables.push_back(variable); + connect(variable.get(), &Variable::updated, this, &VariableModel::onVariableUpdated); endInsertRows(); @@ -177,3 +178,21 @@ QVariant VariableModel::headerData(int section, Qt::Orientation orientation, int return QVariant{}; } + +void VariableModel::onVariableUpdated() noexcept +{ + // Finds variable that has been updated in the model + if (auto updatedVariable = dynamic_cast(sender())) { + auto begin = std::cbegin(impl->m_Variables); + auto end = std::cend(impl->m_Variables); + auto it = std::find_if(begin, end, [updatedVariable](const auto &variable) { + return variable.get() == updatedVariable; + }); + + if (it != end) { + auto updateVariableIndex = std::distance(begin, it); + emit dataChanged(createIndex(updateVariableIndex, 0), + createIndex(updateVariableIndex, columnCount() - 1)); + } + } +} diff --git a/gui/include/Variable/VariableInspectorWidget.h b/gui/include/Variable/VariableInspectorWidget.h index 56fa387..cc98294 100644 --- a/gui/include/Variable/VariableInspectorWidget.h +++ b/gui/include/Variable/VariableInspectorWidget.h @@ -45,6 +45,8 @@ private: private slots: /// Slot called when right clicking on an variable in the table (displays a menu) void onTableMenuRequested(const QPoint &pos) noexcept; + /// Refreshes instantly the variable view + void refresh() noexcept; }; #endif // SCIQLOP_VARIABLEINSPECTORWIDGET_H diff --git a/gui/src/Variable/VariableInspectorWidget.cpp b/gui/src/Variable/VariableInspectorWidget.cpp index fe00434..93b38c8 100644 --- a/gui/src/Variable/VariableInspectorWidget.cpp +++ b/gui/src/Variable/VariableInspectorWidget.cpp @@ -21,7 +21,14 @@ VariableInspectorWidget::VariableInspectorWidget(QWidget *parent) // auto sortFilterModel = new QSortFilterProxyModel{this}; // sortFilterModel->setSourceModel(sqpApp->variableController().variableModel()); - ui->tableView->setModel(sqpApp->variableController().variableModel()); + auto variableModel = sqpApp->variableController().variableModel(); + ui->tableView->setModel(variableModel); + + // Adds extra signal/slot between view and model, so the view can be updated instantly when + // there is a change of data in the model + connect(variableModel, SIGNAL(dataChanged(const QModelIndex &, const QModelIndex &)), this, + SLOT(refresh())); + ui->tableView->setSelectionModel(sqpApp->variableController().variableSelectionModel()); // Fixes column sizes @@ -87,3 +94,8 @@ void VariableInspectorWidget::onTableMenuRequested(const QPoint &pos) noexcept tableMenu.exec(mapToGlobal(pos)); } } + +void VariableInspectorWidget::refresh() noexcept +{ + ui->tableView->viewport()->update(); +}