]> sigrok.org Git - pulseview.git/blame - pv/views/tabular_decoder/model.cpp
DecodeSignal: Sort annotations by start sample and length
[pulseview.git] / pv / views / tabular_decoder / model.cpp
CommitLineData
24d69d27
SA
1/*
2 * This file is part of the PulseView project.
3 *
4 * Copyright (C) 2020 Soeren Apel <soeren@apelpie.net>
5 *
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.
10 *
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.
15 *
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/>.
18 */
19
f54e68b0 20#include <QDebug>
24d69d27
SA
21#include <QString>
22
23#include "pv/views/tabular_decoder/view.hpp"
24
25using std::make_shared;
26
27namespace pv {
28namespace views {
29namespace tabular_decoder {
30
31AnnotationCollectionModel::AnnotationCollectionModel(QObject* parent) :
f54e68b0
SA
32 QAbstractTableModel(parent),
33 all_annotations_(nullptr),
34 prev_segment_(0),
35 prev_last_row_(0)
24d69d27 36{
88a25978
SA
37 GlobalSettings::add_change_handler(this);
38 theme_is_dark_ = GlobalSettings::current_theme_is_dark();
39
f54e68b0 40 // TBD Maybe use empty columns as indentation levels to indicate stacked decoders
88a25978
SA
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
24d69d27
SA
47}
48
49QVariant AnnotationCollectionModel::data(const QModelIndex& index, int role) const
50{
51 if (!index.isValid())
52 return QVariant();
53
88a25978
SA
54 const Annotation* ann =
55 static_cast<const Annotation*>(index.internalPointer());
f54e68b0 56
88a25978 57 if (role == Qt::DisplayRole) {
f54e68b0
SA
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
88a25978
SA
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
f54e68b0
SA
65 default: return QVariant();
66 }
24d69d27
SA
67 }
68
88a25978
SA
69 if (role == Qt::BackgroundRole) {
70 if (theme_is_dark_)
71 return QBrush(ann->dark_color());
72 else
73 return QBrush(ann->bright_color());
74 }
75
24d69d27
SA
76 return QVariant();
77}
78
79Qt::ItemFlags AnnotationCollectionModel::flags(const QModelIndex& index) const
80{
81 if (!index.isValid())
f54e68b0 82 return 0;
24d69d27
SA
83
84 return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
85}
86
87QVariant AnnotationCollectionModel::headerData(int section, Qt::Orientation orientation,
88 int role) const
89{
90 if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
f54e68b0 91 return header_data_.at(section);
24d69d27
SA
92
93 return QVariant();
94}
95
96QModelIndex AnnotationCollectionModel::index(int row, int column,
97 const QModelIndex& parent_idx) const
98{
f54e68b0 99 (void)parent_idx;
24d69d27 100
f54e68b0
SA
101 if (!all_annotations_)
102 return QModelIndex();
24d69d27 103
f54e68b0
SA
104 if ((size_t)row > all_annotations_->size())
105 return QModelIndex();
24d69d27 106
f54e68b0 107 return createIndex(row, column, (void*)(all_annotations_->at(row)));
24d69d27
SA
108}
109
110QModelIndex AnnotationCollectionModel::parent(const QModelIndex& index) const
111{
f54e68b0 112 (void)index;
24d69d27 113
f54e68b0 114 return QModelIndex();
24d69d27
SA
115}
116
117int AnnotationCollectionModel::rowCount(const QModelIndex& parent_idx) const
118{
f54e68b0 119 (void)parent_idx;
24d69d27 120
f54e68b0 121 if (!all_annotations_)
24d69d27
SA
122 return 0;
123
f54e68b0 124 return all_annotations_->size();
24d69d27
SA
125}
126
127int AnnotationCollectionModel::columnCount(const QModelIndex& parent_idx) const
128{
f54e68b0
SA
129 (void)parent_idx;
130
131 return header_data_.size();
24d69d27
SA
132}
133
f54e68b0
SA
134void AnnotationCollectionModel::set_signal_and_segment(data::DecodeSignal* signal, uint32_t current_segment)
135{
88a25978
SA
136 if (!signal) {
137 all_annotations_ = nullptr;
138 dataChanged(QModelIndex(), QModelIndex());
139 layoutChanged();
140 return;
141 }
142
f54e68b0
SA
143 all_annotations_ = signal->get_all_annotations_by_segment(current_segment);
144
145 if (!all_annotations_ || all_annotations_->empty()) {
146 prev_segment_ = current_segment;
147 return;
148 }
149
150 const size_t new_row_count = all_annotations_->size() - 1;
151
152 // Force the view associated with this model to update when the segment changes
153 if (prev_segment_ != current_segment) {
154 dataChanged(QModelIndex(), QModelIndex());
155 layoutChanged();
156 } else {
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()));
161 layoutChanged();
162 }
163 }
164
165 prev_segment_ = current_segment;
166 prev_last_row_ = new_row_count;
167}
24d69d27 168
88a25978
SA
169void AnnotationCollectionModel::on_setting_changed(const QString &key, const QVariant &value)
170{
171 (void)key;
172 (void)value;
173
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();
177}
178
24d69d27
SA
179} // namespace tabular_decoder
180} // namespace views
181} // namespace pv