]> sigrok.org Git - pulseview.git/blame - pv/signal.cpp
Added a label colour chooser dialog
[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"
34c1ef02 24#include "view/view.h"
3e46726a 25
51e77110
JH
26namespace pv {
27
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
49f8ff3f
JH
40void Signal::set_name(QString name)
41{
42 _name = name;
43}
44
b3b57abc
JH
45QColor Signal::get_colour() const
46{
47 return _colour;
48}
49
50void Signal::set_colour(QColor colour)
51{
52 _colour = colour;
53}
54
a29bb7fb 55void Signal::paint_label(QPainter &p, const QRect &rect, bool hover)
3b258424 56{
b3b57abc 57 p.setBrush(_colour);
3b258424
JH
58
59 const QColor colour = get_colour();
60 const float nominal_offset = get_nominal_offset(rect);
ed6f0f4f
JH
61
62 compute_text_size(p);
63 const QRectF label_rect = get_label_rect(rect);
3e46726a
JH
64
65 // Paint the label
66 const QPointF points[] = {
67 label_rect.topLeft(),
68 label_rect.topRight(),
69 QPointF(rect.right(), nominal_offset),
70 label_rect.bottomRight(),
71 label_rect.bottomLeft()
72 };
73
c9b6acc1
JH
74 const QPointF highlight_points[] = {
75 QPointF(label_rect.left() + 1, label_rect.top() + 1),
76 QPointF(label_rect.right(), label_rect.top() + 1),
77 QPointF(rect.right() - 1, nominal_offset),
78 QPointF(label_rect.right(), label_rect.bottom() - 1),
79 QPointF(label_rect.left() + 1, label_rect.bottom() - 1)
80 };
81
82 p.setPen(Qt::transparent);
a29bb7fb 83 p.setBrush(hover ? colour.lighter() : colour);
3e46726a
JH
84 p.drawPolygon(points, countof(points));
85
c9b6acc1
JH
86 p.setPen(colour.lighter());
87 p.setBrush(Qt::transparent);
88 p.drawPolygon(highlight_points, countof(highlight_points));
89
90 p.setPen(colour.darker());
91 p.setBrush(Qt::transparent);
92 p.drawPolygon(points, countof(points));
93
3e46726a
JH
94 // Paint the text
95 p.setPen((colour.lightness() > 64) ? Qt::black : Qt::white);
3b258424 96 p.drawText(label_rect, Qt::AlignCenter | Qt::AlignVCenter, _name);
3e46726a 97}
51e77110 98
ed6f0f4f 99bool Signal::pt_in_label_rect(const QRect &rect, const QPoint &point)
a29bb7fb 100{
ed6f0f4f 101 const QRectF label = get_label_rect(rect);
a29bb7fb
JH
102 return QRectF(
103 QPointF(label.left() - LabelHitPadding,
104 label.top() - LabelHitPadding),
105 QPointF(rect.right(),
106 label.bottom() + LabelHitPadding)
107 ).contains(point);
108}
109
ed6f0f4f 110void Signal::compute_text_size(QPainter &p)
a29bb7fb 111{
ed6f0f4f
JH
112 _text_size = p.boundingRect(QRectF(), 0, _name).size();
113}
a29bb7fb 114
ed6f0f4f
JH
115QRectF Signal::get_label_rect(const QRect &rect)
116{
2e04f9bd
JH
117 using pv::view::View;
118
bb781376 119 const float nominal_offset = get_nominal_offset(rect) + 0.5;
a29bb7fb 120 const QSizeF label_size(
2e04f9bd
JH
121 _text_size.width() + View::LabelPadding.width() * 2,
122 _text_size.height() + View::LabelPadding.height() * 2);
a29bb7fb
JH
123 const float label_arrow_length = label_size.height() / 2;
124 return QRectF(
bb781376
JH
125 rect.right() - label_arrow_length -
126 label_size.width() - 0.5,
a29bb7fb
JH
127 nominal_offset - label_size.height() / 2,
128 label_size.width(), label_size.height());
129}
130
51e77110 131} // namespace pv