]> sigrok.org Git - pulseview.git/blame - pv/view/analogsignal.cpp
Rename Trace::channel_ to Trace::base_, including dependencies
[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
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
3b68d03d
JH
23#include <cassert>
24#include <cmath>
8a2fafcc
JH
25#include <cstdlib>
26#include <limits>
aba1dd16 27
37b9fed4 28#include <QApplication>
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 {
51namespace view {
52
568b90d4
JH
53const 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
46a0cadc
SA
60const QColor AnalogSignal::GridMajorColor = QColor(0, 0, 0, 40*256/100);
61const QColor AnalogSignal::GridMinorColor = QColor(0, 0, 0, 20*256/100);
37b9fed4 62
7c68ddae
JH
63const float AnalogSignal::EnvelopeThreshold = 256.0f;
64
4cffac16 65const int AnalogSignal::MaximumVDivs = 10;
368a37c2
SA
66const int AnalogSignal::MinScaleIndex = -6;
67const int AnalogSignal::MaxScaleIndex = 7;
4cffac16 68
03cc651d
SA
69const int AnalogSignal::InfoTextMarginRight = 20;
70const int AnalogSignal::InfoTextMarginBottom = 5;
71
b86aa8f4 72AnalogSignal::AnalogSignal(
2b81ae46 73 pv::Session &session,
73a25a6e 74 shared_ptr<data::SignalBase> base,
e8d00928 75 shared_ptr<data::Analog> data) :
73a25a6e 76 Signal(session, base),
8dbbc7f0 77 data_(data),
834a4f1b 78 scale_index_(4), // 20 per div
37b9fed4
SA
79 scale_index_drag_offset_(0),
80 div_height_(3 * QFontMetrics(QApplication::font()).height()),
834a4f1b
SA
81 vdivs_(1),
82 resolution_(0)
aba1dd16 83{
73a25a6e 84 base_->set_colour(SignalColours[base_->index() % countof(SignalColours)]);
834a4f1b 85 update_scale();
aba1dd16
JH
86}
87
3009b5b3
JH
88shared_ptr<pv::data::SignalData> AnalogSignal::data() const
89{
8dbbc7f0 90 return data_;
3009b5b3
JH
91}
92
93shared_ptr<pv::data::Analog> AnalogSignal::analog_data() const
9a0cd293 94{
8dbbc7f0 95 return data_;
9a0cd293
JH
96}
97
a5d93c27
JH
98std::pair<int, int> AnalogSignal::v_extents() const
99{
37b9fed4 100 const int h = vdivs_ * div_height_;
8a2fafcc 101 return make_pair(-h, h);
a5d93c27
JH
102}
103
214470fc
JH
104int AnalogSignal::scale_handle_offset() const
105{
37b9fed4
SA
106 const int h = vdivs_ * div_height_;
107
8a2fafcc 108 return ((scale_index_drag_offset_ - scale_index_) *
37b9fed4 109 h / 4) - h / 2;
214470fc
JH
110}
111
112void AnalogSignal::scale_handle_dragged(int offset)
113{
37b9fed4
SA
114 const int h = vdivs_ * div_height_;
115
8a2fafcc 116 scale_index_ = scale_index_drag_offset_ -
37b9fed4 117 (offset + h / 2) / (h / 4);
834a4f1b
SA
118
119 update_scale();
8a2fafcc
JH
120}
121
122void AnalogSignal::scale_handle_drag_release()
123{
124 scale_index_drag_offset_ = scale_index_;
834a4f1b 125 update_scale();
214470fc
JH
126}
127
5b5fa4da 128void AnalogSignal::paint_back(QPainter &p, const ViewItemPaintParams &pp)
fe08b6e8 129{
73a25a6e 130 if (base_->enabled()) {
99af6802 131 Trace::paint_back(p, pp);
97904bf7 132 paint_axis(p, pp, get_visual_y());
99af6802 133 }
fe08b6e8
JH
134}
135
5b5fa4da 136void AnalogSignal::paint_mid(QPainter &p, const ViewItemPaintParams &pp)
aba1dd16 137{
8dbbc7f0 138 assert(data_);
8dbbc7f0 139 assert(owner_);
a8acb46e 140
be9e7b4b 141 const int y = get_visual_y();
01fd3263 142
73a25a6e 143 if (!base_->enabled())
cec48d16
JH
144 return;
145
37b9fed4
SA
146 paint_grid(p, y, pp.left(), pp.right());
147
f3d66e52
JH
148 const deque< shared_ptr<pv::data::AnalogSegment> > &segments =
149 data_->analog_segments();
150 if (segments.empty())
a8acb46e
JH
151 return;
152
f3d66e52
JH
153 const shared_ptr<pv::data::AnalogSegment> &segment =
154 segments.front();
a8acb46e 155
4c8a6a6d 156 const double pixels_offset = pp.pixels_offset();
69e33a1b 157 const double samplerate = max(1.0, segment->samplerate());
60d9b99a 158 const pv::util::Timestamp& start_time = segment->start_time();
f3d66e52 159 const int64_t last_sample = segment->get_sample_count() - 1;
4c8a6a6d 160 const double samples_per_pixel = samplerate * pp.scale();
60d9b99a
JS
161 const pv::util::Timestamp start = samplerate * (pp.offset() - start_time);
162 const pv::util::Timestamp end = start + samples_per_pixel * pp.width();
a8acb46e 163
60d9b99a 164 const int64_t start_sample = min(max(floor(start).convert_to<int64_t>(),
a8acb46e 165 (int64_t)0), last_sample);
60d9b99a 166 const int64_t end_sample = min(max((ceil(end) + 1).convert_to<int64_t>(),
a8acb46e
JH
167 (int64_t)0), last_sample);
168
7c68ddae 169 if (samples_per_pixel < EnvelopeThreshold)
f3d66e52 170 paint_trace(p, segment, y, pp.left(),
7c68ddae
JH
171 start_sample, end_sample,
172 pixels_offset, samples_per_pixel);
173 else
f3d66e52 174 paint_envelope(p, segment, y, pp.left(),
7c68ddae
JH
175 start_sample, end_sample,
176 pixels_offset, samples_per_pixel);
177}
178
03cc651d
SA
179void 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
73a25a6e 189 p.setPen(base_->colour());
03cc651d
SA
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
37b9fed4
SA
200void AnalogSignal::paint_grid(QPainter &p, int y, int left, int right)
201{
46a0cadc
SA
202 p.setRenderHint(QPainter::Antialiasing, false);
203
204 p.setPen(QPen(GridMajorColor, 1, Qt::DashLine));
37b9fed4 205 for (int i = 1; i <= vdivs_; i++) {
46a0cadc 206 const float dy = i * div_height_;
37b9fed4
SA
207 p.drawLine(QLineF(left, y - dy, right, y - dy));
208 p.drawLine(QLineF(left, y + dy, right, y + dy));
209 }
210
46a0cadc 211 p.setPen(QPen(GridMinorColor, 1, Qt::DashLine));
37b9fed4 212 for (int i = 0; i < vdivs_; i++) {
46a0cadc 213 const float dy = i * div_height_;
37b9fed4
SA
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 }
46a0cadc
SA
224
225 p.setRenderHint(QPainter::Antialiasing, true);
37b9fed4
SA
226}
227
7c68ddae 228void AnalogSignal::paint_trace(QPainter &p,
f3d66e52 229 const shared_ptr<pv::data::AnalogSegment> &segment,
7c68ddae
JH
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
f3d66e52 235 const float *const samples = segment->get_samples(start, end);
a8acb46e
JH
236 assert(samples);
237
73a25a6e 238 p.setPen(base_->colour());
7c68ddae 239
d3758367 240 QPointF *points = new QPointF[sample_count];
a8acb46e
JH
241 QPointF *point = points;
242
7c68ddae 243 for (int64_t sample = start; sample != end; sample++) {
a8acb46e 244 const float x = (sample / samples_per_pixel -
2658961b 245 pixels_offset) + left;
d3758367 246 *point++ = QPointF(x,
834a4f1b 247 y - samples[sample - start] * scale_);
a8acb46e
JH
248 }
249
306d43a7 250 p.drawPolyline(points, point - points);
a8acb46e 251
7c68ddae 252 delete[] samples;
a8acb46e 253 delete[] points;
aba1dd16
JH
254}
255
7c68ddae 256void AnalogSignal::paint_envelope(QPainter &p,
f3d66e52 257 const shared_ptr<pv::data::AnalogSegment> &segment,
7c68ddae
JH
258 int y, int left, const int64_t start, const int64_t end,
259 const double pixels_offset, const double samples_per_pixel)
260{
f3d66e52 261 using pv::data::AnalogSegment;
7c68ddae 262
f3d66e52
JH
263 AnalogSegment::EnvelopeSection e;
264 segment->get_envelope_section(e, start, end, samples_per_pixel);
7c68ddae
JH
265
266 if (e.length < 2)
267 return;
268
819f4c25 269 p.setPen(QPen(Qt::NoPen));
73a25a6e 270 p.setBrush(base_->colour());
7c68ddae
JH
271
272 QRectF *const rects = new QRectF[e.length];
273 QRectF *rect = rects;
274
f3290553 275 for (uint64_t sample = 0; sample < e.length-1; sample++) {
7c68ddae
JH
276 const float x = ((e.scale * sample + e.start) /
277 samples_per_pixel - pixels_offset) + left;
f3d66e52 278 const AnalogSegment::EnvelopeSample *const s =
7c68ddae
JH
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
834a4f1b
SA
283 const float b = y - max(s->max, (s+1)->min) * scale_;
284 const float t = y - min(s->min, (s+1)->max) * scale_;
7c68ddae
JH
285
286 float h = b - t;
f3290553 287 if (h >= 0.0f && h <= 1.0f)
7c68ddae 288 h = 1.0f;
f3290553 289 if (h <= 0.0f && h >= -1.0f)
7c68ddae
JH
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
368a37c2 301float AnalogSignal::get_resolution(int scale_index)
8a2fafcc
JH
302{
303 const float seq[] = {1.0f, 2.0f, 5.0f};
834a4f1b 304
8a2fafcc
JH
305 const int offset = std::numeric_limits<int>::max() / (2 * countof(seq));
306 const std::div_t d = std::div(
368a37c2 307 (int)(scale_index + countof(seq) * offset),
8a2fafcc 308 countof(seq));
834a4f1b 309
368a37c2
SA
310 return powf(10.0f, d.quot - offset) * seq[d.rem];
311}
312
313void AnalogSignal::update_scale()
314{
315 resolution_ = get_resolution(scale_index_);
834a4f1b 316 scale_ = div_height_ / resolution_;
8a2fafcc
JH
317}
318
4cffac16
SA
319void AnalogSignal::populate_popup_form(QWidget *parent, QFormLayout *form)
320{
321 // Add the standard options
322 Signal::populate_popup_form(parent, form);
323
368a37c2
SA
324 QFormLayout *const layout = new QFormLayout;
325
326 // Add the number of vdivs
4cffac16
SA
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)));
368a37c2
SA
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);
4cffac16
SA
356}
357
358void AnalogSignal::on_vdivs_changed(int vdivs)
359{
360 vdivs_ = vdivs;
361
75d0779e
SA
362 if (owner_) {
363 // Call order is important, otherwise the lazy event handler won't work
4cffac16 364 owner_->extents_changed(false, true);
75d0779e
SA
365 owner_->row_item_appearance_changed(false, true);
366 }
4cffac16
SA
367}
368
368a37c2
SA
369void 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}
4cffac16 377
aba1dd16
JH
378} // namespace view
379} // namespace pv