]> sigrok.org Git - pulseview.git/blob - pv/view/trace.cpp
4de3f141cc87438bcf6f3d9b2ea1ef1b43822888
[pulseview.git] / pv / view / trace.cpp
1 /*
2  * This file is part of the PulseView project.
3  *
4  * Copyright (C) 2013 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 #include <extdef.h>
22
23 #include <assert.h>
24 #include <math.h>
25
26 #include "trace.h"
27 #include "view.h"
28
29 namespace pv {
30 namespace view {
31
32 const int Trace::LabelHitPadding = 2;
33
34 Trace::Trace(pv::SigSession &session, QString name) :
35         _session(session),
36         _name(name),
37         _v_offset(0)
38 {
39 }
40
41 QString Trace::get_name() const
42 {
43         return _name;
44 }
45
46 void Trace::set_name(QString name)
47 {
48         _name = name;
49 }
50
51 QColor Trace::get_colour() const
52 {
53         return _colour;
54 }
55
56 void Trace::set_colour(QColor colour)
57 {
58         _colour = colour;
59 }
60
61 int Trace::get_v_offset() const
62 {
63         return _v_offset;
64 }
65
66 void Trace::set_v_offset(int v_offset)
67 {
68         _v_offset = v_offset;
69 }
70
71 void Trace::paint_label(QPainter &p, int y, int right, bool hover)
72 {
73         p.setBrush(_colour);
74
75         if (!enabled())
76                 return;
77
78         const QColor colour = get_colour();
79
80         compute_text_size(p);
81         const QRectF label_rect = get_label_rect(y, right);
82
83         // Paint the label
84         const QPointF points[] = {
85                 label_rect.topLeft(),
86                 label_rect.topRight(),
87                 QPointF(right, y),
88                 label_rect.bottomRight(),
89                 label_rect.bottomLeft()
90         };
91
92         const QPointF highlight_points[] = {
93                 QPointF(label_rect.left() + 1, label_rect.top() + 1),
94                 QPointF(label_rect.right(), label_rect.top() + 1),
95                 QPointF(right - 1, y),
96                 QPointF(label_rect.right(), label_rect.bottom() - 1),
97                 QPointF(label_rect.left() + 1, label_rect.bottom() - 1)
98         };
99
100         if (selected()) {
101                 p.setPen(highlight_pen());
102                 p.setBrush(Qt::transparent);
103                 p.drawPolygon(points, countof(points));
104         }
105
106         p.setPen(Qt::transparent);
107         p.setBrush(hover ? colour.lighter() : colour);
108         p.drawPolygon(points, countof(points));
109
110         p.setPen(colour.lighter());
111         p.setBrush(Qt::transparent);
112         p.drawPolygon(highlight_points, countof(highlight_points));
113
114         p.setPen(colour.darker());
115         p.setBrush(Qt::transparent);
116         p.drawPolygon(points, countof(points));
117
118         // Paint the text
119         p.setPen((colour.lightness() > 64) ? Qt::black : Qt::white);
120         p.drawText(label_rect, Qt::AlignCenter | Qt::AlignVCenter, _name);
121 }
122
123 bool Trace::pt_in_label_rect(int y, int left, int right,
124         const QPoint &point)
125 {
126         (void)left;
127
128         const QRectF label = get_label_rect(y, right);
129         return QRectF(
130                 QPointF(label.left() - LabelHitPadding,
131                         label.top() - LabelHitPadding),
132                 QPointF(right, label.bottom() + LabelHitPadding)
133                         ).contains(point);
134 }
135
136 void Trace::compute_text_size(QPainter &p)
137 {
138         _text_size = QSize(
139                 p.boundingRect(QRectF(), 0, _name).width(),
140                 p.boundingRect(QRectF(), 0, "Tg").height());
141 }
142
143 QRectF Trace::get_label_rect(int y, int right)
144 {
145         using pv::view::View;
146
147         const QSizeF label_size(
148                 _text_size.width() + View::LabelPadding.width() * 2,
149                 ceilf((_text_size.height() + View::LabelPadding.height() * 2) / 2) * 2);
150         const float label_arrow_length = label_size.height() / 2;
151         return QRectF(
152                 right - label_arrow_length - label_size.width() - 0.5,
153                 y + 0.5f - label_size.height() / 2,
154                 label_size.width(), label_size.height());
155 }
156
157 } // namespace view
158 } // namespace pv