]> sigrok.org Git - pulseview.git/blame - pv/view/trace.cpp
Initial decode painting
[pulseview.git] / pv / view / trace.cpp
CommitLineData
931f20b0
JH
1/*
2 * This file is part of the PulseView project.
3 *
4 * Copyright (C) 2013 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 <extdef.h>
22
23#include <assert.h>
24#include <math.h>
25
26#include "trace.h"
27#include "view.h"
28
29namespace pv {
30namespace view {
31
32const int Trace::LabelHitPadding = 2;
33
34Trace::Trace(pv::SigSession &session, QString name) :
35 _session(session),
36 _name(name),
37 _v_offset(0)
38{
39}
40
41QString Trace::get_name() const
42{
43 return _name;
44}
45
46void Trace::set_name(QString name)
47{
48 _name = name;
49}
50
51QColor Trace::get_colour() const
52{
53 return _colour;
54}
55
56void Trace::set_colour(QColor colour)
57{
58 _colour = colour;
59}
60
61int Trace::get_v_offset() const
62{
63 return _v_offset;
64}
65
66void Trace::set_v_offset(int v_offset)
67{
68 _v_offset = v_offset;
69}
70
01fd3263 71void Trace::set_view(pv::view::View *view)
931f20b0 72{
01fd3263
JH
73 assert(view);
74 _view = view;
75}
76
77void Trace::paint_label(QPainter &p, int right, bool hover)
78{
79 assert(_view);
80 const int y = _v_offset - _view->v_offset();
81
931f20b0
JH
82 p.setBrush(_colour);
83
84 if (!enabled())
85 return;
86
87 const QColor colour = get_colour();
88
89 compute_text_size(p);
01fd3263 90 const QRectF label_rect = get_label_rect(right);
931f20b0
JH
91
92 // Paint the label
93 const QPointF points[] = {
94 label_rect.topLeft(),
95 label_rect.topRight(),
96 QPointF(right, y),
97 label_rect.bottomRight(),
98 label_rect.bottomLeft()
99 };
100
101 const QPointF highlight_points[] = {
102 QPointF(label_rect.left() + 1, label_rect.top() + 1),
103 QPointF(label_rect.right(), label_rect.top() + 1),
104 QPointF(right - 1, y),
105 QPointF(label_rect.right(), label_rect.bottom() - 1),
106 QPointF(label_rect.left() + 1, label_rect.bottom() - 1)
107 };
108
109 if (selected()) {
110 p.setPen(highlight_pen());
111 p.setBrush(Qt::transparent);
112 p.drawPolygon(points, countof(points));
113 }
114
115 p.setPen(Qt::transparent);
116 p.setBrush(hover ? colour.lighter() : colour);
117 p.drawPolygon(points, countof(points));
118
119 p.setPen(colour.lighter());
120 p.setBrush(Qt::transparent);
121 p.drawPolygon(highlight_points, countof(highlight_points));
122
123 p.setPen(colour.darker());
124 p.setBrush(Qt::transparent);
125 p.drawPolygon(points, countof(points));
126
127 // Paint the text
128 p.setPen((colour.lightness() > 64) ? Qt::black : Qt::white);
129 p.drawText(label_rect, Qt::AlignCenter | Qt::AlignVCenter, _name);
130}
131
01fd3263 132bool Trace::pt_in_label_rect(int left, int right, const QPoint &point)
931f20b0
JH
133{
134 (void)left;
135
01fd3263 136 const QRectF label = get_label_rect(right);
931f20b0
JH
137 return QRectF(
138 QPointF(label.left() - LabelHitPadding,
139 label.top() - LabelHitPadding),
140 QPointF(right, label.bottom() + LabelHitPadding)
141 ).contains(point);
142}
143
144void Trace::compute_text_size(QPainter &p)
145{
146 _text_size = QSize(
147 p.boundingRect(QRectF(), 0, _name).width(),
148 p.boundingRect(QRectF(), 0, "Tg").height());
149}
150
01fd3263 151QRectF Trace::get_label_rect(int right)
931f20b0
JH
152{
153 using pv::view::View;
154
01fd3263
JH
155 assert(_view);
156 const int y = _v_offset - _view->v_offset();
157
931f20b0
JH
158 const QSizeF label_size(
159 _text_size.width() + View::LabelPadding.width() * 2,
160 ceilf((_text_size.height() + View::LabelPadding.height() * 2) / 2) * 2);
161 const float label_arrow_length = label_size.height() / 2;
162 return QRectF(
163 right - label_arrow_length - label_size.width() - 0.5,
164 y + 0.5f - label_size.height() / 2,
165 label_size.width(), label_size.height());
166}
167
168} // namespace view
169} // namespace pv