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