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/>.
23 #include "pv/views/tabular_decoder/view.hpp"
27 #include "pv/util.hpp"
29 using std::make_shared;
31 using pv::util::Timestamp;
32 using pv::util::format_time_si;
33 using pv::util::format_time_minutes;
34 using pv::util::SIPrefix;
38 namespace tabular_decoder {
40 AnnotationCollectionModel::AnnotationCollectionModel(QObject* parent) :
41 QAbstractTableModel(parent),
42 all_annotations_(nullptr),
51 GlobalSettings::add_change_handler(this);
52 theme_is_dark_ = GlobalSettings::current_theme_is_dark();
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 QVariant AnnotationCollectionModel::data_from_ann(const Annotation* ann, int index) const
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");
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);
74 s = format_time_minutes(t, 3, false);
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();
85 QVariant AnnotationCollectionModel::data(const QModelIndex& index, int role) const
87 if (!signal_ || !index.isValid() || !index.internalPointer())
90 const Annotation* ann =
91 static_cast<const Annotation*>(index.internalPointer());
93 if ((role == Qt::DisplayRole) || (role == Qt::ToolTipRole))
94 return data_from_ann(ann, index.column());
96 if (role == Qt::BackgroundRole) {
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);
102 // Only use custom cell background color if column index reached the hierarchy level
103 if (index.column() >= level) {
105 return QBrush(ann->dark_color());
107 return QBrush(ann->bright_color());
114 Qt::ItemFlags AnnotationCollectionModel::flags(const QModelIndex& index) const
116 if (!index.isValid())
117 return Qt::NoItemFlags;
119 return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemNeverHasChildren;
122 QVariant AnnotationCollectionModel::headerData(int section, Qt::Orientation orientation,
125 if ((orientation == Qt::Horizontal) && (role == Qt::DisplayRole))
126 return header_data_.at(section);
131 QModelIndex AnnotationCollectionModel::index(int row, int column,
132 const QModelIndex& parent_idx) const
137 if (!dataset_ || (row < 0))
138 return QModelIndex();
142 if (start_index_ == end_index_) {
143 if ((size_t)row < dataset_->size())
144 idx = createIndex(row, column, (void*)dataset_->at(row));
146 if ((size_t)row < (end_index_ - start_index_))
147 idx = createIndex(row, column, (void*)dataset_->at(start_index_ + row));
153 QModelIndex AnnotationCollectionModel::parent(const QModelIndex& index) const
157 return QModelIndex();
160 int AnnotationCollectionModel::rowCount(const QModelIndex& parent_idx) const
167 if (start_index_ == end_index_)
168 return dataset_->size();
170 return (end_index_ - start_index_);
173 int AnnotationCollectionModel::columnCount(const QModelIndex& parent_idx) const
177 return header_data_.size();
180 void AnnotationCollectionModel::set_signal_and_segment(data::DecodeSignal* signal, uint32_t current_segment)
183 all_annotations_ = nullptr;
187 dataChanged(QModelIndex(), QModelIndex());
192 disconnect(this, SLOT(on_annotation_visibility_changed()));
194 all_annotations_ = signal->get_all_annotations_by_segment(current_segment);
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()));
202 update_annotations_without_hidden();
204 dataset_ = all_annotations_;
206 if (!dataset_ || dataset_->empty()) {
207 prev_segment_ = current_segment;
211 // Re-apply the requested sample range
212 set_sample_range(start_sample_, end_sample_);
214 const size_t new_row_count = dataset_->size() - 1;
216 // Force the view associated with this model to update when the segment changes
217 if (prev_segment_ != current_segment) {
218 dataChanged(QModelIndex(), QModelIndex());
221 // Force the view associated with this model to update when we have more annotations
222 if (prev_last_row_ < new_row_count) {
223 dataChanged(index(prev_last_row_, 0), index(new_row_count, 0));
228 prev_segment_ = current_segment;
229 prev_last_row_ = new_row_count;
232 void AnnotationCollectionModel::set_sample_range(uint64_t start_sample, uint64_t end_sample)
234 // Check if there's even anything to reset
235 if ((start_sample == end_sample) && (start_index_ == end_index_))
238 if (!dataset_ || dataset_->empty() || (end_sample == 0)) {
244 dataChanged(QModelIndex(), QModelIndex());
249 start_sample_ = start_sample;
250 end_sample_ = end_sample;
252 // Determine first and last indices into the annotation list
254 bool ann_outside_range;
258 if (i == (int64_t)dataset_->size()) {
262 dataChanged(QModelIndex(), QModelIndex());
266 const Annotation* ann = (*dataset_)[i];
268 ((ann->start_sample() < start_sample) && (ann->end_sample() < start_sample));
269 } while (ann_outside_range);
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.
282 end_index_ = dataset_->size();
284 dataChanged(index(0, 0), index((end_index_ - start_index_), 0));
288 void AnnotationCollectionModel::set_hide_hidden(bool hide_hidden)
290 hide_hidden_ = hide_hidden;
293 dataset_ = &all_annotations_without_hidden_;
294 update_annotations_without_hidden();
296 dataset_ = all_annotations_;
297 all_annotations_without_hidden_.clear(); // To conserve memory
300 // Re-apply the requested sample range
301 set_sample_range(start_sample_, end_sample_);
304 dataChanged(index(0, 0), index(dataset_->size(), 0));
306 dataChanged(QModelIndex(), QModelIndex());
311 void AnnotationCollectionModel::update_annotations_without_hidden()
315 if (!all_annotations_ || all_annotations_->empty()) {
316 all_annotations_without_hidden_.clear();
320 for (const Annotation* ann : *all_annotations_) {
324 if (all_annotations_without_hidden_.size() < (count + 100))
325 all_annotations_without_hidden_.resize(count + 100);
327 all_annotations_without_hidden_[count++] = ann;
330 all_annotations_without_hidden_.resize(count);
333 void AnnotationCollectionModel::on_setting_changed(const QString &key, const QVariant &value)
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();
343 void AnnotationCollectionModel::on_annotation_visibility_changed()
348 update_annotations_without_hidden();
350 // Re-apply the requested sample range
351 set_sample_range(start_sample_, end_sample_);
354 dataChanged(index(0, 0), index(dataset_->size(), 0));
356 dataChanged(QModelIndex(), QModelIndex());
361 } // namespace tabular_decoder