2 * This file is part of the PulseView project.
4 * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk>
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.
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.
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
24 #include "view/view.h"
28 const int Signal::LabelHitPadding = 2;
30 Signal::Signal(QString name) :
35 QString Signal::get_name() const
40 void Signal::set_name(QString name)
45 void Signal::paint_label(QPainter &p, const QRect &rect, bool hover)
47 p.setBrush(get_colour());
49 const QColor colour = get_colour();
50 const float nominal_offset = get_nominal_offset(rect);
53 const QRectF label_rect = get_label_rect(rect);
56 const QPointF points[] = {
58 label_rect.topRight(),
59 QPointF(rect.right(), nominal_offset),
60 label_rect.bottomRight(),
61 label_rect.bottomLeft()
64 const QPointF highlight_points[] = {
65 QPointF(label_rect.left() + 1, label_rect.top() + 1),
66 QPointF(label_rect.right(), label_rect.top() + 1),
67 QPointF(rect.right() - 1, nominal_offset),
68 QPointF(label_rect.right(), label_rect.bottom() - 1),
69 QPointF(label_rect.left() + 1, label_rect.bottom() - 1)
72 p.setPen(Qt::transparent);
73 p.setBrush(hover ? colour.lighter() : colour);
74 p.drawPolygon(points, countof(points));
76 p.setPen(colour.lighter());
77 p.setBrush(Qt::transparent);
78 p.drawPolygon(highlight_points, countof(highlight_points));
80 p.setPen(colour.darker());
81 p.setBrush(Qt::transparent);
82 p.drawPolygon(points, countof(points));
85 p.setPen((colour.lightness() > 64) ? Qt::black : Qt::white);
86 p.drawText(label_rect, Qt::AlignCenter | Qt::AlignVCenter, _name);
89 bool Signal::pt_in_label_rect(const QRect &rect, const QPoint &point)
91 const QRectF label = get_label_rect(rect);
93 QPointF(label.left() - LabelHitPadding,
94 label.top() - LabelHitPadding),
96 label.bottom() + LabelHitPadding)
100 void Signal::compute_text_size(QPainter &p)
102 _text_size = p.boundingRect(QRectF(), 0, _name).size();
105 QRectF Signal::get_label_rect(const QRect &rect)
107 using pv::view::View;
109 const float nominal_offset = get_nominal_offset(rect) + 0.5;
110 const QSizeF label_size(
111 _text_size.width() + View::LabelPadding.width() * 2,
112 _text_size.height() + View::LabelPadding.height() * 2);
113 const float label_arrow_length = label_size.height() / 2;
115 rect.right() - label_arrow_length -
116 label_size.width() - 0.5,
117 nominal_offset - label_size.height() / 2,
118 label_size.width(), label_size.height());