]> sigrok.org Git - pulseview.git/blame - pv/view/decodetrace.cpp
Draw stacked decoder traces below the other PDs (avoids overlap).
[pulseview.git] / pv / view / decodetrace.cpp
CommitLineData
55d3603d
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
21extern "C" {
22#include <libsigrokdecode/libsigrokdecode.h>
23}
24
06bb4e6a
JH
25#include <extdef.h>
26
b213ef09
JH
27#include <boost/foreach.hpp>
28
c51482b3 29#include <QAction>
d7c0ca4a 30#include <QApplication>
4e5a4405
JH
31#include <QComboBox>
32#include <QFormLayout>
33#include <QLabel>
b213ef09 34#include <QMenu>
ce94e4fd 35#include <QPushButton>
c51482b3 36
b9329558 37#include "decodetrace.h"
55d3603d 38
06bb4e6a 39#include <pv/sigsession.h>
6e89374a 40#include <pv/data/decoderstack.h>
7491a29f 41#include <pv/data/decode/decoder.h>
5dfeb70f
JH
42#include <pv/data/logic.h>
43#include <pv/data/logicsnapshot.h>
06e810f2 44#include <pv/data/decode/annotation.h>
4e5a4405 45#include <pv/view/logicsignal.h>
e0fc5810 46#include <pv/view/view.h>
204bae45 47#include <pv/widgets/decodergroupbox.h>
ce94e4fd 48#include <pv/widgets/decodermenu.h>
119aff65 49
819f4c25
JH
50using boost::dynamic_pointer_cast;
51using boost::shared_ptr;
52using std::list;
53using std::max;
54using std::map;
55using std::min;
56using std::vector;
55d3603d
JH
57
58namespace pv {
59namespace view {
60
b9329558 61const QColor DecodeTrace::DecodeColours[4] = {
06bb4e6a
JH
62 QColor(0xEF, 0x29, 0x29), // Red
63 QColor(0xFC, 0xE9, 0x4F), // Yellow
64 QColor(0x8A, 0xE2, 0x34), // Green
65 QColor(0x72, 0x9F, 0xCF) // Blue
66};
67
b9329558 68const QColor DecodeTrace::ErrorBgColour = QColor(0xEF, 0x29, 0x29);
5dfeb70f 69const QColor DecodeTrace::NoDecodeColour = QColor(0x88, 0x8A, 0x85);
ad50ac1a 70
06e810f2
JH
71const double DecodeTrace::EndCapWidth = 5;
72const int DecodeTrace::DrawPadding = 100;
73
74const 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
82};
83
b9329558 84DecodeTrace::DecodeTrace(pv::SigSession &session,
6e89374a 85 boost::shared_ptr<pv::data::DecoderStack> decoder_stack, int index) :
27e8df22
JH
86 Trace(session, QString::fromUtf8(
87 decoder_stack->stack().front()->decoder()->name)),
613d097c
JH
88 _decoder_stack(decoder_stack),
89 _delete_mapper(this)
55d3603d 90{
6e89374a 91 assert(_decoder_stack);
e0fc5810 92
06bb4e6a 93 _colour = DecodeColours[index % countof(DecodeColours)];
9cef9567 94
6e89374a 95 connect(_decoder_stack.get(), SIGNAL(new_decode_data()),
9cef9567 96 this, SLOT(on_new_decode_data()));
613d097c
JH
97 connect(&_delete_mapper, SIGNAL(mapped(int)),
98 this, SLOT(on_delete_decoder(int)));
55d3603d
JH
99}
100
b9329558 101bool DecodeTrace::enabled() const
55d3603d
JH
102{
103 return true;
104}
105
6e89374a 106const boost::shared_ptr<pv::data::DecoderStack>& DecodeTrace::decoder() const
b6b267bb 107{
6e89374a 108 return _decoder_stack;
b6b267bb
JH
109}
110
b9329558 111void DecodeTrace::set_view(pv::view::View *view)
e0fc5810
JH
112{
113 assert(view);
114 Trace::set_view(view);
115}
116
b9329558 117void DecodeTrace::paint_back(QPainter &p, int left, int right)
fe08b6e8 118{
5dfeb70f 119 Trace::paint_back(p, left, right);
fe08b6e8
JH
120 paint_axis(p, get_y(), left, right);
121}
122
b9329558 123void DecodeTrace::paint_mid(QPainter &p, int left, int right)
55d3603d 124{
06e810f2 125 using pv::data::decode::Annotation;
9472f447 126
9472f447
JH
127 const double scale = _view->scale();
128 assert(scale > 0);
129
9d28da5a 130 double samplerate = _decoder_stack->samplerate();
9472f447
JH
131
132 // Show sample rate as 1Hz when it is unknown
133 if (samplerate == 0.0)
134 samplerate = 1.0;
135
136 const double pixels_offset = (_view->offset() -
6e89374a 137 _decoder_stack->get_start_time()) / scale;
9472f447
JH
138 const double samples_per_pixel = samplerate * scale;
139
d7c0ca4a
JH
140 QFontMetrics m(QApplication::font());
141 const int h = (m.boundingRect(QRect(), 0, "Tg").height() * 5) / 4;
5dfeb70f
JH
142
143 assert(_decoder_stack);
144 const QString err = _decoder_stack->error_message();
145 if (!err.isEmpty())
146 {
147 draw_error(p, err, left, right);
148 draw_unresolved_period(p, h, left, right, samples_per_pixel,
149 pixels_offset);
150 return;
151 }
152
153 assert(_view);
154 const int y = get_y();
155
6e89374a 156 assert(_decoder_stack);
db62bbfd 157 vector<Annotation> annotations(_decoder_stack->annotations());
7e674e43
UH
158
159 BOOST_FOREACH(const Annotation &a, annotations) {
bc870bcf
UH
160 // Every stacked PD is 60 pixels further down.
161 int y_stack_offset = a.pd_index() * 60;
162
7e674e43
UH
163 // Every annotation row is 20 pixels further down.
164 int y_ann_row_offset = a.row() * 20;
165
06e810f2 166 draw_annotation(a, p, get_text_colour(), h, left, right,
7e674e43 167 samples_per_pixel, pixels_offset,
bc870bcf 168 y + y_stack_offset + y_ann_row_offset);
7e674e43 169 }
5dfeb70f
JH
170
171 draw_unresolved_period(p, h, left, right,
172 samples_per_pixel, pixels_offset);
55d3603d
JH
173}
174
b9329558 175void DecodeTrace::populate_popup_form(QWidget *parent, QFormLayout *form)
4e5a4405 176{
613d097c
JH
177 using pv::data::decode::Decoder;
178
4e5a4405
JH
179 assert(form);
180 assert(parent);
6e89374a 181 assert(_decoder_stack);
4e5a4405 182
7491a29f 183 // Add the standard options
4e5a4405
JH
184 Trace::populate_popup_form(parent, form);
185
7491a29f
JH
186 // Add the decoder options
187 _bindings.clear();
188 _probe_selectors.clear();
4e5a4405 189
613d097c 190 const list< shared_ptr<Decoder> >& stack = _decoder_stack->stack();
4e5a4405 191
5069084a
JH
192 if (stack.empty())
193 {
194 QLabel *const l = new QLabel(
195 tr("<p><i>No decoders in the stack</i></p>"));
196 l->setAlignment(Qt::AlignCenter);
197 form->addRow(l);
198 }
199 else
200 {
201 list< shared_ptr<Decoder> >::const_iterator iter =
202 stack.begin();
203 for (int i = 0; i < (int)stack.size(); i++, iter++) {
204 shared_ptr<Decoder> dec(*iter);
205 create_decoder_form(i, dec, parent, form);
206 }
207
208 form->addRow(new QLabel(
209 tr("<i>* Required Probes</i>"), parent));
210 }
4e5a4405 211
ce94e4fd 212 // Add stacking button
ce94e4fd
JH
213 pv::widgets::DecoderMenu *const decoder_menu =
214 new pv::widgets::DecoderMenu(parent);
7491a29f
JH
215 connect(decoder_menu, SIGNAL(decoder_selected(srd_decoder*)),
216 this, SLOT(on_stack_decoder(srd_decoder*)));
217
218 QPushButton *const stack_button =
219 new QPushButton(tr("Stack Decoder"), parent);
ce94e4fd
JH
220 stack_button->setMenu(decoder_menu);
221
222 QHBoxLayout *stack_button_box = new QHBoxLayout;
223 stack_button_box->addWidget(stack_button, 0, Qt::AlignRight);
224 form->addRow(stack_button_box);
4e5a4405
JH
225}
226
b9329558 227QMenu* DecodeTrace::create_context_menu(QWidget *parent)
c51482b3
JH
228{
229 QMenu *const menu = Trace::create_context_menu(parent);
230
231 menu->addSeparator();
232
233 QAction *const del = new QAction(tr("Delete"), this);
a2d21018 234 del->setShortcuts(QKeySequence::Delete);
c51482b3
JH
235 connect(del, SIGNAL(triggered()), this, SLOT(on_delete()));
236 menu->addAction(del);
237
238 return menu;
239}
240
06e810f2
JH
241void DecodeTrace::draw_annotation(const pv::data::decode::Annotation &a, QPainter &p,
242 QColor text_color, int h, int left, int right, double samples_per_pixel,
243 double pixels_offset, int y) const
244{
245 const double start = a.start_sample() / samples_per_pixel -
246 pixels_offset;
247 const double end = a.end_sample() / samples_per_pixel -
248 pixels_offset;
249 const QColor fill = Colours[(a.format() * (countof(Colours) / 2 + 1)) %
250 countof(Colours)];
251 const QColor outline(fill.darker());
252
253 if (start > right + DrawPadding || end < left - DrawPadding)
254 return;
255
256 if (a.start_sample() == a.end_sample())
257 draw_instant(a, p, fill, outline, text_color, h,
258 start, y);
259 else
260 draw_range(a, p, fill, outline, text_color, h,
261 start, end, y);
262}
263
264void DecodeTrace::draw_instant(const pv::data::decode::Annotation &a, QPainter &p,
265 QColor fill, QColor outline, QColor text_color, int h, double x, int y) const
266{
267 const QString text = a.annotations().empty() ?
268 QString() : a.annotations().back();
269 const double w = min(p.boundingRect(QRectF(), 0, text).width(),
270 0.0) + h;
271 const QRectF rect(x - w / 2, y - h / 2, w, h);
272
273 p.setPen(outline);
274 p.setBrush(fill);
275 p.drawRoundedRect(rect, h / 2, h / 2);
276
277 p.setPen(text_color);
278 p.drawText(rect, Qt::AlignCenter | Qt::AlignVCenter, text);
279}
280
281void DecodeTrace::draw_range(const pv::data::decode::Annotation &a, QPainter &p,
282 QColor fill, QColor outline, QColor text_color, int h, double start,
283 double end, int y) const
284{
285 const double top = y + .5 - h / 2;
286 const double bottom = y + .5 + h / 2;
287 const vector<QString> annotations = a.annotations();
288
289 p.setPen(outline);
290 p.setBrush(fill);
291
292 // If the two ends are within 1 pixel, draw a vertical line
293 if (start + 1.0 > end)
294 {
295 p.drawLine(QPointF(start, top), QPointF(start, bottom));
296 return;
297 }
298
299 const double cap_width = min((end - start) / 4, EndCapWidth);
300
301 QPointF pts[] = {
302 QPointF(start, y + .5f),
303 QPointF(start + cap_width, top),
304 QPointF(end - cap_width, top),
305 QPointF(end, y + .5f),
306 QPointF(end - cap_width, bottom),
307 QPointF(start + cap_width, bottom)
308 };
309
310 p.drawConvexPolygon(pts, countof(pts));
311
312 if (annotations.empty())
313 return;
314
315 QRectF rect(start + cap_width, y - h / 2,
316 end - start - cap_width * 2, h);
317 p.setPen(text_color);
318
319 // Try to find an annotation that will fit
320 QString best_annotation;
321 int best_width = 0;
322
323 BOOST_FOREACH(const QString &a, annotations) {
324 const int w = p.boundingRect(QRectF(), 0, a).width();
325 if (w <= rect.width() && w > best_width)
326 best_annotation = a, best_width = w;
327 }
328
329 if (best_annotation.isEmpty())
330 best_annotation = annotations.back();
331
332 // If not ellide the last in the list
333 p.drawText(rect, Qt::AlignCenter, p.fontMetrics().elidedText(
334 best_annotation, Qt::ElideRight, rect.width()));
335}
336
b9329558 337void DecodeTrace::draw_error(QPainter &p, const QString &message,
ad50ac1a
JH
338 int left, int right)
339{
340 const int y = get_y();
341
342 p.setPen(ErrorBgColour.darker());
343 p.setBrush(ErrorBgColour);
344
345 const QRectF bounding_rect =
346 QRectF(left, INT_MIN / 2 + y, right - left, INT_MAX);
347 const QRectF text_rect = p.boundingRect(bounding_rect,
348 Qt::AlignCenter, message);
349 const float r = text_rect.height() / 4;
350
351 p.drawRoundedRect(text_rect.adjusted(-r, -r, r, r), r, r,
352 Qt::AbsoluteSize);
353
354 p.setPen(get_text_colour());
355 p.drawText(text_rect, message);
356}
357
5dfeb70f
JH
358void DecodeTrace::draw_unresolved_period(QPainter &p, int h, int left,
359 int right, double samples_per_pixel, double pixels_offset)
360{
361 using namespace pv::data;
362 using pv::data::decode::Decoder;
363
364 assert(_decoder_stack);
365
366 shared_ptr<Logic> data;
367 shared_ptr<LogicSignal> logic_signal;
368
369 const list< shared_ptr<Decoder> > &stack = _decoder_stack->stack();
370
371 // We get the logic data of the first probe in the list.
372 // This works because we are currently assuming all
373 // LogicSignals have the same data/snapshot
374 BOOST_FOREACH (const shared_ptr<Decoder> &dec, stack)
375 if (dec && !dec->probes().empty() &&
376 ((logic_signal = (*dec->probes().begin()).second)) &&
7aa09b00 377 ((data = logic_signal->logic_data())))
5dfeb70f
JH
378 break;
379
380 if (!data || data->get_snapshots().empty())
381 return;
382
383 const shared_ptr<LogicSnapshot> snapshot =
384 data->get_snapshots().front();
385 assert(snapshot);
386 const int64_t sample_count = (int64_t)snapshot->get_sample_count();
387 if (sample_count == 0)
388 return;
389
390 const int64_t samples_decoded = _decoder_stack->samples_decoded();
391 if (sample_count == samples_decoded)
392 return;
393
394 const int y = get_y();
395 const double start = max(samples_decoded /
396 samples_per_pixel - pixels_offset, left - 1.0);
397 const double end = min(sample_count / samples_per_pixel -
398 pixels_offset, right + 1.0);
399 const QRectF no_decode_rect(start, y - h/2 + 0.5, end - start, h);
400
401 p.setPen(QPen(Qt::NoPen));
402 p.setBrush(Qt::white);
403 p.drawRect(no_decode_rect);
404
405 p.setPen(NoDecodeColour);
406 p.setBrush(QBrush(NoDecodeColour, Qt::Dense6Pattern));
407 p.drawRect(no_decode_rect);
408}
409
613d097c
JH
410void DecodeTrace::create_decoder_form(int index,
411 shared_ptr<data::decode::Decoder> &dec, QWidget *parent,
412 QFormLayout *form)
7491a29f
JH
413{
414 const GSList *probe;
415
416 assert(dec);
417 const srd_decoder *const decoder = dec->decoder();
418 assert(decoder);
419
204bae45 420 pv::widgets::DecoderGroupBox *const group =
27e8df22
JH
421 new pv::widgets::DecoderGroupBox(
422 QString::fromUtf8(decoder->name));
613d097c
JH
423
424 _delete_mapper.setMapping(group, index);
425 connect(group, SIGNAL(delete_decoder()), &_delete_mapper, SLOT(map()));
426
204bae45
JH
427 QFormLayout *const decoder_form = new QFormLayout;
428 group->add_layout(decoder_form);
7491a29f
JH
429
430 // Add the mandatory probes
431 for(probe = decoder->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)));
204bae45 437 decoder_form->addRow(tr("<b>%1</b> (%2) *")
7491a29f
JH
438 .arg(p->name).arg(p->desc), combo);
439
440 const ProbeSelector s = {combo, dec, p};
441 _probe_selectors.push_back(s);
442 }
443
444 // Add the optional probes
445 for(probe = decoder->opt_probes; probe; probe = probe->next) {
446 const struct srd_probe *const p =
447 (struct srd_probe *)probe->data;
448 QComboBox *const combo = create_probe_selector(parent, dec, p);
449 connect(combo, SIGNAL(currentIndexChanged(int)),
450 this, SLOT(on_probe_selected(int)));
204bae45 451 decoder_form->addRow(tr("<b>%1</b> (%2)")
7491a29f
JH
452 .arg(p->name).arg(p->desc), combo);
453
454 const ProbeSelector s = {combo, dec, p};
455 _probe_selectors.push_back(s);
456 }
457
458 // Add the options
459 shared_ptr<prop::binding::DecoderOptions> binding(
460 new prop::binding::DecoderOptions(_decoder_stack, dec));
204bae45 461 binding->add_properties_to_form(decoder_form, true);
7491a29f
JH
462
463 _bindings.push_back(binding);
204bae45
JH
464
465 form->addRow(group);
7491a29f
JH
466}
467
b9329558 468QComboBox* DecodeTrace::create_probe_selector(
7491a29f
JH
469 QWidget *parent, const shared_ptr<data::decode::Decoder> &dec,
470 const srd_probe *const probe)
4e5a4405 471{
7491a29f
JH
472 assert(dec);
473
4e5a4405
JH
474 const vector< shared_ptr<Signal> > sigs = _session.get_signals();
475
6e89374a 476 assert(_decoder_stack);
4e5a4405
JH
477 const map<const srd_probe*,
478 shared_ptr<LogicSignal> >::const_iterator probe_iter =
7491a29f 479 dec->probes().find(probe);
4e5a4405
JH
480
481 QComboBox *selector = new QComboBox(parent);
482
483 selector->addItem("-", qVariantFromValue((void*)NULL));
484
7491a29f 485 if (probe_iter == dec->probes().end())
4e5a4405
JH
486 selector->setCurrentIndex(0);
487
488 for(size_t i = 0; i < sigs.size(); i++) {
489 const shared_ptr<view::Signal> s(sigs[i]);
490 assert(s);
491
492 if (dynamic_pointer_cast<LogicSignal>(s) && s->enabled())
493 {
494 selector->addItem(s->get_name(),
495 qVariantFromValue((void*)s.get()));
496 if ((*probe_iter).second == s)
497 selector->setCurrentIndex(i + 1);
498 }
499 }
500
501 return selector;
502}
503
7491a29f 504void DecodeTrace::commit_decoder_probes(shared_ptr<data::decode::Decoder> &dec)
4e5a4405 505{
7491a29f 506 assert(dec);
4e5a4405
JH
507
508 map<const srd_probe*, shared_ptr<LogicSignal> > probe_map;
509 const vector< shared_ptr<Signal> > sigs = _session.get_signals();
510
7491a29f 511 BOOST_FOREACH(const ProbeSelector &s, _probe_selectors)
4e5a4405 512 {
7491a29f
JH
513 if(s._decoder != dec)
514 break;
515
4e5a4405 516 const LogicSignal *const selection =
7491a29f
JH
517 (LogicSignal*)s._combo->itemData(
518 s._combo->currentIndex()).value<void*>();
4e5a4405 519
7491a29f
JH
520 BOOST_FOREACH(shared_ptr<Signal> sig, sigs)
521 if(sig.get() == selection) {
522 probe_map[s._probe] =
523 dynamic_pointer_cast<LogicSignal>(sig);
4e5a4405
JH
524 break;
525 }
526 }
527
7491a29f
JH
528 dec->set_probes(probe_map);
529}
530
531void DecodeTrace::commit_probes()
532{
533 assert(_decoder_stack);
534 BOOST_FOREACH(shared_ptr<data::decode::Decoder> dec,
535 _decoder_stack->stack())
536 commit_decoder_probes(dec);
537
538 _decoder_stack->begin_decode();
4e5a4405
JH
539}
540
b9329558 541void DecodeTrace::on_new_decode_data()
9cef9567
JH
542{
543 if (_view)
544 _view->update_viewport();
545}
546
b9329558 547void DecodeTrace::delete_pressed()
5ed1adf5
JH
548{
549 on_delete();
550}
551
b9329558 552void DecodeTrace::on_delete()
c51482b3
JH
553{
554 _session.remove_decode_signal(this);
555}
556
b9329558 557void DecodeTrace::on_probe_selected(int)
4e5a4405
JH
558{
559 commit_probes();
560}
561
7491a29f
JH
562void DecodeTrace::on_stack_decoder(srd_decoder *decoder)
563{
564 assert(decoder);
565 assert(_decoder_stack);
566 _decoder_stack->push(shared_ptr<data::decode::Decoder>(
567 new data::decode::Decoder(decoder)));
568 _decoder_stack->begin_decode();
37fd11b1
JH
569
570 create_popup_form();
7491a29f
JH
571}
572
613d097c
JH
573void DecodeTrace::on_delete_decoder(int index)
574{
575 _decoder_stack->remove(index);
576
577 // Update the popup
578 create_popup_form();
579
580 _decoder_stack->begin_decode();
581}
582
55d3603d
JH
583} // namespace view
584} // namespace pv