]> sigrok.org Git - pulseview.git/blame - pv/view/decode/annotation.cpp
Store annotations as objects emplaced in the vector
[pulseview.git] / pv / view / decode / annotation.cpp
CommitLineData
3195aeb6
JH
1/*
2 * This file is part of the PulseView project.
3 *
4 * Copyright (C) 2012 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, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21extern "C" {
22#include <libsigrokdecode/libsigrokdecode.h>
23}
24
1ae79b11
JH
25#include <extdef.h>
26
27#include <algorithm>
28
b213ef09
JH
29#include <boost/foreach.hpp>
30
9472f447
JH
31#include <QPainter>
32
3195aeb6
JH
33#include "annotation.h"
34
3195aeb6
JH
35using namespace std;
36
37namespace pv {
38namespace view {
39namespace decode {
40
1ae79b11 41const double Annotation::EndCapWidth = 5;
ea47a30c 42const int Annotation::DrawPadding = 100;
1ae79b11 43
f9abdc01
JH
44const QColor Annotation::Colours[7] = {
45 QColor(0xFC, 0xE9, 0x4F), // Light Butter
46 QColor(0xFC, 0xAF, 0x3E), // Light Orange
47 QColor(0xE9, 0xB9, 0x6E), // Light Chocolate
48 QColor(0x8A, 0xE2, 0x34), // Light Green
49 QColor(0x72, 0x9F, 0xCF), // Light Blue
50 QColor(0xAD, 0x7F, 0xA8), // Light Plum
51 QColor(0xEF, 0x29, 0x29) // Light Red
52};
53
3195aeb6
JH
54Annotation::Annotation(const srd_proto_data *const pdata) :
55 _start_sample(pdata->start_sample),
88cd7da0 56 _end_sample(pdata->end_sample)
3195aeb6 57{
88cd7da0
JH
58 assert(pdata);
59 const srd_proto_data_annotation *const pda =
60 (const srd_proto_data_annotation*)pdata->data;
61 assert(pda);
62
3248fcde
JH
63 _format = pda->ann_format;
64
88cd7da0 65 const char *const *annotations = (char**)pda->ann_text;
3195aeb6
JH
66 while(*annotations) {
67 _annotations.push_back(QString(*annotations));
68 annotations++;
69 }
70}
71
a007f5ad
JH
72uint64_t Annotation::start_sample() const
73{
74 return _start_sample;
75}
76
77uint64_t Annotation::end_sample() const
78{
79 return _end_sample;
80}
81
5dfeb70f 82void Annotation::paint(QPainter &p, QColor text_color, int h,
f9abdc01 83 int left, int right, double samples_per_pixel, double pixels_offset,
db62bbfd 84 int y) const
9472f447 85{
9472f447
JH
86 const double start = _start_sample / samples_per_pixel -
87 pixels_offset;
88 const double end = _end_sample / samples_per_pixel -
89 pixels_offset;
f9abdc01
JH
90 const QColor fill = Colours[(_format * (countof(Colours) / 2 + 1)) %
91 countof(Colours)];
92 const QColor outline(fill.darker());
9472f447 93
ea47a30c 94 if (start > right + DrawPadding || end < left - DrawPadding)
9472f447
JH
95 return;
96
ea47a30c
JH
97 if (_start_sample == _end_sample)
98 draw_instant(p, fill, outline, text_color, h,
99 start, y);
100 else
101 draw_range(p, fill, outline, text_color, h,
102 start, end, y);
103}
104
105void Annotation::draw_instant(QPainter &p, QColor fill, QColor outline,
db62bbfd 106 QColor text_color, int h, double x, int y) const
ea47a30c
JH
107{
108 const QString text = _annotations.empty() ?
109 QString() : _annotations.back();
110 const double w = min(p.boundingRect(QRectF(), 0, text).width(),
111 0.0) + h;
112 const QRectF rect(x - w / 2, y - h / 2, w, h);
113
114 p.setPen(outline);
115 p.setBrush(fill);
116 p.drawRoundedRect(rect, h / 2, h / 2);
117
118 p.setPen(text_color);
119 p.drawText(rect, Qt::AlignCenter | Qt::AlignVCenter, text);
120}
121
122void Annotation::draw_range(QPainter &p, QColor fill, QColor outline,
db62bbfd 123 QColor text_color, int h, double start, double end, int y) const
ea47a30c 124{
42410f2c
JH
125 const double top = y + .5 - h / 2;
126 const double bottom = y + .5 + h / 2;
127
128 p.setPen(outline);
129 p.setBrush(fill);
130
131 // If the two ends are within 1 pixel, draw a vertical line
132 if (start + 1.0 > end)
133 {
134 p.drawLine(QPointF(start, top), QPointF(start, bottom));
135 return;
136 }
137
6ad174e0 138 const double cap_width = min((end - start) / 4, EndCapWidth);
1ae79b11
JH
139
140 QPointF pts[] = {
141 QPointF(start, y + .5f),
42410f2c
JH
142 QPointF(start + cap_width, top),
143 QPointF(end - cap_width, top),
1ae79b11 144 QPointF(end, y + .5f),
42410f2c
JH
145 QPointF(end - cap_width, bottom),
146 QPointF(start + cap_width, bottom)
1ae79b11 147 };
9472f447 148
1ae79b11 149 p.drawConvexPolygon(pts, countof(pts));
9472f447 150
bb4c6769
JH
151 if (_annotations.empty())
152 return;
153
154 QRectF rect(start + cap_width, y - h / 2,
155 end - start - cap_width * 2, h);
156 p.setPen(text_color);
157
158 // Try to find an annotation that will fit
159 QString best_annotation;
160 int best_width = 0;
161
db62bbfd 162 BOOST_FOREACH(const QString &a, _annotations) {
bb4c6769
JH
163 const int w = p.boundingRect(QRectF(), 0, a).width();
164 if (w <= rect.width() && w > best_width)
165 best_annotation = a, best_width = w;
1ae79b11 166 }
bb4c6769
JH
167
168 if (best_annotation.isEmpty())
169 best_annotation = _annotations.back();
170
171 // If not ellide the last in the list
172 p.drawText(rect, Qt::AlignCenter, p.fontMetrics().elidedText(
173 best_annotation, Qt::ElideRight, rect.width()));
9472f447
JH
174}
175
3195aeb6
JH
176} // namespace decode
177} // namespace view
178} // namespace pv