2 * This file is part of the PulseView project.
4 * Copyright (C) 2020 Soeren Apel <soeren@apelpie.net>
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.
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.
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/>.
20 #include <QApplication>
24 #include "pv/views/tabular_decoder/view.hpp"
28 #include "pv/util.hpp"
29 #include "pv/globalsettings.hpp"
31 using std::make_shared;
33 using pv::util::Timestamp;
34 using pv::util::format_time_si;
35 using pv::util::format_time_minutes;
36 using pv::util::SIPrefix;
40 namespace tabular_decoder {
42 AnnotationCollectionModel::AnnotationCollectionModel(QObject* parent) :
43 QAbstractTableModel(parent),
44 all_annotations_(nullptr),
51 had_highlight_before_(false),
54 // TBD Maybe use empty columns as indentation levels to indicate stacked decoders
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
63 int AnnotationCollectionModel::get_hierarchy_level(const Annotation* ann) const
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);
73 QVariant AnnotationCollectionModel::data_from_ann(const Annotation* ann, int index) const
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");
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);
84 s = format_time_minutes(t, 3, false);
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();
95 QVariant AnnotationCollectionModel::data(const QModelIndex& index, int role) const
97 if (!signal_ || !index.isValid() || !index.internalPointer())
100 const Annotation* ann =
101 static_cast<const Annotation*>(index.internalPointer());
103 if ((role == Qt::DisplayRole) || (role == Qt::ToolTipRole))
104 return data_from_ann(ann, index.column());
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_);
113 if (must_highlight) {
114 if (GlobalSettings::current_theme_is_dark())
115 return QApplication::palette().brush(QPalette::Window);
117 return QApplication::palette().brush(QPalette::WindowText);
121 return QApplication::palette().brush(QPalette::WindowText);
124 if (role == Qt::BackgroundRole) {
125 // Only use custom cell background color if column index reached the hierarchy level
126 if (index.column() >= get_hierarchy_level(ann)) {
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_);
134 color = ann->color();
136 color = GlobalSettings::current_theme_is_dark() ?
137 ann->dark_color() : ann->bright_color();
139 return QBrush(color);
146 Qt::ItemFlags AnnotationCollectionModel::flags(const QModelIndex& index) const
148 if (!index.isValid())
149 return Qt::NoItemFlags;
151 return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemNeverHasChildren;
154 QVariant AnnotationCollectionModel::headerData(int section, Qt::Orientation orientation,
157 if ((orientation == Qt::Horizontal) && (role == Qt::DisplayRole))
158 return header_data_.at(section);
163 QModelIndex AnnotationCollectionModel::index(int row, int column,
164 const QModelIndex& parent_idx) const
169 if (!dataset_ || (row < 0))
170 return QModelIndex();
174 if (start_index_ == end_index_) {
175 if ((size_t)row < dataset_->size())
176 idx = createIndex(row, column, (void*)dataset_->at(row));
178 if ((size_t)row < (end_index_ - start_index_))
179 idx = createIndex(row, column, (void*)dataset_->at(start_index_ + row));
185 QModelIndex AnnotationCollectionModel::parent(const QModelIndex& index) const
189 return QModelIndex();
192 int AnnotationCollectionModel::rowCount(const QModelIndex& parent_idx) const
199 if (start_index_ == end_index_)
200 return dataset_->size();
202 return (end_index_ - start_index_);
205 int AnnotationCollectionModel::columnCount(const QModelIndex& parent_idx) const
209 return header_data_.size();
212 void AnnotationCollectionModel::set_signal_and_segment(data::DecodeSignal* signal, uint32_t current_segment)
215 all_annotations_ = nullptr;
219 dataChanged(QModelIndex(), QModelIndex());
224 disconnect(this, SLOT(on_annotation_visibility_changed()));
226 all_annotations_ = signal->get_all_annotations_by_segment(current_segment);
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()));
234 update_annotations_without_hidden();
236 dataset_ = all_annotations_;
238 if (!dataset_ || dataset_->empty()) {
239 prev_segment_ = current_segment;
243 // Re-apply the requested sample range
244 set_sample_range(start_sample_, end_sample_);
246 const size_t new_row_count = dataset_->size() - 1;
248 // Force the view associated with this model to update when the segment changes
249 if (prev_segment_ != current_segment) {
250 dataChanged(QModelIndex(), QModelIndex());
253 // Force the view associated with this model to update when we have more annotations
254 if (prev_last_row_ < new_row_count) {
255 dataChanged(index(prev_last_row_, 0), index(new_row_count, 0));
260 prev_segment_ = current_segment;
261 prev_last_row_ = new_row_count;
264 void AnnotationCollectionModel::set_sample_range(uint64_t start_sample, uint64_t end_sample)
266 // Check if there's even anything to reset
267 if ((start_sample == end_sample) && (start_index_ == end_index_))
270 if (!dataset_ || dataset_->empty() || (end_sample == 0)) {
276 dataChanged(QModelIndex(), QModelIndex());
281 start_sample_ = start_sample;
282 end_sample_ = end_sample;
284 // Determine first and last indices into the annotation list
286 bool ann_outside_range;
290 if (i == (int64_t)dataset_->size()) {
294 dataChanged(QModelIndex(), QModelIndex());
298 const Annotation* ann = (*dataset_)[i];
300 ((ann->start_sample() < start_sample) && (ann->end_sample() < start_sample));
301 } while (ann_outside_range);
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.
314 end_index_ = dataset_->size();
316 dataChanged(index(0, 0), index((end_index_ - start_index_), 0));
320 void AnnotationCollectionModel::set_hide_hidden(bool hide_hidden)
322 hide_hidden_ = hide_hidden;
325 dataset_ = &all_annotations_without_hidden_;
326 update_annotations_without_hidden();
328 dataset_ = all_annotations_;
329 all_annotations_without_hidden_.clear(); // To conserve memory
332 // Re-apply the requested sample range
333 set_sample_range(start_sample_, end_sample_);
336 dataChanged(index(0, 0), index(dataset_->size(), 0));
338 dataChanged(QModelIndex(), QModelIndex());
343 void AnnotationCollectionModel::update_annotations_without_hidden()
347 if (!all_annotations_ || all_annotations_->empty()) {
348 all_annotations_without_hidden_.clear();
352 for (const Annotation* ann : *all_annotations_) {
356 if (all_annotations_without_hidden_.size() < (count + 100))
357 all_annotations_without_hidden_.resize(count + 100);
359 all_annotations_without_hidden_[count++] = ann;
362 all_annotations_without_hidden_.resize(count);
365 void AnnotationCollectionModel::update_highlighted_rows(QModelIndex first,
366 QModelIndex last, int64_t sample_num)
368 bool has_highlight = false;
370 highlight_sample_num_ = sample_num;
372 if (!dataset_ || dataset_->empty())
375 if (sample_num >= 0) {
376 last = last.sibling(last.row() + 1, 0);
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;
382 const Annotation* ann =
383 static_cast<const Annotation*>(index.internalPointer());
386 if (((int64_t)ann->start_sample() <= sample_num) &&
387 ((int64_t)ann->end_sample() >= sample_num)) {
388 has_highlight = true;
392 index = index.sibling(index.row() + 1, 0);
393 } while (index != last);
396 if (has_highlight || had_highlight_before_)
397 dataChanged(first, last);
399 had_highlight_before_ = has_highlight;
402 void AnnotationCollectionModel::on_annotation_visibility_changed()
407 update_annotations_without_hidden();
409 // Re-apply the requested sample range
410 set_sample_range(start_sample_, end_sample_);
413 dataChanged(index(0, 0), index(dataset_->size(), 0));
415 dataChanged(QModelIndex(), QModelIndex());
420 } // namespace tabular_decoder