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
23 #include <QApplication>
31 const int Signal::LabelHitPadding = 2;
32 const int Signal::LabelHighlightRadius = 6;
34 Signal::Signal(QString name) :
41 QString Signal::get_name() const
46 void Signal::set_name(QString name)
51 QColor Signal::get_colour() const
56 void Signal::set_colour(QColor colour)
61 int Signal::get_v_offset() const
66 void Signal::set_v_offset(int v_offset)
71 bool Signal::selected() const
76 void Signal::select(bool select)
81 void Signal::paint_label(QPainter &p, const QRect &rect, bool hover)
85 const QColor colour = get_colour();
86 const float nominal_offset = get_nominal_offset(rect);
89 const QRectF label_rect = get_label_rect(rect);
92 const QPointF points[] = {
94 label_rect.topRight(),
95 QPointF(rect.right(), nominal_offset),
96 label_rect.bottomRight(),
97 label_rect.bottomLeft()
100 const QPointF highlight_points[] = {
101 QPointF(label_rect.left() + 1, label_rect.top() + 1),
102 QPointF(label_rect.right(), label_rect.top() + 1),
103 QPointF(rect.right() - 1, nominal_offset),
104 QPointF(label_rect.right(), label_rect.bottom() - 1),
105 QPointF(label_rect.left() + 1, label_rect.bottom() - 1)
109 p.setPen(QPen(QApplication::palette().brush(
110 QPalette::Highlight), LabelHighlightRadius,
111 Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
112 p.setBrush(Qt::transparent);
113 p.drawPolygon(points, countof(points));
116 p.setPen(Qt::transparent);
117 p.setBrush(hover ? colour.lighter() : colour);
118 p.drawPolygon(points, countof(points));
120 p.setPen(colour.lighter());
121 p.setBrush(Qt::transparent);
122 p.drawPolygon(highlight_points, countof(highlight_points));
124 p.setPen(colour.darker());
125 p.setBrush(Qt::transparent);
126 p.drawPolygon(points, countof(points));
129 p.setPen((colour.lightness() > 64) ? Qt::black : Qt::white);
130 p.drawText(label_rect, Qt::AlignCenter | Qt::AlignVCenter, _name);
133 bool Signal::pt_in_label_rect(const QRect &rect, const QPoint &point)
135 const QRectF label = get_label_rect(rect);
137 QPointF(label.left() - LabelHitPadding,
138 label.top() - LabelHitPadding),
139 QPointF(rect.right(),
140 label.bottom() + LabelHitPadding)
144 void Signal::compute_text_size(QPainter &p)
146 _text_size = p.boundingRect(QRectF(), 0, _name).size();
149 QRectF Signal::get_label_rect(const QRect &rect)
151 using pv::view::View;
153 const float nominal_offset = get_nominal_offset(rect) + 0.5;
154 const QSizeF label_size(
155 _text_size.width() + View::LabelPadding.width() * 2,
156 _text_size.height() + View::LabelPadding.height() * 2);
157 const float label_arrow_length = label_size.height() / 2;
159 rect.right() - label_arrow_length -
160 label_size.width() - 0.5,
161 nominal_offset - label_size.height() / 2,
162 label_size.width(), label_size.height());