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 using std::make_shared;
31 namespace tabular_decoder {
33 AnnotationCollectionModel::AnnotationCollectionModel(QObject* parent) :
34 QAbstractTableModel(parent),
35 all_annotations_(nullptr),
40 GlobalSettings::add_change_handler(this);
41 theme_is_dark_ = GlobalSettings::current_theme_is_dark();
43 // TBD Maybe use empty columns as indentation levels to indicate stacked decoders
44 header_data_.emplace_back(tr("Sample")); // Column #0
45 header_data_.emplace_back(tr("Time")); // Column #1
46 header_data_.emplace_back(tr("Decoder")); // Column #2
47 header_data_.emplace_back(tr("Ann Row")); // Column #3
48 header_data_.emplace_back(tr("Ann Class")); // Column #4
49 header_data_.emplace_back(tr("Value")); // Column #5
52 QVariant AnnotationCollectionModel::data(const QModelIndex& index, int role) const
54 if (!index.isValid() || !signal_)
57 const Annotation* ann =
58 static_cast<const Annotation*>(index.internalPointer());
60 if (role == Qt::DisplayRole) {
61 switch (index.column()) {
62 case 0: return QVariant((qulonglong)ann->start_sample()); // Column #0, Start Sample
63 case 1: return QVariant(0/*(qulonglong)ann->start_sample()*/); // Column #1, Start Time
64 case 2: return QVariant(ann->row()->decoder()->name()); // Column #2, Decoder
65 case 3: return QVariant(ann->row()->description()); // Column #3, Ann Row
66 case 4: return QVariant(ann->ann_class_description()); // Column #4, Ann Class
67 case 5: return QVariant(ann->longest_annotation()); // Column #5, Value
68 default: return QVariant();
72 if (role == Qt::BackgroundRole) {
75 const unsigned int ann_stack_level = ann->row_data()->row()->decoder()->get_stack_level();
76 level = (signal_->decoder_stack().size() - 1 - ann_stack_level);
78 // Only use custom cell background color if column index reached the hierarchy level
79 if (index.column() >= level) {
81 return QBrush(ann->dark_color());
83 return QBrush(ann->bright_color());
90 Qt::ItemFlags AnnotationCollectionModel::flags(const QModelIndex& index) const
95 return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
98 QVariant AnnotationCollectionModel::headerData(int section, Qt::Orientation orientation,
101 if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
102 return header_data_.at(section);
107 QModelIndex AnnotationCollectionModel::index(int row, int column,
108 const QModelIndex& parent_idx) const
112 if (!all_annotations_)
113 return QModelIndex();
115 if ((size_t)row > all_annotations_->size())
116 return QModelIndex();
118 return createIndex(row, column, (void*)(all_annotations_->at(row)));
121 QModelIndex AnnotationCollectionModel::parent(const QModelIndex& index) const
125 return QModelIndex();
128 int AnnotationCollectionModel::rowCount(const QModelIndex& parent_idx) const
132 if (!all_annotations_)
135 return all_annotations_->size();
138 int AnnotationCollectionModel::columnCount(const QModelIndex& parent_idx) const
142 return header_data_.size();
145 void AnnotationCollectionModel::set_signal_and_segment(data::DecodeSignal* signal, uint32_t current_segment)
148 all_annotations_ = nullptr;
150 dataChanged(QModelIndex(), QModelIndex());
155 all_annotations_ = signal->get_all_annotations_by_segment(current_segment);
158 if (!all_annotations_ || all_annotations_->empty()) {
159 prev_segment_ = current_segment;
163 const size_t new_row_count = all_annotations_->size() - 1;
165 // Force the view associated with this model to update when the segment changes
166 if (prev_segment_ != current_segment) {
167 dataChanged(QModelIndex(), QModelIndex());
170 // Force the view associated with this model to update when we have more annotations
171 if (prev_last_row_ < new_row_count) {
172 dataChanged(index(prev_last_row_, 0, QModelIndex()),
173 index(new_row_count, 0, QModelIndex()));
178 prev_segment_ = current_segment;
179 prev_last_row_ = new_row_count;
182 void AnnotationCollectionModel::on_setting_changed(const QString &key, const QVariant &value)
187 // We don't really care about the actual setting, we just update the
188 // flag that indicates whether we are using a bright or dark color theme
189 theme_is_dark_ = GlobalSettings::current_theme_is_dark();
192 } // namespace tabular_decoder