]> sigrok.org Git - pulseview.git/blame - pv/view/analogsignal.cpp
Fix #771 by using black with alpha instead of an opaque grey
[pulseview.git] / pv / view / analogsignal.cpp
CommitLineData
aba1dd16
JH
1/*
2 * This file is part of the PulseView project.
3 *
4 * Copyright (C) 2012 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
3b68d03d
JH
23#include <cassert>
24#include <cmath>
8a2fafcc
JH
25#include <cstdlib>
26#include <limits>
aba1dd16 27
37b9fed4 28#include <QApplication>
368a37c2 29#include <QComboBox>
4cffac16 30#include <QFormLayout>
368a37c2
SA
31#include <QGridLayout>
32#include <QLabel>
4cffac16 33#include <QSpinBox>
03cc651d 34#include <QString>
37b9fed4 35
2acdb232
JH
36#include "analogsignal.hpp"
37#include "pv/data/analog.hpp"
f3d66e52 38#include "pv/data/analogsegment.hpp"
2acdb232 39#include "pv/view/view.hpp"
aba1dd16 40
fe3a1c21 41#include <libsigrokcxx/libsigrokcxx.hpp>
e8d00928 42
819f4c25 43using std::max;
a5d93c27 44using std::make_pair;
819f4c25 45using std::min;
f9abf97e 46using std::shared_ptr;
819f4c25 47using std::deque;
aba1dd16 48
e8d00928
ML
49using sigrok::Channel;
50
aba1dd16
JH
51namespace pv {
52namespace view {
53
568b90d4
JH
54const QColor AnalogSignal::SignalColours[4] = {
55 QColor(0xC4, 0xA0, 0x00), // Yellow
56 QColor(0x87, 0x20, 0x7A), // Magenta
57 QColor(0x20, 0x4A, 0x87), // Blue
58 QColor(0x4E, 0x9A, 0x06) // Green
59};
60
46a0cadc
SA
61const QColor AnalogSignal::GridMajorColor = QColor(0, 0, 0, 40*256/100);
62const QColor AnalogSignal::GridMinorColor = QColor(0, 0, 0, 20*256/100);
37b9fed4 63
7c68ddae
JH
64const float AnalogSignal::EnvelopeThreshold = 256.0f;
65
4cffac16 66const int AnalogSignal::MaximumVDivs = 10;
368a37c2
SA
67const int AnalogSignal::MinScaleIndex = -6;
68const int AnalogSignal::MaxScaleIndex = 7;
4cffac16 69
03cc651d
SA
70const int AnalogSignal::InfoTextMarginRight = 20;
71const int AnalogSignal::InfoTextMarginBottom = 5;
72
b86aa8f4 73AnalogSignal::AnalogSignal(
2b81ae46 74 pv::Session &session,
b86aa8f4 75 shared_ptr<Channel> channel,
e8d00928 76 shared_ptr<data::Analog> data) :
b86aa8f4 77 Signal(session, channel),
8dbbc7f0 78 data_(data),
834a4f1b 79 scale_index_(4), // 20 per div
37b9fed4
SA
80 scale_index_drag_offset_(0),
81 div_height_(3 * QFontMetrics(QApplication::font()).height()),
834a4f1b
SA
82 vdivs_(1),
83 resolution_(0)
aba1dd16 84{
99af6802 85 set_colour(SignalColours[channel_->index() % countof(SignalColours)]);
834a4f1b 86 update_scale();
aba1dd16
JH
87}
88
3009b5b3
JH
89shared_ptr<pv::data::SignalData> AnalogSignal::data() const
90{
8dbbc7f0 91 return data_;
3009b5b3
JH
92}
93
94shared_ptr<pv::data::Analog> AnalogSignal::analog_data() const
9a0cd293 95{
8dbbc7f0 96 return data_;
9a0cd293
JH
97}
98
a5d93c27
JH
99std::pair<int, int> AnalogSignal::v_extents() const
100{
37b9fed4 101 const int h = vdivs_ * div_height_;
8a2fafcc 102 return make_pair(-h, h);
a5d93c27
JH
103}
104
214470fc
JH
105int AnalogSignal::scale_handle_offset() const
106{
37b9fed4
SA
107 const int h = vdivs_ * div_height_;
108
8a2fafcc 109 return ((scale_index_drag_offset_ - scale_index_) *
37b9fed4 110 h / 4) - h / 2;
214470fc
JH
111}
112
113void AnalogSignal::scale_handle_dragged(int offset)
114{
37b9fed4
SA
115 const int h = vdivs_ * div_height_;
116
8a2fafcc 117 scale_index_ = scale_index_drag_offset_ -
37b9fed4 118 (offset + h / 2) / (h / 4);
834a4f1b
SA
119
120 update_scale();
8a2fafcc
JH
121}
122
123void AnalogSignal::scale_handle_drag_release()
124{
125 scale_index_drag_offset_ = scale_index_;
834a4f1b 126 update_scale();
214470fc
JH
127}
128
5b5fa4da 129void AnalogSignal::paint_back(QPainter &p, const ViewItemPaintParams &pp)
fe08b6e8 130{
99af6802
SA
131 if (channel_->enabled()) {
132 Trace::paint_back(p, pp);
97904bf7 133 paint_axis(p, pp, get_visual_y());
99af6802 134 }
fe08b6e8
JH
135}
136
5b5fa4da 137void AnalogSignal::paint_mid(QPainter &p, const ViewItemPaintParams &pp)
aba1dd16 138{
8dbbc7f0 139 assert(data_);
8dbbc7f0 140 assert(owner_);
a8acb46e 141
be9e7b4b 142 const int y = get_visual_y();
01fd3263 143
8dbbc7f0 144 if (!channel_->enabled())
cec48d16
JH
145 return;
146
37b9fed4
SA
147 paint_grid(p, y, pp.left(), pp.right());
148
f3d66e52
JH
149 const deque< shared_ptr<pv::data::AnalogSegment> > &segments =
150 data_->analog_segments();
151 if (segments.empty())
a8acb46e
JH
152 return;
153
f3d66e52
JH
154 const shared_ptr<pv::data::AnalogSegment> &segment =
155 segments.front();
a8acb46e 156
4c8a6a6d 157 const double pixels_offset = pp.pixels_offset();
69e33a1b 158 const double samplerate = max(1.0, segment->samplerate());
60d9b99a 159 const pv::util::Timestamp& start_time = segment->start_time();
f3d66e52 160 const int64_t last_sample = segment->get_sample_count() - 1;
4c8a6a6d 161 const double samples_per_pixel = samplerate * pp.scale();
60d9b99a
JS
162 const pv::util::Timestamp start = samplerate * (pp.offset() - start_time);
163 const pv::util::Timestamp end = start + samples_per_pixel * pp.width();
a8acb46e 164
60d9b99a 165 const int64_t start_sample = min(max(floor(start).convert_to<int64_t>(),
a8acb46e 166 (int64_t)0), last_sample);
60d9b99a 167 const int64_t end_sample = min(max((ceil(end) + 1).convert_to<int64_t>(),
a8acb46e
JH
168 (int64_t)0), last_sample);
169
7c68ddae 170 if (samples_per_pixel < EnvelopeThreshold)
f3d66e52 171 paint_trace(p, segment, y, pp.left(),
7c68ddae
JH
172 start_sample, end_sample,
173 pixels_offset, samples_per_pixel);
174 else
f3d66e52 175 paint_envelope(p, segment, y, pp.left(),
7c68ddae
JH
176 start_sample, end_sample,
177 pixels_offset, samples_per_pixel);
178}
179
03cc651d
SA
180void AnalogSignal::paint_fore(QPainter &p, const ViewItemPaintParams &pp)
181{
182 if (!enabled())
183 return;
184
185 const int y = get_visual_y();
186
187 // Show the info section on the right side of the trace
188 const QString infotext = QString("%1 V/div").arg(resolution_);
189
190 p.setPen(colour_);
191 p.setFont(QApplication::font());
192
193 const QRectF bounding_rect = QRectF(pp.left(),
194 y + v_extents().first,
195 pp.width() - InfoTextMarginRight,
196 v_extents().second - v_extents().first - InfoTextMarginBottom);
197
198 p.drawText(bounding_rect, Qt::AlignRight | Qt::AlignBottom, infotext);
199}
200
37b9fed4
SA
201void AnalogSignal::paint_grid(QPainter &p, int y, int left, int right)
202{
46a0cadc
SA
203 p.setRenderHint(QPainter::Antialiasing, false);
204
205 p.setPen(QPen(GridMajorColor, 1, Qt::DashLine));
37b9fed4 206 for (int i = 1; i <= vdivs_; i++) {
46a0cadc 207 const float dy = i * div_height_;
37b9fed4
SA
208 p.drawLine(QLineF(left, y - dy, right, y - dy));
209 p.drawLine(QLineF(left, y + dy, right, y + dy));
210 }
211
46a0cadc 212 p.setPen(QPen(GridMinorColor, 1, Qt::DashLine));
37b9fed4 213 for (int i = 0; i < vdivs_; i++) {
46a0cadc 214 const float dy = i * div_height_;
37b9fed4
SA
215 const float dy25 = dy + (0.25 * div_height_);
216 const float dy50 = dy + (0.50 * div_height_);
217 const float dy75 = dy + (0.75 * div_height_);
218 p.drawLine(QLineF(left, y - dy25, right, y - dy25));
219 p.drawLine(QLineF(left, y + dy25, right, y + dy25));
220 p.drawLine(QLineF(left, y - dy50, right, y - dy50));
221 p.drawLine(QLineF(left, y + dy50, right, y + dy50));
222 p.drawLine(QLineF(left, y - dy75, right, y - dy75));
223 p.drawLine(QLineF(left, y + dy75, right, y + dy75));
224 }
46a0cadc
SA
225
226 p.setRenderHint(QPainter::Antialiasing, true);
37b9fed4
SA
227}
228
7c68ddae 229void AnalogSignal::paint_trace(QPainter &p,
f3d66e52 230 const shared_ptr<pv::data::AnalogSegment> &segment,
7c68ddae
JH
231 int y, int left, const int64_t start, const int64_t end,
232 const double pixels_offset, const double samples_per_pixel)
233{
234 const int64_t sample_count = end - start;
235
f3d66e52 236 const float *const samples = segment->get_samples(start, end);
a8acb46e
JH
237 assert(samples);
238
8dbbc7f0 239 p.setPen(colour_);
7c68ddae 240
d3758367 241 QPointF *points = new QPointF[sample_count];
a8acb46e
JH
242 QPointF *point = points;
243
7c68ddae 244 for (int64_t sample = start; sample != end; sample++) {
a8acb46e 245 const float x = (sample / samples_per_pixel -
2658961b 246 pixels_offset) + left;
d3758367 247 *point++ = QPointF(x,
834a4f1b 248 y - samples[sample - start] * scale_);
a8acb46e
JH
249 }
250
306d43a7 251 p.drawPolyline(points, point - points);
a8acb46e 252
7c68ddae 253 delete[] samples;
a8acb46e 254 delete[] points;
aba1dd16
JH
255}
256
7c68ddae 257void AnalogSignal::paint_envelope(QPainter &p,
f3d66e52 258 const shared_ptr<pv::data::AnalogSegment> &segment,
7c68ddae
JH
259 int y, int left, const int64_t start, const int64_t end,
260 const double pixels_offset, const double samples_per_pixel)
261{
f3d66e52 262 using pv::data::AnalogSegment;
7c68ddae 263
f3d66e52
JH
264 AnalogSegment::EnvelopeSection e;
265 segment->get_envelope_section(e, start, end, samples_per_pixel);
7c68ddae
JH
266
267 if (e.length < 2)
268 return;
269
819f4c25 270 p.setPen(QPen(Qt::NoPen));
8dbbc7f0 271 p.setBrush(colour_);
7c68ddae
JH
272
273 QRectF *const rects = new QRectF[e.length];
274 QRectF *rect = rects;
275
f3290553 276 for (uint64_t sample = 0; sample < e.length-1; sample++) {
7c68ddae
JH
277 const float x = ((e.scale * sample + e.start) /
278 samples_per_pixel - pixels_offset) + left;
f3d66e52 279 const AnalogSegment::EnvelopeSample *const s =
7c68ddae
JH
280 e.samples + sample;
281
282 // We overlap this sample with the next so that vertical
283 // gaps do not appear during steep rising or falling edges
834a4f1b
SA
284 const float b = y - max(s->max, (s+1)->min) * scale_;
285 const float t = y - min(s->min, (s+1)->max) * scale_;
7c68ddae
JH
286
287 float h = b - t;
f3290553 288 if (h >= 0.0f && h <= 1.0f)
7c68ddae 289 h = 1.0f;
f3290553 290 if (h <= 0.0f && h >= -1.0f)
7c68ddae
JH
291 h = -1.0f;
292
293 *rect++ = QRectF(x, t, 1.0f, h);
294 }
295
296 p.drawRects(rects, e.length);
297
298 delete[] rects;
299 delete[] e.samples;
300}
301
368a37c2 302float AnalogSignal::get_resolution(int scale_index)
8a2fafcc
JH
303{
304 const float seq[] = {1.0f, 2.0f, 5.0f};
834a4f1b 305
8a2fafcc
JH
306 const int offset = std::numeric_limits<int>::max() / (2 * countof(seq));
307 const std::div_t d = std::div(
368a37c2 308 (int)(scale_index + countof(seq) * offset),
8a2fafcc 309 countof(seq));
834a4f1b 310
368a37c2
SA
311 return powf(10.0f, d.quot - offset) * seq[d.rem];
312}
313
314void AnalogSignal::update_scale()
315{
316 resolution_ = get_resolution(scale_index_);
834a4f1b 317 scale_ = div_height_ / resolution_;
8a2fafcc
JH
318}
319
4cffac16
SA
320void AnalogSignal::populate_popup_form(QWidget *parent, QFormLayout *form)
321{
322 // Add the standard options
323 Signal::populate_popup_form(parent, form);
324
368a37c2
SA
325 QFormLayout *const layout = new QFormLayout;
326
327 // Add the number of vdivs
4cffac16
SA
328 QSpinBox *vdiv_sb = new QSpinBox(parent);
329 vdiv_sb->setRange(1, MaximumVDivs);
330 vdiv_sb->setValue(vdivs_);
331 connect(vdiv_sb, SIGNAL(valueChanged(int)),
332 this, SLOT(on_vdivs_changed(int)));
368a37c2
SA
333 layout->addRow(tr("Number of vertical divs"), vdiv_sb);
334
335 // Add the vertical resolution
336 resolution_cb_ = new QComboBox(parent);
337
338 for (int i = MinScaleIndex; i < MaxScaleIndex; i++) {
339 const QString label = QString("%1").arg(get_resolution(i));
340 resolution_cb_->insertItem(0, label, QVariant(i));
341 }
342
343 const int cur_idx = resolution_cb_->findData(QVariant(scale_index_));
344 resolution_cb_->setCurrentIndex(cur_idx);
345
346 connect(resolution_cb_, SIGNAL(currentIndexChanged(int)),
347 this, SLOT(on_resolution_changed(int)));
348
349 QGridLayout *const vdiv_layout = new QGridLayout;
350 QLabel *const vdiv_unit = new QLabel(tr("V/div"));
351 vdiv_layout->addWidget(resolution_cb_, 0, 0);
352 vdiv_layout->addWidget(vdiv_unit, 0, 1);
353
354 layout->addRow(tr("Vertical resolution"), vdiv_layout);
355
356 form->addRow(layout);
4cffac16
SA
357}
358
359void AnalogSignal::on_vdivs_changed(int vdivs)
360{
361 vdivs_ = vdivs;
362
75d0779e
SA
363 if (owner_) {
364 // Call order is important, otherwise the lazy event handler won't work
4cffac16 365 owner_->extents_changed(false, true);
75d0779e
SA
366 owner_->row_item_appearance_changed(false, true);
367 }
4cffac16
SA
368}
369
368a37c2
SA
370void AnalogSignal::on_resolution_changed(int index)
371{
372 scale_index_ = resolution_cb_->itemData(index).toInt();
373 update_scale();
374
375 if (owner_)
376 owner_->row_item_appearance_changed(false, true);
377}
4cffac16 378
aba1dd16
JH
379} // namespace view
380} // namespace pv