]> sigrok.org Git - pulseview.git/blame - pv/views/tabular_decoder/model.cpp
TabularDecView: Model fixes
[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
1c521100 20#include <QApplication>
f54e68b0 21#include <QDebug>
24d69d27
SA
22#include <QString>
23
24#include "pv/views/tabular_decoder/view.hpp"
25
d656b010
SA
26#include "view.hpp"
27
85125b0f 28#include "pv/util.hpp"
8e168b23 29#include "pv/globalsettings.hpp"
85125b0f 30
24d69d27
SA
31using std::make_shared;
32
85125b0f
SA
33using pv::util::Timestamp;
34using pv::util::format_time_si;
35using pv::util::format_time_minutes;
36using pv::util::SIPrefix;
37
24d69d27
SA
38namespace pv {
39namespace views {
40namespace tabular_decoder {
41
42AnnotationCollectionModel::AnnotationCollectionModel(QObject* parent) :
f54e68b0
SA
43 QAbstractTableModel(parent),
44 all_annotations_(nullptr),
86d4b8e3 45 dataset_(nullptr),
d656b010 46 signal_(nullptr),
6f43db70 47 first_hidden_column_(0),
f54e68b0 48 prev_segment_(0),
86d4b8e3 49 prev_last_row_(0),
1c521100 50 had_highlight_before_(false),
86d4b8e3 51 hide_hidden_(false)
24d69d27 52{
6f43db70
SA
53 // Note: when adding entries, consider ViewVisibleFilterProxyModel::filterAcceptsRow()
54
55 uint8_t i = 0;
56 header_data_.emplace_back(tr("Sample")); i++; // Column #0
57 header_data_.emplace_back(tr("Time")); i++; // Column #1
58 header_data_.emplace_back(tr("Decoder")); i++; // Column #2
59 header_data_.emplace_back(tr("Ann Row")); i++; // Column #3
60 header_data_.emplace_back(tr("Ann Class")); i++; // Column #4
61 header_data_.emplace_back(tr("Value")); i++; // Column #5
62
63 first_hidden_column_ = i;
64 header_data_.emplace_back("End Sample"); // Column #6, hidden
24d69d27
SA
65}
66
1c521100
SA
67int AnnotationCollectionModel::get_hierarchy_level(const Annotation* ann) const
68{
69 int level = 0;
70
71 const unsigned int ann_stack_level = ann->row_data()->row()->decoder()->get_stack_level();
72 level = (signal_->decoder_stack().size() - 1 - ann_stack_level);
73
74 return level;
75}
76
85125b0f
SA
77QVariant AnnotationCollectionModel::data_from_ann(const Annotation* ann, int index) const
78{
79 switch (index) {
80 case 0: return QVariant((qulonglong)ann->start_sample()); // Column #0, Start Sample
81 case 1: { // Column #1, Start Time
82 Timestamp t = ann->start_sample() / signal_->get_samplerate();
83 QString unit = signal_->get_samplerate() ? tr("s") : tr("sa");
84 QString s;
85 if ((t < 60) || (signal_->get_samplerate() == 0)) // i.e. if unit is sa
86 s = format_time_si(t, SIPrefix::unspecified, 3, unit, false);
87 else
88 s = format_time_minutes(t, 3, false);
89 return QVariant(s);
90 }
91 case 2: return QVariant(ann->row()->decoder()->name()); // Column #2, Decoder
92 case 3: return QVariant(ann->row()->description()); // Column #3, Ann Row
93 case 4: return QVariant(ann->ann_class_description()); // Column #4, Ann Class
94 case 5: return QVariant(ann->longest_annotation()); // Column #5, Value
6f43db70 95 case 6: return QVariant((qulonglong)ann->end_sample()); // Column #6, End Sample
85125b0f
SA
96 default: return QVariant();
97 }
98}
99
24d69d27
SA
100QVariant AnnotationCollectionModel::data(const QModelIndex& index, int role) const
101{
6d46525f 102 if (!signal_ || !index.isValid() || !index.internalPointer())
24d69d27
SA
103 return QVariant();
104
88a25978
SA
105 const Annotation* ann =
106 static_cast<const Annotation*>(index.internalPointer());
f54e68b0 107
85125b0f
SA
108 if ((role == Qt::DisplayRole) || (role == Qt::ToolTipRole))
109 return data_from_ann(ann, index.column());
24d69d27 110
1c521100
SA
111 if (role == Qt::ForegroundRole) {
112 if (index.column() >= get_hierarchy_level(ann)) {
113 // Invert the text color if this cell is highlighted
114 const bool must_highlight = (highlight_sample_num_ > 0) &&
115 ((int64_t)ann->start_sample() <= highlight_sample_num_) &&
116 ((int64_t)ann->end_sample() >= highlight_sample_num_);
117
118 if (must_highlight) {
119 if (GlobalSettings::current_theme_is_dark())
120 return QApplication::palette().brush(QPalette::Window);
121 else
122 return QApplication::palette().brush(QPalette::WindowText);
123 }
124 }
d656b010 125
1c521100
SA
126 return QApplication::palette().brush(QPalette::WindowText);
127 }
d656b010 128
1c521100 129 if (role == Qt::BackgroundRole) {
d656b010 130 // Only use custom cell background color if column index reached the hierarchy level
1c521100
SA
131 if (index.column() >= get_hierarchy_level(ann)) {
132
133 QColor color;
134 const bool must_highlight = (highlight_sample_num_ > 0) &&
135 ((int64_t)ann->start_sample() <= highlight_sample_num_) &&
136 ((int64_t)ann->end_sample() >= highlight_sample_num_);
137
138 if (must_highlight)
139 color = ann->color();
d656b010 140 else
1c521100
SA
141 color = GlobalSettings::current_theme_is_dark() ?
142 ann->dark_color() : ann->bright_color();
143
144 return QBrush(color);
d656b010 145 }
88a25978
SA
146 }
147
24d69d27
SA
148 return QVariant();
149}
150
151Qt::ItemFlags AnnotationCollectionModel::flags(const QModelIndex& index) const
152{
153 if (!index.isValid())
9a35b05d 154 return Qt::NoItemFlags;
24d69d27 155
9a35b05d 156 return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemNeverHasChildren;
24d69d27
SA
157}
158
6f43db70
SA
159uint8_t AnnotationCollectionModel::first_hidden_column() const
160{
161 return first_hidden_column_;
162}
163
24d69d27
SA
164QVariant AnnotationCollectionModel::headerData(int section, Qt::Orientation orientation,
165 int role) const
166{
6d46525f 167 if ((orientation == Qt::Horizontal) && (role == Qt::DisplayRole))
f54e68b0 168 return header_data_.at(section);
24d69d27
SA
169
170 return QVariant();
171}
172
173QModelIndex AnnotationCollectionModel::index(int row, int column,
174 const QModelIndex& parent_idx) const
175{
f54e68b0 176 (void)parent_idx;
6d46525f 177 assert(column >= 0);
24d69d27 178
593ea025 179 if (!dataset_ || (row < 0))
f54e68b0 180 return QModelIndex();
24d69d27 181
86d4b8e3
SA
182 QModelIndex idx;
183
6f43db70
SA
184 if ((size_t)row < dataset_->size())
185 idx = createIndex(row, column, (void*)dataset_->at(row));
24d69d27 186
86d4b8e3 187 return idx;
24d69d27
SA
188}
189
190QModelIndex AnnotationCollectionModel::parent(const QModelIndex& index) const
191{
f54e68b0 192 (void)index;
24d69d27 193
f54e68b0 194 return QModelIndex();
24d69d27
SA
195}
196
197int AnnotationCollectionModel::rowCount(const QModelIndex& parent_idx) const
198{
f54e68b0 199 (void)parent_idx;
24d69d27 200
86d4b8e3 201 if (!dataset_)
24d69d27
SA
202 return 0;
203
6f43db70 204 return dataset_->size();
24d69d27
SA
205}
206
207int AnnotationCollectionModel::columnCount(const QModelIndex& parent_idx) const
208{
f54e68b0
SA
209 (void)parent_idx;
210
211 return header_data_.size();
24d69d27
SA
212}
213
f54e68b0
SA
214void AnnotationCollectionModel::set_signal_and_segment(data::DecodeSignal* signal, uint32_t current_segment)
215{
49a0a403
SA
216 layoutAboutToBeChanged();
217
88a25978
SA
218 if (!signal) {
219 all_annotations_ = nullptr;
86d4b8e3 220 dataset_ = nullptr;
d656b010 221 signal_ = nullptr;
86d4b8e3 222
88a25978
SA
223 dataChanged(QModelIndex(), QModelIndex());
224 layoutChanged();
225 return;
226 }
227
02078aa1
SA
228 disconnect(this, SLOT(on_annotation_visibility_changed()));
229
f54e68b0 230 all_annotations_ = signal->get_all_annotations_by_segment(current_segment);
d656b010 231 signal_ = signal;
f54e68b0 232
02078aa1
SA
233 for (const shared_ptr<Decoder>& dec : signal_->decoder_stack())
234 connect(dec.get(), SIGNAL(annotation_visibility_changed()),
235 this, SLOT(on_annotation_visibility_changed()));
236
86d4b8e3
SA
237 if (hide_hidden_)
238 update_annotations_without_hidden();
239 else
240 dataset_ = all_annotations_;
241
242 if (!dataset_ || dataset_->empty()) {
f54e68b0
SA
243 prev_segment_ = current_segment;
244 return;
245 }
246
86d4b8e3 247 const size_t new_row_count = dataset_->size() - 1;
f54e68b0
SA
248
249 // Force the view associated with this model to update when the segment changes
250 if (prev_segment_ != current_segment) {
6f43db70 251 dataChanged(index(0, 0), index(new_row_count, 0));
f54e68b0
SA
252 layoutChanged();
253 } else {
254 // Force the view associated with this model to update when we have more annotations
255 if (prev_last_row_ < new_row_count) {
c84afcfd 256 dataChanged(index(prev_last_row_, 0), index(new_row_count, 0));
f54e68b0
SA
257 layoutChanged();
258 }
259 }
260
261 prev_segment_ = current_segment;
262 prev_last_row_ = new_row_count;
263}
24d69d27 264
86d4b8e3
SA
265void AnnotationCollectionModel::set_hide_hidden(bool hide_hidden)
266{
49a0a403
SA
267 layoutAboutToBeChanged();
268
86d4b8e3
SA
269 hide_hidden_ = hide_hidden;
270
271 if (hide_hidden_) {
272 dataset_ = &all_annotations_without_hidden_;
273 update_annotations_without_hidden();
274 } else {
275 dataset_ = all_annotations_;
276 all_annotations_without_hidden_.clear(); // To conserve memory
277 }
8997f62a 278
8997f62a 279 if (dataset_)
6f43db70 280 dataChanged(index(0, 0), index(dataset_->size() - 1, 0));
8997f62a
SA
281 else
282 dataChanged(QModelIndex(), QModelIndex());
283
284 layoutChanged();
86d4b8e3
SA
285}
286
287void AnnotationCollectionModel::update_annotations_without_hidden()
288{
289 uint64_t count = 0;
290
291 if (!all_annotations_ || all_annotations_->empty()) {
292 all_annotations_without_hidden_.clear();
293 return;
294 }
295
296 for (const Annotation* ann : *all_annotations_) {
297 if (!ann->visible())
298 continue;
299
300 if (all_annotations_without_hidden_.size() < (count + 100))
301 all_annotations_without_hidden_.resize(count + 100);
302
303 all_annotations_without_hidden_[count++] = ann;
304 }
305
306 all_annotations_without_hidden_.resize(count);
86d4b8e3
SA
307}
308
6f43db70 309QModelIndex AnnotationCollectionModel::update_highlighted_rows(QModelIndex first,
1c521100
SA
310 QModelIndex last, int64_t sample_num)
311{
312 bool has_highlight = false;
6f43db70 313 QModelIndex result;
1c521100
SA
314
315 highlight_sample_num_ = sample_num;
316
317 if (!dataset_ || dataset_->empty())
6f43db70 318 return result;
1c521100
SA
319
320 if (sample_num >= 0) {
321 last = last.sibling(last.row() + 1, 0);
322
323 // Check if there are any annotations visible in the table view that
324 // we would need to highlight - only then do we do so
325 QModelIndex index = first;
326 do {
327 const Annotation* ann =
328 static_cast<const Annotation*>(index.internalPointer());
329 assert(ann);
330
331 if (((int64_t)ann->start_sample() <= sample_num) &&
332 ((int64_t)ann->end_sample() >= sample_num)) {
6f43db70 333 result = index;
1c521100
SA
334 has_highlight = true;
335 break;
336 }
337
338 index = index.sibling(index.row() + 1, 0);
339 } while (index != last);
340 }
341
49a0a403 342 if (has_highlight || had_highlight_before_)
1c521100
SA
343 dataChanged(first, last);
344
345 had_highlight_before_ = has_highlight;
6f43db70
SA
346
347 return result;
1c521100
SA
348}
349
02078aa1
SA
350void AnnotationCollectionModel::on_annotation_visibility_changed()
351{
352 if (!hide_hidden_)
353 return;
354
49a0a403
SA
355 layoutAboutToBeChanged();
356
02078aa1
SA
357 update_annotations_without_hidden();
358
02078aa1 359 if (dataset_)
6f43db70 360 dataChanged(index(0, 0), index(dataset_->size() - 1, 0));
02078aa1
SA
361 else
362 dataChanged(QModelIndex(), QModelIndex());
363
364 layoutChanged();
365}
366
24d69d27
SA
367} // namespace tabular_decoder
368} // namespace views
369} // namespace pv