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