]> sigrok.org Git - pulseview.git/blame - pv/view/analogsignal.cpp
AnalogSignal: Use Q_OBJECT and implement vdiv selector in dialog
[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>
4cffac16
SA
29#include <QFormLayout>
30#include <QSpinBox>
37b9fed4 31
2acdb232
JH
32#include "analogsignal.hpp"
33#include "pv/data/analog.hpp"
f3d66e52 34#include "pv/data/analogsegment.hpp"
2acdb232 35#include "pv/view/view.hpp"
aba1dd16 36
fe3a1c21 37#include <libsigrokcxx/libsigrokcxx.hpp>
e8d00928 38
819f4c25 39using std::max;
a5d93c27 40using std::make_pair;
819f4c25 41using std::min;
f9abf97e 42using std::shared_ptr;
819f4c25 43using std::deque;
aba1dd16 44
e8d00928
ML
45using sigrok::Channel;
46
aba1dd16
JH
47namespace pv {
48namespace view {
49
568b90d4
JH
50const QColor AnalogSignal::SignalColours[4] = {
51 QColor(0xC4, 0xA0, 0x00), // Yellow
52 QColor(0x87, 0x20, 0x7A), // Magenta
53 QColor(0x20, 0x4A, 0x87), // Blue
54 QColor(0x4E, 0x9A, 0x06) // Green
55};
56
37b9fed4
SA
57const QColor AnalogSignal::GridMajorColor = QColor(0xB0, 0xB0, 0xB0);
58const QColor AnalogSignal::GridMinorColor = QColor(0xD0, 0xD0, 0xD0);
59
7c68ddae
JH
60const float AnalogSignal::EnvelopeThreshold = 256.0f;
61
4cffac16
SA
62const int AnalogSignal::MaximumVDivs = 10;
63
b86aa8f4 64AnalogSignal::AnalogSignal(
2b81ae46 65 pv::Session &session,
b86aa8f4 66 shared_ptr<Channel> channel,
e8d00928 67 shared_ptr<data::Analog> data) :
b86aa8f4 68 Signal(session, channel),
8dbbc7f0 69 data_(data),
834a4f1b 70 scale_index_(4), // 20 per div
37b9fed4
SA
71 scale_index_drag_offset_(0),
72 div_height_(3 * QFontMetrics(QApplication::font()).height()),
834a4f1b
SA
73 vdivs_(1),
74 resolution_(0)
aba1dd16 75{
99af6802 76 set_colour(SignalColours[channel_->index() % countof(SignalColours)]);
834a4f1b 77 update_scale();
aba1dd16
JH
78}
79
3009b5b3
JH
80shared_ptr<pv::data::SignalData> AnalogSignal::data() const
81{
8dbbc7f0 82 return data_;
3009b5b3
JH
83}
84
85shared_ptr<pv::data::Analog> AnalogSignal::analog_data() const
9a0cd293 86{
8dbbc7f0 87 return data_;
9a0cd293
JH
88}
89
a5d93c27
JH
90std::pair<int, int> AnalogSignal::v_extents() const
91{
37b9fed4 92 const int h = vdivs_ * div_height_;
8a2fafcc 93 return make_pair(-h, h);
a5d93c27
JH
94}
95
214470fc
JH
96int AnalogSignal::scale_handle_offset() const
97{
37b9fed4
SA
98 const int h = vdivs_ * div_height_;
99
8a2fafcc 100 return ((scale_index_drag_offset_ - scale_index_) *
37b9fed4 101 h / 4) - h / 2;
214470fc
JH
102}
103
104void AnalogSignal::scale_handle_dragged(int offset)
105{
37b9fed4
SA
106 const int h = vdivs_ * div_height_;
107
8a2fafcc 108 scale_index_ = scale_index_drag_offset_ -
37b9fed4 109 (offset + h / 2) / (h / 4);
834a4f1b
SA
110
111 update_scale();
8a2fafcc
JH
112}
113
114void AnalogSignal::scale_handle_drag_release()
115{
116 scale_index_drag_offset_ = scale_index_;
834a4f1b 117 update_scale();
214470fc
JH
118}
119
5b5fa4da 120void AnalogSignal::paint_back(QPainter &p, const ViewItemPaintParams &pp)
fe08b6e8 121{
99af6802
SA
122 if (channel_->enabled()) {
123 Trace::paint_back(p, pp);
97904bf7 124 paint_axis(p, pp, get_visual_y());
99af6802 125 }
fe08b6e8
JH
126}
127
5b5fa4da 128void AnalogSignal::paint_mid(QPainter &p, const ViewItemPaintParams &pp)
aba1dd16 129{
8dbbc7f0 130 assert(data_);
8dbbc7f0 131 assert(owner_);
a8acb46e 132
be9e7b4b 133 const int y = get_visual_y();
01fd3263 134
8dbbc7f0 135 if (!channel_->enabled())
cec48d16
JH
136 return;
137
37b9fed4
SA
138 paint_grid(p, y, pp.left(), pp.right());
139
f3d66e52
JH
140 const deque< shared_ptr<pv::data::AnalogSegment> > &segments =
141 data_->analog_segments();
142 if (segments.empty())
a8acb46e
JH
143 return;
144
f3d66e52
JH
145 const shared_ptr<pv::data::AnalogSegment> &segment =
146 segments.front();
a8acb46e 147
4c8a6a6d 148 const double pixels_offset = pp.pixels_offset();
69e33a1b 149 const double samplerate = max(1.0, segment->samplerate());
60d9b99a 150 const pv::util::Timestamp& start_time = segment->start_time();
f3d66e52 151 const int64_t last_sample = segment->get_sample_count() - 1;
4c8a6a6d 152 const double samples_per_pixel = samplerate * pp.scale();
60d9b99a
JS
153 const pv::util::Timestamp start = samplerate * (pp.offset() - start_time);
154 const pv::util::Timestamp end = start + samples_per_pixel * pp.width();
a8acb46e 155
60d9b99a 156 const int64_t start_sample = min(max(floor(start).convert_to<int64_t>(),
a8acb46e 157 (int64_t)0), last_sample);
60d9b99a 158 const int64_t end_sample = min(max((ceil(end) + 1).convert_to<int64_t>(),
a8acb46e
JH
159 (int64_t)0), last_sample);
160
7c68ddae 161 if (samples_per_pixel < EnvelopeThreshold)
f3d66e52 162 paint_trace(p, segment, y, pp.left(),
7c68ddae
JH
163 start_sample, end_sample,
164 pixels_offset, samples_per_pixel);
165 else
f3d66e52 166 paint_envelope(p, segment, y, pp.left(),
7c68ddae
JH
167 start_sample, end_sample,
168 pixels_offset, samples_per_pixel);
169}
170
37b9fed4
SA
171void AnalogSignal::paint_grid(QPainter &p, int y, int left, int right)
172{
173 p.setPen(QPen(GridMajorColor, 0.5, Qt::DashLine));
174 for (int i = 1; i <= vdivs_; i++) {
175 const int dy = i * div_height_;
176 p.drawLine(QLineF(left, y - dy, right, y - dy));
177 p.drawLine(QLineF(left, y + dy, right, y + dy));
178 }
179
180 p.setPen(QPen(GridMinorColor, 0.5, Qt::DashLine));
181 for (int i = 0; i < vdivs_; i++) {
182 const int dy = i * div_height_;
183 const float dy25 = dy + (0.25 * div_height_);
184 const float dy50 = dy + (0.50 * div_height_);
185 const float dy75 = dy + (0.75 * div_height_);
186 p.drawLine(QLineF(left, y - dy25, right, y - dy25));
187 p.drawLine(QLineF(left, y + dy25, right, y + dy25));
188 p.drawLine(QLineF(left, y - dy50, right, y - dy50));
189 p.drawLine(QLineF(left, y + dy50, right, y + dy50));
190 p.drawLine(QLineF(left, y - dy75, right, y - dy75));
191 p.drawLine(QLineF(left, y + dy75, right, y + dy75));
192 }
193}
194
7c68ddae 195void AnalogSignal::paint_trace(QPainter &p,
f3d66e52 196 const shared_ptr<pv::data::AnalogSegment> &segment,
7c68ddae
JH
197 int y, int left, const int64_t start, const int64_t end,
198 const double pixels_offset, const double samples_per_pixel)
199{
200 const int64_t sample_count = end - start;
201
f3d66e52 202 const float *const samples = segment->get_samples(start, end);
a8acb46e
JH
203 assert(samples);
204
8dbbc7f0 205 p.setPen(colour_);
7c68ddae 206
d3758367 207 QPointF *points = new QPointF[sample_count];
a8acb46e
JH
208 QPointF *point = points;
209
7c68ddae 210 for (int64_t sample = start; sample != end; sample++) {
a8acb46e 211 const float x = (sample / samples_per_pixel -
2658961b 212 pixels_offset) + left;
d3758367 213 *point++ = QPointF(x,
834a4f1b 214 y - samples[sample - start] * scale_);
a8acb46e
JH
215 }
216
306d43a7 217 p.drawPolyline(points, point - points);
a8acb46e 218
7c68ddae 219 delete[] samples;
a8acb46e 220 delete[] points;
aba1dd16
JH
221}
222
7c68ddae 223void AnalogSignal::paint_envelope(QPainter &p,
f3d66e52 224 const shared_ptr<pv::data::AnalogSegment> &segment,
7c68ddae
JH
225 int y, int left, const int64_t start, const int64_t end,
226 const double pixels_offset, const double samples_per_pixel)
227{
f3d66e52 228 using pv::data::AnalogSegment;
7c68ddae 229
f3d66e52
JH
230 AnalogSegment::EnvelopeSection e;
231 segment->get_envelope_section(e, start, end, samples_per_pixel);
7c68ddae
JH
232
233 if (e.length < 2)
234 return;
235
819f4c25 236 p.setPen(QPen(Qt::NoPen));
8dbbc7f0 237 p.setBrush(colour_);
7c68ddae
JH
238
239 QRectF *const rects = new QRectF[e.length];
240 QRectF *rect = rects;
241
f3290553 242 for (uint64_t sample = 0; sample < e.length-1; sample++) {
7c68ddae
JH
243 const float x = ((e.scale * sample + e.start) /
244 samples_per_pixel - pixels_offset) + left;
f3d66e52 245 const AnalogSegment::EnvelopeSample *const s =
7c68ddae
JH
246 e.samples + sample;
247
248 // We overlap this sample with the next so that vertical
249 // gaps do not appear during steep rising or falling edges
834a4f1b
SA
250 const float b = y - max(s->max, (s+1)->min) * scale_;
251 const float t = y - min(s->min, (s+1)->max) * scale_;
7c68ddae
JH
252
253 float h = b - t;
f3290553 254 if (h >= 0.0f && h <= 1.0f)
7c68ddae 255 h = 1.0f;
f3290553 256 if (h <= 0.0f && h >= -1.0f)
7c68ddae
JH
257 h = -1.0f;
258
259 *rect++ = QRectF(x, t, 1.0f, h);
260 }
261
262 p.drawRects(rects, e.length);
263
264 delete[] rects;
265 delete[] e.samples;
266}
267
834a4f1b 268void AnalogSignal::update_scale()
8a2fafcc
JH
269{
270 const float seq[] = {1.0f, 2.0f, 5.0f};
834a4f1b 271
8a2fafcc
JH
272 const int offset = std::numeric_limits<int>::max() / (2 * countof(seq));
273 const std::div_t d = std::div(
274 (int)(scale_index_ + countof(seq) * offset),
275 countof(seq));
834a4f1b
SA
276
277 resolution_ = powf(10.0f, d.quot - offset) * seq[d.rem];
278 scale_ = div_height_ / resolution_;
8a2fafcc
JH
279}
280
4cffac16
SA
281void AnalogSignal::populate_popup_form(QWidget *parent, QFormLayout *form)
282{
283 // Add the standard options
284 Signal::populate_popup_form(parent, form);
285
286 // Add the vdiv settings
287 QSpinBox *vdiv_sb = new QSpinBox(parent);
288 vdiv_sb->setRange(1, MaximumVDivs);
289 vdiv_sb->setValue(vdivs_);
290 connect(vdiv_sb, SIGNAL(valueChanged(int)),
291 this, SLOT(on_vdivs_changed(int)));
292 form->addRow(tr("Number of vertical divs"), vdiv_sb);
293}
294
295void AnalogSignal::on_vdivs_changed(int vdivs)
296{
297 vdivs_ = vdivs;
298
299 if (owner_)
300 owner_->extents_changed(false, true);
301}
302
303
aba1dd16
JH
304} // namespace view
305} // namespace pv