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 boost::dynamic_pointer_cast;
51 using boost::shared_ptr;
61 const QColor DecodeTrace::DecodeColours[4] = {
62 QColor(0xEF, 0x29, 0x29), // Red
63 QColor(0xFC, 0xE9, 0x4F), // Yellow
64 QColor(0x8A, 0xE2, 0x34), // Green
65 QColor(0x72, 0x9F, 0xCF) // Blue
68 const QColor DecodeTrace::ErrorBgColour = QColor(0xEF, 0x29, 0x29);
69 const QColor DecodeTrace::NoDecodeColour = QColor(0x88, 0x8A, 0x85);
71 const double DecodeTrace::EndCapWidth = 5;
72 const int DecodeTrace::DrawPadding = 100;
74 const QColor DecodeTrace::Colours[7] = {
75 QColor(0xFC, 0xE9, 0x4F), // Light Butter
76 QColor(0xFC, 0xAF, 0x3E), // Light Orange
77 QColor(0xE9, 0xB9, 0x6E), // Light Chocolate
78 QColor(0x8A, 0xE2, 0x34), // Light Green
79 QColor(0x72, 0x9F, 0xCF), // Light Blue
80 QColor(0xAD, 0x7F, 0xA8), // Light Plum
81 QColor(0xEF, 0x29, 0x29) // Light Red
84 DecodeTrace::DecodeTrace(pv::SigSession &session,
85 boost::shared_ptr<pv::data::DecoderStack> decoder_stack, int index) :
86 Trace(session, QString::fromUtf8(
87 decoder_stack->stack().front()->decoder()->name)),
88 _decoder_stack(decoder_stack),
91 assert(_decoder_stack);
93 _colour = DecodeColours[index % countof(DecodeColours)];
95 connect(_decoder_stack.get(), SIGNAL(new_decode_data()),
96 this, SLOT(on_new_decode_data()));
97 connect(&_delete_mapper, SIGNAL(mapped(int)),
98 this, SLOT(on_delete_decoder(int)));
101 bool DecodeTrace::enabled() const
106 const boost::shared_ptr<pv::data::DecoderStack>& DecodeTrace::decoder() const
108 return _decoder_stack;
111 void DecodeTrace::set_view(pv::view::View *view)
114 Trace::set_view(view);
117 void DecodeTrace::paint_back(QPainter &p, int left, int right)
119 Trace::paint_back(p, left, right);
120 paint_axis(p, get_y(), left, right);
123 void DecodeTrace::paint_mid(QPainter &p, int left, int right)
125 using namespace pv::data::decode;
127 const double scale = _view->scale();
130 double samplerate = _decoder_stack->samplerate();
132 // Show sample rate as 1Hz when it is unknown
133 if (samplerate == 0.0)
136 const double pixels_offset = (_view->offset() -
137 _decoder_stack->get_start_time()) / scale;
138 const double samples_per_pixel = samplerate * scale;
140 const uint64_t start_sample = (uint64_t)max((left + pixels_offset) *
141 samples_per_pixel, 0.0);
142 const uint64_t end_sample = (uint64_t)max((right + pixels_offset) *
143 samples_per_pixel, 0.0);
145 QFontMetrics m(QApplication::font());
146 const int text_height = m.boundingRect(QRect(), 0, "Tg").height();
147 const int annotation_height = (text_height * 5) / 4;
148 const int row_height = (text_height * 6) / 4;
150 assert(_decoder_stack);
151 const QString err = _decoder_stack->error_message();
154 draw_error(p, err, left, right);
155 draw_unresolved_period(p, annotation_height, left, right,
156 samples_per_pixel, pixels_offset);
160 // Iterate through the rows
164 assert(_decoder_stack);
166 const vector<Row> rows(_decoder_stack->get_rows());
167 for (size_t i = 0; i < rows.size(); i++)
169 const Row &row = rows[i];
170 vector<Annotation> annotations;
171 _decoder_stack->get_annotation_subset(annotations, row,
172 start_sample, end_sample);
173 if (!annotations.empty()) {
174 BOOST_FOREACH(const Annotation &a, annotations)
175 draw_annotation(a, p, get_text_colour(),
176 annotation_height, left, right,
177 samples_per_pixel, pixels_offset, y, i);
183 draw_unresolved_period(p, annotation_height, left, right,
184 samples_per_pixel, pixels_offset);
187 void DecodeTrace::populate_popup_form(QWidget *parent, QFormLayout *form)
189 using pv::data::decode::Decoder;
193 assert(_decoder_stack);
195 // Add the standard options
196 Trace::populate_popup_form(parent, form);
198 // Add the decoder options
200 _probe_selectors.clear();
202 const list< shared_ptr<Decoder> >& stack = _decoder_stack->stack();
206 QLabel *const l = new QLabel(
207 tr("<p><i>No decoders in the stack</i></p>"));
208 l->setAlignment(Qt::AlignCenter);
213 list< shared_ptr<Decoder> >::const_iterator iter =
215 for (int i = 0; i < (int)stack.size(); i++, iter++) {
216 shared_ptr<Decoder> dec(*iter);
217 create_decoder_form(i, dec, parent, form);
220 form->addRow(new QLabel(
221 tr("<i>* Required Probes</i>"), parent));
224 // Add stacking button
225 pv::widgets::DecoderMenu *const decoder_menu =
226 new pv::widgets::DecoderMenu(parent);
227 connect(decoder_menu, SIGNAL(decoder_selected(srd_decoder*)),
228 this, SLOT(on_stack_decoder(srd_decoder*)));
230 QPushButton *const stack_button =
231 new QPushButton(tr("Stack Decoder"), parent);
232 stack_button->setMenu(decoder_menu);
234 QHBoxLayout *stack_button_box = new QHBoxLayout;
235 stack_button_box->addWidget(stack_button, 0, Qt::AlignRight);
236 form->addRow(stack_button_box);
239 QMenu* DecodeTrace::create_context_menu(QWidget *parent)
241 QMenu *const menu = Trace::create_context_menu(parent);
243 menu->addSeparator();
245 QAction *const del = new QAction(tr("Delete"), this);
246 del->setShortcuts(QKeySequence::Delete);
247 connect(del, SIGNAL(triggered()), this, SLOT(on_delete()));
248 menu->addAction(del);
253 void DecodeTrace::draw_annotation(const pv::data::decode::Annotation &a, QPainter &p,
254 QColor text_color, int h, int left, int right, double samples_per_pixel,
255 double pixels_offset, int y, unsigned int row_index) const
257 const double start = a.start_sample() / samples_per_pixel -
259 const double end = a.end_sample() / samples_per_pixel -
261 const QColor fill = Colours[((row_index * 2 + a.format()) *
262 (countof(Colours) / 4 + 1)) % countof(Colours)];
263 const QColor outline(fill.darker());
265 if (start > right + DrawPadding || end < left - DrawPadding)
268 if (a.start_sample() == a.end_sample())
269 draw_instant(a, p, fill, outline, text_color, h,
272 draw_range(a, p, fill, outline, text_color, h,
276 void DecodeTrace::draw_instant(const pv::data::decode::Annotation &a, QPainter &p,
277 QColor fill, QColor outline, QColor text_color, int h, double x, int y) const
279 const QString text = a.annotations().empty() ?
280 QString() : a.annotations().back();
281 const double w = min(p.boundingRect(QRectF(), 0, text).width(),
283 const QRectF rect(x - w / 2, y - h / 2, w, h);
287 p.drawRoundedRect(rect, h / 2, h / 2);
289 p.setPen(text_color);
290 p.drawText(rect, Qt::AlignCenter | Qt::AlignVCenter, text);
293 void DecodeTrace::draw_range(const pv::data::decode::Annotation &a, QPainter &p,
294 QColor fill, QColor outline, QColor text_color, int h, double start,
295 double end, int y) const
297 const double top = y + .5 - h / 2;
298 const double bottom = y + .5 + h / 2;
299 const vector<QString> annotations = a.annotations();
304 // If the two ends are within 1 pixel, draw a vertical line
305 if (start + 1.0 > end)
307 p.drawLine(QPointF(start, top), QPointF(start, bottom));
311 const double cap_width = min((end - start) / 4, EndCapWidth);
314 QPointF(start, y + .5f),
315 QPointF(start + cap_width, top),
316 QPointF(end - cap_width, top),
317 QPointF(end, y + .5f),
318 QPointF(end - cap_width, bottom),
319 QPointF(start + cap_width, bottom)
322 p.drawConvexPolygon(pts, countof(pts));
324 if (annotations.empty())
327 QRectF rect(start + cap_width, y - h / 2,
328 end - start - cap_width * 2, h);
329 p.setPen(text_color);
331 // Try to find an annotation that will fit
332 QString best_annotation;
335 BOOST_FOREACH(const QString &a, annotations) {
336 const int w = p.boundingRect(QRectF(), 0, a).width();
337 if (w <= rect.width() && w > best_width)
338 best_annotation = a, best_width = w;
341 if (best_annotation.isEmpty())
342 best_annotation = annotations.back();
344 // If not ellide the last in the list
345 p.drawText(rect, Qt::AlignCenter, p.fontMetrics().elidedText(
346 best_annotation, Qt::ElideRight, rect.width()));
349 void DecodeTrace::draw_error(QPainter &p, const QString &message,
352 const int y = get_y();
354 p.setPen(ErrorBgColour.darker());
355 p.setBrush(ErrorBgColour);
357 const QRectF bounding_rect =
358 QRectF(left, INT_MIN / 2 + y, right - left, INT_MAX);
359 const QRectF text_rect = p.boundingRect(bounding_rect,
360 Qt::AlignCenter, message);
361 const float r = text_rect.height() / 4;
363 p.drawRoundedRect(text_rect.adjusted(-r, -r, r, r), r, r,
366 p.setPen(get_text_colour());
367 p.drawText(text_rect, message);
370 void DecodeTrace::draw_unresolved_period(QPainter &p, int h, int left,
371 int right, double samples_per_pixel, double pixels_offset)
373 using namespace pv::data;
374 using pv::data::decode::Decoder;
376 assert(_decoder_stack);
378 shared_ptr<Logic> data;
379 shared_ptr<LogicSignal> logic_signal;
381 const list< shared_ptr<Decoder> > &stack = _decoder_stack->stack();
383 // We get the logic data of the first probe in the list.
384 // This works because we are currently assuming all
385 // LogicSignals have the same data/snapshot
386 BOOST_FOREACH (const shared_ptr<Decoder> &dec, stack)
387 if (dec && !dec->probes().empty() &&
388 ((logic_signal = (*dec->probes().begin()).second)) &&
389 ((data = logic_signal->logic_data())))
392 if (!data || data->get_snapshots().empty())
395 const shared_ptr<LogicSnapshot> snapshot =
396 data->get_snapshots().front();
398 const int64_t sample_count = (int64_t)snapshot->get_sample_count();
399 if (sample_count == 0)
402 const int64_t samples_decoded = _decoder_stack->samples_decoded();
403 if (sample_count == samples_decoded)
406 const int y = get_y();
407 const double start = max(samples_decoded /
408 samples_per_pixel - pixels_offset, left - 1.0);
409 const double end = min(sample_count / samples_per_pixel -
410 pixels_offset, right + 1.0);
411 const QRectF no_decode_rect(start, y - h/2 + 0.5, end - start, h);
413 p.setPen(QPen(Qt::NoPen));
414 p.setBrush(Qt::white);
415 p.drawRect(no_decode_rect);
417 p.setPen(NoDecodeColour);
418 p.setBrush(QBrush(NoDecodeColour, Qt::Dense6Pattern));
419 p.drawRect(no_decode_rect);
422 void DecodeTrace::create_decoder_form(int index,
423 shared_ptr<data::decode::Decoder> &dec, QWidget *parent,
429 const srd_decoder *const decoder = dec->decoder();
432 pv::widgets::DecoderGroupBox *const group =
433 new pv::widgets::DecoderGroupBox(
434 QString::fromUtf8(decoder->name));
436 _delete_mapper.setMapping(group, index);
437 connect(group, SIGNAL(delete_decoder()), &_delete_mapper, SLOT(map()));
439 QFormLayout *const decoder_form = new QFormLayout;
440 group->add_layout(decoder_form);
442 // Add the mandatory probes
443 for(probe = decoder->probes; probe; probe = probe->next) {
444 const struct srd_probe *const p =
445 (struct srd_probe *)probe->data;
446 QComboBox *const combo = create_probe_selector(parent, dec, p);
447 connect(combo, SIGNAL(currentIndexChanged(int)),
448 this, SLOT(on_probe_selected(int)));
449 decoder_form->addRow(tr("<b>%1</b> (%2) *")
450 .arg(p->name).arg(p->desc), combo);
452 const ProbeSelector s = {combo, dec, p};
453 _probe_selectors.push_back(s);
456 // Add the optional probes
457 for(probe = decoder->opt_probes; probe; probe = probe->next) {
458 const struct srd_probe *const p =
459 (struct srd_probe *)probe->data;
460 QComboBox *const combo = create_probe_selector(parent, dec, p);
461 connect(combo, SIGNAL(currentIndexChanged(int)),
462 this, SLOT(on_probe_selected(int)));
463 decoder_form->addRow(tr("<b>%1</b> (%2)")
464 .arg(p->name).arg(p->desc), combo);
466 const ProbeSelector s = {combo, dec, p};
467 _probe_selectors.push_back(s);
471 shared_ptr<prop::binding::DecoderOptions> binding(
472 new prop::binding::DecoderOptions(_decoder_stack, dec));
473 binding->add_properties_to_form(decoder_form, true);
475 _bindings.push_back(binding);
480 QComboBox* DecodeTrace::create_probe_selector(
481 QWidget *parent, const shared_ptr<data::decode::Decoder> &dec,
482 const srd_probe *const probe)
486 const vector< shared_ptr<Signal> > sigs = _session.get_signals();
488 assert(_decoder_stack);
489 const map<const srd_probe*,
490 shared_ptr<LogicSignal> >::const_iterator probe_iter =
491 dec->probes().find(probe);
493 QComboBox *selector = new QComboBox(parent);
495 selector->addItem("-", qVariantFromValue((void*)NULL));
497 if (probe_iter == dec->probes().end())
498 selector->setCurrentIndex(0);
500 for(size_t i = 0; i < sigs.size(); i++) {
501 const shared_ptr<view::Signal> s(sigs[i]);
504 if (dynamic_pointer_cast<LogicSignal>(s) && s->enabled())
506 selector->addItem(s->get_name(),
507 qVariantFromValue((void*)s.get()));
508 if ((*probe_iter).second == s)
509 selector->setCurrentIndex(i + 1);
516 void DecodeTrace::commit_decoder_probes(shared_ptr<data::decode::Decoder> &dec)
520 map<const srd_probe*, shared_ptr<LogicSignal> > probe_map;
521 const vector< shared_ptr<Signal> > sigs = _session.get_signals();
523 BOOST_FOREACH(const ProbeSelector &s, _probe_selectors)
525 if(s._decoder != dec)
528 const LogicSignal *const selection =
529 (LogicSignal*)s._combo->itemData(
530 s._combo->currentIndex()).value<void*>();
532 BOOST_FOREACH(shared_ptr<Signal> sig, sigs)
533 if(sig.get() == selection) {
534 probe_map[s._probe] =
535 dynamic_pointer_cast<LogicSignal>(sig);
540 dec->set_probes(probe_map);
543 void DecodeTrace::commit_probes()
545 assert(_decoder_stack);
546 BOOST_FOREACH(shared_ptr<data::decode::Decoder> dec,
547 _decoder_stack->stack())
548 commit_decoder_probes(dec);
550 _decoder_stack->begin_decode();
553 void DecodeTrace::on_new_decode_data()
556 _view->update_viewport();
559 void DecodeTrace::delete_pressed()
564 void DecodeTrace::on_delete()
566 _session.remove_decode_signal(this);
569 void DecodeTrace::on_probe_selected(int)
574 void DecodeTrace::on_stack_decoder(srd_decoder *decoder)
577 assert(_decoder_stack);
578 _decoder_stack->push(shared_ptr<data::decode::Decoder>(
579 new data::decode::Decoder(decoder)));
580 _decoder_stack->begin_decode();
585 void DecodeTrace::on_delete_decoder(int index)
587 _decoder_stack->remove(index);
592 _decoder_stack->begin_decode();