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