]> sigrok.org Git - pulseview.git/blob - pv/views/tabular_decoder/model.cpp
TabularDecView-related bug fixes
[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 (!index.isValid() || !signal_)
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
132         if (!all_annotations_)
133                 return QModelIndex();
134
135         if ((size_t)row > all_annotations_->size())
136                 return QModelIndex();
137
138         return createIndex(row, column, (void*)(all_annotations_->at(row)));
139 }
140
141 QModelIndex AnnotationCollectionModel::parent(const QModelIndex& index) const
142 {
143         (void)index;
144
145         return QModelIndex();
146 }
147
148 int AnnotationCollectionModel::rowCount(const QModelIndex& parent_idx) const
149 {
150         (void)parent_idx;
151
152         if (!all_annotations_)
153                 return 0;
154
155         return all_annotations_->size();
156 }
157
158 int AnnotationCollectionModel::columnCount(const QModelIndex& parent_idx) const
159 {
160         (void)parent_idx;
161
162         return header_data_.size();
163 }
164
165 void AnnotationCollectionModel::set_signal_and_segment(data::DecodeSignal* signal, uint32_t current_segment)
166 {
167         if (!signal) {
168                 all_annotations_ = nullptr;
169                 signal_ = nullptr;
170                 dataChanged(QModelIndex(), QModelIndex());
171                 layoutChanged();
172                 return;
173         }
174
175         all_annotations_ = signal->get_all_annotations_by_segment(current_segment);
176         signal_ = signal;
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 }
201
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
212 } // namespace tabular_decoder
213 } // namespace views
214 } // namespace pv