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

Auto status change to "Under Review"

You need to be logged in to leave comments. Login now