]> sigrok.org Git - pulseview.git/blame - pv/view/analogsignal.cpp
AnalogSignal: display type-related improvements
[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>
4cffac16 34#include <QSpinBox>
03cc651d 35#include <QString>
37b9fed4 36
2acdb232
JH
37#include "analogsignal.hpp"
38#include "pv/data/analog.hpp"
f3d66e52 39#include "pv/data/analogsegment.hpp"
a8fd2759
SA
40#include "pv/data/logic.hpp"
41#include "pv/data/logicsegment.hpp"
bf0edd2b 42#include "pv/data/signalbase.hpp"
2acdb232 43#include "pv/view/view.hpp"
a8fd2759 44#include "pv/view/logicsignal.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
SA
61namespace views {
62namespace TraceView {
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
46a0cadc
SA
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
7c68ddae
JH
76const float AnalogSignal::EnvelopeThreshold = 256.0f;
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
8a2fafcc 160 return ((scale_index_drag_offset_ - scale_index_) *
37b9fed4 161 h / 4) - h / 2;
214470fc
JH
162}
163
164void AnalogSignal::scale_handle_dragged(int offset)
165{
459db2c5 166 const int h = (pos_vdivs_ + neg_vdivs_) * div_height_;
37b9fed4 167
8a2fafcc 168 scale_index_ = scale_index_drag_offset_ -
37b9fed4 169 (offset + h / 2) / (h / 4);
834a4f1b
SA
170
171 update_scale();
8a2fafcc
JH
172}
173
174void AnalogSignal::scale_handle_drag_release()
175{
176 scale_index_drag_offset_ = scale_index_;
834a4f1b 177 update_scale();
214470fc
JH
178}
179
5b5fa4da 180void AnalogSignal::paint_back(QPainter &p, const ViewItemPaintParams &pp)
fe08b6e8 181{
73a25a6e 182 if (base_->enabled()) {
99af6802 183 Trace::paint_back(p, pp);
97904bf7 184 paint_axis(p, pp, get_visual_y());
99af6802 185 }
fe08b6e8
JH
186}
187
5b5fa4da 188void AnalogSignal::paint_mid(QPainter &p, const ViewItemPaintParams &pp)
aba1dd16 189{
cbd2a2de 190 assert(base_->analog_data());
8dbbc7f0 191 assert(owner_);
a8acb46e 192
be9e7b4b 193 const int y = get_visual_y();
01fd3263 194
73a25a6e 195 if (!base_->enabled())
cec48d16
JH
196 return;
197
a8fd2759
SA
198 if ((display_type_ == DisplayAnalog) || (display_type_ == DisplayBoth)) {
199 paint_grid(p, y, pp.left(), pp.right());
200
201 const deque< shared_ptr<pv::data::AnalogSegment> > &segments =
202 base_->analog_data()->analog_segments();
203 if (segments.empty())
204 return;
205
206 const shared_ptr<pv::data::AnalogSegment> &segment =
207 segments.front();
208
209 const double pixels_offset = pp.pixels_offset();
210 const double samplerate = max(1.0, segment->samplerate());
211 const pv::util::Timestamp& start_time = segment->start_time();
212 const int64_t last_sample = segment->get_sample_count() - 1;
213 const double samples_per_pixel = samplerate * pp.scale();
214 const pv::util::Timestamp start = samplerate * (pp.offset() - start_time);
215 const pv::util::Timestamp end = start + samples_per_pixel * pp.width();
216
217 const int64_t start_sample = min(max(floor(start).convert_to<int64_t>(),
218 (int64_t)0), last_sample);
219 const int64_t end_sample = min(max((ceil(end) + 1).convert_to<int64_t>(),
220 (int64_t)0), last_sample);
221
222 if (samples_per_pixel < EnvelopeThreshold)
223 paint_trace(p, segment, y, pp.left(),
224 start_sample, end_sample,
225 pixels_offset, samples_per_pixel);
226 else
227 paint_envelope(p, segment, y, pp.left(),
228 start_sample, end_sample,
229 pixels_offset, samples_per_pixel);
230 }
a8acb46e 231
a8fd2759
SA
232 if ((display_type_ == DisplayConverted) || (display_type_ == DisplayBoth)) {
233 if (((conversion_type_ == data::SignalBase::A2LConversionByTreshold) ||
234 (conversion_type_ == data::SignalBase::A2LConversionBySchmittTrigger))) {
a8acb46e 235
a8fd2759
SA
236 paint_logic_mid(p, pp);
237 }
238 }
7c68ddae
JH
239}
240
03cc651d
SA
241void AnalogSignal::paint_fore(QPainter &p, const ViewItemPaintParams &pp)
242{
243 if (!enabled())
244 return;
245
b8c4a95b
SA
246 if ((display_type_ == DisplayAnalog) || (display_type_ == DisplayBoth)) {
247 const int y = get_visual_y();
03cc651d 248
b8c4a95b
SA
249 // Show the info section on the right side of the trace
250 const QString infotext = QString("%1 V/div").arg(resolution_);
03cc651d 251
b8c4a95b
SA
252 p.setPen(base_->colour());
253 p.setFont(QApplication::font());
03cc651d 254
b8c4a95b
SA
255 const QRectF bounding_rect = QRectF(pp.left(),
256 y + v_extents().first,
257 pp.width() - InfoTextMarginRight,
258 v_extents().second - v_extents().first - InfoTextMarginBottom);
03cc651d 259
b8c4a95b
SA
260 p.drawText(bounding_rect, Qt::AlignRight | Qt::AlignBottom, infotext);
261 }
03cc651d
SA
262}
263
37b9fed4
SA
264void AnalogSignal::paint_grid(QPainter &p, int y, int left, int right)
265{
46a0cadc
SA
266 p.setRenderHint(QPainter::Antialiasing, false);
267
459db2c5
SA
268 if (pos_vdivs_ > 0) {
269 p.setPen(QPen(GridMajorColor, 1, Qt::DashLine));
270 for (int i = 1; i <= pos_vdivs_; i++) {
271 const float dy = i * div_height_;
272 p.drawLine(QLineF(left, y - dy, right, y - dy));
273 }
274
275 p.setPen(QPen(GridMinorColor, 1, Qt::DashLine));
276 for (int i = 0; i < pos_vdivs_; i++) {
277 const float dy = i * div_height_;
278 const float dy25 = dy + (0.25 * div_height_);
279 const float dy50 = dy + (0.50 * div_height_);
280 const float dy75 = dy + (0.75 * div_height_);
281 p.drawLine(QLineF(left, y - dy25, right, y - dy25));
282 p.drawLine(QLineF(left, y - dy50, right, y - dy50));
283 p.drawLine(QLineF(left, y - dy75, right, y - dy75));
284 }
37b9fed4
SA
285 }
286
459db2c5
SA
287 if (neg_vdivs_ > 0) {
288 p.setPen(QPen(GridMajorColor, 1, Qt::DashLine));
289 for (int i = 1; i <= neg_vdivs_; i++) {
290 const float dy = i * div_height_;
291 p.drawLine(QLineF(left, y + dy, right, y + dy));
292 }
293
294 p.setPen(QPen(GridMinorColor, 1, Qt::DashLine));
295 for (int i = 0; i < neg_vdivs_; i++) {
296 const float dy = i * div_height_;
297 const float dy25 = dy + (0.25 * div_height_);
298 const float dy50 = dy + (0.50 * div_height_);
299 const float dy75 = dy + (0.75 * div_height_);
300 p.drawLine(QLineF(left, y + dy25, right, y + dy25));
301 p.drawLine(QLineF(left, y + dy50, right, y + dy50));
302 p.drawLine(QLineF(left, y + dy75, right, y + dy75));
303 }
37b9fed4 304 }
46a0cadc
SA
305
306 p.setRenderHint(QPainter::Antialiasing, true);
37b9fed4
SA
307}
308
7c68ddae 309void AnalogSignal::paint_trace(QPainter &p,
f3d66e52 310 const shared_ptr<pv::data::AnalogSegment> &segment,
7c68ddae
JH
311 int y, int left, const int64_t start, const int64_t end,
312 const double pixels_offset, const double samples_per_pixel)
313{
73a25a6e 314 p.setPen(base_->colour());
7c68ddae 315
76ce6c7a
UH
316 const int64_t points_count = end - start;
317
318 QPointF *points = new QPointF[points_count];
a8acb46e
JH
319 QPointF *point = points;
320
8de1e1b2
UH
321 QRectF *const sampling_points = new QRectF[points_count];
322 QRectF *sampling_point = sampling_points;
323
26a883ed
SA
324 pv::data::SegmentAnalogDataIterator* it =
325 segment->begin_sample_iteration(start);
326
8de1e1b2 327 const int w = 2;
7c68ddae 328 for (int64_t sample = start; sample != end; sample++) {
a8acb46e 329 const float x = (sample / samples_per_pixel -
2658961b 330 pixels_offset) + left;
26a883ed
SA
331
332 *point++ = QPointF(x, y - *((float*)it->value) * scale_);
8de1e1b2
UH
333 *sampling_point++ = QRectF(x - (w / 2), y - *((float*)it->value) * scale_ - (w / 2), w, w);
334
26a883ed 335 segment->continue_sample_iteration(it, 1);
a8acb46e 336 }
26a883ed 337 segment->end_sample_iteration(it);
a8acb46e 338
76ce6c7a 339 p.drawPolyline(points, points_count);
a8acb46e 340
8de1e1b2
UH
341 // Paint the sampling points if enabled
342 GlobalSettings settings;
343 const bool show_sampling_points =
344 settings.value(GlobalSettings::Key_View_ShowSamplingPoints).toBool();
00f6bae9
UH
345
346 if (show_sampling_points && (samples_per_pixel < 0.25)) {
8de1e1b2
UH
347 p.setPen(SamplingPointColour);
348 p.drawRects(sampling_points, points_count);
349 }
350
a8acb46e 351 delete[] points;
8de1e1b2 352 delete[] sampling_points;
aba1dd16
JH
353}
354
7c68ddae 355void AnalogSignal::paint_envelope(QPainter &p,
f3d66e52 356 const shared_ptr<pv::data::AnalogSegment> &segment,
7c68ddae
JH
357 int y, int left, const int64_t start, const int64_t end,
358 const double pixels_offset, const double samples_per_pixel)
359{
f3d66e52 360 using pv::data::AnalogSegment;
7c68ddae 361
f3d66e52
JH
362 AnalogSegment::EnvelopeSection e;
363 segment->get_envelope_section(e, start, end, samples_per_pixel);
7c68ddae
JH
364
365 if (e.length < 2)
366 return;
367
819f4c25 368 p.setPen(QPen(Qt::NoPen));
73a25a6e 369 p.setBrush(base_->colour());
7c68ddae
JH
370
371 QRectF *const rects = new QRectF[e.length];
372 QRectF *rect = rects;
373
f3290553 374 for (uint64_t sample = 0; sample < e.length-1; sample++) {
7c68ddae
JH
375 const float x = ((e.scale * sample + e.start) /
376 samples_per_pixel - pixels_offset) + left;
f3d66e52 377 const AnalogSegment::EnvelopeSample *const s =
7c68ddae
JH
378 e.samples + sample;
379
380 // We overlap this sample with the next so that vertical
381 // gaps do not appear during steep rising or falling edges
834a4f1b
SA
382 const float b = y - max(s->max, (s+1)->min) * scale_;
383 const float t = y - min(s->min, (s+1)->max) * scale_;
7c68ddae
JH
384
385 float h = b - t;
f3290553 386 if (h >= 0.0f && h <= 1.0f)
7c68ddae 387 h = 1.0f;
f3290553 388 if (h <= 0.0f && h >= -1.0f)
7c68ddae
JH
389 h = -1.0f;
390
391 *rect++ = QRectF(x, t, 1.0f, h);
392 }
393
394 p.drawRects(rects, e.length);
395
396 delete[] rects;
397 delete[] e.samples;
398}
399
a8fd2759
SA
400void AnalogSignal::paint_logic_mid(QPainter &p, const ViewItemPaintParams &pp)
401{
402 QLineF *line;
403
404 vector< pair<int64_t, bool> > edges;
405
406 assert(base_);
407
408 const int y = get_visual_y();
409
410 if (!base_->enabled())
411 return;
412
413 const int signal_margin =
414 QFontMetrics(QApplication::font()).height() / 2;
415
416 const int ph = min(pos_vdivs_, 1) * div_height_;
417 const int nh = min(neg_vdivs_, 1) * div_height_;
418 const float high_offset = y - ph + signal_margin + 0.5f;
419 const float low_offset = y + nh - signal_margin - 0.5f;
420
421 const deque< shared_ptr<pv::data::LogicSegment> > &segments =
422 base_->logic_data()->logic_segments();
423 if (segments.empty())
424 return;
425
426 const shared_ptr<pv::data::LogicSegment> &segment =
427 segments.front();
428
429 double samplerate = segment->samplerate();
430
431 // Show sample rate as 1Hz when it is unknown
432 if (samplerate == 0.0)
433 samplerate = 1.0;
434
435 const double pixels_offset = pp.pixels_offset();
436 const pv::util::Timestamp& start_time = segment->start_time();
437 const int64_t last_sample = segment->get_sample_count() - 1;
438 const double samples_per_pixel = samplerate * pp.scale();
439 const pv::util::Timestamp start = samplerate * (pp.offset() - start_time);
440 const pv::util::Timestamp end = start + samples_per_pixel * pp.width();
441
442 const int64_t start_sample = min(max(floor(start).convert_to<int64_t>(),
443 (int64_t)0), last_sample);
444 const uint64_t end_sample = min(max(ceil(end).convert_to<int64_t>(),
445 (int64_t)0), last_sample);
446
447 segment->get_subsampled_edges(edges, start_sample, end_sample,
448 samples_per_pixel / LogicSignal::Oversampling, 0);
449 assert(edges.size() >= 2);
450
451 // Paint the edges
452 const unsigned int edge_count = edges.size() - 2;
453 QLineF *const edge_lines = new QLineF[edge_count];
454 line = edge_lines;
455
456 for (auto i = edges.cbegin() + 1; i != edges.cend() - 1; i++) {
457 const float x = ((*i).first / samples_per_pixel -
458 pixels_offset) + pp.left();
459 *line++ = QLineF(x, high_offset, x, low_offset);
460 }
461
462 p.setPen(LogicSignal::EdgeColour);
463 p.drawLines(edge_lines, edge_count);
464 delete[] edge_lines;
465
466 // Paint the caps
467 const unsigned int max_cap_line_count = edges.size();
468 QLineF *const cap_lines = new QLineF[max_cap_line_count];
469
470 p.setPen(LogicSignal::HighColour);
471 paint_logic_caps(p, cap_lines, edges, true, samples_per_pixel,
472 pixels_offset, pp.left(), high_offset);
473 p.setPen(LogicSignal::LowColour);
474 paint_logic_caps(p, cap_lines, edges, false, samples_per_pixel,
475 pixels_offset, pp.left(), low_offset);
476
477 delete[] cap_lines;
478
479 // Return if we don't need to paint the sampling points
480 GlobalSettings settings;
481 const bool show_sampling_points =
482 settings.value(GlobalSettings::Key_View_ShowSamplingPoints).toBool();
483
484 if (!show_sampling_points || (samples_per_pixel >= 0.25))
485 return;
486
487 // Paint the sampling points
488 const uint64_t sampling_points_count = end_sample - start_sample + 1;
489 QRectF *const sampling_points = new QRectF[sampling_points_count];
490 QRectF *sampling_point = sampling_points;
491
492 const int w = 1;
493 const float y_middle = high_offset - ((high_offset - low_offset) / 2);
494 for (uint64_t i = start_sample; i < end_sample + 1; ++i) {
495 const float x = (i / samples_per_pixel - pixels_offset) + pp.left();
496 *sampling_point++ = QRectF(x - (w / 2), y_middle - (w / 2), w, w);
497 }
498
499 p.setPen(SamplingPointColour);
500 p.drawRects(sampling_points, sampling_points_count);
501 delete[] sampling_points;
502}
503
504void AnalogSignal::paint_logic_caps(QPainter &p, QLineF *const lines,
505 vector< pair<int64_t, bool> > &edges, bool level,
506 double samples_per_pixel, double pixels_offset, float x_offset,
507 float y_offset)
508{
509 QLineF *line = lines;
510
511 for (auto i = edges.begin(); i != (edges.end() - 1); i++)
512 if ((*i).second == level) {
513 *line++ = QLineF(
514 ((*i).first / samples_per_pixel -
515 pixels_offset) + x_offset, y_offset,
516 ((*(i+1)).first / samples_per_pixel -
517 pixels_offset) + x_offset, y_offset);
518 }
519
520 p.drawLines(lines, line - lines);
521}
522
368a37c2 523float AnalogSignal::get_resolution(int scale_index)
8a2fafcc
JH
524{
525 const float seq[] = {1.0f, 2.0f, 5.0f};
834a4f1b 526
6f925ba9
UH
527 const int offset = numeric_limits<int>::max() / (2 * countof(seq));
528 const div_t d = div((int)(scale_index + countof(seq) * offset),
8a2fafcc 529 countof(seq));
834a4f1b 530
368a37c2
SA
531 return powf(10.0f, d.quot - offset) * seq[d.rem];
532}
533
534void AnalogSignal::update_scale()
535{
536 resolution_ = get_resolution(scale_index_);
834a4f1b 537 scale_ = div_height_ / resolution_;
8a2fafcc
JH
538}
539
a8fd2759
SA
540void AnalogSignal::update_conversion_type()
541{
542 base_->set_conversion_type(conversion_type_);
543
544 if (owner_)
545 owner_->row_item_appearance_changed(false, true);
546}
547
73e377fe
SA
548void AnalogSignal::perform_autoranging(bool force_update)
549{
550 const deque< shared_ptr<pv::data::AnalogSegment> > &segments =
551 base_->analog_data()->analog_segments();
552
553 if (segments.empty())
554 return;
555
556 static double prev_min = 0, prev_max = 0;
557 double min = 0, max = 0;
558
559 for (shared_ptr<pv::data::AnalogSegment> segment : segments) {
6f925ba9 560 pair<double, double> mm = segment->get_min_max();
73e377fe
SA
561 min = std::min(min, mm.first);
562 max = std::max(max, mm.second);
563 }
564
565 if ((min == prev_min) && (max == prev_max) && !force_update)
566 return;
567
568 prev_min = min;
569 prev_max = max;
570
571 // Use all divs for the positive range if there are no negative values
572 if ((min == 0) && (neg_vdivs_ > 0)) {
573 pos_vdivs_ += neg_vdivs_;
574 neg_vdivs_ = 0;
575 }
576
681b6d5a
SA
577 // Split up the divs if there are negative values but no negative divs
578 if ((min < 0) && (neg_vdivs_ == 0)) {
579 neg_vdivs_ = pos_vdivs_ / 2;
580 pos_vdivs_ -= neg_vdivs_;
581 }
582
73e377fe
SA
583 double min_value_per_div;
584 if ((pos_vdivs_ > 0) && (neg_vdivs_ > 0))
585 min_value_per_div = std::max(max / pos_vdivs_, -min / neg_vdivs_);
586 else if (pos_vdivs_ > 0)
587 min_value_per_div = max / pos_vdivs_;
588 else
589 min_value_per_div = -min / neg_vdivs_;
590
591 // Find first scale value that is bigger than the value we need
592 for (int i = MinScaleIndex; i < MaxScaleIndex; i++)
593 if (get_resolution(i) > min_value_per_div) {
594 scale_index_ = i;
595 break;
596 }
597
598 update_scale();
599}
600
4cffac16
SA
601void AnalogSignal::populate_popup_form(QWidget *parent, QFormLayout *form)
602{
603 // Add the standard options
604 Signal::populate_popup_form(parent, form);
605
368a37c2
SA
606 QFormLayout *const layout = new QFormLayout;
607
608 // Add the number of vdivs
459db2c5
SA
609 QSpinBox *pvdiv_sb = new QSpinBox(parent);
610 pvdiv_sb->setRange(0, MaximumVDivs);
611 pvdiv_sb->setValue(pos_vdivs_);
612 connect(pvdiv_sb, SIGNAL(valueChanged(int)),
613 this, SLOT(on_pos_vdivs_changed(int)));
614 layout->addRow(tr("Number of pos vertical divs"), pvdiv_sb);
615
616 QSpinBox *nvdiv_sb = new QSpinBox(parent);
617 nvdiv_sb->setRange(0, MaximumVDivs);
618 nvdiv_sb->setValue(neg_vdivs_);
619 connect(nvdiv_sb, SIGNAL(valueChanged(int)),
620 this, SLOT(on_neg_vdivs_changed(int)));
621 layout->addRow(tr("Number of neg vertical divs"), nvdiv_sb);
368a37c2
SA
622
623 // Add the vertical resolution
624 resolution_cb_ = new QComboBox(parent);
625
626 for (int i = MinScaleIndex; i < MaxScaleIndex; i++) {
627 const QString label = QString("%1").arg(get_resolution(i));
628 resolution_cb_->insertItem(0, label, QVariant(i));
629 }
630
a970015f 631 int cur_idx = resolution_cb_->findData(QVariant(scale_index_));
368a37c2
SA
632 resolution_cb_->setCurrentIndex(cur_idx);
633
634 connect(resolution_cb_, SIGNAL(currentIndexChanged(int)),
635 this, SLOT(on_resolution_changed(int)));
636
637 QGridLayout *const vdiv_layout = new QGridLayout;
638 QLabel *const vdiv_unit = new QLabel(tr("V/div"));
639 vdiv_layout->addWidget(resolution_cb_, 0, 0);
640 vdiv_layout->addWidget(vdiv_unit, 0, 1);
641
642 layout->addRow(tr("Vertical resolution"), vdiv_layout);
643
73e377fe
SA
644 // Add the autoranging checkbox
645 QCheckBox* autoranging_cb = new QCheckBox();
646 autoranging_cb->setCheckState(autoranging_ ? Qt::Checked : Qt::Unchecked);
647
648 connect(autoranging_cb, SIGNAL(stateChanged(int)),
649 this, SLOT(on_autoranging_changed(int)));
650
651 layout->addRow(tr("Autoranging"), autoranging_cb);
652
a970015f
SA
653 // Add the conversion type dropdown
654 conversion_cb_ = new QComboBox();
655
656 conversion_cb_->addItem("none", data::SignalBase::NoConversion);
657 conversion_cb_->addItem("to logic via threshold", data::SignalBase::A2LConversionByTreshold);
658 conversion_cb_->addItem("to logic via schmitt-trigger", data::SignalBase::A2LConversionBySchmittTrigger);
659
660 cur_idx = conversion_cb_->findData(QVariant(conversion_type_));
661 conversion_cb_->setCurrentIndex(cur_idx);
662
663 layout->addRow(tr("Conversion"), conversion_cb_);
664
665 connect(conversion_cb_, SIGNAL(currentIndexChanged(int)),
666 this, SLOT(on_conversion_changed(int)));
667
668 // Add the display type dropdown
669 display_type_cb_ = new QComboBox();
670
671 display_type_cb_->addItem(tr("Analog"), DisplayAnalog);
672 display_type_cb_->addItem(tr("Converted"), DisplayConverted);
673 display_type_cb_->addItem(tr("Both"), DisplayBoth);
674
675 cur_idx = display_type_cb_->findData(QVariant(display_type_));
676 display_type_cb_->setCurrentIndex(cur_idx);
677
678 layout->addRow(tr("Traces to show:"), display_type_cb_);
679
b8c4a95b
SA
680 connect(display_type_cb_, SIGNAL(currentIndexChanged(int)),
681 this, SLOT(on_display_type_changed(int)));
682
368a37c2 683 form->addRow(layout);
4cffac16
SA
684}
685
85715407
SA
686void AnalogSignal::on_samples_added()
687{
688 perform_autoranging();
689
690 if (owner_) {
691 // Call order is important, otherwise the lazy event handler won't work
692 owner_->extents_changed(false, true);
693 owner_->row_item_appearance_changed(false, true);
694 }
695}
696
459db2c5
SA
697void AnalogSignal::on_pos_vdivs_changed(int vdivs)
698{
699 pos_vdivs_ = vdivs;
700
73e377fe
SA
701 if (autoranging_)
702 perform_autoranging(true);
703
459db2c5
SA
704 if (owner_) {
705 // Call order is important, otherwise the lazy event handler won't work
706 owner_->extents_changed(false, true);
707 owner_->row_item_appearance_changed(false, true);
708 }
709}
710
711void AnalogSignal::on_neg_vdivs_changed(int vdivs)
4cffac16 712{
459db2c5 713 neg_vdivs_ = vdivs;
4cffac16 714
73e377fe
SA
715 if (autoranging_)
716 perform_autoranging(true);
717
75d0779e
SA
718 if (owner_) {
719 // Call order is important, otherwise the lazy event handler won't work
4cffac16 720 owner_->extents_changed(false, true);
75d0779e
SA
721 owner_->row_item_appearance_changed(false, true);
722 }
4cffac16
SA
723}
724
368a37c2
SA
725void AnalogSignal::on_resolution_changed(int index)
726{
727 scale_index_ = resolution_cb_->itemData(index).toInt();
728 update_scale();
729
730 if (owner_)
731 owner_->row_item_appearance_changed(false, true);
732}
4cffac16 733
73e377fe
SA
734void AnalogSignal::on_autoranging_changed(int state)
735{
736 autoranging_ = (state == Qt::Checked);
737
738 if (autoranging_)
739 perform_autoranging(true);
740
85715407
SA
741 if (owner_) {
742 // Call order is important, otherwise the lazy event handler won't work
743 owner_->extents_changed(false, true);
73e377fe 744 owner_->row_item_appearance_changed(false, true);
85715407 745 }
73e377fe
SA
746}
747
a970015f
SA
748void AnalogSignal::on_conversion_changed(int index)
749{
a8fd2759
SA
750 data::SignalBase::ConversionType old_conv_type = conversion_type_;
751
a970015f 752 conversion_type_ = (data::SignalBase::ConversionType)(conversion_cb_->itemData(index).toInt());
a8fd2759
SA
753
754 if (conversion_type_ != old_conv_type) {
755 base_->set_conversion_type(conversion_type_);
756 update_conversion_type();
757 }
a970015f
SA
758}
759
b8c4a95b
SA
760void AnalogSignal::on_display_type_changed(int index)
761{
762 display_type_ = (DisplayType)(display_type_cb_->itemData(index).toInt());
763
764 if (owner_)
765 owner_->row_item_appearance_changed(false, true);
766}
767
f4e57597
SA
768} // namespace TraceView
769} // namespace views
aba1dd16 770} // namespace pv