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