]> sigrok.org Git - pulseview.git/blame - pv/signal.cpp
Fixed timeline scale
[pulseview.git] / pv / signal.cpp
CommitLineData
28a4c9c5 1/*
b3f22de0 2 * This file is part of the PulseView project.
28a4c9c5
JH
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
cef18fc6 21#include <extdef.h>
28a4c9c5 22
cef18fc6 23#include "signal.h"
3e46726a 24
51e77110
JH
25namespace pv {
26
3e46726a 27const QSizeF Signal::LabelPadding(4, 0);
a29bb7fb 28const int Signal::LabelHitPadding = 2;
28a4c9c5 29
2858b391
JH
30Signal::Signal(QString name) :
31 _name(name)
28a4c9c5
JH
32{
33}
34
35QString Signal::get_name() const
36{
37 return _name;
38}
3e46726a 39
a29bb7fb 40void Signal::paint_label(QPainter &p, const QRect &rect, bool hover)
3b258424
JH
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);
3e46726a
JH
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
c9b6acc1
JH
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);
a29bb7fb 66 p.setBrush(hover ? colour.lighter() : colour);
3e46726a
JH
67 p.drawPolygon(points, countof(points));
68
c9b6acc1
JH
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
3e46726a
JH
77 // Paint the text
78 p.setPen((colour.lightness() > 64) ? Qt::black : Qt::white);
3b258424 79 p.drawText(label_rect, Qt::AlignCenter | Qt::AlignVCenter, _name);
3e46726a 80}
51e77110 81
a29bb7fb
JH
82bool 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
94QRectF 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
51e77110 110} // namespace pv