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