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