2 * This file is part of the PulseView project.
4 * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk>
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.
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.
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
28 #include <QApplication>
30 #include <QFormLayout>
31 #include <QGridLayout>
36 #include "analogsignal.hpp"
37 #include "pv/data/analog.hpp"
38 #include "pv/data/analogsegment.hpp"
39 #include "pv/view/view.hpp"
41 #include <libsigrokcxx/libsigrokcxx.hpp>
46 using std::shared_ptr;
49 using sigrok::Channel;
54 const 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
61 const QColor AnalogSignal::GridMajorColor = QColor(0, 0, 0, 40*256/100);
62 const QColor AnalogSignal::GridMinorColor = QColor(0, 0, 0, 20*256/100);
64 const float AnalogSignal::EnvelopeThreshold = 256.0f;
66 const int AnalogSignal::MaximumVDivs = 10;
67 const int AnalogSignal::MinScaleIndex = -6;
68 const int AnalogSignal::MaxScaleIndex = 7;
70 const int AnalogSignal::InfoTextMarginRight = 20;
71 const int AnalogSignal::InfoTextMarginBottom = 5;
73 AnalogSignal::AnalogSignal(
75 shared_ptr<Channel> channel,
76 shared_ptr<data::Analog> data) :
77 Signal(session, channel),
79 scale_index_(4), // 20 per div
80 scale_index_drag_offset_(0),
81 div_height_(3 * QFontMetrics(QApplication::font()).height()),
85 set_colour(SignalColours[channel_->index() % countof(SignalColours)]);
89 shared_ptr<pv::data::SignalData> AnalogSignal::data() const
94 shared_ptr<pv::data::Analog> AnalogSignal::analog_data() const
99 std::pair<int, int> AnalogSignal::v_extents() const
101 const int h = vdivs_ * div_height_;
102 return make_pair(-h, h);
105 int AnalogSignal::scale_handle_offset() const
107 const int h = vdivs_ * div_height_;
109 return ((scale_index_drag_offset_ - scale_index_) *
113 void AnalogSignal::scale_handle_dragged(int offset)
115 const int h = vdivs_ * div_height_;
117 scale_index_ = scale_index_drag_offset_ -
118 (offset + h / 2) / (h / 4);
123 void AnalogSignal::scale_handle_drag_release()
125 scale_index_drag_offset_ = scale_index_;
129 void AnalogSignal::paint_back(QPainter &p, const ViewItemPaintParams &pp)
131 if (channel_->enabled()) {
132 Trace::paint_back(p, pp);
133 paint_axis(p, pp, get_visual_y());
137 void AnalogSignal::paint_mid(QPainter &p, const ViewItemPaintParams &pp)
142 const int y = get_visual_y();
144 if (!channel_->enabled())
147 paint_grid(p, y, pp.left(), pp.right());
149 const deque< shared_ptr<pv::data::AnalogSegment> > &segments =
150 data_->analog_segments();
151 if (segments.empty())
154 const shared_ptr<pv::data::AnalogSegment> &segment =
157 const double pixels_offset = pp.pixels_offset();
158 const double samplerate = max(1.0, segment->samplerate());
159 const pv::util::Timestamp& start_time = segment->start_time();
160 const int64_t last_sample = segment->get_sample_count() - 1;
161 const double samples_per_pixel = samplerate * pp.scale();
162 const pv::util::Timestamp start = samplerate * (pp.offset() - start_time);
163 const pv::util::Timestamp end = start + samples_per_pixel * pp.width();
165 const int64_t start_sample = min(max(floor(start).convert_to<int64_t>(),
166 (int64_t)0), last_sample);
167 const int64_t end_sample = min(max((ceil(end) + 1).convert_to<int64_t>(),
168 (int64_t)0), last_sample);
170 if (samples_per_pixel < EnvelopeThreshold)
171 paint_trace(p, segment, y, pp.left(),
172 start_sample, end_sample,
173 pixels_offset, samples_per_pixel);
175 paint_envelope(p, segment, y, pp.left(),
176 start_sample, end_sample,
177 pixels_offset, samples_per_pixel);
180 void AnalogSignal::paint_fore(QPainter &p, const ViewItemPaintParams &pp)
185 const int y = get_visual_y();
187 // Show the info section on the right side of the trace
188 const QString infotext = QString("%1 V/div").arg(resolution_);
191 p.setFont(QApplication::font());
193 const QRectF bounding_rect = QRectF(pp.left(),
194 y + v_extents().first,
195 pp.width() - InfoTextMarginRight,
196 v_extents().second - v_extents().first - InfoTextMarginBottom);
198 p.drawText(bounding_rect, Qt::AlignRight | Qt::AlignBottom, infotext);
201 void AnalogSignal::paint_grid(QPainter &p, int y, int left, int right)
203 p.setRenderHint(QPainter::Antialiasing, false);
205 p.setPen(QPen(GridMajorColor, 1, Qt::DashLine));
206 for (int i = 1; i <= vdivs_; i++) {
207 const float dy = i * div_height_;
208 p.drawLine(QLineF(left, y - dy, right, y - dy));
209 p.drawLine(QLineF(left, y + dy, right, y + dy));
212 p.setPen(QPen(GridMinorColor, 1, Qt::DashLine));
213 for (int i = 0; i < vdivs_; i++) {
214 const float dy = i * div_height_;
215 const float dy25 = dy + (0.25 * div_height_);
216 const float dy50 = dy + (0.50 * div_height_);
217 const float dy75 = dy + (0.75 * div_height_);
218 p.drawLine(QLineF(left, y - dy25, right, y - dy25));
219 p.drawLine(QLineF(left, y + dy25, right, y + dy25));
220 p.drawLine(QLineF(left, y - dy50, right, y - dy50));
221 p.drawLine(QLineF(left, y + dy50, right, y + dy50));
222 p.drawLine(QLineF(left, y - dy75, right, y - dy75));
223 p.drawLine(QLineF(left, y + dy75, right, y + dy75));
226 p.setRenderHint(QPainter::Antialiasing, true);
229 void AnalogSignal::paint_trace(QPainter &p,
230 const shared_ptr<pv::data::AnalogSegment> &segment,
231 int y, int left, const int64_t start, const int64_t end,
232 const double pixels_offset, const double samples_per_pixel)
234 const int64_t sample_count = end - start;
236 const float *const samples = segment->get_samples(start, end);
241 QPointF *points = new QPointF[sample_count];
242 QPointF *point = points;
244 for (int64_t sample = start; sample != end; sample++) {
245 const float x = (sample / samples_per_pixel -
246 pixels_offset) + left;
247 *point++ = QPointF(x,
248 y - samples[sample - start] * scale_);
251 p.drawPolyline(points, point - points);
257 void AnalogSignal::paint_envelope(QPainter &p,
258 const shared_ptr<pv::data::AnalogSegment> &segment,
259 int y, int left, const int64_t start, const int64_t end,
260 const double pixels_offset, const double samples_per_pixel)
262 using pv::data::AnalogSegment;
264 AnalogSegment::EnvelopeSection e;
265 segment->get_envelope_section(e, start, end, samples_per_pixel);
270 p.setPen(QPen(Qt::NoPen));
273 QRectF *const rects = new QRectF[e.length];
274 QRectF *rect = rects;
276 for (uint64_t sample = 0; sample < e.length-1; sample++) {
277 const float x = ((e.scale * sample + e.start) /
278 samples_per_pixel - pixels_offset) + left;
279 const AnalogSegment::EnvelopeSample *const s =
282 // We overlap this sample with the next so that vertical
283 // gaps do not appear during steep rising or falling edges
284 const float b = y - max(s->max, (s+1)->min) * scale_;
285 const float t = y - min(s->min, (s+1)->max) * scale_;
288 if (h >= 0.0f && h <= 1.0f)
290 if (h <= 0.0f && h >= -1.0f)
293 *rect++ = QRectF(x, t, 1.0f, h);
296 p.drawRects(rects, e.length);
302 float AnalogSignal::get_resolution(int scale_index)
304 const float seq[] = {1.0f, 2.0f, 5.0f};
306 const int offset = std::numeric_limits<int>::max() / (2 * countof(seq));
307 const std::div_t d = std::div(
308 (int)(scale_index + countof(seq) * offset),
311 return powf(10.0f, d.quot - offset) * seq[d.rem];
314 void AnalogSignal::update_scale()
316 resolution_ = get_resolution(scale_index_);
317 scale_ = div_height_ / resolution_;
320 void AnalogSignal::populate_popup_form(QWidget *parent, QFormLayout *form)
322 // Add the standard options
323 Signal::populate_popup_form(parent, form);
325 QFormLayout *const layout = new QFormLayout;
327 // Add the number of vdivs
328 QSpinBox *vdiv_sb = new QSpinBox(parent);
329 vdiv_sb->setRange(1, MaximumVDivs);
330 vdiv_sb->setValue(vdivs_);
331 connect(vdiv_sb, SIGNAL(valueChanged(int)),
332 this, SLOT(on_vdivs_changed(int)));
333 layout->addRow(tr("Number of vertical divs"), vdiv_sb);
335 // Add the vertical resolution
336 resolution_cb_ = new QComboBox(parent);
338 for (int i = MinScaleIndex; i < MaxScaleIndex; i++) {
339 const QString label = QString("%1").arg(get_resolution(i));
340 resolution_cb_->insertItem(0, label, QVariant(i));
343 const int cur_idx = resolution_cb_->findData(QVariant(scale_index_));
344 resolution_cb_->setCurrentIndex(cur_idx);
346 connect(resolution_cb_, SIGNAL(currentIndexChanged(int)),
347 this, SLOT(on_resolution_changed(int)));
349 QGridLayout *const vdiv_layout = new QGridLayout;
350 QLabel *const vdiv_unit = new QLabel(tr("V/div"));
351 vdiv_layout->addWidget(resolution_cb_, 0, 0);
352 vdiv_layout->addWidget(vdiv_unit, 0, 1);
354 layout->addRow(tr("Vertical resolution"), vdiv_layout);
356 form->addRow(layout);
359 void AnalogSignal::on_vdivs_changed(int vdivs)
364 // Call order is important, otherwise the lazy event handler won't work
365 owner_->extents_changed(false, true);
366 owner_->row_item_appearance_changed(false, true);
370 void AnalogSignal::on_resolution_changed(int index)
372 scale_index_ = resolution_cb_->itemData(index).toInt();
376 owner_->row_item_appearance_changed(false, true);