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