]> sigrok.org Git - pulseview.git/blame - pv/view/trace.cpp
Split signal painting into 3 layers
[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
fe08b6e8 32const QPen Trace::AxisPen(QColor(128, 128, 128, 64));
931f20b0
JH
33const int Trace::LabelHitPadding = 2;
34
35Trace::Trace(pv::SigSession &session, QString name) :
36 _session(session),
37 _name(name),
38 _v_offset(0)
39{
40}
41
42QString Trace::get_name() const
43{
44 return _name;
45}
46
47void Trace::set_name(QString name)
48{
49 _name = name;
50}
51
52QColor Trace::get_colour() const
53{
54 return _colour;
55}
56
57void Trace::set_colour(QColor colour)
58{
59 _colour = colour;
60}
61
62int Trace::get_v_offset() const
63{
64 return _v_offset;
65}
66
67void Trace::set_v_offset(int v_offset)
68{
69 _v_offset = v_offset;
70}
71
01fd3263 72void Trace::set_view(pv::view::View *view)
931f20b0 73{
01fd3263
JH
74 assert(view);
75 _view = view;
76}
77
fe08b6e8
JH
78void Trace::paint_back(QPainter &p, int left, int right)
79{
80 (void)p;
81 (void)left;
82 (void)right;
83}
84
85void Trace::paint_mid(QPainter &p, int left, int right)
86{
87 (void)p;
88 (void)left;
89 (void)right;
90}
91
92void Trace::paint_fore(QPainter &p, int left, int right)
93{
94 (void)p;
95 (void)left;
96 (void)right;
97}
98
01fd3263
JH
99void Trace::paint_label(QPainter &p, int right, bool hover)
100{
101 assert(_view);
102 const int y = _v_offset - _view->v_offset();
103
931f20b0
JH
104 p.setBrush(_colour);
105
106 if (!enabled())
107 return;
108
109 const QColor colour = get_colour();
110
111 compute_text_size(p);
01fd3263 112 const QRectF label_rect = get_label_rect(right);
931f20b0
JH
113
114 // Paint the label
115 const QPointF points[] = {
116 label_rect.topLeft(),
117 label_rect.topRight(),
118 QPointF(right, y),
119 label_rect.bottomRight(),
120 label_rect.bottomLeft()
121 };
122
123 const QPointF highlight_points[] = {
124 QPointF(label_rect.left() + 1, label_rect.top() + 1),
125 QPointF(label_rect.right(), label_rect.top() + 1),
126 QPointF(right - 1, y),
127 QPointF(label_rect.right(), label_rect.bottom() - 1),
128 QPointF(label_rect.left() + 1, label_rect.bottom() - 1)
129 };
130
131 if (selected()) {
132 p.setPen(highlight_pen());
133 p.setBrush(Qt::transparent);
134 p.drawPolygon(points, countof(points));
135 }
136
137 p.setPen(Qt::transparent);
138 p.setBrush(hover ? colour.lighter() : colour);
139 p.drawPolygon(points, countof(points));
140
141 p.setPen(colour.lighter());
142 p.setBrush(Qt::transparent);
143 p.drawPolygon(highlight_points, countof(highlight_points));
144
145 p.setPen(colour.darker());
146 p.setBrush(Qt::transparent);
147 p.drawPolygon(points, countof(points));
148
149 // Paint the text
150 p.setPen((colour.lightness() > 64) ? Qt::black : Qt::white);
151 p.drawText(label_rect, Qt::AlignCenter | Qt::AlignVCenter, _name);
152}
153
01fd3263 154bool Trace::pt_in_label_rect(int left, int right, const QPoint &point)
931f20b0
JH
155{
156 (void)left;
157
01fd3263 158 const QRectF label = get_label_rect(right);
931f20b0
JH
159 return QRectF(
160 QPointF(label.left() - LabelHitPadding,
161 label.top() - LabelHitPadding),
162 QPointF(right, label.bottom() + LabelHitPadding)
163 ).contains(point);
164}
165
fe08b6e8
JH
166int Trace::get_y() const
167{
168 return _v_offset - _view->v_offset();
169}
170
171void Trace::paint_axis(QPainter &p, int y, int left, int right)
172{
173 p.setPen(AxisPen);
174 p.drawLine(QPointF(left, y + 0.5f), QPointF(right, y + 0.5f));
175}
176
931f20b0
JH
177void Trace::compute_text_size(QPainter &p)
178{
179 _text_size = QSize(
180 p.boundingRect(QRectF(), 0, _name).width(),
181 p.boundingRect(QRectF(), 0, "Tg").height());
182}
183
01fd3263 184QRectF Trace::get_label_rect(int right)
931f20b0
JH
185{
186 using pv::view::View;
187
01fd3263
JH
188 assert(_view);
189 const int y = _v_offset - _view->v_offset();
190
931f20b0
JH
191 const QSizeF label_size(
192 _text_size.width() + View::LabelPadding.width() * 2,
193 ceilf((_text_size.height() + View::LabelPadding.height() * 2) / 2) * 2);
194 const float label_arrow_length = label_size.height() / 2;
195 return QRectF(
196 right - label_arrow_length - label_size.width() - 0.5,
197 y + 0.5f - label_size.height() / 2,
198 label_size.width(), label_size.height());
199}
200
201} // namespace view
202} // namespace pv