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/>.
20 #include <QApplication>
24 #include "pv/views/tabular_decoder/view.hpp"
28 #include "pv/util.hpp"
29 #include "pv/globalsettings.hpp"
31 using std::make_shared;
33 using pv::util::Timestamp;
34 using pv::util::format_time_si;
35 using pv::util::format_time_minutes;
36 using pv::util::SIPrefix;
40 namespace tabular_decoder {
42 AnnotationCollectionModel::AnnotationCollectionModel(QObject* parent) :
43 QAbstractTableModel(parent),
44 all_annotations_(nullptr),
47 first_hidden_column_(0),
50 had_highlight_before_(false),
53 // Note: when adding entries, consider ViewVisibleFilterProxyModel::filterAcceptsRow()
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
63 first_hidden_column_ = i;
64 header_data_.emplace_back("End Sample"); // Column #6, hidden
67 int AnnotationCollectionModel::get_hierarchy_level(const Annotation* ann) const
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);
77 QVariant AnnotationCollectionModel::data_from_ann(const Annotation* ann, int index) const
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");
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);
88 s = format_time_minutes(t, 3, false);
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
95 case 6: return QVariant((qulonglong)ann->end_sample()); // Column #6, End Sample
96 default: return QVariant();
100 QVariant AnnotationCollectionModel::data(const QModelIndex& index, int role) const
102 if (!signal_ || !index.isValid() || !index.internalPointer())
105 const Annotation* ann =
106 static_cast<const Annotation*>(index.internalPointer());
108 if ((role == Qt::DisplayRole) || (role == Qt::ToolTipRole))
109 return data_from_ann(ann, index.column());
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_);
118 if (must_highlight) {
119 if (GlobalSettings::current_theme_is_dark())
120 return QApplication::palette().brush(QPalette::Window);
122 return QApplication::palette().brush(QPalette::WindowText);
126 return QApplication::palette().brush(QPalette::WindowText);
129 if (role == Qt::BackgroundRole) {
130 // Only use custom cell background color if column index reached the hierarchy level
131 if (index.column() >= get_hierarchy_level(ann)) {
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_);
139 color = ann->color();
141 color = GlobalSettings::current_theme_is_dark() ?
142 ann->dark_color() : ann->bright_color();
144 return QBrush(color);
151 Qt::ItemFlags AnnotationCollectionModel::flags(const QModelIndex& index) const
153 if (!index.isValid())
154 return Qt::NoItemFlags;
156 return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemNeverHasChildren;
159 uint8_t AnnotationCollectionModel::first_hidden_column() const
161 return first_hidden_column_;
164 QVariant AnnotationCollectionModel::headerData(int section, Qt::Orientation orientation,
167 if ((section < 0) || (section >= (int)header_data_.size()))
170 if ((orientation == Qt::Horizontal) && (role == Qt::DisplayRole))
171 return header_data_[section];
176 QModelIndex AnnotationCollectionModel::index(int row, int column,
177 const QModelIndex& parent_idx) const
182 if (!dataset_ || (row < 0))
183 return QModelIndex();
187 if ((size_t)row < dataset_->size())
188 idx = createIndex(row, column, (void*)dataset_->at(row));
193 QModelIndex AnnotationCollectionModel::parent(const QModelIndex& index) const
197 return QModelIndex();
200 int AnnotationCollectionModel::rowCount(const QModelIndex& parent_idx) const
207 return dataset_->size();
210 int AnnotationCollectionModel::columnCount(const QModelIndex& parent_idx) const
214 return header_data_.size();
217 void AnnotationCollectionModel::set_signal_and_segment(data::DecodeSignal* signal, uint32_t current_segment)
219 layoutAboutToBeChanged();
222 all_annotations_ = nullptr;
226 dataChanged(QModelIndex(), QModelIndex());
231 disconnect(this, SLOT(on_annotation_visibility_changed()));
233 all_annotations_ = signal->get_all_annotations_by_segment(current_segment);
236 for (const shared_ptr<Decoder>& dec : signal_->decoder_stack())
237 connect(dec.get(), SIGNAL(annotation_visibility_changed()),
238 this, SLOT(on_annotation_visibility_changed()));
241 update_annotations_without_hidden();
243 dataset_ = all_annotations_;
245 if (!dataset_ || dataset_->empty()) {
246 prev_segment_ = current_segment;
250 const size_t new_row_count = dataset_->size() - 1;
252 // Force the view associated with this model to update when the segment changes
253 if (prev_segment_ != current_segment) {
254 dataChanged(index(0, 0), index(new_row_count, 0));
257 // Force the view associated with this model to update when we have more annotations
258 if (prev_last_row_ < new_row_count) {
259 dataChanged(index(prev_last_row_, 0), index(new_row_count, 0));
264 prev_segment_ = current_segment;
265 prev_last_row_ = new_row_count;
268 void AnnotationCollectionModel::set_hide_hidden(bool hide_hidden)
270 layoutAboutToBeChanged();
272 hide_hidden_ = hide_hidden;
275 dataset_ = &all_annotations_without_hidden_;
276 update_annotations_without_hidden();
278 dataset_ = all_annotations_;
279 all_annotations_without_hidden_.clear(); // To conserve memory
283 dataChanged(index(0, 0), index(dataset_->size() - 1, 0));
285 dataChanged(QModelIndex(), QModelIndex());
290 void AnnotationCollectionModel::update_annotations_without_hidden()
294 if (!all_annotations_ || all_annotations_->empty()) {
295 all_annotations_without_hidden_.clear();
299 for (const Annotation* ann : *all_annotations_) {
303 if (all_annotations_without_hidden_.size() < (count + 100))
304 all_annotations_without_hidden_.resize(count + 100);
306 all_annotations_without_hidden_[count++] = ann;
309 all_annotations_without_hidden_.resize(count);
312 QModelIndex AnnotationCollectionModel::update_highlighted_rows(QModelIndex first,
313 QModelIndex last, int64_t sample_num)
315 bool has_highlight = false;
318 highlight_sample_num_ = sample_num;
320 if (!dataset_ || dataset_->empty())
323 if (sample_num >= 0) {
324 last = last.sibling(last.row() + 1, 0);
326 // Check if there are any annotations visible in the table view that
327 // we would need to highlight - only then do we do so
328 QModelIndex index = first;
330 const Annotation* ann = static_cast<const Annotation*>(index.internalPointer());
331 if (!ann) // Can happen if the table is being modified at this exact time
334 if (((int64_t)ann->start_sample() <= sample_num) &&
335 ((int64_t)ann->end_sample() >= sample_num)) {
337 has_highlight = true;
341 index = index.sibling(index.row() + 1, 0);
342 } while (index != last);
345 if (has_highlight || had_highlight_before_)
346 dataChanged(first, last);
348 had_highlight_before_ = has_highlight;
353 void AnnotationCollectionModel::on_annotation_visibility_changed()
358 layoutAboutToBeChanged();
360 update_annotations_without_hidden();
363 dataChanged(index(0, 0), index(dataset_->size() - 1, 0));
365 dataChanged(QModelIndex(), QModelIndex());
370 } // namespace tabular_decoder