ffd151a5300c4278a297a45fe040b84f22e4e246
[pulseview.git] / pv / views / tabular_decoder / model.cpp
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
20 #include <QDebug>
21 #include <QString>
22
23 #include "pv/views/tabular_decoder/view.hpp"
24
25 #include "view.hpp"
26
27 #include "pv/util.hpp"
28
29 using std::make_shared;
30
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
36 namespace pv {
37 namespace views {
38 namespace tabular_decoder {
39
40 AnnotationCollectionModel::AnnotationCollectionModel(QObject* parent) :
41         QAbstractTableModel(parent),
42         all_annotations_(nullptr),
43         signal_(nullptr),
44         prev_segment_(0),
45         prev_last_row_(0)
46 {
47         GlobalSettings::add_change_handler(this);
48         theme_is_dark_ = GlobalSettings::current_theme_is_dark();
49
50         // TBD Maybe use empty columns as indentation levels to indicate stacked decoders
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
57 }
58
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
81 QVariant AnnotationCollectionModel::data(const QModelIndex& index, int role) const
82 {
83         if (!signal_ || !index.isValid() || !index.internalPointer())
84                 return QVariant();
85
86         const Annotation* ann =
87                 static_cast<const Annotation*>(index.internalPointer());
88
89         if ((role == Qt::DisplayRole) || (role == Qt::ToolTipRole))
90                 return data_from_ann(ann, index.column());
91
92         if (role == Qt::BackgroundRole) {
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                 }
105         }
106
107         return QVariant();
108 }
109
110 Qt::ItemFlags AnnotationCollectionModel::flags(const QModelIndex& index) const
111 {
112         if (!index.isValid())
113                 return Qt::NoItemFlags;
114
115         return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemNeverHasChildren;
116 }
117
118 QVariant AnnotationCollectionModel::headerData(int section, Qt::Orientation orientation,
119         int role) const
120 {
121         if ((orientation == Qt::Horizontal) && (role == Qt::DisplayRole))
122                 return header_data_.at(section);
123
124         return QVariant();
125 }
126
127 QModelIndex AnnotationCollectionModel::index(int row, int column,
128         const QModelIndex& parent_idx) const
129 {
130         (void)parent_idx;
131         assert(row >= 0);
132         assert(column >= 0);
133
134         if (!all_annotations_)
135                 return QModelIndex();
136
137         if ((size_t)row > all_annotations_->size())
138                 return QModelIndex();
139
140         return createIndex(row, column, (void*)(all_annotations_->at(row)));
141 }
142
143 QModelIndex AnnotationCollectionModel::parent(const QModelIndex& index) const
144 {
145         (void)index;
146
147         return QModelIndex();
148 }
149
150 int AnnotationCollectionModel::rowCount(const QModelIndex& parent_idx) const
151 {
152         (void)parent_idx;
153
154         if (!all_annotations_)
155                 return 0;
156
157         return all_annotations_->size();
158 }
159
160 int AnnotationCollectionModel::columnCount(const QModelIndex& parent_idx) const
161 {
162         (void)parent_idx;
163
164         return header_data_.size();
165 }
166
167 void AnnotationCollectionModel::set_signal_and_segment(data::DecodeSignal* signal, uint32_t current_segment)
168 {
169         if (!signal) {
170                 all_annotations_ = nullptr;
171                 signal_ = nullptr;
172                 dataChanged(QModelIndex(), QModelIndex());
173                 layoutChanged();
174                 return;
175         }
176
177         all_annotations_ = signal->get_all_annotations_by_segment(current_segment);
178         signal_ = signal;
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 }
203
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
214 } // namespace tabular_decoder
215 } // namespace views
216 } // namespace pv