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