##// END OF EJS Templates
Drag of variables
trabillard -
r849:73092e810307
parent child
Show More
@@ -1,130 +1,132
1 #ifndef SCIQLOP_VARIABLECONTROLLER_H
1 #ifndef SCIQLOP_VARIABLECONTROLLER_H
2 #define SCIQLOP_VARIABLECONTROLLER_H
2 #define SCIQLOP_VARIABLECONTROLLER_H
3
3
4 #include "CoreGlobal.h"
4 #include "CoreGlobal.h"
5
5
6 #include <Data/AcquisitionDataPacket.h>
6 #include <Data/AcquisitionDataPacket.h>
7 #include <Data/SqpRange.h>
7 #include <Data/SqpRange.h>
8
8
9 #include <QLoggingCategory>
9 #include <QLoggingCategory>
10 #include <QObject>
10 #include <QObject>
11 #include <QUuid>
11 #include <QUuid>
12
12
13 #include <Common/spimpl.h>
13 #include <Common/spimpl.h>
14
14
15 class IDataProvider;
15 class IDataProvider;
16 class QItemSelectionModel;
16 class QItemSelectionModel;
17 class TimeController;
17 class TimeController;
18 class Variable;
18 class Variable;
19 class VariableModel;
19 class VariableModel;
20
20
21 Q_DECLARE_LOGGING_CATEGORY(LOG_VariableController)
21 Q_DECLARE_LOGGING_CATEGORY(LOG_VariableController)
22
22
23
23
24 /**
24 /**
25 * Possible types of zoom operation
25 * Possible types of zoom operation
26 */
26 */
27 enum class AcquisitionZoomType { ZoomOut, ZoomIn, PanRight, PanLeft, Unknown };
27 enum class AcquisitionZoomType { ZoomOut, ZoomIn, PanRight, PanLeft, Unknown };
28
28
29
29
30 /**
30 /**
31 * @brief The VariableController class aims to handle the variables in SciQlop.
31 * @brief The VariableController class aims to handle the variables in SciQlop.
32 */
32 */
33 class SCIQLOP_CORE_EXPORT VariableController : public QObject {
33 class SCIQLOP_CORE_EXPORT VariableController : public QObject {
34 Q_OBJECT
34 Q_OBJECT
35 public:
35 public:
36 explicit VariableController(QObject *parent = 0);
36 explicit VariableController(QObject *parent = 0);
37 virtual ~VariableController();
37 virtual ~VariableController();
38
38
39 VariableModel *variableModel() noexcept;
39 VariableModel *variableModel() noexcept;
40 QItemSelectionModel *variableSelectionModel() noexcept;
40 QItemSelectionModel *variableSelectionModel() noexcept;
41
41
42 void setTimeController(TimeController *timeController) noexcept;
42 void setTimeController(TimeController *timeController) noexcept;
43
43
44 /**
44 /**
45 * Clones the variable passed in parameter and adds the duplicate to the controller
45 * Clones the variable passed in parameter and adds the duplicate to the controller
46 * @param variable the variable to duplicate
46 * @param variable the variable to duplicate
47 * @return the duplicate created, nullptr if the variable couldn't be created
47 * @return the duplicate created, nullptr if the variable couldn't be created
48 */
48 */
49 std::shared_ptr<Variable> cloneVariable(std::shared_ptr<Variable> variable) noexcept;
49 std::shared_ptr<Variable> cloneVariable(std::shared_ptr<Variable> variable) noexcept;
50
50
51 /**
51 /**
52 * Deletes from the controller the variable passed in parameter.
52 * Deletes from the controller the variable passed in parameter.
53 *
53 *
54 * Delete a variable includes:
54 * Delete a variable includes:
55 * - the deletion of the various references to the variable in SciQlop
55 * - the deletion of the various references to the variable in SciQlop
56 * - the deletion of the model variable
56 * - the deletion of the model variable
57 * - the deletion of the provider associated with the variable
57 * - the deletion of the provider associated with the variable
58 * - removing the cache associated with the variable
58 * - removing the cache associated with the variable
59 *
59 *
60 * @param variable the variable to delete from the controller.
60 * @param variable the variable to delete from the controller.
61 */
61 */
62 void deleteVariable(std::shared_ptr<Variable> variable) noexcept;
62 void deleteVariable(std::shared_ptr<Variable> variable) noexcept;
63
63
64 /**
64 /**
65 * Deletes from the controller the variables passed in parameter.
65 * Deletes from the controller the variables passed in parameter.
66 * @param variables the variables to delete from the controller.
66 * @param variables the variables to delete from the controller.
67 * @sa deleteVariable()
67 * @sa deleteVariable()
68 */
68 */
69 void deleteVariables(const QVector<std::shared_ptr<Variable> > &variables) noexcept;
69 void deleteVariables(const QVector<std::shared_ptr<Variable> > &variables) noexcept;
70
70
71 QByteArray mimeDataForVariables(const QList<std::shared_ptr<Variable> > &variables) const;
72 QList<std::shared_ptr<Variable> > variablesForMimeData(const QByteArray &mimeData) const;
71
73
72 static AcquisitionZoomType getZoomType(const SqpRange &range, const SqpRange &oldRange);
74 static AcquisitionZoomType getZoomType(const SqpRange &range, const SqpRange &oldRange);
73 signals:
75 signals:
74 /// Signal emitted when a variable is about to be deleted from the controller
76 /// Signal emitted when a variable is about to be deleted from the controller
75 void variableAboutToBeDeleted(std::shared_ptr<Variable> variable);
77 void variableAboutToBeDeleted(std::shared_ptr<Variable> variable);
76
78
77 /// Signal emitted when a data acquisition is requested on a range for a variable
79 /// Signal emitted when a data acquisition is requested on a range for a variable
78 void rangeChanged(std::shared_ptr<Variable> variable, const SqpRange &range);
80 void rangeChanged(std::shared_ptr<Variable> variable, const SqpRange &range);
79
81
80 /// Signal emitted when a sub range of the cacheRange of the variable can be displayed
82 /// Signal emitted when a sub range of the cacheRange of the variable can be displayed
81 void updateVarDisplaying(std::shared_ptr<Variable> variable, const SqpRange &range);
83 void updateVarDisplaying(std::shared_ptr<Variable> variable, const SqpRange &range);
82
84
83 public slots:
85 public slots:
84 /// Request the data loading of the variable whithin range
86 /// Request the data loading of the variable whithin range
85 void onRequestDataLoading(QVector<std::shared_ptr<Variable> > variables, const SqpRange &range,
87 void onRequestDataLoading(QVector<std::shared_ptr<Variable> > variables, const SqpRange &range,
86 bool synchronise);
88 bool synchronise);
87 /**
89 /**
88 * Creates a new variable and adds it to the model
90 * Creates a new variable and adds it to the model
89 * @param name the name of the new variable
91 * @param name the name of the new variable
90 * @param metadata the metadata of the new variable
92 * @param metadata the metadata of the new variable
91 * @param provider the data provider for the new variable
93 * @param provider the data provider for the new variable
92 * @return the pointer to the new variable or nullptr if the creation failed
94 * @return the pointer to the new variable or nullptr if the creation failed
93 */
95 */
94 std::shared_ptr<Variable> createVariable(const QString &name, const QVariantHash &metadata,
96 std::shared_ptr<Variable> createVariable(const QString &name, const QVariantHash &metadata,
95 std::shared_ptr<IDataProvider> provider) noexcept;
97 std::shared_ptr<IDataProvider> provider) noexcept;
96
98
97 /// Update the temporal parameters of every selected variable to dateTime
99 /// Update the temporal parameters of every selected variable to dateTime
98 void onDateTimeOnSelection(const SqpRange &dateTime);
100 void onDateTimeOnSelection(const SqpRange &dateTime);
99
101
100
102
101 void onDataProvided(QUuid vIdentifier, const SqpRange &rangeRequested,
103 void onDataProvided(QUuid vIdentifier, const SqpRange &rangeRequested,
102 const SqpRange &cacheRangeRequested,
104 const SqpRange &cacheRangeRequested,
103 QVector<AcquisitionDataPacket> dataAcquired);
105 QVector<AcquisitionDataPacket> dataAcquired);
104
106
105 void onVariableRetrieveDataInProgress(QUuid identifier, double progress);
107 void onVariableRetrieveDataInProgress(QUuid identifier, double progress);
106
108
107 /// Cancel the current request for the variable
109 /// Cancel the current request for the variable
108 void onAbortProgressRequested(std::shared_ptr<Variable> variable);
110 void onAbortProgressRequested(std::shared_ptr<Variable> variable);
109 void onAbortAcquisitionRequested(QUuid vIdentifier);
111 void onAbortAcquisitionRequested(QUuid vIdentifier);
110
112
111 // synchronization group methods
113 // synchronization group methods
112 void onAddSynchronizationGroupId(QUuid synchronizationGroupId);
114 void onAddSynchronizationGroupId(QUuid synchronizationGroupId);
113 void onRemoveSynchronizationGroupId(QUuid synchronizationGroupId);
115 void onRemoveSynchronizationGroupId(QUuid synchronizationGroupId);
114 void onAddSynchronized(std::shared_ptr<Variable> variable, QUuid synchronizationGroupId);
116 void onAddSynchronized(std::shared_ptr<Variable> variable, QUuid synchronizationGroupId);
115
117
116 /// Desynchronizes the variable of the group whose identifier is passed in parameter
118 /// Desynchronizes the variable of the group whose identifier is passed in parameter
117 /// @remarks the method does nothing if the variable is not part of the group
119 /// @remarks the method does nothing if the variable is not part of the group
118 void desynchronize(std::shared_ptr<Variable> variable, QUuid synchronizationGroupId);
120 void desynchronize(std::shared_ptr<Variable> variable, QUuid synchronizationGroupId);
119
121
120 void initialize();
122 void initialize();
121 void finalize();
123 void finalize();
122
124
123 private:
125 private:
124 void waitForFinish();
126 void waitForFinish();
125
127
126 class VariableControllerPrivate;
128 class VariableControllerPrivate;
127 spimpl::unique_impl_ptr<VariableControllerPrivate> impl;
129 spimpl::unique_impl_ptr<VariableControllerPrivate> impl;
128 };
130 };
129
131
130 #endif // SCIQLOP_VARIABLECONTROLLER_H
132 #endif // SCIQLOP_VARIABLECONTROLLER_H
@@ -1,95 +1,109
1 #ifndef SCIQLOP_VARIABLEMODEL_H
1 #ifndef SCIQLOP_VARIABLEMODEL_H
2 #define SCIQLOP_VARIABLEMODEL_H
2 #define SCIQLOP_VARIABLEMODEL_H
3
3
4 #include "CoreGlobal.h"
4 #include "CoreGlobal.h"
5
5
6 #include <Data/SqpRange.h>
6 #include <Data/SqpRange.h>
7
7
8 #include <QAbstractTableModel>
8 #include <QAbstractTableModel>
9 #include <QLoggingCategory>
9 #include <QLoggingCategory>
10
10
11 #include <Common/MetaTypes.h>
11 #include <Common/MetaTypes.h>
12 #include <Common/spimpl.h>
12 #include <Common/spimpl.h>
13
13
14 Q_DECLARE_LOGGING_CATEGORY(LOG_VariableModel)
14 Q_DECLARE_LOGGING_CATEGORY(LOG_VariableModel)
15
15
16 enum VariableRoles { ProgressRole = Qt::UserRole };
16 enum VariableRoles { ProgressRole = Qt::UserRole };
17
17
18
18
19 class IDataSeries;
19 class IDataSeries;
20 class Variable;
20 class Variable;
21 class VariableController;
21
22
22 /**
23 /**
23 * @brief The VariableModel class aims to hold the variables that have been created in SciQlop
24 * @brief The VariableModel class aims to hold the variables that have been created in SciQlop
24 */
25 */
25 class SCIQLOP_CORE_EXPORT VariableModel : public QAbstractTableModel {
26 class SCIQLOP_CORE_EXPORT VariableModel : public QAbstractTableModel {
26 Q_OBJECT
27 Q_OBJECT
27 public:
28 public:
28 explicit VariableModel(QObject *parent = nullptr);
29 explicit VariableModel(VariableController *parent = nullptr);
29
30
30 /**
31 /**
31 * Adds an existing variable in the model.
32 * Adds an existing variable in the model.
32 * @param variable the variable to add.
33 * @param variable the variable to add.
33 * @remarks the variable's name is modified to avoid name duplicates
34 * @remarks the variable's name is modified to avoid name duplicates
34 * @remarks this method does nothing if the variable already exists in the model
35 * @remarks this method does nothing if the variable already exists in the model
35 */
36 */
36 void addVariable(std::shared_ptr<Variable> variable) noexcept;
37 void addVariable(std::shared_ptr<Variable> variable) noexcept;
37
38
38 /**
39 /**
39 * Checks that a variable is contained in the model
40 * Checks that a variable is contained in the model
40 * @param variable the variable to check
41 * @param variable the variable to check
41 * @return true if the variable is in the model, false otherwise
42 * @return true if the variable is in the model, false otherwise
42 */
43 */
43 bool containsVariable(std::shared_ptr<Variable> variable) const noexcept;
44 bool containsVariable(std::shared_ptr<Variable> variable) const noexcept;
44
45
45 /**
46 /**
46 * Creates a new variable in the model
47 * Creates a new variable in the model
47 * @param name the name of the new variable
48 * @param name the name of the new variable
48 * @param metadata the metadata associated to the new variable
49 * @param metadata the metadata associated to the new variable
49 * @return the pointer to the new variable
50 * @return the pointer to the new variable
50 */
51 */
51 std::shared_ptr<Variable> createVariable(const QString &name,
52 std::shared_ptr<Variable> createVariable(const QString &name,
52 const QVariantHash &metadata) noexcept;
53 const QVariantHash &metadata) noexcept;
53
54
54 /**
55 /**
55 * Deletes a variable from the model, if it exists
56 * Deletes a variable from the model, if it exists
56 * @param variable the variable to delete
57 * @param variable the variable to delete
57 */
58 */
58 void deleteVariable(std::shared_ptr<Variable> variable) noexcept;
59 void deleteVariable(std::shared_ptr<Variable> variable) noexcept;
59
60
60
61
61 std::shared_ptr<Variable> variable(int index) const;
62 std::shared_ptr<Variable> variable(int index) const;
62 std::vector<std::shared_ptr<Variable> > variables() const;
63 std::vector<std::shared_ptr<Variable> > variables() const;
63
64
64 void setDataProgress(std::shared_ptr<Variable> variable, double progress);
65 void setDataProgress(std::shared_ptr<Variable> variable, double progress);
65
66
66
67
67 // /////////////////////////// //
68 // /////////////////////////// //
68 // QAbstractTableModel methods //
69 // QAbstractTableModel methods //
69 // /////////////////////////// //
70 // /////////////////////////// //
70
71
71 virtual int columnCount(const QModelIndex &parent = QModelIndex{}) const override;
72 virtual int columnCount(const QModelIndex &parent = QModelIndex{}) const override;
72 virtual int rowCount(const QModelIndex &parent = QModelIndex{}) const override;
73 virtual int rowCount(const QModelIndex &parent = QModelIndex{}) const override;
73 virtual QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
74 virtual QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
74 virtual QVariant headerData(int section, Qt::Orientation orientation,
75 virtual QVariant headerData(int section, Qt::Orientation orientation,
75 int role = Qt::DisplayRole) const override;
76 int role = Qt::DisplayRole) const override;
76
77 virtual Qt::ItemFlags flags(const QModelIndex &index) const override;
78
79 // ///////////////// //
80 // Drag&Drop methods //
81 // ///////////////// //
82
83 virtual Qt::DropActions supportedDropActions() const override;
84 virtual Qt::DropActions supportedDragActions() const override;
85 virtual QStringList mimeTypes() const override;
86 virtual QMimeData *mimeData(const QModelIndexList &indexes) const override;
87 virtual bool canDropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column,
88 const QModelIndex &parent) const override;
89 virtual bool dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column,
90 const QModelIndex &parent) override;
77
91
78 void abortProgress(const QModelIndex &index);
92 void abortProgress(const QModelIndex &index);
79
93
80 signals:
94 signals:
81 void abortProgessRequested(std::shared_ptr<Variable> variable);
95 void abortProgessRequested(std::shared_ptr<Variable> variable);
82
96
83 private:
97 private:
84 class VariableModelPrivate;
98 class VariableModelPrivate;
85 spimpl::unique_impl_ptr<VariableModelPrivate> impl;
99 spimpl::unique_impl_ptr<VariableModelPrivate> impl;
86
100
87 private slots:
101 private slots:
88 /// Slot called when data of a variable has been updated
102 /// Slot called when data of a variable has been updated
89 void onVariableUpdated() noexcept;
103 void onVariableUpdated() noexcept;
90 };
104 };
91
105
92 // Registers QVector<int> metatype so it can be used in VariableModel::dataChanged() signal
106 // Registers QVector<int> metatype so it can be used in VariableModel::dataChanged() signal
93 SCIQLOP_REGISTER_META_TYPE(QVECTOR_INT_REGISTRY, QVector<int>)
107 SCIQLOP_REGISTER_META_TYPE(QVECTOR_INT_REGISTRY, QVector<int>)
94
108
95 #endif // SCIQLOP_VARIABLEMODEL_H
109 #endif // SCIQLOP_VARIABLEMODEL_H
@@ -1,1012 +1,1053
1 #include <Variable/Variable.h>
1 #include <Variable/Variable.h>
2 #include <Variable/VariableAcquisitionWorker.h>
2 #include <Variable/VariableAcquisitionWorker.h>
3 #include <Variable/VariableCacheStrategy.h>
3 #include <Variable/VariableCacheStrategy.h>
4 #include <Variable/VariableCacheStrategyFactory.h>
4 #include <Variable/VariableCacheStrategyFactory.h>
5 #include <Variable/VariableController.h>
5 #include <Variable/VariableController.h>
6 #include <Variable/VariableModel.h>
6 #include <Variable/VariableModel.h>
7 #include <Variable/VariableSynchronizationGroup.h>
7 #include <Variable/VariableSynchronizationGroup.h>
8
8
9 #include <Data/DataProviderParameters.h>
9 #include <Data/DataProviderParameters.h>
10 #include <Data/IDataProvider.h>
10 #include <Data/IDataProvider.h>
11 #include <Data/IDataSeries.h>
11 #include <Data/IDataSeries.h>
12 #include <Data/VariableRequest.h>
12 #include <Data/VariableRequest.h>
13 #include <Time/TimeController.h>
13 #include <Time/TimeController.h>
14
14
15 #include <QDataStream>
15 #include <QMutex>
16 #include <QMutex>
16 #include <QThread>
17 #include <QThread>
17 #include <QUuid>
18 #include <QUuid>
18 #include <QtCore/QItemSelectionModel>
19 #include <QtCore/QItemSelectionModel>
19
20
20 #include <deque>
21 #include <deque>
21 #include <set>
22 #include <set>
22 #include <unordered_map>
23 #include <unordered_map>
23
24
24 Q_LOGGING_CATEGORY(LOG_VariableController, "VariableController")
25 Q_LOGGING_CATEGORY(LOG_VariableController, "VariableController")
25
26
26 namespace {
27 namespace {
27
28
28 SqpRange computeSynchroRangeRequested(const SqpRange &varRange, const SqpRange &graphRange,
29 SqpRange computeSynchroRangeRequested(const SqpRange &varRange, const SqpRange &graphRange,
29 const SqpRange &oldGraphRange)
30 const SqpRange &oldGraphRange)
30 {
31 {
31 auto zoomType = VariableController::getZoomType(graphRange, oldGraphRange);
32 auto zoomType = VariableController::getZoomType(graphRange, oldGraphRange);
32
33
33 auto varRangeRequested = varRange;
34 auto varRangeRequested = varRange;
34 switch (zoomType) {
35 switch (zoomType) {
35 case AcquisitionZoomType::ZoomIn: {
36 case AcquisitionZoomType::ZoomIn: {
36 auto deltaLeft = graphRange.m_TStart - oldGraphRange.m_TStart;
37 auto deltaLeft = graphRange.m_TStart - oldGraphRange.m_TStart;
37 auto deltaRight = oldGraphRange.m_TEnd - graphRange.m_TEnd;
38 auto deltaRight = oldGraphRange.m_TEnd - graphRange.m_TEnd;
38 varRangeRequested.m_TStart += deltaLeft;
39 varRangeRequested.m_TStart += deltaLeft;
39 varRangeRequested.m_TEnd -= deltaRight;
40 varRangeRequested.m_TEnd -= deltaRight;
40 break;
41 break;
41 }
42 }
42
43
43 case AcquisitionZoomType::ZoomOut: {
44 case AcquisitionZoomType::ZoomOut: {
44 auto deltaLeft = oldGraphRange.m_TStart - graphRange.m_TStart;
45 auto deltaLeft = oldGraphRange.m_TStart - graphRange.m_TStart;
45 auto deltaRight = graphRange.m_TEnd - oldGraphRange.m_TEnd;
46 auto deltaRight = graphRange.m_TEnd - oldGraphRange.m_TEnd;
46 varRangeRequested.m_TStart -= deltaLeft;
47 varRangeRequested.m_TStart -= deltaLeft;
47 varRangeRequested.m_TEnd += deltaRight;
48 varRangeRequested.m_TEnd += deltaRight;
48 break;
49 break;
49 }
50 }
50 case AcquisitionZoomType::PanRight: {
51 case AcquisitionZoomType::PanRight: {
51 auto deltaLeft = graphRange.m_TStart - oldGraphRange.m_TStart;
52 auto deltaLeft = graphRange.m_TStart - oldGraphRange.m_TStart;
52 auto deltaRight = graphRange.m_TEnd - oldGraphRange.m_TEnd;
53 auto deltaRight = graphRange.m_TEnd - oldGraphRange.m_TEnd;
53 varRangeRequested.m_TStart += deltaLeft;
54 varRangeRequested.m_TStart += deltaLeft;
54 varRangeRequested.m_TEnd += deltaRight;
55 varRangeRequested.m_TEnd += deltaRight;
55 break;
56 break;
56 }
57 }
57 case AcquisitionZoomType::PanLeft: {
58 case AcquisitionZoomType::PanLeft: {
58 auto deltaLeft = oldGraphRange.m_TStart - graphRange.m_TStart;
59 auto deltaLeft = oldGraphRange.m_TStart - graphRange.m_TStart;
59 auto deltaRight = oldGraphRange.m_TEnd - graphRange.m_TEnd;
60 auto deltaRight = oldGraphRange.m_TEnd - graphRange.m_TEnd;
60 varRangeRequested.m_TStart -= deltaLeft;
61 varRangeRequested.m_TStart -= deltaLeft;
61 varRangeRequested.m_TEnd -= deltaRight;
62 varRangeRequested.m_TEnd -= deltaRight;
62 break;
63 break;
63 }
64 }
64 case AcquisitionZoomType::Unknown: {
65 case AcquisitionZoomType::Unknown: {
65 qCCritical(LOG_VariableController())
66 qCCritical(LOG_VariableController())
66 << VariableController::tr("Impossible to synchronize: zoom type unknown");
67 << VariableController::tr("Impossible to synchronize: zoom type unknown");
67 break;
68 break;
68 }
69 }
69 default:
70 default:
70 qCCritical(LOG_VariableController()) << VariableController::tr(
71 qCCritical(LOG_VariableController()) << VariableController::tr(
71 "Impossible to synchronize: zoom type not take into account");
72 "Impossible to synchronize: zoom type not take into account");
72 // No action
73 // No action
73 break;
74 break;
74 }
75 }
75
76
76 return varRangeRequested;
77 return varRangeRequested;
77 }
78 }
78 }
79 }
79
80
80 enum class VariableRequestHandlerState { OFF, RUNNING, PENDING };
81 enum class VariableRequestHandlerState { OFF, RUNNING, PENDING };
81
82
82 struct VariableRequestHandler {
83 struct VariableRequestHandler {
83
84
84 VariableRequestHandler()
85 VariableRequestHandler()
85 {
86 {
86 m_CanUpdate = false;
87 m_CanUpdate = false;
87 m_State = VariableRequestHandlerState::OFF;
88 m_State = VariableRequestHandlerState::OFF;
88 }
89 }
89
90
90 QUuid m_VarId;
91 QUuid m_VarId;
91 VariableRequest m_RunningVarRequest;
92 VariableRequest m_RunningVarRequest;
92 VariableRequest m_PendingVarRequest;
93 VariableRequest m_PendingVarRequest;
93 VariableRequestHandlerState m_State;
94 VariableRequestHandlerState m_State;
94 bool m_CanUpdate;
95 bool m_CanUpdate;
95 };
96 };
96
97
97 struct VariableController::VariableControllerPrivate {
98 struct VariableController::VariableControllerPrivate {
98 explicit VariableControllerPrivate(VariableController *parent)
99 explicit VariableControllerPrivate(VariableController *parent)
99 : m_WorkingMutex{},
100 : m_WorkingMutex{},
100 m_VariableModel{new VariableModel{parent}},
101 m_VariableModel{new VariableModel{parent}},
101 m_VariableSelectionModel{new QItemSelectionModel{m_VariableModel, parent}},
102 m_VariableSelectionModel{new QItemSelectionModel{m_VariableModel, parent}},
102 // m_VariableCacheStrategy{std::make_unique<VariableCacheStrategy>()},
103 // m_VariableCacheStrategy{std::make_unique<VariableCacheStrategy>()},
103 m_VariableCacheStrategy{VariableCacheStrategyFactory::createCacheStrategy(
104 m_VariableCacheStrategy{VariableCacheStrategyFactory::createCacheStrategy(
104 CacheStrategy::SingleThreshold)},
105 CacheStrategy::SingleThreshold)},
105 m_VariableAcquisitionWorker{std::make_unique<VariableAcquisitionWorker>()},
106 m_VariableAcquisitionWorker{std::make_unique<VariableAcquisitionWorker>()},
106 q{parent}
107 q{parent}
107 {
108 {
108
109
109 m_VariableAcquisitionWorker->moveToThread(&m_VariableAcquisitionWorkerThread);
110 m_VariableAcquisitionWorker->moveToThread(&m_VariableAcquisitionWorkerThread);
110 m_VariableAcquisitionWorkerThread.setObjectName("VariableAcquisitionWorkerThread");
111 m_VariableAcquisitionWorkerThread.setObjectName("VariableAcquisitionWorkerThread");
111 }
112 }
112
113
113
114
114 virtual ~VariableControllerPrivate()
115 virtual ~VariableControllerPrivate()
115 {
116 {
116 qCDebug(LOG_VariableController()) << tr("VariableControllerPrivate destruction");
117 qCDebug(LOG_VariableController()) << tr("VariableControllerPrivate destruction");
117 m_VariableAcquisitionWorkerThread.quit();
118 m_VariableAcquisitionWorkerThread.quit();
118 m_VariableAcquisitionWorkerThread.wait();
119 m_VariableAcquisitionWorkerThread.wait();
119 }
120 }
120
121
121
122
122 void processRequest(std::shared_ptr<Variable> var, const SqpRange &rangeRequested,
123 void processRequest(std::shared_ptr<Variable> var, const SqpRange &rangeRequested,
123 QUuid varRequestId);
124 QUuid varRequestId);
124
125
125 std::shared_ptr<Variable> findVariable(QUuid vIdentifier);
126 std::shared_ptr<Variable> findVariable(QUuid vIdentifier);
126 std::shared_ptr<IDataSeries>
127 std::shared_ptr<IDataSeries>
127 retrieveDataSeries(const QVector<AcquisitionDataPacket> acqDataPacketVector);
128 retrieveDataSeries(const QVector<AcquisitionDataPacket> acqDataPacketVector);
128
129
129 void registerProvider(std::shared_ptr<IDataProvider> provider);
130 void registerProvider(std::shared_ptr<IDataProvider> provider);
130
131
131 void storeVariableRequest(QUuid varId, QUuid varRequestId, const VariableRequest &varRequest);
132 void storeVariableRequest(QUuid varId, QUuid varRequestId, const VariableRequest &varRequest);
132 QUuid acceptVariableRequest(QUuid varId, std::shared_ptr<IDataSeries> dataSeries);
133 QUuid acceptVariableRequest(QUuid varId, std::shared_ptr<IDataSeries> dataSeries);
133 void updateVariables(QUuid varRequestId);
134 void updateVariables(QUuid varRequestId);
134 void updateVariableRequest(QUuid varRequestId);
135 void updateVariableRequest(QUuid varRequestId);
135 void cancelVariableRequest(QUuid varRequestId);
136 void cancelVariableRequest(QUuid varRequestId);
136 void executeVarRequest(std::shared_ptr<Variable> var, VariableRequest &varRequest);
137 void executeVarRequest(std::shared_ptr<Variable> var, VariableRequest &varRequest);
137
138
138 QMutex m_WorkingMutex;
139 QMutex m_WorkingMutex;
139 /// Variable model. The VariableController has the ownership
140 /// Variable model. The VariableController has the ownership
140 VariableModel *m_VariableModel;
141 VariableModel *m_VariableModel;
141 QItemSelectionModel *m_VariableSelectionModel;
142 QItemSelectionModel *m_VariableSelectionModel;
142
143
143
144
144 TimeController *m_TimeController{nullptr};
145 TimeController *m_TimeController{nullptr};
145 std::unique_ptr<VariableCacheStrategy> m_VariableCacheStrategy;
146 std::unique_ptr<VariableCacheStrategy> m_VariableCacheStrategy;
146 std::unique_ptr<VariableAcquisitionWorker> m_VariableAcquisitionWorker;
147 std::unique_ptr<VariableAcquisitionWorker> m_VariableAcquisitionWorker;
147 QThread m_VariableAcquisitionWorkerThread;
148 QThread m_VariableAcquisitionWorkerThread;
148
149
149 std::unordered_map<std::shared_ptr<Variable>, std::shared_ptr<IDataProvider> >
150 std::unordered_map<std::shared_ptr<Variable>, std::shared_ptr<IDataProvider> >
150 m_VariableToProviderMap;
151 m_VariableToProviderMap;
151 std::unordered_map<std::shared_ptr<Variable>, QUuid> m_VariableToIdentifierMap;
152 std::unordered_map<std::shared_ptr<Variable>, QUuid> m_VariableToIdentifierMap;
152 std::map<QUuid, std::shared_ptr<VariableSynchronizationGroup> >
153 std::map<QUuid, std::shared_ptr<VariableSynchronizationGroup> >
153 m_GroupIdToVariableSynchronizationGroupMap;
154 m_GroupIdToVariableSynchronizationGroupMap;
154 std::map<QUuid, QUuid> m_VariableIdGroupIdMap;
155 std::map<QUuid, QUuid> m_VariableIdGroupIdMap;
155 std::set<std::shared_ptr<IDataProvider> > m_ProviderSet;
156 std::set<std::shared_ptr<IDataProvider> > m_ProviderSet;
156
157
157 std::map<QUuid, std::list<QUuid> > m_VarGroupIdToVarIds;
158 std::map<QUuid, std::list<QUuid> > m_VarGroupIdToVarIds;
158 std::map<QUuid, std::unique_ptr<VariableRequestHandler> > m_VarIdToVarRequestHandler;
159 std::map<QUuid, std::unique_ptr<VariableRequestHandler> > m_VarIdToVarRequestHandler;
159
160
160 VariableController *q;
161 VariableController *q;
161 };
162 };
162
163
163
164
164 VariableController::VariableController(QObject *parent)
165 VariableController::VariableController(QObject *parent)
165 : QObject{parent}, impl{spimpl::make_unique_impl<VariableControllerPrivate>(this)}
166 : QObject{parent}, impl{spimpl::make_unique_impl<VariableControllerPrivate>(this)}
166 {
167 {
167 qCDebug(LOG_VariableController()) << tr("VariableController construction")
168 qCDebug(LOG_VariableController()) << tr("VariableController construction")
168 << QThread::currentThread();
169 << QThread::currentThread();
169
170
170 connect(impl->m_VariableModel, &VariableModel::abortProgessRequested, this,
171 connect(impl->m_VariableModel, &VariableModel::abortProgessRequested, this,
171 &VariableController::onAbortProgressRequested);
172 &VariableController::onAbortProgressRequested);
172
173
173 connect(impl->m_VariableAcquisitionWorker.get(),
174 connect(impl->m_VariableAcquisitionWorker.get(),
174 &VariableAcquisitionWorker::variableCanceledRequested, this,
175 &VariableAcquisitionWorker::variableCanceledRequested, this,
175 &VariableController::onAbortAcquisitionRequested);
176 &VariableController::onAbortAcquisitionRequested);
176
177
177 connect(impl->m_VariableAcquisitionWorker.get(), &VariableAcquisitionWorker::dataProvided, this,
178 connect(impl->m_VariableAcquisitionWorker.get(), &VariableAcquisitionWorker::dataProvided, this,
178 &VariableController::onDataProvided);
179 &VariableController::onDataProvided);
179 connect(impl->m_VariableAcquisitionWorker.get(),
180 connect(impl->m_VariableAcquisitionWorker.get(),
180 &VariableAcquisitionWorker::variableRequestInProgress, this,
181 &VariableAcquisitionWorker::variableRequestInProgress, this,
181 &VariableController::onVariableRetrieveDataInProgress);
182 &VariableController::onVariableRetrieveDataInProgress);
182
183
183
184
184 connect(&impl->m_VariableAcquisitionWorkerThread, &QThread::started,
185 connect(&impl->m_VariableAcquisitionWorkerThread, &QThread::started,
185 impl->m_VariableAcquisitionWorker.get(), &VariableAcquisitionWorker::initialize);
186 impl->m_VariableAcquisitionWorker.get(), &VariableAcquisitionWorker::initialize);
186 connect(&impl->m_VariableAcquisitionWorkerThread, &QThread::finished,
187 connect(&impl->m_VariableAcquisitionWorkerThread, &QThread::finished,
187 impl->m_VariableAcquisitionWorker.get(), &VariableAcquisitionWorker::finalize);
188 impl->m_VariableAcquisitionWorker.get(), &VariableAcquisitionWorker::finalize);
188
189
189
190
190 impl->m_VariableAcquisitionWorkerThread.start();
191 impl->m_VariableAcquisitionWorkerThread.start();
191 }
192 }
192
193
193 VariableController::~VariableController()
194 VariableController::~VariableController()
194 {
195 {
195 qCDebug(LOG_VariableController()) << tr("VariableController destruction")
196 qCDebug(LOG_VariableController()) << tr("VariableController destruction")
196 << QThread::currentThread();
197 << QThread::currentThread();
197 this->waitForFinish();
198 this->waitForFinish();
198 }
199 }
199
200
200 VariableModel *VariableController::variableModel() noexcept
201 VariableModel *VariableController::variableModel() noexcept
201 {
202 {
202 return impl->m_VariableModel;
203 return impl->m_VariableModel;
203 }
204 }
204
205
205 QItemSelectionModel *VariableController::variableSelectionModel() noexcept
206 QItemSelectionModel *VariableController::variableSelectionModel() noexcept
206 {
207 {
207 return impl->m_VariableSelectionModel;
208 return impl->m_VariableSelectionModel;
208 }
209 }
209
210
210 void VariableController::setTimeController(TimeController *timeController) noexcept
211 void VariableController::setTimeController(TimeController *timeController) noexcept
211 {
212 {
212 impl->m_TimeController = timeController;
213 impl->m_TimeController = timeController;
213 }
214 }
214
215
215 std::shared_ptr<Variable>
216 std::shared_ptr<Variable>
216 VariableController::cloneVariable(std::shared_ptr<Variable> variable) noexcept
217 VariableController::cloneVariable(std::shared_ptr<Variable> variable) noexcept
217 {
218 {
218 if (impl->m_VariableModel->containsVariable(variable)) {
219 if (impl->m_VariableModel->containsVariable(variable)) {
219 // Clones variable
220 // Clones variable
220 auto duplicate = variable->clone();
221 auto duplicate = variable->clone();
221
222
222 // Adds clone to model
223 // Adds clone to model
223 impl->m_VariableModel->addVariable(duplicate);
224 impl->m_VariableModel->addVariable(duplicate);
224
225
225 // Generates clone identifier
226 // Generates clone identifier
226 impl->m_VariableToIdentifierMap[duplicate] = QUuid::createUuid();
227 impl->m_VariableToIdentifierMap[duplicate] = QUuid::createUuid();
227
228
228 // Registers provider
229 // Registers provider
229 auto variableProvider = impl->m_VariableToProviderMap.at(variable);
230 auto variableProvider = impl->m_VariableToProviderMap.at(variable);
230 auto duplicateProvider = variableProvider != nullptr ? variableProvider->clone() : nullptr;
231 auto duplicateProvider = variableProvider != nullptr ? variableProvider->clone() : nullptr;
231
232
232 impl->m_VariableToProviderMap[duplicate] = duplicateProvider;
233 impl->m_VariableToProviderMap[duplicate] = duplicateProvider;
233 if (duplicateProvider) {
234 if (duplicateProvider) {
234 impl->registerProvider(duplicateProvider);
235 impl->registerProvider(duplicateProvider);
235 }
236 }
236
237
237 return duplicate;
238 return duplicate;
238 }
239 }
239 else {
240 else {
240 qCCritical(LOG_VariableController())
241 qCCritical(LOG_VariableController())
241 << tr("Can't create duplicate of variable %1: variable not registered in the model")
242 << tr("Can't create duplicate of variable %1: variable not registered in the model")
242 .arg(variable->name());
243 .arg(variable->name());
243 return nullptr;
244 return nullptr;
244 }
245 }
245 }
246 }
246
247
247 void VariableController::deleteVariable(std::shared_ptr<Variable> variable) noexcept
248 void VariableController::deleteVariable(std::shared_ptr<Variable> variable) noexcept
248 {
249 {
249 if (!variable) {
250 if (!variable) {
250 qCCritical(LOG_VariableController()) << "Can't delete variable: variable is null";
251 qCCritical(LOG_VariableController()) << "Can't delete variable: variable is null";
251 return;
252 return;
252 }
253 }
253
254
254 // Spreads in SciQlop that the variable will be deleted, so that potential receivers can
255 // Spreads in SciQlop that the variable will be deleted, so that potential receivers can
255 // make some treatments before the deletion
256 // make some treatments before the deletion
256 emit variableAboutToBeDeleted(variable);
257 emit variableAboutToBeDeleted(variable);
257
258
258 // Deletes identifier
259 // Deletes identifier
259 impl->m_VariableToIdentifierMap.erase(variable);
260 impl->m_VariableToIdentifierMap.erase(variable);
260
261
261 // Deletes provider
262 // Deletes provider
262 auto nbProvidersDeleted = impl->m_VariableToProviderMap.erase(variable);
263 auto nbProvidersDeleted = impl->m_VariableToProviderMap.erase(variable);
263 qCDebug(LOG_VariableController())
264 qCDebug(LOG_VariableController())
264 << tr("Number of providers deleted for variable %1: %2")
265 << tr("Number of providers deleted for variable %1: %2")
265 .arg(variable->name(), QString::number(nbProvidersDeleted));
266 .arg(variable->name(), QString::number(nbProvidersDeleted));
266
267
267
268
268 // Deletes from model
269 // Deletes from model
269 impl->m_VariableModel->deleteVariable(variable);
270 impl->m_VariableModel->deleteVariable(variable);
270 }
271 }
271
272
272 void VariableController::deleteVariables(
273 void VariableController::deleteVariables(
273 const QVector<std::shared_ptr<Variable> > &variables) noexcept
274 const QVector<std::shared_ptr<Variable> > &variables) noexcept
274 {
275 {
275 for (auto variable : qAsConst(variables)) {
276 for (auto variable : qAsConst(variables)) {
276 deleteVariable(variable);
277 deleteVariable(variable);
277 }
278 }
278 }
279 }
279
280
281 QByteArray
282 VariableController::mimeDataForVariables(const QList<std::shared_ptr<Variable> > &variables) const
283 {
284 auto encodedData = QByteArray{};
285
286 QVariantList ids;
287 for (auto &var : variables) {
288 auto itVar = impl->m_VariableToIdentifierMap.find(var);
289 if (itVar == impl->m_VariableToIdentifierMap.cend()) {
290 qCCritical(LOG_VariableController())
291 << tr("Impossible to find the data for an unknown variable.");
292 }
293
294 ids << itVar->second.toByteArray();
295 }
296
297 QDataStream stream{&encodedData, QIODevice::WriteOnly};
298 stream << ids;
299
300 return encodedData;
301 }
302
303 QList<std::shared_ptr<Variable> >
304 VariableController::variablesForMimeData(const QByteArray &mimeData) const
305 {
306 auto variables = QList<std::shared_ptr<Variable> >{};
307 QDataStream stream{mimeData};
308
309 QVariantList ids;
310 stream >> ids;
311
312 for (auto id : ids) {
313 auto uuid = QUuid(id.toByteArray());
314 auto var = impl->findVariable(uuid);
315 variables << var;
316 }
317
318 return variables;
319 }
320
280 std::shared_ptr<Variable>
321 std::shared_ptr<Variable>
281 VariableController::createVariable(const QString &name, const QVariantHash &metadata,
322 VariableController::createVariable(const QString &name, const QVariantHash &metadata,
282 std::shared_ptr<IDataProvider> provider) noexcept
323 std::shared_ptr<IDataProvider> provider) noexcept
283 {
324 {
284 if (!impl->m_TimeController) {
325 if (!impl->m_TimeController) {
285 qCCritical(LOG_VariableController())
326 qCCritical(LOG_VariableController())
286 << tr("Impossible to create variable: The time controller is null");
327 << tr("Impossible to create variable: The time controller is null");
287 return nullptr;
328 return nullptr;
288 }
329 }
289
330
290 auto range = impl->m_TimeController->dateTime();
331 auto range = impl->m_TimeController->dateTime();
291
332
292 if (auto newVariable = impl->m_VariableModel->createVariable(name, metadata)) {
333 if (auto newVariable = impl->m_VariableModel->createVariable(name, metadata)) {
293 auto varId = QUuid::createUuid();
334 auto varId = QUuid::createUuid();
294
335
295 // Create the handler
336 // Create the handler
296 auto varRequestHandler = std::make_unique<VariableRequestHandler>();
337 auto varRequestHandler = std::make_unique<VariableRequestHandler>();
297 varRequestHandler->m_VarId = varId;
338 varRequestHandler->m_VarId = varId;
298
339
299 impl->m_VarIdToVarRequestHandler.insert(
340 impl->m_VarIdToVarRequestHandler.insert(
300 std::make_pair(varId, std::move(varRequestHandler)));
341 std::make_pair(varId, std::move(varRequestHandler)));
301
342
302 // store the provider
343 // store the provider
303 impl->registerProvider(provider);
344 impl->registerProvider(provider);
304
345
305 // Associate the provider
346 // Associate the provider
306 impl->m_VariableToProviderMap[newVariable] = provider;
347 impl->m_VariableToProviderMap[newVariable] = provider;
307 impl->m_VariableToIdentifierMap[newVariable] = varId;
348 impl->m_VariableToIdentifierMap[newVariable] = varId;
308
349
309 this->onRequestDataLoading(QVector<std::shared_ptr<Variable> >{newVariable}, range, false);
350 this->onRequestDataLoading(QVector<std::shared_ptr<Variable> >{newVariable}, range, false);
310
351
311 // auto varRequestId = QUuid::createUuid();
352 // auto varRequestId = QUuid::createUuid();
312 // qCInfo(LOG_VariableController()) << "createVariable: " << varId << varRequestId;
353 // qCInfo(LOG_VariableController()) << "createVariable: " << varId << varRequestId;
313 // impl->processRequest(newVariable, range, varRequestId);
354 // impl->processRequest(newVariable, range, varRequestId);
314 // impl->updateVariableRequest(varRequestId);
355 // impl->updateVariableRequest(varRequestId);
315
356
316 return newVariable;
357 return newVariable;
317 }
358 }
318 }
359 }
319
360
320 void VariableController::onDateTimeOnSelection(const SqpRange &dateTime)
361 void VariableController::onDateTimeOnSelection(const SqpRange &dateTime)
321 {
362 {
322 // NOTE: Even if acquisition request is aborting, the graphe range will be changed
363 // NOTE: Even if acquisition request is aborting, the graphe range will be changed
323 qCDebug(LOG_VariableController()) << "VariableController::onDateTimeOnSelection"
364 qCDebug(LOG_VariableController()) << "VariableController::onDateTimeOnSelection"
324 << QThread::currentThread()->objectName();
365 << QThread::currentThread()->objectName();
325 auto selectedRows = impl->m_VariableSelectionModel->selectedRows();
366 auto selectedRows = impl->m_VariableSelectionModel->selectedRows();
326
367
327 // NOTE we only permit the time modification for one variable
368 // NOTE we only permit the time modification for one variable
328 // DEPRECATED
369 // DEPRECATED
329 // auto variables = QVector<std::shared_ptr<Variable> >{};
370 // auto variables = QVector<std::shared_ptr<Variable> >{};
330 // for (const auto &selectedRow : qAsConst(selectedRows)) {
371 // for (const auto &selectedRow : qAsConst(selectedRows)) {
331 // if (auto selectedVariable =
372 // if (auto selectedVariable =
332 // impl->m_VariableModel->variable(selectedRow.row())) {
373 // impl->m_VariableModel->variable(selectedRow.row())) {
333 // variables << selectedVariable;
374 // variables << selectedVariable;
334
375
335 // // notify that rescale operation has to be done
376 // // notify that rescale operation has to be done
336 // emit rangeChanged(selectedVariable, dateTime);
377 // emit rangeChanged(selectedVariable, dateTime);
337 // }
378 // }
338 // }
379 // }
339 // if (!variables.isEmpty()) {
380 // if (!variables.isEmpty()) {
340 // this->onRequestDataLoading(variables, dateTime, synchro);
381 // this->onRequestDataLoading(variables, dateTime, synchro);
341 // }
382 // }
342 if (selectedRows.size() == 1) {
383 if (selectedRows.size() == 1) {
343
384
344 if (auto selectedVariable
385 if (auto selectedVariable
345 = impl->m_VariableModel->variable(qAsConst(selectedRows).first().row())) {
386 = impl->m_VariableModel->variable(qAsConst(selectedRows).first().row())) {
346
387
347 auto itVar = impl->m_VariableToIdentifierMap.find(selectedVariable);
388 auto itVar = impl->m_VariableToIdentifierMap.find(selectedVariable);
348 if (itVar == impl->m_VariableToIdentifierMap.cend()) {
389 if (itVar == impl->m_VariableToIdentifierMap.cend()) {
349 qCCritical(LOG_VariableController())
390 qCCritical(LOG_VariableController())
350 << tr("Impossible to onDateTimeOnSelection request for unknown variable");
391 << tr("Impossible to onDateTimeOnSelection request for unknown variable");
351 return;
392 return;
352 }
393 }
353
394
354 // notify that rescale operation has to be done
395 // notify that rescale operation has to be done
355 emit rangeChanged(selectedVariable, dateTime);
396 emit rangeChanged(selectedVariable, dateTime);
356
397
357 auto synchro = impl->m_VariableIdGroupIdMap.find(itVar->second)
398 auto synchro = impl->m_VariableIdGroupIdMap.find(itVar->second)
358 != impl->m_VariableIdGroupIdMap.cend();
399 != impl->m_VariableIdGroupIdMap.cend();
359
400
360 this->onRequestDataLoading(QVector<std::shared_ptr<Variable> >{selectedVariable},
401 this->onRequestDataLoading(QVector<std::shared_ptr<Variable> >{selectedVariable},
361 dateTime, synchro);
402 dateTime, synchro);
362 }
403 }
363 }
404 }
364 else if (selectedRows.size() > 1) {
405 else if (selectedRows.size() > 1) {
365 qCCritical(LOG_VariableController())
406 qCCritical(LOG_VariableController())
366 << tr("Impossible to set time for more than 1 variable in the same time");
407 << tr("Impossible to set time for more than 1 variable in the same time");
367 }
408 }
368 else {
409 else {
369 qCWarning(LOG_VariableController())
410 qCWarning(LOG_VariableController())
370 << tr("There is no variable selected to set the time one");
411 << tr("There is no variable selected to set the time one");
371 }
412 }
372 }
413 }
373
414
374 void VariableController::onDataProvided(QUuid vIdentifier, const SqpRange &rangeRequested,
415 void VariableController::onDataProvided(QUuid vIdentifier, const SqpRange &rangeRequested,
375 const SqpRange &cacheRangeRequested,
416 const SqpRange &cacheRangeRequested,
376 QVector<AcquisitionDataPacket> dataAcquired)
417 QVector<AcquisitionDataPacket> dataAcquired)
377 {
418 {
378 qCDebug(LOG_VariableController()) << tr("onDataProvided") << QThread::currentThread();
419 qCDebug(LOG_VariableController()) << tr("onDataProvided") << QThread::currentThread();
379 auto retrievedDataSeries = impl->retrieveDataSeries(dataAcquired);
420 auto retrievedDataSeries = impl->retrieveDataSeries(dataAcquired);
380 auto varRequestId = impl->acceptVariableRequest(vIdentifier, retrievedDataSeries);
421 auto varRequestId = impl->acceptVariableRequest(vIdentifier, retrievedDataSeries);
381 if (!varRequestId.isNull()) {
422 if (!varRequestId.isNull()) {
382 impl->updateVariables(varRequestId);
423 impl->updateVariables(varRequestId);
383 }
424 }
384 }
425 }
385
426
386 void VariableController::onVariableRetrieveDataInProgress(QUuid identifier, double progress)
427 void VariableController::onVariableRetrieveDataInProgress(QUuid identifier, double progress)
387 {
428 {
388 qCDebug(LOG_VariableController())
429 qCDebug(LOG_VariableController())
389 << "TORM: variableController::onVariableRetrieveDataInProgress"
430 << "TORM: variableController::onVariableRetrieveDataInProgress"
390 << QThread::currentThread()->objectName() << progress;
431 << QThread::currentThread()->objectName() << progress;
391 if (auto var = impl->findVariable(identifier)) {
432 if (auto var = impl->findVariable(identifier)) {
392 impl->m_VariableModel->setDataProgress(var, progress);
433 impl->m_VariableModel->setDataProgress(var, progress);
393 }
434 }
394 else {
435 else {
395 qCCritical(LOG_VariableController())
436 qCCritical(LOG_VariableController())
396 << tr("Impossible to notify progression of a null variable");
437 << tr("Impossible to notify progression of a null variable");
397 }
438 }
398 }
439 }
399
440
400 void VariableController::onAbortProgressRequested(std::shared_ptr<Variable> variable)
441 void VariableController::onAbortProgressRequested(std::shared_ptr<Variable> variable)
401 {
442 {
402 qCDebug(LOG_VariableController()) << "TORM: variableController::onAbortProgressRequested"
443 qCDebug(LOG_VariableController()) << "TORM: variableController::onAbortProgressRequested"
403 << QThread::currentThread()->objectName() << variable->name();
444 << QThread::currentThread()->objectName() << variable->name();
404
445
405 auto itVar = impl->m_VariableToIdentifierMap.find(variable);
446 auto itVar = impl->m_VariableToIdentifierMap.find(variable);
406 if (itVar == impl->m_VariableToIdentifierMap.cend()) {
447 if (itVar == impl->m_VariableToIdentifierMap.cend()) {
407 qCCritical(LOG_VariableController())
448 qCCritical(LOG_VariableController())
408 << tr("Impossible to onAbortProgressRequested request for unknown variable");
449 << tr("Impossible to onAbortProgressRequested request for unknown variable");
409 return;
450 return;
410 }
451 }
411
452
412 auto varId = itVar->second;
453 auto varId = itVar->second;
413
454
414 auto itVarHandler = impl->m_VarIdToVarRequestHandler.find(varId);
455 auto itVarHandler = impl->m_VarIdToVarRequestHandler.find(varId);
415 if (itVarHandler == impl->m_VarIdToVarRequestHandler.cend()) {
456 if (itVarHandler == impl->m_VarIdToVarRequestHandler.cend()) {
416 qCCritical(LOG_VariableController())
457 qCCritical(LOG_VariableController())
417 << tr("Impossible to onAbortProgressRequested for variable with unknown handler");
458 << tr("Impossible to onAbortProgressRequested for variable with unknown handler");
418 return;
459 return;
419 }
460 }
420
461
421 auto varHandler = itVarHandler->second.get();
462 auto varHandler = itVarHandler->second.get();
422
463
423 // case where a variable has a running request
464 // case where a variable has a running request
424 if (varHandler->m_State != VariableRequestHandlerState::OFF) {
465 if (varHandler->m_State != VariableRequestHandlerState::OFF) {
425 impl->cancelVariableRequest(varHandler->m_RunningVarRequest.m_VariableGroupId);
466 impl->cancelVariableRequest(varHandler->m_RunningVarRequest.m_VariableGroupId);
426 }
467 }
427 }
468 }
428
469
429 void VariableController::onAbortAcquisitionRequested(QUuid vIdentifier)
470 void VariableController::onAbortAcquisitionRequested(QUuid vIdentifier)
430 {
471 {
431 qCDebug(LOG_VariableController()) << "TORM: variableController::onAbortAcquisitionRequested"
472 qCDebug(LOG_VariableController()) << "TORM: variableController::onAbortAcquisitionRequested"
432 << QThread::currentThread()->objectName() << vIdentifier;
473 << QThread::currentThread()->objectName() << vIdentifier;
433
474
434 if (auto var = impl->findVariable(vIdentifier)) {
475 if (auto var = impl->findVariable(vIdentifier)) {
435 this->onAbortProgressRequested(var);
476 this->onAbortProgressRequested(var);
436 }
477 }
437 else {
478 else {
438 qCCritical(LOG_VariableController())
479 qCCritical(LOG_VariableController())
439 << tr("Impossible to abort Acquisition Requestof a null variable");
480 << tr("Impossible to abort Acquisition Requestof a null variable");
440 }
481 }
441 }
482 }
442
483
443 void VariableController::onAddSynchronizationGroupId(QUuid synchronizationGroupId)
484 void VariableController::onAddSynchronizationGroupId(QUuid synchronizationGroupId)
444 {
485 {
445 qCDebug(LOG_VariableController()) << "TORM: VariableController::onAddSynchronizationGroupId"
486 qCDebug(LOG_VariableController()) << "TORM: VariableController::onAddSynchronizationGroupId"
446 << QThread::currentThread()->objectName()
487 << QThread::currentThread()->objectName()
447 << synchronizationGroupId;
488 << synchronizationGroupId;
448 auto vSynchroGroup = std::make_shared<VariableSynchronizationGroup>();
489 auto vSynchroGroup = std::make_shared<VariableSynchronizationGroup>();
449 impl->m_GroupIdToVariableSynchronizationGroupMap.insert(
490 impl->m_GroupIdToVariableSynchronizationGroupMap.insert(
450 std::make_pair(synchronizationGroupId, vSynchroGroup));
491 std::make_pair(synchronizationGroupId, vSynchroGroup));
451 }
492 }
452
493
453 void VariableController::onRemoveSynchronizationGroupId(QUuid synchronizationGroupId)
494 void VariableController::onRemoveSynchronizationGroupId(QUuid synchronizationGroupId)
454 {
495 {
455 impl->m_GroupIdToVariableSynchronizationGroupMap.erase(synchronizationGroupId);
496 impl->m_GroupIdToVariableSynchronizationGroupMap.erase(synchronizationGroupId);
456 }
497 }
457
498
458 void VariableController::onAddSynchronized(std::shared_ptr<Variable> variable,
499 void VariableController::onAddSynchronized(std::shared_ptr<Variable> variable,
459 QUuid synchronizationGroupId)
500 QUuid synchronizationGroupId)
460
501
461 {
502 {
462 qCDebug(LOG_VariableController()) << "TORM: VariableController::onAddSynchronized"
503 qCDebug(LOG_VariableController()) << "TORM: VariableController::onAddSynchronized"
463 << synchronizationGroupId;
504 << synchronizationGroupId;
464 auto varToVarIdIt = impl->m_VariableToIdentifierMap.find(variable);
505 auto varToVarIdIt = impl->m_VariableToIdentifierMap.find(variable);
465 if (varToVarIdIt != impl->m_VariableToIdentifierMap.cend()) {
506 if (varToVarIdIt != impl->m_VariableToIdentifierMap.cend()) {
466 auto groupIdToVSGIt
507 auto groupIdToVSGIt
467 = impl->m_GroupIdToVariableSynchronizationGroupMap.find(synchronizationGroupId);
508 = impl->m_GroupIdToVariableSynchronizationGroupMap.find(synchronizationGroupId);
468 if (groupIdToVSGIt != impl->m_GroupIdToVariableSynchronizationGroupMap.cend()) {
509 if (groupIdToVSGIt != impl->m_GroupIdToVariableSynchronizationGroupMap.cend()) {
469 impl->m_VariableIdGroupIdMap.insert(
510 impl->m_VariableIdGroupIdMap.insert(
470 std::make_pair(varToVarIdIt->second, synchronizationGroupId));
511 std::make_pair(varToVarIdIt->second, synchronizationGroupId));
471 groupIdToVSGIt->second->addVariableId(varToVarIdIt->second);
512 groupIdToVSGIt->second->addVariableId(varToVarIdIt->second);
472 }
513 }
473 else {
514 else {
474 qCCritical(LOG_VariableController())
515 qCCritical(LOG_VariableController())
475 << tr("Impossible to synchronize a variable with an unknown sycnhronization group")
516 << tr("Impossible to synchronize a variable with an unknown sycnhronization group")
476 << variable->name();
517 << variable->name();
477 }
518 }
478 }
519 }
479 else {
520 else {
480 qCCritical(LOG_VariableController())
521 qCCritical(LOG_VariableController())
481 << tr("Impossible to synchronize a variable with no identifier") << variable->name();
522 << tr("Impossible to synchronize a variable with no identifier") << variable->name();
482 }
523 }
483 }
524 }
484
525
485 void VariableController::desynchronize(std::shared_ptr<Variable> variable,
526 void VariableController::desynchronize(std::shared_ptr<Variable> variable,
486 QUuid synchronizationGroupId)
527 QUuid synchronizationGroupId)
487 {
528 {
488 // Gets variable id
529 // Gets variable id
489 auto variableIt = impl->m_VariableToIdentifierMap.find(variable);
530 auto variableIt = impl->m_VariableToIdentifierMap.find(variable);
490 if (variableIt == impl->m_VariableToIdentifierMap.cend()) {
531 if (variableIt == impl->m_VariableToIdentifierMap.cend()) {
491 qCCritical(LOG_VariableController())
532 qCCritical(LOG_VariableController())
492 << tr("Can't desynchronize variable %1: variable identifier not found")
533 << tr("Can't desynchronize variable %1: variable identifier not found")
493 .arg(variable->name());
534 .arg(variable->name());
494 return;
535 return;
495 }
536 }
496
537
497 // Gets synchronization group
538 // Gets synchronization group
498 auto groupIt = impl->m_GroupIdToVariableSynchronizationGroupMap.find(synchronizationGroupId);
539 auto groupIt = impl->m_GroupIdToVariableSynchronizationGroupMap.find(synchronizationGroupId);
499 if (groupIt == impl->m_GroupIdToVariableSynchronizationGroupMap.cend()) {
540 if (groupIt == impl->m_GroupIdToVariableSynchronizationGroupMap.cend()) {
500 qCCritical(LOG_VariableController())
541 qCCritical(LOG_VariableController())
501 << tr("Can't desynchronize variable %1: unknown synchronization group")
542 << tr("Can't desynchronize variable %1: unknown synchronization group")
502 .arg(variable->name());
543 .arg(variable->name());
503 return;
544 return;
504 }
545 }
505
546
506 auto variableId = variableIt->second;
547 auto variableId = variableIt->second;
507
548
508 // Removes variable from synchronization group
549 // Removes variable from synchronization group
509 auto synchronizationGroup = groupIt->second;
550 auto synchronizationGroup = groupIt->second;
510 synchronizationGroup->removeVariableId(variableId);
551 synchronizationGroup->removeVariableId(variableId);
511
552
512 // Removes link between variable and synchronization group
553 // Removes link between variable and synchronization group
513 impl->m_VariableIdGroupIdMap.erase(variableId);
554 impl->m_VariableIdGroupIdMap.erase(variableId);
514 }
555 }
515
556
516 void VariableController::onRequestDataLoading(QVector<std::shared_ptr<Variable> > variables,
557 void VariableController::onRequestDataLoading(QVector<std::shared_ptr<Variable> > variables,
517 const SqpRange &range, bool synchronise)
558 const SqpRange &range, bool synchronise)
518 {
559 {
519 // variables is assumed synchronized
560 // variables is assumed synchronized
520 // TODO: Asser variables synchronization
561 // TODO: Asser variables synchronization
521 // we want to load data of the variable for the dateTime.
562 // we want to load data of the variable for the dateTime.
522 if (variables.isEmpty()) {
563 if (variables.isEmpty()) {
523 return;
564 return;
524 }
565 }
525
566
526 auto varRequestId = QUuid::createUuid();
567 auto varRequestId = QUuid::createUuid();
527 qCDebug(LOG_VariableController()) << "VariableController::onRequestDataLoading"
568 qCDebug(LOG_VariableController()) << "VariableController::onRequestDataLoading"
528 << QThread::currentThread()->objectName() << varRequestId
569 << QThread::currentThread()->objectName() << varRequestId
529 << range << synchronise;
570 << range << synchronise;
530
571
531 if (!synchronise) {
572 if (!synchronise) {
532 auto varIds = std::list<QUuid>{};
573 auto varIds = std::list<QUuid>{};
533 for (const auto &var : variables) {
574 for (const auto &var : variables) {
534 auto vId = impl->m_VariableToIdentifierMap.at(var);
575 auto vId = impl->m_VariableToIdentifierMap.at(var);
535 varIds.push_back(vId);
576 varIds.push_back(vId);
536 }
577 }
537 impl->m_VarGroupIdToVarIds.insert(std::make_pair(varRequestId, varIds));
578 impl->m_VarGroupIdToVarIds.insert(std::make_pair(varRequestId, varIds));
538 for (const auto &var : variables) {
579 for (const auto &var : variables) {
539 qCDebug(LOG_VariableController()) << "processRequest for" << var->name() << varRequestId
580 qCDebug(LOG_VariableController()) << "processRequest for" << var->name() << varRequestId
540 << varIds.size();
581 << varIds.size();
541 impl->processRequest(var, range, varRequestId);
582 impl->processRequest(var, range, varRequestId);
542 }
583 }
543 }
584 }
544 else {
585 else {
545 auto vId = impl->m_VariableToIdentifierMap.at(variables.first());
586 auto vId = impl->m_VariableToIdentifierMap.at(variables.first());
546 auto varIdToGroupIdIt = impl->m_VariableIdGroupIdMap.find(vId);
587 auto varIdToGroupIdIt = impl->m_VariableIdGroupIdMap.find(vId);
547 if (varIdToGroupIdIt != impl->m_VariableIdGroupIdMap.cend()) {
588 if (varIdToGroupIdIt != impl->m_VariableIdGroupIdMap.cend()) {
548 auto groupId = varIdToGroupIdIt->second;
589 auto groupId = varIdToGroupIdIt->second;
549
590
550 auto vSynchronizationGroup
591 auto vSynchronizationGroup
551 = impl->m_GroupIdToVariableSynchronizationGroupMap.at(groupId);
592 = impl->m_GroupIdToVariableSynchronizationGroupMap.at(groupId);
552 auto vSyncIds = vSynchronizationGroup->getIds();
593 auto vSyncIds = vSynchronizationGroup->getIds();
553
594
554 auto varIds = std::list<QUuid>{};
595 auto varIds = std::list<QUuid>{};
555 for (auto vId : vSyncIds) {
596 for (auto vId : vSyncIds) {
556 varIds.push_back(vId);
597 varIds.push_back(vId);
557 }
598 }
558 impl->m_VarGroupIdToVarIds.insert(std::make_pair(varRequestId, varIds));
599 impl->m_VarGroupIdToVarIds.insert(std::make_pair(varRequestId, varIds));
559
600
560 for (auto vId : vSyncIds) {
601 for (auto vId : vSyncIds) {
561 auto var = impl->findVariable(vId);
602 auto var = impl->findVariable(vId);
562
603
563 // Don't process already processed var
604 // Don't process already processed var
564 if (var != nullptr) {
605 if (var != nullptr) {
565 qCDebug(LOG_VariableController()) << "processRequest synchro for" << var->name()
606 qCDebug(LOG_VariableController()) << "processRequest synchro for" << var->name()
566 << varRequestId;
607 << varRequestId;
567 auto vSyncRangeRequested
608 auto vSyncRangeRequested
568 = variables.contains(var)
609 = variables.contains(var)
569 ? range
610 ? range
570 : computeSynchroRangeRequested(var->range(), range,
611 : computeSynchroRangeRequested(var->range(), range,
571 variables.first()->range());
612 variables.first()->range());
572 qCDebug(LOG_VariableController()) << "synchro RR" << vSyncRangeRequested;
613 qCDebug(LOG_VariableController()) << "synchro RR" << vSyncRangeRequested;
573 impl->processRequest(var, vSyncRangeRequested, varRequestId);
614 impl->processRequest(var, vSyncRangeRequested, varRequestId);
574 }
615 }
575 else {
616 else {
576 qCCritical(LOG_VariableController())
617 qCCritical(LOG_VariableController())
577
618
578 << tr("Impossible to synchronize a null variable");
619 << tr("Impossible to synchronize a null variable");
579 }
620 }
580 }
621 }
581 }
622 }
582 }
623 }
583
624
584 impl->updateVariables(varRequestId);
625 impl->updateVariables(varRequestId);
585 }
626 }
586
627
587
628
588 void VariableController::initialize()
629 void VariableController::initialize()
589 {
630 {
590 qCDebug(LOG_VariableController()) << tr("VariableController init") << QThread::currentThread();
631 qCDebug(LOG_VariableController()) << tr("VariableController init") << QThread::currentThread();
591 impl->m_WorkingMutex.lock();
632 impl->m_WorkingMutex.lock();
592 qCDebug(LOG_VariableController()) << tr("VariableController init END");
633 qCDebug(LOG_VariableController()) << tr("VariableController init END");
593 }
634 }
594
635
595 void VariableController::finalize()
636 void VariableController::finalize()
596 {
637 {
597 impl->m_WorkingMutex.unlock();
638 impl->m_WorkingMutex.unlock();
598 }
639 }
599
640
600 void VariableController::waitForFinish()
641 void VariableController::waitForFinish()
601 {
642 {
602 QMutexLocker locker{&impl->m_WorkingMutex};
643 QMutexLocker locker{&impl->m_WorkingMutex};
603 }
644 }
604
645
605 AcquisitionZoomType VariableController::getZoomType(const SqpRange &range, const SqpRange &oldRange)
646 AcquisitionZoomType VariableController::getZoomType(const SqpRange &range, const SqpRange &oldRange)
606 {
647 {
607 // t1.m_TStart <= t2.m_TStart && t2.m_TEnd <= t1.m_TEnd
648 // t1.m_TStart <= t2.m_TStart && t2.m_TEnd <= t1.m_TEnd
608 auto zoomType = AcquisitionZoomType::Unknown;
649 auto zoomType = AcquisitionZoomType::Unknown;
609 if (range.m_TStart <= oldRange.m_TStart && oldRange.m_TEnd <= range.m_TEnd) {
650 if (range.m_TStart <= oldRange.m_TStart && oldRange.m_TEnd <= range.m_TEnd) {
610 qCDebug(LOG_VariableController()) << "zoomtype: ZoomOut";
651 qCDebug(LOG_VariableController()) << "zoomtype: ZoomOut";
611 zoomType = AcquisitionZoomType::ZoomOut;
652 zoomType = AcquisitionZoomType::ZoomOut;
612 }
653 }
613 else if (range.m_TStart > oldRange.m_TStart && range.m_TEnd > oldRange.m_TEnd) {
654 else if (range.m_TStart > oldRange.m_TStart && range.m_TEnd > oldRange.m_TEnd) {
614 qCDebug(LOG_VariableController()) << "zoomtype: PanRight";
655 qCDebug(LOG_VariableController()) << "zoomtype: PanRight";
615 zoomType = AcquisitionZoomType::PanRight;
656 zoomType = AcquisitionZoomType::PanRight;
616 }
657 }
617 else if (range.m_TStart < oldRange.m_TStart && range.m_TEnd < oldRange.m_TEnd) {
658 else if (range.m_TStart < oldRange.m_TStart && range.m_TEnd < oldRange.m_TEnd) {
618 qCDebug(LOG_VariableController()) << "zoomtype: PanLeft";
659 qCDebug(LOG_VariableController()) << "zoomtype: PanLeft";
619 zoomType = AcquisitionZoomType::PanLeft;
660 zoomType = AcquisitionZoomType::PanLeft;
620 }
661 }
621 else if (range.m_TStart > oldRange.m_TStart && oldRange.m_TEnd > range.m_TEnd) {
662 else if (range.m_TStart > oldRange.m_TStart && oldRange.m_TEnd > range.m_TEnd) {
622 qCDebug(LOG_VariableController()) << "zoomtype: ZoomIn";
663 qCDebug(LOG_VariableController()) << "zoomtype: ZoomIn";
623 zoomType = AcquisitionZoomType::ZoomIn;
664 zoomType = AcquisitionZoomType::ZoomIn;
624 }
665 }
625 else {
666 else {
626 qCDebug(LOG_VariableController()) << "getZoomType: Unknown type detected";
667 qCDebug(LOG_VariableController()) << "getZoomType: Unknown type detected";
627 }
668 }
628 return zoomType;
669 return zoomType;
629 }
670 }
630
671
631 void VariableController::VariableControllerPrivate::processRequest(std::shared_ptr<Variable> var,
672 void VariableController::VariableControllerPrivate::processRequest(std::shared_ptr<Variable> var,
632 const SqpRange &rangeRequested,
673 const SqpRange &rangeRequested,
633 QUuid varRequestId)
674 QUuid varRequestId)
634 {
675 {
635 auto itVar = m_VariableToIdentifierMap.find(var);
676 auto itVar = m_VariableToIdentifierMap.find(var);
636 if (itVar == m_VariableToIdentifierMap.cend()) {
677 if (itVar == m_VariableToIdentifierMap.cend()) {
637 qCCritical(LOG_VariableController())
678 qCCritical(LOG_VariableController())
638 << tr("Impossible to process request for unknown variable");
679 << tr("Impossible to process request for unknown variable");
639 return;
680 return;
640 }
681 }
641
682
642 auto varId = itVar->second;
683 auto varId = itVar->second;
643
684
644 auto itVarHandler = m_VarIdToVarRequestHandler.find(varId);
685 auto itVarHandler = m_VarIdToVarRequestHandler.find(varId);
645 if (itVarHandler == m_VarIdToVarRequestHandler.cend()) {
686 if (itVarHandler == m_VarIdToVarRequestHandler.cend()) {
646 qCCritical(LOG_VariableController())
687 qCCritical(LOG_VariableController())
647 << tr("Impossible to process request for variable with unknown handler");
688 << tr("Impossible to process request for variable with unknown handler");
648 return;
689 return;
649 }
690 }
650
691
651 auto oldRange = var->range();
692 auto oldRange = var->range();
652
693
653 auto varHandler = itVarHandler->second.get();
694 auto varHandler = itVarHandler->second.get();
654
695
655 if (varHandler->m_State != VariableRequestHandlerState::OFF) {
696 if (varHandler->m_State != VariableRequestHandlerState::OFF) {
656 oldRange = varHandler->m_RunningVarRequest.m_RangeRequested;
697 oldRange = varHandler->m_RunningVarRequest.m_RangeRequested;
657 }
698 }
658
699
659 auto varRequest = VariableRequest{};
700 auto varRequest = VariableRequest{};
660 varRequest.m_VariableGroupId = varRequestId;
701 varRequest.m_VariableGroupId = varRequestId;
661 auto varStrategyRangesRequested
702 auto varStrategyRangesRequested
662 = m_VariableCacheStrategy->computeRange(oldRange, rangeRequested);
703 = m_VariableCacheStrategy->computeRange(oldRange, rangeRequested);
663 varRequest.m_RangeRequested = varStrategyRangesRequested.first;
704 varRequest.m_RangeRequested = varStrategyRangesRequested.first;
664 varRequest.m_CacheRangeRequested = varStrategyRangesRequested.second;
705 varRequest.m_CacheRangeRequested = varStrategyRangesRequested.second;
665
706
666 switch (varHandler->m_State) {
707 switch (varHandler->m_State) {
667 case VariableRequestHandlerState::OFF: {
708 case VariableRequestHandlerState::OFF: {
668 qCDebug(LOG_VariableController()) << tr("Process Request OFF")
709 qCDebug(LOG_VariableController()) << tr("Process Request OFF")
669 << varRequest.m_RangeRequested
710 << varRequest.m_RangeRequested
670 << varRequest.m_CacheRangeRequested;
711 << varRequest.m_CacheRangeRequested;
671 varHandler->m_RunningVarRequest = varRequest;
712 varHandler->m_RunningVarRequest = varRequest;
672 varHandler->m_State = VariableRequestHandlerState::RUNNING;
713 varHandler->m_State = VariableRequestHandlerState::RUNNING;
673 executeVarRequest(var, varRequest);
714 executeVarRequest(var, varRequest);
674 break;
715 break;
675 }
716 }
676 case VariableRequestHandlerState::RUNNING: {
717 case VariableRequestHandlerState::RUNNING: {
677 qCDebug(LOG_VariableController()) << tr("Process Request RUNNING")
718 qCDebug(LOG_VariableController()) << tr("Process Request RUNNING")
678 << varRequest.m_RangeRequested
719 << varRequest.m_RangeRequested
679 << varRequest.m_CacheRangeRequested;
720 << varRequest.m_CacheRangeRequested;
680 varHandler->m_State = VariableRequestHandlerState::PENDING;
721 varHandler->m_State = VariableRequestHandlerState::PENDING;
681 varHandler->m_PendingVarRequest = varRequest;
722 varHandler->m_PendingVarRequest = varRequest;
682 break;
723 break;
683 }
724 }
684 case VariableRequestHandlerState::PENDING: {
725 case VariableRequestHandlerState::PENDING: {
685 qCDebug(LOG_VariableController()) << tr("Process Request PENDING")
726 qCDebug(LOG_VariableController()) << tr("Process Request PENDING")
686 << varRequest.m_RangeRequested
727 << varRequest.m_RangeRequested
687 << varRequest.m_CacheRangeRequested;
728 << varRequest.m_CacheRangeRequested;
688 auto variableGroupIdToCancel = varHandler->m_PendingVarRequest.m_VariableGroupId;
729 auto variableGroupIdToCancel = varHandler->m_PendingVarRequest.m_VariableGroupId;
689 cancelVariableRequest(variableGroupIdToCancel);
730 cancelVariableRequest(variableGroupIdToCancel);
690 // Cancel variable can make state downgrade
731 // Cancel variable can make state downgrade
691 varHandler->m_State = VariableRequestHandlerState::PENDING;
732 varHandler->m_State = VariableRequestHandlerState::PENDING;
692 varHandler->m_PendingVarRequest = varRequest;
733 varHandler->m_PendingVarRequest = varRequest;
693
734
694 break;
735 break;
695 }
736 }
696 default:
737 default:
697 qCCritical(LOG_VariableController())
738 qCCritical(LOG_VariableController())
698 << QObject::tr("Unknown VariableRequestHandlerState");
739 << QObject::tr("Unknown VariableRequestHandlerState");
699 }
740 }
700 }
741 }
701
742
702 std::shared_ptr<Variable>
743 std::shared_ptr<Variable>
703 VariableController::VariableControllerPrivate::findVariable(QUuid vIdentifier)
744 VariableController::VariableControllerPrivate::findVariable(QUuid vIdentifier)
704 {
745 {
705 std::shared_ptr<Variable> var;
746 std::shared_ptr<Variable> var;
706 auto findReply = [vIdentifier](const auto &entry) { return vIdentifier == entry.second; };
747 auto findReply = [vIdentifier](const auto &entry) { return vIdentifier == entry.second; };
707
748
708 auto end = m_VariableToIdentifierMap.cend();
749 auto end = m_VariableToIdentifierMap.cend();
709 auto it = std::find_if(m_VariableToIdentifierMap.cbegin(), end, findReply);
750 auto it = std::find_if(m_VariableToIdentifierMap.cbegin(), end, findReply);
710 if (it != end) {
751 if (it != end) {
711 var = it->first;
752 var = it->first;
712 }
753 }
713 else {
754 else {
714 qCCritical(LOG_VariableController())
755 qCCritical(LOG_VariableController())
715 << tr("Impossible to find the variable with the identifier: ") << vIdentifier;
756 << tr("Impossible to find the variable with the identifier: ") << vIdentifier;
716 }
757 }
717
758
718 return var;
759 return var;
719 }
760 }
720
761
721 std::shared_ptr<IDataSeries> VariableController::VariableControllerPrivate::retrieveDataSeries(
762 std::shared_ptr<IDataSeries> VariableController::VariableControllerPrivate::retrieveDataSeries(
722 const QVector<AcquisitionDataPacket> acqDataPacketVector)
763 const QVector<AcquisitionDataPacket> acqDataPacketVector)
723 {
764 {
724 qCDebug(LOG_VariableController()) << tr("TORM: retrieveDataSeries acqDataPacketVector size")
765 qCDebug(LOG_VariableController()) << tr("TORM: retrieveDataSeries acqDataPacketVector size")
725 << acqDataPacketVector.size();
766 << acqDataPacketVector.size();
726 std::shared_ptr<IDataSeries> dataSeries;
767 std::shared_ptr<IDataSeries> dataSeries;
727 if (!acqDataPacketVector.isEmpty()) {
768 if (!acqDataPacketVector.isEmpty()) {
728 dataSeries = acqDataPacketVector[0].m_DateSeries;
769 dataSeries = acqDataPacketVector[0].m_DateSeries;
729 for (int i = 1; i < acqDataPacketVector.size(); ++i) {
770 for (int i = 1; i < acqDataPacketVector.size(); ++i) {
730 dataSeries->merge(acqDataPacketVector[i].m_DateSeries.get());
771 dataSeries->merge(acqDataPacketVector[i].m_DateSeries.get());
731 }
772 }
732 }
773 }
733 qCDebug(LOG_VariableController()) << tr("TORM: retrieveDataSeries acqDataPacketVector size END")
774 qCDebug(LOG_VariableController()) << tr("TORM: retrieveDataSeries acqDataPacketVector size END")
734 << acqDataPacketVector.size();
775 << acqDataPacketVector.size();
735 return dataSeries;
776 return dataSeries;
736 }
777 }
737
778
738 void VariableController::VariableControllerPrivate::registerProvider(
779 void VariableController::VariableControllerPrivate::registerProvider(
739 std::shared_ptr<IDataProvider> provider)
780 std::shared_ptr<IDataProvider> provider)
740 {
781 {
741 if (m_ProviderSet.find(provider) == m_ProviderSet.end()) {
782 if (m_ProviderSet.find(provider) == m_ProviderSet.end()) {
742 qCDebug(LOG_VariableController()) << tr("Registering of a new provider")
783 qCDebug(LOG_VariableController()) << tr("Registering of a new provider")
743 << provider->objectName();
784 << provider->objectName();
744 m_ProviderSet.insert(provider);
785 m_ProviderSet.insert(provider);
745 connect(provider.get(), &IDataProvider::dataProvided, m_VariableAcquisitionWorker.get(),
786 connect(provider.get(), &IDataProvider::dataProvided, m_VariableAcquisitionWorker.get(),
746 &VariableAcquisitionWorker::onVariableDataAcquired);
787 &VariableAcquisitionWorker::onVariableDataAcquired);
747 connect(provider.get(), &IDataProvider::dataProvidedProgress,
788 connect(provider.get(), &IDataProvider::dataProvidedProgress,
748 m_VariableAcquisitionWorker.get(),
789 m_VariableAcquisitionWorker.get(),
749 &VariableAcquisitionWorker::onVariableRetrieveDataInProgress);
790 &VariableAcquisitionWorker::onVariableRetrieveDataInProgress);
750 connect(provider.get(), &IDataProvider::dataProvidedFailed,
791 connect(provider.get(), &IDataProvider::dataProvidedFailed,
751 m_VariableAcquisitionWorker.get(),
792 m_VariableAcquisitionWorker.get(),
752 &VariableAcquisitionWorker::onVariableAcquisitionFailed);
793 &VariableAcquisitionWorker::onVariableAcquisitionFailed);
753 }
794 }
754 else {
795 else {
755 qCDebug(LOG_VariableController()) << tr("Cannot register provider, it already exists ");
796 qCDebug(LOG_VariableController()) << tr("Cannot register provider, it already exists ");
756 }
797 }
757 }
798 }
758
799
759 QUuid VariableController::VariableControllerPrivate::acceptVariableRequest(
800 QUuid VariableController::VariableControllerPrivate::acceptVariableRequest(
760 QUuid varId, std::shared_ptr<IDataSeries> dataSeries)
801 QUuid varId, std::shared_ptr<IDataSeries> dataSeries)
761 {
802 {
762 auto itVarHandler = m_VarIdToVarRequestHandler.find(varId);
803 auto itVarHandler = m_VarIdToVarRequestHandler.find(varId);
763 if (itVarHandler == m_VarIdToVarRequestHandler.cend()) {
804 if (itVarHandler == m_VarIdToVarRequestHandler.cend()) {
764 return QUuid();
805 return QUuid();
765 }
806 }
766
807
767 auto varHandler = itVarHandler->second.get();
808 auto varHandler = itVarHandler->second.get();
768 if (varHandler->m_State == VariableRequestHandlerState::OFF) {
809 if (varHandler->m_State == VariableRequestHandlerState::OFF) {
769 qCCritical(LOG_VariableController())
810 qCCritical(LOG_VariableController())
770 << tr("acceptVariableRequest impossible on a variable with OFF state");
811 << tr("acceptVariableRequest impossible on a variable with OFF state");
771 }
812 }
772
813
773 varHandler->m_RunningVarRequest.m_DataSeries = dataSeries;
814 varHandler->m_RunningVarRequest.m_DataSeries = dataSeries;
774 varHandler->m_CanUpdate = true;
815 varHandler->m_CanUpdate = true;
775
816
776 // Element traitΓ©, on a dΓ©jΓ  toutes les donnΓ©es necessaires
817 // Element traitΓ©, on a dΓ©jΓ  toutes les donnΓ©es necessaires
777 auto varGroupId = varHandler->m_RunningVarRequest.m_VariableGroupId;
818 auto varGroupId = varHandler->m_RunningVarRequest.m_VariableGroupId;
778 qCDebug(LOG_VariableController()) << "Variable::acceptVariableRequest" << varGroupId
819 qCDebug(LOG_VariableController()) << "Variable::acceptVariableRequest" << varGroupId
779 << m_VarGroupIdToVarIds.size();
820 << m_VarGroupIdToVarIds.size();
780
821
781 return varHandler->m_RunningVarRequest.m_VariableGroupId;
822 return varHandler->m_RunningVarRequest.m_VariableGroupId;
782 }
823 }
783
824
784 void VariableController::VariableControllerPrivate::updateVariables(QUuid varRequestId)
825 void VariableController::VariableControllerPrivate::updateVariables(QUuid varRequestId)
785 {
826 {
786 qCDebug(LOG_VariableController()) << "VariableControllerPrivate::updateVariables"
827 qCDebug(LOG_VariableController()) << "VariableControllerPrivate::updateVariables"
787 << QThread::currentThread()->objectName() << varRequestId;
828 << QThread::currentThread()->objectName() << varRequestId;
788
829
789 auto varGroupIdToVarIdsIt = m_VarGroupIdToVarIds.find(varRequestId);
830 auto varGroupIdToVarIdsIt = m_VarGroupIdToVarIds.find(varRequestId);
790 if (varGroupIdToVarIdsIt == m_VarGroupIdToVarIds.end()) {
831 if (varGroupIdToVarIdsIt == m_VarGroupIdToVarIds.end()) {
791 qCWarning(LOG_VariableController())
832 qCWarning(LOG_VariableController())
792 << tr("Impossible to updateVariables of unknown variables") << varRequestId;
833 << tr("Impossible to updateVariables of unknown variables") << varRequestId;
793 return;
834 return;
794 }
835 }
795
836
796 auto &varIds = varGroupIdToVarIdsIt->second;
837 auto &varIds = varGroupIdToVarIdsIt->second;
797 auto varIdsEnd = varIds.end();
838 auto varIdsEnd = varIds.end();
798 bool processVariableUpdate = true;
839 bool processVariableUpdate = true;
799 qCDebug(LOG_VariableController()) << "VariableControllerPrivate::updateVariables"
840 qCDebug(LOG_VariableController()) << "VariableControllerPrivate::updateVariables"
800 << varRequestId << varIds.size();
841 << varRequestId << varIds.size();
801 for (auto varIdsIt = varIds.begin(); (varIdsIt != varIdsEnd) && processVariableUpdate;
842 for (auto varIdsIt = varIds.begin(); (varIdsIt != varIdsEnd) && processVariableUpdate;
802 ++varIdsIt) {
843 ++varIdsIt) {
803 auto itVarHandler = m_VarIdToVarRequestHandler.find(*varIdsIt);
844 auto itVarHandler = m_VarIdToVarRequestHandler.find(*varIdsIt);
804 if (itVarHandler != m_VarIdToVarRequestHandler.cend()) {
845 if (itVarHandler != m_VarIdToVarRequestHandler.cend()) {
805 processVariableUpdate &= itVarHandler->second->m_CanUpdate;
846 processVariableUpdate &= itVarHandler->second->m_CanUpdate;
806 }
847 }
807 }
848 }
808
849
809 if (processVariableUpdate) {
850 if (processVariableUpdate) {
810 qCDebug(LOG_VariableController()) << "Final update OK for the var request" << varIds.size();
851 qCDebug(LOG_VariableController()) << "Final update OK for the var request" << varIds.size();
811 for (auto varIdsIt = varIds.begin(); varIdsIt != varIdsEnd; ++varIdsIt) {
852 for (auto varIdsIt = varIds.begin(); varIdsIt != varIdsEnd; ++varIdsIt) {
812 auto itVarHandler = m_VarIdToVarRequestHandler.find(*varIdsIt);
853 auto itVarHandler = m_VarIdToVarRequestHandler.find(*varIdsIt);
813 if (itVarHandler != m_VarIdToVarRequestHandler.cend()) {
854 if (itVarHandler != m_VarIdToVarRequestHandler.cend()) {
814 if (auto var = findVariable(*varIdsIt)) {
855 if (auto var = findVariable(*varIdsIt)) {
815 auto &varRequest = itVarHandler->second->m_RunningVarRequest;
856 auto &varRequest = itVarHandler->second->m_RunningVarRequest;
816 var->setRange(varRequest.m_RangeRequested);
857 var->setRange(varRequest.m_RangeRequested);
817 var->setCacheRange(varRequest.m_CacheRangeRequested);
858 var->setCacheRange(varRequest.m_CacheRangeRequested);
818 qCDebug(LOG_VariableController()) << tr("1: onDataProvided")
859 qCDebug(LOG_VariableController()) << tr("1: onDataProvided")
819 << varRequest.m_RangeRequested
860 << varRequest.m_RangeRequested
820 << varRequest.m_CacheRangeRequested;
861 << varRequest.m_CacheRangeRequested;
821 qCDebug(LOG_VariableController()) << tr("2: onDataProvided var points before")
862 qCDebug(LOG_VariableController()) << tr("2: onDataProvided var points before")
822 << var->nbPoints()
863 << var->nbPoints()
823 << varRequest.m_DataSeries->nbPoints();
864 << varRequest.m_DataSeries->nbPoints();
824 var->mergeDataSeries(varRequest.m_DataSeries);
865 var->mergeDataSeries(varRequest.m_DataSeries);
825 qCDebug(LOG_VariableController()) << tr("3: onDataProvided var points after")
866 qCDebug(LOG_VariableController()) << tr("3: onDataProvided var points after")
826 << var->nbPoints();
867 << var->nbPoints();
827
868
828 emit var->updated();
869 emit var->updated();
829 qCDebug(LOG_VariableController()) << tr("Update OK");
870 qCDebug(LOG_VariableController()) << tr("Update OK");
830 }
871 }
831 else {
872 else {
832 qCCritical(LOG_VariableController())
873 qCCritical(LOG_VariableController())
833 << tr("Impossible to update data to a null variable");
874 << tr("Impossible to update data to a null variable");
834 }
875 }
835 }
876 }
836 }
877 }
837 updateVariableRequest(varRequestId);
878 updateVariableRequest(varRequestId);
838
879
839 // cleaning varRequestId
880 // cleaning varRequestId
840 qCDebug(LOG_VariableController()) << tr("m_VarGroupIdToVarIds erase") << varRequestId;
881 qCDebug(LOG_VariableController()) << tr("m_VarGroupIdToVarIds erase") << varRequestId;
841 m_VarGroupIdToVarIds.erase(varRequestId);
882 m_VarGroupIdToVarIds.erase(varRequestId);
842 }
883 }
843 }
884 }
844
885
845
886
846 void VariableController::VariableControllerPrivate::updateVariableRequest(QUuid varRequestId)
887 void VariableController::VariableControllerPrivate::updateVariableRequest(QUuid varRequestId)
847 {
888 {
848 auto varGroupIdToVarIdsIt = m_VarGroupIdToVarIds.find(varRequestId);
889 auto varGroupIdToVarIdsIt = m_VarGroupIdToVarIds.find(varRequestId);
849 if (varGroupIdToVarIdsIt == m_VarGroupIdToVarIds.end()) {
890 if (varGroupIdToVarIdsIt == m_VarGroupIdToVarIds.end()) {
850 qCCritical(LOG_VariableController()) << QObject::tr(
891 qCCritical(LOG_VariableController()) << QObject::tr(
851 "Impossible to updateVariableRequest since varGroupdId isn't here anymore");
892 "Impossible to updateVariableRequest since varGroupdId isn't here anymore");
852
893
853 return;
894 return;
854 }
895 }
855
896
856 auto &varIds = varGroupIdToVarIdsIt->second;
897 auto &varIds = varGroupIdToVarIdsIt->second;
857 auto varIdsEnd = varIds.end();
898 auto varIdsEnd = varIds.end();
858 for (auto varIdsIt = varIds.begin(); (varIdsIt != varIdsEnd); ++varIdsIt) {
899 for (auto varIdsIt = varIds.begin(); (varIdsIt != varIdsEnd); ++varIdsIt) {
859 auto itVarHandler = m_VarIdToVarRequestHandler.find(*varIdsIt);
900 auto itVarHandler = m_VarIdToVarRequestHandler.find(*varIdsIt);
860 if (itVarHandler != m_VarIdToVarRequestHandler.cend()) {
901 if (itVarHandler != m_VarIdToVarRequestHandler.cend()) {
861
902
862 auto varHandler = itVarHandler->second.get();
903 auto varHandler = itVarHandler->second.get();
863 varHandler->m_CanUpdate = false;
904 varHandler->m_CanUpdate = false;
864
905
865
906
866 switch (varHandler->m_State) {
907 switch (varHandler->m_State) {
867 case VariableRequestHandlerState::OFF: {
908 case VariableRequestHandlerState::OFF: {
868 qCCritical(LOG_VariableController())
909 qCCritical(LOG_VariableController())
869 << QObject::tr("Impossible to update a variable with handler in OFF state");
910 << QObject::tr("Impossible to update a variable with handler in OFF state");
870 } break;
911 } break;
871 case VariableRequestHandlerState::RUNNING: {
912 case VariableRequestHandlerState::RUNNING: {
872 varHandler->m_State = VariableRequestHandlerState::OFF;
913 varHandler->m_State = VariableRequestHandlerState::OFF;
873 varHandler->m_RunningVarRequest = VariableRequest{};
914 varHandler->m_RunningVarRequest = VariableRequest{};
874 break;
915 break;
875 }
916 }
876 case VariableRequestHandlerState::PENDING: {
917 case VariableRequestHandlerState::PENDING: {
877 varHandler->m_State = VariableRequestHandlerState::RUNNING;
918 varHandler->m_State = VariableRequestHandlerState::RUNNING;
878 varHandler->m_RunningVarRequest = varHandler->m_PendingVarRequest;
919 varHandler->m_RunningVarRequest = varHandler->m_PendingVarRequest;
879 varHandler->m_PendingVarRequest = VariableRequest{};
920 varHandler->m_PendingVarRequest = VariableRequest{};
880 auto var = findVariable(itVarHandler->first);
921 auto var = findVariable(itVarHandler->first);
881 executeVarRequest(var, varHandler->m_RunningVarRequest);
922 executeVarRequest(var, varHandler->m_RunningVarRequest);
882 break;
923 break;
883 }
924 }
884 default:
925 default:
885 qCCritical(LOG_VariableController())
926 qCCritical(LOG_VariableController())
886 << QObject::tr("Unknown VariableRequestHandlerState");
927 << QObject::tr("Unknown VariableRequestHandlerState");
887 }
928 }
888 }
929 }
889 }
930 }
890 }
931 }
891
932
892
933
893 void VariableController::VariableControllerPrivate::cancelVariableRequest(QUuid varRequestId)
934 void VariableController::VariableControllerPrivate::cancelVariableRequest(QUuid varRequestId)
894 {
935 {
895 qCDebug(LOG_VariableController()) << tr("cancelVariableRequest") << varRequestId;
936 qCDebug(LOG_VariableController()) << tr("cancelVariableRequest") << varRequestId;
896
937
897 auto varGroupIdToVarIdsIt = m_VarGroupIdToVarIds.find(varRequestId);
938 auto varGroupIdToVarIdsIt = m_VarGroupIdToVarIds.find(varRequestId);
898 if (varGroupIdToVarIdsIt == m_VarGroupIdToVarIds.end()) {
939 if (varGroupIdToVarIdsIt == m_VarGroupIdToVarIds.end()) {
899 qCCritical(LOG_VariableController())
940 qCCritical(LOG_VariableController())
900 << tr("Impossible to cancelVariableRequest for unknown varGroupdId") << varRequestId;
941 << tr("Impossible to cancelVariableRequest for unknown varGroupdId") << varRequestId;
901 return;
942 return;
902 }
943 }
903
944
904 auto &varIds = varGroupIdToVarIdsIt->second;
945 auto &varIds = varGroupIdToVarIdsIt->second;
905 auto varIdsEnd = varIds.end();
946 auto varIdsEnd = varIds.end();
906 for (auto varIdsIt = varIds.begin(); (varIdsIt != varIdsEnd); ++varIdsIt) {
947 for (auto varIdsIt = varIds.begin(); (varIdsIt != varIdsEnd); ++varIdsIt) {
907 auto itVarHandler = m_VarIdToVarRequestHandler.find(*varIdsIt);
948 auto itVarHandler = m_VarIdToVarRequestHandler.find(*varIdsIt);
908 if (itVarHandler != m_VarIdToVarRequestHandler.cend()) {
949 if (itVarHandler != m_VarIdToVarRequestHandler.cend()) {
909
950
910 auto varHandler = itVarHandler->second.get();
951 auto varHandler = itVarHandler->second.get();
911 varHandler->m_VarId = QUuid{};
952 varHandler->m_VarId = QUuid{};
912 switch (varHandler->m_State) {
953 switch (varHandler->m_State) {
913 case VariableRequestHandlerState::OFF: {
954 case VariableRequestHandlerState::OFF: {
914 qCWarning(LOG_VariableController())
955 qCWarning(LOG_VariableController())
915 << QObject::tr("Impossible to cancel a variable with no running request");
956 << QObject::tr("Impossible to cancel a variable with no running request");
916 break;
957 break;
917 }
958 }
918 case VariableRequestHandlerState::RUNNING: {
959 case VariableRequestHandlerState::RUNNING: {
919
960
920 if (varHandler->m_RunningVarRequest.m_VariableGroupId == varRequestId) {
961 if (varHandler->m_RunningVarRequest.m_VariableGroupId == varRequestId) {
921 auto var = findVariable(itVarHandler->first);
962 auto var = findVariable(itVarHandler->first);
922 auto varProvider = m_VariableToProviderMap.at(var);
963 auto varProvider = m_VariableToProviderMap.at(var);
923 if (varProvider != nullptr) {
964 if (varProvider != nullptr) {
924 m_VariableAcquisitionWorker->abortProgressRequested(
965 m_VariableAcquisitionWorker->abortProgressRequested(
925 itVarHandler->first);
966 itVarHandler->first);
926 }
967 }
927 m_VariableModel->setDataProgress(var, 0.0);
968 m_VariableModel->setDataProgress(var, 0.0);
928 varHandler->m_CanUpdate = false;
969 varHandler->m_CanUpdate = false;
929 varHandler->m_State = VariableRequestHandlerState::OFF;
970 varHandler->m_State = VariableRequestHandlerState::OFF;
930 varHandler->m_RunningVarRequest = VariableRequest{};
971 varHandler->m_RunningVarRequest = VariableRequest{};
931 }
972 }
932 else {
973 else {
933 // TODO: log Impossible to cancel the running variable request beacause its
974 // TODO: log Impossible to cancel the running variable request beacause its
934 // varRequestId isn't not the canceled one
975 // varRequestId isn't not the canceled one
935 }
976 }
936 break;
977 break;
937 }
978 }
938 case VariableRequestHandlerState::PENDING: {
979 case VariableRequestHandlerState::PENDING: {
939 if (varHandler->m_RunningVarRequest.m_VariableGroupId == varRequestId) {
980 if (varHandler->m_RunningVarRequest.m_VariableGroupId == varRequestId) {
940 auto var = findVariable(itVarHandler->first);
981 auto var = findVariable(itVarHandler->first);
941 auto varProvider = m_VariableToProviderMap.at(var);
982 auto varProvider = m_VariableToProviderMap.at(var);
942 if (varProvider != nullptr) {
983 if (varProvider != nullptr) {
943 m_VariableAcquisitionWorker->abortProgressRequested(
984 m_VariableAcquisitionWorker->abortProgressRequested(
944 itVarHandler->first);
985 itVarHandler->first);
945 }
986 }
946 m_VariableModel->setDataProgress(var, 0.0);
987 m_VariableModel->setDataProgress(var, 0.0);
947 varHandler->m_CanUpdate = false;
988 varHandler->m_CanUpdate = false;
948 varHandler->m_State = VariableRequestHandlerState::RUNNING;
989 varHandler->m_State = VariableRequestHandlerState::RUNNING;
949 varHandler->m_RunningVarRequest = varHandler->m_PendingVarRequest;
990 varHandler->m_RunningVarRequest = varHandler->m_PendingVarRequest;
950 varHandler->m_PendingVarRequest = VariableRequest{};
991 varHandler->m_PendingVarRequest = VariableRequest{};
951 executeVarRequest(var, varHandler->m_RunningVarRequest);
992 executeVarRequest(var, varHandler->m_RunningVarRequest);
952 }
993 }
953 else if (varHandler->m_PendingVarRequest.m_VariableGroupId == varRequestId) {
994 else if (varHandler->m_PendingVarRequest.m_VariableGroupId == varRequestId) {
954 varHandler->m_State = VariableRequestHandlerState::RUNNING;
995 varHandler->m_State = VariableRequestHandlerState::RUNNING;
955 varHandler->m_PendingVarRequest = VariableRequest{};
996 varHandler->m_PendingVarRequest = VariableRequest{};
956 }
997 }
957 else {
998 else {
958 // TODO: log Impossible to cancel the variable request beacause its
999 // TODO: log Impossible to cancel the variable request beacause its
959 // varRequestId isn't not the canceled one
1000 // varRequestId isn't not the canceled one
960 }
1001 }
961 break;
1002 break;
962 }
1003 }
963 default:
1004 default:
964 qCCritical(LOG_VariableController())
1005 qCCritical(LOG_VariableController())
965 << QObject::tr("Unknown VariableRequestHandlerState");
1006 << QObject::tr("Unknown VariableRequestHandlerState");
966 }
1007 }
967 }
1008 }
968 }
1009 }
969 qCDebug(LOG_VariableController()) << tr("cancelVariableRequest: erase") << varRequestId;
1010 qCDebug(LOG_VariableController()) << tr("cancelVariableRequest: erase") << varRequestId;
970 m_VarGroupIdToVarIds.erase(varRequestId);
1011 m_VarGroupIdToVarIds.erase(varRequestId);
971 }
1012 }
972
1013
973 void VariableController::VariableControllerPrivate::executeVarRequest(std::shared_ptr<Variable> var,
1014 void VariableController::VariableControllerPrivate::executeVarRequest(std::shared_ptr<Variable> var,
974 VariableRequest &varRequest)
1015 VariableRequest &varRequest)
975 {
1016 {
976 qCDebug(LOG_VariableController()) << tr("TORM: executeVarRequest");
1017 qCDebug(LOG_VariableController()) << tr("TORM: executeVarRequest");
977
1018
978 auto varId = m_VariableToIdentifierMap.at(var);
1019 auto varId = m_VariableToIdentifierMap.at(var);
979
1020
980 auto varCacheRange = var->cacheRange();
1021 auto varCacheRange = var->cacheRange();
981 auto varCacheRangeRequested = varRequest.m_CacheRangeRequested;
1022 auto varCacheRangeRequested = varRequest.m_CacheRangeRequested;
982 auto notInCacheRangeList
1023 auto notInCacheRangeList
983 = Variable::provideNotInCacheRangeList(varCacheRange, varCacheRangeRequested);
1024 = Variable::provideNotInCacheRangeList(varCacheRange, varCacheRangeRequested);
984 auto inCacheRangeList
1025 auto inCacheRangeList
985 = Variable::provideInCacheRangeList(varCacheRange, varCacheRangeRequested);
1026 = Variable::provideInCacheRangeList(varCacheRange, varCacheRangeRequested);
986
1027
987 if (!notInCacheRangeList.empty()) {
1028 if (!notInCacheRangeList.empty()) {
988
1029
989 auto varProvider = m_VariableToProviderMap.at(var);
1030 auto varProvider = m_VariableToProviderMap.at(var);
990 if (varProvider != nullptr) {
1031 if (varProvider != nullptr) {
991 qCDebug(LOG_VariableController()) << "executeVarRequest " << varRequest.m_RangeRequested
1032 qCDebug(LOG_VariableController()) << "executeVarRequest " << varRequest.m_RangeRequested
992 << varRequest.m_CacheRangeRequested;
1033 << varRequest.m_CacheRangeRequested;
993 m_VariableAcquisitionWorker->pushVariableRequest(
1034 m_VariableAcquisitionWorker->pushVariableRequest(
994 varRequest.m_VariableGroupId, varId, varRequest.m_RangeRequested,
1035 varRequest.m_VariableGroupId, varId, varRequest.m_RangeRequested,
995 varRequest.m_CacheRangeRequested,
1036 varRequest.m_CacheRangeRequested,
996 DataProviderParameters{std::move(notInCacheRangeList), var->metadata()},
1037 DataProviderParameters{std::move(notInCacheRangeList), var->metadata()},
997 varProvider);
1038 varProvider);
998 }
1039 }
999 else {
1040 else {
1000 qCCritical(LOG_VariableController())
1041 qCCritical(LOG_VariableController())
1001 << "Impossible to provide data with a null provider";
1042 << "Impossible to provide data with a null provider";
1002 }
1043 }
1003
1044
1004 if (!inCacheRangeList.empty()) {
1045 if (!inCacheRangeList.empty()) {
1005 emit q->updateVarDisplaying(var, inCacheRangeList.first());
1046 emit q->updateVarDisplaying(var, inCacheRangeList.first());
1006 }
1047 }
1007 }
1048 }
1008 else {
1049 else {
1009 acceptVariableRequest(varId,
1050 acceptVariableRequest(varId,
1010 var->dataSeries()->subDataSeries(varRequest.m_CacheRangeRequested));
1051 var->dataSeries()->subDataSeries(varRequest.m_CacheRangeRequested));
1011 }
1052 }
1012 }
1053 }
@@ -1,293 +1,349
1 #include <Variable/Variable.h>
1 #include <Variable/Variable.h>
2 #include <Variable/VariableController.h>
2 #include <Variable/VariableModel.h>
3 #include <Variable/VariableModel.h>
3
4
4 #include <Common/DateUtils.h>
5 #include <Common/DateUtils.h>
6 #include <Common/MimeTypesDef.h>
5 #include <Common/StringUtils.h>
7 #include <Common/StringUtils.h>
6
8
7 #include <Data/IDataSeries.h>
9 #include <Data/IDataSeries.h>
8
10
11 #include <QMimeData>
9 #include <QSize>
12 #include <QSize>
10 #include <unordered_map>
13 #include <unordered_map>
11
14
12 Q_LOGGING_CATEGORY(LOG_VariableModel, "VariableModel")
15 Q_LOGGING_CATEGORY(LOG_VariableModel, "VariableModel")
13
16
14 namespace {
17 namespace {
15
18
16 // Column indexes
19 // Column indexes
17 const auto NAME_COLUMN = 0;
20 const auto NAME_COLUMN = 0;
18 const auto TSTART_COLUMN = 1;
21 const auto TSTART_COLUMN = 1;
19 const auto TEND_COLUMN = 2;
22 const auto TEND_COLUMN = 2;
20 const auto NBPOINTS_COLUMN = 3;
23 const auto NBPOINTS_COLUMN = 3;
21 const auto UNIT_COLUMN = 4;
24 const auto UNIT_COLUMN = 4;
22 const auto MISSION_COLUMN = 5;
25 const auto MISSION_COLUMN = 5;
23 const auto PLUGIN_COLUMN = 6;
26 const auto PLUGIN_COLUMN = 6;
24 const auto NB_COLUMNS = 7;
27 const auto NB_COLUMNS = 7;
25
28
26 // Column properties
29 // Column properties
27 const auto DEFAULT_HEIGHT = 25;
30 const auto DEFAULT_HEIGHT = 25;
28 const auto DEFAULT_WIDTH = 100;
31 const auto DEFAULT_WIDTH = 100;
29
32
30 struct ColumnProperties {
33 struct ColumnProperties {
31 ColumnProperties(const QString &name = {}, int width = DEFAULT_WIDTH,
34 ColumnProperties(const QString &name = {}, int width = DEFAULT_WIDTH,
32 int height = DEFAULT_HEIGHT)
35 int height = DEFAULT_HEIGHT)
33 : m_Name{name}, m_Width{width}, m_Height{height}
36 : m_Name{name}, m_Width{width}, m_Height{height}
34 {
37 {
35 }
38 }
36
39
37 QString m_Name;
40 QString m_Name;
38 int m_Width;
41 int m_Width;
39 int m_Height;
42 int m_Height;
40 };
43 };
41
44
42 const auto COLUMN_PROPERTIES = QHash<int, ColumnProperties>{
45 const auto COLUMN_PROPERTIES = QHash<int, ColumnProperties>{
43 {NAME_COLUMN, {QObject::tr("Name")}}, {TSTART_COLUMN, {QObject::tr("tStart"), 180}},
46 {NAME_COLUMN, {QObject::tr("Name")}}, {TSTART_COLUMN, {QObject::tr("tStart"), 180}},
44 {TEND_COLUMN, {QObject::tr("tEnd"), 180}}, {NBPOINTS_COLUMN, {QObject::tr("Nb points")}},
47 {TEND_COLUMN, {QObject::tr("tEnd"), 180}}, {NBPOINTS_COLUMN, {QObject::tr("Nb points")}},
45 {UNIT_COLUMN, {QObject::tr("Unit")}}, {MISSION_COLUMN, {QObject::tr("Mission")}},
48 {UNIT_COLUMN, {QObject::tr("Unit")}}, {MISSION_COLUMN, {QObject::tr("Mission")}},
46 {PLUGIN_COLUMN, {QObject::tr("Plugin")}}};
49 {PLUGIN_COLUMN, {QObject::tr("Plugin")}}};
47
50
48 /// Format for datetimes
51 /// Format for datetimes
49 const auto DATETIME_FORMAT = QStringLiteral("dd/MM/yyyy \nhh:mm:ss:zzz");
52 const auto DATETIME_FORMAT = QStringLiteral("dd/MM/yyyy \nhh:mm:ss:zzz");
50
53
51 QString uniqueName(const QString &defaultName,
54 QString uniqueName(const QString &defaultName,
52 const std::vector<std::shared_ptr<Variable> > &variables)
55 const std::vector<std::shared_ptr<Variable> > &variables)
53 {
56 {
54 auto forbiddenNames = std::vector<QString>(variables.size());
57 auto forbiddenNames = std::vector<QString>(variables.size());
55 std::transform(variables.cbegin(), variables.cend(), forbiddenNames.begin(),
58 std::transform(variables.cbegin(), variables.cend(), forbiddenNames.begin(),
56 [](const auto &variable) { return variable->name(); });
59 [](const auto &variable) { return variable->name(); });
57 auto uniqueName = StringUtils::uniqueName(defaultName, forbiddenNames);
60 auto uniqueName = StringUtils::uniqueName(defaultName, forbiddenNames);
58 Q_ASSERT(!uniqueName.isEmpty());
61 Q_ASSERT(!uniqueName.isEmpty());
59
62
60 return uniqueName;
63 return uniqueName;
61 }
64 }
62
65
63 } // namespace
66 } // namespace
64
67
65 struct VariableModel::VariableModelPrivate {
68 struct VariableModel::VariableModelPrivate {
66 /// Variables created in SciQlop
69 /// Variables created in SciQlop
67 std::vector<std::shared_ptr<Variable> > m_Variables;
70 std::vector<std::shared_ptr<Variable> > m_Variables;
68 std::unordered_map<std::shared_ptr<Variable>, double> m_VariableToProgress;
71 std::unordered_map<std::shared_ptr<Variable>, double> m_VariableToProgress;
72 VariableController *m_VariableController;
69
73
70 /// Return the row index of the variable. -1 if it's not found
74 /// Return the row index of the variable. -1 if it's not found
71 int indexOfVariable(Variable *variable) const noexcept;
75 int indexOfVariable(Variable *variable) const noexcept;
72 };
76 };
73
77
74 VariableModel::VariableModel(QObject *parent)
78 VariableModel::VariableModel(VariableController *parent)
75 : QAbstractTableModel{parent}, impl{spimpl::make_unique_impl<VariableModelPrivate>()}
79 : QAbstractTableModel{parent}, impl{spimpl::make_unique_impl<VariableModelPrivate>()}
76 {
80 {
81 impl->m_VariableController = parent;
77 }
82 }
78
83
79 void VariableModel::addVariable(std::shared_ptr<Variable> variable) noexcept
84 void VariableModel::addVariable(std::shared_ptr<Variable> variable) noexcept
80 {
85 {
81 auto insertIndex = rowCount();
86 auto insertIndex = rowCount();
82 beginInsertRows({}, insertIndex, insertIndex);
87 beginInsertRows({}, insertIndex, insertIndex);
83
88
84 // Generates unique name for the variable
89 // Generates unique name for the variable
85 variable->setName(uniqueName(variable->name(), impl->m_Variables));
90 variable->setName(uniqueName(variable->name(), impl->m_Variables));
86
91
87 impl->m_Variables.push_back(variable);
92 impl->m_Variables.push_back(variable);
88 connect(variable.get(), &Variable::updated, this, &VariableModel::onVariableUpdated);
93 connect(variable.get(), &Variable::updated, this, &VariableModel::onVariableUpdated);
89
94
90 endInsertRows();
95 endInsertRows();
91 }
96 }
92
97
93 bool VariableModel::containsVariable(std::shared_ptr<Variable> variable) const noexcept
98 bool VariableModel::containsVariable(std::shared_ptr<Variable> variable) const noexcept
94 {
99 {
95 auto end = impl->m_Variables.cend();
100 auto end = impl->m_Variables.cend();
96 return std::find(impl->m_Variables.cbegin(), end, variable) != end;
101 return std::find(impl->m_Variables.cbegin(), end, variable) != end;
97 }
102 }
98
103
99 std::shared_ptr<Variable> VariableModel::createVariable(const QString &name,
104 std::shared_ptr<Variable> VariableModel::createVariable(const QString &name,
100 const QVariantHash &metadata) noexcept
105 const QVariantHash &metadata) noexcept
101 {
106 {
102 auto variable = std::make_shared<Variable>(name, metadata);
107 auto variable = std::make_shared<Variable>(name, metadata);
103 addVariable(variable);
108 addVariable(variable);
104
109
105 return variable;
110 return variable;
106 }
111 }
107
112
108 void VariableModel::deleteVariable(std::shared_ptr<Variable> variable) noexcept
113 void VariableModel::deleteVariable(std::shared_ptr<Variable> variable) noexcept
109 {
114 {
110 if (!variable) {
115 if (!variable) {
111 qCCritical(LOG_Variable()) << "Can't delete a null variable from the model";
116 qCCritical(LOG_Variable()) << "Can't delete a null variable from the model";
112 return;
117 return;
113 }
118 }
114
119
115 // Finds variable in the model
120 // Finds variable in the model
116 auto begin = impl->m_Variables.cbegin();
121 auto begin = impl->m_Variables.cbegin();
117 auto end = impl->m_Variables.cend();
122 auto end = impl->m_Variables.cend();
118 auto it = std::find(begin, end, variable);
123 auto it = std::find(begin, end, variable);
119 if (it != end) {
124 if (it != end) {
120 auto removeIndex = std::distance(begin, it);
125 auto removeIndex = std::distance(begin, it);
121
126
122 // Deletes variable
127 // Deletes variable
123 beginRemoveRows({}, removeIndex, removeIndex);
128 beginRemoveRows({}, removeIndex, removeIndex);
124 impl->m_Variables.erase(it);
129 impl->m_Variables.erase(it);
125 endRemoveRows();
130 endRemoveRows();
126 }
131 }
127 else {
132 else {
128 qCritical(LOG_VariableModel())
133 qCritical(LOG_VariableModel())
129 << tr("Can't delete variable %1 from the model: the variable is not in the model")
134 << tr("Can't delete variable %1 from the model: the variable is not in the model")
130 .arg(variable->name());
135 .arg(variable->name());
131 }
136 }
132
137
133 // Removes variable from progress map
138 // Removes variable from progress map
134 impl->m_VariableToProgress.erase(variable);
139 impl->m_VariableToProgress.erase(variable);
135 }
140 }
136
141
137
142
138 std::shared_ptr<Variable> VariableModel::variable(int index) const
143 std::shared_ptr<Variable> VariableModel::variable(int index) const
139 {
144 {
140 return (index >= 0 && index < impl->m_Variables.size()) ? impl->m_Variables[index] : nullptr;
145 return (index >= 0 && index < impl->m_Variables.size()) ? impl->m_Variables[index] : nullptr;
141 }
146 }
142
147
143 std::vector<std::shared_ptr<Variable> > VariableModel::variables() const
148 std::vector<std::shared_ptr<Variable> > VariableModel::variables() const
144 {
149 {
145 return impl->m_Variables;
150 return impl->m_Variables;
146 }
151 }
147
152
148 void VariableModel::setDataProgress(std::shared_ptr<Variable> variable, double progress)
153 void VariableModel::setDataProgress(std::shared_ptr<Variable> variable, double progress)
149 {
154 {
150 if (progress > 0.0) {
155 if (progress > 0.0) {
151 impl->m_VariableToProgress[variable] = progress;
156 impl->m_VariableToProgress[variable] = progress;
152 }
157 }
153 else {
158 else {
154 impl->m_VariableToProgress.erase(variable);
159 impl->m_VariableToProgress.erase(variable);
155 }
160 }
156 auto modelIndex = createIndex(impl->indexOfVariable(variable.get()), NAME_COLUMN);
161 auto modelIndex = createIndex(impl->indexOfVariable(variable.get()), NAME_COLUMN);
157
162
158 emit dataChanged(modelIndex, modelIndex);
163 emit dataChanged(modelIndex, modelIndex);
159 }
164 }
160
165
161 int VariableModel::columnCount(const QModelIndex &parent) const
166 int VariableModel::columnCount(const QModelIndex &parent) const
162 {
167 {
163 Q_UNUSED(parent);
168 Q_UNUSED(parent);
164
169
165 return NB_COLUMNS;
170 return NB_COLUMNS;
166 }
171 }
167
172
168 int VariableModel::rowCount(const QModelIndex &parent) const
173 int VariableModel::rowCount(const QModelIndex &parent) const
169 {
174 {
170 Q_UNUSED(parent);
175 Q_UNUSED(parent);
171
176
172 return impl->m_Variables.size();
177 return impl->m_Variables.size();
173 }
178 }
174
179
175 QVariant VariableModel::data(const QModelIndex &index, int role) const
180 QVariant VariableModel::data(const QModelIndex &index, int role) const
176 {
181 {
177 if (!index.isValid()) {
182 if (!index.isValid()) {
178 return QVariant{};
183 return QVariant{};
179 }
184 }
180
185
181 if (index.row() < 0 || index.row() >= rowCount()) {
186 if (index.row() < 0 || index.row() >= rowCount()) {
182 return QVariant{};
187 return QVariant{};
183 }
188 }
184
189
185 if (role == Qt::DisplayRole) {
190 if (role == Qt::DisplayRole) {
186 if (auto variable = impl->m_Variables.at(index.row()).get()) {
191 if (auto variable = impl->m_Variables.at(index.row()).get()) {
187 switch (index.column()) {
192 switch (index.column()) {
188 case NAME_COLUMN:
193 case NAME_COLUMN:
189 return variable->name();
194 return variable->name();
190 case TSTART_COLUMN: {
195 case TSTART_COLUMN: {
191 auto range = variable->realRange();
196 auto range = variable->realRange();
192 return range != INVALID_RANGE
197 return range != INVALID_RANGE
193 ? DateUtils::dateTime(range.m_TStart).toString(DATETIME_FORMAT)
198 ? DateUtils::dateTime(range.m_TStart).toString(DATETIME_FORMAT)
194 : QVariant{};
199 : QVariant{};
195 }
200 }
196 case TEND_COLUMN: {
201 case TEND_COLUMN: {
197 auto range = variable->realRange();
202 auto range = variable->realRange();
198 return range != INVALID_RANGE
203 return range != INVALID_RANGE
199 ? DateUtils::dateTime(range.m_TEnd).toString(DATETIME_FORMAT)
204 ? DateUtils::dateTime(range.m_TEnd).toString(DATETIME_FORMAT)
200 : QVariant{};
205 : QVariant{};
201 }
206 }
202 case NBPOINTS_COLUMN:
207 case NBPOINTS_COLUMN:
203 return variable->nbPoints();
208 return variable->nbPoints();
204 case UNIT_COLUMN:
209 case UNIT_COLUMN:
205 return variable->metadata().value(QStringLiteral("units"));
210 return variable->metadata().value(QStringLiteral("units"));
206 case MISSION_COLUMN:
211 case MISSION_COLUMN:
207 return variable->metadata().value(QStringLiteral("mission"));
212 return variable->metadata().value(QStringLiteral("mission"));
208 case PLUGIN_COLUMN:
213 case PLUGIN_COLUMN:
209 return variable->metadata().value(QStringLiteral("plugin"));
214 return variable->metadata().value(QStringLiteral("plugin"));
210 default:
215 default:
211 // No action
216 // No action
212 break;
217 break;
213 }
218 }
214
219
215 qWarning(LOG_VariableModel())
220 qWarning(LOG_VariableModel())
216 << tr("Can't get data (unknown column %1)").arg(index.column());
221 << tr("Can't get data (unknown column %1)").arg(index.column());
217 }
222 }
218 else {
223 else {
219 qWarning(LOG_VariableModel()) << tr("Can't get data (no variable)");
224 qWarning(LOG_VariableModel()) << tr("Can't get data (no variable)");
220 }
225 }
221 }
226 }
222 else if (role == VariableRoles::ProgressRole) {
227 else if (role == VariableRoles::ProgressRole) {
223 if (auto variable = impl->m_Variables.at(index.row())) {
228 if (auto variable = impl->m_Variables.at(index.row())) {
224
229
225 auto it = impl->m_VariableToProgress.find(variable);
230 auto it = impl->m_VariableToProgress.find(variable);
226 if (it != impl->m_VariableToProgress.cend()) {
231 if (it != impl->m_VariableToProgress.cend()) {
227 return it->second;
232 return it->second;
228 }
233 }
229 }
234 }
230 }
235 }
231
236
232 return QVariant{};
237 return QVariant{};
233 }
238 }
234
239
235 QVariant VariableModel::headerData(int section, Qt::Orientation orientation, int role) const
240 QVariant VariableModel::headerData(int section, Qt::Orientation orientation, int role) const
236 {
241 {
237 if (role != Qt::DisplayRole && role != Qt::SizeHintRole) {
242 if (role != Qt::DisplayRole && role != Qt::SizeHintRole) {
238 return QVariant{};
243 return QVariant{};
239 }
244 }
240
245
241 if (orientation == Qt::Horizontal) {
246 if (orientation == Qt::Horizontal) {
242 auto propertiesIt = COLUMN_PROPERTIES.find(section);
247 auto propertiesIt = COLUMN_PROPERTIES.find(section);
243 if (propertiesIt != COLUMN_PROPERTIES.cend()) {
248 if (propertiesIt != COLUMN_PROPERTIES.cend()) {
244 // Role is either DisplayRole or SizeHintRole
249 // Role is either DisplayRole or SizeHintRole
245 return (role == Qt::DisplayRole)
250 return (role == Qt::DisplayRole)
246 ? QVariant{propertiesIt->m_Name}
251 ? QVariant{propertiesIt->m_Name}
247 : QVariant{QSize{propertiesIt->m_Width, propertiesIt->m_Height}};
252 : QVariant{QSize{propertiesIt->m_Width, propertiesIt->m_Height}};
248 }
253 }
249 else {
254 else {
250 qWarning(LOG_VariableModel())
255 qWarning(LOG_VariableModel())
251 << tr("Can't get header data (unknown column %1)").arg(section);
256 << tr("Can't get header data (unknown column %1)").arg(section);
252 }
257 }
253 }
258 }
254
259
255 return QVariant{};
260 return QVariant{};
256 }
261 }
257
262
263 Qt::ItemFlags VariableModel::flags(const QModelIndex &index) const
264 {
265 return QAbstractTableModel::flags(index) | Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled;
266 }
267
268 Qt::DropActions VariableModel::supportedDropActions() const
269 {
270 return Qt::MoveAction;
271 }
272
273 Qt::DropActions VariableModel::supportedDragActions() const
274 {
275 return Qt::CopyAction | Qt::MoveAction;
276 }
277
278 QStringList VariableModel::mimeTypes() const
279 {
280 return {MIME_TYPE_VARIABLE_LIST};
281 }
282
283 QMimeData *VariableModel::mimeData(const QModelIndexList &indexes) const
284 {
285 auto mimeData = new QMimeData;
286
287 QList<std::shared_ptr<Variable> > variableList;
288
289 for (const auto &index : indexes) {
290 auto variable = impl->m_Variables.at(index.row());
291 if (variable.get() && index.isValid()) {
292 variableList << variable;
293 }
294 }
295
296 auto encodedData = impl->m_VariableController->mimeDataForVariables(variableList);
297 mimeData->setData(MIME_TYPE_VARIABLE_LIST, encodedData);
298
299 return mimeData;
300 }
301
302 bool VariableModel::canDropMimeData(const QMimeData *data, Qt::DropAction action, int row,
303 int column, const QModelIndex &parent) const
304 {
305 return false;
306 }
307
308 bool VariableModel::dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column,
309 const QModelIndex &parent)
310 {
311 return false;
312 }
313
258 void VariableModel::abortProgress(const QModelIndex &index)
314 void VariableModel::abortProgress(const QModelIndex &index)
259 {
315 {
260 if (auto variable = impl->m_Variables.at(index.row())) {
316 if (auto variable = impl->m_Variables.at(index.row())) {
261 emit abortProgessRequested(variable);
317 emit abortProgessRequested(variable);
262 }
318 }
263 }
319 }
264
320
265 void VariableModel::onVariableUpdated() noexcept
321 void VariableModel::onVariableUpdated() noexcept
266 {
322 {
267 // Finds variable that has been updated in the model
323 // Finds variable that has been updated in the model
268 if (auto updatedVariable = dynamic_cast<Variable *>(sender())) {
324 if (auto updatedVariable = dynamic_cast<Variable *>(sender())) {
269 auto updatedVariableIndex = impl->indexOfVariable(updatedVariable);
325 auto updatedVariableIndex = impl->indexOfVariable(updatedVariable);
270
326
271 if (updatedVariableIndex > -1) {
327 if (updatedVariableIndex > -1) {
272 emit dataChanged(createIndex(updatedVariableIndex, 0),
328 emit dataChanged(createIndex(updatedVariableIndex, 0),
273 createIndex(updatedVariableIndex, columnCount() - 1));
329 createIndex(updatedVariableIndex, columnCount() - 1));
274 }
330 }
275 }
331 }
276 }
332 }
277
333
278 int VariableModel::VariableModelPrivate::indexOfVariable(Variable *variable) const noexcept
334 int VariableModel::VariableModelPrivate::indexOfVariable(Variable *variable) const noexcept
279 {
335 {
280 auto begin = std::cbegin(m_Variables);
336 auto begin = std::cbegin(m_Variables);
281 auto end = std::cend(m_Variables);
337 auto end = std::cend(m_Variables);
282 auto it
338 auto it
283 = std::find_if(begin, end, [variable](const auto &var) { return var.get() == variable; });
339 = std::find_if(begin, end, [variable](const auto &var) { return var.get() == variable; });
284
340
285 if (it != end) {
341 if (it != end) {
286 // Gets the index of the variable in the model: we assume here that views have the same
342 // Gets the index of the variable in the model: we assume here that views have the same
287 // order as the model
343 // order as the model
288 return std::distance(begin, it);
344 return std::distance(begin, it);
289 }
345 }
290 else {
346 else {
291 return -1;
347 return -1;
292 }
348 }
293 }
349 }
General Comments 0
You need to be logged in to leave comments. Login now