Add a comment to spicify that you assume order on variable in the m_Variables is the same that it is in the VariableInspector table view
@@ -7,6 +7,7 | |||
|
7 | 7 | #include <QAbstractTableModel> |
|
8 | 8 | #include <QLoggingCategory> |
|
9 | 9 | |
|
10 | #include <Common/MetaTypes.h> | |
|
10 | 11 | #include <Common/spimpl.h> |
|
11 | 12 | |
|
12 | 13 | Q_DECLARE_LOGGING_CATEGORY(LOG_VariableModel) |
@@ -50,6 +51,13 public: | |||
|
50 | 51 | private: |
|
51 | 52 | class VariableModelPrivate; |
|
52 | 53 | spimpl::unique_impl_ptr<VariableModelPrivate> impl; |
|
54 | ||
|
55 | private slots: | |
|
56 | /// Slot called when data of a variable has been updated | |
|
57 | void onVariableUpdated() noexcept; | |
|
53 | 58 | }; |
|
54 | 59 | |
|
60 | // Registers QVector<int> metatype so it can be used in VariableModel::dataChanged() signal | |
|
61 | SCIQLOP_REGISTER_META_TYPE(QVECTOR_INT_REGISTRY, QVector<int>) | |
|
62 | ||
|
55 | 63 | #endif // SCIQLOP_VARIABLEMODEL_H |
@@ -63,6 +63,7 std::shared_ptr<Variable> VariableModel::createVariable(const QString &name, | |||
|
63 | 63 | QStringLiteral("mission"), dateTime); |
|
64 | 64 | |
|
65 | 65 | impl->m_Variables.push_back(variable); |
|
66 | connect(variable.get(), &Variable::updated, this, &VariableModel::onVariableUpdated); | |
|
66 | 67 | |
|
67 | 68 | endInsertRows(); |
|
68 | 69 | |
@@ -177,3 +178,21 QVariant VariableModel::headerData(int section, Qt::Orientation orientation, int | |||
|
177 | 178 | |
|
178 | 179 | return QVariant{}; |
|
179 | 180 | } |
|
181 | ||
|
182 | void VariableModel::onVariableUpdated() noexcept | |
|
183 | { | |
|
184 | // Finds variable that has been updated in the model | |
|
185 | if (auto updatedVariable = dynamic_cast<Variable *>(sender())) { | |
|
186 | auto begin = std::cbegin(impl->m_Variables); | |
|
187 | auto end = std::cend(impl->m_Variables); | |
|
188 |
auto it = std::find_if(begin, end, [updatedVariable](const auto &variable) {
|
|
|
189 | return variable.get() == updatedVariable; | |
|
190 | }); | |
|
191 | ||
|
192 | if (it != end) { | |
|
193 | auto updateVariableIndex = std::distance(begin, it); | |
|
194 | emit dataChanged(createIndex(updateVariableIndex, 0), | |
|
195 | createIndex(updateVariableIndex, columnCount() - 1)); | |
|
196 | } | |
|
197 | } | |
|
198 | } |
@@ -45,6 +45,8 private: | |||
|
45 | 45 | private slots: |
|
46 | 46 | /// Slot called when right clicking on an variable in the table (displays a menu) |
|
47 | 47 | void onTableMenuRequested(const QPoint &pos) noexcept; |
|
48 | /// Refreshes instantly the variable view | |
|
49 | void refresh() noexcept; | |
|
48 | 50 | }; |
|
49 | 51 | |
|
50 | 52 | #endif // SCIQLOP_VARIABLEINSPECTORWIDGET_H |
@@ -21,7 +21,14 VariableInspectorWidget::VariableInspectorWidget(QWidget *parent) | |||
|
21 | 21 | // auto sortFilterModel = new QSortFilterProxyModel{this}; |
|
22 | 22 | // sortFilterModel->setSourceModel(sqpApp->variableController().variableModel()); |
|
23 | 23 | |
|
24 |
|
|
|
24 | auto variableModel = sqpApp->variableController().variableModel(); | |
|
25 | ui->tableView->setModel(variableModel); | |
|
26 | ||
|
27 | // Adds extra signal/slot between view and model, so the view can be updated instantly when | |
|
28 | // there is a change of data in the model | |
|
29 | connect(variableModel, SIGNAL(dataChanged(const QModelIndex &, const QModelIndex &)), this, | |
|
30 | SLOT(refresh())); | |
|
31 | ||
|
25 | 32 | ui->tableView->setSelectionModel(sqpApp->variableController().variableSelectionModel()); |
|
26 | 33 | |
|
27 | 34 | // Fixes column sizes |
@@ -87,3 +94,8 void VariableInspectorWidget::onTableMenuRequested(const QPoint &pos) noexcept | |||
|
87 | 94 | tableMenu.exec(mapToGlobal(pos)); |
|
88 | 95 | } |
|
89 | 96 | } |
|
97 | ||
|
98 | void VariableInspectorWidget::refresh() noexcept | |
|
99 | { | |
|
100 | ui->tableView->viewport()->update(); | |
|
101 | } |
General Comments 0
You need to be logged in to leave comments.
Login now