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