##// END OF EJS Templates
(Refactoring) Renames IDataSeries::minData() and IDataSeries::maxData()
Alexandre Leroux -
r604:6cc1dd13aa83
parent child
Show More
@@ -1,306 +1,306
1 1 #ifndef SCIQLOP_DATASERIES_H
2 2 #define SCIQLOP_DATASERIES_H
3 3
4 4 #include "CoreGlobal.h"
5 5
6 6 #include <Common/SortUtils.h>
7 7
8 8 #include <Data/ArrayData.h>
9 9 #include <Data/IDataSeries.h>
10 10
11 11 #include <QLoggingCategory>
12 12 #include <QReadLocker>
13 13 #include <QReadWriteLock>
14 14 #include <memory>
15 15
16 16 // We don't use the Qt macro since the log is used in the header file, which causes multiple log
17 17 // definitions with inheritance. Inline method is used instead
18 18 inline const QLoggingCategory &LOG_DataSeries()
19 19 {
20 20 static const QLoggingCategory category{"DataSeries"};
21 21 return category;
22 22 }
23 23
24 24 template <int Dim>
25 25 class DataSeries;
26 26
27 27 namespace dataseries_detail {
28 28
29 29 template <int Dim>
30 30 class IteratorValue : public DataSeriesIteratorValue::Impl {
31 31 public:
32 32 explicit IteratorValue(const DataSeries<Dim> &dataSeries, bool begin)
33 33 : m_XIt(begin ? dataSeries.xAxisData()->cbegin() : dataSeries.xAxisData()->cend()),
34 34 m_ValuesIt(begin ? dataSeries.valuesData()->cbegin()
35 35 : dataSeries.valuesData()->cend())
36 36 {
37 37 }
38 38 IteratorValue(const IteratorValue &other) = default;
39 39
40 40 std::unique_ptr<DataSeriesIteratorValue::Impl> clone() const override
41 41 {
42 42 return std::make_unique<IteratorValue<Dim> >(*this);
43 43 }
44 44
45 45 bool equals(const DataSeriesIteratorValue::Impl &other) const override try {
46 46 const auto &otherImpl = dynamic_cast<const IteratorValue &>(other);
47 47 return std::tie(m_XIt, m_ValuesIt) == std::tie(otherImpl.m_XIt, otherImpl.m_ValuesIt);
48 48 }
49 49 catch (const std::bad_cast &) {
50 50 return false;
51 51 }
52 52
53 53 void next() override
54 54 {
55 55 ++m_XIt;
56 56 ++m_ValuesIt;
57 57 }
58 58
59 59 void prev() override
60 60 {
61 61 --m_XIt;
62 62 --m_ValuesIt;
63 63 }
64 64
65 65 double x() const override { return m_XIt->at(0); }
66 66 double value() const override { return m_ValuesIt->at(0); }
67 67 double value(int componentIndex) const override { return m_ValuesIt->at(componentIndex); }
68 68
69 69 private:
70 70 ArrayData<1>::Iterator m_XIt;
71 71 typename ArrayData<Dim>::Iterator m_ValuesIt;
72 72 };
73 73 } // namespace dataseries_detail
74 74
75 75 /**
76 76 * @brief The DataSeries class is the base (abstract) implementation of IDataSeries.
77 77 *
78 78 * It proposes to set a dimension for the values ​​data.
79 79 *
80 80 * A DataSeries is always sorted on its x-axis data.
81 81 *
82 82 * @tparam Dim The dimension of the values data
83 83 *
84 84 */
85 85 template <int Dim>
86 86 class SCIQLOP_CORE_EXPORT DataSeries : public IDataSeries {
87 87 public:
88 88 /// @sa IDataSeries::xAxisData()
89 89 std::shared_ptr<ArrayData<1> > xAxisData() override { return m_XAxisData; }
90 90 const std::shared_ptr<ArrayData<1> > xAxisData() const { return m_XAxisData; }
91 91
92 92 /// @sa IDataSeries::xAxisUnit()
93 93 Unit xAxisUnit() const override { return m_XAxisUnit; }
94 94
95 95 /// @return the values dataset
96 96 std::shared_ptr<ArrayData<Dim> > valuesData() { return m_ValuesData; }
97 97 const std::shared_ptr<ArrayData<Dim> > valuesData() const { return m_ValuesData; }
98 98
99 99 /// @sa IDataSeries::valuesUnit()
100 100 Unit valuesUnit() const override { return m_ValuesUnit; }
101 101
102 102
103 103 SqpRange range() const override
104 104 {
105 105 if (!m_XAxisData->cdata().isEmpty()) {
106 106 return SqpRange{m_XAxisData->cdata().first(), m_XAxisData->cdata().last()};
107 107 }
108 108
109 109 return SqpRange{};
110 110 }
111 111
112 112 void clear()
113 113 {
114 114 m_XAxisData->clear();
115 115 m_ValuesData->clear();
116 116 }
117 117
118 118 /// Merges into the data series an other data series
119 119 /// @remarks the data series to merge with is cleared after the operation
120 120 void merge(IDataSeries *dataSeries) override
121 121 {
122 122 dataSeries->lockWrite();
123 123 lockWrite();
124 124
125 125 if (auto other = dynamic_cast<DataSeries<Dim> *>(dataSeries)) {
126 126 const auto &otherXAxisData = other->xAxisData()->cdata();
127 127 const auto &xAxisData = m_XAxisData->cdata();
128 128
129 129 // As data series are sorted, we can improve performances of merge, by call the sort
130 130 // method only if the two data series overlap.
131 131 if (!otherXAxisData.empty()) {
132 132 auto firstValue = otherXAxisData.front();
133 133 auto lastValue = otherXAxisData.back();
134 134
135 135 auto xAxisDataBegin = xAxisData.cbegin();
136 136 auto xAxisDataEnd = xAxisData.cend();
137 137
138 138 bool prepend;
139 139 bool sortNeeded;
140 140
141 141 if (std::lower_bound(xAxisDataBegin, xAxisDataEnd, firstValue) == xAxisDataEnd) {
142 142 // Other data series if after data series
143 143 prepend = false;
144 144 sortNeeded = false;
145 145 }
146 146 else if (std::upper_bound(xAxisDataBegin, xAxisDataEnd, lastValue)
147 147 == xAxisDataBegin) {
148 148 // Other data series if before data series
149 149 prepend = true;
150 150 sortNeeded = false;
151 151 }
152 152 else {
153 153 // The two data series overlap
154 154 prepend = false;
155 155 sortNeeded = true;
156 156 }
157 157
158 158 // Makes the merge
159 159 m_XAxisData->add(*other->xAxisData(), prepend);
160 160 m_ValuesData->add(*other->valuesData(), prepend);
161 161
162 162 if (sortNeeded) {
163 163 sort();
164 164 }
165 165 }
166 166
167 167 // Clears the other data series
168 168 other->clear();
169 169 }
170 170 else {
171 171 qCWarning(LOG_DataSeries())
172 172 << QObject::tr("Detection of a type of IDataSeries we cannot merge with !");
173 173 }
174 174 unlock();
175 175 dataSeries->unlock();
176 176 }
177 177
178 178 // ///////// //
179 179 // Iterators //
180 180 // ///////// //
181 181
182 182 DataSeriesIterator cbegin() const override
183 183 {
184 184 return DataSeriesIterator{DataSeriesIteratorValue{
185 185 std::make_unique<dataseries_detail::IteratorValue<Dim> >(*this, true)}};
186 186 }
187 187
188 188 DataSeriesIterator cend() const override
189 189 {
190 190 return DataSeriesIterator{DataSeriesIteratorValue{
191 191 std::make_unique<dataseries_detail::IteratorValue<Dim> >(*this, false)}};
192 192 }
193 193
194 /// @sa IDataSeries::minData()
195 DataSeriesIterator minData(double minXAxisData) const override
194 /// @sa IDataSeries::minXAxisData()
195 DataSeriesIterator minXAxisData(double minXAxisData) const override
196 196 {
197 197 return std::lower_bound(
198 198 cbegin(), cend(), minXAxisData,
199 199 [](const auto &itValue, const auto &value) { return itValue.x() < value; });
200 200 }
201 201
202 /// @sa IDataSeries::maxData()
203 DataSeriesIterator maxData(double maxXAxisData) const override
202 /// @sa IDataSeries::maxXAxisData()
203 DataSeriesIterator maxXAxisData(double maxXAxisData) const override
204 204 {
205 205 // Gets the first element that greater than max value
206 206 auto it = std::upper_bound(
207 207 cbegin(), cend(), maxXAxisData,
208 208 [](const auto &value, const auto &itValue) { return value < itValue.x(); });
209 209
210 210 return it == cbegin() ? cend() : --it;
211 211 }
212 212
213 213 std::pair<DataSeriesIterator, DataSeriesIterator> subData(double min, double max) const override
214 214 {
215 215 if (min > max) {
216 216 std::swap(min, max);
217 217 }
218 218
219 219 auto begin = cbegin();
220 220 auto end = cend();
221 221
222 222 auto lowerIt
223 223 = std::lower_bound(begin, end, min, [](const auto &itValue, const auto &value) {
224 224 return itValue.x() < value;
225 225 });
226 226 auto upperIt
227 227 = std::upper_bound(begin, end, max, [](const auto &value, const auto &itValue) {
228 228 return value < itValue.x();
229 229 });
230 230
231 231 return std::make_pair(lowerIt, upperIt);
232 232 }
233 233
234 234 // /////// //
235 235 // Mutexes //
236 236 // /////// //
237 237
238 238 virtual void lockRead() { m_Lock.lockForRead(); }
239 239 virtual void lockWrite() { m_Lock.lockForWrite(); }
240 240 virtual void unlock() { m_Lock.unlock(); }
241 241
242 242 protected:
243 243 /// Protected ctor (DataSeries is abstract). The vectors must have the same size, otherwise a
244 244 /// DataSeries with no values will be created.
245 245 /// @remarks data series is automatically sorted on its x-axis data
246 246 explicit DataSeries(std::shared_ptr<ArrayData<1> > xAxisData, const Unit &xAxisUnit,
247 247 std::shared_ptr<ArrayData<Dim> > valuesData, const Unit &valuesUnit)
248 248 : m_XAxisData{xAxisData},
249 249 m_XAxisUnit{xAxisUnit},
250 250 m_ValuesData{valuesData},
251 251 m_ValuesUnit{valuesUnit}
252 252 {
253 253 if (m_XAxisData->size() != m_ValuesData->size()) {
254 254 clear();
255 255 }
256 256
257 257 // Sorts data if it's not the case
258 258 const auto &xAxisCData = m_XAxisData->cdata();
259 259 if (!std::is_sorted(xAxisCData.cbegin(), xAxisCData.cend())) {
260 260 sort();
261 261 }
262 262 }
263 263
264 264 /// Copy ctor
265 265 explicit DataSeries(const DataSeries<Dim> &other)
266 266 : m_XAxisData{std::make_shared<ArrayData<1> >(*other.m_XAxisData)},
267 267 m_XAxisUnit{other.m_XAxisUnit},
268 268 m_ValuesData{std::make_shared<ArrayData<Dim> >(*other.m_ValuesData)},
269 269 m_ValuesUnit{other.m_ValuesUnit}
270 270 {
271 271 // Since a series is ordered from its construction and is always ordered, it is not
272 272 // necessary to call the sort method here ('other' is sorted)
273 273 }
274 274
275 275 /// Assignment operator
276 276 template <int D>
277 277 DataSeries &operator=(DataSeries<D> other)
278 278 {
279 279 std::swap(m_XAxisData, other.m_XAxisData);
280 280 std::swap(m_XAxisUnit, other.m_XAxisUnit);
281 281 std::swap(m_ValuesData, other.m_ValuesData);
282 282 std::swap(m_ValuesUnit, other.m_ValuesUnit);
283 283
284 284 return *this;
285 285 }
286 286
287 287 private:
288 288 /**
289 289 * Sorts data series on its x-axis data
290 290 */
291 291 void sort() noexcept
292 292 {
293 293 auto permutation = SortUtils::sortPermutation(*m_XAxisData, std::less<double>());
294 294 m_XAxisData = m_XAxisData->sort(permutation);
295 295 m_ValuesData = m_ValuesData->sort(permutation);
296 296 }
297 297
298 298 std::shared_ptr<ArrayData<1> > m_XAxisData;
299 299 Unit m_XAxisUnit;
300 300 std::shared_ptr<ArrayData<Dim> > m_ValuesData;
301 301 Unit m_ValuesUnit;
302 302
303 303 QReadWriteLock m_Lock;
304 304 };
305 305
306 306 #endif // SCIQLOP_DATASERIES_H
@@ -1,96 +1,96
1 1 #ifndef SCIQLOP_IDATASERIES_H
2 2 #define SCIQLOP_IDATASERIES_H
3 3
4 4 #include <Common/MetaTypes.h>
5 5 #include <Data/DataSeriesIterator.h>
6 6 #include <Data/SqpRange.h>
7 7
8 8 #include <memory>
9 9
10 10 #include <QString>
11 11
12 12 template <int Dim>
13 13 class ArrayData;
14 14
15 15 struct Unit {
16 16 explicit Unit(const QString &name = {}, bool timeUnit = false)
17 17 : m_Name{name}, m_TimeUnit{timeUnit}
18 18 {
19 19 }
20 20
21 21 inline bool operator==(const Unit &other) const
22 22 {
23 23 return std::tie(m_Name, m_TimeUnit) == std::tie(other.m_Name, other.m_TimeUnit);
24 24 }
25 25 inline bool operator!=(const Unit &other) const { return !(*this == other); }
26 26
27 27 QString m_Name; ///< Unit name
28 28 bool m_TimeUnit; ///< The unit is a unit of time (UTC)
29 29 };
30 30
31 31 /**
32 32 * @brief The IDataSeries aims to declare a data series.
33 33 *
34 34 * A data series is an entity that contains at least :
35 35 * - one dataset representing the x-axis
36 36 * - one dataset representing the values
37 37 *
38 38 * Each dataset is represented by an ArrayData, and is associated with a unit.
39 39 *
40 40 * An ArrayData can be unidimensional or two-dimensional, depending on the implementation of the
41 41 * IDataSeries. The x-axis dataset is always unidimensional.
42 42 *
43 43 * @sa ArrayData
44 44 */
45 45 class IDataSeries {
46 46 public:
47 47 virtual ~IDataSeries() noexcept = default;
48 48
49 49 /// Returns the x-axis dataset
50 50 virtual std::shared_ptr<ArrayData<1> > xAxisData() = 0;
51 51
52 52 /// Returns the x-axis dataset (as const)
53 53 virtual const std::shared_ptr<ArrayData<1> > xAxisData() const = 0;
54 54
55 55 virtual Unit xAxisUnit() const = 0;
56 56
57 57 virtual Unit valuesUnit() const = 0;
58 58
59 59 virtual void merge(IDataSeries *dataSeries) = 0;
60 60 /// @todo Review the name and signature of this method
61 61 virtual std::shared_ptr<IDataSeries> subDataSeries(const SqpRange &range) = 0;
62 62
63 63 virtual std::unique_ptr<IDataSeries> clone() const = 0;
64 64 virtual SqpRange range() const = 0;
65 65
66 66 // ///////// //
67 67 // Iterators //
68 68 // ///////// //
69 69
70 70 virtual DataSeriesIterator cbegin() const = 0;
71 71 virtual DataSeriesIterator cend() const = 0;
72 72
73 73 /// @return the iterator to the first entry of the data series whose x-axis data is greater than
74 74 /// or equal to the value passed in parameter, or the end iterator if there is no matching value
75 virtual DataSeriesIterator minData(double minXAxisData) const = 0;
75 virtual DataSeriesIterator minXAxisData(double minXAxisData) const = 0;
76 76
77 77 /// @return the iterator to the last entry of the data series whose x-axis data is less than or
78 78 /// equal to the value passed in parameter, or the end iterator if there is no matching value
79 virtual DataSeriesIterator maxData(double maxXAxisData) const = 0;
79 virtual DataSeriesIterator maxXAxisData(double maxXAxisData) const = 0;
80 80
81 81 virtual std::pair<DataSeriesIterator, DataSeriesIterator> subData(double min,
82 82 double max) const = 0;
83 83
84 84 // /////// //
85 85 // Mutexes //
86 86 // /////// //
87 87
88 88 virtual void lockRead() = 0;
89 89 virtual void lockWrite() = 0;
90 90 virtual void unlock() = 0;
91 91 };
92 92
93 93 // Required for using shared_ptr in signals/slots
94 94 SCIQLOP_REGISTER_META_TYPE(IDATASERIES_PTR_REGISTRY, std::shared_ptr<IDataSeries>)
95 95
96 96 #endif // SCIQLOP_IDATASERIES_H
@@ -1,271 +1,271
1 1 #include <Variable/Variable.h>
2 2 #include <Variable/VariableModel.h>
3 3
4 4 #include <Common/DateUtils.h>
5 5
6 6 #include <Data/IDataSeries.h>
7 7
8 8 #include <QSize>
9 9 #include <unordered_map>
10 10
11 11 Q_LOGGING_CATEGORY(LOG_VariableModel, "VariableModel")
12 12
13 13 namespace {
14 14
15 15 // Column indexes
16 16 const auto NAME_COLUMN = 0;
17 17 const auto TSTART_COLUMN = 1;
18 18 const auto TEND_COLUMN = 2;
19 19 const auto UNIT_COLUMN = 3;
20 20 const auto MISSION_COLUMN = 4;
21 21 const auto PLUGIN_COLUMN = 5;
22 22 const auto NB_COLUMNS = 6;
23 23
24 24 // Column properties
25 25 const auto DEFAULT_HEIGHT = 25;
26 26 const auto DEFAULT_WIDTH = 100;
27 27
28 28 struct ColumnProperties {
29 29 ColumnProperties(const QString &name = {}, int width = DEFAULT_WIDTH,
30 30 int height = DEFAULT_HEIGHT)
31 31 : m_Name{name}, m_Width{width}, m_Height{height}
32 32 {
33 33 }
34 34
35 35 QString m_Name;
36 36 int m_Width;
37 37 int m_Height;
38 38 };
39 39
40 40 const auto COLUMN_PROPERTIES = QHash<int, ColumnProperties>{
41 41 {NAME_COLUMN, {QObject::tr("Name")}}, {TSTART_COLUMN, {QObject::tr("tStart"), 180}},
42 42 {TEND_COLUMN, {QObject::tr("tEnd"), 180}}, {UNIT_COLUMN, {QObject::tr("Unit")}},
43 43 {MISSION_COLUMN, {QObject::tr("Mission")}}, {PLUGIN_COLUMN, {QObject::tr("Plugin")}}};
44 44
45 45 /// Format for datetimes
46 46 const auto DATETIME_FORMAT = QStringLiteral("dd/MM/yyyy \nhh:mm:ss:zzz");
47 47
48 48
49 49 } // namespace
50 50
51 51 struct VariableModel::VariableModelPrivate {
52 52 /// Variables created in SciQlop
53 53 std::vector<std::shared_ptr<Variable> > m_Variables;
54 54 std::unordered_map<std::shared_ptr<Variable>, double> m_VariableToProgress;
55 55
56 56 /// Return the row index of the variable. -1 if it's not found
57 57 int indexOfVariable(Variable *variable) const noexcept;
58 58 };
59 59
60 60 VariableModel::VariableModel(QObject *parent)
61 61 : QAbstractTableModel{parent}, impl{spimpl::make_unique_impl<VariableModelPrivate>()}
62 62 {
63 63 }
64 64
65 65 std::shared_ptr<Variable> VariableModel::createVariable(const QString &name,
66 66 const SqpRange &dateTime,
67 67 const QVariantHash &metadata) noexcept
68 68 {
69 69 auto insertIndex = rowCount();
70 70 beginInsertRows({}, insertIndex, insertIndex);
71 71
72 72 auto variable = std::make_shared<Variable>(name, dateTime, metadata);
73 73
74 74 impl->m_Variables.push_back(variable);
75 75 connect(variable.get(), &Variable::updated, this, &VariableModel::onVariableUpdated);
76 76
77 77 endInsertRows();
78 78
79 79 return variable;
80 80 }
81 81
82 82 void VariableModel::deleteVariable(std::shared_ptr<Variable> variable) noexcept
83 83 {
84 84 if (!variable) {
85 85 qCCritical(LOG_Variable()) << "Can't delete a null variable from the model";
86 86 return;
87 87 }
88 88
89 89 // Finds variable in the model
90 90 auto begin = impl->m_Variables.cbegin();
91 91 auto end = impl->m_Variables.cend();
92 92 auto it = std::find(begin, end, variable);
93 93 if (it != end) {
94 94 auto removeIndex = std::distance(begin, it);
95 95
96 96 // Deletes variable
97 97 beginRemoveRows({}, removeIndex, removeIndex);
98 98 impl->m_Variables.erase(it);
99 99 endRemoveRows();
100 100 }
101 101 else {
102 102 qCritical(LOG_VariableModel())
103 103 << tr("Can't delete variable %1 from the model: the variable is not in the model")
104 104 .arg(variable->name());
105 105 }
106 106
107 107 // Removes variable from progress map
108 108 impl->m_VariableToProgress.erase(variable);
109 109 }
110 110
111 111
112 112 std::shared_ptr<Variable> VariableModel::variable(int index) const
113 113 {
114 114 return (index >= 0 && index < impl->m_Variables.size()) ? impl->m_Variables[index] : nullptr;
115 115 }
116 116
117 117 void VariableModel::setDataProgress(std::shared_ptr<Variable> variable, double progress)
118 118 {
119 119 if (progress > 0.0) {
120 120 impl->m_VariableToProgress[variable] = progress;
121 121 }
122 122 else {
123 123 impl->m_VariableToProgress.erase(variable);
124 124 }
125 125 auto modelIndex = createIndex(impl->indexOfVariable(variable.get()), NAME_COLUMN);
126 126
127 127 emit dataChanged(modelIndex, modelIndex);
128 128 }
129 129
130 130 int VariableModel::columnCount(const QModelIndex &parent) const
131 131 {
132 132 Q_UNUSED(parent);
133 133
134 134 return NB_COLUMNS;
135 135 }
136 136
137 137 int VariableModel::rowCount(const QModelIndex &parent) const
138 138 {
139 139 Q_UNUSED(parent);
140 140
141 141 return impl->m_Variables.size();
142 142 }
143 143
144 144 QVariant VariableModel::data(const QModelIndex &index, int role) const
145 145 {
146 146 if (!index.isValid()) {
147 147 return QVariant{};
148 148 }
149 149
150 150 if (index.row() < 0 || index.row() >= rowCount()) {
151 151 return QVariant{};
152 152 }
153 153
154 154 if (role == Qt::DisplayRole) {
155 155 if (auto variable = impl->m_Variables.at(index.row()).get()) {
156 156 /// Lambda function that builds the variant to return for a time value
157 157 /// @param getValueFun function used to get for a data series the iterator on the entry
158 158 /// that contains the time value to display
159 159 auto dateTimeVariant = [variable](const auto &getValueFun) {
160 160 if (auto dataSeries = variable->dataSeries()) {
161 161 auto it = getValueFun(*dataSeries);
162 162 return (it != dataSeries->cend())
163 163 ? DateUtils::dateTime(it->x()).toString(DATETIME_FORMAT)
164 164 : QVariant{};
165 165 }
166 166 else {
167 167 return QVariant{};
168 168 }
169 169 };
170 170
171 171 switch (index.column()) {
172 172 case NAME_COLUMN:
173 173 return variable->name();
174 174 case TSTART_COLUMN:
175 175 // Shows the min value of the data series above the range tstart
176 176 return dateTimeVariant([min = variable->range().m_TStart](
177 const auto &dataSeries) { return dataSeries.minData(min); });
177 const auto &dataSeries) { return dataSeries.minXAxisData(min); });
178 178 case TEND_COLUMN:
179 179 // Shows the max value of the data series under the range tend
180 180 return dateTimeVariant([max = variable->range().m_TEnd](
181 const auto &dataSeries) { return dataSeries.maxData(max); });
181 const auto &dataSeries) { return dataSeries.maxXAxisData(max); });
182 182 case UNIT_COLUMN:
183 183 return variable->metadata().value(QStringLiteral("units"));
184 184 case MISSION_COLUMN:
185 185 return variable->metadata().value(QStringLiteral("mission"));
186 186 case PLUGIN_COLUMN:
187 187 return variable->metadata().value(QStringLiteral("plugin"));
188 188 default:
189 189 // No action
190 190 break;
191 191 }
192 192
193 193 qWarning(LOG_VariableModel())
194 194 << tr("Can't get data (unknown column %1)").arg(index.column());
195 195 }
196 196 else {
197 197 qWarning(LOG_VariableModel()) << tr("Can't get data (no variable)");
198 198 }
199 199 }
200 200 else if (role == VariableRoles::ProgressRole) {
201 201 if (auto variable = impl->m_Variables.at(index.row())) {
202 202
203 203 auto it = impl->m_VariableToProgress.find(variable);
204 204 if (it != impl->m_VariableToProgress.cend()) {
205 205 return it->second;
206 206 }
207 207 }
208 208 }
209 209
210 210 return QVariant{};
211 211 }
212 212
213 213 QVariant VariableModel::headerData(int section, Qt::Orientation orientation, int role) const
214 214 {
215 215 if (role != Qt::DisplayRole && role != Qt::SizeHintRole) {
216 216 return QVariant{};
217 217 }
218 218
219 219 if (orientation == Qt::Horizontal) {
220 220 auto propertiesIt = COLUMN_PROPERTIES.find(section);
221 221 if (propertiesIt != COLUMN_PROPERTIES.cend()) {
222 222 // Role is either DisplayRole or SizeHintRole
223 223 return (role == Qt::DisplayRole)
224 224 ? QVariant{propertiesIt->m_Name}
225 225 : QVariant{QSize{propertiesIt->m_Width, propertiesIt->m_Height}};
226 226 }
227 227 else {
228 228 qWarning(LOG_VariableModel())
229 229 << tr("Can't get header data (unknown column %1)").arg(section);
230 230 }
231 231 }
232 232
233 233 return QVariant{};
234 234 }
235 235
236 236 void VariableModel::abortProgress(const QModelIndex &index)
237 237 {
238 238 if (auto variable = impl->m_Variables.at(index.row())) {
239 239 emit abortProgessRequested(variable);
240 240 }
241 241 }
242 242
243 243 void VariableModel::onVariableUpdated() noexcept
244 244 {
245 245 // Finds variable that has been updated in the model
246 246 if (auto updatedVariable = dynamic_cast<Variable *>(sender())) {
247 247 auto updatedVariableIndex = impl->indexOfVariable(updatedVariable);
248 248
249 249 if (updatedVariableIndex > -1) {
250 250 emit dataChanged(createIndex(updatedVariableIndex, 0),
251 251 createIndex(updatedVariableIndex, columnCount() - 1));
252 252 }
253 253 }
254 254 }
255 255
256 256 int VariableModel::VariableModelPrivate::indexOfVariable(Variable *variable) const noexcept
257 257 {
258 258 auto begin = std::cbegin(m_Variables);
259 259 auto end = std::cend(m_Variables);
260 260 auto it
261 261 = std::find_if(begin, end, [variable](const auto &var) { return var.get() == variable; });
262 262
263 263 if (it != end) {
264 264 // Gets the index of the variable in the model: we assume here that views have the same
265 265 // order as the model
266 266 return std::distance(begin, it);
267 267 }
268 268 else {
269 269 return -1;
270 270 }
271 271 }
@@ -1,354 +1,354
1 1 #include "Data/DataSeries.h"
2 2 #include "Data/ScalarSeries.h"
3 3
4 4 #include <QObject>
5 5 #include <QtTest>
6 6
7 7 Q_DECLARE_METATYPE(std::shared_ptr<ScalarSeries>)
8 8
9 9 class TestDataSeries : public QObject {
10 10 Q_OBJECT
11 11 private slots:
12 12 /// Input test data
13 13 /// @sa testCtor()
14 14 void testCtor_data();
15 15
16 16 /// Tests construction of a data series
17 17 void testCtor();
18 18
19 19 /// Input test data
20 20 /// @sa testMerge()
21 21 void testMerge_data();
22 22
23 23 /// Tests merge of two data series
24 24 void testMerge();
25 25
26 26 /// Input test data
27 /// @sa testMinData()
28 void testMinData_data();
27 /// @sa testMinXAxisData()
28 void testMinXAxisData_data();
29 29
30 /// Tests get min data of a data series
31 void testMinData();
30 /// Tests get min x-axis data of a data series
31 void testMinXAxisData();
32 32
33 33 /// Input test data
34 /// @sa testMaxData()
35 void testMaxData_data();
34 /// @sa testMaxXAxisData()
35 void testMaxXAxisData_data();
36 36
37 /// Tests get max data of a data series
38 void testMaxData();
37 /// Tests get max x-axis data of a data series
38 void testMaxXAxisData();
39 39
40 40 /// Input test data
41 41 /// @sa testSubdata()
42 42 void testSubdata_data();
43 43
44 44 /// Tests get subdata of two data series
45 45 void testSubdata();
46 46 };
47 47
48 48 void TestDataSeries::testCtor_data()
49 49 {
50 50 // ////////////// //
51 51 // Test structure //
52 52 // ////////////// //
53 53
54 54 // x-axis data
55 55 QTest::addColumn<QVector<double> >("xAxisData");
56 56 // values data
57 57 QTest::addColumn<QVector<double> >("valuesData");
58 58
59 59 // expected x-axis data
60 60 QTest::addColumn<QVector<double> >("expectedXAxisData");
61 61 // expected values data
62 62 QTest::addColumn<QVector<double> >("expectedValuesData");
63 63
64 64 // ////////// //
65 65 // Test cases //
66 66 // ////////// //
67 67
68 68 QTest::newRow("invalidData (different sizes of vectors)")
69 69 << QVector<double>{1., 2., 3., 4., 5.} << QVector<double>{100., 200., 300.}
70 70 << QVector<double>{} << QVector<double>{};
71 71
72 72 QTest::newRow("sortedData") << QVector<double>{1., 2., 3., 4., 5.}
73 73 << QVector<double>{100., 200., 300., 400., 500.}
74 74 << QVector<double>{1., 2., 3., 4., 5.}
75 75 << QVector<double>{100., 200., 300., 400., 500.};
76 76
77 77 QTest::newRow("unsortedData") << QVector<double>{5., 4., 3., 2., 1.}
78 78 << QVector<double>{100., 200., 300., 400., 500.}
79 79 << QVector<double>{1., 2., 3., 4., 5.}
80 80 << QVector<double>{500., 400., 300., 200., 100.};
81 81
82 82 QTest::newRow("unsortedData2")
83 83 << QVector<double>{1., 4., 3., 5., 2.} << QVector<double>{100., 200., 300., 400., 500.}
84 84 << QVector<double>{1., 2., 3., 4., 5.} << QVector<double>{100., 500., 300., 200., 400.};
85 85 }
86 86
87 87 void TestDataSeries::testCtor()
88 88 {
89 89 // Creates series
90 90 QFETCH(QVector<double>, xAxisData);
91 91 QFETCH(QVector<double>, valuesData);
92 92
93 93 auto series = std::make_shared<ScalarSeries>(std::move(xAxisData), std::move(valuesData),
94 94 Unit{}, Unit{});
95 95
96 96 // Validates results : we check that the data series is sorted on its x-axis data
97 97 QFETCH(QVector<double>, expectedXAxisData);
98 98 QFETCH(QVector<double>, expectedValuesData);
99 99
100 100 auto seriesXAxisData = series->xAxisData()->data();
101 101 auto seriesValuesData = series->valuesData()->data();
102 102
103 103 QVERIFY(
104 104 std::equal(expectedXAxisData.cbegin(), expectedXAxisData.cend(), seriesXAxisData.cbegin()));
105 105 QVERIFY(std::equal(expectedValuesData.cbegin(), expectedValuesData.cend(),
106 106 seriesValuesData.cbegin()));
107 107 }
108 108
109 109 namespace {
110 110
111 111 std::shared_ptr<ScalarSeries> createSeries(QVector<double> xAxisData, QVector<double> valuesData)
112 112 {
113 113 return std::make_shared<ScalarSeries>(std::move(xAxisData), std::move(valuesData), Unit{},
114 114 Unit{});
115 115 }
116 116
117 117 } // namespace
118 118
119 119 void TestDataSeries::testMerge_data()
120 120 {
121 121 // ////////////// //
122 122 // Test structure //
123 123 // ////////////// //
124 124
125 125 // Data series to merge
126 126 QTest::addColumn<std::shared_ptr<ScalarSeries> >("dataSeries");
127 127 QTest::addColumn<std::shared_ptr<ScalarSeries> >("dataSeries2");
128 128
129 129 // Expected values in the first data series after merge
130 130 QTest::addColumn<QVector<double> >("expectedXAxisData");
131 131 QTest::addColumn<QVector<double> >("expectedValuesData");
132 132
133 133 // ////////// //
134 134 // Test cases //
135 135 // ////////// //
136 136
137 137 QTest::newRow("sortedMerge")
138 138 << createSeries({1., 2., 3., 4., 5.}, {100., 200., 300., 400., 500.})
139 139 << createSeries({6., 7., 8., 9., 10.}, {600., 700., 800., 900., 1000.})
140 140 << QVector<double>{1., 2., 3., 4., 5., 6., 7., 8., 9., 10.}
141 141 << QVector<double>{100., 200., 300., 400., 500., 600., 700., 800., 900., 1000.};
142 142
143 143 QTest::newRow("unsortedMerge")
144 144 << createSeries({6., 7., 8., 9., 10.}, {600., 700., 800., 900., 1000.})
145 145 << createSeries({1., 2., 3., 4., 5.}, {100., 200., 300., 400., 500.})
146 146 << QVector<double>{1., 2., 3., 4., 5., 6., 7., 8., 9., 10.}
147 147 << QVector<double>{100., 200., 300., 400., 500., 600., 700., 800., 900., 1000.};
148 148
149 149 QTest::newRow("unsortedMerge2")
150 150 << createSeries({1., 2., 8., 9., 10}, {100., 200., 300., 400., 500.})
151 151 << createSeries({3., 4., 5., 6., 7.}, {600., 700., 800., 900., 1000.})
152 152 << QVector<double>{1., 2., 3., 4., 5., 6., 7., 8., 9., 10.}
153 153 << QVector<double>{100., 200., 600., 700., 800., 900., 1000., 300., 400., 500.};
154 154
155 155 QTest::newRow("unsortedMerge3")
156 156 << createSeries({3., 5., 8., 7., 2}, {100., 200., 300., 400., 500.})
157 157 << createSeries({6., 4., 9., 10., 1.}, {600., 700., 800., 900., 1000.})
158 158 << QVector<double>{1., 2., 3., 4., 5., 6., 7., 8., 9., 10.}
159 159 << QVector<double>{1000., 500., 100., 700., 200., 600., 400., 300., 800., 900.};
160 160 }
161 161
162 162 void TestDataSeries::testMerge()
163 163 {
164 164 // Merges series
165 165 QFETCH(std::shared_ptr<ScalarSeries>, dataSeries);
166 166 QFETCH(std::shared_ptr<ScalarSeries>, dataSeries2);
167 167
168 168 dataSeries->merge(dataSeries2.get());
169 169
170 170 // Validates results : we check that the merge is valid and the data series is sorted on its
171 171 // x-axis data
172 172 QFETCH(QVector<double>, expectedXAxisData);
173 173 QFETCH(QVector<double>, expectedValuesData);
174 174
175 175 auto seriesXAxisData = dataSeries->xAxisData()->data();
176 176 auto seriesValuesData = dataSeries->valuesData()->data();
177 177
178 178 QVERIFY(
179 179 std::equal(expectedXAxisData.cbegin(), expectedXAxisData.cend(), seriesXAxisData.cbegin()));
180 180 QVERIFY(std::equal(expectedValuesData.cbegin(), expectedValuesData.cend(),
181 181 seriesValuesData.cbegin()));
182 182 }
183 183
184 void TestDataSeries::testMinData_data()
184 void TestDataSeries::testMinXAxisData_data()
185 185 {
186 186 // ////////////// //
187 187 // Test structure //
188 188 // ////////////// //
189 189
190 190 // Data series to get min data
191 191 QTest::addColumn<std::shared_ptr<ScalarSeries> >("dataSeries");
192 192
193 193 // Min data
194 194 QTest::addColumn<double>("min");
195 195
196 196 // Expected results
197 197 QTest::addColumn<bool>(
198 198 "expectedOK"); // if true, expects to have a result (i.e. the iterator != end iterator)
199 199 QTest::addColumn<double>(
200 200 "expectedMin"); // Expected value when method doesn't return end iterator
201 201
202 202 // ////////// //
203 203 // Test cases //
204 204 // ////////// //
205 205
206 206 QTest::newRow("minData1") << createSeries({1., 2., 3., 4., 5.}, {100., 200., 300., 400., 500.})
207 207 << 0. << true << 1.;
208 208 QTest::newRow("minData2") << createSeries({1., 2., 3., 4., 5.}, {100., 200., 300., 400., 500.})
209 209 << 1. << true << 1.;
210 210 QTest::newRow("minData3") << createSeries({1., 2., 3., 4., 5.}, {100., 200., 300., 400., 500.})
211 211 << 1.1 << true << 2.;
212 212 QTest::newRow("minData4") << createSeries({1., 2., 3., 4., 5.}, {100., 200., 300., 400., 500.})
213 213 << 5. << true << 5.;
214 214 QTest::newRow("minData5") << createSeries({1., 2., 3., 4., 5.}, {100., 200., 300., 400., 500.})
215 215 << 5.1 << false << std::numeric_limits<double>::quiet_NaN();
216 216 QTest::newRow("minData6") << createSeries({}, {}) << 1.1 << false
217 217 << std::numeric_limits<double>::quiet_NaN();
218 218 }
219 219
220 void TestDataSeries::testMinData()
220 void TestDataSeries::testMinXAxisData()
221 221 {
222 222 QFETCH(std::shared_ptr<ScalarSeries>, dataSeries);
223 223 QFETCH(double, min);
224 224
225 225 QFETCH(bool, expectedOK);
226 226 QFETCH(double, expectedMin);
227 227
228 auto it = dataSeries->minData(min);
228 auto it = dataSeries->minXAxisData(min);
229 229
230 230 QCOMPARE(expectedOK, it != dataSeries->cend());
231 231
232 232 // If the method doesn't return a end iterator, checks with expected value
233 233 if (expectedOK) {
234 234 QCOMPARE(expectedMin, it->x());
235 235 }
236 236 }
237 237
238 void TestDataSeries::testMaxData_data()
238 void TestDataSeries::testMaxXAxisData_data()
239 239 {
240 240 // ////////////// //
241 241 // Test structure //
242 242 // ////////////// //
243 243
244 244 // Data series to get max data
245 245 QTest::addColumn<std::shared_ptr<ScalarSeries> >("dataSeries");
246 246
247 247 // Max data
248 248 QTest::addColumn<double>("max");
249 249
250 250 // Expected results
251 251 QTest::addColumn<bool>(
252 252 "expectedOK"); // if true, expects to have a result (i.e. the iterator != end iterator)
253 253 QTest::addColumn<double>(
254 254 "expectedMax"); // Expected value when method doesn't return end iterator
255 255
256 256 // ////////// //
257 257 // Test cases //
258 258 // ////////// //
259 259
260 260 QTest::newRow("maxData1") << createSeries({1., 2., 3., 4., 5.}, {100., 200., 300., 400., 500.})
261 261 << 6. << true << 5.;
262 262 QTest::newRow("maxData2") << createSeries({1., 2., 3., 4., 5.}, {100., 200., 300., 400., 500.})
263 263 << 5. << true << 5.;
264 264 QTest::newRow("maxData3") << createSeries({1., 2., 3., 4., 5.}, {100., 200., 300., 400., 500.})
265 265 << 4.9 << true << 4.;
266 266 QTest::newRow("maxData4") << createSeries({1., 2., 3., 4., 5.}, {100., 200., 300., 400., 500.})
267 267 << 1.1 << true << 1.;
268 268 QTest::newRow("maxData5") << createSeries({1., 2., 3., 4., 5.}, {100., 200., 300., 400., 500.})
269 269 << 1. << true << 1.;
270 270 QTest::newRow("maxData6") << createSeries({}, {}) << 1.1 << false
271 271 << std::numeric_limits<double>::quiet_NaN();
272 272 }
273 273
274 void TestDataSeries::testMaxData()
274 void TestDataSeries::testMaxXAxisData()
275 275 {
276 276 QFETCH(std::shared_ptr<ScalarSeries>, dataSeries);
277 277 QFETCH(double, max);
278 278
279 279 QFETCH(bool, expectedOK);
280 280 QFETCH(double, expectedMax);
281 281
282 auto it = dataSeries->maxData(max);
282 auto it = dataSeries->maxXAxisData(max);
283 283
284 284 QCOMPARE(expectedOK, it != dataSeries->cend());
285 285
286 286 // If the method doesn't return a end iterator, checks with expected value
287 287 if (expectedOK) {
288 288 QCOMPARE(expectedMax, it->x());
289 289 }
290 290 }
291 291
292 292 void TestDataSeries::testSubdata_data()
293 293 {
294 294 // ////////////// //
295 295 // Test structure //
296 296 // ////////////// //
297 297
298 298 // Data series to get subdata
299 299 QTest::addColumn<std::shared_ptr<ScalarSeries> >("dataSeries");
300 300
301 301 // Min/max values
302 302 QTest::addColumn<double>("min");
303 303 QTest::addColumn<double>("max");
304 304
305 305 // Expected values after subdata
306 306 QTest::addColumn<QVector<double> >("expectedXAxisData");
307 307 QTest::addColumn<QVector<double> >("expectedValuesData");
308 308
309 309 // ////////// //
310 310 // Test cases //
311 311 // ////////// //
312 312
313 313 QTest::newRow("subData1") << createSeries({1., 2., 3., 4., 5.}, {100., 200., 300., 400., 500.})
314 314 << -1. << 3.2 << QVector<double>{1., 2., 3.}
315 315 << QVector<double>{100., 200., 300.};
316 316 QTest::newRow("subData2") << createSeries({1., 2., 3., 4., 5.}, {100., 200., 300., 400., 500.})
317 317 << 1. << 4. << QVector<double>{1., 2., 3., 4.}
318 318 << QVector<double>{100., 200., 300., 400.};
319 319 QTest::newRow("subData3") << createSeries({1., 2., 3., 4., 5.}, {100., 200., 300., 400., 500.})
320 320 << 1. << 3.9 << QVector<double>{1., 2., 3.}
321 321 << QVector<double>{100., 200., 300.};
322 322 QTest::newRow("subData4") << createSeries({1., 2., 3., 4., 5.}, {100., 200., 300., 400., 500.})
323 323 << 0. << 0.9 << QVector<double>{} << QVector<double>{};
324 324 QTest::newRow("subData5") << createSeries({1., 2., 3., 4., 5.}, {100., 200., 300., 400., 500.})
325 325 << 0. << 1. << QVector<double>{1.} << QVector<double>{100.};
326 326 QTest::newRow("subData6") << createSeries({1., 2., 3., 4., 5.}, {100., 200., 300., 400., 500.})
327 327 << 2.1 << 6. << QVector<double>{3., 4., 5.}
328 328 << QVector<double>{300., 400., 500.};
329 329 QTest::newRow("subData7") << createSeries({1., 2., 3., 4., 5.}, {100., 200., 300., 400., 500.})
330 330 << 6. << 9. << QVector<double>{} << QVector<double>{};
331 331 QTest::newRow("subData8") << createSeries({1., 2., 3., 4., 5.}, {100., 200., 300., 400., 500.})
332 332 << 5. << 9. << QVector<double>{5.} << QVector<double>{500.};
333 333 }
334 334
335 335 void TestDataSeries::testSubdata()
336 336 {
337 337 QFETCH(std::shared_ptr<ScalarSeries>, dataSeries);
338 338 QFETCH(double, min);
339 339 QFETCH(double, max);
340 340
341 341 QFETCH(QVector<double>, expectedXAxisData);
342 342 QFETCH(QVector<double>, expectedValuesData);
343 343
344 344 auto bounds = dataSeries->subData(min, max);
345 345 QVERIFY(std::equal(bounds.first, bounds.second, expectedXAxisData.cbegin(),
346 346 expectedXAxisData.cend(),
347 347 [](const auto &it, const auto &expectedX) { return it.x() == expectedX; }));
348 348 QVERIFY(std::equal(
349 349 bounds.first, bounds.second, expectedValuesData.cbegin(), expectedValuesData.cend(),
350 350 [](const auto &it, const auto &expectedVal) { return it.value() == expectedVal; }));
351 351 }
352 352
353 353 QTEST_MAIN(TestDataSeries)
354 354 #include "TestDataSeries.moc"
General Comments 0
You need to be logged in to leave comments. Login now