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