]>
Commit | Line | Data |
---|---|---|
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 | ||
21 | extern "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> | |
4e5a4405 | 44 | #include <pv/view/logicsignal.h> |
e0fc5810 | 45 | #include <pv/view/view.h> |
9472f447 | 46 | #include <pv/view/decode/annotation.h> |
204bae45 | 47 | #include <pv/widgets/decodergroupbox.h> |
ce94e4fd | 48 | #include <pv/widgets/decodermenu.h> |
119aff65 | 49 | |
55d3603d JH |
50 | using namespace boost; |
51 | using namespace std; | |
52 | ||
53 | namespace pv { | |
54 | namespace view { | |
55 | ||
b9329558 | 56 | const 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 | 63 | const QColor DecodeTrace::ErrorBgColour = QColor(0xEF, 0x29, 0x29); |
5dfeb70f | 64 | const QColor DecodeTrace::NoDecodeColour = QColor(0x88, 0x8A, 0x85); |
ad50ac1a | 65 | |
b9329558 | 66 | DecodeTrace::DecodeTrace(pv::SigSession &session, |
6e89374a | 67 | boost::shared_ptr<pv::data::DecoderStack> decoder_stack, int index) : |
7491a29f | 68 | Trace(session, QString(decoder_stack->stack().front()->decoder()->name)), |
613d097c JH |
69 | _decoder_stack(decoder_stack), |
70 | _delete_mapper(this) | |
55d3603d | 71 | { |
6e89374a | 72 | assert(_decoder_stack); |
e0fc5810 | 73 | |
06bb4e6a | 74 | _colour = DecodeColours[index % countof(DecodeColours)]; |
9cef9567 | 75 | |
6e89374a | 76 | connect(_decoder_stack.get(), SIGNAL(new_decode_data()), |
9cef9567 | 77 | this, SLOT(on_new_decode_data())); |
613d097c JH |
78 | connect(&_delete_mapper, SIGNAL(mapped(int)), |
79 | this, SLOT(on_delete_decoder(int))); | |
55d3603d JH |
80 | } |
81 | ||
b9329558 | 82 | bool DecodeTrace::enabled() const |
55d3603d JH |
83 | { |
84 | return true; | |
85 | } | |
86 | ||
6e89374a | 87 | const boost::shared_ptr<pv::data::DecoderStack>& DecodeTrace::decoder() const |
b6b267bb | 88 | { |
6e89374a | 89 | return _decoder_stack; |
b6b267bb JH |
90 | } |
91 | ||
b9329558 | 92 | void DecodeTrace::set_view(pv::view::View *view) |
e0fc5810 JH |
93 | { |
94 | assert(view); | |
95 | Trace::set_view(view); | |
96 | } | |
97 | ||
b9329558 | 98 | void DecodeTrace::paint_back(QPainter &p, int left, int right) |
fe08b6e8 | 99 | { |
5dfeb70f | 100 | Trace::paint_back(p, left, right); |
fe08b6e8 JH |
101 | paint_axis(p, get_y(), left, right); |
102 | } | |
103 | ||
b9329558 | 104 | void DecodeTrace::paint_mid(QPainter &p, int left, int right) |
55d3603d | 105 | { |
9472f447 JH |
106 | using namespace pv::view::decode; |
107 | ||
9472f447 JH |
108 | const double scale = _view->scale(); |
109 | assert(scale > 0); | |
110 | ||
9d28da5a | 111 | double samplerate = _decoder_stack->samplerate(); |
9472f447 JH |
112 | |
113 | // Show sample rate as 1Hz when it is unknown | |
114 | if (samplerate == 0.0) | |
115 | samplerate = 1.0; | |
116 | ||
117 | const double pixels_offset = (_view->offset() - | |
6e89374a | 118 | _decoder_stack->get_start_time()) / scale; |
9472f447 JH |
119 | const double samples_per_pixel = samplerate * scale; |
120 | ||
d7c0ca4a JH |
121 | QFontMetrics m(QApplication::font()); |
122 | const int h = (m.boundingRect(QRect(), 0, "Tg").height() * 5) / 4; | |
5dfeb70f JH |
123 | |
124 | assert(_decoder_stack); | |
125 | const QString err = _decoder_stack->error_message(); | |
126 | if (!err.isEmpty()) | |
127 | { | |
128 | draw_error(p, err, left, right); | |
129 | draw_unresolved_period(p, h, left, right, samples_per_pixel, | |
130 | pixels_offset); | |
131 | return; | |
132 | } | |
133 | ||
134 | assert(_view); | |
135 | const int y = get_y(); | |
136 | ||
6e89374a JH |
137 | assert(_decoder_stack); |
138 | vector< shared_ptr<Annotation> > annotations(_decoder_stack->annotations()); | |
9472f447 JH |
139 | BOOST_FOREACH(shared_ptr<Annotation> a, annotations) { |
140 | assert(a); | |
5dfeb70f JH |
141 | a->paint(p, get_text_colour(), h, left, right, |
142 | samples_per_pixel, pixels_offset, y); | |
9472f447 | 143 | } |
5dfeb70f JH |
144 | |
145 | draw_unresolved_period(p, h, left, right, | |
146 | samples_per_pixel, pixels_offset); | |
55d3603d JH |
147 | } |
148 | ||
b9329558 | 149 | void DecodeTrace::populate_popup_form(QWidget *parent, QFormLayout *form) |
4e5a4405 | 150 | { |
613d097c JH |
151 | using pv::data::decode::Decoder; |
152 | ||
4e5a4405 JH |
153 | assert(form); |
154 | assert(parent); | |
6e89374a | 155 | assert(_decoder_stack); |
4e5a4405 | 156 | |
7491a29f | 157 | // Add the standard options |
4e5a4405 JH |
158 | Trace::populate_popup_form(parent, form); |
159 | ||
7491a29f JH |
160 | // Add the decoder options |
161 | _bindings.clear(); | |
162 | _probe_selectors.clear(); | |
4e5a4405 | 163 | |
613d097c | 164 | const list< shared_ptr<Decoder> >& stack = _decoder_stack->stack(); |
4e5a4405 | 165 | |
5069084a JH |
166 | if (stack.empty()) |
167 | { | |
168 | QLabel *const l = new QLabel( | |
169 | tr("<p><i>No decoders in the stack</i></p>")); | |
170 | l->setAlignment(Qt::AlignCenter); | |
171 | form->addRow(l); | |
172 | } | |
173 | else | |
174 | { | |
175 | list< shared_ptr<Decoder> >::const_iterator iter = | |
176 | stack.begin(); | |
177 | for (int i = 0; i < (int)stack.size(); i++, iter++) { | |
178 | shared_ptr<Decoder> dec(*iter); | |
179 | create_decoder_form(i, dec, parent, form); | |
180 | } | |
181 | ||
182 | form->addRow(new QLabel( | |
183 | tr("<i>* Required Probes</i>"), parent)); | |
184 | } | |
4e5a4405 | 185 | |
ce94e4fd | 186 | // Add stacking button |
ce94e4fd JH |
187 | pv::widgets::DecoderMenu *const decoder_menu = |
188 | new pv::widgets::DecoderMenu(parent); | |
7491a29f JH |
189 | connect(decoder_menu, SIGNAL(decoder_selected(srd_decoder*)), |
190 | this, SLOT(on_stack_decoder(srd_decoder*))); | |
191 | ||
192 | QPushButton *const stack_button = | |
193 | new QPushButton(tr("Stack Decoder"), parent); | |
ce94e4fd JH |
194 | stack_button->setMenu(decoder_menu); |
195 | ||
196 | QHBoxLayout *stack_button_box = new QHBoxLayout; | |
197 | stack_button_box->addWidget(stack_button, 0, Qt::AlignRight); | |
198 | form->addRow(stack_button_box); | |
4e5a4405 JH |
199 | } |
200 | ||
b9329558 | 201 | QMenu* DecodeTrace::create_context_menu(QWidget *parent) |
c51482b3 JH |
202 | { |
203 | QMenu *const menu = Trace::create_context_menu(parent); | |
204 | ||
205 | menu->addSeparator(); | |
206 | ||
207 | QAction *const del = new QAction(tr("Delete"), this); | |
a2d21018 | 208 | del->setShortcuts(QKeySequence::Delete); |
c51482b3 JH |
209 | connect(del, SIGNAL(triggered()), this, SLOT(on_delete())); |
210 | menu->addAction(del); | |
211 | ||
212 | return menu; | |
213 | } | |
214 | ||
b9329558 | 215 | void DecodeTrace::draw_error(QPainter &p, const QString &message, |
ad50ac1a JH |
216 | int left, int right) |
217 | { | |
218 | const int y = get_y(); | |
219 | ||
220 | p.setPen(ErrorBgColour.darker()); | |
221 | p.setBrush(ErrorBgColour); | |
222 | ||
223 | const QRectF bounding_rect = | |
224 | QRectF(left, INT_MIN / 2 + y, right - left, INT_MAX); | |
225 | const QRectF text_rect = p.boundingRect(bounding_rect, | |
226 | Qt::AlignCenter, message); | |
227 | const float r = text_rect.height() / 4; | |
228 | ||
229 | p.drawRoundedRect(text_rect.adjusted(-r, -r, r, r), r, r, | |
230 | Qt::AbsoluteSize); | |
231 | ||
232 | p.setPen(get_text_colour()); | |
233 | p.drawText(text_rect, message); | |
234 | } | |
235 | ||
5dfeb70f JH |
236 | void DecodeTrace::draw_unresolved_period(QPainter &p, int h, int left, |
237 | int right, double samples_per_pixel, double pixels_offset) | |
238 | { | |
239 | using namespace pv::data; | |
240 | using pv::data::decode::Decoder; | |
241 | ||
242 | assert(_decoder_stack); | |
243 | ||
244 | shared_ptr<Logic> data; | |
245 | shared_ptr<LogicSignal> logic_signal; | |
246 | ||
247 | const list< shared_ptr<Decoder> > &stack = _decoder_stack->stack(); | |
248 | ||
249 | // We get the logic data of the first probe in the list. | |
250 | // This works because we are currently assuming all | |
251 | // LogicSignals have the same data/snapshot | |
252 | BOOST_FOREACH (const shared_ptr<Decoder> &dec, stack) | |
253 | if (dec && !dec->probes().empty() && | |
254 | ((logic_signal = (*dec->probes().begin()).second)) && | |
7aa09b00 | 255 | ((data = logic_signal->logic_data()))) |
5dfeb70f JH |
256 | break; |
257 | ||
258 | if (!data || data->get_snapshots().empty()) | |
259 | return; | |
260 | ||
261 | const shared_ptr<LogicSnapshot> snapshot = | |
262 | data->get_snapshots().front(); | |
263 | assert(snapshot); | |
264 | const int64_t sample_count = (int64_t)snapshot->get_sample_count(); | |
265 | if (sample_count == 0) | |
266 | return; | |
267 | ||
268 | const int64_t samples_decoded = _decoder_stack->samples_decoded(); | |
269 | if (sample_count == samples_decoded) | |
270 | return; | |
271 | ||
272 | const int y = get_y(); | |
273 | const double start = max(samples_decoded / | |
274 | samples_per_pixel - pixels_offset, left - 1.0); | |
275 | const double end = min(sample_count / samples_per_pixel - | |
276 | pixels_offset, right + 1.0); | |
277 | const QRectF no_decode_rect(start, y - h/2 + 0.5, end - start, h); | |
278 | ||
279 | p.setPen(QPen(Qt::NoPen)); | |
280 | p.setBrush(Qt::white); | |
281 | p.drawRect(no_decode_rect); | |
282 | ||
283 | p.setPen(NoDecodeColour); | |
284 | p.setBrush(QBrush(NoDecodeColour, Qt::Dense6Pattern)); | |
285 | p.drawRect(no_decode_rect); | |
286 | } | |
287 | ||
613d097c JH |
288 | void DecodeTrace::create_decoder_form(int index, |
289 | shared_ptr<data::decode::Decoder> &dec, QWidget *parent, | |
290 | QFormLayout *form) | |
7491a29f JH |
291 | { |
292 | const GSList *probe; | |
293 | ||
294 | assert(dec); | |
295 | const srd_decoder *const decoder = dec->decoder(); | |
296 | assert(decoder); | |
297 | ||
204bae45 JH |
298 | pv::widgets::DecoderGroupBox *const group = |
299 | new pv::widgets::DecoderGroupBox(decoder->name); | |
613d097c JH |
300 | |
301 | _delete_mapper.setMapping(group, index); | |
302 | connect(group, SIGNAL(delete_decoder()), &_delete_mapper, SLOT(map())); | |
303 | ||
204bae45 JH |
304 | QFormLayout *const decoder_form = new QFormLayout; |
305 | group->add_layout(decoder_form); | |
7491a29f JH |
306 | |
307 | // Add the mandatory probes | |
308 | for(probe = decoder->probes; probe; probe = probe->next) { | |
309 | const struct srd_probe *const p = | |
310 | (struct srd_probe *)probe->data; | |
311 | QComboBox *const combo = create_probe_selector(parent, dec, p); | |
312 | connect(combo, SIGNAL(currentIndexChanged(int)), | |
313 | this, SLOT(on_probe_selected(int))); | |
204bae45 | 314 | decoder_form->addRow(tr("<b>%1</b> (%2) *") |
7491a29f JH |
315 | .arg(p->name).arg(p->desc), combo); |
316 | ||
317 | const ProbeSelector s = {combo, dec, p}; | |
318 | _probe_selectors.push_back(s); | |
319 | } | |
320 | ||
321 | // Add the optional probes | |
322 | for(probe = decoder->opt_probes; probe; probe = probe->next) { | |
323 | const struct srd_probe *const p = | |
324 | (struct srd_probe *)probe->data; | |
325 | QComboBox *const combo = create_probe_selector(parent, dec, p); | |
326 | connect(combo, SIGNAL(currentIndexChanged(int)), | |
327 | this, SLOT(on_probe_selected(int))); | |
204bae45 | 328 | decoder_form->addRow(tr("<b>%1</b> (%2)") |
7491a29f JH |
329 | .arg(p->name).arg(p->desc), combo); |
330 | ||
331 | const ProbeSelector s = {combo, dec, p}; | |
332 | _probe_selectors.push_back(s); | |
333 | } | |
334 | ||
335 | // Add the options | |
336 | shared_ptr<prop::binding::DecoderOptions> binding( | |
337 | new prop::binding::DecoderOptions(_decoder_stack, dec)); | |
204bae45 | 338 | binding->add_properties_to_form(decoder_form, true); |
7491a29f JH |
339 | |
340 | _bindings.push_back(binding); | |
204bae45 JH |
341 | |
342 | form->addRow(group); | |
7491a29f JH |
343 | } |
344 | ||
b9329558 | 345 | QComboBox* DecodeTrace::create_probe_selector( |
7491a29f JH |
346 | QWidget *parent, const shared_ptr<data::decode::Decoder> &dec, |
347 | const srd_probe *const probe) | |
4e5a4405 | 348 | { |
7491a29f JH |
349 | assert(dec); |
350 | ||
4e5a4405 JH |
351 | const vector< shared_ptr<Signal> > sigs = _session.get_signals(); |
352 | ||
6e89374a | 353 | assert(_decoder_stack); |
4e5a4405 JH |
354 | const map<const srd_probe*, |
355 | shared_ptr<LogicSignal> >::const_iterator probe_iter = | |
7491a29f | 356 | dec->probes().find(probe); |
4e5a4405 JH |
357 | |
358 | QComboBox *selector = new QComboBox(parent); | |
359 | ||
360 | selector->addItem("-", qVariantFromValue((void*)NULL)); | |
361 | ||
7491a29f | 362 | if (probe_iter == dec->probes().end()) |
4e5a4405 JH |
363 | selector->setCurrentIndex(0); |
364 | ||
365 | for(size_t i = 0; i < sigs.size(); i++) { | |
366 | const shared_ptr<view::Signal> s(sigs[i]); | |
367 | assert(s); | |
368 | ||
369 | if (dynamic_pointer_cast<LogicSignal>(s) && s->enabled()) | |
370 | { | |
371 | selector->addItem(s->get_name(), | |
372 | qVariantFromValue((void*)s.get())); | |
373 | if ((*probe_iter).second == s) | |
374 | selector->setCurrentIndex(i + 1); | |
375 | } | |
376 | } | |
377 | ||
378 | return selector; | |
379 | } | |
380 | ||
7491a29f | 381 | void DecodeTrace::commit_decoder_probes(shared_ptr<data::decode::Decoder> &dec) |
4e5a4405 | 382 | { |
7491a29f | 383 | assert(dec); |
4e5a4405 JH |
384 | |
385 | map<const srd_probe*, shared_ptr<LogicSignal> > probe_map; | |
386 | const vector< shared_ptr<Signal> > sigs = _session.get_signals(); | |
387 | ||
7491a29f | 388 | BOOST_FOREACH(const ProbeSelector &s, _probe_selectors) |
4e5a4405 | 389 | { |
7491a29f JH |
390 | if(s._decoder != dec) |
391 | break; | |
392 | ||
4e5a4405 | 393 | const LogicSignal *const selection = |
7491a29f JH |
394 | (LogicSignal*)s._combo->itemData( |
395 | s._combo->currentIndex()).value<void*>(); | |
4e5a4405 | 396 | |
7491a29f JH |
397 | BOOST_FOREACH(shared_ptr<Signal> sig, sigs) |
398 | if(sig.get() == selection) { | |
399 | probe_map[s._probe] = | |
400 | dynamic_pointer_cast<LogicSignal>(sig); | |
4e5a4405 JH |
401 | break; |
402 | } | |
403 | } | |
404 | ||
7491a29f JH |
405 | dec->set_probes(probe_map); |
406 | } | |
407 | ||
408 | void DecodeTrace::commit_probes() | |
409 | { | |
410 | assert(_decoder_stack); | |
411 | BOOST_FOREACH(shared_ptr<data::decode::Decoder> dec, | |
412 | _decoder_stack->stack()) | |
413 | commit_decoder_probes(dec); | |
414 | ||
415 | _decoder_stack->begin_decode(); | |
4e5a4405 JH |
416 | } |
417 | ||
b9329558 | 418 | void DecodeTrace::on_new_decode_data() |
9cef9567 JH |
419 | { |
420 | if (_view) | |
421 | _view->update_viewport(); | |
422 | } | |
423 | ||
b9329558 | 424 | void DecodeTrace::delete_pressed() |
5ed1adf5 JH |
425 | { |
426 | on_delete(); | |
427 | } | |
428 | ||
b9329558 | 429 | void DecodeTrace::on_delete() |
c51482b3 JH |
430 | { |
431 | _session.remove_decode_signal(this); | |
432 | } | |
433 | ||
b9329558 | 434 | void DecodeTrace::on_probe_selected(int) |
4e5a4405 JH |
435 | { |
436 | commit_probes(); | |
437 | } | |
438 | ||
7491a29f JH |
439 | void DecodeTrace::on_stack_decoder(srd_decoder *decoder) |
440 | { | |
441 | assert(decoder); | |
442 | assert(_decoder_stack); | |
443 | _decoder_stack->push(shared_ptr<data::decode::Decoder>( | |
444 | new data::decode::Decoder(decoder))); | |
445 | _decoder_stack->begin_decode(); | |
37fd11b1 JH |
446 | |
447 | create_popup_form(); | |
7491a29f JH |
448 | } |
449 | ||
613d097c JH |
450 | void DecodeTrace::on_delete_decoder(int index) |
451 | { | |
452 | _decoder_stack->remove(index); | |
453 | ||
454 | // Update the popup | |
455 | create_popup_form(); | |
456 | ||
457 | _decoder_stack->begin_decode(); | |
458 | } | |
459 | ||
55d3603d JH |
460 | } // namespace view |
461 | } // namespace pv |