]> sigrok.org Git - pulseview.git/blob - pv/view/analogsignal.cpp
Tie the "use coloured bg" setting in with the settings mgmt
[pulseview.git] / pv / view / analogsignal.cpp
1 /*
2  * This file is part of the PulseView project.
3  *
4  * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, see <http://www.gnu.org/licenses/>.
18  */
19
20 #include <extdef.h>
21
22 #include <cassert>
23 #include <cmath>
24 #include <cstdlib>
25 #include <limits>
26
27 #include <QApplication>
28 #include <QCheckBox>
29 #include <QComboBox>
30 #include <QFormLayout>
31 #include <QGridLayout>
32 #include <QLabel>
33 #include <QSpinBox>
34 #include <QString>
35
36 #include "analogsignal.hpp"
37 #include "pv/data/analog.hpp"
38 #include "pv/data/analogsegment.hpp"
39 #include "pv/data/signalbase.hpp"
40 #include "pv/view/view.hpp"
41
42 #include <libsigrokcxx/libsigrokcxx.hpp>
43
44 using std::max;
45 using std::make_pair;
46 using std::min;
47 using std::shared_ptr;
48 using std::deque;
49
50 namespace pv {
51 namespace views {
52 namespace TraceView {
53
54 const 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
61 const QColor AnalogSignal::GridMajorColor = QColor(0, 0, 0, 40*256/100);
62 const QColor AnalogSignal::GridMinorColor = QColor(0, 0, 0, 20*256/100);
63
64 const float AnalogSignal::EnvelopeThreshold = 256.0f;
65
66 const int AnalogSignal::MaximumVDivs = 10;
67 const int AnalogSignal::MinScaleIndex = -6;
68 const int AnalogSignal::MaxScaleIndex = 7;
69
70 const int AnalogSignal::InfoTextMarginRight = 20;
71 const int AnalogSignal::InfoTextMarginBottom = 5;
72
73 AnalogSignal::AnalogSignal(
74         pv::Session &session,
75         shared_ptr<data::SignalBase> base) :
76         Signal(session, base),
77         scale_index_(4), // 20 per div
78         scale_index_drag_offset_(0),
79         div_height_(3 * QFontMetrics(QApplication::font()).height()),
80         pos_vdivs_(1),
81         neg_vdivs_(1),
82         resolution_(0),
83         autoranging_(1)
84 {
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
91         base_->set_colour(SignalColours[base_->index() % countof(SignalColours)]);
92         update_scale();
93 }
94
95 shared_ptr<pv::data::SignalData> AnalogSignal::data() const
96 {
97         return base_->analog_data();
98 }
99
100 void AnalogSignal::save_settings(QSettings &settings) const
101 {
102         settings.setValue("pos_vdivs", pos_vdivs_);
103         settings.setValue("neg_vdivs", neg_vdivs_);
104         settings.setValue("scale_index", scale_index_);
105         settings.setValue("autoranging", autoranging_);
106 }
107
108 void AnalogSignal::restore_settings(QSettings &settings)
109 {
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();
115
116         if (settings.contains("scale_index")) {
117                 scale_index_ = settings.value("scale_index").toInt();
118                 update_scale();
119         }
120
121         if (settings.contains("autoranging"))
122                 autoranging_ = settings.value("autoranging").toBool();
123 }
124
125 std::pair<int, int> AnalogSignal::v_extents() const
126 {
127         const int ph = pos_vdivs_ * div_height_;
128         const int nh = neg_vdivs_ * div_height_;
129         return make_pair(-ph, nh);
130 }
131
132 int AnalogSignal::scale_handle_offset() const
133 {
134         const int h = (pos_vdivs_ + neg_vdivs_) * div_height_;
135
136         return ((scale_index_drag_offset_ - scale_index_) *
137                 h / 4) - h / 2;
138 }
139
140 void AnalogSignal::scale_handle_dragged(int offset)
141 {
142         const int h = (pos_vdivs_ + neg_vdivs_) * div_height_;
143
144         scale_index_ = scale_index_drag_offset_ -
145                 (offset + h / 2) / (h / 4);
146
147         update_scale();
148 }
149
150 void AnalogSignal::scale_handle_drag_release()
151 {
152         scale_index_drag_offset_ = scale_index_;
153         update_scale();
154 }
155
156 void AnalogSignal::paint_back(QPainter &p, const ViewItemPaintParams &pp)
157 {
158         if (base_->enabled()) {
159                 Trace::paint_back(p, pp);
160                 paint_axis(p, pp, get_visual_y());
161         }
162 }
163
164 void AnalogSignal::paint_mid(QPainter &p, const ViewItemPaintParams &pp)
165 {
166         assert(base_->analog_data());
167         assert(owner_);
168
169         const int y = get_visual_y();
170
171         if (!base_->enabled())
172                 return;
173
174         paint_grid(p, y, pp.left(), pp.right());
175
176         const deque< shared_ptr<pv::data::AnalogSegment> > &segments =
177                 base_->analog_data()->analog_segments();
178         if (segments.empty())
179                 return;
180
181         const shared_ptr<pv::data::AnalogSegment> &segment =
182                 segments.front();
183
184         const double pixels_offset = pp.pixels_offset();
185         const double samplerate = max(1.0, segment->samplerate());
186         const pv::util::Timestamp& start_time = segment->start_time();
187         const int64_t last_sample = segment->get_sample_count() - 1;
188         const double samples_per_pixel = samplerate * pp.scale();
189         const pv::util::Timestamp start = samplerate * (pp.offset() - start_time);
190         const pv::util::Timestamp end = start + samples_per_pixel * pp.width();
191
192         const int64_t start_sample = min(max(floor(start).convert_to<int64_t>(),
193                 (int64_t)0), last_sample);
194         const int64_t end_sample = min(max((ceil(end) + 1).convert_to<int64_t>(),
195                 (int64_t)0), last_sample);
196
197         if (samples_per_pixel < EnvelopeThreshold)
198                 paint_trace(p, segment, y, pp.left(),
199                         start_sample, end_sample,
200                         pixels_offset, samples_per_pixel);
201         else
202                 paint_envelope(p, segment, y, pp.left(),
203                         start_sample, end_sample,
204                         pixels_offset, samples_per_pixel);
205 }
206
207 void 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
217         p.setPen(base_->colour());
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
228 void AnalogSignal::paint_grid(QPainter &p, int y, int left, int right)
229 {
230         p.setRenderHint(QPainter::Antialiasing, false);
231
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                 }
249         }
250
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                 }
268         }
269
270         p.setRenderHint(QPainter::Antialiasing, true);
271 }
272
273 void AnalogSignal::paint_trace(QPainter &p,
274         const shared_ptr<pv::data::AnalogSegment> &segment,
275         int y, int left, const int64_t start, const int64_t end,
276         const double pixels_offset, const double samples_per_pixel)
277 {
278         p.setPen(base_->colour());
279
280         QPointF *points = new QPointF[end - start];
281         QPointF *point = points;
282
283         pv::data::SegmentAnalogDataIterator* it =
284                 segment->begin_sample_iteration(start);
285
286         for (int64_t sample = start; sample != end; sample++) {
287                 const float x = (sample / samples_per_pixel -
288                         pixels_offset) + left;
289
290                 *point++ = QPointF(x, y - *((float*)it->value) * scale_);
291                 segment->continue_sample_iteration(it, 1);
292         }
293         segment->end_sample_iteration(it);
294
295         p.drawPolyline(points, point - points);
296
297         delete[] points;
298 }
299
300 void AnalogSignal::paint_envelope(QPainter &p,
301         const shared_ptr<pv::data::AnalogSegment> &segment,
302         int y, int left, const int64_t start, const int64_t end,
303         const double pixels_offset, const double samples_per_pixel)
304 {
305         using pv::data::AnalogSegment;
306
307         AnalogSegment::EnvelopeSection e;
308         segment->get_envelope_section(e, start, end, samples_per_pixel);
309
310         if (e.length < 2)
311                 return;
312
313         p.setPen(QPen(Qt::NoPen));
314         p.setBrush(base_->colour());
315
316         QRectF *const rects = new QRectF[e.length];
317         QRectF *rect = rects;
318
319         for (uint64_t sample = 0; sample < e.length-1; sample++) {
320                 const float x = ((e.scale * sample + e.start) /
321                         samples_per_pixel - pixels_offset) + left;
322                 const AnalogSegment::EnvelopeSample *const s =
323                         e.samples + sample;
324
325                 // We overlap this sample with the next so that vertical
326                 // gaps do not appear during steep rising or falling edges
327                 const float b = y - max(s->max, (s+1)->min) * scale_;
328                 const float t = y - min(s->min, (s+1)->max) * scale_;
329
330                 float h = b - t;
331                 if (h >= 0.0f && h <= 1.0f)
332                         h = 1.0f;
333                 if (h <= 0.0f && h >= -1.0f)
334                         h = -1.0f;
335
336                 *rect++ = QRectF(x, t, 1.0f, h);
337         }
338
339         p.drawRects(rects, e.length);
340
341         delete[] rects;
342         delete[] e.samples;
343 }
344
345 float AnalogSignal::get_resolution(int scale_index)
346 {
347         const float seq[] = {1.0f, 2.0f, 5.0f};
348
349         const int offset = std::numeric_limits<int>::max() / (2 * countof(seq));
350         const std::div_t d = std::div(
351                 (int)(scale_index + countof(seq) * offset),
352                 countof(seq));
353
354         return powf(10.0f, d.quot - offset) * seq[d.rem];
355 }
356
357 void AnalogSignal::update_scale()
358 {
359         resolution_ = get_resolution(scale_index_);
360         scale_ = div_height_ / resolution_;
361 }
362
363 void AnalogSignal::perform_autoranging(bool force_update)
364 {
365         const deque< shared_ptr<pv::data::AnalogSegment> > &segments =
366                 base_->analog_data()->analog_segments();
367
368         if (segments.empty())
369                 return;
370
371         static double prev_min = 0, prev_max = 0;
372         double min = 0, max = 0;
373
374         for (shared_ptr<pv::data::AnalogSegment> segment : segments) {
375                 std::pair<double, double> mm = segment->get_min_max();
376                 min = std::min(min, mm.first);
377                 max = std::max(max, mm.second);
378         }
379
380         if ((min == prev_min) && (max == prev_max) && !force_update)
381                 return;
382
383         prev_min = min;
384         prev_max = max;
385
386         // Use all divs for the positive range if there are no negative values
387         if ((min == 0) && (neg_vdivs_ > 0)) {
388                 pos_vdivs_ += neg_vdivs_;
389                 neg_vdivs_ = 0;
390         }
391
392         // Split up the divs if there are negative values but no negative divs
393         if ((min < 0) && (neg_vdivs_ == 0)) {
394                 neg_vdivs_ = pos_vdivs_ / 2;
395                 pos_vdivs_ -= neg_vdivs_;
396         }
397
398         double min_value_per_div;
399         if ((pos_vdivs_ > 0) && (neg_vdivs_ >  0))
400                 min_value_per_div = std::max(max / pos_vdivs_, -min / neg_vdivs_);
401         else if (pos_vdivs_ > 0)
402                 min_value_per_div = max / pos_vdivs_;
403         else
404                 min_value_per_div = -min / neg_vdivs_;
405
406         // Find first scale value that is bigger than the value we need
407         for (int i = MinScaleIndex; i < MaxScaleIndex; i++)
408                 if (get_resolution(i) > min_value_per_div) {
409                         scale_index_ = i;
410                         break;
411                 }
412
413         update_scale();
414 }
415
416 void AnalogSignal::populate_popup_form(QWidget *parent, QFormLayout *form)
417 {
418         // Add the standard options
419         Signal::populate_popup_form(parent, form);
420
421         QFormLayout *const layout = new QFormLayout;
422
423         // Add the number of vdivs
424         QSpinBox *pvdiv_sb = new QSpinBox(parent);
425         pvdiv_sb->setRange(0, MaximumVDivs);
426         pvdiv_sb->setValue(pos_vdivs_);
427         connect(pvdiv_sb, SIGNAL(valueChanged(int)),
428                 this, SLOT(on_pos_vdivs_changed(int)));
429         layout->addRow(tr("Number of pos vertical divs"), pvdiv_sb);
430
431         QSpinBox *nvdiv_sb = new QSpinBox(parent);
432         nvdiv_sb->setRange(0, MaximumVDivs);
433         nvdiv_sb->setValue(neg_vdivs_);
434         connect(nvdiv_sb, SIGNAL(valueChanged(int)),
435                 this, SLOT(on_neg_vdivs_changed(int)));
436         layout->addRow(tr("Number of neg vertical divs"), nvdiv_sb);
437
438         // Add the vertical resolution
439         resolution_cb_ = new QComboBox(parent);
440
441         for (int i = MinScaleIndex; i < MaxScaleIndex; i++) {
442                 const QString label = QString("%1").arg(get_resolution(i));
443                 resolution_cb_->insertItem(0, label, QVariant(i));
444         }
445
446         const int cur_idx = resolution_cb_->findData(QVariant(scale_index_));
447         resolution_cb_->setCurrentIndex(cur_idx);
448
449         connect(resolution_cb_, SIGNAL(currentIndexChanged(int)),
450                 this, SLOT(on_resolution_changed(int)));
451
452         QGridLayout *const vdiv_layout = new QGridLayout;
453         QLabel *const vdiv_unit = new QLabel(tr("V/div"));
454         vdiv_layout->addWidget(resolution_cb_, 0, 0);
455         vdiv_layout->addWidget(vdiv_unit, 0, 1);
456
457         layout->addRow(tr("Vertical resolution"), vdiv_layout);
458
459         // Add the autoranging checkbox
460         QCheckBox* autoranging_cb = new QCheckBox();
461         autoranging_cb->setCheckState(autoranging_ ? Qt::Checked : Qt::Unchecked);
462
463         connect(autoranging_cb, SIGNAL(stateChanged(int)),
464                 this, SLOT(on_autoranging_changed(int)));
465
466         layout->addRow(tr("Autoranging"), autoranging_cb);
467
468         form->addRow(layout);
469 }
470
471 void AnalogSignal::on_samples_added()
472 {
473         perform_autoranging();
474
475         if (owner_) {
476                 // Call order is important, otherwise the lazy event handler won't work
477                 owner_->extents_changed(false, true);
478                 owner_->row_item_appearance_changed(false, true);
479         }
480 }
481
482 void AnalogSignal::on_pos_vdivs_changed(int vdivs)
483 {
484         pos_vdivs_ = vdivs;
485
486         if (autoranging_)
487                 perform_autoranging(true);
488
489         if (owner_) {
490                 // Call order is important, otherwise the lazy event handler won't work
491                 owner_->extents_changed(false, true);
492                 owner_->row_item_appearance_changed(false, true);
493         }
494 }
495
496 void AnalogSignal::on_neg_vdivs_changed(int vdivs)
497 {
498         neg_vdivs_ = vdivs;
499
500         if (autoranging_)
501                 perform_autoranging(true);
502
503         if (owner_) {
504                 // Call order is important, otherwise the lazy event handler won't work
505                 owner_->extents_changed(false, true);
506                 owner_->row_item_appearance_changed(false, true);
507         }
508 }
509
510 void AnalogSignal::on_resolution_changed(int index)
511 {
512         scale_index_ = resolution_cb_->itemData(index).toInt();
513         update_scale();
514
515         if (owner_)
516                 owner_->row_item_appearance_changed(false, true);
517 }
518
519 void AnalogSignal::on_autoranging_changed(int state)
520 {
521         autoranging_ = (state == Qt::Checked);
522
523         if (autoranging_)
524                 perform_autoranging(true);
525
526         if (owner_) {
527                 // Call order is important, otherwise the lazy event handler won't work
528                 owner_->extents_changed(false, true);
529                 owner_->row_item_appearance_changed(false, true);
530         }
531 }
532
533 } // namespace TraceView
534 } // namespace views
535 } // namespace pv