]> sigrok.org Git - pulseview.git/blame - pv/views/trace/analogsignal.cpp
Use getter for the conversion type instead of a local copy
[pulseview.git] / pv / views / trace / 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
efdec55a 17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
aba1dd16
JH
18 */
19
20#include <extdef.h>
21
3b68d03d
JH
22#include <cassert>
23#include <cmath>
8a2fafcc
JH
24#include <cstdlib>
25#include <limits>
a8fd2759 26#include <vector>
aba1dd16 27
37b9fed4 28#include <QApplication>
73e377fe 29#include <QCheckBox>
368a37c2 30#include <QComboBox>
4cffac16 31#include <QFormLayout>
368a37c2
SA
32#include <QGridLayout>
33#include <QLabel>
03cc651d 34#include <QString>
37b9fed4 35
2acdb232 36#include "analogsignal.hpp"
1573bf16
SA
37#include "logicsignal.hpp"
38#include "view.hpp"
39
2acdb232 40#include "pv/data/analog.hpp"
f3d66e52 41#include "pv/data/analogsegment.hpp"
a8fd2759
SA
42#include "pv/data/logic.hpp"
43#include "pv/data/logicsegment.hpp"
bf0edd2b 44#include "pv/data/signalbase.hpp"
8de1e1b2 45#include "pv/globalsettings.hpp"
aba1dd16 46
fe3a1c21 47#include <libsigrokcxx/libsigrokcxx.hpp>
e8d00928 48
6f925ba9
UH
49using std::deque;
50using std::div;
51using std::div_t;
819f4c25 52using std::max;
a5d93c27 53using std::make_pair;
819f4c25 54using std::min;
6f925ba9
UH
55using std::numeric_limits;
56using std::pair;
f9abf97e 57using std::shared_ptr;
a8fd2759 58using std::vector;
aba1dd16
JH
59
60namespace pv {
f4e57597 61namespace views {
1573bf16 62namespace trace {
aba1dd16 63
568b90d4
JH
64const QColor AnalogSignal::SignalColours[4] = {
65 QColor(0xC4, 0xA0, 0x00), // Yellow
66 QColor(0x87, 0x20, 0x7A), // Magenta
67 QColor(0x20, 0x4A, 0x87), // Blue
68 QColor(0x4E, 0x9A, 0x06) // Green
69};
70
c063290a
UH
71const QColor AnalogSignal::GridMajorColor = QColor(0, 0, 0, 40 * 256 / 100);
72const QColor AnalogSignal::GridMinorColor = QColor(0, 0, 0, 20 * 256 / 100);
37b9fed4 73
8de1e1b2
UH
74const QColor AnalogSignal::SamplingPointColour(0x77, 0x77, 0x77);
75
fdfd5b3e
SA
76const int64_t AnalogSignal::TracePaintBlockSize = 1024 * 1024; // 4 MiB (due to float)
77const float AnalogSignal::EnvelopeThreshold = 64.0f;
7c68ddae 78
4cffac16 79const int AnalogSignal::MaximumVDivs = 10;
368a37c2
SA
80const int AnalogSignal::MinScaleIndex = -6;
81const int AnalogSignal::MaxScaleIndex = 7;
4cffac16 82
03cc651d
SA
83const int AnalogSignal::InfoTextMarginRight = 20;
84const int AnalogSignal::InfoTextMarginBottom = 5;
85
b86aa8f4 86AnalogSignal::AnalogSignal(
2b81ae46 87 pv::Session &session,
cbd2a2de 88 shared_ptr<data::SignalBase> base) :
73a25a6e 89 Signal(session, base),
834a4f1b 90 scale_index_(4), // 20 per div
37b9fed4 91 scale_index_drag_offset_(0),
459db2c5
SA
92 pos_vdivs_(1),
93 neg_vdivs_(1),
73e377fe 94 resolution_(0),
a970015f 95 display_type_(DisplayBoth),
1f1d55ce 96 autoranging_(true)
aba1dd16 97{
85715407
SA
98 pv::data::Analog* analog_data =
99 dynamic_cast<pv::data::Analog*>(data().get());
100
101 connect(analog_data, SIGNAL(samples_added(QObject*, uint64_t, uint64_t)),
102 this, SLOT(on_samples_added()));
103
cbb50547
SA
104 GlobalSettings gs;
105 div_height_ = gs.value(GlobalSettings::Key_View_DefaultDivHeight).toInt();
106
73a25a6e 107 base_->set_colour(SignalColours[base_->index() % countof(SignalColours)]);
834a4f1b 108 update_scale();
aba1dd16
JH
109}
110
3009b5b3
JH
111shared_ptr<pv::data::SignalData> AnalogSignal::data() const
112{
cbd2a2de 113 return base_->analog_data();
9a0cd293
JH
114}
115
3a21afa6
SA
116void AnalogSignal::save_settings(QSettings &settings) const
117{
459db2c5
SA
118 settings.setValue("pos_vdivs", pos_vdivs_);
119 settings.setValue("neg_vdivs", neg_vdivs_);
3a21afa6 120 settings.setValue("scale_index", scale_index_);
a970015f 121 settings.setValue("display_type", display_type_);
73e377fe 122 settings.setValue("autoranging", autoranging_);
cbb50547 123 settings.setValue("div_height", div_height_);
3a21afa6
SA
124}
125
126void AnalogSignal::restore_settings(QSettings &settings)
127{
459db2c5
SA
128 if (settings.contains("pos_vdivs"))
129 pos_vdivs_ = settings.value("pos_vdivs").toInt();
130
131 if (settings.contains("neg_vdivs"))
132 neg_vdivs_ = settings.value("neg_vdivs").toInt();
3a21afa6
SA
133
134 if (settings.contains("scale_index")) {
135 scale_index_ = settings.value("scale_index").toInt();
136 update_scale();
137 }
73e377fe 138
a970015f
SA
139 if (settings.contains("display_type"))
140 display_type_ = (DisplayType)(settings.value("display_type").toInt());
141
73e377fe
SA
142 if (settings.contains("autoranging"))
143 autoranging_ = settings.value("autoranging").toBool();
cbb50547
SA
144
145 if (settings.contains("div_height")) {
146 const int old_height = div_height_;
147 div_height_ = settings.value("div_height").toInt();
148
149 if ((div_height_ != old_height) && owner_) {
150 // Call order is important, otherwise the lazy event handler won't work
151 owner_->extents_changed(false, true);
152 owner_->row_item_appearance_changed(false, true);
153 }
154 }
3a21afa6
SA
155}
156
6f925ba9 157pair<int, int> AnalogSignal::v_extents() const
a5d93c27 158{
459db2c5
SA
159 const int ph = pos_vdivs_ * div_height_;
160 const int nh = neg_vdivs_ * div_height_;
161 return make_pair(-ph, nh);
a5d93c27
JH
162}
163
214470fc
JH
164int AnalogSignal::scale_handle_offset() const
165{
459db2c5 166 const int h = (pos_vdivs_ + neg_vdivs_) * div_height_;
37b9fed4 167
c063290a 168 return ((scale_index_drag_offset_ - scale_index_) * h / 4) - h / 2;
214470fc
JH
169}
170
171void AnalogSignal::scale_handle_dragged(int offset)
172{
459db2c5 173 const int h = (pos_vdivs_ + neg_vdivs_) * div_height_;
37b9fed4 174
c063290a 175 scale_index_ = scale_index_drag_offset_ - (offset + h / 2) / (h / 4);
834a4f1b
SA
176
177 update_scale();
8a2fafcc
JH
178}
179
180void AnalogSignal::scale_handle_drag_release()
181{
182 scale_index_drag_offset_ = scale_index_;
834a4f1b 183 update_scale();
214470fc
JH
184}
185
60938e04 186void AnalogSignal::paint_back(QPainter &p, ViewItemPaintParams &pp)
fe08b6e8 187{
73a25a6e 188 if (base_->enabled()) {
99af6802 189 Trace::paint_back(p, pp);
97904bf7 190 paint_axis(p, pp, get_visual_y());
99af6802 191 }
fe08b6e8
JH
192}
193
60938e04 194void AnalogSignal::paint_mid(QPainter &p, ViewItemPaintParams &pp)
aba1dd16 195{
cbd2a2de 196 assert(base_->analog_data());
8dbbc7f0 197 assert(owner_);
a8acb46e 198
be9e7b4b 199 const int y = get_visual_y();
01fd3263 200
73a25a6e 201 if (!base_->enabled())
cec48d16
JH
202 return;
203
a8fd2759
SA
204 if ((display_type_ == DisplayAnalog) || (display_type_ == DisplayBoth)) {
205 paint_grid(p, y, pp.left(), pp.right());
206
207 const deque< shared_ptr<pv::data::AnalogSegment> > &segments =
208 base_->analog_data()->analog_segments();
209 if (segments.empty())
210 return;
211
212 const shared_ptr<pv::data::AnalogSegment> &segment =
213 segments.front();
214
215 const double pixels_offset = pp.pixels_offset();
216 const double samplerate = max(1.0, segment->samplerate());
217 const pv::util::Timestamp& start_time = segment->start_time();
218 const int64_t last_sample = segment->get_sample_count() - 1;
219 const double samples_per_pixel = samplerate * pp.scale();
220 const pv::util::Timestamp start = samplerate * (pp.offset() - start_time);
221 const pv::util::Timestamp end = start + samples_per_pixel * pp.width();
222
223 const int64_t start_sample = min(max(floor(start).convert_to<int64_t>(),
224 (int64_t)0), last_sample);
225 const int64_t end_sample = min(max((ceil(end) + 1).convert_to<int64_t>(),
226 (int64_t)0), last_sample);
227
228 if (samples_per_pixel < EnvelopeThreshold)
229 paint_trace(p, segment, y, pp.left(),
230 start_sample, end_sample,
231 pixels_offset, samples_per_pixel);
232 else
233 paint_envelope(p, segment, y, pp.left(),
234 start_sample, end_sample,
235 pixels_offset, samples_per_pixel);
236 }
a8acb46e 237
a8fd2759 238 if ((display_type_ == DisplayConverted) || (display_type_ == DisplayBoth)) {
06b6ce26
SA
239 const data::SignalBase::ConversionType conv_type =
240 base_->get_conversion_type();
241
242 if (((conv_type == data::SignalBase::A2LConversionByTreshold) ||
243 (conv_type == data::SignalBase::A2LConversionBySchmittTrigger))) {
a8acb46e 244
a8fd2759
SA
245 paint_logic_mid(p, pp);
246 }
247 }
7c68ddae
JH
248}
249
60938e04 250void AnalogSignal::paint_fore(QPainter &p, ViewItemPaintParams &pp)
03cc651d
SA
251{
252 if (!enabled())
253 return;
254
b8c4a95b
SA
255 if ((display_type_ == DisplayAnalog) || (display_type_ == DisplayBoth)) {
256 const int y = get_visual_y();
03cc651d 257
b8c4a95b
SA
258 // Show the info section on the right side of the trace
259 const QString infotext = QString("%1 V/div").arg(resolution_);
03cc651d 260
b8c4a95b
SA
261 p.setPen(base_->colour());
262 p.setFont(QApplication::font());
03cc651d 263
b8c4a95b
SA
264 const QRectF bounding_rect = QRectF(pp.left(),
265 y + v_extents().first,
266 pp.width() - InfoTextMarginRight,
267 v_extents().second - v_extents().first - InfoTextMarginBottom);
03cc651d 268
b8c4a95b
SA
269 p.drawText(bounding_rect, Qt::AlignRight | Qt::AlignBottom, infotext);
270 }
03cc651d
SA
271}
272
37b9fed4
SA
273void AnalogSignal::paint_grid(QPainter &p, int y, int left, int right)
274{
46a0cadc
SA
275 p.setRenderHint(QPainter::Antialiasing, false);
276
8ad61f40
UH
277 GlobalSettings settings;
278 const bool show_analog_minor_grid =
279 settings.value(GlobalSettings::Key_View_ShowAnalogMinorGrid).toBool();
280
459db2c5
SA
281 if (pos_vdivs_ > 0) {
282 p.setPen(QPen(GridMajorColor, 1, Qt::DashLine));
283 for (int i = 1; i <= pos_vdivs_; i++) {
284 const float dy = i * div_height_;
285 p.drawLine(QLineF(left, y - dy, right, y - dy));
286 }
8ad61f40 287 }
459db2c5 288
8ad61f40 289 if ((pos_vdivs_ > 0) && show_analog_minor_grid) {
459db2c5
SA
290 p.setPen(QPen(GridMinorColor, 1, Qt::DashLine));
291 for (int i = 0; i < pos_vdivs_; i++) {
292 const float dy = i * div_height_;
293 const float dy25 = dy + (0.25 * div_height_);
294 const float dy50 = dy + (0.50 * div_height_);
295 const float dy75 = dy + (0.75 * div_height_);
296 p.drawLine(QLineF(left, y - dy25, right, y - dy25));
297 p.drawLine(QLineF(left, y - dy50, right, y - dy50));
298 p.drawLine(QLineF(left, y - dy75, right, y - dy75));
299 }
37b9fed4
SA
300 }
301
459db2c5
SA
302 if (neg_vdivs_ > 0) {
303 p.setPen(QPen(GridMajorColor, 1, Qt::DashLine));
304 for (int i = 1; i <= neg_vdivs_; i++) {
305 const float dy = i * div_height_;
306 p.drawLine(QLineF(left, y + dy, right, y + dy));
307 }
8ad61f40 308 }
459db2c5 309
8ad61f40 310 if ((pos_vdivs_ > 0) && show_analog_minor_grid) {
459db2c5
SA
311 p.setPen(QPen(GridMinorColor, 1, Qt::DashLine));
312 for (int i = 0; i < neg_vdivs_; i++) {
313 const float dy = i * div_height_;
314 const float dy25 = dy + (0.25 * div_height_);
315 const float dy50 = dy + (0.50 * div_height_);
316 const float dy75 = dy + (0.75 * div_height_);
317 p.drawLine(QLineF(left, y + dy25, right, y + dy25));
318 p.drawLine(QLineF(left, y + dy50, right, y + dy50));
319 p.drawLine(QLineF(left, y + dy75, right, y + dy75));
320 }
37b9fed4 321 }
46a0cadc
SA
322
323 p.setRenderHint(QPainter::Antialiasing, true);
37b9fed4
SA
324}
325
7c68ddae 326void AnalogSignal::paint_trace(QPainter &p,
f3d66e52 327 const shared_ptr<pv::data::AnalogSegment> &segment,
7c68ddae
JH
328 int y, int left, const int64_t start, const int64_t end,
329 const double pixels_offset, const double samples_per_pixel)
330{
fdfd5b3e
SA
331 if (end <= start)
332 return;
333
334 // Calculate and paint the sampling points if enabled and useful
335 GlobalSettings settings;
336 const bool show_sampling_points =
337 settings.value(GlobalSettings::Key_View_ShowSamplingPoints).toBool() &&
338 (samples_per_pixel < 0.25);
339
73a25a6e 340 p.setPen(base_->colour());
7c68ddae 341
76ce6c7a
UH
342 const int64_t points_count = end - start;
343
344 QPointF *points = new QPointF[points_count];
a8acb46e
JH
345 QPointF *point = points;
346
fdfd5b3e
SA
347 QRectF *sampling_points = nullptr;
348 if (show_sampling_points)
349 sampling_points = new QRectF[points_count];
8de1e1b2
UH
350 QRectF *sampling_point = sampling_points;
351
fdfd5b3e
SA
352 int64_t sample_count = min(points_count, TracePaintBlockSize);
353 int64_t block_sample = 0;
b82243f7
SA
354 float *sample_block = new float[TracePaintBlockSize];
355 segment->get_samples(start, start + sample_count, sample_block);
26a883ed 356
8de1e1b2 357 const int w = 2;
fdfd5b3e
SA
358 for (int64_t sample = start; sample != end; sample++, block_sample++) {
359
360 if (block_sample == TracePaintBlockSize) {
361 block_sample = 0;
fdfd5b3e 362 sample_count = min(points_count - sample, TracePaintBlockSize);
b82243f7 363 segment->get_samples(sample, sample + sample_count, sample_block);
fdfd5b3e
SA
364 }
365
a8acb46e 366 const float x = (sample / samples_per_pixel -
2658961b 367 pixels_offset) + left;
26a883ed 368
fdfd5b3e 369 *point++ = QPointF(x, y - sample_block[block_sample] * scale_);
8de1e1b2 370
fdfd5b3e
SA
371 if (show_sampling_points)
372 *sampling_point++ =
373 QRectF(x - (w / 2), y - sample_block[block_sample] * scale_ - (w / 2), w, w);
a8acb46e 374 }
fdfd5b3e 375 delete[] sample_block;
a8acb46e 376
76ce6c7a 377 p.drawPolyline(points, points_count);
a8acb46e 378
fdfd5b3e 379 if (show_sampling_points) {
8de1e1b2
UH
380 p.setPen(SamplingPointColour);
381 p.drawRects(sampling_points, points_count);
fdfd5b3e 382 delete[] sampling_points;
8de1e1b2
UH
383 }
384
a8acb46e 385 delete[] points;
aba1dd16
JH
386}
387
7c68ddae 388void AnalogSignal::paint_envelope(QPainter &p,
f3d66e52 389 const shared_ptr<pv::data::AnalogSegment> &segment,
7c68ddae
JH
390 int y, int left, const int64_t start, const int64_t end,
391 const double pixels_offset, const double samples_per_pixel)
392{
f3d66e52 393 using pv::data::AnalogSegment;
7c68ddae 394
f3d66e52
JH
395 AnalogSegment::EnvelopeSection e;
396 segment->get_envelope_section(e, start, end, samples_per_pixel);
7c68ddae
JH
397
398 if (e.length < 2)
399 return;
400
819f4c25 401 p.setPen(QPen(Qt::NoPen));
73a25a6e 402 p.setBrush(base_->colour());
7c68ddae
JH
403
404 QRectF *const rects = new QRectF[e.length];
405 QRectF *rect = rects;
406
c063290a 407 for (uint64_t sample = 0; sample < e.length - 1; sample++) {
7c68ddae
JH
408 const float x = ((e.scale * sample + e.start) /
409 samples_per_pixel - pixels_offset) + left;
f3d66e52 410 const AnalogSegment::EnvelopeSample *const s =
7c68ddae
JH
411 e.samples + sample;
412
413 // We overlap this sample with the next so that vertical
414 // gaps do not appear during steep rising or falling edges
c063290a
UH
415 const float b = y - max(s->max, (s + 1)->min) * scale_;
416 const float t = y - min(s->min, (s + 1)->max) * scale_;
7c68ddae
JH
417
418 float h = b - t;
f3290553 419 if (h >= 0.0f && h <= 1.0f)
7c68ddae 420 h = 1.0f;
f3290553 421 if (h <= 0.0f && h >= -1.0f)
7c68ddae
JH
422 h = -1.0f;
423
424 *rect++ = QRectF(x, t, 1.0f, h);
425 }
426
427 p.drawRects(rects, e.length);
428
429 delete[] rects;
430 delete[] e.samples;
431}
432
60938e04 433void AnalogSignal::paint_logic_mid(QPainter &p, ViewItemPaintParams &pp)
a8fd2759
SA
434{
435 QLineF *line;
436
437 vector< pair<int64_t, bool> > edges;
438
439 assert(base_);
440
441 const int y = get_visual_y();
442
6c0df7c4 443 if (!base_->enabled() || !base_->logic_data())
a8fd2759
SA
444 return;
445
446 const int signal_margin =
447 QFontMetrics(QApplication::font()).height() / 2;
448
449 const int ph = min(pos_vdivs_, 1) * div_height_;
450 const int nh = min(neg_vdivs_, 1) * div_height_;
451 const float high_offset = y - ph + signal_margin + 0.5f;
452 const float low_offset = y + nh - signal_margin - 0.5f;
453
454 const deque< shared_ptr<pv::data::LogicSegment> > &segments =
455 base_->logic_data()->logic_segments();
6c0df7c4 456
a8fd2759
SA
457 if (segments.empty())
458 return;
459
460 const shared_ptr<pv::data::LogicSegment> &segment =
461 segments.front();
462
463 double samplerate = segment->samplerate();
464
465 // Show sample rate as 1Hz when it is unknown
466 if (samplerate == 0.0)
467 samplerate = 1.0;
468
469 const double pixels_offset = pp.pixels_offset();
470 const pv::util::Timestamp& start_time = segment->start_time();
471 const int64_t last_sample = segment->get_sample_count() - 1;
472 const double samples_per_pixel = samplerate * pp.scale();
124be547 473 const double pixels_per_sample = 1 / samples_per_pixel;
a8fd2759
SA
474 const pv::util::Timestamp start = samplerate * (pp.offset() - start_time);
475 const pv::util::Timestamp end = start + samples_per_pixel * pp.width();
476
477 const int64_t start_sample = min(max(floor(start).convert_to<int64_t>(),
478 (int64_t)0), last_sample);
479 const uint64_t end_sample = min(max(ceil(end).convert_to<int64_t>(),
480 (int64_t)0), last_sample);
481
482 segment->get_subsampled_edges(edges, start_sample, end_sample,
483 samples_per_pixel / LogicSignal::Oversampling, 0);
484 assert(edges.size() >= 2);
485
124be547
SA
486 // Check whether we need to paint the sampling points
487 GlobalSettings settings;
488 const bool show_sampling_points =
489 settings.value(GlobalSettings::Key_View_ShowSamplingPoints).toBool() &&
490 (samples_per_pixel < 0.25);
491
492 vector<QRectF> sampling_points;
493 float sampling_point_x = 0.0f;
494 int64_t sampling_point_sample = start_sample;
495 const int w = 2;
496
497 if (show_sampling_points) {
498 sampling_points.reserve(end_sample - start_sample + 1);
499 sampling_point_x = (edges.cbegin()->first / samples_per_pixel - pixels_offset) + pp.left();
500 }
501
a8fd2759
SA
502 // Paint the edges
503 const unsigned int edge_count = edges.size() - 2;
504 QLineF *const edge_lines = new QLineF[edge_count];
505 line = edge_lines;
506
507 for (auto i = edges.cbegin() + 1; i != edges.cend() - 1; i++) {
508 const float x = ((*i).first / samples_per_pixel -
509 pixels_offset) + pp.left();
510 *line++ = QLineF(x, high_offset, x, low_offset);
124be547
SA
511
512 if (show_sampling_points)
513 while (sampling_point_sample < (*i).first) {
514 const float y = (*i).second ? low_offset : high_offset;
515 sampling_points.emplace_back(
516 QRectF(sampling_point_x - (w / 2), y - (w / 2), w, w));
517 sampling_point_sample++;
518 sampling_point_x += pixels_per_sample;
519 };
a8fd2759
SA
520 }
521
124be547
SA
522 // Calculate the sample points from the last edge to the end of the trace
523 if (show_sampling_points)
524 while ((uint64_t)sampling_point_sample <= end_sample) {
525 // Signal changed after the last edge, so the level is inverted
526 const float y = (edges.cend() - 1)->second ? high_offset : low_offset;
527 sampling_points.emplace_back(
528 QRectF(sampling_point_x - (w / 2), y - (w / 2), w, w));
529 sampling_point_sample++;
530 sampling_point_x += pixels_per_sample;
531 };
532
a8fd2759
SA
533 p.setPen(LogicSignal::EdgeColour);
534 p.drawLines(edge_lines, edge_count);
535 delete[] edge_lines;
536
537 // Paint the caps
538 const unsigned int max_cap_line_count = edges.size();
539 QLineF *const cap_lines = new QLineF[max_cap_line_count];
540
541 p.setPen(LogicSignal::HighColour);
542 paint_logic_caps(p, cap_lines, edges, true, samples_per_pixel,
543 pixels_offset, pp.left(), high_offset);
544 p.setPen(LogicSignal::LowColour);
545 paint_logic_caps(p, cap_lines, edges, false, samples_per_pixel,
546 pixels_offset, pp.left(), low_offset);
547
548 delete[] cap_lines;
549
a8fd2759 550 // Paint the sampling points
124be547
SA
551 if (show_sampling_points) {
552 p.setPen(SamplingPointColour);
553 p.drawRects(sampling_points.data(), sampling_points.size());
a8fd2759 554 }
a8fd2759
SA
555}
556
557void AnalogSignal::paint_logic_caps(QPainter &p, QLineF *const lines,
558 vector< pair<int64_t, bool> > &edges, bool level,
559 double samples_per_pixel, double pixels_offset, float x_offset,
560 float y_offset)
561{
562 QLineF *line = lines;
563
564 for (auto i = edges.begin(); i != (edges.end() - 1); i++)
565 if ((*i).second == level) {
566 *line++ = QLineF(
567 ((*i).first / samples_per_pixel -
568 pixels_offset) + x_offset, y_offset,
569 ((*(i+1)).first / samples_per_pixel -
570 pixels_offset) + x_offset, y_offset);
571 }
572
573 p.drawLines(lines, line - lines);
574}
575
368a37c2 576float AnalogSignal::get_resolution(int scale_index)
8a2fafcc
JH
577{
578 const float seq[] = {1.0f, 2.0f, 5.0f};
834a4f1b 579
6f925ba9
UH
580 const int offset = numeric_limits<int>::max() / (2 * countof(seq));
581 const div_t d = div((int)(scale_index + countof(seq) * offset),
8a2fafcc 582 countof(seq));
834a4f1b 583
368a37c2
SA
584 return powf(10.0f, d.quot - offset) * seq[d.rem];
585}
586
587void AnalogSignal::update_scale()
588{
589 resolution_ = get_resolution(scale_index_);
834a4f1b 590 scale_ = div_height_ / resolution_;
8a2fafcc
JH
591}
592
430b94aa 593void AnalogSignal::perform_autoranging(bool keep_divs, bool force_update)
73e377fe
SA
594{
595 const deque< shared_ptr<pv::data::AnalogSegment> > &segments =
596 base_->analog_data()->analog_segments();
597
598 if (segments.empty())
599 return;
600
601 static double prev_min = 0, prev_max = 0;
602 double min = 0, max = 0;
603
604 for (shared_ptr<pv::data::AnalogSegment> segment : segments) {
6f925ba9 605 pair<double, double> mm = segment->get_min_max();
73e377fe
SA
606 min = std::min(min, mm.first);
607 max = std::max(max, mm.second);
608 }
609
610 if ((min == prev_min) && (max == prev_max) && !force_update)
611 return;
612
613 prev_min = min;
614 prev_max = max;
615
430b94aa
SA
616 // If we're allowed to alter the div assignment...
617 if (!keep_divs) {
618 // Use all divs for the positive range if there are no negative values
619 if ((min == 0) && (neg_vdivs_ > 0)) {
620 pos_vdivs_ += neg_vdivs_;
621 neg_vdivs_ = 0;
622 }
73e377fe 623
430b94aa
SA
624 // Split up the divs if there are negative values but no negative divs
625 if ((min < 0) && (neg_vdivs_ == 0)) {
626 neg_vdivs_ = pos_vdivs_ / 2;
627 pos_vdivs_ -= neg_vdivs_;
628 }
681b6d5a
SA
629 }
630
430b94aa
SA
631 // If there is still no positive div when we need it, add one
632 // (this can happen when pos_vdivs==neg_vdivs==0)
17c7f31c 633 if ((max > 0) && (pos_vdivs_ == 0)) {
430b94aa 634 pos_vdivs_ = 1;
17c7f31c
SA
635 owner_->extents_changed(false, true);
636 }
430b94aa
SA
637
638 // If there is still no negative div when we need it, add one
639 // (this can happen when pos_vdivs was 0 or 1 when trying to split)
17c7f31c 640 if ((min < 0) && (neg_vdivs_ == 0)) {
430b94aa 641 neg_vdivs_ = 1;
17c7f31c
SA
642 owner_->extents_changed(false, true);
643 }
430b94aa 644
73e377fe
SA
645 double min_value_per_div;
646 if ((pos_vdivs_ > 0) && (neg_vdivs_ > 0))
647 min_value_per_div = std::max(max / pos_vdivs_, -min / neg_vdivs_);
648 else if (pos_vdivs_ > 0)
649 min_value_per_div = max / pos_vdivs_;
650 else
651 min_value_per_div = -min / neg_vdivs_;
652
653 // Find first scale value that is bigger than the value we need
654 for (int i = MinScaleIndex; i < MaxScaleIndex; i++)
655 if (get_resolution(i) > min_value_per_div) {
656 scale_index_ = i;
657 break;
658 }
659
660 update_scale();
661}
662
4cffac16
SA
663void AnalogSignal::populate_popup_form(QWidget *parent, QFormLayout *form)
664{
665 // Add the standard options
666 Signal::populate_popup_form(parent, form);
667
368a37c2
SA
668 QFormLayout *const layout = new QFormLayout;
669
cbb50547 670 // Add div-related settings
430b94aa
SA
671 pvdiv_sb_ = new QSpinBox(parent);
672 pvdiv_sb_->setRange(0, MaximumVDivs);
673 pvdiv_sb_->setValue(pos_vdivs_);
674 connect(pvdiv_sb_, SIGNAL(valueChanged(int)),
459db2c5 675 this, SLOT(on_pos_vdivs_changed(int)));
430b94aa 676 layout->addRow(tr("Number of pos vertical divs"), pvdiv_sb_);
459db2c5 677
430b94aa
SA
678 nvdiv_sb_ = new QSpinBox(parent);
679 nvdiv_sb_->setRange(0, MaximumVDivs);
680 nvdiv_sb_->setValue(neg_vdivs_);
681 connect(nvdiv_sb_, SIGNAL(valueChanged(int)),
459db2c5 682 this, SLOT(on_neg_vdivs_changed(int)));
430b94aa 683 layout->addRow(tr("Number of neg vertical divs"), nvdiv_sb_);
368a37c2 684
cbb50547
SA
685 div_height_sb_ = new QSpinBox(parent);
686 div_height_sb_->setRange(20, 1000);
687 div_height_sb_->setSingleStep(5);
688 div_height_sb_->setSuffix(tr(" pixels"));
689 div_height_sb_->setValue(div_height_);
690 connect(div_height_sb_, SIGNAL(valueChanged(int)),
691 this, SLOT(on_div_height_changed(int)));
692 layout->addRow(tr("Div height"), div_height_sb_);
693
368a37c2
SA
694 // Add the vertical resolution
695 resolution_cb_ = new QComboBox(parent);
696
697 for (int i = MinScaleIndex; i < MaxScaleIndex; i++) {
698 const QString label = QString("%1").arg(get_resolution(i));
699 resolution_cb_->insertItem(0, label, QVariant(i));
700 }
701
a970015f 702 int cur_idx = resolution_cb_->findData(QVariant(scale_index_));
368a37c2
SA
703 resolution_cb_->setCurrentIndex(cur_idx);
704
705 connect(resolution_cb_, SIGNAL(currentIndexChanged(int)),
706 this, SLOT(on_resolution_changed(int)));
707
708 QGridLayout *const vdiv_layout = new QGridLayout;
709 QLabel *const vdiv_unit = new QLabel(tr("V/div"));
710 vdiv_layout->addWidget(resolution_cb_, 0, 0);
711 vdiv_layout->addWidget(vdiv_unit, 0, 1);
712
713 layout->addRow(tr("Vertical resolution"), vdiv_layout);
714
73e377fe
SA
715 // Add the autoranging checkbox
716 QCheckBox* autoranging_cb = new QCheckBox();
717 autoranging_cb->setCheckState(autoranging_ ? Qt::Checked : Qt::Unchecked);
718
719 connect(autoranging_cb, SIGNAL(stateChanged(int)),
720 this, SLOT(on_autoranging_changed(int)));
721
722 layout->addRow(tr("Autoranging"), autoranging_cb);
723
a970015f
SA
724 // Add the conversion type dropdown
725 conversion_cb_ = new QComboBox();
726
727 conversion_cb_->addItem("none", data::SignalBase::NoConversion);
728 conversion_cb_->addItem("to logic via threshold", data::SignalBase::A2LConversionByTreshold);
729 conversion_cb_->addItem("to logic via schmitt-trigger", data::SignalBase::A2LConversionBySchmittTrigger);
730
06b6ce26 731 cur_idx = conversion_cb_->findData(QVariant(base_->get_conversion_type()));
a970015f
SA
732 conversion_cb_->setCurrentIndex(cur_idx);
733
7c84c074 734 layout->addRow(tr("Conversion"), conversion_cb_);
a970015f
SA
735
736 connect(conversion_cb_, SIGNAL(currentIndexChanged(int)),
737 this, SLOT(on_conversion_changed(int)));
738
739 // Add the display type dropdown
740 display_type_cb_ = new QComboBox();
741
742 display_type_cb_->addItem(tr("Analog"), DisplayAnalog);
743 display_type_cb_->addItem(tr("Converted"), DisplayConverted);
744 display_type_cb_->addItem(tr("Both"), DisplayBoth);
745
746 cur_idx = display_type_cb_->findData(QVariant(display_type_));
747 display_type_cb_->setCurrentIndex(cur_idx);
748
7c84c074 749 layout->addRow(tr("Traces to show:"), display_type_cb_);
a970015f 750
b8c4a95b
SA
751 connect(display_type_cb_, SIGNAL(currentIndexChanged(int)),
752 this, SLOT(on_display_type_changed(int)));
753
368a37c2 754 form->addRow(layout);
4cffac16
SA
755}
756
85715407
SA
757void AnalogSignal::on_samples_added()
758{
430b94aa 759 perform_autoranging(false, false);
85715407
SA
760}
761
459db2c5
SA
762void AnalogSignal::on_pos_vdivs_changed(int vdivs)
763{
430b94aa
SA
764 if (vdivs == pos_vdivs_)
765 return;
766
459db2c5
SA
767 pos_vdivs_ = vdivs;
768
80067e49
SA
769 // There has to be at least one div, positive or negative
770 if ((neg_vdivs_ == 0) && (pos_vdivs_ == 0)) {
771 pos_vdivs_ = 1;
772 if (pvdiv_sb_)
773 pvdiv_sb_->setValue(pos_vdivs_);
774 }
775
430b94aa
SA
776 if (autoranging_) {
777 perform_autoranging(true, true);
778
779 // It could be that a positive or negative div was added, so update
780 if (pvdiv_sb_) {
781 pvdiv_sb_->setValue(pos_vdivs_);
782 nvdiv_sb_->setValue(neg_vdivs_);
783 }
784 }
73e377fe 785
459db2c5
SA
786 if (owner_) {
787 // Call order is important, otherwise the lazy event handler won't work
788 owner_->extents_changed(false, true);
789 owner_->row_item_appearance_changed(false, true);
790 }
791}
792
793void AnalogSignal::on_neg_vdivs_changed(int vdivs)
4cffac16 794{
430b94aa
SA
795 if (vdivs == neg_vdivs_)
796 return;
797
459db2c5 798 neg_vdivs_ = vdivs;
4cffac16 799
80067e49
SA
800 // There has to be at least one div, positive or negative
801 if ((neg_vdivs_ == 0) && (pos_vdivs_ == 0)) {
802 pos_vdivs_ = 1;
803 if (pvdiv_sb_)
804 pvdiv_sb_->setValue(pos_vdivs_);
805 }
806
430b94aa
SA
807 if (autoranging_) {
808 perform_autoranging(true, true);
809
810 // It could be that a positive or negative div was added, so update
811 if (pvdiv_sb_) {
812 pvdiv_sb_->setValue(pos_vdivs_);
813 nvdiv_sb_->setValue(neg_vdivs_);
814 }
815 }
73e377fe 816
75d0779e
SA
817 if (owner_) {
818 // Call order is important, otherwise the lazy event handler won't work
4cffac16 819 owner_->extents_changed(false, true);
75d0779e
SA
820 owner_->row_item_appearance_changed(false, true);
821 }
4cffac16
SA
822}
823
cbb50547
SA
824void AnalogSignal::on_div_height_changed(int height)
825{
826 div_height_ = height;
827 update_scale();
828
829 if (owner_) {
830 // Call order is important, otherwise the lazy event handler won't work
831 owner_->extents_changed(false, true);
832 owner_->row_item_appearance_changed(false, true);
833 }
834}
835
368a37c2
SA
836void AnalogSignal::on_resolution_changed(int index)
837{
838 scale_index_ = resolution_cb_->itemData(index).toInt();
839 update_scale();
840
841 if (owner_)
842 owner_->row_item_appearance_changed(false, true);
843}
4cffac16 844
73e377fe
SA
845void AnalogSignal::on_autoranging_changed(int state)
846{
847 autoranging_ = (state == Qt::Checked);
848
849 if (autoranging_)
430b94aa 850 perform_autoranging(false, true);
73e377fe 851
85715407
SA
852 if (owner_) {
853 // Call order is important, otherwise the lazy event handler won't work
854 owner_->extents_changed(false, true);
73e377fe 855 owner_->row_item_appearance_changed(false, true);
85715407 856 }
73e377fe
SA
857}
858
a970015f
SA
859void AnalogSignal::on_conversion_changed(int index)
860{
06b6ce26
SA
861 data::SignalBase::ConversionType old_conv_type =
862 base_->get_conversion_type();
a8fd2759 863
06b6ce26
SA
864 data::SignalBase::ConversionType conv_type =
865 (data::SignalBase::ConversionType)(conversion_cb_->itemData(index).toInt());
a8fd2759 866
06b6ce26
SA
867 if (conv_type != old_conv_type) {
868 base_->set_conversion_type(conv_type);
869
870 if (owner_)
871 owner_->row_item_appearance_changed(false, true);
a8fd2759 872 }
a970015f
SA
873}
874
b8c4a95b
SA
875void AnalogSignal::on_display_type_changed(int index)
876{
877 display_type_ = (DisplayType)(display_type_cb_->itemData(index).toInt());
878
879 if (owner_)
880 owner_->row_item_appearance_changed(false, true);
881}
882
1573bf16 883} // namespace trace
f4e57597 884} // namespace views
aba1dd16 885} // namespace pv