]> sigrok.org Git - pulseview.git/blame - pv/view/decodetrace.cpp
Handle C-strings as UTF-8
[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
55d3603d
JH
50using namespace boost;
51using namespace std;
52
53namespace pv {
54namespace view {
55
b9329558 56const QColor DecodeTrace::DecodeColours[4] = {
06bb4e6a
JH
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
b9329558 63const QColor DecodeTrace::ErrorBgColour = QColor(0xEF, 0x29, 0x29);
5dfeb70f 64const QColor DecodeTrace::NoDecodeColour = QColor(0x88, 0x8A, 0x85);
ad50ac1a 65
06e810f2
JH
66const double DecodeTrace::EndCapWidth = 5;
67const int DecodeTrace::DrawPadding = 100;
68
69const 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
b9329558 79DecodeTrace::DecodeTrace(pv::SigSession &session,
6e89374a 80 boost::shared_ptr<pv::data::DecoderStack> decoder_stack, int index) :
27e8df22
JH
81 Trace(session, QString::fromUtf8(
82 decoder_stack->stack().front()->decoder()->name)),
613d097c
JH
83 _decoder_stack(decoder_stack),
84 _delete_mapper(this)
55d3603d 85{
6e89374a 86 assert(_decoder_stack);
e0fc5810 87
06bb4e6a 88 _colour = DecodeColours[index % countof(DecodeColours)];
9cef9567 89
6e89374a 90 connect(_decoder_stack.get(), SIGNAL(new_decode_data()),
9cef9567 91 this, SLOT(on_new_decode_data()));
613d097c
JH
92 connect(&_delete_mapper, SIGNAL(mapped(int)),
93 this, SLOT(on_delete_decoder(int)));
55d3603d
JH
94}
95
b9329558 96bool DecodeTrace::enabled() const
55d3603d
JH
97{
98 return true;
99}
100
6e89374a 101const boost::shared_ptr<pv::data::DecoderStack>& DecodeTrace::decoder() const
b6b267bb 102{
6e89374a 103 return _decoder_stack;
b6b267bb
JH
104}
105
b9329558 106void DecodeTrace::set_view(pv::view::View *view)
e0fc5810
JH
107{
108 assert(view);
109 Trace::set_view(view);
110}
111
b9329558 112void DecodeTrace::paint_back(QPainter &p, int left, int right)
fe08b6e8 113{
5dfeb70f 114 Trace::paint_back(p, left, right);
fe08b6e8
JH
115 paint_axis(p, get_y(), left, right);
116}
117
b9329558 118void DecodeTrace::paint_mid(QPainter &p, int left, int right)
55d3603d 119{
06e810f2 120 using pv::data::decode::Annotation;
9472f447 121
9472f447
JH
122 const double scale = _view->scale();
123 assert(scale > 0);
124
9d28da5a 125 double samplerate = _decoder_stack->samplerate();
9472f447
JH
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() -
6e89374a 132 _decoder_stack->get_start_time()) / scale;
9472f447
JH
133 const double samples_per_pixel = samplerate * scale;
134
d7c0ca4a
JH
135 QFontMetrics m(QApplication::font());
136 const int h = (m.boundingRect(QRect(), 0, "Tg").height() * 5) / 4;
5dfeb70f
JH
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
6e89374a 151 assert(_decoder_stack);
db62bbfd
JH
152 vector<Annotation> annotations(_decoder_stack->annotations());
153 BOOST_FOREACH(const Annotation &a, annotations)
06e810f2 154 draw_annotation(a, p, get_text_colour(), h, left, right,
5dfeb70f 155 samples_per_pixel, pixels_offset, y);
5dfeb70f
JH
156
157 draw_unresolved_period(p, h, left, right,
158 samples_per_pixel, pixels_offset);
55d3603d
JH
159}
160
b9329558 161void DecodeTrace::populate_popup_form(QWidget *parent, QFormLayout *form)
4e5a4405 162{
613d097c
JH
163 using pv::data::decode::Decoder;
164
4e5a4405
JH
165 assert(form);
166 assert(parent);
6e89374a 167 assert(_decoder_stack);
4e5a4405 168
7491a29f 169 // Add the standard options
4e5a4405
JH
170 Trace::populate_popup_form(parent, form);
171
7491a29f
JH
172 // Add the decoder options
173 _bindings.clear();
174 _probe_selectors.clear();
4e5a4405 175
613d097c 176 const list< shared_ptr<Decoder> >& stack = _decoder_stack->stack();
4e5a4405 177
5069084a
JH
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 }
4e5a4405 197
ce94e4fd 198 // Add stacking button
ce94e4fd
JH
199 pv::widgets::DecoderMenu *const decoder_menu =
200 new pv::widgets::DecoderMenu(parent);
7491a29f
JH
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);
ce94e4fd
JH
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);
4e5a4405
JH
211}
212
b9329558 213QMenu* DecodeTrace::create_context_menu(QWidget *parent)
c51482b3
JH
214{
215 QMenu *const menu = Trace::create_context_menu(parent);
216
217 menu->addSeparator();
218
219 QAction *const del = new QAction(tr("Delete"), this);
a2d21018 220 del->setShortcuts(QKeySequence::Delete);
c51482b3
JH
221 connect(del, SIGNAL(triggered()), this, SLOT(on_delete()));
222 menu->addAction(del);
223
224 return menu;
225}
226
06e810f2
JH
227void 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
250void 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
267void 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
b9329558 323void DecodeTrace::draw_error(QPainter &p, const QString &message,
ad50ac1a
JH
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
5dfeb70f
JH
344void 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)) &&
7aa09b00 363 ((data = logic_signal->logic_data())))
5dfeb70f
JH
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
613d097c
JH
396void DecodeTrace::create_decoder_form(int index,
397 shared_ptr<data::decode::Decoder> &dec, QWidget *parent,
398 QFormLayout *form)
7491a29f
JH
399{
400 const GSList *probe;
401
402 assert(dec);
403 const srd_decoder *const decoder = dec->decoder();
404 assert(decoder);
405
204bae45 406 pv::widgets::DecoderGroupBox *const group =
27e8df22
JH
407 new pv::widgets::DecoderGroupBox(
408 QString::fromUtf8(decoder->name));
613d097c
JH
409
410 _delete_mapper.setMapping(group, index);
411 connect(group, SIGNAL(delete_decoder()), &_delete_mapper, SLOT(map()));
412
204bae45
JH
413 QFormLayout *const decoder_form = new QFormLayout;
414 group->add_layout(decoder_form);
7491a29f
JH
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)));
204bae45 423 decoder_form->addRow(tr("<b>%1</b> (%2) *")
7491a29f
JH
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)));
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 options
445 shared_ptr<prop::binding::DecoderOptions> binding(
446 new prop::binding::DecoderOptions(_decoder_stack, dec));
204bae45 447 binding->add_properties_to_form(decoder_form, true);
7491a29f
JH
448
449 _bindings.push_back(binding);
204bae45
JH
450
451 form->addRow(group);
7491a29f
JH
452}
453
b9329558 454QComboBox* DecodeTrace::create_probe_selector(
7491a29f
JH
455 QWidget *parent, const shared_ptr<data::decode::Decoder> &dec,
456 const srd_probe *const probe)
4e5a4405 457{
7491a29f
JH
458 assert(dec);
459
4e5a4405
JH
460 const vector< shared_ptr<Signal> > sigs = _session.get_signals();
461
6e89374a 462 assert(_decoder_stack);
4e5a4405
JH
463 const map<const srd_probe*,
464 shared_ptr<LogicSignal> >::const_iterator probe_iter =
7491a29f 465 dec->probes().find(probe);
4e5a4405
JH
466
467 QComboBox *selector = new QComboBox(parent);
468
469 selector->addItem("-", qVariantFromValue((void*)NULL));
470
7491a29f 471 if (probe_iter == dec->probes().end())
4e5a4405
JH
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
7491a29f 490void DecodeTrace::commit_decoder_probes(shared_ptr<data::decode::Decoder> &dec)
4e5a4405 491{
7491a29f 492 assert(dec);
4e5a4405
JH
493
494 map<const srd_probe*, shared_ptr<LogicSignal> > probe_map;
495 const vector< shared_ptr<Signal> > sigs = _session.get_signals();
496
7491a29f 497 BOOST_FOREACH(const ProbeSelector &s, _probe_selectors)
4e5a4405 498 {
7491a29f
JH
499 if(s._decoder != dec)
500 break;
501
4e5a4405 502 const LogicSignal *const selection =
7491a29f
JH
503 (LogicSignal*)s._combo->itemData(
504 s._combo->currentIndex()).value<void*>();
4e5a4405 505
7491a29f
JH
506 BOOST_FOREACH(shared_ptr<Signal> sig, sigs)
507 if(sig.get() == selection) {
508 probe_map[s._probe] =
509 dynamic_pointer_cast<LogicSignal>(sig);
4e5a4405
JH
510 break;
511 }
512 }
513
7491a29f
JH
514 dec->set_probes(probe_map);
515}
516
517void 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();
4e5a4405
JH
525}
526
b9329558 527void DecodeTrace::on_new_decode_data()
9cef9567
JH
528{
529 if (_view)
530 _view->update_viewport();
531}
532
b9329558 533void DecodeTrace::delete_pressed()
5ed1adf5
JH
534{
535 on_delete();
536}
537
b9329558 538void DecodeTrace::on_delete()
c51482b3
JH
539{
540 _session.remove_decode_signal(this);
541}
542
b9329558 543void DecodeTrace::on_probe_selected(int)
4e5a4405
JH
544{
545 commit_probes();
546}
547
7491a29f
JH
548void 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();
37fd11b1
JH
555
556 create_popup_form();
7491a29f
JH
557}
558
613d097c
JH
559void 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
55d3603d
JH
569} // namespace view
570} // namespace pv