]> sigrok.org Git - pulseview.git/blob - pv/signal.cpp
Moved all classes into the pv namespace
[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 "signal.h"
22
23 #include "extdef.h"
24
25 namespace pv {
26
27 const QSizeF Signal::LabelPadding(4, 0);
28
29 Signal::Signal(QString name) :
30         _name(name)
31 {
32 }
33
34 QString Signal::get_name() const
35 {
36         return _name;
37 }
38
39 void Signal::paint_label(QPainter &p, const QRect &rect)
40 {
41         p.setBrush(get_colour());
42
43         const QString text(_name);
44         const QColor colour = get_colour();
45
46         const QSizeF text_size = p.boundingRect(
47                 QRectF(0, 0, rect.width(), 0), 0, text).size();
48
49         const float nominal_offset = get_nominal_offset(rect);
50         const QSizeF label_size(
51                 text_size.width() + LabelPadding.width() * 2,
52                 text_size.height() + LabelPadding.height() * 2);
53         const float label_arrow_length = label_size.height() / 2;
54         const QRectF label_rect(
55                 rect.right() - label_arrow_length - label_size.width(),
56                 nominal_offset - label_size.height() / 2,
57                 label_size.width(), label_size.height());
58
59         // Paint the label
60         const QPointF points[] = {
61                 label_rect.topLeft(),
62                 label_rect.topRight(),
63                 QPointF(rect.right(), nominal_offset),
64                 label_rect.bottomRight(),
65                 label_rect.bottomLeft()
66         };
67
68         const QPointF highlight_points[] = {
69                 QPointF(label_rect.left() + 1, label_rect.top() + 1),
70                 QPointF(label_rect.right(), label_rect.top() + 1),
71                 QPointF(rect.right() - 1, nominal_offset),
72                 QPointF(label_rect.right(), label_rect.bottom() - 1),
73                 QPointF(label_rect.left() + 1, label_rect.bottom() - 1)
74         };
75
76         p.setPen(Qt::transparent);
77         p.setBrush(colour);
78         p.drawPolygon(points, countof(points));
79
80         p.setPen(colour.lighter());
81         p.setBrush(Qt::transparent);
82         p.drawPolygon(highlight_points, countof(highlight_points));
83
84         p.setPen(colour.darker());
85         p.setBrush(Qt::transparent);
86         p.drawPolygon(points, countof(points));
87
88         // Paint the text
89         p.setPen((colour.lightness() > 64) ? Qt::black : Qt::white);
90         p.drawText(label_rect, Qt::AlignCenter | Qt::AlignVCenter, text);
91 }
92
93 } // namespace pv