##// END OF EJS Templates
Use std::shared_ptr in CosinusProvider
Alexandre Leroux -
r287:9a5cb57f1573
parent child
Show More
@@ -1,70 +1,93
1 1 #ifndef SCIQLOP_DATASERIES_H
2 2 #define SCIQLOP_DATASERIES_H
3 3
4 4 #include <Data/ArrayData.h>
5 5 #include <Data/IDataSeries.h>
6 6
7 7 #include <QLoggingCategory>
8 8
9 9 #include <memory>
10 10
11 11
12 12 Q_DECLARE_LOGGING_CATEGORY(LOG_DataSeries)
13 13 Q_LOGGING_CATEGORY(LOG_DataSeries, "DataSeries")
14 14
15 15
16 16 /**
17 17 * @brief The DataSeries class is the base (abstract) implementation of IDataSeries.
18 18 *
19 19 * It proposes to set a dimension for the values ​​data
20 20 *
21 21 * @tparam Dim The dimension of the values data
22 22 *
23 23 */
24 24 template <int Dim>
25 25 class DataSeries : public IDataSeries {
26 26 public:
27 27 /// @sa IDataSeries::xAxisData()
28 28 std::shared_ptr<ArrayData<1> > xAxisData() override { return m_XAxisData; }
29 const std::shared_ptr<ArrayData<1> > xAxisData() const { return m_XAxisData; }
29 30
30 31 /// @sa IDataSeries::xAxisUnit()
31 32 Unit xAxisUnit() const override { return m_XAxisUnit; }
32 33
33 34 /// @return the values dataset
34 std::shared_ptr<ArrayData<Dim> > valuesData() const { return m_ValuesData; }
35 std::shared_ptr<ArrayData<Dim> > valuesData() { return m_ValuesData; }
36 const std::shared_ptr<ArrayData<Dim> > valuesData() const { return m_ValuesData; }
35 37
36 38 /// @sa IDataSeries::valuesUnit()
37 39 Unit valuesUnit() const override { return m_ValuesUnit; }
38 40
39 41 /// @sa IDataSeries::merge()
40 42 void merge(IDataSeries *dataSeries) override
41 43 {
42 44 if (auto dimDataSeries = dynamic_cast<DataSeries<Dim> *>(dataSeries)) {
43 45 m_XAxisData->merge(*dimDataSeries->xAxisData());
44 46 m_ValuesData->merge(*dimDataSeries->valuesData());
45 47 }
46 48 else {
47 49 qCWarning(LOG_DataSeries())
48 50 << QObject::tr("Dection of a type of IDataSeries we cannot merge with !");
49 51 }
50 52 }
51 53
52 54 protected:
53 55 /// Protected ctor (DataSeries is abstract)
54 56 explicit DataSeries(std::shared_ptr<ArrayData<1> > xAxisData, const Unit &xAxisUnit,
55 57 std::shared_ptr<ArrayData<Dim> > valuesData, const Unit &valuesUnit)
56 58 : m_XAxisData{xAxisData},
57 59 m_XAxisUnit{xAxisUnit},
58 60 m_ValuesData{valuesData},
59 61 m_ValuesUnit{valuesUnit}
60 62 {
61 63 }
62 64
65 /// Copy ctor
66 explicit DataSeries(const DataSeries<Dim> &other)
67 : m_XAxisData{std::make_shared<ArrayData<1> >(*other.m_XAxisData)},
68 m_XAxisUnit{other.m_XAxisUnit},
69 m_ValuesData{std::make_shared<ArrayData<Dim> >(*other.m_ValuesData)},
70 m_ValuesUnit{other.m_ValuesUnit}
71 {
72 }
73
74 /// Assignment operator
75 template <int D>
76 DataSeries &operator=(DataSeries<D> other)
77 {
78 std::swap(m_XAxisData, other.m_XAxisData);
79 std::swap(m_XAxisUnit, other.m_XAxisUnit);
80 std::swap(m_ValuesData, other.m_ValuesData);
81 std::swap(m_ValuesUnit, other.m_ValuesUnit);
82
83 return *this;
84 }
85
63 86 private:
64 87 std::shared_ptr<ArrayData<1> > m_XAxisData;
65 88 Unit m_XAxisUnit;
66 89 std::shared_ptr<ArrayData<Dim> > m_ValuesData;
67 90 Unit m_ValuesUnit;
68 91 };
69 92
70 93 #endif // SCIQLOP_DATASERIES_H
@@ -1,42 +1,42
1 1 #ifndef SCIQLOP_IDATAPROVIDER_H
2 2 #define SCIQLOP_IDATAPROVIDER_H
3 3
4 4 #include <memory>
5 5
6 6 #include <QObject>
7 7
8 8 #include <Common/MetaTypes.h>
9 9
10 10 #include <Data/SqpDateTime.h>
11 11
12 12 class DataProviderParameters;
13 13 class IDataSeries;
14 14
15 15 /**
16 16 * @brief The IDataProvider interface aims to declare a data provider.
17 17 *
18 18 * A data provider is an entity that generates data and returns it according to various parameters
19 19 * (time interval, product to retrieve the data, etc.)
20 20 *
21 21 * @sa IDataSeries
22 22 */
23 23 class IDataProvider : public QObject {
24 24
25 25 Q_OBJECT
26 26 public:
27 27 virtual ~IDataProvider() noexcept = default;
28 28
29 virtual std::unique_ptr<IDataSeries>
29 virtual std::shared_ptr<IDataSeries>
30 30 retrieveData(const DataProviderParameters &parameters) const = 0;
31 31
32 32
33 33 virtual void requestDataLoading(const QVector<SqpDateTime> &dateTimeList) = 0;
34 34
35 35 signals:
36 36 void dataProvided(std::shared_ptr<IDataSeries> dateSerie, const SqpDateTime &dateTime);
37 37 };
38 38
39 39 // Required for using shared_ptr in signals/slots
40 40 SCIQLOP_REGISTER_META_TYPE(IDATAPROVIDER_PTR_REGISTRY, std::shared_ptr<IDataProvider>)
41 41
42 42 #endif // SCIQLOP_IDATAPROVIDER_H
@@ -1,54 +1,59
1 1 #ifndef SCIQLOP_IDATASERIES_H
2 2 #define SCIQLOP_IDATASERIES_H
3 3
4 4 #include <Common/MetaTypes.h>
5 5
6 6 #include <memory>
7 7
8 8 #include <QString>
9 9
10 10 template <int Dim>
11 11 class ArrayData;
12 12
13 13 struct Unit {
14 14 explicit Unit(const QString &name = {}, bool timeUnit = false)
15 15 : m_Name{name}, m_TimeUnit{timeUnit}
16 16 {
17 17 }
18 18
19 19 QString m_Name; ///< Unit name
20 20 bool m_TimeUnit; ///< The unit is a unit of time
21 21 };
22 22
23 23 /**
24 24 * @brief The IDataSeries aims to declare a data series.
25 25 *
26 26 * A data series is an entity that contains at least :
27 27 * - one dataset representing the x-axis
28 28 * - one dataset representing the values
29 29 *
30 30 * Each dataset is represented by an ArrayData, and is associated with a unit.
31 31 *
32 32 * An ArrayData can be unidimensional or two-dimensional, depending on the implementation of the
33 33 * IDataSeries. The x-axis dataset is always unidimensional.
34 34 *
35 35 * @sa ArrayData
36 36 */
37 37 class IDataSeries {
38 38 public:
39 39 virtual ~IDataSeries() noexcept = default;
40 40
41 41 /// Returns the x-axis dataset
42 42 virtual std::shared_ptr<ArrayData<1> > xAxisData() = 0;
43 43
44 /// Returns the x-axis dataset (as const)
45 virtual const std::shared_ptr<ArrayData<1> > xAxisData() const = 0;
46
44 47 virtual Unit xAxisUnit() const = 0;
45 48
46 49 virtual Unit valuesUnit() const = 0;
47 50
48 51 virtual void merge(IDataSeries *dataSeries) = 0;
52
53 virtual std::unique_ptr<IDataSeries> clone() const = 0;
49 54 };
50 55
51 56 // Required for using shared_ptr in signals/slots
52 57 SCIQLOP_REGISTER_META_TYPE(IDATASERIES_PTR_REGISTRY, std::shared_ptr<IDataSeries>)
53 58
54 59 #endif // SCIQLOP_IDATASERIES_H
@@ -1,28 +1,30
1 1 #ifndef SCIQLOP_SCALARSERIES_H
2 2 #define SCIQLOP_SCALARSERIES_H
3 3
4 4 #include <Data/DataSeries.h>
5 5
6 6 /**
7 7 * @brief The ScalarSeries class is the implementation for a data series representing a scalar.
8 8 */
9 9 class ScalarSeries : public DataSeries<1> {
10 10 public:
11 11 /**
12 12 * Ctor
13 13 * @param size the number of data the series will hold
14 14 * @param xAxisUnit x-axis unit
15 15 * @param valuesUnit values unit
16 16 */
17 17 explicit ScalarSeries(int size, const Unit &xAxisUnit, const Unit &valuesUnit);
18 18
19 19 /**
20 20 * Sets data for a specific index. The index has to be valid to be effective
21 21 * @param index the index to which the data will be set
22 22 * @param x the x-axis data
23 23 * @param value the value data
24 24 */
25 25 void setData(int index, double x, double value) noexcept;
26
27 std::unique_ptr<IDataSeries> clone() const;
26 28 };
27 29
28 30 #endif // SCIQLOP_SCALARSERIES_H
@@ -1,55 +1,54
1 1 #ifndef SCIQLOP_VARIABLE_H
2 2 #define SCIQLOP_VARIABLE_H
3 3
4 4 #include <Data/SqpDateTime.h>
5 5
6 6 #include <QLoggingCategory>
7 7 #include <QObject>
8 8
9 9 #include <Common/MetaTypes.h>
10 10 #include <Common/spimpl.h>
11 11
12 12 Q_DECLARE_LOGGING_CATEGORY(LOG_Variable)
13 13
14 14 class IDataSeries;
15 15 class QString;
16 16
17 17 /**
18 18 * @brief The Variable class represents a variable in SciQlop.
19 19 */
20 20 class Variable : public QObject {
21 21
22 22 Q_OBJECT
23 23
24 24 public:
25 25 explicit Variable(const QString &name, const QString &unit, const QString &mission,
26 26 const SqpDateTime &dateTime);
27 27
28 28 QString name() const noexcept;
29 29 QString mission() const noexcept;
30 30 QString unit() const noexcept;
31 31 SqpDateTime dateTime() const noexcept;
32 32 void setDateTime(const SqpDateTime &dateTime) noexcept;
33 33
34 34 /// @return the data of the variable, nullptr if there is no data
35 35 IDataSeries *dataSeries() const noexcept;
36 36
37 37 bool contains(const SqpDateTime &dateTime);
38 38 bool intersect(const SqpDateTime &dateTime);
39 void setDataSeries(std::unique_ptr<IDataSeries> dataSeries) noexcept;
40 39
41 40 public slots:
42 void onAddDataSeries(std::shared_ptr<IDataSeries> dataSeries) noexcept;
41 void setDataSeries(std::shared_ptr<IDataSeries> dataSeries) noexcept;
43 42
44 43 signals:
45 44 void updated();
46 45
47 46 private:
48 47 class VariablePrivate;
49 48 spimpl::unique_impl_ptr<VariablePrivate> impl;
50 49 };
51 50
52 51 // Required for using shared_ptr in signals/slots
53 52 SCIQLOP_REGISTER_META_TYPE(VARIABLE_PTR_REGISTRY, std::shared_ptr<Variable>)
54 53
55 54 #endif // SCIQLOP_VARIABLE_H
@@ -1,51 +1,51
1 1 #ifndef SCIQLOP_VARIABLEMODEL_H
2 2 #define SCIQLOP_VARIABLEMODEL_H
3 3
4 4
5 5 #include <Data/SqpDateTime.h>
6 6
7 7 #include <QAbstractTableModel>
8 8 #include <QLoggingCategory>
9 9
10 10 #include <Common/spimpl.h>
11 11
12 12 Q_DECLARE_LOGGING_CATEGORY(LOG_VariableModel)
13 13
14 14 class IDataSeries;
15 15 class Variable;
16 16
17 17 /**
18 18 * @brief The VariableModel class aims to hold the variables that have been created in SciQlop
19 19 */
20 20 class VariableModel : public QAbstractTableModel {
21 21 public:
22 22 explicit VariableModel(QObject *parent = nullptr);
23 23
24 24 /**
25 25 * Creates a new variable in the model
26 26 * @param name the name of the new variable
27 27 * @param dateTime the dateTime of the new variable
28 28 * @param defaultDataSeries the default data of the new variable
29 29 * @return the pointer to the new variable
30 30 */
31 31 std::shared_ptr<Variable>
32 32 createVariable(const QString &name, const SqpDateTime &dateTime,
33 std::unique_ptr<IDataSeries> defaultDataSeries) noexcept;
33 std::shared_ptr<IDataSeries> defaultDataSeries) noexcept;
34 34
35 35 std::shared_ptr<Variable> variable(int index) const;
36 36
37 37 // /////////////////////////// //
38 38 // QAbstractTableModel methods //
39 39 // /////////////////////////// //
40 40 virtual int columnCount(const QModelIndex &parent = QModelIndex{}) const override;
41 41 virtual int rowCount(const QModelIndex &parent = QModelIndex{}) const override;
42 42 virtual QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
43 43 virtual QVariant headerData(int section, Qt::Orientation orientation,
44 44 int role = Qt::DisplayRole) const override;
45 45
46 46 private:
47 47 class VariableModelPrivate;
48 48 spimpl::unique_impl_ptr<VariableModelPrivate> impl;
49 49 };
50 50
51 51 #endif // SCIQLOP_VARIABLEMODEL_H
@@ -1,13 +1,18
1 1 #include <Data/ScalarSeries.h>
2 2
3 3 ScalarSeries::ScalarSeries(int size, const Unit &xAxisUnit, const Unit &valuesUnit)
4 4 : DataSeries{std::make_shared<ArrayData<1> >(size), xAxisUnit,
5 5 std::make_shared<ArrayData<1> >(size), valuesUnit}
6 6 {
7 7 }
8 8
9 9 void ScalarSeries::setData(int index, double x, double value) noexcept
10 10 {
11 11 xAxisData()->setData(index, x);
12 12 valuesData()->setData(index, value);
13 13 }
14
15 std::unique_ptr<IDataSeries> ScalarSeries::clone() const
16 {
17 return std::make_unique<ScalarSeries>(*this);
18 }
@@ -1,87 +1,89
1 1 #include "Variable/Variable.h"
2 2
3 3 #include <Data/IDataSeries.h>
4 4 #include <Data/SqpDateTime.h>
5 5
6 6 Q_LOGGING_CATEGORY(LOG_Variable, "Variable")
7 7
8 8 struct Variable::VariablePrivate {
9 9 explicit VariablePrivate(const QString &name, const QString &unit, const QString &mission,
10 10 const SqpDateTime &dateTime)
11 11 : m_Name{name},
12 12 m_Unit{unit},
13 13 m_Mission{mission},
14 14 m_DateTime{dateTime},
15 15 m_DataSeries{nullptr}
16 16 {
17 17 }
18 18
19 19 QString m_Name;
20 20 QString m_Unit;
21 21 QString m_Mission;
22 22
23 23 SqpDateTime m_DateTime; // The dateTime available in the view and loaded. not the cache.
24 24 std::unique_ptr<IDataSeries> m_DataSeries;
25 25 };
26 26
27 27 Variable::Variable(const QString &name, const QString &unit, const QString &mission,
28 28 const SqpDateTime &dateTime)
29 29 : impl{spimpl::make_unique_impl<VariablePrivate>(name, unit, mission, dateTime)}
30 30 {
31 31 }
32 32
33 33 QString Variable::name() const noexcept
34 34 {
35 35 return impl->m_Name;
36 36 }
37 37
38 38 QString Variable::mission() const noexcept
39 39 {
40 40 return impl->m_Mission;
41 41 }
42 42
43 43 QString Variable::unit() const noexcept
44 44 {
45 45 return impl->m_Unit;
46 46 }
47 47
48 48 SqpDateTime Variable::dateTime() const noexcept
49 49 {
50 50 return impl->m_DateTime;
51 51 }
52 52
53 53 void Variable::setDateTime(const SqpDateTime &dateTime) noexcept
54 54 {
55 55 impl->m_DateTime = dateTime;
56 56 }
57 57
58 void Variable::setDataSeries(std::unique_ptr<IDataSeries> dataSeries) noexcept
58 void Variable::setDataSeries(std::shared_ptr<IDataSeries> dataSeries) noexcept
59 59 {
60 if (!impl->m_DataSeries) {
61 impl->m_DataSeries = std::move(dataSeries);
60 if (!dataSeries) {
61 /// @todo ALX : log
62 return;
62 63 }
63 }
64 64
65 void Variable::onAddDataSeries(std::shared_ptr<IDataSeries> dataSeries) noexcept
66 {
67 if (impl->m_DataSeries) {
65 // Inits the data series of the variable
66 if (!impl->m_DataSeries) {
67 impl->m_DataSeries = dataSeries->clone();
68 }
69 else {
68 70 impl->m_DataSeries->merge(dataSeries.get());
69 71
70 72 emit updated();
71 73 }
72 74 }
73 75
74 76 IDataSeries *Variable::dataSeries() const noexcept
75 77 {
76 78 return impl->m_DataSeries.get();
77 79 }
78 80
79 81 bool Variable::contains(const SqpDateTime &dateTime)
80 82 {
81 83 return impl->m_DateTime.contains(dateTime);
82 84 }
83 85
84 86 bool Variable::intersect(const SqpDateTime &dateTime)
85 87 {
86 88 return impl->m_DateTime.intersect(dateTime);
87 89 }
@@ -1,175 +1,175
1 1 #include <Variable/Variable.h>
2 2 #include <Variable/VariableCacheController.h>
3 3 #include <Variable/VariableController.h>
4 4 #include <Variable/VariableModel.h>
5 5
6 6 #include <Data/DataProviderParameters.h>
7 7 #include <Data/IDataProvider.h>
8 8 #include <Data/IDataSeries.h>
9 9 #include <Time/TimeController.h>
10 10
11 11 #include <QDateTime>
12 12 #include <QMutex>
13 13 #include <QThread>
14 14 #include <QtCore/QItemSelectionModel>
15 15
16 16 #include <unordered_map>
17 17
18 18 Q_LOGGING_CATEGORY(LOG_VariableController, "VariableController")
19 19
20 20 namespace {
21 21
22 22 /// @todo Generates default dataseries, according to the provider passed in parameter. This method
23 23 /// will be deleted when the timerange is recovered from SciQlop
24 std::unique_ptr<IDataSeries> generateDefaultDataSeries(const IDataProvider &provider,
24 std::shared_ptr<IDataSeries> generateDefaultDataSeries(const IDataProvider &provider,
25 25 const SqpDateTime &dateTime) noexcept
26 26 {
27 27 auto parameters = DataProviderParameters{dateTime};
28 28
29 29 return provider.retrieveData(parameters);
30 30 }
31 31
32 32 } // namespace
33 33
34 34 struct VariableController::VariableControllerPrivate {
35 35 explicit VariableControllerPrivate(VariableController *parent)
36 36 : m_WorkingMutex{},
37 37 m_VariableModel{new VariableModel{parent}},
38 38 m_VariableSelectionModel{new QItemSelectionModel{m_VariableModel, parent}},
39 39 m_VariableCacheController{std::make_unique<VariableCacheController>()}
40 40 {
41 41 }
42 42
43 43 QMutex m_WorkingMutex;
44 44 /// Variable model. The VariableController has the ownership
45 45 VariableModel *m_VariableModel;
46 46 QItemSelectionModel *m_VariableSelectionModel;
47 47
48 48
49 49 TimeController *m_TimeController{nullptr};
50 50 std::unique_ptr<VariableCacheController> m_VariableCacheController;
51 51
52 52 std::unordered_map<std::shared_ptr<Variable>, std::shared_ptr<IDataProvider> >
53 53 m_VariableToProviderMap;
54 54 };
55 55
56 56 VariableController::VariableController(QObject *parent)
57 57 : QObject{parent}, impl{spimpl::make_unique_impl<VariableControllerPrivate>(this)}
58 58 {
59 59 qCDebug(LOG_VariableController()) << tr("VariableController construction")
60 60 << QThread::currentThread();
61 61 }
62 62
63 63 VariableController::~VariableController()
64 64 {
65 65 qCDebug(LOG_VariableController()) << tr("VariableController destruction")
66 66 << QThread::currentThread();
67 67 this->waitForFinish();
68 68 }
69 69
70 70 VariableModel *VariableController::variableModel() noexcept
71 71 {
72 72 return impl->m_VariableModel;
73 73 }
74 74
75 75 QItemSelectionModel *VariableController::variableSelectionModel() noexcept
76 76 {
77 77 return impl->m_VariableSelectionModel;
78 78 }
79 79
80 80 void VariableController::setTimeController(TimeController *timeController) noexcept
81 81 {
82 82 impl->m_TimeController = timeController;
83 83 }
84 84
85 85 void VariableController::createVariable(const QString &name,
86 86 std::shared_ptr<IDataProvider> provider) noexcept
87 87 {
88 88
89 89 if (!impl->m_TimeController) {
90 90 qCCritical(LOG_VariableController())
91 91 << tr("Impossible to create variable: The time controller is null");
92 92 return;
93 93 }
94 94
95 95
96 96 /// @todo : for the moment :
97 97 /// - the provider is only used to retrieve data from the variable for its initialization, but
98 98 /// it will be retained later
99 99 /// - default data are generated for the variable, without taking into account the timerange set
100 100 /// in sciqlop
101 101 auto dateTime = impl->m_TimeController->dateTime();
102 102 if (auto newVariable = impl->m_VariableModel->createVariable(
103 103 name, dateTime, generateDefaultDataSeries(*provider, dateTime))) {
104 104
105 105 // store the provider
106 106 impl->m_VariableToProviderMap[newVariable] = provider;
107 107 connect(provider.get(), &IDataProvider::dataProvided, newVariable.get(),
108 &Variable::onAddDataSeries);
108 &Variable::setDataSeries);
109 109
110 110
111 111 // store in cache
112 112 impl->m_VariableCacheController->addDateTime(newVariable, dateTime);
113 113
114 114 // notify the creation
115 115 emit variableCreated(newVariable);
116 116 }
117 117 }
118 118
119 119 void VariableController::onDateTimeOnSelection(const SqpDateTime &dateTime)
120 120 {
121 121 auto selectedRows = impl->m_VariableSelectionModel->selectedRows();
122 122
123 123 for (const auto &selectedRow : qAsConst(selectedRows)) {
124 124 if (auto selectedVariable = impl->m_VariableModel->variable(selectedRow.row())) {
125 125 selectedVariable->setDateTime(dateTime);
126 126 this->onRequestDataLoading(selectedVariable, dateTime);
127 127 }
128 128 }
129 129 }
130 130
131 131
132 132 void VariableController::onRequestDataLoading(std::shared_ptr<Variable> variable,
133 133 const SqpDateTime &dateTime)
134 134 {
135 135 // we want to load data of the variable for the dateTime.
136 136 // First we check if the cache contains some of them.
137 137 // For the other, we ask the provider to give them.
138 138 if (variable) {
139 139
140 140 auto dateTimeListNotInCache
141 141 = impl->m_VariableCacheController->provideNotInCacheDateTimeList(variable, dateTime);
142 142
143 143 if (!dateTimeListNotInCache.empty()) {
144 144 // Ask the provider for each data on the dateTimeListNotInCache
145 145 impl->m_VariableToProviderMap.at(variable)->requestDataLoading(
146 146 std::move(dateTimeListNotInCache));
147 147 // store in cache
148 148 impl->m_VariableCacheController->addDateTime(variable, dateTime);
149 149 }
150 150 else {
151 151 emit variable->updated();
152 152 }
153 153 }
154 154 else {
155 155 qCCritical(LOG_VariableController()) << tr("Impossible to load data of a variable null");
156 156 }
157 157 }
158 158
159 159
160 160 void VariableController::initialize()
161 161 {
162 162 qCDebug(LOG_VariableController()) << tr("VariableController init") << QThread::currentThread();
163 163 impl->m_WorkingMutex.lock();
164 164 qCDebug(LOG_VariableController()) << tr("VariableController init END");
165 165 }
166 166
167 167 void VariableController::finalize()
168 168 {
169 169 impl->m_WorkingMutex.unlock();
170 170 }
171 171
172 172 void VariableController::waitForFinish()
173 173 {
174 174 QMutexLocker locker{&impl->m_WorkingMutex};
175 175 }
@@ -1,155 +1,155
1 1 #include <Variable/Variable.h>
2 2 #include <Variable/VariableModel.h>
3 3
4 4 #include <Data/IDataSeries.h>
5 5
6 6 #include <QDateTime>
7 7 #include <QSize>
8 8
9 9 Q_LOGGING_CATEGORY(LOG_VariableModel, "VariableModel")
10 10
11 11 namespace {
12 12
13 13 // Column indexes
14 14 const auto NAME_COLUMN = 0;
15 15 const auto TSTART_COLUMN = 1;
16 16 const auto TEND_COLUMN = 2;
17 17 const auto NB_COLUMNS = 3;
18 18
19 19 // Column properties
20 20 const auto DEFAULT_HEIGHT = 25;
21 21 const auto DEFAULT_WIDTH = 100;
22 22
23 23 struct ColumnProperties {
24 24 ColumnProperties(const QString &name = {}, int width = DEFAULT_WIDTH,
25 25 int height = DEFAULT_HEIGHT)
26 26 : m_Name{name}, m_Width{width}, m_Height{height}
27 27 {
28 28 }
29 29
30 30 QString m_Name;
31 31 int m_Width;
32 32 int m_Height;
33 33 };
34 34
35 35 const auto COLUMN_PROPERTIES
36 36 = QHash<int, ColumnProperties>{{NAME_COLUMN, {QObject::tr("Name")}},
37 37 {TSTART_COLUMN, {QObject::tr("tStart"), 180}},
38 38 {TEND_COLUMN, {QObject::tr("tEnd"), 180}}};
39 39
40 40 /// Format for datetimes
41 41 const auto DATETIME_FORMAT = QStringLiteral("dd/MM/yyyy \nhh:mm:ss:zzz");
42 42
43 43 } // namespace
44 44
45 45 struct VariableModel::VariableModelPrivate {
46 46 /// Variables created in SciQlop
47 47 std::vector<std::shared_ptr<Variable> > m_Variables;
48 48 };
49 49
50 50 VariableModel::VariableModel(QObject *parent)
51 51 : QAbstractTableModel{parent}, impl{spimpl::make_unique_impl<VariableModelPrivate>()}
52 52 {
53 53 }
54 54
55 55 std::shared_ptr<Variable>
56 56 VariableModel::createVariable(const QString &name, const SqpDateTime &dateTime,
57 std::unique_ptr<IDataSeries> defaultDataSeries) noexcept
57 std::shared_ptr<IDataSeries> defaultDataSeries) noexcept
58 58 {
59 59 auto insertIndex = rowCount();
60 60 beginInsertRows({}, insertIndex, insertIndex);
61 61
62 62 /// @todo For the moment, the other data of the variable is initialized with default values
63 63 auto variable = std::make_shared<Variable>(name, QStringLiteral("unit"),
64 64 QStringLiteral("mission"), dateTime);
65 65 variable->setDataSeries(std::move(defaultDataSeries));
66 66
67 67 impl->m_Variables.push_back(variable);
68 68
69 69 endInsertRows();
70 70
71 71 return variable;
72 72 }
73 73
74 74 std::shared_ptr<Variable> VariableModel::variable(int index) const
75 75 {
76 76 return (index >= 0 && index < impl->m_Variables.size()) ? impl->m_Variables[index] : nullptr;
77 77 }
78 78
79 79 int VariableModel::columnCount(const QModelIndex &parent) const
80 80 {
81 81 Q_UNUSED(parent);
82 82
83 83 return NB_COLUMNS;
84 84 }
85 85
86 86 int VariableModel::rowCount(const QModelIndex &parent) const
87 87 {
88 88 Q_UNUSED(parent);
89 89
90 90 return impl->m_Variables.size();
91 91 }
92 92
93 93 QVariant VariableModel::data(const QModelIndex &index, int role) const
94 94 {
95 95 if (!index.isValid()) {
96 96 return QVariant{};
97 97 }
98 98
99 99 if (index.row() < 0 || index.row() >= rowCount()) {
100 100 return QVariant{};
101 101 }
102 102
103 103 if (role == Qt::DisplayRole) {
104 104 if (auto variable = impl->m_Variables.at(index.row()).get()) {
105 105 /// Lambda function that builds the variant to return for a time value
106 106 auto dateTimeVariant = [](double time) {
107 107 auto dateTime = QDateTime::fromMSecsSinceEpoch(time * 1000.);
108 108 return dateTime.toString(DATETIME_FORMAT);
109 109 };
110 110
111 111 switch (index.column()) {
112 112 case NAME_COLUMN:
113 113 return variable->name();
114 114 case TSTART_COLUMN:
115 115 return dateTimeVariant(variable->dateTime().m_TStart);
116 116 case TEND_COLUMN:
117 117 return dateTimeVariant(variable->dateTime().m_TEnd);
118 118 default:
119 119 // No action
120 120 break;
121 121 }
122 122
123 123 qWarning(LOG_VariableModel())
124 124 << tr("Can't get data (unknown column %1)").arg(index.column());
125 125 }
126 126 else {
127 127 qWarning(LOG_VariableModel()) << tr("Can't get data (no variable)");
128 128 }
129 129 }
130 130
131 131 return QVariant{};
132 132 }
133 133
134 134 QVariant VariableModel::headerData(int section, Qt::Orientation orientation, int role) const
135 135 {
136 136 if (role != Qt::DisplayRole && role != Qt::SizeHintRole) {
137 137 return QVariant{};
138 138 }
139 139
140 140 if (orientation == Qt::Horizontal) {
141 141 auto propertiesIt = COLUMN_PROPERTIES.find(section);
142 142 if (propertiesIt != COLUMN_PROPERTIES.cend()) {
143 143 // Role is either DisplayRole or SizeHintRole
144 144 return (role == Qt::DisplayRole)
145 145 ? QVariant{propertiesIt->m_Name}
146 146 : QVariant{QSize{propertiesIt->m_Width, propertiesIt->m_Height}};
147 147 }
148 148 else {
149 149 qWarning(LOG_VariableModel())
150 150 << tr("Can't get header data (unknown column %1)").arg(section);
151 151 }
152 152 }
153 153
154 154 return QVariant{};
155 155 }
@@ -1,26 +1,26
1 1 #ifndef SCIQLOP_COSINUSPROVIDER_H
2 2 #define SCIQLOP_COSINUSPROVIDER_H
3 3
4 4 #include <Data/IDataProvider.h>
5 5
6 6 #include <QLoggingCategory>
7 7
8 8 Q_DECLARE_LOGGING_CATEGORY(LOG_CosinusProvider)
9 9
10 10 /**
11 11 * @brief The CosinusProvider class is an example of how a data provider can generate data
12 12 */
13 13 class CosinusProvider : public IDataProvider {
14 14 public:
15 15 /// @sa IDataProvider::retrieveData()
16 std::unique_ptr<IDataSeries>
16 std::shared_ptr<IDataSeries>
17 17 retrieveData(const DataProviderParameters &parameters) const override;
18 18
19 19 void requestDataLoading(const QVector<SqpDateTime> &dateTimeList) override;
20 20
21 21
22 22 private:
23 23 std::shared_ptr<IDataSeries> retrieveDataSeries(const SqpDateTime &dateTime);
24 24 };
25 25
26 26 #endif // SCIQLOP_COSINUSPROVIDER_H
@@ -1,74 +1,47
1 1 #include "CosinusProvider.h"
2 2
3 3 #include <Data/DataProviderParameters.h>
4 4 #include <Data/ScalarSeries.h>
5 5
6 6 #include <cmath>
7 7
8 8 #include <QDateTime>
9 9
10 10 Q_LOGGING_CATEGORY(LOG_CosinusProvider, "CosinusProvider")
11 11
12 std::unique_ptr<IDataSeries>
12 std::shared_ptr<IDataSeries>
13 13 CosinusProvider::retrieveData(const DataProviderParameters &parameters) const
14 14 {
15 15 auto dateTime = parameters.m_Time;
16 16
17 // Gets the timerange from the parameters
18 auto start = dateTime.m_TStart;
19 auto end = dateTime.m_TEnd;
20
21
22 // We assure that timerange is valid
23 if (end < start) {
24 std::swap(start, end);
25 }
26
27 // Generates scalar series containing cosinus values (one value per second)
28 auto scalarSeries
29 = std::make_unique<ScalarSeries>(end - start, Unit{QStringLiteral("t"), true}, Unit{});
30
31 auto dataIndex = 0;
32 for (auto time = start; time < end; ++time, ++dataIndex) {
33 scalarSeries->setData(dataIndex, time, std::cos(time));
34 }
35
36 return scalarSeries;
37 }
38
39 void CosinusProvider::requestDataLoading(const QVector<SqpDateTime> &dateTimeList)
40 {
41 // NOTE: Try to use multithread if possible
42 for (const auto &dateTime : dateTimeList) {
43
44 auto scalarSeries = this->retrieveDataSeries(dateTime);
45
46 emit dataProvided(scalarSeries, dateTime);
47 }
48 }
49
50
51 std::shared_ptr<IDataSeries> CosinusProvider::retrieveDataSeries(const SqpDateTime &dateTime)
52 {
53 17 auto dataIndex = 0;
54 18
55 19 // Gets the timerange from the parameters
56 20 double freq = 100.0;
57 21 double start = dateTime.m_TStart * freq; // 100 htz
58 22 double end = dateTime.m_TEnd * freq; // 100 htz
59 23
60 24 // We assure that timerange is valid
61 25 if (end < start) {
62 26 std::swap(start, end);
63 27 }
64 28
65 29 // Generates scalar series containing cosinus values (one value per second)
66 30 auto scalarSeries
67 31 = std::make_shared<ScalarSeries>(end - start, Unit{QStringLiteral("t"), true}, Unit{});
68 32
69 33 for (auto time = start; time < end; ++time, ++dataIndex) {
70 34 const auto timeOnFreq = time / freq;
71 35 scalarSeries->setData(dataIndex, timeOnFreq, std::cos(timeOnFreq));
72 36 }
73 37 return scalarSeries;
74 38 }
39
40 void CosinusProvider::requestDataLoading(const QVector<SqpDateTime> &dateTimeList)
41 {
42 // NOTE: Try to use multithread if possible
43 for (const auto &dateTime : dateTimeList) {
44 auto scalarSeries = this->retrieveData(DataProviderParameters{dateTime});
45 emit dataProvided(scalarSeries, dateTime);
46 }
47 }
General Comments 0
You need to be logged in to leave comments. Login now