##// END OF EJS Templates
change dateTime method for range method
perrinel -
r534:97af8d6f7e1c
parent child
Show More
@@ -1,249 +1,249
1 #include <Variable/Variable.h>
1 #include <Variable/Variable.h>
2 #include <Variable/VariableModel.h>
2 #include <Variable/VariableModel.h>
3
3
4 #include <Common/DateUtils.h>
4 #include <Common/DateUtils.h>
5
5
6 #include <Data/IDataSeries.h>
6 #include <Data/IDataSeries.h>
7
7
8 #include <QSize>
8 #include <QSize>
9 #include <unordered_map>
9 #include <unordered_map>
10
10
11 Q_LOGGING_CATEGORY(LOG_VariableModel, "VariableModel")
11 Q_LOGGING_CATEGORY(LOG_VariableModel, "VariableModel")
12
12
13 namespace {
13 namespace {
14
14
15 // Column indexes
15 // Column indexes
16 const auto NAME_COLUMN = 0;
16 const auto NAME_COLUMN = 0;
17 const auto TSTART_COLUMN = 1;
17 const auto TSTART_COLUMN = 1;
18 const auto TEND_COLUMN = 2;
18 const auto TEND_COLUMN = 2;
19 const auto NB_COLUMNS = 3;
19 const auto NB_COLUMNS = 3;
20
20
21 // Column properties
21 // Column properties
22 const auto DEFAULT_HEIGHT = 25;
22 const auto DEFAULT_HEIGHT = 25;
23 const auto DEFAULT_WIDTH = 100;
23 const auto DEFAULT_WIDTH = 100;
24
24
25 struct ColumnProperties {
25 struct ColumnProperties {
26 ColumnProperties(const QString &name = {}, int width = DEFAULT_WIDTH,
26 ColumnProperties(const QString &name = {}, int width = DEFAULT_WIDTH,
27 int height = DEFAULT_HEIGHT)
27 int height = DEFAULT_HEIGHT)
28 : m_Name{name}, m_Width{width}, m_Height{height}
28 : m_Name{name}, m_Width{width}, m_Height{height}
29 {
29 {
30 }
30 }
31
31
32 QString m_Name;
32 QString m_Name;
33 int m_Width;
33 int m_Width;
34 int m_Height;
34 int m_Height;
35 };
35 };
36
36
37 const auto COLUMN_PROPERTIES
37 const auto COLUMN_PROPERTIES
38 = QHash<int, ColumnProperties>{{NAME_COLUMN, {QObject::tr("Name")}},
38 = QHash<int, ColumnProperties>{{NAME_COLUMN, {QObject::tr("Name")}},
39 {TSTART_COLUMN, {QObject::tr("tStart"), 180}},
39 {TSTART_COLUMN, {QObject::tr("tStart"), 180}},
40 {TEND_COLUMN, {QObject::tr("tEnd"), 180}}};
40 {TEND_COLUMN, {QObject::tr("tEnd"), 180}}};
41
41
42 /// Format for datetimes
42 /// Format for datetimes
43 const auto DATETIME_FORMAT = QStringLiteral("dd/MM/yyyy \nhh:mm:ss:zzz");
43 const auto DATETIME_FORMAT = QStringLiteral("dd/MM/yyyy \nhh:mm:ss:zzz");
44
44
45
45
46 } // namespace
46 } // namespace
47
47
48 struct VariableModel::VariableModelPrivate {
48 struct VariableModel::VariableModelPrivate {
49 /// Variables created in SciQlop
49 /// Variables created in SciQlop
50 std::vector<std::shared_ptr<Variable> > m_Variables;
50 std::vector<std::shared_ptr<Variable> > m_Variables;
51 std::unordered_map<std::shared_ptr<Variable>, double> m_VariableToProgress;
51 std::unordered_map<std::shared_ptr<Variable>, double> m_VariableToProgress;
52
52
53 /// Return the row index of the variable. -1 if it's not found
53 /// Return the row index of the variable. -1 if it's not found
54 int indexOfVariable(Variable *variable) const noexcept;
54 int indexOfVariable(Variable *variable) const noexcept;
55 };
55 };
56
56
57 VariableModel::VariableModel(QObject *parent)
57 VariableModel::VariableModel(QObject *parent)
58 : QAbstractTableModel{parent}, impl{spimpl::make_unique_impl<VariableModelPrivate>()}
58 : QAbstractTableModel{parent}, impl{spimpl::make_unique_impl<VariableModelPrivate>()}
59 {
59 {
60 }
60 }
61
61
62 std::shared_ptr<Variable> VariableModel::createVariable(const QString &name,
62 std::shared_ptr<Variable> VariableModel::createVariable(const QString &name,
63 const SqpRange &dateTime,
63 const SqpRange &dateTime,
64 const QVariantHash &metadata) noexcept
64 const QVariantHash &metadata) noexcept
65 {
65 {
66 auto insertIndex = rowCount();
66 auto insertIndex = rowCount();
67 beginInsertRows({}, insertIndex, insertIndex);
67 beginInsertRows({}, insertIndex, insertIndex);
68
68
69 auto variable = std::make_shared<Variable>(name, dateTime, metadata);
69 auto variable = std::make_shared<Variable>(name, dateTime, metadata);
70
70
71 impl->m_Variables.push_back(variable);
71 impl->m_Variables.push_back(variable);
72 connect(variable.get(), &Variable::updated, this, &VariableModel::onVariableUpdated);
72 connect(variable.get(), &Variable::updated, this, &VariableModel::onVariableUpdated);
73
73
74 endInsertRows();
74 endInsertRows();
75
75
76 return variable;
76 return variable;
77 }
77 }
78
78
79 void VariableModel::deleteVariable(std::shared_ptr<Variable> variable) noexcept
79 void VariableModel::deleteVariable(std::shared_ptr<Variable> variable) noexcept
80 {
80 {
81 if (!variable) {
81 if (!variable) {
82 qCCritical(LOG_Variable()) << "Can't delete a null variable from the model";
82 qCCritical(LOG_Variable()) << "Can't delete a null variable from the model";
83 return;
83 return;
84 }
84 }
85
85
86 // Finds variable in the model
86 // Finds variable in the model
87 auto begin = impl->m_Variables.cbegin();
87 auto begin = impl->m_Variables.cbegin();
88 auto end = impl->m_Variables.cend();
88 auto end = impl->m_Variables.cend();
89 auto it = std::find(begin, end, variable);
89 auto it = std::find(begin, end, variable);
90 if (it != end) {
90 if (it != end) {
91 auto removeIndex = std::distance(begin, it);
91 auto removeIndex = std::distance(begin, it);
92
92
93 // Deletes variable
93 // Deletes variable
94 beginRemoveRows({}, removeIndex, removeIndex);
94 beginRemoveRows({}, removeIndex, removeIndex);
95 impl->m_Variables.erase(it);
95 impl->m_Variables.erase(it);
96 endRemoveRows();
96 endRemoveRows();
97 }
97 }
98 else {
98 else {
99 qCritical(LOG_VariableModel())
99 qCritical(LOG_VariableModel())
100 << tr("Can't delete variable %1 from the model: the variable is not in the model")
100 << tr("Can't delete variable %1 from the model: the variable is not in the model")
101 .arg(variable->name());
101 .arg(variable->name());
102 }
102 }
103
103
104 // Removes variable from progress map
104 // Removes variable from progress map
105 impl->m_VariableToProgress.erase(variable);
105 impl->m_VariableToProgress.erase(variable);
106 }
106 }
107
107
108
108
109 std::shared_ptr<Variable> VariableModel::variable(int index) const
109 std::shared_ptr<Variable> VariableModel::variable(int index) const
110 {
110 {
111 return (index >= 0 && index < impl->m_Variables.size()) ? impl->m_Variables[index] : nullptr;
111 return (index >= 0 && index < impl->m_Variables.size()) ? impl->m_Variables[index] : nullptr;
112 }
112 }
113
113
114 void VariableModel::setDataProgress(std::shared_ptr<Variable> variable, double progress)
114 void VariableModel::setDataProgress(std::shared_ptr<Variable> variable, double progress)
115 {
115 {
116 if (progress > 0.0) {
116 if (progress > 0.0) {
117 impl->m_VariableToProgress[variable] = progress;
117 impl->m_VariableToProgress[variable] = progress;
118 }
118 }
119 else {
119 else {
120 impl->m_VariableToProgress.erase(variable);
120 impl->m_VariableToProgress.erase(variable);
121 }
121 }
122 auto modelIndex = createIndex(impl->indexOfVariable(variable.get()), NAME_COLUMN);
122 auto modelIndex = createIndex(impl->indexOfVariable(variable.get()), NAME_COLUMN);
123
123
124 emit dataChanged(modelIndex, modelIndex);
124 emit dataChanged(modelIndex, modelIndex);
125 }
125 }
126
126
127 int VariableModel::columnCount(const QModelIndex &parent) const
127 int VariableModel::columnCount(const QModelIndex &parent) const
128 {
128 {
129 Q_UNUSED(parent);
129 Q_UNUSED(parent);
130
130
131 return NB_COLUMNS;
131 return NB_COLUMNS;
132 }
132 }
133
133
134 int VariableModel::rowCount(const QModelIndex &parent) const
134 int VariableModel::rowCount(const QModelIndex &parent) const
135 {
135 {
136 Q_UNUSED(parent);
136 Q_UNUSED(parent);
137
137
138 return impl->m_Variables.size();
138 return impl->m_Variables.size();
139 }
139 }
140
140
141 QVariant VariableModel::data(const QModelIndex &index, int role) const
141 QVariant VariableModel::data(const QModelIndex &index, int role) const
142 {
142 {
143 if (!index.isValid()) {
143 if (!index.isValid()) {
144 return QVariant{};
144 return QVariant{};
145 }
145 }
146
146
147 if (index.row() < 0 || index.row() >= rowCount()) {
147 if (index.row() < 0 || index.row() >= rowCount()) {
148 return QVariant{};
148 return QVariant{};
149 }
149 }
150
150
151 if (role == Qt::DisplayRole) {
151 if (role == Qt::DisplayRole) {
152 if (auto variable = impl->m_Variables.at(index.row()).get()) {
152 if (auto variable = impl->m_Variables.at(index.row()).get()) {
153 /// Lambda function that builds the variant to return for a time value
153 /// Lambda function that builds the variant to return for a time value
154 auto dateTimeVariant = [](double secs) {
154 auto dateTimeVariant = [](double secs) {
155 auto dateTime = DateUtils::dateTime(secs);
155 auto dateTime = DateUtils::dateTime(secs);
156 return dateTime.toString(DATETIME_FORMAT);
156 return dateTime.toString(DATETIME_FORMAT);
157 };
157 };
158
158
159 switch (index.column()) {
159 switch (index.column()) {
160 case NAME_COLUMN:
160 case NAME_COLUMN:
161 return variable->name();
161 return variable->name();
162 case TSTART_COLUMN:
162 case TSTART_COLUMN:
163 return dateTimeVariant(variable->dateTime().m_TStart);
163 return dateTimeVariant(variable->range().m_TStart);
164 case TEND_COLUMN:
164 case TEND_COLUMN:
165 return dateTimeVariant(variable->dateTime().m_TEnd);
165 return dateTimeVariant(variable->range().m_TEnd);
166 default:
166 default:
167 // No action
167 // No action
168 break;
168 break;
169 }
169 }
170
170
171 qWarning(LOG_VariableModel())
171 qWarning(LOG_VariableModel())
172 << tr("Can't get data (unknown column %1)").arg(index.column());
172 << tr("Can't get data (unknown column %1)").arg(index.column());
173 }
173 }
174 else {
174 else {
175 qWarning(LOG_VariableModel()) << tr("Can't get data (no variable)");
175 qWarning(LOG_VariableModel()) << tr("Can't get data (no variable)");
176 }
176 }
177 }
177 }
178 else if (role == VariableRoles::ProgressRole) {
178 else if (role == VariableRoles::ProgressRole) {
179 if (auto variable = impl->m_Variables.at(index.row())) {
179 if (auto variable = impl->m_Variables.at(index.row())) {
180
180
181 auto it = impl->m_VariableToProgress.find(variable);
181 auto it = impl->m_VariableToProgress.find(variable);
182 if (it != impl->m_VariableToProgress.cend()) {
182 if (it != impl->m_VariableToProgress.cend()) {
183 return it->second;
183 return it->second;
184 }
184 }
185 }
185 }
186 }
186 }
187
187
188 return QVariant{};
188 return QVariant{};
189 }
189 }
190
190
191 QVariant VariableModel::headerData(int section, Qt::Orientation orientation, int role) const
191 QVariant VariableModel::headerData(int section, Qt::Orientation orientation, int role) const
192 {
192 {
193 if (role != Qt::DisplayRole && role != Qt::SizeHintRole) {
193 if (role != Qt::DisplayRole && role != Qt::SizeHintRole) {
194 return QVariant{};
194 return QVariant{};
195 }
195 }
196
196
197 if (orientation == Qt::Horizontal) {
197 if (orientation == Qt::Horizontal) {
198 auto propertiesIt = COLUMN_PROPERTIES.find(section);
198 auto propertiesIt = COLUMN_PROPERTIES.find(section);
199 if (propertiesIt != COLUMN_PROPERTIES.cend()) {
199 if (propertiesIt != COLUMN_PROPERTIES.cend()) {
200 // Role is either DisplayRole or SizeHintRole
200 // Role is either DisplayRole or SizeHintRole
201 return (role == Qt::DisplayRole)
201 return (role == Qt::DisplayRole)
202 ? QVariant{propertiesIt->m_Name}
202 ? QVariant{propertiesIt->m_Name}
203 : QVariant{QSize{propertiesIt->m_Width, propertiesIt->m_Height}};
203 : QVariant{QSize{propertiesIt->m_Width, propertiesIt->m_Height}};
204 }
204 }
205 else {
205 else {
206 qWarning(LOG_VariableModel())
206 qWarning(LOG_VariableModel())
207 << tr("Can't get header data (unknown column %1)").arg(section);
207 << tr("Can't get header data (unknown column %1)").arg(section);
208 }
208 }
209 }
209 }
210
210
211 return QVariant{};
211 return QVariant{};
212 }
212 }
213
213
214 void VariableModel::abortProgress(const QModelIndex &index)
214 void VariableModel::abortProgress(const QModelIndex &index)
215 {
215 {
216 if (auto variable = impl->m_Variables.at(index.row())) {
216 if (auto variable = impl->m_Variables.at(index.row())) {
217 emit abortProgessRequested(variable);
217 emit abortProgessRequested(variable);
218 }
218 }
219 }
219 }
220
220
221 void VariableModel::onVariableUpdated() noexcept
221 void VariableModel::onVariableUpdated() noexcept
222 {
222 {
223 // Finds variable that has been updated in the model
223 // Finds variable that has been updated in the model
224 if (auto updatedVariable = dynamic_cast<Variable *>(sender())) {
224 if (auto updatedVariable = dynamic_cast<Variable *>(sender())) {
225 auto updatedVariableIndex = impl->indexOfVariable(updatedVariable);
225 auto updatedVariableIndex = impl->indexOfVariable(updatedVariable);
226
226
227 if (updatedVariableIndex > -1) {
227 if (updatedVariableIndex > -1) {
228 emit dataChanged(createIndex(updatedVariableIndex, 0),
228 emit dataChanged(createIndex(updatedVariableIndex, 0),
229 createIndex(updatedVariableIndex, columnCount() - 1));
229 createIndex(updatedVariableIndex, columnCount() - 1));
230 }
230 }
231 }
231 }
232 }
232 }
233
233
234 int VariableModel::VariableModelPrivate::indexOfVariable(Variable *variable) const noexcept
234 int VariableModel::VariableModelPrivate::indexOfVariable(Variable *variable) const noexcept
235 {
235 {
236 auto begin = std::cbegin(m_Variables);
236 auto begin = std::cbegin(m_Variables);
237 auto end = std::cend(m_Variables);
237 auto end = std::cend(m_Variables);
238 auto it
238 auto it
239 = std::find_if(begin, end, [variable](const auto &var) { return var.get() == variable; });
239 = std::find_if(begin, end, [variable](const auto &var) { return var.get() == variable; });
240
240
241 if (it != end) {
241 if (it != end) {
242 // Gets the index of the variable in the model: we assume here that views have the same
242 // Gets the index of the variable in the model: we assume here that views have the same
243 // order as the model
243 // order as the model
244 return std::distance(begin, it);
244 return std::distance(begin, it);
245 }
245 }
246 else {
246 else {
247 return -1;
247 return -1;
248 }
248 }
249 }
249 }
General Comments 0
You need to be logged in to leave comments. Login now