]> sigrok.org Git - pulseview.git/blob - pv/view/analogsignal.cpp
Introduce pv::data::SignalBase
[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, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
19  */
20
21 #include <extdef.h>
22
23 #include <cassert>
24 #include <cmath>
25 #include <cstdlib>
26 #include <limits>
27
28 #include <QApplication>
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 view {
52
53 const QColor AnalogSignal::SignalColours[4] = {
54         QColor(0xC4, 0xA0, 0x00),       // Yellow
55         QColor(0x87, 0x20, 0x7A),       // Magenta
56         QColor(0x20, 0x4A, 0x87),       // Blue
57         QColor(0x4E, 0x9A, 0x06)        // Green
58 };
59
60 const QColor AnalogSignal::GridMajorColor = QColor(0, 0, 0, 40*256/100);
61 const QColor AnalogSignal::GridMinorColor = QColor(0, 0, 0, 20*256/100);
62
63 const float AnalogSignal::EnvelopeThreshold = 256.0f;
64
65 const int AnalogSignal::MaximumVDivs = 10;
66 const int AnalogSignal::MinScaleIndex = -6;
67 const int AnalogSignal::MaxScaleIndex = 7;
68
69 const int AnalogSignal::InfoTextMarginRight = 20;
70 const int AnalogSignal::InfoTextMarginBottom = 5;
71
72 AnalogSignal::AnalogSignal(
73         pv::Session &session,
74         shared_ptr<data::SignalBase> channel,
75         shared_ptr<data::Analog> data) :
76         Signal(session, channel),
77         data_(data),
78         scale_index_(4), // 20 per div
79         scale_index_drag_offset_(0),
80         div_height_(3 * QFontMetrics(QApplication::font()).height()),
81         vdivs_(1),
82         resolution_(0)
83 {
84         channel_->set_colour(SignalColours[channel_->index() % countof(SignalColours)]);
85         update_scale();
86 }
87
88 shared_ptr<pv::data::SignalData> AnalogSignal::data() const
89 {
90         return data_;
91 }
92
93 shared_ptr<pv::data::Analog> AnalogSignal::analog_data() const
94 {
95         return data_;
96 }
97
98 std::pair<int, int> AnalogSignal::v_extents() const
99 {
100         const int h = vdivs_ * div_height_;
101         return make_pair(-h, h);
102 }
103
104 int AnalogSignal::scale_handle_offset() const
105 {
106         const int h = vdivs_ * div_height_;
107
108         return ((scale_index_drag_offset_ - scale_index_) *
109                 h / 4) - h / 2;
110 }
111
112 void AnalogSignal::scale_handle_dragged(int offset)
113 {
114         const int h = vdivs_ * div_height_;
115
116         scale_index_ = scale_index_drag_offset_ -
117                 (offset + h / 2) / (h / 4);
118
119         update_scale();
120 }
121
122 void AnalogSignal::scale_handle_drag_release()
123 {
124         scale_index_drag_offset_ = scale_index_;
125         update_scale();
126 }
127
128 void AnalogSignal::paint_back(QPainter &p, const ViewItemPaintParams &pp)
129 {
130         if (channel_->enabled()) {
131                 Trace::paint_back(p, pp);
132                 paint_axis(p, pp, get_visual_y());
133         }
134 }
135
136 void AnalogSignal::paint_mid(QPainter &p, const ViewItemPaintParams &pp)
137 {
138         assert(data_);
139         assert(owner_);
140
141         const int y = get_visual_y();
142
143         if (!channel_->enabled())
144                 return;
145
146         paint_grid(p, y, pp.left(), pp.right());
147
148         const deque< shared_ptr<pv::data::AnalogSegment> > &segments =
149                 data_->analog_segments();
150         if (segments.empty())
151                 return;
152
153         const shared_ptr<pv::data::AnalogSegment> &segment =
154                 segments.front();
155
156         const double pixels_offset = pp.pixels_offset();
157         const double samplerate = max(1.0, segment->samplerate());
158         const pv::util::Timestamp& start_time = segment->start_time();
159         const int64_t last_sample = segment->get_sample_count() - 1;
160         const double samples_per_pixel = samplerate * pp.scale();
161         const pv::util::Timestamp start = samplerate * (pp.offset() - start_time);
162         const pv::util::Timestamp end = start + samples_per_pixel * pp.width();
163
164         const int64_t start_sample = min(max(floor(start).convert_to<int64_t>(),
165                 (int64_t)0), last_sample);
166         const int64_t end_sample = min(max((ceil(end) + 1).convert_to<int64_t>(),
167                 (int64_t)0), last_sample);
168
169         if (samples_per_pixel < EnvelopeThreshold)
170                 paint_trace(p, segment, y, pp.left(),
171                         start_sample, end_sample,
172                         pixels_offset, samples_per_pixel);
173         else
174                 paint_envelope(p, segment, y, pp.left(),
175                         start_sample, end_sample,
176                         pixels_offset, samples_per_pixel);
177 }
178
179 void AnalogSignal::paint_fore(QPainter &p, const ViewItemPaintParams &pp)
180 {
181         if (!enabled())
182                 return;
183
184         const int y = get_visual_y();
185
186         // Show the info section on the right side of the trace
187         const QString infotext = QString("%1 V/div").arg(resolution_);
188
189         p.setPen(channel_->colour());
190         p.setFont(QApplication::font());
191
192         const QRectF bounding_rect = QRectF(pp.left(),
193                         y + v_extents().first,
194                         pp.width() - InfoTextMarginRight,
195                         v_extents().second - v_extents().first - InfoTextMarginBottom);
196
197         p.drawText(bounding_rect, Qt::AlignRight | Qt::AlignBottom, infotext);
198 }
199
200 void AnalogSignal::paint_grid(QPainter &p, int y, int left, int right)
201 {
202         p.setRenderHint(QPainter::Antialiasing, false);
203
204         p.setPen(QPen(GridMajorColor, 1, Qt::DashLine));
205         for (int i = 1; i <= vdivs_; i++) {
206                 const float dy = i * div_height_;
207                 p.drawLine(QLineF(left, y - dy, right, y - dy));
208                 p.drawLine(QLineF(left, y + dy, right, y + dy));
209         }
210
211         p.setPen(QPen(GridMinorColor, 1, Qt::DashLine));
212         for (int i = 0; i < vdivs_; i++) {
213                 const float dy = i * div_height_;
214                 const float dy25 = dy + (0.25 * div_height_);
215                 const float dy50 = dy + (0.50 * div_height_);
216                 const float dy75 = dy + (0.75 * div_height_);
217                 p.drawLine(QLineF(left, y - dy25, right, y - dy25));
218                 p.drawLine(QLineF(left, y + dy25, right, y + dy25));
219                 p.drawLine(QLineF(left, y - dy50, right, y - dy50));
220                 p.drawLine(QLineF(left, y + dy50, right, y + dy50));
221                 p.drawLine(QLineF(left, y - dy75, right, y - dy75));
222                 p.drawLine(QLineF(left, y + dy75, right, y + dy75));
223         }
224
225         p.setRenderHint(QPainter::Antialiasing, true);
226 }
227
228 void AnalogSignal::paint_trace(QPainter &p,
229         const shared_ptr<pv::data::AnalogSegment> &segment,
230         int y, int left, const int64_t start, const int64_t end,
231         const double pixels_offset, const double samples_per_pixel)
232 {
233         const int64_t sample_count = end - start;
234
235         const float *const samples = segment->get_samples(start, end);
236         assert(samples);
237
238         p.setPen(channel_->colour());
239
240         QPointF *points = new QPointF[sample_count];
241         QPointF *point = points;
242
243         for (int64_t sample = start; sample != end; sample++) {
244                 const float x = (sample / samples_per_pixel -
245                         pixels_offset) + left;
246                 *point++ = QPointF(x,
247                         y - samples[sample - start] * scale_);
248         }
249
250         p.drawPolyline(points, point - points);
251
252         delete[] samples;
253         delete[] points;
254 }
255
256 void AnalogSignal::paint_envelope(QPainter &p,
257         const shared_ptr<pv::data::AnalogSegment> &segment,
258         int y, int left, const int64_t start, const int64_t end,
259         const double pixels_offset, const double samples_per_pixel)
260 {
261         using pv::data::AnalogSegment;
262
263         AnalogSegment::EnvelopeSection e;
264         segment->get_envelope_section(e, start, end, samples_per_pixel);
265
266         if (e.length < 2)
267                 return;
268
269         p.setPen(QPen(Qt::NoPen));
270         p.setBrush(channel_->colour());
271
272         QRectF *const rects = new QRectF[e.length];
273         QRectF *rect = rects;
274
275         for (uint64_t sample = 0; sample < e.length-1; sample++) {
276                 const float x = ((e.scale * sample + e.start) /
277                         samples_per_pixel - pixels_offset) + left;
278                 const AnalogSegment::EnvelopeSample *const s =
279                         e.samples + sample;
280
281                 // We overlap this sample with the next so that vertical
282                 // gaps do not appear during steep rising or falling edges
283                 const float b = y - max(s->max, (s+1)->min) * scale_;
284                 const float t = y - min(s->min, (s+1)->max) * scale_;
285
286                 float h = b - t;
287                 if (h >= 0.0f && h <= 1.0f)
288                         h = 1.0f;
289                 if (h <= 0.0f && h >= -1.0f)
290                         h = -1.0f;
291
292                 *rect++ = QRectF(x, t, 1.0f, h);
293         }
294
295         p.drawRects(rects, e.length);
296
297         delete[] rects;
298         delete[] e.samples;
299 }
300
301 float AnalogSignal::get_resolution(int scale_index)
302 {
303         const float seq[] = {1.0f, 2.0f, 5.0f};
304
305         const int offset = std::numeric_limits<int>::max() / (2 * countof(seq));
306         const std::div_t d = std::div(
307                 (int)(scale_index + countof(seq) * offset),
308                 countof(seq));
309
310         return powf(10.0f, d.quot - offset) * seq[d.rem];
311 }
312
313 void AnalogSignal::update_scale()
314 {
315         resolution_ = get_resolution(scale_index_);
316         scale_ = div_height_ / resolution_;
317 }
318
319 void AnalogSignal::populate_popup_form(QWidget *parent, QFormLayout *form)
320 {
321         // Add the standard options
322         Signal::populate_popup_form(parent, form);
323
324         QFormLayout *const layout = new QFormLayout;
325
326         // Add the number of vdivs
327         QSpinBox *vdiv_sb = new QSpinBox(parent);
328         vdiv_sb->setRange(1, MaximumVDivs);
329         vdiv_sb->setValue(vdivs_);
330         connect(vdiv_sb, SIGNAL(valueChanged(int)),
331                 this, SLOT(on_vdivs_changed(int)));
332         layout->addRow(tr("Number of vertical divs"), vdiv_sb);
333
334         // Add the vertical resolution
335         resolution_cb_ = new QComboBox(parent);
336
337         for (int i = MinScaleIndex; i < MaxScaleIndex; i++) {
338                 const QString label = QString("%1").arg(get_resolution(i));
339                 resolution_cb_->insertItem(0, label, QVariant(i));
340         }
341
342         const int cur_idx = resolution_cb_->findData(QVariant(scale_index_));
343         resolution_cb_->setCurrentIndex(cur_idx);
344
345         connect(resolution_cb_, SIGNAL(currentIndexChanged(int)),
346                 this, SLOT(on_resolution_changed(int)));
347
348         QGridLayout *const vdiv_layout = new QGridLayout;
349         QLabel *const vdiv_unit = new QLabel(tr("V/div"));
350         vdiv_layout->addWidget(resolution_cb_, 0, 0);
351         vdiv_layout->addWidget(vdiv_unit, 0, 1);
352
353         layout->addRow(tr("Vertical resolution"), vdiv_layout);
354
355         form->addRow(layout);
356 }
357
358 void AnalogSignal::on_vdivs_changed(int vdivs)
359 {
360         vdivs_ = vdivs;
361
362         if (owner_) {
363                 // Call order is important, otherwise the lazy event handler won't work
364                 owner_->extents_changed(false, true);
365                 owner_->row_item_appearance_changed(false, true);
366         }
367 }
368
369 void AnalogSignal::on_resolution_changed(int index)
370 {
371         scale_index_ = resolution_cb_->itemData(index).toInt();
372         update_scale();
373
374         if (owner_)
375                 owner_->row_item_appearance_changed(false, true);
376 }
377
378 } // namespace view
379 } // namespace pv