]> sigrok.org Git - pulseview.git/blame - pv/views/tabular_decoder/model.cpp
Implement MetadataObjMousePos and use it in the TabularDecView
[pulseview.git] / pv / views / tabular_decoder / model.cpp
CommitLineData
24d69d27
SA
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
1c521100 20#include <QApplication>
f54e68b0 21#include <QDebug>
24d69d27
SA
22#include <QString>
23
24#include "pv/views/tabular_decoder/view.hpp"
25
d656b010
SA
26#include "view.hpp"
27
85125b0f 28#include "pv/util.hpp"
8e168b23 29#include "pv/globalsettings.hpp"
85125b0f 30
24d69d27
SA
31using std::make_shared;
32
85125b0f
SA
33using pv::util::Timestamp;
34using pv::util::format_time_si;
35using pv::util::format_time_minutes;
36using pv::util::SIPrefix;
37
24d69d27
SA
38namespace pv {
39namespace views {
40namespace tabular_decoder {
41
42AnnotationCollectionModel::AnnotationCollectionModel(QObject* parent) :
f54e68b0
SA
43 QAbstractTableModel(parent),
44 all_annotations_(nullptr),
86d4b8e3 45 dataset_(nullptr),
d656b010 46 signal_(nullptr),
f54e68b0 47 prev_segment_(0),
86d4b8e3 48 prev_last_row_(0),
8997f62a
SA
49 start_index_(0),
50 end_index_(0),
1c521100 51 had_highlight_before_(false),
86d4b8e3 52 hide_hidden_(false)
24d69d27 53{
f54e68b0 54 // TBD Maybe use empty columns as indentation levels to indicate stacked decoders
88a25978
SA
55 header_data_.emplace_back(tr("Sample")); // Column #0
56 header_data_.emplace_back(tr("Time")); // Column #1
57 header_data_.emplace_back(tr("Decoder")); // Column #2
58 header_data_.emplace_back(tr("Ann Row")); // Column #3
59 header_data_.emplace_back(tr("Ann Class")); // Column #4
60 header_data_.emplace_back(tr("Value")); // Column #5
24d69d27
SA
61}
62
1c521100
SA
63int AnnotationCollectionModel::get_hierarchy_level(const Annotation* ann) const
64{
65 int level = 0;
66
67 const unsigned int ann_stack_level = ann->row_data()->row()->decoder()->get_stack_level();
68 level = (signal_->decoder_stack().size() - 1 - ann_stack_level);
69
70 return level;
71}
72
85125b0f
SA
73QVariant AnnotationCollectionModel::data_from_ann(const Annotation* ann, int index) const
74{
75 switch (index) {
76 case 0: return QVariant((qulonglong)ann->start_sample()); // Column #0, Start Sample
77 case 1: { // Column #1, Start Time
78 Timestamp t = ann->start_sample() / signal_->get_samplerate();
79 QString unit = signal_->get_samplerate() ? tr("s") : tr("sa");
80 QString s;
81 if ((t < 60) || (signal_->get_samplerate() == 0)) // i.e. if unit is sa
82 s = format_time_si(t, SIPrefix::unspecified, 3, unit, false);
83 else
84 s = format_time_minutes(t, 3, false);
85 return QVariant(s);
86 }
87 case 2: return QVariant(ann->row()->decoder()->name()); // Column #2, Decoder
88 case 3: return QVariant(ann->row()->description()); // Column #3, Ann Row
89 case 4: return QVariant(ann->ann_class_description()); // Column #4, Ann Class
90 case 5: return QVariant(ann->longest_annotation()); // Column #5, Value
91 default: return QVariant();
92 }
93}
94
24d69d27
SA
95QVariant AnnotationCollectionModel::data(const QModelIndex& index, int role) const
96{
6d46525f 97 if (!signal_ || !index.isValid() || !index.internalPointer())
24d69d27
SA
98 return QVariant();
99
88a25978
SA
100 const Annotation* ann =
101 static_cast<const Annotation*>(index.internalPointer());
f54e68b0 102
85125b0f
SA
103 if ((role == Qt::DisplayRole) || (role == Qt::ToolTipRole))
104 return data_from_ann(ann, index.column());
24d69d27 105
1c521100
SA
106 if (role == Qt::ForegroundRole) {
107 if (index.column() >= get_hierarchy_level(ann)) {
108 // Invert the text color if this cell is highlighted
109 const bool must_highlight = (highlight_sample_num_ > 0) &&
110 ((int64_t)ann->start_sample() <= highlight_sample_num_) &&
111 ((int64_t)ann->end_sample() >= highlight_sample_num_);
112
113 if (must_highlight) {
114 if (GlobalSettings::current_theme_is_dark())
115 return QApplication::palette().brush(QPalette::Window);
116 else
117 return QApplication::palette().brush(QPalette::WindowText);
118 }
119 }
d656b010 120
1c521100
SA
121 return QApplication::palette().brush(QPalette::WindowText);
122 }
d656b010 123
1c521100 124 if (role == Qt::BackgroundRole) {
d656b010 125 // Only use custom cell background color if column index reached the hierarchy level
1c521100
SA
126 if (index.column() >= get_hierarchy_level(ann)) {
127
128 QColor color;
129 const bool must_highlight = (highlight_sample_num_ > 0) &&
130 ((int64_t)ann->start_sample() <= highlight_sample_num_) &&
131 ((int64_t)ann->end_sample() >= highlight_sample_num_);
132
133 if (must_highlight)
134 color = ann->color();
d656b010 135 else
1c521100
SA
136 color = GlobalSettings::current_theme_is_dark() ?
137 ann->dark_color() : ann->bright_color();
138
139 return QBrush(color);
d656b010 140 }
88a25978
SA
141 }
142
24d69d27
SA
143 return QVariant();
144}
145
146Qt::ItemFlags AnnotationCollectionModel::flags(const QModelIndex& index) const
147{
148 if (!index.isValid())
9a35b05d 149 return Qt::NoItemFlags;
24d69d27 150
9a35b05d 151 return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemNeverHasChildren;
24d69d27
SA
152}
153
154QVariant AnnotationCollectionModel::headerData(int section, Qt::Orientation orientation,
155 int role) const
156{
6d46525f 157 if ((orientation == Qt::Horizontal) && (role == Qt::DisplayRole))
f54e68b0 158 return header_data_.at(section);
24d69d27
SA
159
160 return QVariant();
161}
162
163QModelIndex AnnotationCollectionModel::index(int row, int column,
164 const QModelIndex& parent_idx) const
165{
f54e68b0 166 (void)parent_idx;
6d46525f 167 assert(column >= 0);
24d69d27 168
593ea025 169 if (!dataset_ || (row < 0))
f54e68b0 170 return QModelIndex();
24d69d27 171
86d4b8e3
SA
172 QModelIndex idx;
173
8997f62a
SA
174 if (start_index_ == end_index_) {
175 if ((size_t)row < dataset_->size())
176 idx = createIndex(row, column, (void*)dataset_->at(row));
177 } else {
178 if ((size_t)row < (end_index_ - start_index_))
179 idx = createIndex(row, column, (void*)dataset_->at(start_index_ + row));
180 }
24d69d27 181
86d4b8e3 182 return idx;
24d69d27
SA
183}
184
185QModelIndex AnnotationCollectionModel::parent(const QModelIndex& index) const
186{
f54e68b0 187 (void)index;
24d69d27 188
f54e68b0 189 return QModelIndex();
24d69d27
SA
190}
191
192int AnnotationCollectionModel::rowCount(const QModelIndex& parent_idx) const
193{
f54e68b0 194 (void)parent_idx;
24d69d27 195
86d4b8e3 196 if (!dataset_)
24d69d27
SA
197 return 0;
198
8997f62a
SA
199 if (start_index_ == end_index_)
200 return dataset_->size();
201 else
202 return (end_index_ - start_index_);
24d69d27
SA
203}
204
205int AnnotationCollectionModel::columnCount(const QModelIndex& parent_idx) const
206{
f54e68b0
SA
207 (void)parent_idx;
208
209 return header_data_.size();
24d69d27
SA
210}
211
f54e68b0
SA
212void AnnotationCollectionModel::set_signal_and_segment(data::DecodeSignal* signal, uint32_t current_segment)
213{
88a25978
SA
214 if (!signal) {
215 all_annotations_ = nullptr;
86d4b8e3 216 dataset_ = nullptr;
d656b010 217 signal_ = nullptr;
86d4b8e3 218
88a25978
SA
219 dataChanged(QModelIndex(), QModelIndex());
220 layoutChanged();
221 return;
222 }
223
02078aa1
SA
224 disconnect(this, SLOT(on_annotation_visibility_changed()));
225
f54e68b0 226 all_annotations_ = signal->get_all_annotations_by_segment(current_segment);
d656b010 227 signal_ = signal;
f54e68b0 228
02078aa1
SA
229 for (const shared_ptr<Decoder>& dec : signal_->decoder_stack())
230 connect(dec.get(), SIGNAL(annotation_visibility_changed()),
231 this, SLOT(on_annotation_visibility_changed()));
232
86d4b8e3
SA
233 if (hide_hidden_)
234 update_annotations_without_hidden();
235 else
236 dataset_ = all_annotations_;
237
238 if (!dataset_ || dataset_->empty()) {
f54e68b0
SA
239 prev_segment_ = current_segment;
240 return;
241 }
242
8997f62a
SA
243 // Re-apply the requested sample range
244 set_sample_range(start_sample_, end_sample_);
245
86d4b8e3 246 const size_t new_row_count = dataset_->size() - 1;
f54e68b0
SA
247
248 // Force the view associated with this model to update when the segment changes
249 if (prev_segment_ != current_segment) {
250 dataChanged(QModelIndex(), QModelIndex());
251 layoutChanged();
252 } else {
253 // Force the view associated with this model to update when we have more annotations
254 if (prev_last_row_ < new_row_count) {
c84afcfd 255 dataChanged(index(prev_last_row_, 0), index(new_row_count, 0));
f54e68b0
SA
256 layoutChanged();
257 }
258 }
259
260 prev_segment_ = current_segment;
261 prev_last_row_ = new_row_count;
262}
24d69d27 263
8997f62a
SA
264void AnnotationCollectionModel::set_sample_range(uint64_t start_sample, uint64_t end_sample)
265{
266 // Check if there's even anything to reset
267 if ((start_sample == end_sample) && (start_index_ == end_index_))
268 return;
269
270 if (!dataset_ || dataset_->empty() || (end_sample == 0)) {
271 start_index_ = 0;
272 end_index_ = 0;
273 start_sample_ = 0;
274 end_sample_ = 0;
275
276 dataChanged(QModelIndex(), QModelIndex());
277 layoutChanged();
278 return;
279 }
280
281 start_sample_ = start_sample;
282 end_sample_ = end_sample;
283
284 // Determine first and last indices into the annotation list
285 int64_t i = -1;
286 bool ann_outside_range;
287 do {
288 i++;
289
290 if (i == (int64_t)dataset_->size()) {
291 start_index_ = 0;
292 end_index_ = 0;
293
294 dataChanged(QModelIndex(), QModelIndex());
295 layoutChanged();
296 return;
297 }
298 const Annotation* ann = (*dataset_)[i];
299 ann_outside_range =
300 ((ann->start_sample() < start_sample) && (ann->end_sample() < start_sample));
301 } while (ann_outside_range);
302 start_index_ = i;
303
304 // Ideally, we would be able to set end_index_ to the last annotation that
305 // is within range. However, as annotations in the list are sorted by
306 // start sample and hierarchy level, we may encounter this scenario:
307 // [long annotation that spans across view]
308 // [short annotations that aren't seen]
309 // [short annotations that are seen]
310 // ..in which our output would only show the first long annotations.
311 // For this reason, we simply show everything after the first visible
312 // annotation for now.
313
314 end_index_ = dataset_->size();
315
316 dataChanged(index(0, 0), index((end_index_ - start_index_), 0));
317 layoutChanged();
318}
319
86d4b8e3
SA
320void AnnotationCollectionModel::set_hide_hidden(bool hide_hidden)
321{
322 hide_hidden_ = hide_hidden;
323
324 if (hide_hidden_) {
325 dataset_ = &all_annotations_without_hidden_;
326 update_annotations_without_hidden();
327 } else {
328 dataset_ = all_annotations_;
329 all_annotations_without_hidden_.clear(); // To conserve memory
330 }
8997f62a
SA
331
332 // Re-apply the requested sample range
333 set_sample_range(start_sample_, end_sample_);
334
335 if (dataset_)
336 dataChanged(index(0, 0), index(dataset_->size(), 0));
337 else
338 dataChanged(QModelIndex(), QModelIndex());
339
340 layoutChanged();
86d4b8e3
SA
341}
342
343void AnnotationCollectionModel::update_annotations_without_hidden()
344{
345 uint64_t count = 0;
346
347 if (!all_annotations_ || all_annotations_->empty()) {
348 all_annotations_without_hidden_.clear();
349 return;
350 }
351
352 for (const Annotation* ann : *all_annotations_) {
353 if (!ann->visible())
354 continue;
355
356 if (all_annotations_without_hidden_.size() < (count + 100))
357 all_annotations_without_hidden_.resize(count + 100);
358
359 all_annotations_without_hidden_[count++] = ann;
360 }
361
362 all_annotations_without_hidden_.resize(count);
86d4b8e3
SA
363}
364
1c521100
SA
365void AnnotationCollectionModel::update_highlighted_rows(QModelIndex first,
366 QModelIndex last, int64_t sample_num)
367{
368 bool has_highlight = false;
369
370 highlight_sample_num_ = sample_num;
371
372 if (!dataset_ || dataset_->empty())
373 return;
374
375 if (sample_num >= 0) {
376 last = last.sibling(last.row() + 1, 0);
377
378 // Check if there are any annotations visible in the table view that
379 // we would need to highlight - only then do we do so
380 QModelIndex index = first;
381 do {
382 const Annotation* ann =
383 static_cast<const Annotation*>(index.internalPointer());
384 assert(ann);
385
386 if (((int64_t)ann->start_sample() <= sample_num) &&
387 ((int64_t)ann->end_sample() >= sample_num)) {
388 has_highlight = true;
389 break;
390 }
391
392 index = index.sibling(index.row() + 1, 0);
393 } while (index != last);
394 }
395
396 if (has_highlight || had_highlight_before_)
397 dataChanged(first, last);
398
399 had_highlight_before_ = has_highlight;
400}
401
02078aa1
SA
402void AnnotationCollectionModel::on_annotation_visibility_changed()
403{
404 if (!hide_hidden_)
405 return;
406
407 update_annotations_without_hidden();
408
409 // Re-apply the requested sample range
410 set_sample_range(start_sample_, end_sample_);
411
412 if (dataset_)
413 dataChanged(index(0, 0), index(dataset_->size(), 0));
414 else
415 dataChanged(QModelIndex(), QModelIndex());
416
417 layoutChanged();
418}
419
24d69d27
SA
420} // namespace tabular_decoder
421} // namespace views
422} // namespace pv