]>
Commit | Line | Data |
---|---|---|
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 | const QColor Annotation::Colours[7] = { | |
44 | QColor(0xFC, 0xE9, 0x4F), // Light Butter | |
45 | QColor(0xFC, 0xAF, 0x3E), // Light Orange | |
46 | QColor(0xE9, 0xB9, 0x6E), // Light Chocolate | |
47 | QColor(0x8A, 0xE2, 0x34), // Light Green | |
48 | QColor(0x72, 0x9F, 0xCF), // Light Blue | |
49 | QColor(0xAD, 0x7F, 0xA8), // Light Plum | |
50 | QColor(0xEF, 0x29, 0x29) // Light Red | |
51 | }; | |
52 | ||
53 | Annotation::Annotation(const srd_proto_data *const pdata) : | |
54 | _start_sample(pdata->start_sample), | |
55 | _end_sample(pdata->end_sample), | |
56 | _format(pdata->ann_format) | |
57 | { | |
58 | const char *const *annotations = (char**)pdata->data; | |
59 | while(*annotations) { | |
60 | _annotations.push_back(QString(*annotations)); | |
61 | annotations++; | |
62 | } | |
63 | } | |
64 | ||
65 | void Annotation::paint(QPainter &p, QColor text_color, int text_height, | |
66 | int left, int right, double samples_per_pixel, double pixels_offset, | |
67 | int y) | |
68 | { | |
69 | const int h = (text_height * 3) / 2; | |
70 | const double start = _start_sample / samples_per_pixel - | |
71 | pixels_offset; | |
72 | const double end = _end_sample / samples_per_pixel - | |
73 | pixels_offset; | |
74 | const QColor fill = Colours[(_format * (countof(Colours) / 2 + 1)) % | |
75 | countof(Colours)]; | |
76 | const QColor outline(fill.darker()); | |
77 | ||
78 | if (start > right + DrawPadding || end < left - DrawPadding) | |
79 | return; | |
80 | ||
81 | if (_start_sample == _end_sample) | |
82 | draw_instant(p, fill, outline, text_color, h, | |
83 | start, y); | |
84 | else | |
85 | draw_range(p, fill, outline, text_color, h, | |
86 | start, end, y); | |
87 | } | |
88 | ||
89 | void Annotation::draw_instant(QPainter &p, QColor fill, QColor outline, | |
90 | QColor text_color, int h, double x, int y) | |
91 | { | |
92 | const QString text = _annotations.empty() ? | |
93 | QString() : _annotations.back(); | |
94 | const double w = min(p.boundingRect(QRectF(), 0, text).width(), | |
95 | 0.0) + h; | |
96 | const QRectF rect(x - w / 2, y - h / 2, w, h); | |
97 | ||
98 | p.setPen(outline); | |
99 | p.setBrush(fill); | |
100 | p.drawRoundedRect(rect, h / 2, h / 2); | |
101 | ||
102 | p.setPen(text_color); | |
103 | p.drawText(rect, Qt::AlignCenter | Qt::AlignVCenter, text); | |
104 | } | |
105 | ||
106 | void Annotation::draw_range(QPainter &p, QColor fill, QColor outline, | |
107 | QColor text_color, int h, double start, double end, int y) | |
108 | { | |
109 | const double cap_width = min((end - start) / 2, EndCapWidth); | |
110 | ||
111 | QPointF pts[] = { | |
112 | QPointF(start, y + .5f), | |
113 | QPointF(start + cap_width, y + .5f - h / 2), | |
114 | QPointF(end - cap_width, y + .5f - h / 2), | |
115 | QPointF(end, y + .5f), | |
116 | QPointF(end - cap_width, y + .5f + h / 2), | |
117 | QPointF(start + cap_width, y + .5f + h / 2) | |
118 | }; | |
119 | ||
120 | p.setPen(outline); | |
121 | p.setBrush(fill); | |
122 | p.drawConvexPolygon(pts, countof(pts)); | |
123 | ||
124 | if (_annotations.empty()) | |
125 | return; | |
126 | ||
127 | QRectF rect(start + cap_width, y - h / 2, | |
128 | end - start - cap_width * 2, h); | |
129 | p.setPen(text_color); | |
130 | ||
131 | // Try to find an annotation that will fit | |
132 | QString best_annotation; | |
133 | int best_width = 0; | |
134 | ||
135 | BOOST_FOREACH(QString &a, _annotations) { | |
136 | const int w = p.boundingRect(QRectF(), 0, a).width(); | |
137 | if (w <= rect.width() && w > best_width) | |
138 | best_annotation = a, best_width = w; | |
139 | } | |
140 | ||
141 | if (best_annotation.isEmpty()) | |
142 | best_annotation = _annotations.back(); | |
143 | ||
144 | // If not ellide the last in the list | |
145 | p.drawText(rect, Qt::AlignCenter, p.fontMetrics().elidedText( | |
146 | best_annotation, Qt::ElideRight, rect.width())); | |
147 | } | |
148 | ||
149 | } // namespace decode | |
150 | } // namespace view | |
151 | } // namespace pv |