]> sigrok.org Git - pulseview.git/blame - pv/view/analogsignal.cpp
MainWindow: Use regular pointer for QDockWidgets
[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 {
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()),
834a4f1b
SA
80 vdivs_(1),
81 resolution_(0)
aba1dd16 82{
73a25a6e 83 base_->set_colour(SignalColours[base_->index() % countof(SignalColours)]);
834a4f1b 84 update_scale();
aba1dd16
JH
85}
86
3009b5b3
JH
87shared_ptr<pv::data::SignalData> AnalogSignal::data() const
88{
cbd2a2de 89 return base_->analog_data();
9a0cd293
JH
90}
91
3a21afa6
SA
92void AnalogSignal::save_settings(QSettings &settings) const
93{
94 settings.setValue("vdivs", vdivs_);
95 settings.setValue("scale_index", scale_index_);
96}
97
98void AnalogSignal::restore_settings(QSettings &settings)
99{
100 if (settings.contains("vdivs"))
101 vdivs_ = settings.value("vdivs").toInt();
102
103 if (settings.contains("scale_index")) {
104 scale_index_ = settings.value("scale_index").toInt();
105 update_scale();
106 }
107}
108
a5d93c27
JH
109std::pair<int, int> AnalogSignal::v_extents() const
110{
37b9fed4 111 const int h = vdivs_ * div_height_;
8a2fafcc 112 return make_pair(-h, h);
a5d93c27
JH
113}
114
214470fc
JH
115int AnalogSignal::scale_handle_offset() const
116{
37b9fed4
SA
117 const int h = vdivs_ * div_height_;
118
8a2fafcc 119 return ((scale_index_drag_offset_ - scale_index_) *
37b9fed4 120 h / 4) - h / 2;
214470fc
JH
121}
122
123void AnalogSignal::scale_handle_dragged(int offset)
124{
37b9fed4
SA
125 const int h = vdivs_ * div_height_;
126
8a2fafcc 127 scale_index_ = scale_index_drag_offset_ -
37b9fed4 128 (offset + h / 2) / (h / 4);
834a4f1b
SA
129
130 update_scale();
8a2fafcc
JH
131}
132
133void AnalogSignal::scale_handle_drag_release()
134{
135 scale_index_drag_offset_ = scale_index_;
834a4f1b 136 update_scale();
214470fc
JH
137}
138
5b5fa4da 139void AnalogSignal::paint_back(QPainter &p, const ViewItemPaintParams &pp)
fe08b6e8 140{
73a25a6e 141 if (base_->enabled()) {
99af6802 142 Trace::paint_back(p, pp);
97904bf7 143 paint_axis(p, pp, get_visual_y());
99af6802 144 }
fe08b6e8
JH
145}
146
5b5fa4da 147void AnalogSignal::paint_mid(QPainter &p, const ViewItemPaintParams &pp)
aba1dd16 148{
cbd2a2de 149 assert(base_->analog_data());
8dbbc7f0 150 assert(owner_);
a8acb46e 151
be9e7b4b 152 const int y = get_visual_y();
01fd3263 153
73a25a6e 154 if (!base_->enabled())
cec48d16
JH
155 return;
156
37b9fed4
SA
157 paint_grid(p, y, pp.left(), pp.right());
158
f3d66e52 159 const deque< shared_ptr<pv::data::AnalogSegment> > &segments =
cbd2a2de 160 base_->analog_data()->analog_segments();
f3d66e52 161 if (segments.empty())
a8acb46e
JH
162 return;
163
f3d66e52
JH
164 const shared_ptr<pv::data::AnalogSegment> &segment =
165 segments.front();
a8acb46e 166
4c8a6a6d 167 const double pixels_offset = pp.pixels_offset();
69e33a1b 168 const double samplerate = max(1.0, segment->samplerate());
60d9b99a 169 const pv::util::Timestamp& start_time = segment->start_time();
f3d66e52 170 const int64_t last_sample = segment->get_sample_count() - 1;
4c8a6a6d 171 const double samples_per_pixel = samplerate * pp.scale();
60d9b99a
JS
172 const pv::util::Timestamp start = samplerate * (pp.offset() - start_time);
173 const pv::util::Timestamp end = start + samples_per_pixel * pp.width();
a8acb46e 174
60d9b99a 175 const int64_t start_sample = min(max(floor(start).convert_to<int64_t>(),
a8acb46e 176 (int64_t)0), last_sample);
60d9b99a 177 const int64_t end_sample = min(max((ceil(end) + 1).convert_to<int64_t>(),
a8acb46e
JH
178 (int64_t)0), last_sample);
179
7c68ddae 180 if (samples_per_pixel < EnvelopeThreshold)
f3d66e52 181 paint_trace(p, segment, y, pp.left(),
7c68ddae
JH
182 start_sample, end_sample,
183 pixels_offset, samples_per_pixel);
184 else
f3d66e52 185 paint_envelope(p, segment, y, pp.left(),
7c68ddae
JH
186 start_sample, end_sample,
187 pixels_offset, samples_per_pixel);
188}
189
03cc651d
SA
190void AnalogSignal::paint_fore(QPainter &p, const ViewItemPaintParams &pp)
191{
192 if (!enabled())
193 return;
194
195 const int y = get_visual_y();
196
197 // Show the info section on the right side of the trace
198 const QString infotext = QString("%1 V/div").arg(resolution_);
199
73a25a6e 200 p.setPen(base_->colour());
03cc651d
SA
201 p.setFont(QApplication::font());
202
203 const QRectF bounding_rect = QRectF(pp.left(),
204 y + v_extents().first,
205 pp.width() - InfoTextMarginRight,
206 v_extents().second - v_extents().first - InfoTextMarginBottom);
207
208 p.drawText(bounding_rect, Qt::AlignRight | Qt::AlignBottom, infotext);
209}
210
37b9fed4
SA
211void AnalogSignal::paint_grid(QPainter &p, int y, int left, int right)
212{
46a0cadc
SA
213 p.setRenderHint(QPainter::Antialiasing, false);
214
215 p.setPen(QPen(GridMajorColor, 1, Qt::DashLine));
37b9fed4 216 for (int i = 1; i <= vdivs_; i++) {
46a0cadc 217 const float dy = i * div_height_;
37b9fed4
SA
218 p.drawLine(QLineF(left, y - dy, right, y - dy));
219 p.drawLine(QLineF(left, y + dy, right, y + dy));
220 }
221
46a0cadc 222 p.setPen(QPen(GridMinorColor, 1, Qt::DashLine));
37b9fed4 223 for (int i = 0; i < vdivs_; i++) {
46a0cadc 224 const float dy = i * div_height_;
37b9fed4
SA
225 const float dy25 = dy + (0.25 * div_height_);
226 const float dy50 = dy + (0.50 * div_height_);
227 const float dy75 = dy + (0.75 * div_height_);
228 p.drawLine(QLineF(left, y - dy25, right, y - dy25));
229 p.drawLine(QLineF(left, y + dy25, right, y + dy25));
230 p.drawLine(QLineF(left, y - dy50, right, y - dy50));
231 p.drawLine(QLineF(left, y + dy50, right, y + dy50));
232 p.drawLine(QLineF(left, y - dy75, right, y - dy75));
233 p.drawLine(QLineF(left, y + dy75, right, y + dy75));
234 }
46a0cadc
SA
235
236 p.setRenderHint(QPainter::Antialiasing, true);
37b9fed4
SA
237}
238
7c68ddae 239void AnalogSignal::paint_trace(QPainter &p,
f3d66e52 240 const shared_ptr<pv::data::AnalogSegment> &segment,
7c68ddae
JH
241 int y, int left, const int64_t start, const int64_t end,
242 const double pixels_offset, const double samples_per_pixel)
243{
244 const int64_t sample_count = end - start;
245
f3d66e52 246 const float *const samples = segment->get_samples(start, end);
a8acb46e
JH
247 assert(samples);
248
73a25a6e 249 p.setPen(base_->colour());
7c68ddae 250
d3758367 251 QPointF *points = new QPointF[sample_count];
a8acb46e
JH
252 QPointF *point = points;
253
7c68ddae 254 for (int64_t sample = start; sample != end; sample++) {
a8acb46e 255 const float x = (sample / samples_per_pixel -
2658961b 256 pixels_offset) + left;
d3758367 257 *point++ = QPointF(x,
834a4f1b 258 y - samples[sample - start] * scale_);
a8acb46e
JH
259 }
260
306d43a7 261 p.drawPolyline(points, point - points);
a8acb46e 262
7c68ddae 263 delete[] samples;
a8acb46e 264 delete[] points;
aba1dd16
JH
265}
266
7c68ddae 267void AnalogSignal::paint_envelope(QPainter &p,
f3d66e52 268 const shared_ptr<pv::data::AnalogSegment> &segment,
7c68ddae
JH
269 int y, int left, const int64_t start, const int64_t end,
270 const double pixels_offset, const double samples_per_pixel)
271{
f3d66e52 272 using pv::data::AnalogSegment;
7c68ddae 273
f3d66e52
JH
274 AnalogSegment::EnvelopeSection e;
275 segment->get_envelope_section(e, start, end, samples_per_pixel);
7c68ddae
JH
276
277 if (e.length < 2)
278 return;
279
819f4c25 280 p.setPen(QPen(Qt::NoPen));
73a25a6e 281 p.setBrush(base_->colour());
7c68ddae
JH
282
283 QRectF *const rects = new QRectF[e.length];
284 QRectF *rect = rects;
285
f3290553 286 for (uint64_t sample = 0; sample < e.length-1; sample++) {
7c68ddae
JH
287 const float x = ((e.scale * sample + e.start) /
288 samples_per_pixel - pixels_offset) + left;
f3d66e52 289 const AnalogSegment::EnvelopeSample *const s =
7c68ddae
JH
290 e.samples + sample;
291
292 // We overlap this sample with the next so that vertical
293 // gaps do not appear during steep rising or falling edges
834a4f1b
SA
294 const float b = y - max(s->max, (s+1)->min) * scale_;
295 const float t = y - min(s->min, (s+1)->max) * scale_;
7c68ddae
JH
296
297 float h = b - t;
f3290553 298 if (h >= 0.0f && h <= 1.0f)
7c68ddae 299 h = 1.0f;
f3290553 300 if (h <= 0.0f && h >= -1.0f)
7c68ddae
JH
301 h = -1.0f;
302
303 *rect++ = QRectF(x, t, 1.0f, h);
304 }
305
306 p.drawRects(rects, e.length);
307
308 delete[] rects;
309 delete[] e.samples;
310}
311
368a37c2 312float AnalogSignal::get_resolution(int scale_index)
8a2fafcc
JH
313{
314 const float seq[] = {1.0f, 2.0f, 5.0f};
834a4f1b 315
8a2fafcc
JH
316 const int offset = std::numeric_limits<int>::max() / (2 * countof(seq));
317 const std::div_t d = std::div(
368a37c2 318 (int)(scale_index + countof(seq) * offset),
8a2fafcc 319 countof(seq));
834a4f1b 320
368a37c2
SA
321 return powf(10.0f, d.quot - offset) * seq[d.rem];
322}
323
324void AnalogSignal::update_scale()
325{
326 resolution_ = get_resolution(scale_index_);
834a4f1b 327 scale_ = div_height_ / resolution_;
8a2fafcc
JH
328}
329
4cffac16
SA
330void AnalogSignal::populate_popup_form(QWidget *parent, QFormLayout *form)
331{
332 // Add the standard options
333 Signal::populate_popup_form(parent, form);
334
368a37c2
SA
335 QFormLayout *const layout = new QFormLayout;
336
337 // Add the number of vdivs
4cffac16
SA
338 QSpinBox *vdiv_sb = new QSpinBox(parent);
339 vdiv_sb->setRange(1, MaximumVDivs);
340 vdiv_sb->setValue(vdivs_);
341 connect(vdiv_sb, SIGNAL(valueChanged(int)),
342 this, SLOT(on_vdivs_changed(int)));
368a37c2
SA
343 layout->addRow(tr("Number of vertical divs"), vdiv_sb);
344
345 // Add the vertical resolution
346 resolution_cb_ = new QComboBox(parent);
347
348 for (int i = MinScaleIndex; i < MaxScaleIndex; i++) {
349 const QString label = QString("%1").arg(get_resolution(i));
350 resolution_cb_->insertItem(0, label, QVariant(i));
351 }
352
353 const int cur_idx = resolution_cb_->findData(QVariant(scale_index_));
354 resolution_cb_->setCurrentIndex(cur_idx);
355
356 connect(resolution_cb_, SIGNAL(currentIndexChanged(int)),
357 this, SLOT(on_resolution_changed(int)));
358
359 QGridLayout *const vdiv_layout = new QGridLayout;
360 QLabel *const vdiv_unit = new QLabel(tr("V/div"));
361 vdiv_layout->addWidget(resolution_cb_, 0, 0);
362 vdiv_layout->addWidget(vdiv_unit, 0, 1);
363
364 layout->addRow(tr("Vertical resolution"), vdiv_layout);
365
366 form->addRow(layout);
4cffac16
SA
367}
368
369void AnalogSignal::on_vdivs_changed(int vdivs)
370{
371 vdivs_ = vdivs;
372
75d0779e
SA
373 if (owner_) {
374 // Call order is important, otherwise the lazy event handler won't work
4cffac16 375 owner_->extents_changed(false, true);
75d0779e
SA
376 owner_->row_item_appearance_changed(false, true);
377 }
4cffac16
SA
378}
379
368a37c2
SA
380void AnalogSignal::on_resolution_changed(int index)
381{
382 scale_index_ = resolution_cb_->itemData(index).toInt();
383 update_scale();
384
385 if (owner_)
386 owner_->row_item_appearance_changed(false, true);
387}
4cffac16 388
f4e57597
SA
389} // namespace TraceView
390} // namespace views
aba1dd16 391} // namespace pv