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),
47 GlobalSettings::add_change_handler(this);
48 theme_is_dark_ = GlobalSettings::current_theme_is_dark();
50 // TBD Maybe use empty columns as indentation levels to indicate stacked decoders
51 header_data_.emplace_back(tr("Sample")); // Column #0
52 header_data_.emplace_back(tr("Time")); // Column #1
53 header_data_.emplace_back(tr("Decoder")); // Column #2
54 header_data_.emplace_back(tr("Ann Row")); // Column #3
55 header_data_.emplace_back(tr("Ann Class")); // Column #4
56 header_data_.emplace_back(tr("Value")); // Column #5
59 QVariant AnnotationCollectionModel::data_from_ann(const Annotation* ann, int index) const
62 case 0: return QVariant((qulonglong)ann->start_sample()); // Column #0, Start Sample
63 case 1: { // Column #1, Start Time
64 Timestamp t = ann->start_sample() / signal_->get_samplerate();
65 QString unit = signal_->get_samplerate() ? tr("s") : tr("sa");
67 if ((t < 60) || (signal_->get_samplerate() == 0)) // i.e. if unit is sa
68 s = format_time_si(t, SIPrefix::unspecified, 3, unit, false);
70 s = format_time_minutes(t, 3, false);
73 case 2: return QVariant(ann->row()->decoder()->name()); // Column #2, Decoder
74 case 3: return QVariant(ann->row()->description()); // Column #3, Ann Row
75 case 4: return QVariant(ann->ann_class_description()); // Column #4, Ann Class
76 case 5: return QVariant(ann->longest_annotation()); // Column #5, Value
77 default: return QVariant();
81 QVariant AnnotationCollectionModel::data(const QModelIndex& index, int role) const
83 if (!index.isValid() || !signal_)
86 const Annotation* ann =
87 static_cast<const Annotation*>(index.internalPointer());
89 if ((role == Qt::DisplayRole) || (role == Qt::ToolTipRole))
90 return data_from_ann(ann, index.column());
92 if (role == Qt::BackgroundRole) {
95 const unsigned int ann_stack_level = ann->row_data()->row()->decoder()->get_stack_level();
96 level = (signal_->decoder_stack().size() - 1 - ann_stack_level);
98 // Only use custom cell background color if column index reached the hierarchy level
99 if (index.column() >= level) {
101 return QBrush(ann->dark_color());
103 return QBrush(ann->bright_color());
110 Qt::ItemFlags AnnotationCollectionModel::flags(const QModelIndex& index) const
112 if (!index.isValid())
113 return Qt::NoItemFlags;
115 return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemNeverHasChildren;
118 QVariant AnnotationCollectionModel::headerData(int section, Qt::Orientation orientation,
121 if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
122 return header_data_.at(section);
127 QModelIndex AnnotationCollectionModel::index(int row, int column,
128 const QModelIndex& parent_idx) const
132 if (!all_annotations_)
133 return QModelIndex();
135 if ((size_t)row > all_annotations_->size())
136 return QModelIndex();
138 return createIndex(row, column, (void*)(all_annotations_->at(row)));
141 QModelIndex AnnotationCollectionModel::parent(const QModelIndex& index) const
145 return QModelIndex();
148 int AnnotationCollectionModel::rowCount(const QModelIndex& parent_idx) const
152 if (!all_annotations_)
155 return all_annotations_->size();
158 int AnnotationCollectionModel::columnCount(const QModelIndex& parent_idx) const
162 return header_data_.size();
165 void AnnotationCollectionModel::set_signal_and_segment(data::DecodeSignal* signal, uint32_t current_segment)
168 all_annotations_ = nullptr;
170 dataChanged(QModelIndex(), QModelIndex());
175 all_annotations_ = signal->get_all_annotations_by_segment(current_segment);
178 if (!all_annotations_ || all_annotations_->empty()) {
179 prev_segment_ = current_segment;
183 const size_t new_row_count = all_annotations_->size() - 1;
185 // Force the view associated with this model to update when the segment changes
186 if (prev_segment_ != current_segment) {
187 dataChanged(QModelIndex(), QModelIndex());
190 // Force the view associated with this model to update when we have more annotations
191 if (prev_last_row_ < new_row_count) {
192 dataChanged(index(prev_last_row_, 0, QModelIndex()),
193 index(new_row_count, 0, QModelIndex()));
198 prev_segment_ = current_segment;
199 prev_last_row_ = new_row_count;
202 void AnnotationCollectionModel::on_setting_changed(const QString &key, const QVariant &value)
207 // We don't really care about the actual setting, we just update the
208 // flag that indicates whether we are using a bright or dark color theme
209 theme_is_dark_ = GlobalSettings::current_theme_is_dark();
212 } // namespace tabular_decoder