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