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