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