##// END OF EJS Templates
Review fixes
Alexandre Leroux -
r372:c0501a972eb4
parent child
Show More
@@ -1,108 +1,105
1 1 #ifndef SCIQLOP_ARRAYDATA_H
2 2 #define SCIQLOP_ARRAYDATA_H
3 3
4 4 #include <QReadLocker>
5 5 #include <QReadWriteLock>
6 6 #include <QVector>
7 7 /**
8 8 * @brief The ArrayData class represents a dataset for a data series.
9 9 *
10 10 * A dataset can be unidimensional or two-dimensional. This property is determined by the Dim
11 11 * template-parameter.
12 12 *
13 13 * @tparam Dim the dimension of the ArrayData (one or two)
14 14 * @sa IDataSeries
15 15 */
16 16 template <int Dim>
17 17 class ArrayData {
18 18 public:
19 19 /**
20 20 * Ctor for a unidimensional ArrayData
21 21 * @param nbColumns the number of values the ArrayData will hold
22 22 */
23 23 template <int D = Dim, typename = std::enable_if_t<D == 1> >
24 24 explicit ArrayData(int nbColumns) : m_Data{1, QVector<double>{}}
25 25 {
26 QWriteLocker locker(&m_Lock);
26 QWriteLocker locker{&m_Lock};
27 27 m_Data[0].resize(nbColumns);
28 28 }
29 29
30 /**
31 * Ctor for a unidimensional ArrayData
32 * @param nbColumns the number of values the ArrayData will hold
33 */
30 /// Copy ctor
34 31 explicit ArrayData(const ArrayData &other)
35 32 {
36 QReadLocker otherLocker(&other.m_Lock);
37 QWriteLocker locker(&m_Lock);
33 QReadLocker otherLocker{&other.m_Lock};
34 QWriteLocker locker{&m_Lock};
38 35 m_Data = other.m_Data;
39 36 }
40 37
41 38 /**
42 39 * Sets a data at a specified index. The index has to be valid to be effective
43 40 * @param index the index to which the data will be set
44 41 * @param data the data to set
45 42 * @remarks this method is only available for a unidimensional ArrayData
46 43 */
47 44 template <int D = Dim, typename = std::enable_if_t<D == 1> >
48 45 void setData(int index, double data) noexcept
49 46 {
50 QWriteLocker locker(&m_Lock);
47 QWriteLocker locker{&m_Lock};
51 48 if (index >= 0 && index < m_Data.at(0).size()) {
52 49 m_Data[0].replace(index, data);
53 50 }
54 51 }
55 52
56 53 /**
57 54 * @return the data as a vector
58 55 * @remarks this method is only available for a unidimensional ArrayData
59 56 */
60 57 template <int D = Dim, typename = std::enable_if_t<D == 1> >
61 58 QVector<double> data() const noexcept
62 59 {
63 QReadLocker locker(&m_Lock);
60 QReadLocker locker{&m_Lock};
64 61 return m_Data[0];
65 62 }
66 63
67 64 /**
68 65 * @return the data as a vector
69 66 * @remarks this method is only available for a unidimensional ArrayData
70 67 */
71 68 template <int D = Dim, typename = std::enable_if_t<D == 1> >
72 69 QVector<double> data(double tStart, double tEnd) const noexcept
73 70 {
74 QReadLocker locker(&m_Lock);
71 QReadLocker locker{&m_Lock};
75 72 return m_Data.at(tStart);
76 73 }
77 74
78 75 // TODO Comment
79 76 template <int D = Dim, typename = std::enable_if_t<D == 1> >
80 77 void merge(const ArrayData<1> &arrayData)
81 78 {
82 QWriteLocker locker(&m_Lock);
79 QWriteLocker locker{&m_Lock};
83 80 if (!m_Data.empty()) {
84 QReadLocker otherLocker(&arrayData.m_Lock);
81 QReadLocker otherLocker{&arrayData.m_Lock};
85 82 m_Data[0] += arrayData.data();
86 83 }
87 84 }
88 85
89 86 template <int D = Dim, typename = std::enable_if_t<D == 1> >
90 87 int size() const
91 88 {
92 QReadLocker locker(&m_Lock);
89 QReadLocker locker{&m_Lock};
93 90 return m_Data[0].size();
94 91 }
95 92
96 93 void clear()
97 94 {
98 QWriteLocker locker(&m_Lock);
95 QWriteLocker locker{&m_Lock};
99 96 m_Data.clear();
100 97 }
101 98
102 99
103 100 private:
104 101 QVector<QVector<double> > m_Data;
105 102 mutable QReadWriteLock m_Lock;
106 103 };
107 104
108 105 #endif // SCIQLOP_ARRAYDATA_H
@@ -1,198 +1,200
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> VariableModel::createVariable(const QString &name,
56 56 const SqpDateTime &dateTime) noexcept
57 57 {
58 58 auto insertIndex = rowCount();
59 59 beginInsertRows({}, insertIndex, insertIndex);
60 60
61 61 /// @todo For the moment, the other data of the variable is initialized with default values
62 62 auto variable = std::make_shared<Variable>(name, QStringLiteral("unit"),
63 63 QStringLiteral("mission"), dateTime);
64 64
65 65 impl->m_Variables.push_back(variable);
66 66 connect(variable.get(), &Variable::updated, this, &VariableModel::onVariableUpdated);
67 67
68 68 endInsertRows();
69 69
70 70 return variable;
71 71 }
72 72
73 73 void VariableModel::deleteVariable(std::shared_ptr<Variable> variable) noexcept
74 74 {
75 75 if (!variable) {
76 76 qCCritical(LOG_Variable()) << "Can't delete a null variable from the model";
77 77 return;
78 78 }
79 79
80 80 // Finds variable in the model
81 81 auto begin = impl->m_Variables.cbegin();
82 82 auto end = impl->m_Variables.cend();
83 83 auto it = std::find(begin, end, variable);
84 84 if (it != end) {
85 85 auto removeIndex = std::distance(begin, it);
86 86
87 87 // Deletes variable
88 88 beginRemoveRows({}, removeIndex, removeIndex);
89 89 impl->m_Variables.erase(it);
90 90 endRemoveRows();
91 91 }
92 92 else {
93 93 qCritical(LOG_VariableModel())
94 94 << tr("Can't delete variable %1 from the model: the variable is not in the model")
95 95 .arg(variable->name());
96 96 }
97 97 }
98 98
99 99 std::shared_ptr<Variable> VariableModel::variable(int index) const
100 100 {
101 101 return (index >= 0 && index < impl->m_Variables.size()) ? impl->m_Variables[index] : nullptr;
102 102 }
103 103
104 104 int VariableModel::columnCount(const QModelIndex &parent) const
105 105 {
106 106 Q_UNUSED(parent);
107 107
108 108 return NB_COLUMNS;
109 109 }
110 110
111 111 int VariableModel::rowCount(const QModelIndex &parent) const
112 112 {
113 113 Q_UNUSED(parent);
114 114
115 115 return impl->m_Variables.size();
116 116 }
117 117
118 118 QVariant VariableModel::data(const QModelIndex &index, int role) const
119 119 {
120 120 if (!index.isValid()) {
121 121 return QVariant{};
122 122 }
123 123
124 124 if (index.row() < 0 || index.row() >= rowCount()) {
125 125 return QVariant{};
126 126 }
127 127
128 128 if (role == Qt::DisplayRole) {
129 129 if (auto variable = impl->m_Variables.at(index.row()).get()) {
130 130 /// Lambda function that builds the variant to return for a time value
131 131 auto dateTimeVariant = [](double time) {
132 132 auto dateTime = QDateTime::fromMSecsSinceEpoch(time * 1000.);
133 133 return dateTime.toString(DATETIME_FORMAT);
134 134 };
135 135
136 136 switch (index.column()) {
137 137 case NAME_COLUMN:
138 138 return variable->name();
139 139 case TSTART_COLUMN:
140 140 return dateTimeVariant(variable->dateTime().m_TStart);
141 141 case TEND_COLUMN:
142 142 return dateTimeVariant(variable->dateTime().m_TEnd);
143 143 default:
144 144 // No action
145 145 break;
146 146 }
147 147
148 148 qWarning(LOG_VariableModel())
149 149 << tr("Can't get data (unknown column %1)").arg(index.column());
150 150 }
151 151 else {
152 152 qWarning(LOG_VariableModel()) << tr("Can't get data (no variable)");
153 153 }
154 154 }
155 155
156 156 return QVariant{};
157 157 }
158 158
159 159 QVariant VariableModel::headerData(int section, Qt::Orientation orientation, int role) const
160 160 {
161 161 if (role != Qt::DisplayRole && role != Qt::SizeHintRole) {
162 162 return QVariant{};
163 163 }
164 164
165 165 if (orientation == Qt::Horizontal) {
166 166 auto propertiesIt = COLUMN_PROPERTIES.find(section);
167 167 if (propertiesIt != COLUMN_PROPERTIES.cend()) {
168 168 // Role is either DisplayRole or SizeHintRole
169 169 return (role == Qt::DisplayRole)
170 170 ? QVariant{propertiesIt->m_Name}
171 171 : QVariant{QSize{propertiesIt->m_Width, propertiesIt->m_Height}};
172 172 }
173 173 else {
174 174 qWarning(LOG_VariableModel())
175 175 << tr("Can't get header data (unknown column %1)").arg(section);
176 176 }
177 177 }
178 178
179 179 return QVariant{};
180 180 }
181 181
182 182 void VariableModel::onVariableUpdated() noexcept
183 183 {
184 184 // Finds variable that has been updated in the model
185 185 if (auto updatedVariable = dynamic_cast<Variable *>(sender())) {
186 186 auto begin = std::cbegin(impl->m_Variables);
187 187 auto end = std::cend(impl->m_Variables);
188 188 auto it = std::find_if(begin, end, [updatedVariable](const auto &variable) {
189 189 return variable.get() == updatedVariable;
190 190 });
191 191
192 192 if (it != end) {
193 // Gets the index of the variable in the model: we assume here that views have the same
194 // order as the model
193 195 auto updateVariableIndex = std::distance(begin, it);
194 196 emit dataChanged(createIndex(updateVariableIndex, 0),
195 197 createIndex(updateVariableIndex, columnCount() - 1));
196 198 }
197 199 }
198 200 }
General Comments 0
You need to be logged in to leave comments. Login now