]> sigrok.org Git - pulseview.git/blame - signal.cpp
Initial implementation of labels
[pulseview.git] / signal.cpp
CommitLineData
28a4c9c5
JH
1/*
2 * This file is part of the sigrok 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
3e46726a
JH
23#include "extdef.h"
24
25const QSizeF Signal::LabelPadding(4, 0);
28a4c9c5 26
2858b391
JH
27Signal::Signal(QString name) :
28 _name(name)
28a4c9c5
JH
29{
30}
31
32QString Signal::get_name() const
33{
34 return _name;
35}
3e46726a
JH
36
37void Signal::paint_label(QPainter &p, const QRect &rect)
38{
39 p.setBrush(get_colour());
40
41 const QString text(_name);
42 const QColor colour = get_colour();
43
44 const QSizeF text_size = p.boundingRect(
45 QRectF(0, 0, rect.width(), 0), 0, text).size();
46
47 const float nominal_offset = get_nominal_offset(rect);
48 const QSizeF label_size(
49 text_size.width() + LabelPadding.width() * 2,
50 text_size.height() + LabelPadding.height() * 2);
51 const float label_arrow_length = label_size.height() / 2;
52 const QRectF label_rect(
53 rect.right() - label_arrow_length - label_size.width(),
54 nominal_offset - label_size.height() / 2,
55 label_size.width(), label_size.height());
56
57 // Paint the label
58 const QPointF points[] = {
59 label_rect.topLeft(),
60 label_rect.topRight(),
61 QPointF(rect.right(), nominal_offset),
62 label_rect.bottomRight(),
63 label_rect.bottomLeft()
64 };
65
66 p.setPen(Qt::black);
67 p.setBrush(colour);
68 p.drawPolygon(points, countof(points));
69
70 // Paint the text
71 p.setPen((colour.lightness() > 64) ? Qt::black : Qt::white);
72 p.drawText(label_rect, Qt::AlignCenter | Qt::AlignVCenter, text);
73}