]> sigrok.org Git - pulseview.git/blob - pv/view/decode/annotation.cpp
f2b2f37f475621d8f1e8224b7553a04094c5ec31
[pulseview.git] / pv / view / decode / annotation.cpp
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
21 extern "C" {
22 #include <libsigrokdecode/libsigrokdecode.h>
23 }
24
25 #include <extdef.h>
26
27 #include <algorithm>
28
29 #include <QPainter>
30
31 #include "annotation.h"
32
33 using namespace boost;
34 using namespace std;
35
36 namespace pv {
37 namespace view {
38 namespace decode {
39
40 const double Annotation::EndCapWidth = 5;
41 const int Annotation::DrawPadding = 100;
42
43 Annotation::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
54 void Annotation::paint(QPainter &p, QColor fill, QColor outline,
55         QColor text_color, int text_height, int left, int right,
56         double samples_per_pixel, double pixels_offset, int y)
57 {
58         const int h = (text_height * 3) / 2;
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
64         if (start > right + DrawPadding || end < left - DrawPadding)
65                 return;
66
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
75 void 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
92 void Annotation::draw_range(QPainter &p, QColor fill, QColor outline,
93         QColor text_color, int h, double start, double end, int y)
94 {
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         };
105
106         p.setPen(outline);
107         p.setBrush(fill);
108         p.drawConvexPolygon(pts, countof(pts));
109
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;
125         }
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()));
133 }
134
135 } // namespace decode
136 } // namespace view
137 } // namespace pv