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