]> sigrok.org Git - pulseview.git/blame - pv/view/decode/annotation.cpp
Draw insteantaneous annotations with a circle
[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
9472f447
JH
29#include <QPainter>
30
3195aeb6
JH
31#include "annotation.h"
32
33using namespace boost;
34using namespace std;
35
36namespace pv {
37namespace view {
38namespace decode {
39
1ae79b11 40const double Annotation::EndCapWidth = 5;
ea47a30c 41const int Annotation::DrawPadding = 100;
1ae79b11 42
3195aeb6
JH
43Annotation::Annotation(const srd_proto_data *const pdata) :
44 _start_sample(pdata->start_sample),
45 _end_sample(pdata->end_sample)
46{
47 const char *const *annotations = (char**)pdata->data;
48 while(*annotations) {
49 _annotations.push_back(QString(*annotations));
50 annotations++;
51 }
52}
53
1ae79b11
JH
54void Annotation::paint(QPainter &p, QColor fill, QColor outline,
55 QColor text_color, int text_height, int left, int right,
9472f447
JH
56 double samples_per_pixel, double pixels_offset, int y)
57{
1ae79b11 58 const int h = (text_height * 3) / 2;
9472f447
JH
59 const double start = _start_sample / samples_per_pixel -
60 pixels_offset;
61 const double end = _end_sample / samples_per_pixel -
62 pixels_offset;
63
ea47a30c 64 if (start > right + DrawPadding || end < left - DrawPadding)
9472f447
JH
65 return;
66
ea47a30c
JH
67 if (_start_sample == _end_sample)
68 draw_instant(p, fill, outline, text_color, h,
69 start, y);
70 else
71 draw_range(p, fill, outline, text_color, h,
72 start, end, y);
73}
74
75void Annotation::draw_instant(QPainter &p, QColor fill, QColor outline,
76 QColor text_color, int h, double x, int y)
77{
78 const QString text = _annotations.empty() ?
79 QString() : _annotations.back();
80 const double w = min(p.boundingRect(QRectF(), 0, text).width(),
81 0.0) + h;
82 const QRectF rect(x - w / 2, y - h / 2, w, h);
83
84 p.setPen(outline);
85 p.setBrush(fill);
86 p.drawRoundedRect(rect, h / 2, h / 2);
87
88 p.setPen(text_color);
89 p.drawText(rect, Qt::AlignCenter | Qt::AlignVCenter, text);
90}
91
92void Annotation::draw_range(QPainter &p, QColor fill, QColor outline,
93 QColor text_color, int h, double start, double end, int y)
94{
1ae79b11
JH
95 const double cap_width = min((end - start) / 2, EndCapWidth);
96
97 QPointF pts[] = {
98 QPointF(start, y + .5f),
99 QPointF(start + cap_width, y + .5f - h / 2),
100 QPointF(end - cap_width, y + .5f - h / 2),
101 QPointF(end, y + .5f),
102 QPointF(end - cap_width, y + .5f + h / 2),
103 QPointF(start + cap_width, y + .5f + h / 2)
104 };
9472f447 105
1ae79b11
JH
106 p.setPen(outline);
107 p.setBrush(fill);
108 p.drawConvexPolygon(pts, countof(pts));
9472f447 109
bb4c6769
JH
110 if (_annotations.empty())
111 return;
112
113 QRectF rect(start + cap_width, y - h / 2,
114 end - start - cap_width * 2, h);
115 p.setPen(text_color);
116
117 // Try to find an annotation that will fit
118 QString best_annotation;
119 int best_width = 0;
120
121 BOOST_FOREACH(QString &a, _annotations) {
122 const int w = p.boundingRect(QRectF(), 0, a).width();
123 if (w <= rect.width() && w > best_width)
124 best_annotation = a, best_width = w;
1ae79b11 125 }
bb4c6769
JH
126
127 if (best_annotation.isEmpty())
128 best_annotation = _annotations.back();
129
130 // If not ellide the last in the list
131 p.drawText(rect, Qt::AlignCenter, p.fontMetrics().elidedText(
132 best_annotation, Qt::ElideRight, rect.width()));
9472f447
JH
133}
134
3195aeb6
JH
135} // namespace decode
136} // namespace view
137} // namespace pv