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"
25 using std::make_shared;
29 namespace tabular_decoder {
31 AnnotationCollectionModel::AnnotationCollectionModel(QObject* parent) :
32 QAbstractTableModel(parent),
33 all_annotations_(nullptr),
37 GlobalSettings::add_change_handler(this);
38 theme_is_dark_ = GlobalSettings::current_theme_is_dark();
40 // TBD Maybe use empty columns as indentation levels to indicate stacked decoders
41 header_data_.emplace_back(tr("Sample")); // Column #0
42 header_data_.emplace_back(tr("Time")); // Column #1
43 header_data_.emplace_back(tr("Decoder")); // Column #2
44 header_data_.emplace_back(tr("Ann Row")); // Column #3
45 header_data_.emplace_back(tr("Ann Class")); // Column #4
46 header_data_.emplace_back(tr("Value")); // Column #5
49 QVariant AnnotationCollectionModel::data(const QModelIndex& index, int role) const
54 const Annotation* ann =
55 static_cast<const Annotation*>(index.internalPointer());
57 if (role == Qt::DisplayRole) {
58 switch (index.column()) {
59 case 0: return QVariant((qulonglong)ann->start_sample()); // Column #0, Start Sample
60 case 1: return QVariant(0/*(qulonglong)ann->start_sample()*/); // Column #1, Start Time
61 case 2: return QVariant(ann->row()->decoder()->name()); // Column #2, Decoder
62 case 3: return QVariant(ann->row()->description()); // Column #3, Ann Row
63 case 4: return QVariant(ann->ann_class_description()); // Column #4, Ann Class
64 case 5: return QVariant(ann->longest_annotation()); // Column #5, Value
65 default: return QVariant();
69 if (role == Qt::BackgroundRole) {
71 return QBrush(ann->dark_color());
73 return QBrush(ann->bright_color());
79 Qt::ItemFlags AnnotationCollectionModel::flags(const QModelIndex& index) const
84 return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
87 QVariant AnnotationCollectionModel::headerData(int section, Qt::Orientation orientation,
90 if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
91 return header_data_.at(section);
96 QModelIndex AnnotationCollectionModel::index(int row, int column,
97 const QModelIndex& parent_idx) const
101 if (!all_annotations_)
102 return QModelIndex();
104 if ((size_t)row > all_annotations_->size())
105 return QModelIndex();
107 return createIndex(row, column, (void*)(all_annotations_->at(row)));
110 QModelIndex AnnotationCollectionModel::parent(const QModelIndex& index) const
114 return QModelIndex();
117 int AnnotationCollectionModel::rowCount(const QModelIndex& parent_idx) const
121 if (!all_annotations_)
124 return all_annotations_->size();
127 int AnnotationCollectionModel::columnCount(const QModelIndex& parent_idx) const
131 return header_data_.size();
134 void AnnotationCollectionModel::set_signal_and_segment(data::DecodeSignal* signal, uint32_t current_segment)
137 all_annotations_ = nullptr;
138 dataChanged(QModelIndex(), QModelIndex());
143 all_annotations_ = signal->get_all_annotations_by_segment(current_segment);
145 if (!all_annotations_ || all_annotations_->empty()) {
146 prev_segment_ = current_segment;
150 const size_t new_row_count = all_annotations_->size() - 1;
152 // Force the view associated with this model to update when the segment changes
153 if (prev_segment_ != current_segment) {
154 dataChanged(QModelIndex(), QModelIndex());
157 // Force the view associated with this model to update when we have more annotations
158 if (prev_last_row_ < new_row_count) {
159 dataChanged(index(prev_last_row_, 0, QModelIndex()),
160 index(new_row_count, 0, QModelIndex()));
165 prev_segment_ = current_segment;
166 prev_last_row_ = new_row_count;
169 void AnnotationCollectionModel::on_setting_changed(const QString &key, const QVariant &value)
174 // We don't really care about the actual setting, we just update the
175 // flag that indicates whether we are using a bright or dark color theme
176 theme_is_dark_ = GlobalSettings::current_theme_is_dark();
179 } // namespace tabular_decoder