]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * This file is part of the PulseView project. | |
3 | * | |
4 | * Copyright (C) 2020 Soeren Apel <soeren@apelpie.net> | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License as published by | |
8 | * the Free Software Foundation; either version 2 of the License, or | |
9 | * (at your option) any later version. | |
10 | * | |
11 | * This program is distributed in the hope that it will be useful, | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | * GNU General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU General Public License | |
17 | * along with this program; if not, see <http://www.gnu.org/licenses/>. | |
18 | */ | |
19 | ||
20 | #include <QApplication> | |
21 | #include <QDebug> | |
22 | #include <QString> | |
23 | ||
24 | #include "pv/views/tabular_decoder/view.hpp" | |
25 | ||
26 | #include "view.hpp" | |
27 | ||
28 | #include "pv/util.hpp" | |
29 | #include "pv/globalsettings.hpp" | |
30 | ||
31 | using std::make_shared; | |
32 | ||
33 | using pv::util::Timestamp; | |
34 | using pv::util::format_time_si; | |
35 | using pv::util::format_time_minutes; | |
36 | using pv::util::SIPrefix; | |
37 | ||
38 | namespace pv { | |
39 | namespace views { | |
40 | namespace tabular_decoder { | |
41 | ||
42 | AnnotationCollectionModel::AnnotationCollectionModel(QObject* parent) : | |
43 | QAbstractTableModel(parent), | |
44 | all_annotations_(nullptr), | |
45 | dataset_(nullptr), | |
46 | signal_(nullptr), | |
47 | first_hidden_column_(0), | |
48 | prev_segment_(0), | |
49 | prev_last_row_(0), | |
50 | had_highlight_before_(false), | |
51 | hide_hidden_(false) | |
52 | { | |
53 | // Note: when adding entries, consider ViewVisibleFilterProxyModel::filterAcceptsRow() | |
54 | ||
55 | uint8_t i = 0; | |
56 | header_data_.emplace_back(tr("Sample")); i++; // Column #0 | |
57 | header_data_.emplace_back(tr("Time")); i++; // Column #1 | |
58 | header_data_.emplace_back(tr("Decoder")); i++; // Column #2 | |
59 | header_data_.emplace_back(tr("Ann Row")); i++; // Column #3 | |
60 | header_data_.emplace_back(tr("Ann Class")); i++; // Column #4 | |
61 | header_data_.emplace_back(tr("Value")); i++; // Column #5 | |
62 | ||
63 | first_hidden_column_ = i; | |
64 | header_data_.emplace_back("End Sample"); // Column #6, hidden | |
65 | } | |
66 | ||
67 | int AnnotationCollectionModel::get_hierarchy_level(const Annotation* ann) const | |
68 | { | |
69 | int level = 0; | |
70 | ||
71 | const unsigned int ann_stack_level = ann->row_data()->row()->decoder()->get_stack_level(); | |
72 | level = (signal_->decoder_stack().size() - 1 - ann_stack_level); | |
73 | ||
74 | return level; | |
75 | } | |
76 | ||
77 | QVariant AnnotationCollectionModel::data_from_ann(const Annotation* ann, int index) const | |
78 | { | |
79 | switch (index) { | |
80 | case 0: return QVariant((qulonglong)ann->start_sample()); // Column #0, Start Sample | |
81 | case 1: { // Column #1, Start Time | |
82 | Timestamp t = ann->start_sample() / signal_->get_samplerate(); | |
83 | QString unit = signal_->get_samplerate() ? tr("s") : tr("sa"); | |
84 | QString s; | |
85 | if ((t < 60) || (signal_->get_samplerate() == 0)) // i.e. if unit is sa | |
86 | s = format_time_si(t, SIPrefix::unspecified, 3, unit, false); | |
87 | else | |
88 | s = format_time_minutes(t, 3, false); | |
89 | return QVariant(s); | |
90 | } | |
91 | case 2: return QVariant(ann->row()->decoder()->name()); // Column #2, Decoder | |
92 | case 3: return QVariant(ann->row()->description()); // Column #3, Ann Row | |
93 | case 4: return QVariant(ann->ann_class_description()); // Column #4, Ann Class | |
94 | case 5: return QVariant(ann->longest_annotation()); // Column #5, Value | |
95 | case 6: return QVariant((qulonglong)ann->end_sample()); // Column #6, End Sample | |
96 | default: return QVariant(); | |
97 | } | |
98 | } | |
99 | ||
100 | QVariant AnnotationCollectionModel::data(const QModelIndex& index, int role) const | |
101 | { | |
102 | if (!signal_ || !index.isValid() || !index.internalPointer()) | |
103 | return QVariant(); | |
104 | ||
105 | const Annotation* ann = | |
106 | static_cast<const Annotation*>(index.internalPointer()); | |
107 | ||
108 | if ((role == Qt::DisplayRole) || (role == Qt::ToolTipRole)) | |
109 | return data_from_ann(ann, index.column()); | |
110 | ||
111 | if (role == Qt::ForegroundRole) { | |
112 | if (index.column() >= get_hierarchy_level(ann)) { | |
113 | // Invert the text color if this cell is highlighted | |
114 | const bool must_highlight = (highlight_sample_num_ > 0) && | |
115 | ((int64_t)ann->start_sample() <= highlight_sample_num_) && | |
116 | ((int64_t)ann->end_sample() >= highlight_sample_num_); | |
117 | ||
118 | if (must_highlight) { | |
119 | if (GlobalSettings::current_theme_is_dark()) | |
120 | return QApplication::palette().brush(QPalette::Window); | |
121 | else | |
122 | return QApplication::palette().brush(QPalette::WindowText); | |
123 | } | |
124 | } | |
125 | ||
126 | return QApplication::palette().brush(QPalette::WindowText); | |
127 | } | |
128 | ||
129 | if (role == Qt::BackgroundRole) { | |
130 | // Only use custom cell background color if column index reached the hierarchy level | |
131 | if (index.column() >= get_hierarchy_level(ann)) { | |
132 | ||
133 | QColor color; | |
134 | const bool must_highlight = (highlight_sample_num_ > 0) && | |
135 | ((int64_t)ann->start_sample() <= highlight_sample_num_) && | |
136 | ((int64_t)ann->end_sample() >= highlight_sample_num_); | |
137 | ||
138 | if (must_highlight) | |
139 | color = ann->color(); | |
140 | else | |
141 | color = GlobalSettings::current_theme_is_dark() ? | |
142 | ann->dark_color() : ann->bright_color(); | |
143 | ||
144 | return QBrush(color); | |
145 | } | |
146 | } | |
147 | ||
148 | return QVariant(); | |
149 | } | |
150 | ||
151 | Qt::ItemFlags AnnotationCollectionModel::flags(const QModelIndex& index) const | |
152 | { | |
153 | if (!index.isValid()) | |
154 | return Qt::NoItemFlags; | |
155 | ||
156 | return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemNeverHasChildren; | |
157 | } | |
158 | ||
159 | uint8_t AnnotationCollectionModel::first_hidden_column() const | |
160 | { | |
161 | return first_hidden_column_; | |
162 | } | |
163 | ||
164 | QVariant AnnotationCollectionModel::headerData(int section, Qt::Orientation orientation, | |
165 | int role) const | |
166 | { | |
167 | if ((orientation == Qt::Horizontal) && (role == Qt::DisplayRole)) | |
168 | return header_data_.at(section); | |
169 | ||
170 | return QVariant(); | |
171 | } | |
172 | ||
173 | QModelIndex AnnotationCollectionModel::index(int row, int column, | |
174 | const QModelIndex& parent_idx) const | |
175 | { | |
176 | (void)parent_idx; | |
177 | assert(column >= 0); | |
178 | ||
179 | if (!dataset_ || (row < 0)) | |
180 | return QModelIndex(); | |
181 | ||
182 | QModelIndex idx; | |
183 | ||
184 | if ((size_t)row < dataset_->size()) | |
185 | idx = createIndex(row, column, (void*)dataset_->at(row)); | |
186 | ||
187 | return idx; | |
188 | } | |
189 | ||
190 | QModelIndex AnnotationCollectionModel::parent(const QModelIndex& index) const | |
191 | { | |
192 | (void)index; | |
193 | ||
194 | return QModelIndex(); | |
195 | } | |
196 | ||
197 | int AnnotationCollectionModel::rowCount(const QModelIndex& parent_idx) const | |
198 | { | |
199 | (void)parent_idx; | |
200 | ||
201 | if (!dataset_) | |
202 | return 0; | |
203 | ||
204 | return dataset_->size(); | |
205 | } | |
206 | ||
207 | int AnnotationCollectionModel::columnCount(const QModelIndex& parent_idx) const | |
208 | { | |
209 | (void)parent_idx; | |
210 | ||
211 | return header_data_.size(); | |
212 | } | |
213 | ||
214 | void AnnotationCollectionModel::set_signal_and_segment(data::DecodeSignal* signal, uint32_t current_segment) | |
215 | { | |
216 | if (!signal) { | |
217 | all_annotations_ = nullptr; | |
218 | dataset_ = nullptr; | |
219 | signal_ = nullptr; | |
220 | ||
221 | dataChanged(QModelIndex(), QModelIndex()); | |
222 | layoutChanged(); | |
223 | return; | |
224 | } | |
225 | ||
226 | disconnect(this, SLOT(on_annotation_visibility_changed())); | |
227 | ||
228 | all_annotations_ = signal->get_all_annotations_by_segment(current_segment); | |
229 | signal_ = signal; | |
230 | ||
231 | for (const shared_ptr<Decoder>& dec : signal_->decoder_stack()) | |
232 | connect(dec.get(), SIGNAL(annotation_visibility_changed()), | |
233 | this, SLOT(on_annotation_visibility_changed())); | |
234 | ||
235 | if (hide_hidden_) | |
236 | update_annotations_without_hidden(); | |
237 | else | |
238 | dataset_ = all_annotations_; | |
239 | ||
240 | if (!dataset_ || dataset_->empty()) { | |
241 | prev_segment_ = current_segment; | |
242 | return; | |
243 | } | |
244 | ||
245 | const size_t new_row_count = dataset_->size() - 1; | |
246 | ||
247 | // Force the view associated with this model to update when the segment changes | |
248 | if (prev_segment_ != current_segment) { | |
249 | dataChanged(index(0, 0), index(new_row_count, 0)); | |
250 | layoutChanged(); | |
251 | } else { | |
252 | // Force the view associated with this model to update when we have more annotations | |
253 | if (prev_last_row_ < new_row_count) { | |
254 | dataChanged(index(prev_last_row_, 0), index(new_row_count, 0)); | |
255 | layoutChanged(); | |
256 | } | |
257 | } | |
258 | ||
259 | prev_segment_ = current_segment; | |
260 | prev_last_row_ = new_row_count; | |
261 | } | |
262 | ||
263 | void AnnotationCollectionModel::set_hide_hidden(bool hide_hidden) | |
264 | { | |
265 | hide_hidden_ = hide_hidden; | |
266 | ||
267 | if (hide_hidden_) { | |
268 | dataset_ = &all_annotations_without_hidden_; | |
269 | update_annotations_without_hidden(); | |
270 | } else { | |
271 | dataset_ = all_annotations_; | |
272 | all_annotations_without_hidden_.clear(); // To conserve memory | |
273 | } | |
274 | ||
275 | if (dataset_) | |
276 | dataChanged(index(0, 0), index(dataset_->size() - 1, 0)); | |
277 | else | |
278 | dataChanged(QModelIndex(), QModelIndex()); | |
279 | ||
280 | layoutChanged(); | |
281 | } | |
282 | ||
283 | void AnnotationCollectionModel::update_annotations_without_hidden() | |
284 | { | |
285 | uint64_t count = 0; | |
286 | ||
287 | if (!all_annotations_ || all_annotations_->empty()) { | |
288 | all_annotations_without_hidden_.clear(); | |
289 | return; | |
290 | } | |
291 | ||
292 | for (const Annotation* ann : *all_annotations_) { | |
293 | if (!ann->visible()) | |
294 | continue; | |
295 | ||
296 | if (all_annotations_without_hidden_.size() < (count + 100)) | |
297 | all_annotations_without_hidden_.resize(count + 100); | |
298 | ||
299 | all_annotations_without_hidden_[count++] = ann; | |
300 | } | |
301 | ||
302 | all_annotations_without_hidden_.resize(count); | |
303 | } | |
304 | ||
305 | QModelIndex AnnotationCollectionModel::update_highlighted_rows(QModelIndex first, | |
306 | QModelIndex last, int64_t sample_num) | |
307 | { | |
308 | bool has_highlight = false; | |
309 | QModelIndex result; | |
310 | ||
311 | highlight_sample_num_ = sample_num; | |
312 | ||
313 | if (!dataset_ || dataset_->empty()) | |
314 | return result; | |
315 | ||
316 | if (sample_num >= 0) { | |
317 | last = last.sibling(last.row() + 1, 0); | |
318 | ||
319 | // Check if there are any annotations visible in the table view that | |
320 | // we would need to highlight - only then do we do so | |
321 | QModelIndex index = first; | |
322 | do { | |
323 | const Annotation* ann = | |
324 | static_cast<const Annotation*>(index.internalPointer()); | |
325 | assert(ann); | |
326 | ||
327 | if (((int64_t)ann->start_sample() <= sample_num) && | |
328 | ((int64_t)ann->end_sample() >= sample_num)) { | |
329 | result = index; | |
330 | has_highlight = true; | |
331 | break; | |
332 | } | |
333 | ||
334 | index = index.sibling(index.row() + 1, 0); | |
335 | } while (index != last); | |
336 | } | |
337 | ||
338 | if (has_highlight || had_highlight_before_) { | |
339 | dataChanged(first, last); | |
340 | layoutChanged(); | |
341 | } | |
342 | ||
343 | had_highlight_before_ = has_highlight; | |
344 | ||
345 | return result; | |
346 | } | |
347 | ||
348 | void AnnotationCollectionModel::on_annotation_visibility_changed() | |
349 | { | |
350 | if (!hide_hidden_) | |
351 | return; | |
352 | ||
353 | update_annotations_without_hidden(); | |
354 | ||
355 | if (dataset_) | |
356 | dataChanged(index(0, 0), index(dataset_->size() - 1, 0)); | |
357 | else | |
358 | dataChanged(QModelIndex(), QModelIndex()); | |
359 | ||
360 | layoutChanged(); | |
361 | } | |
362 | ||
363 | } // namespace tabular_decoder | |
364 | } // namespace views | |
365 | } // namespace pv |