]> sigrok.org Git - pulseview.git/blob - pv/signal.cpp
Added mouse-over behaviour to signal labels
[pulseview.git] / pv / 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
25 namespace pv {
26
27 const QSizeF Signal::LabelPadding(4, 0);
28 const int Signal::LabelHitPadding = 2;
29
30 Signal::Signal(QString name) :
31         _name(name)
32 {
33 }
34
35 QString Signal::get_name() const
36 {
37         return _name;
38 }
39
40 void Signal::paint_label(QPainter &p, const QRect &rect, bool hover)
41 {
42         p.setBrush(get_colour());
43
44         const QColor colour = get_colour();
45         const float nominal_offset = get_nominal_offset(rect);
46         const QRectF label_rect = get_label_rect(p, rect);
47
48         // Paint the label
49         const QPointF points[] = {
50                 label_rect.topLeft(),
51                 label_rect.topRight(),
52                 QPointF(rect.right(), nominal_offset),
53                 label_rect.bottomRight(),
54                 label_rect.bottomLeft()
55         };
56
57         const QPointF highlight_points[] = {
58                 QPointF(label_rect.left() + 1, label_rect.top() + 1),
59                 QPointF(label_rect.right(), label_rect.top() + 1),
60                 QPointF(rect.right() - 1, nominal_offset),
61                 QPointF(label_rect.right(), label_rect.bottom() - 1),
62                 QPointF(label_rect.left() + 1, label_rect.bottom() - 1)
63         };
64
65         p.setPen(Qt::transparent);
66         p.setBrush(hover ? colour.lighter() : colour);
67         p.drawPolygon(points, countof(points));
68
69         p.setPen(colour.lighter());
70         p.setBrush(Qt::transparent);
71         p.drawPolygon(highlight_points, countof(highlight_points));
72
73         p.setPen(colour.darker());
74         p.setBrush(Qt::transparent);
75         p.drawPolygon(points, countof(points));
76
77         // Paint the text
78         p.setPen((colour.lightness() > 64) ? Qt::black : Qt::white);
79         p.drawText(label_rect, Qt::AlignCenter | Qt::AlignVCenter, _name);
80 }
81
82 bool Signal::pt_in_label_rect(QPainter &p,
83         const QRect &rect, const QPoint &point)
84 {
85         const QRectF label = get_label_rect(p, rect);
86         return QRectF(
87                 QPointF(label.left() - LabelHitPadding,
88                         label.top() - LabelHitPadding),
89                 QPointF(rect.right(),
90                         label.bottom() + LabelHitPadding)
91                 ).contains(point);
92 }
93
94 QRectF Signal::get_label_rect(QPainter &p, const QRect &rect)
95 {
96         const QSizeF text_size = p.boundingRect(
97                 QRectF(0, 0, rect.width(), 0), 0, _name).size();
98
99         const float nominal_offset = get_nominal_offset(rect);
100         const QSizeF label_size(
101                 text_size.width() + LabelPadding.width() * 2,
102                 text_size.height() + LabelPadding.height() * 2);
103         const float label_arrow_length = label_size.height() / 2;
104         return QRectF(
105                 rect.right() - label_arrow_length - label_size.width(),
106                 nominal_offset - label_size.height() / 2,
107                 label_size.width(), label_size.height());
108 }
109
110 } // namespace pv