]> sigrok.org Git - pulseview.git/blame - pv/view/analogsignal.cpp
AnalogSignal::paint_trace(): Factor out a const variable.
[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>
aba1dd16 26
37b9fed4 27#include <QApplication>
73e377fe 28#include <QCheckBox>
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"
bf0edd2b 39#include "pv/data/signalbase.hpp"
2acdb232 40#include "pv/view/view.hpp"
aba1dd16 41
fe3a1c21 42#include <libsigrokcxx/libsigrokcxx.hpp>
e8d00928 43
819f4c25 44using std::max;
a5d93c27 45using std::make_pair;
819f4c25 46using std::min;
f9abf97e 47using std::shared_ptr;
819f4c25 48using std::deque;
aba1dd16
JH
49
50namespace pv {
f4e57597
SA
51namespace views {
52namespace TraceView {
aba1dd16 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,
cbd2a2de 75 shared_ptr<data::SignalBase> base) :
73a25a6e 76 Signal(session, base),
834a4f1b 77 scale_index_(4), // 20 per div
37b9fed4
SA
78 scale_index_drag_offset_(0),
79 div_height_(3 * QFontMetrics(QApplication::font()).height()),
459db2c5
SA
80 pos_vdivs_(1),
81 neg_vdivs_(1),
73e377fe 82 resolution_(0),
1f1d55ce 83 autoranging_(true)
aba1dd16 84{
85715407
SA
85 pv::data::Analog* analog_data =
86 dynamic_cast<pv::data::Analog*>(data().get());
87
88 connect(analog_data, SIGNAL(samples_added(QObject*, uint64_t, uint64_t)),
89 this, SLOT(on_samples_added()));
90
73a25a6e 91 base_->set_colour(SignalColours[base_->index() % countof(SignalColours)]);
834a4f1b 92 update_scale();
aba1dd16
JH
93}
94
3009b5b3
JH
95shared_ptr<pv::data::SignalData> AnalogSignal::data() const
96{
cbd2a2de 97 return base_->analog_data();
9a0cd293
JH
98}
99
3a21afa6
SA
100void AnalogSignal::save_settings(QSettings &settings) const
101{
459db2c5
SA
102 settings.setValue("pos_vdivs", pos_vdivs_);
103 settings.setValue("neg_vdivs", neg_vdivs_);
3a21afa6 104 settings.setValue("scale_index", scale_index_);
73e377fe 105 settings.setValue("autoranging", autoranging_);
3a21afa6
SA
106}
107
108void AnalogSignal::restore_settings(QSettings &settings)
109{
459db2c5
SA
110 if (settings.contains("pos_vdivs"))
111 pos_vdivs_ = settings.value("pos_vdivs").toInt();
112
113 if (settings.contains("neg_vdivs"))
114 neg_vdivs_ = settings.value("neg_vdivs").toInt();
3a21afa6
SA
115
116 if (settings.contains("scale_index")) {
117 scale_index_ = settings.value("scale_index").toInt();
118 update_scale();
119 }
73e377fe
SA
120
121 if (settings.contains("autoranging"))
122 autoranging_ = settings.value("autoranging").toBool();
3a21afa6
SA
123}
124
a5d93c27
JH
125std::pair<int, int> AnalogSignal::v_extents() const
126{
459db2c5
SA
127 const int ph = pos_vdivs_ * div_height_;
128 const int nh = neg_vdivs_ * div_height_;
129 return make_pair(-ph, nh);
a5d93c27
JH
130}
131
214470fc
JH
132int AnalogSignal::scale_handle_offset() const
133{
459db2c5 134 const int h = (pos_vdivs_ + neg_vdivs_) * div_height_;
37b9fed4 135
8a2fafcc 136 return ((scale_index_drag_offset_ - scale_index_) *
37b9fed4 137 h / 4) - h / 2;
214470fc
JH
138}
139
140void AnalogSignal::scale_handle_dragged(int offset)
141{
459db2c5 142 const int h = (pos_vdivs_ + neg_vdivs_) * div_height_;
37b9fed4 143
8a2fafcc 144 scale_index_ = scale_index_drag_offset_ -
37b9fed4 145 (offset + h / 2) / (h / 4);
834a4f1b
SA
146
147 update_scale();
8a2fafcc
JH
148}
149
150void AnalogSignal::scale_handle_drag_release()
151{
152 scale_index_drag_offset_ = scale_index_;
834a4f1b 153 update_scale();
214470fc
JH
154}
155
5b5fa4da 156void AnalogSignal::paint_back(QPainter &p, const ViewItemPaintParams &pp)
fe08b6e8 157{
73a25a6e 158 if (base_->enabled()) {
99af6802 159 Trace::paint_back(p, pp);
97904bf7 160 paint_axis(p, pp, get_visual_y());
99af6802 161 }
fe08b6e8
JH
162}
163
5b5fa4da 164void AnalogSignal::paint_mid(QPainter &p, const ViewItemPaintParams &pp)
aba1dd16 165{
cbd2a2de 166 assert(base_->analog_data());
8dbbc7f0 167 assert(owner_);
a8acb46e 168
be9e7b4b 169 const int y = get_visual_y();
01fd3263 170
73a25a6e 171 if (!base_->enabled())
cec48d16
JH
172 return;
173
37b9fed4
SA
174 paint_grid(p, y, pp.left(), pp.right());
175
f3d66e52 176 const deque< shared_ptr<pv::data::AnalogSegment> > &segments =
cbd2a2de 177 base_->analog_data()->analog_segments();
f3d66e52 178 if (segments.empty())
a8acb46e
JH
179 return;
180
f3d66e52
JH
181 const shared_ptr<pv::data::AnalogSegment> &segment =
182 segments.front();
a8acb46e 183
4c8a6a6d 184 const double pixels_offset = pp.pixels_offset();
69e33a1b 185 const double samplerate = max(1.0, segment->samplerate());
60d9b99a 186 const pv::util::Timestamp& start_time = segment->start_time();
f3d66e52 187 const int64_t last_sample = segment->get_sample_count() - 1;
4c8a6a6d 188 const double samples_per_pixel = samplerate * pp.scale();
60d9b99a
JS
189 const pv::util::Timestamp start = samplerate * (pp.offset() - start_time);
190 const pv::util::Timestamp end = start + samples_per_pixel * pp.width();
a8acb46e 191
60d9b99a 192 const int64_t start_sample = min(max(floor(start).convert_to<int64_t>(),
a8acb46e 193 (int64_t)0), last_sample);
60d9b99a 194 const int64_t end_sample = min(max((ceil(end) + 1).convert_to<int64_t>(),
a8acb46e
JH
195 (int64_t)0), last_sample);
196
7c68ddae 197 if (samples_per_pixel < EnvelopeThreshold)
f3d66e52 198 paint_trace(p, segment, y, pp.left(),
7c68ddae
JH
199 start_sample, end_sample,
200 pixels_offset, samples_per_pixel);
201 else
f3d66e52 202 paint_envelope(p, segment, y, pp.left(),
7c68ddae
JH
203 start_sample, end_sample,
204 pixels_offset, samples_per_pixel);
205}
206
03cc651d
SA
207void AnalogSignal::paint_fore(QPainter &p, const ViewItemPaintParams &pp)
208{
209 if (!enabled())
210 return;
211
212 const int y = get_visual_y();
213
214 // Show the info section on the right side of the trace
215 const QString infotext = QString("%1 V/div").arg(resolution_);
216
73a25a6e 217 p.setPen(base_->colour());
03cc651d
SA
218 p.setFont(QApplication::font());
219
220 const QRectF bounding_rect = QRectF(pp.left(),
221 y + v_extents().first,
222 pp.width() - InfoTextMarginRight,
223 v_extents().second - v_extents().first - InfoTextMarginBottom);
224
225 p.drawText(bounding_rect, Qt::AlignRight | Qt::AlignBottom, infotext);
226}
227
37b9fed4
SA
228void AnalogSignal::paint_grid(QPainter &p, int y, int left, int right)
229{
46a0cadc
SA
230 p.setRenderHint(QPainter::Antialiasing, false);
231
459db2c5
SA
232 if (pos_vdivs_ > 0) {
233 p.setPen(QPen(GridMajorColor, 1, Qt::DashLine));
234 for (int i = 1; i <= pos_vdivs_; i++) {
235 const float dy = i * div_height_;
236 p.drawLine(QLineF(left, y - dy, right, y - dy));
237 }
238
239 p.setPen(QPen(GridMinorColor, 1, Qt::DashLine));
240 for (int i = 0; i < pos_vdivs_; i++) {
241 const float dy = i * div_height_;
242 const float dy25 = dy + (0.25 * div_height_);
243 const float dy50 = dy + (0.50 * div_height_);
244 const float dy75 = dy + (0.75 * div_height_);
245 p.drawLine(QLineF(left, y - dy25, right, y - dy25));
246 p.drawLine(QLineF(left, y - dy50, right, y - dy50));
247 p.drawLine(QLineF(left, y - dy75, right, y - dy75));
248 }
37b9fed4
SA
249 }
250
459db2c5
SA
251 if (neg_vdivs_ > 0) {
252 p.setPen(QPen(GridMajorColor, 1, Qt::DashLine));
253 for (int i = 1; i <= neg_vdivs_; i++) {
254 const float dy = i * div_height_;
255 p.drawLine(QLineF(left, y + dy, right, y + dy));
256 }
257
258 p.setPen(QPen(GridMinorColor, 1, Qt::DashLine));
259 for (int i = 0; i < neg_vdivs_; i++) {
260 const float dy = i * div_height_;
261 const float dy25 = dy + (0.25 * div_height_);
262 const float dy50 = dy + (0.50 * div_height_);
263 const float dy75 = dy + (0.75 * div_height_);
264 p.drawLine(QLineF(left, y + dy25, right, y + dy25));
265 p.drawLine(QLineF(left, y + dy50, right, y + dy50));
266 p.drawLine(QLineF(left, y + dy75, right, y + dy75));
267 }
37b9fed4 268 }
46a0cadc
SA
269
270 p.setRenderHint(QPainter::Antialiasing, true);
37b9fed4
SA
271}
272
7c68ddae 273void AnalogSignal::paint_trace(QPainter &p,
f3d66e52 274 const shared_ptr<pv::data::AnalogSegment> &segment,
7c68ddae
JH
275 int y, int left, const int64_t start, const int64_t end,
276 const double pixels_offset, const double samples_per_pixel)
277{
73a25a6e 278 p.setPen(base_->colour());
7c68ddae 279
76ce6c7a
UH
280 const int64_t points_count = end - start;
281
282 QPointF *points = new QPointF[points_count];
a8acb46e
JH
283 QPointF *point = points;
284
26a883ed
SA
285 pv::data::SegmentAnalogDataIterator* it =
286 segment->begin_sample_iteration(start);
287
7c68ddae 288 for (int64_t sample = start; sample != end; sample++) {
a8acb46e 289 const float x = (sample / samples_per_pixel -
2658961b 290 pixels_offset) + left;
26a883ed
SA
291
292 *point++ = QPointF(x, y - *((float*)it->value) * scale_);
293 segment->continue_sample_iteration(it, 1);
a8acb46e 294 }
26a883ed 295 segment->end_sample_iteration(it);
a8acb46e 296
76ce6c7a 297 p.drawPolyline(points, points_count);
a8acb46e
JH
298
299 delete[] points;
aba1dd16
JH
300}
301
7c68ddae 302void AnalogSignal::paint_envelope(QPainter &p,
f3d66e52 303 const shared_ptr<pv::data::AnalogSegment> &segment,
7c68ddae
JH
304 int y, int left, const int64_t start, const int64_t end,
305 const double pixels_offset, const double samples_per_pixel)
306{
f3d66e52 307 using pv::data::AnalogSegment;
7c68ddae 308
f3d66e52
JH
309 AnalogSegment::EnvelopeSection e;
310 segment->get_envelope_section(e, start, end, samples_per_pixel);
7c68ddae
JH
311
312 if (e.length < 2)
313 return;
314
819f4c25 315 p.setPen(QPen(Qt::NoPen));
73a25a6e 316 p.setBrush(base_->colour());
7c68ddae
JH
317
318 QRectF *const rects = new QRectF[e.length];
319 QRectF *rect = rects;
320
f3290553 321 for (uint64_t sample = 0; sample < e.length-1; sample++) {
7c68ddae
JH
322 const float x = ((e.scale * sample + e.start) /
323 samples_per_pixel - pixels_offset) + left;
f3d66e52 324 const AnalogSegment::EnvelopeSample *const s =
7c68ddae
JH
325 e.samples + sample;
326
327 // We overlap this sample with the next so that vertical
328 // gaps do not appear during steep rising or falling edges
834a4f1b
SA
329 const float b = y - max(s->max, (s+1)->min) * scale_;
330 const float t = y - min(s->min, (s+1)->max) * scale_;
7c68ddae
JH
331
332 float h = b - t;
f3290553 333 if (h >= 0.0f && h <= 1.0f)
7c68ddae 334 h = 1.0f;
f3290553 335 if (h <= 0.0f && h >= -1.0f)
7c68ddae
JH
336 h = -1.0f;
337
338 *rect++ = QRectF(x, t, 1.0f, h);
339 }
340
341 p.drawRects(rects, e.length);
342
343 delete[] rects;
344 delete[] e.samples;
345}
346
368a37c2 347float AnalogSignal::get_resolution(int scale_index)
8a2fafcc
JH
348{
349 const float seq[] = {1.0f, 2.0f, 5.0f};
834a4f1b 350
8a2fafcc
JH
351 const int offset = std::numeric_limits<int>::max() / (2 * countof(seq));
352 const std::div_t d = std::div(
368a37c2 353 (int)(scale_index + countof(seq) * offset),
8a2fafcc 354 countof(seq));
834a4f1b 355
368a37c2
SA
356 return powf(10.0f, d.quot - offset) * seq[d.rem];
357}
358
359void AnalogSignal::update_scale()
360{
361 resolution_ = get_resolution(scale_index_);
834a4f1b 362 scale_ = div_height_ / resolution_;
8a2fafcc
JH
363}
364
73e377fe
SA
365void AnalogSignal::perform_autoranging(bool force_update)
366{
367 const deque< shared_ptr<pv::data::AnalogSegment> > &segments =
368 base_->analog_data()->analog_segments();
369
370 if (segments.empty())
371 return;
372
373 static double prev_min = 0, prev_max = 0;
374 double min = 0, max = 0;
375
376 for (shared_ptr<pv::data::AnalogSegment> segment : segments) {
377 std::pair<double, double> mm = segment->get_min_max();
378 min = std::min(min, mm.first);
379 max = std::max(max, mm.second);
380 }
381
382 if ((min == prev_min) && (max == prev_max) && !force_update)
383 return;
384
385 prev_min = min;
386 prev_max = max;
387
388 // Use all divs for the positive range if there are no negative values
389 if ((min == 0) && (neg_vdivs_ > 0)) {
390 pos_vdivs_ += neg_vdivs_;
391 neg_vdivs_ = 0;
392 }
393
681b6d5a
SA
394 // Split up the divs if there are negative values but no negative divs
395 if ((min < 0) && (neg_vdivs_ == 0)) {
396 neg_vdivs_ = pos_vdivs_ / 2;
397 pos_vdivs_ -= neg_vdivs_;
398 }
399
73e377fe
SA
400 double min_value_per_div;
401 if ((pos_vdivs_ > 0) && (neg_vdivs_ > 0))
402 min_value_per_div = std::max(max / pos_vdivs_, -min / neg_vdivs_);
403 else if (pos_vdivs_ > 0)
404 min_value_per_div = max / pos_vdivs_;
405 else
406 min_value_per_div = -min / neg_vdivs_;
407
408 // Find first scale value that is bigger than the value we need
409 for (int i = MinScaleIndex; i < MaxScaleIndex; i++)
410 if (get_resolution(i) > min_value_per_div) {
411 scale_index_ = i;
412 break;
413 }
414
415 update_scale();
416}
417
4cffac16
SA
418void AnalogSignal::populate_popup_form(QWidget *parent, QFormLayout *form)
419{
420 // Add the standard options
421 Signal::populate_popup_form(parent, form);
422
368a37c2
SA
423 QFormLayout *const layout = new QFormLayout;
424
425 // Add the number of vdivs
459db2c5
SA
426 QSpinBox *pvdiv_sb = new QSpinBox(parent);
427 pvdiv_sb->setRange(0, MaximumVDivs);
428 pvdiv_sb->setValue(pos_vdivs_);
429 connect(pvdiv_sb, SIGNAL(valueChanged(int)),
430 this, SLOT(on_pos_vdivs_changed(int)));
431 layout->addRow(tr("Number of pos vertical divs"), pvdiv_sb);
432
433 QSpinBox *nvdiv_sb = new QSpinBox(parent);
434 nvdiv_sb->setRange(0, MaximumVDivs);
435 nvdiv_sb->setValue(neg_vdivs_);
436 connect(nvdiv_sb, SIGNAL(valueChanged(int)),
437 this, SLOT(on_neg_vdivs_changed(int)));
438 layout->addRow(tr("Number of neg vertical divs"), nvdiv_sb);
368a37c2
SA
439
440 // Add the vertical resolution
441 resolution_cb_ = new QComboBox(parent);
442
443 for (int i = MinScaleIndex; i < MaxScaleIndex; i++) {
444 const QString label = QString("%1").arg(get_resolution(i));
445 resolution_cb_->insertItem(0, label, QVariant(i));
446 }
447
448 const int cur_idx = resolution_cb_->findData(QVariant(scale_index_));
449 resolution_cb_->setCurrentIndex(cur_idx);
450
451 connect(resolution_cb_, SIGNAL(currentIndexChanged(int)),
452 this, SLOT(on_resolution_changed(int)));
453
454 QGridLayout *const vdiv_layout = new QGridLayout;
455 QLabel *const vdiv_unit = new QLabel(tr("V/div"));
456 vdiv_layout->addWidget(resolution_cb_, 0, 0);
457 vdiv_layout->addWidget(vdiv_unit, 0, 1);
458
459 layout->addRow(tr("Vertical resolution"), vdiv_layout);
460
73e377fe
SA
461 // Add the autoranging checkbox
462 QCheckBox* autoranging_cb = new QCheckBox();
463 autoranging_cb->setCheckState(autoranging_ ? Qt::Checked : Qt::Unchecked);
464
465 connect(autoranging_cb, SIGNAL(stateChanged(int)),
466 this, SLOT(on_autoranging_changed(int)));
467
468 layout->addRow(tr("Autoranging"), autoranging_cb);
469
368a37c2 470 form->addRow(layout);
4cffac16
SA
471}
472
85715407
SA
473void AnalogSignal::on_samples_added()
474{
475 perform_autoranging();
476
477 if (owner_) {
478 // Call order is important, otherwise the lazy event handler won't work
479 owner_->extents_changed(false, true);
480 owner_->row_item_appearance_changed(false, true);
481 }
482}
483
459db2c5
SA
484void AnalogSignal::on_pos_vdivs_changed(int vdivs)
485{
486 pos_vdivs_ = vdivs;
487
73e377fe
SA
488 if (autoranging_)
489 perform_autoranging(true);
490
459db2c5
SA
491 if (owner_) {
492 // Call order is important, otherwise the lazy event handler won't work
493 owner_->extents_changed(false, true);
494 owner_->row_item_appearance_changed(false, true);
495 }
496}
497
498void AnalogSignal::on_neg_vdivs_changed(int vdivs)
4cffac16 499{
459db2c5 500 neg_vdivs_ = vdivs;
4cffac16 501
73e377fe
SA
502 if (autoranging_)
503 perform_autoranging(true);
504
75d0779e
SA
505 if (owner_) {
506 // Call order is important, otherwise the lazy event handler won't work
4cffac16 507 owner_->extents_changed(false, true);
75d0779e
SA
508 owner_->row_item_appearance_changed(false, true);
509 }
4cffac16
SA
510}
511
368a37c2
SA
512void AnalogSignal::on_resolution_changed(int index)
513{
514 scale_index_ = resolution_cb_->itemData(index).toInt();
515 update_scale();
516
517 if (owner_)
518 owner_->row_item_appearance_changed(false, true);
519}
4cffac16 520
73e377fe
SA
521void AnalogSignal::on_autoranging_changed(int state)
522{
523 autoranging_ = (state == Qt::Checked);
524
525 if (autoranging_)
526 perform_autoranging(true);
527
85715407
SA
528 if (owner_) {
529 // Call order is important, otherwise the lazy event handler won't work
530 owner_->extents_changed(false, true);
73e377fe 531 owner_->row_item_appearance_changed(false, true);
85715407 532 }
73e377fe
SA
533}
534
f4e57597
SA
535} // namespace TraceView
536} // namespace views
aba1dd16 537} // namespace pv