]>
Commit | Line | Data |
---|---|---|
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 | ||
d656b010 SA |
25 | #include "view.hpp" |
26 | ||
85125b0f SA |
27 | #include "pv/util.hpp" |
28 | ||
24d69d27 SA |
29 | using std::make_shared; |
30 | ||
85125b0f SA |
31 | using pv::util::Timestamp; |
32 | using pv::util::format_time_si; | |
33 | using pv::util::format_time_minutes; | |
34 | using pv::util::SIPrefix; | |
35 | ||
24d69d27 SA |
36 | namespace pv { |
37 | namespace views { | |
38 | namespace tabular_decoder { | |
39 | ||
40 | AnnotationCollectionModel::AnnotationCollectionModel(QObject* parent) : | |
f54e68b0 SA |
41 | QAbstractTableModel(parent), |
42 | all_annotations_(nullptr), | |
d656b010 | 43 | signal_(nullptr), |
f54e68b0 SA |
44 | prev_segment_(0), |
45 | prev_last_row_(0) | |
24d69d27 | 46 | { |
88a25978 SA |
47 | GlobalSettings::add_change_handler(this); |
48 | theme_is_dark_ = GlobalSettings::current_theme_is_dark(); | |
49 | ||
f54e68b0 | 50 | // TBD Maybe use empty columns as indentation levels to indicate stacked decoders |
88a25978 SA |
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 | |
24d69d27 SA |
57 | } |
58 | ||
85125b0f SA |
59 | QVariant AnnotationCollectionModel::data_from_ann(const Annotation* ann, int index) const |
60 | { | |
61 | switch (index) { | |
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"); | |
66 | QString s; | |
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); | |
69 | else | |
70 | s = format_time_minutes(t, 3, false); | |
71 | return QVariant(s); | |
72 | } | |
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(); | |
78 | } | |
79 | } | |
80 | ||
24d69d27 SA |
81 | QVariant AnnotationCollectionModel::data(const QModelIndex& index, int role) const |
82 | { | |
d656b010 | 83 | if (!index.isValid() || !signal_) |
24d69d27 SA |
84 | return QVariant(); |
85 | ||
88a25978 SA |
86 | const Annotation* ann = |
87 | static_cast<const Annotation*>(index.internalPointer()); | |
f54e68b0 | 88 | |
85125b0f SA |
89 | if ((role == Qt::DisplayRole) || (role == Qt::ToolTipRole)) |
90 | return data_from_ann(ann, index.column()); | |
24d69d27 | 91 | |
88a25978 | 92 | if (role == Qt::BackgroundRole) { |
d656b010 SA |
93 | int level = 0; |
94 | ||
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); | |
97 | ||
98 | // Only use custom cell background color if column index reached the hierarchy level | |
99 | if (index.column() >= level) { | |
100 | if (theme_is_dark_) | |
101 | return QBrush(ann->dark_color()); | |
102 | else | |
103 | return QBrush(ann->bright_color()); | |
104 | } | |
88a25978 SA |
105 | } |
106 | ||
24d69d27 SA |
107 | return QVariant(); |
108 | } | |
109 | ||
110 | Qt::ItemFlags AnnotationCollectionModel::flags(const QModelIndex& index) const | |
111 | { | |
112 | if (!index.isValid()) | |
9a35b05d | 113 | return Qt::NoItemFlags; |
24d69d27 | 114 | |
9a35b05d | 115 | return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemNeverHasChildren; |
24d69d27 SA |
116 | } |
117 | ||
118 | QVariant AnnotationCollectionModel::headerData(int section, Qt::Orientation orientation, | |
119 | int role) const | |
120 | { | |
121 | if (orientation == Qt::Horizontal && role == Qt::DisplayRole) | |
f54e68b0 | 122 | return header_data_.at(section); |
24d69d27 SA |
123 | |
124 | return QVariant(); | |
125 | } | |
126 | ||
127 | QModelIndex AnnotationCollectionModel::index(int row, int column, | |
128 | const QModelIndex& parent_idx) const | |
129 | { | |
f54e68b0 | 130 | (void)parent_idx; |
24d69d27 | 131 | |
f54e68b0 SA |
132 | if (!all_annotations_) |
133 | return QModelIndex(); | |
24d69d27 | 134 | |
f54e68b0 SA |
135 | if ((size_t)row > all_annotations_->size()) |
136 | return QModelIndex(); | |
24d69d27 | 137 | |
f54e68b0 | 138 | return createIndex(row, column, (void*)(all_annotations_->at(row))); |
24d69d27 SA |
139 | } |
140 | ||
141 | QModelIndex AnnotationCollectionModel::parent(const QModelIndex& index) const | |
142 | { | |
f54e68b0 | 143 | (void)index; |
24d69d27 | 144 | |
f54e68b0 | 145 | return QModelIndex(); |
24d69d27 SA |
146 | } |
147 | ||
148 | int AnnotationCollectionModel::rowCount(const QModelIndex& parent_idx) const | |
149 | { | |
f54e68b0 | 150 | (void)parent_idx; |
24d69d27 | 151 | |
f54e68b0 | 152 | if (!all_annotations_) |
24d69d27 SA |
153 | return 0; |
154 | ||
f54e68b0 | 155 | return all_annotations_->size(); |
24d69d27 SA |
156 | } |
157 | ||
158 | int AnnotationCollectionModel::columnCount(const QModelIndex& parent_idx) const | |
159 | { | |
f54e68b0 SA |
160 | (void)parent_idx; |
161 | ||
162 | return header_data_.size(); | |
24d69d27 SA |
163 | } |
164 | ||
f54e68b0 SA |
165 | void AnnotationCollectionModel::set_signal_and_segment(data::DecodeSignal* signal, uint32_t current_segment) |
166 | { | |
88a25978 SA |
167 | if (!signal) { |
168 | all_annotations_ = nullptr; | |
d656b010 | 169 | signal_ = nullptr; |
88a25978 SA |
170 | dataChanged(QModelIndex(), QModelIndex()); |
171 | layoutChanged(); | |
172 | return; | |
173 | } | |
174 | ||
f54e68b0 | 175 | all_annotations_ = signal->get_all_annotations_by_segment(current_segment); |
d656b010 | 176 | signal_ = signal; |
f54e68b0 SA |
177 | |
178 | if (!all_annotations_ || all_annotations_->empty()) { | |
179 | prev_segment_ = current_segment; | |
180 | return; | |
181 | } | |
182 | ||
183 | const size_t new_row_count = all_annotations_->size() - 1; | |
184 | ||
185 | // Force the view associated with this model to update when the segment changes | |
186 | if (prev_segment_ != current_segment) { | |
187 | dataChanged(QModelIndex(), QModelIndex()); | |
188 | layoutChanged(); | |
189 | } else { | |
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())); | |
194 | layoutChanged(); | |
195 | } | |
196 | } | |
197 | ||
198 | prev_segment_ = current_segment; | |
199 | prev_last_row_ = new_row_count; | |
200 | } | |
24d69d27 | 201 | |
88a25978 SA |
202 | void AnnotationCollectionModel::on_setting_changed(const QString &key, const QVariant &value) |
203 | { | |
204 | (void)key; | |
205 | (void)value; | |
206 | ||
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(); | |
210 | } | |
211 | ||
24d69d27 SA |
212 | } // namespace tabular_decoder |
213 | } // namespace views | |
214 | } // namespace pv |