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
22 #include <libsigrokdecode/libsigrokdecode.h>
27 #include <boost/foreach.hpp>
30 #include <QApplication>
32 #include <QFormLayout>
35 #include <QPushButton>
37 #include "decodetrace.h"
39 #include <pv/sigsession.h>
40 #include <pv/data/decoderstack.h>
41 #include <pv/data/decode/decoder.h>
42 #include <pv/data/logic.h>
43 #include <pv/data/logicsnapshot.h>
44 #include <pv/data/decode/annotation.h>
45 #include <pv/view/logicsignal.h>
46 #include <pv/view/view.h>
47 #include <pv/widgets/decodergroupbox.h>
48 #include <pv/widgets/decodermenu.h>
50 using namespace boost;
56 const QColor DecodeTrace::DecodeColours[4] = {
57 QColor(0xEF, 0x29, 0x29), // Red
58 QColor(0xFC, 0xE9, 0x4F), // Yellow
59 QColor(0x8A, 0xE2, 0x34), // Green
60 QColor(0x72, 0x9F, 0xCF) // Blue
63 const QColor DecodeTrace::ErrorBgColour = QColor(0xEF, 0x29, 0x29);
64 const QColor DecodeTrace::NoDecodeColour = QColor(0x88, 0x8A, 0x85);
66 const double DecodeTrace::EndCapWidth = 5;
67 const int DecodeTrace::DrawPadding = 100;
69 const QColor DecodeTrace::Colours[7] = {
70 QColor(0xFC, 0xE9, 0x4F), // Light Butter
71 QColor(0xFC, 0xAF, 0x3E), // Light Orange
72 QColor(0xE9, 0xB9, 0x6E), // Light Chocolate
73 QColor(0x8A, 0xE2, 0x34), // Light Green
74 QColor(0x72, 0x9F, 0xCF), // Light Blue
75 QColor(0xAD, 0x7F, 0xA8), // Light Plum
76 QColor(0xEF, 0x29, 0x29) // Light Red
79 DecodeTrace::DecodeTrace(pv::SigSession &session,
80 boost::shared_ptr<pv::data::DecoderStack> decoder_stack, int index) :
81 Trace(session, QString::fromUtf8(
82 decoder_stack->stack().front()->decoder()->name)),
83 _decoder_stack(decoder_stack),
86 assert(_decoder_stack);
88 _colour = DecodeColours[index % countof(DecodeColours)];
90 connect(_decoder_stack.get(), SIGNAL(new_decode_data()),
91 this, SLOT(on_new_decode_data()));
92 connect(&_delete_mapper, SIGNAL(mapped(int)),
93 this, SLOT(on_delete_decoder(int)));
96 bool DecodeTrace::enabled() const
101 const boost::shared_ptr<pv::data::DecoderStack>& DecodeTrace::decoder() const
103 return _decoder_stack;
106 void DecodeTrace::set_view(pv::view::View *view)
109 Trace::set_view(view);
112 void DecodeTrace::paint_back(QPainter &p, int left, int right)
114 Trace::paint_back(p, left, right);
115 paint_axis(p, get_y(), left, right);
118 void DecodeTrace::paint_mid(QPainter &p, int left, int right)
120 using pv::data::decode::Annotation;
122 const double scale = _view->scale();
125 double samplerate = _decoder_stack->samplerate();
127 // Show sample rate as 1Hz when it is unknown
128 if (samplerate == 0.0)
131 const double pixels_offset = (_view->offset() -
132 _decoder_stack->get_start_time()) / scale;
133 const double samples_per_pixel = samplerate * scale;
135 QFontMetrics m(QApplication::font());
136 const int h = (m.boundingRect(QRect(), 0, "Tg").height() * 5) / 4;
138 assert(_decoder_stack);
139 const QString err = _decoder_stack->error_message();
142 draw_error(p, err, left, right);
143 draw_unresolved_period(p, h, left, right, samples_per_pixel,
149 const int y = get_y();
151 assert(_decoder_stack);
152 vector<Annotation> annotations(_decoder_stack->annotations());
153 BOOST_FOREACH(const Annotation &a, annotations)
154 draw_annotation(a, p, get_text_colour(), h, left, right,
155 samples_per_pixel, pixels_offset, y);
157 draw_unresolved_period(p, h, left, right,
158 samples_per_pixel, pixels_offset);
161 void DecodeTrace::populate_popup_form(QWidget *parent, QFormLayout *form)
163 using pv::data::decode::Decoder;
167 assert(_decoder_stack);
169 // Add the standard options
170 Trace::populate_popup_form(parent, form);
172 // Add the decoder options
174 _probe_selectors.clear();
176 const list< shared_ptr<Decoder> >& stack = _decoder_stack->stack();
180 QLabel *const l = new QLabel(
181 tr("<p><i>No decoders in the stack</i></p>"));
182 l->setAlignment(Qt::AlignCenter);
187 list< shared_ptr<Decoder> >::const_iterator iter =
189 for (int i = 0; i < (int)stack.size(); i++, iter++) {
190 shared_ptr<Decoder> dec(*iter);
191 create_decoder_form(i, dec, parent, form);
194 form->addRow(new QLabel(
195 tr("<i>* Required Probes</i>"), parent));
198 // Add stacking button
199 pv::widgets::DecoderMenu *const decoder_menu =
200 new pv::widgets::DecoderMenu(parent);
201 connect(decoder_menu, SIGNAL(decoder_selected(srd_decoder*)),
202 this, SLOT(on_stack_decoder(srd_decoder*)));
204 QPushButton *const stack_button =
205 new QPushButton(tr("Stack Decoder"), parent);
206 stack_button->setMenu(decoder_menu);
208 QHBoxLayout *stack_button_box = new QHBoxLayout;
209 stack_button_box->addWidget(stack_button, 0, Qt::AlignRight);
210 form->addRow(stack_button_box);
213 QMenu* DecodeTrace::create_context_menu(QWidget *parent)
215 QMenu *const menu = Trace::create_context_menu(parent);
217 menu->addSeparator();
219 QAction *const del = new QAction(tr("Delete"), this);
220 del->setShortcuts(QKeySequence::Delete);
221 connect(del, SIGNAL(triggered()), this, SLOT(on_delete()));
222 menu->addAction(del);
227 void DecodeTrace::draw_annotation(const pv::data::decode::Annotation &a, QPainter &p,
228 QColor text_color, int h, int left, int right, double samples_per_pixel,
229 double pixels_offset, int y) const
231 const double start = a.start_sample() / samples_per_pixel -
233 const double end = a.end_sample() / samples_per_pixel -
235 const QColor fill = Colours[(a.format() * (countof(Colours) / 2 + 1)) %
237 const QColor outline(fill.darker());
239 if (start > right + DrawPadding || end < left - DrawPadding)
242 if (a.start_sample() == a.end_sample())
243 draw_instant(a, p, fill, outline, text_color, h,
246 draw_range(a, p, fill, outline, text_color, h,
250 void DecodeTrace::draw_instant(const pv::data::decode::Annotation &a, QPainter &p,
251 QColor fill, QColor outline, QColor text_color, int h, double x, int y) const
253 const QString text = a.annotations().empty() ?
254 QString() : a.annotations().back();
255 const double w = min(p.boundingRect(QRectF(), 0, text).width(),
257 const QRectF rect(x - w / 2, y - h / 2, w, h);
261 p.drawRoundedRect(rect, h / 2, h / 2);
263 p.setPen(text_color);
264 p.drawText(rect, Qt::AlignCenter | Qt::AlignVCenter, text);
267 void DecodeTrace::draw_range(const pv::data::decode::Annotation &a, QPainter &p,
268 QColor fill, QColor outline, QColor text_color, int h, double start,
269 double end, int y) const
271 const double top = y + .5 - h / 2;
272 const double bottom = y + .5 + h / 2;
273 const vector<QString> annotations = a.annotations();
278 // If the two ends are within 1 pixel, draw a vertical line
279 if (start + 1.0 > end)
281 p.drawLine(QPointF(start, top), QPointF(start, bottom));
285 const double cap_width = min((end - start) / 4, EndCapWidth);
288 QPointF(start, y + .5f),
289 QPointF(start + cap_width, top),
290 QPointF(end - cap_width, top),
291 QPointF(end, y + .5f),
292 QPointF(end - cap_width, bottom),
293 QPointF(start + cap_width, bottom)
296 p.drawConvexPolygon(pts, countof(pts));
298 if (annotations.empty())
301 QRectF rect(start + cap_width, y - h / 2,
302 end - start - cap_width * 2, h);
303 p.setPen(text_color);
305 // Try to find an annotation that will fit
306 QString best_annotation;
309 BOOST_FOREACH(const QString &a, annotations) {
310 const int w = p.boundingRect(QRectF(), 0, a).width();
311 if (w <= rect.width() && w > best_width)
312 best_annotation = a, best_width = w;
315 if (best_annotation.isEmpty())
316 best_annotation = annotations.back();
318 // If not ellide the last in the list
319 p.drawText(rect, Qt::AlignCenter, p.fontMetrics().elidedText(
320 best_annotation, Qt::ElideRight, rect.width()));
323 void DecodeTrace::draw_error(QPainter &p, const QString &message,
326 const int y = get_y();
328 p.setPen(ErrorBgColour.darker());
329 p.setBrush(ErrorBgColour);
331 const QRectF bounding_rect =
332 QRectF(left, INT_MIN / 2 + y, right - left, INT_MAX);
333 const QRectF text_rect = p.boundingRect(bounding_rect,
334 Qt::AlignCenter, message);
335 const float r = text_rect.height() / 4;
337 p.drawRoundedRect(text_rect.adjusted(-r, -r, r, r), r, r,
340 p.setPen(get_text_colour());
341 p.drawText(text_rect, message);
344 void DecodeTrace::draw_unresolved_period(QPainter &p, int h, int left,
345 int right, double samples_per_pixel, double pixels_offset)
347 using namespace pv::data;
348 using pv::data::decode::Decoder;
350 assert(_decoder_stack);
352 shared_ptr<Logic> data;
353 shared_ptr<LogicSignal> logic_signal;
355 const list< shared_ptr<Decoder> > &stack = _decoder_stack->stack();
357 // We get the logic data of the first probe in the list.
358 // This works because we are currently assuming all
359 // LogicSignals have the same data/snapshot
360 BOOST_FOREACH (const shared_ptr<Decoder> &dec, stack)
361 if (dec && !dec->probes().empty() &&
362 ((logic_signal = (*dec->probes().begin()).second)) &&
363 ((data = logic_signal->logic_data())))
366 if (!data || data->get_snapshots().empty())
369 const shared_ptr<LogicSnapshot> snapshot =
370 data->get_snapshots().front();
372 const int64_t sample_count = (int64_t)snapshot->get_sample_count();
373 if (sample_count == 0)
376 const int64_t samples_decoded = _decoder_stack->samples_decoded();
377 if (sample_count == samples_decoded)
380 const int y = get_y();
381 const double start = max(samples_decoded /
382 samples_per_pixel - pixels_offset, left - 1.0);
383 const double end = min(sample_count / samples_per_pixel -
384 pixels_offset, right + 1.0);
385 const QRectF no_decode_rect(start, y - h/2 + 0.5, end - start, h);
387 p.setPen(QPen(Qt::NoPen));
388 p.setBrush(Qt::white);
389 p.drawRect(no_decode_rect);
391 p.setPen(NoDecodeColour);
392 p.setBrush(QBrush(NoDecodeColour, Qt::Dense6Pattern));
393 p.drawRect(no_decode_rect);
396 void DecodeTrace::create_decoder_form(int index,
397 shared_ptr<data::decode::Decoder> &dec, QWidget *parent,
403 const srd_decoder *const decoder = dec->decoder();
406 pv::widgets::DecoderGroupBox *const group =
407 new pv::widgets::DecoderGroupBox(
408 QString::fromUtf8(decoder->name));
410 _delete_mapper.setMapping(group, index);
411 connect(group, SIGNAL(delete_decoder()), &_delete_mapper, SLOT(map()));
413 QFormLayout *const decoder_form = new QFormLayout;
414 group->add_layout(decoder_form);
416 // Add the mandatory probes
417 for(probe = decoder->probes; probe; probe = probe->next) {
418 const struct srd_probe *const p =
419 (struct srd_probe *)probe->data;
420 QComboBox *const combo = create_probe_selector(parent, dec, p);
421 connect(combo, SIGNAL(currentIndexChanged(int)),
422 this, SLOT(on_probe_selected(int)));
423 decoder_form->addRow(tr("<b>%1</b> (%2) *")
424 .arg(p->name).arg(p->desc), combo);
426 const ProbeSelector s = {combo, dec, p};
427 _probe_selectors.push_back(s);
430 // Add the optional probes
431 for(probe = decoder->opt_probes; probe; probe = probe->next) {
432 const struct srd_probe *const p =
433 (struct srd_probe *)probe->data;
434 QComboBox *const combo = create_probe_selector(parent, dec, p);
435 connect(combo, SIGNAL(currentIndexChanged(int)),
436 this, SLOT(on_probe_selected(int)));
437 decoder_form->addRow(tr("<b>%1</b> (%2)")
438 .arg(p->name).arg(p->desc), combo);
440 const ProbeSelector s = {combo, dec, p};
441 _probe_selectors.push_back(s);
445 shared_ptr<prop::binding::DecoderOptions> binding(
446 new prop::binding::DecoderOptions(_decoder_stack, dec));
447 binding->add_properties_to_form(decoder_form, true);
449 _bindings.push_back(binding);
454 QComboBox* DecodeTrace::create_probe_selector(
455 QWidget *parent, const shared_ptr<data::decode::Decoder> &dec,
456 const srd_probe *const probe)
460 const vector< shared_ptr<Signal> > sigs = _session.get_signals();
462 assert(_decoder_stack);
463 const map<const srd_probe*,
464 shared_ptr<LogicSignal> >::const_iterator probe_iter =
465 dec->probes().find(probe);
467 QComboBox *selector = new QComboBox(parent);
469 selector->addItem("-", qVariantFromValue((void*)NULL));
471 if (probe_iter == dec->probes().end())
472 selector->setCurrentIndex(0);
474 for(size_t i = 0; i < sigs.size(); i++) {
475 const shared_ptr<view::Signal> s(sigs[i]);
478 if (dynamic_pointer_cast<LogicSignal>(s) && s->enabled())
480 selector->addItem(s->get_name(),
481 qVariantFromValue((void*)s.get()));
482 if ((*probe_iter).second == s)
483 selector->setCurrentIndex(i + 1);
490 void DecodeTrace::commit_decoder_probes(shared_ptr<data::decode::Decoder> &dec)
494 map<const srd_probe*, shared_ptr<LogicSignal> > probe_map;
495 const vector< shared_ptr<Signal> > sigs = _session.get_signals();
497 BOOST_FOREACH(const ProbeSelector &s, _probe_selectors)
499 if(s._decoder != dec)
502 const LogicSignal *const selection =
503 (LogicSignal*)s._combo->itemData(
504 s._combo->currentIndex()).value<void*>();
506 BOOST_FOREACH(shared_ptr<Signal> sig, sigs)
507 if(sig.get() == selection) {
508 probe_map[s._probe] =
509 dynamic_pointer_cast<LogicSignal>(sig);
514 dec->set_probes(probe_map);
517 void DecodeTrace::commit_probes()
519 assert(_decoder_stack);
520 BOOST_FOREACH(shared_ptr<data::decode::Decoder> dec,
521 _decoder_stack->stack())
522 commit_decoder_probes(dec);
524 _decoder_stack->begin_decode();
527 void DecodeTrace::on_new_decode_data()
530 _view->update_viewport();
533 void DecodeTrace::delete_pressed()
538 void DecodeTrace::on_delete()
540 _session.remove_decode_signal(this);
543 void DecodeTrace::on_probe_selected(int)
548 void DecodeTrace::on_stack_decoder(srd_decoder *decoder)
551 assert(_decoder_stack);
552 _decoder_stack->push(shared_ptr<data::decode::Decoder>(
553 new data::decode::Decoder(decoder)));
554 _decoder_stack->begin_decode();
559 void DecodeTrace::on_delete_decoder(int index)
561 _decoder_stack->remove(index);
566 _decoder_stack->begin_decode();