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