]> sigrok.org Git - pulseview.git/blame - pv/view/trace.cpp
RowItem: Renamed get_v_offset to v_offset
[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
d7c0ca4a 26#include <QApplication>
b213ef09 27#include <QFormLayout>
0891e69b 28#include <QKeyEvent>
b213ef09
JH
29#include <QLineEdit>
30
931f20b0 31#include "trace.h"
91e8bf08 32#include "tracepalette.h"
931f20b0
JH
33#include "view.h"
34
91e8bf08 35#include <pv/widgets/colourbutton.h>
1f81a394 36#include <pv/widgets/popup.h>
569d1e41 37
931f20b0
JH
38namespace pv {
39namespace view {
40
fe08b6e8 41const QPen Trace::AxisPen(QColor(128, 128, 128, 64));
931f20b0
JH
42const int Trace::LabelHitPadding = 2;
43
83c23cc9 44Trace::Trace(QString name) :
931f20b0 45 _name(name),
20eaae39
JH
46 _popup(NULL),
47 _popup_form(NULL)
931f20b0
JH
48{
49}
50
51QString Trace::get_name() const
52{
53 return _name;
54}
55
56void Trace::set_name(QString name)
57{
58 _name = name;
59}
60
61QColor Trace::get_colour() const
62{
63 return _colour;
64}
65
66void Trace::set_colour(QColor colour)
67{
68 _colour = colour;
69}
70
01fd3263
JH
71void Trace::paint_label(QPainter &p, int right, bool hover)
72{
d5d1f925 73 const int y = get_y();
01fd3263 74
931f20b0
JH
75 p.setBrush(_colour);
76
77 if (!enabled())
78 return;
79
80 const QColor colour = get_colour();
81
1053d05c 82 const QRectF r = label_rect(right);
931f20b0
JH
83
84 // Paint the label
b5cb6c35 85 const float label_arrow_length = r.height() / 2;
931f20b0 86 const QPointF points[] = {
1053d05c 87 r.topLeft(),
b5cb6c35
JH
88 QPointF(r.right() - label_arrow_length, r.top()),
89 QPointF(r.right(), y),
90 QPointF(r.right() - label_arrow_length, r.bottom()),
1053d05c 91 r.bottomLeft()
931f20b0 92 };
931f20b0 93 const QPointF highlight_points[] = {
1053d05c 94 QPointF(r.left() + 1, r.top() + 1),
b5cb6c35
JH
95 QPointF(r.right() - label_arrow_length, r.top() + 1),
96 QPointF(r.right() - 1, y),
97 QPointF(r.right() - label_arrow_length, r.bottom() - 1),
1053d05c 98 QPointF(r.left() + 1, r.bottom() - 1)
931f20b0
JH
99 };
100
101 if (selected()) {
102 p.setPen(highlight_pen());
103 p.setBrush(Qt::transparent);
104 p.drawPolygon(points, countof(points));
105 }
106
107 p.setPen(Qt::transparent);
108 p.setBrush(hover ? colour.lighter() : colour);
109 p.drawPolygon(points, countof(points));
110
111 p.setPen(colour.lighter());
112 p.setBrush(Qt::transparent);
113 p.drawPolygon(highlight_points, countof(highlight_points));
114
115 p.setPen(colour.darker());
116 p.setBrush(Qt::transparent);
117 p.drawPolygon(points, countof(points));
118
119 // Paint the text
410f9c81 120 p.setPen(get_text_colour());
d7c0ca4a 121 p.setFont(QApplication::font());
b5cb6c35
JH
122 p.drawText(QRectF(r.x(), r.y(),
123 r.width() - label_arrow_length, r.height()),
124 Qt::AlignCenter | Qt::AlignVCenter, _name);
931f20b0
JH
125}
126
9b6378f1
JH
127QMenu* Trace::create_context_menu(QWidget *parent)
128{
129 QMenu *const menu = SelectableItem::create_context_menu(parent);
130
9b6378f1
JH
131 return menu;
132}
133
569d1e41
JH
134pv::widgets::Popup* Trace::create_popup(QWidget *parent)
135{
136 using pv::widgets::Popup;
20eaae39
JH
137
138 _popup = new Popup(parent);
20eaae39 139
37fd11b1 140 create_popup_form();
20eaae39
JH
141
142 connect(_popup, SIGNAL(closed()),
143 this, SLOT(on_popup_closed()));
144
145 return _popup;
569d1e41
JH
146}
147
1053d05c 148QRectF Trace::label_rect(int right)
d7c0ca4a
JH
149{
150 using pv::view::View;
151
d7c0ca4a
JH
152 QFontMetrics m(QApplication::font());
153 const QSize text_size(
154 m.boundingRect(QRect(), 0, _name).width(),
155 m.boundingRect(QRect(), 0, "Tg").height());
156 const QSizeF label_size(
157 text_size.width() + View::LabelPadding.width() * 2,
158 ceilf((text_size.height() + View::LabelPadding.height() * 2) / 2) * 2);
b5cb6c35 159 const float half_height = label_size.height() / 2;
d7c0ca4a 160 return QRectF(
b5cb6c35
JH
161 right - half_height - label_size.width() - 0.5,
162 get_y() + 0.5f - half_height,
163 label_size.width() + half_height,
164 label_size.height());
d7c0ca4a
JH
165}
166
410f9c81
JH
167QColor Trace::get_text_colour() const
168{
169 return (_colour.lightness() > 64) ? Qt::black : Qt::white;
170}
171
fe08b6e8
JH
172void Trace::paint_axis(QPainter &p, int y, int left, int right)
173{
174 p.setPen(AxisPen);
175 p.drawLine(QPointF(left, y + 0.5f), QPointF(right, y + 0.5f));
176}
177
91e8bf08
JH
178void Trace::add_colour_option(QWidget *parent, QFormLayout *form)
179{
180 using pv::widgets::ColourButton;
181
182 ColourButton *const colour_button = new ColourButton(
183 TracePalette::Rows, TracePalette::Cols, parent);
184 colour_button->set_palette(TracePalette::Colours);
185 colour_button->set_colour(_colour);
186 connect(colour_button, SIGNAL(selected(const QColor&)),
187 this, SLOT(on_colour_changed(const QColor&)));
188
189 form->addRow(tr("Colour"), colour_button);
190}
191
37fd11b1
JH
192void Trace::create_popup_form()
193{
194 // Clear the layout
195
196 // Transfer the layout and the child widgets to a temporary widget
197 // which then goes out of scope destroying the layout and all the child
198 // widgets.
199 if (_popup_form)
200 QWidget().setLayout(_popup_form);
201
202 // Repopulate the popup
203 _popup_form = new QFormLayout(_popup);
204 _popup->setLayout(_popup_form);
205 populate_popup_form(_popup, _popup_form);
206}
207
569d1e41
JH
208void Trace::populate_popup_form(QWidget *parent, QFormLayout *form)
209{
68456dab
JH
210 QLineEdit *const name_edit = new QLineEdit(parent);
211 name_edit->setText(_name);
212 connect(name_edit, SIGNAL(textChanged(const QString&)),
213 this, SLOT(on_text_changed(const QString&)));
214 form->addRow(tr("Name"), name_edit);
91e8bf08
JH
215
216 add_colour_option(parent, form);
569d1e41
JH
217}
218
20eaae39
JH
219void Trace::on_popup_closed()
220{
221 _popup = NULL;
222 _popup_form = NULL;
223}
224
68456dab
JH
225void Trace::on_text_changed(const QString &text)
226{
227 set_name(text);
228 text_changed();
229}
9b6378f1 230
91e8bf08
JH
231void Trace::on_colour_changed(const QColor &colour)
232{
233 set_colour(colour);
234 colour_changed();
235}
236
931f20b0
JH
237} // namespace view
238} // namespace pv