]> sigrok.org Git - pulseview.git/blame_incremental - pv/view/trace.cpp
Implement "use coloured background" functionality
[pulseview.git] / pv / view / trace.cpp
... / ...
CommitLineData
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 <cmath>
25
26#include <QApplication>
27#include <QFormLayout>
28#include <QKeyEvent>
29#include <QLineEdit>
30
31#include "trace.hpp"
32#include "tracepalette.hpp"
33#include "view.hpp"
34
35#include <pv/widgets/colourbutton.hpp>
36#include <pv/widgets/popup.hpp>
37
38namespace pv {
39namespace view {
40
41const QPen Trace::AxisPen(QColor(128, 128, 128, 64));
42const int Trace::LabelHitPadding = 2;
43
44Trace::Trace(QString name) :
45 name_(name),
46 coloured_bg_(true), // Default setting is set in MainWindow::setup_ui()
47 popup_(nullptr),
48 popup_form_(nullptr)
49{
50}
51
52QString Trace::name() const
53{
54 return name_;
55}
56
57void Trace::set_name(QString name)
58{
59 name_ = name;
60}
61
62QColor Trace::colour() const
63{
64 return colour_;
65}
66
67void Trace::set_colour(QColor colour)
68{
69 colour_ = colour;
70
71 bgcolour_ = colour;
72 bgcolour_.setAlpha(20);
73}
74
75void Trace::set_coloured_bg(bool state)
76{
77 coloured_bg_ = state;
78}
79
80void Trace::paint_label(QPainter &p, const QRect &rect, bool hover)
81{
82 const int y = get_visual_y();
83
84 p.setBrush(colour_);
85
86 if (!enabled())
87 return;
88
89 const QRectF r = label_rect(rect);
90
91 // Paint the label
92 const float label_arrow_length = r.height() / 2;
93 const QPointF points[] = {
94 r.topLeft(),
95 QPointF(r.right() - label_arrow_length, r.top()),
96 QPointF(r.right(), y),
97 QPointF(r.right() - label_arrow_length, r.bottom()),
98 r.bottomLeft()
99 };
100 const QPointF highlight_points[] = {
101 QPointF(r.left() + 1, r.top() + 1),
102 QPointF(r.right() - label_arrow_length, r.top() + 1),
103 QPointF(r.right() - 1, y),
104 QPointF(r.right() - label_arrow_length, r.bottom() - 1),
105 QPointF(r.left() + 1, r.bottom() - 1)
106 };
107
108 if (selected()) {
109 p.setPen(highlight_pen());
110 p.setBrush(Qt::transparent);
111 p.drawPolygon(points, countof(points));
112 }
113
114 p.setPen(Qt::transparent);
115 p.setBrush(hover ? colour_.lighter() : colour_);
116 p.drawPolygon(points, countof(points));
117
118 p.setPen(colour_.lighter());
119 p.setBrush(Qt::transparent);
120 p.drawPolygon(highlight_points, countof(highlight_points));
121
122 p.setPen(colour_.darker());
123 p.setBrush(Qt::transparent);
124 p.drawPolygon(points, countof(points));
125
126 // Paint the text
127 p.setPen(select_text_colour(colour_));
128 p.setFont(QApplication::font());
129 p.drawText(QRectF(r.x(), r.y(),
130 r.width() - label_arrow_length, r.height()),
131 Qt::AlignCenter | Qt::AlignVCenter, name_);
132}
133
134QMenu* Trace::create_context_menu(QWidget *parent)
135{
136 QMenu *const menu = ViewItem::create_context_menu(parent);
137
138 return menu;
139}
140
141pv::widgets::Popup* Trace::create_popup(QWidget *parent)
142{
143 using pv::widgets::Popup;
144
145 popup_ = new Popup(parent);
146 popup_->set_position(parent->mapToGlobal(
147 point(parent->rect())), Popup::Right);
148
149 create_popup_form();
150
151 connect(popup_, SIGNAL(closed()),
152 this, SLOT(on_popup_closed()));
153
154 return popup_;
155}
156
157QRectF Trace::label_rect(const QRectF &rect) const
158{
159 using pv::view::View;
160
161 QFontMetrics m(QApplication::font());
162 const QSize text_size(
163 m.boundingRect(QRect(), 0, name_).width(), m.height());
164 const QSizeF label_size(
165 text_size.width() + LabelPadding.width() * 2,
166 ceilf((text_size.height() + LabelPadding.height() * 2) / 2) * 2);
167 const float half_height = label_size.height() / 2;
168 return QRectF(
169 rect.right() - half_height - label_size.width() - 0.5,
170 get_visual_y() + 0.5f - half_height,
171 label_size.width() + half_height,
172 label_size.height());
173}
174
175QRectF Trace::hit_box_rect(const ViewItemPaintParams &pp) const
176{
177 const float h = QFontMetrics(QApplication::font()).height();
178 return QRectF(pp.left(), get_visual_y() - h / 2.0f,
179 pp.width(), h);
180}
181
182void Trace::paint_back(QPainter &p, const ViewItemPaintParams &pp)
183{
184 if (coloured_bg_) {
185 p.setPen(QPen(Qt::NoPen));
186 p.setBrush(bgcolour_);
187
188 const std::pair<int, int> extents = v_extents();
189
190 const int x = 0;
191 const int y = get_visual_y() + extents.first;
192 const int w = pp.right() - pp.left();
193 const int h = extents.second - extents.first;
194
195 p.drawRect(x, y, w, h);
196 }
197}
198
199void Trace::paint_axis(QPainter &p, const ViewItemPaintParams &pp, int y)
200{
201 p.setPen(AxisPen);
202 p.drawLine(QPointF(pp.left(), y + 0.5f), QPointF(pp.right(), y + 0.5f));
203}
204
205void Trace::add_colour_option(QWidget *parent, QFormLayout *form)
206{
207 using pv::widgets::ColourButton;
208
209 ColourButton *const colour_button = new ColourButton(
210 TracePalette::Rows, TracePalette::Cols, parent);
211 colour_button->set_palette(TracePalette::Colours);
212 colour_button->set_colour(colour_);
213 connect(colour_button, SIGNAL(selected(const QColor&)),
214 this, SLOT(on_colour_changed(const QColor&)));
215
216 form->addRow(tr("Colour"), colour_button);
217}
218
219void Trace::create_popup_form()
220{
221 // Clear the layout
222
223 // Transfer the layout and the child widgets to a temporary widget
224 // which then goes out of scope destroying the layout and all the child
225 // widgets.
226 if (popup_form_)
227 QWidget().setLayout(popup_form_);
228
229 // Repopulate the popup
230 popup_form_ = new QFormLayout(popup_);
231 popup_->setLayout(popup_form_);
232 populate_popup_form(popup_, popup_form_);
233}
234
235void Trace::populate_popup_form(QWidget *parent, QFormLayout *form)
236{
237 QLineEdit *const name_edit = new QLineEdit(parent);
238 name_edit->setText(name_);
239 connect(name_edit, SIGNAL(textChanged(const QString&)),
240 this, SLOT(on_text_changed(const QString&)));
241 form->addRow(tr("Name"), name_edit);
242
243 add_colour_option(parent, form);
244}
245
246void Trace::on_popup_closed()
247{
248 popup_ = nullptr;
249 popup_form_ = nullptr;
250}
251
252void Trace::on_text_changed(const QString &text)
253{
254 set_name(text);
255
256 if (owner_)
257 owner_->extents_changed(true, false);
258}
259
260void Trace::on_colour_changed(const QColor &colour)
261{
262 set_colour(colour);
263
264 if (owner_)
265 owner_->row_item_appearance_changed(true, true);
266}
267
268} // namespace view
269} // namespace pv