]>
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 | { | |
6d46525f | 83 | if (!signal_ || !index.isValid() || !index.internalPointer()) |
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 | { | |
6d46525f | 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; |
6d46525f SA |
131 | assert(row >= 0); |
132 | assert(column >= 0); | |
24d69d27 | 133 | |
f54e68b0 SA |
134 | if (!all_annotations_) |
135 | return QModelIndex(); | |
24d69d27 | 136 | |
f54e68b0 SA |
137 | if ((size_t)row > all_annotations_->size()) |
138 | return QModelIndex(); | |
24d69d27 | 139 | |
f54e68b0 | 140 | return createIndex(row, column, (void*)(all_annotations_->at(row))); |
24d69d27 SA |
141 | } |
142 | ||
143 | QModelIndex AnnotationCollectionModel::parent(const QModelIndex& index) const | |
144 | { | |
f54e68b0 | 145 | (void)index; |
24d69d27 | 146 | |
f54e68b0 | 147 | return QModelIndex(); |
24d69d27 SA |
148 | } |
149 | ||
150 | int AnnotationCollectionModel::rowCount(const QModelIndex& parent_idx) const | |
151 | { | |
f54e68b0 | 152 | (void)parent_idx; |
24d69d27 | 153 | |
f54e68b0 | 154 | if (!all_annotations_) |
24d69d27 SA |
155 | return 0; |
156 | ||
f54e68b0 | 157 | return all_annotations_->size(); |
24d69d27 SA |
158 | } |
159 | ||
160 | int AnnotationCollectionModel::columnCount(const QModelIndex& parent_idx) const | |
161 | { | |
f54e68b0 SA |
162 | (void)parent_idx; |
163 | ||
164 | return header_data_.size(); | |
24d69d27 SA |
165 | } |
166 | ||
f54e68b0 SA |
167 | void AnnotationCollectionModel::set_signal_and_segment(data::DecodeSignal* signal, uint32_t current_segment) |
168 | { | |
88a25978 SA |
169 | if (!signal) { |
170 | all_annotations_ = nullptr; | |
d656b010 | 171 | signal_ = nullptr; |
88a25978 SA |
172 | dataChanged(QModelIndex(), QModelIndex()); |
173 | layoutChanged(); | |
174 | return; | |
175 | } | |
176 | ||
f54e68b0 | 177 | all_annotations_ = signal->get_all_annotations_by_segment(current_segment); |
d656b010 | 178 | signal_ = signal; |
f54e68b0 SA |
179 | |
180 | if (!all_annotations_ || all_annotations_->empty()) { | |
181 | prev_segment_ = current_segment; | |
182 | return; | |
183 | } | |
184 | ||
185 | const size_t new_row_count = all_annotations_->size() - 1; | |
186 | ||
187 | // Force the view associated with this model to update when the segment changes | |
188 | if (prev_segment_ != current_segment) { | |
189 | dataChanged(QModelIndex(), QModelIndex()); | |
190 | layoutChanged(); | |
191 | } else { | |
192 | // Force the view associated with this model to update when we have more annotations | |
193 | if (prev_last_row_ < new_row_count) { | |
194 | dataChanged(index(prev_last_row_, 0, QModelIndex()), | |
195 | index(new_row_count, 0, QModelIndex())); | |
196 | layoutChanged(); | |
197 | } | |
198 | } | |
199 | ||
200 | prev_segment_ = current_segment; | |
201 | prev_last_row_ = new_row_count; | |
202 | } | |
24d69d27 | 203 | |
88a25978 SA |
204 | void AnnotationCollectionModel::on_setting_changed(const QString &key, const QVariant &value) |
205 | { | |
206 | (void)key; | |
207 | (void)value; | |
208 | ||
209 | // We don't really care about the actual setting, we just update the | |
210 | // flag that indicates whether we are using a bright or dark color theme | |
211 | theme_is_dark_ = GlobalSettings::current_theme_is_dark(); | |
212 | } | |
213 | ||
24d69d27 SA |
214 | } // namespace tabular_decoder |
215 | } // namespace views | |
216 | } // namespace pv |