]> sigrok.org Git - pulseview.git/blob - pv/data/decode/row.cpp
Move row/annotation color management out of DecodeTrace
[pulseview.git] / pv / data / decode / row.cpp
1 /*
2  * This file is part of the PulseView project.
3  *
4  * Copyright (C) 2014 Joel Holdsworth <joel@airwebreathe.org.uk>
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 <cassert>
21
22 #include "decoder.hpp"
23 #include "row.hpp"
24
25 #include <libsigrokdecode/libsigrokdecode.h>
26
27 namespace pv {
28 namespace data {
29 namespace decode {
30
31 Row::Row() :
32         index_(0),
33         decoder_(nullptr),
34         srd_row_(nullptr),
35         visible_(true)
36 {
37 }
38
39 Row::Row(uint32_t index, Decoder* decoder, const srd_decoder_annotation_row* srd_row) :
40         index_(index),
41         decoder_(decoder),
42         srd_row_(srd_row),
43         visible_(true)
44 {
45 }
46
47 const Decoder* Row::decoder() const
48 {
49         return decoder_;
50 }
51
52 const srd_decoder_annotation_row* Row::get_srd_row() const
53 {
54         return srd_row_;
55 }
56
57 const QString Row::title() const
58 {
59         if (decoder_ && decoder_->name() && srd_row_ && srd_row_->desc)
60                 return QString("%1: %2")
61                         .arg(QString::fromUtf8(decoder_->name()),
62                              QString::fromUtf8(srd_row_->desc));
63         if (decoder_ && decoder_->name())
64                 return QString::fromUtf8(decoder_->name());
65         if (srd_row_ && srd_row_->desc)
66                 return QString::fromUtf8(srd_row_->desc);
67
68         return QString();
69 }
70
71 const QString Row::description() const
72 {
73         if (srd_row_ && srd_row_->desc)
74                 return QString::fromUtf8(srd_row_->desc);
75         return QString();
76 }
77
78 vector<AnnotationClass*> Row::ann_classes() const
79 {
80         assert(decoder_);
81
82         vector<AnnotationClass*> result;
83
84         if (!srd_row_) {
85                 if (index_ == 0) {
86                         // When operating as the fallback row, all annotation classes belong to it
87                         return decoder_->ann_classes();
88                 }
89                 return result;
90         }
91
92         for (GSList *l = srd_row_->ann_classes; l; l = l->next) {
93                 size_t class_id = (size_t)l->data;
94                 result.push_back(decoder_->get_ann_class_by_id(class_id));
95         }
96
97         return result;
98 }
99
100 uint32_t Row::index() const
101 {
102         return index_;
103 }
104
105 bool Row::visible() const
106 {
107         return visible_;
108 }
109
110 void Row::set_visible(bool visible)
111 {
112         visible_ = visible;
113 }
114
115 void Row::set_base_color(QColor base_color)
116 {
117         // For the row color, use the base color hue and add an offset that's
118         // not a dividend of 360
119
120         const int h = (base_color.toHsv().hue() + 20 * index_) % 360;
121         const int s = DECODE_COLOR_SATURATION;
122         const int v = DECODE_COLOR_VALUE;
123         color_.setHsl(h, s, v);
124
125         vector<AnnotationClass*> classes = ann_classes();
126         for (const AnnotationClass* ann_class : classes) {
127
128                 // For each class color, use the row color hue and add an offset that's
129                 // not a dividend of 360 and not a multiple of the row offset
130
131                 QColor ann_color(color_);
132                 const int h = (ann_color.toHsv().hue() + 55 * ann_class->id) % 360;
133                 const int s = DECODE_COLOR_SATURATION;
134                 const int v = DECODE_COLOR_VALUE;
135                 ann_color.setHsl(h, s, v);
136
137                 ann_class_color_[ann_class->id] = ann_color;
138                 ann_bright_class_color_[ann_class->id] = ann_color.lighter();
139                 ann_dark_class_color_[ann_class->id] = ann_color.darker();
140         }
141 }
142
143 const QColor Row::color() const
144 {
145         return color_;
146 }
147
148 const QColor Row::get_class_color(uint32_t ann_class_id) const
149 {
150         return ann_class_color_.at(ann_class_id);
151 }
152
153 const QColor Row::get_bright_class_color(uint32_t ann_class_id) const
154 {
155         return ann_bright_class_color_.at(ann_class_id);
156 }
157
158 const QColor Row::get_dark_class_color(uint32_t ann_class_id) const
159 {
160         return ann_dark_class_color_.at(ann_class_id);
161 }
162
163 bool Row::has_hidden_classes() const
164 {
165         for (const AnnotationClass* c : ann_classes())
166                 if (!c->visible)
167                         return true;
168
169         return false;
170 }
171
172 bool Row::operator<(const Row& other) const
173 {
174         return (decoder_ < other.decoder_) ||
175                 (decoder_ == other.decoder_ && srd_row_ < other.srd_row_);
176 }
177
178 bool Row::operator==(const Row& other) const
179 {
180         return ((decoder_ == other.decoder()) && (srd_row_ == other.srd_row_));
181 }
182
183 }  // namespace decode
184 }  // namespace data
185 }  // namespace pv