]>
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 | 137 | assert(_decoder_stack); |
db62bbfd JH |
138 | vector<Annotation> annotations(_decoder_stack->annotations()); |
139 | BOOST_FOREACH(const Annotation &a, annotations) | |
140 | a.paint(p, get_text_colour(), h, left, right, | |
5dfeb70f | 141 | samples_per_pixel, pixels_offset, y); |
5dfeb70f JH |
142 | |
143 | draw_unresolved_period(p, h, left, right, | |
144 | samples_per_pixel, pixels_offset); | |
55d3603d JH |
145 | } |
146 | ||
b9329558 | 147 | void DecodeTrace::populate_popup_form(QWidget *parent, QFormLayout *form) |
4e5a4405 | 148 | { |
613d097c JH |
149 | using pv::data::decode::Decoder; |
150 | ||
4e5a4405 JH |
151 | assert(form); |
152 | assert(parent); | |
6e89374a | 153 | assert(_decoder_stack); |
4e5a4405 | 154 | |
7491a29f | 155 | // Add the standard options |
4e5a4405 JH |
156 | Trace::populate_popup_form(parent, form); |
157 | ||
7491a29f JH |
158 | // Add the decoder options |
159 | _bindings.clear(); | |
160 | _probe_selectors.clear(); | |
4e5a4405 | 161 | |
613d097c | 162 | const list< shared_ptr<Decoder> >& stack = _decoder_stack->stack(); |
4e5a4405 | 163 | |
5069084a JH |
164 | if (stack.empty()) |
165 | { | |
166 | QLabel *const l = new QLabel( | |
167 | tr("<p><i>No decoders in the stack</i></p>")); | |
168 | l->setAlignment(Qt::AlignCenter); | |
169 | form->addRow(l); | |
170 | } | |
171 | else | |
172 | { | |
173 | list< shared_ptr<Decoder> >::const_iterator iter = | |
174 | stack.begin(); | |
175 | for (int i = 0; i < (int)stack.size(); i++, iter++) { | |
176 | shared_ptr<Decoder> dec(*iter); | |
177 | create_decoder_form(i, dec, parent, form); | |
178 | } | |
179 | ||
180 | form->addRow(new QLabel( | |
181 | tr("<i>* Required Probes</i>"), parent)); | |
182 | } | |
4e5a4405 | 183 | |
ce94e4fd | 184 | // Add stacking button |
ce94e4fd JH |
185 | pv::widgets::DecoderMenu *const decoder_menu = |
186 | new pv::widgets::DecoderMenu(parent); | |
7491a29f JH |
187 | connect(decoder_menu, SIGNAL(decoder_selected(srd_decoder*)), |
188 | this, SLOT(on_stack_decoder(srd_decoder*))); | |
189 | ||
190 | QPushButton *const stack_button = | |
191 | new QPushButton(tr("Stack Decoder"), parent); | |
ce94e4fd JH |
192 | stack_button->setMenu(decoder_menu); |
193 | ||
194 | QHBoxLayout *stack_button_box = new QHBoxLayout; | |
195 | stack_button_box->addWidget(stack_button, 0, Qt::AlignRight); | |
196 | form->addRow(stack_button_box); | |
4e5a4405 JH |
197 | } |
198 | ||
b9329558 | 199 | QMenu* DecodeTrace::create_context_menu(QWidget *parent) |
c51482b3 JH |
200 | { |
201 | QMenu *const menu = Trace::create_context_menu(parent); | |
202 | ||
203 | menu->addSeparator(); | |
204 | ||
205 | QAction *const del = new QAction(tr("Delete"), this); | |
a2d21018 | 206 | del->setShortcuts(QKeySequence::Delete); |
c51482b3 JH |
207 | connect(del, SIGNAL(triggered()), this, SLOT(on_delete())); |
208 | menu->addAction(del); | |
209 | ||
210 | return menu; | |
211 | } | |
212 | ||
b9329558 | 213 | void DecodeTrace::draw_error(QPainter &p, const QString &message, |
ad50ac1a JH |
214 | int left, int right) |
215 | { | |
216 | const int y = get_y(); | |
217 | ||
218 | p.setPen(ErrorBgColour.darker()); | |
219 | p.setBrush(ErrorBgColour); | |
220 | ||
221 | const QRectF bounding_rect = | |
222 | QRectF(left, INT_MIN / 2 + y, right - left, INT_MAX); | |
223 | const QRectF text_rect = p.boundingRect(bounding_rect, | |
224 | Qt::AlignCenter, message); | |
225 | const float r = text_rect.height() / 4; | |
226 | ||
227 | p.drawRoundedRect(text_rect.adjusted(-r, -r, r, r), r, r, | |
228 | Qt::AbsoluteSize); | |
229 | ||
230 | p.setPen(get_text_colour()); | |
231 | p.drawText(text_rect, message); | |
232 | } | |
233 | ||
5dfeb70f JH |
234 | void DecodeTrace::draw_unresolved_period(QPainter &p, int h, int left, |
235 | int right, double samples_per_pixel, double pixels_offset) | |
236 | { | |
237 | using namespace pv::data; | |
238 | using pv::data::decode::Decoder; | |
239 | ||
240 | assert(_decoder_stack); | |
241 | ||
242 | shared_ptr<Logic> data; | |
243 | shared_ptr<LogicSignal> logic_signal; | |
244 | ||
245 | const list< shared_ptr<Decoder> > &stack = _decoder_stack->stack(); | |
246 | ||
247 | // We get the logic data of the first probe in the list. | |
248 | // This works because we are currently assuming all | |
249 | // LogicSignals have the same data/snapshot | |
250 | BOOST_FOREACH (const shared_ptr<Decoder> &dec, stack) | |
251 | if (dec && !dec->probes().empty() && | |
252 | ((logic_signal = (*dec->probes().begin()).second)) && | |
7aa09b00 | 253 | ((data = logic_signal->logic_data()))) |
5dfeb70f JH |
254 | break; |
255 | ||
256 | if (!data || data->get_snapshots().empty()) | |
257 | return; | |
258 | ||
259 | const shared_ptr<LogicSnapshot> snapshot = | |
260 | data->get_snapshots().front(); | |
261 | assert(snapshot); | |
262 | const int64_t sample_count = (int64_t)snapshot->get_sample_count(); | |
263 | if (sample_count == 0) | |
264 | return; | |
265 | ||
266 | const int64_t samples_decoded = _decoder_stack->samples_decoded(); | |
267 | if (sample_count == samples_decoded) | |
268 | return; | |
269 | ||
270 | const int y = get_y(); | |
271 | const double start = max(samples_decoded / | |
272 | samples_per_pixel - pixels_offset, left - 1.0); | |
273 | const double end = min(sample_count / samples_per_pixel - | |
274 | pixels_offset, right + 1.0); | |
275 | const QRectF no_decode_rect(start, y - h/2 + 0.5, end - start, h); | |
276 | ||
277 | p.setPen(QPen(Qt::NoPen)); | |
278 | p.setBrush(Qt::white); | |
279 | p.drawRect(no_decode_rect); | |
280 | ||
281 | p.setPen(NoDecodeColour); | |
282 | p.setBrush(QBrush(NoDecodeColour, Qt::Dense6Pattern)); | |
283 | p.drawRect(no_decode_rect); | |
284 | } | |
285 | ||
613d097c JH |
286 | void DecodeTrace::create_decoder_form(int index, |
287 | shared_ptr<data::decode::Decoder> &dec, QWidget *parent, | |
288 | QFormLayout *form) | |
7491a29f JH |
289 | { |
290 | const GSList *probe; | |
291 | ||
292 | assert(dec); | |
293 | const srd_decoder *const decoder = dec->decoder(); | |
294 | assert(decoder); | |
295 | ||
204bae45 JH |
296 | pv::widgets::DecoderGroupBox *const group = |
297 | new pv::widgets::DecoderGroupBox(decoder->name); | |
613d097c JH |
298 | |
299 | _delete_mapper.setMapping(group, index); | |
300 | connect(group, SIGNAL(delete_decoder()), &_delete_mapper, SLOT(map())); | |
301 | ||
204bae45 JH |
302 | QFormLayout *const decoder_form = new QFormLayout; |
303 | group->add_layout(decoder_form); | |
7491a29f JH |
304 | |
305 | // Add the mandatory probes | |
306 | for(probe = decoder->probes; probe; probe = probe->next) { | |
307 | const struct srd_probe *const p = | |
308 | (struct srd_probe *)probe->data; | |
309 | QComboBox *const combo = create_probe_selector(parent, dec, p); | |
310 | connect(combo, SIGNAL(currentIndexChanged(int)), | |
311 | this, SLOT(on_probe_selected(int))); | |
204bae45 | 312 | decoder_form->addRow(tr("<b>%1</b> (%2) *") |
7491a29f JH |
313 | .arg(p->name).arg(p->desc), combo); |
314 | ||
315 | const ProbeSelector s = {combo, dec, p}; | |
316 | _probe_selectors.push_back(s); | |
317 | } | |
318 | ||
319 | // Add the optional probes | |
320 | for(probe = decoder->opt_probes; probe; probe = probe->next) { | |
321 | const struct srd_probe *const p = | |
322 | (struct srd_probe *)probe->data; | |
323 | QComboBox *const combo = create_probe_selector(parent, dec, p); | |
324 | connect(combo, SIGNAL(currentIndexChanged(int)), | |
325 | this, SLOT(on_probe_selected(int))); | |
204bae45 | 326 | decoder_form->addRow(tr("<b>%1</b> (%2)") |
7491a29f JH |
327 | .arg(p->name).arg(p->desc), combo); |
328 | ||
329 | const ProbeSelector s = {combo, dec, p}; | |
330 | _probe_selectors.push_back(s); | |
331 | } | |
332 | ||
333 | // Add the options | |
334 | shared_ptr<prop::binding::DecoderOptions> binding( | |
335 | new prop::binding::DecoderOptions(_decoder_stack, dec)); | |
204bae45 | 336 | binding->add_properties_to_form(decoder_form, true); |
7491a29f JH |
337 | |
338 | _bindings.push_back(binding); | |
204bae45 JH |
339 | |
340 | form->addRow(group); | |
7491a29f JH |
341 | } |
342 | ||
b9329558 | 343 | QComboBox* DecodeTrace::create_probe_selector( |
7491a29f JH |
344 | QWidget *parent, const shared_ptr<data::decode::Decoder> &dec, |
345 | const srd_probe *const probe) | |
4e5a4405 | 346 | { |
7491a29f JH |
347 | assert(dec); |
348 | ||
4e5a4405 JH |
349 | const vector< shared_ptr<Signal> > sigs = _session.get_signals(); |
350 | ||
6e89374a | 351 | assert(_decoder_stack); |
4e5a4405 JH |
352 | const map<const srd_probe*, |
353 | shared_ptr<LogicSignal> >::const_iterator probe_iter = | |
7491a29f | 354 | dec->probes().find(probe); |
4e5a4405 JH |
355 | |
356 | QComboBox *selector = new QComboBox(parent); | |
357 | ||
358 | selector->addItem("-", qVariantFromValue((void*)NULL)); | |
359 | ||
7491a29f | 360 | if (probe_iter == dec->probes().end()) |
4e5a4405 JH |
361 | selector->setCurrentIndex(0); |
362 | ||
363 | for(size_t i = 0; i < sigs.size(); i++) { | |
364 | const shared_ptr<view::Signal> s(sigs[i]); | |
365 | assert(s); | |
366 | ||
367 | if (dynamic_pointer_cast<LogicSignal>(s) && s->enabled()) | |
368 | { | |
369 | selector->addItem(s->get_name(), | |
370 | qVariantFromValue((void*)s.get())); | |
371 | if ((*probe_iter).second == s) | |
372 | selector->setCurrentIndex(i + 1); | |
373 | } | |
374 | } | |
375 | ||
376 | return selector; | |
377 | } | |
378 | ||
7491a29f | 379 | void DecodeTrace::commit_decoder_probes(shared_ptr<data::decode::Decoder> &dec) |
4e5a4405 | 380 | { |
7491a29f | 381 | assert(dec); |
4e5a4405 JH |
382 | |
383 | map<const srd_probe*, shared_ptr<LogicSignal> > probe_map; | |
384 | const vector< shared_ptr<Signal> > sigs = _session.get_signals(); | |
385 | ||
7491a29f | 386 | BOOST_FOREACH(const ProbeSelector &s, _probe_selectors) |
4e5a4405 | 387 | { |
7491a29f JH |
388 | if(s._decoder != dec) |
389 | break; | |
390 | ||
4e5a4405 | 391 | const LogicSignal *const selection = |
7491a29f JH |
392 | (LogicSignal*)s._combo->itemData( |
393 | s._combo->currentIndex()).value<void*>(); | |
4e5a4405 | 394 | |
7491a29f JH |
395 | BOOST_FOREACH(shared_ptr<Signal> sig, sigs) |
396 | if(sig.get() == selection) { | |
397 | probe_map[s._probe] = | |
398 | dynamic_pointer_cast<LogicSignal>(sig); | |
4e5a4405 JH |
399 | break; |
400 | } | |
401 | } | |
402 | ||
7491a29f JH |
403 | dec->set_probes(probe_map); |
404 | } | |
405 | ||
406 | void DecodeTrace::commit_probes() | |
407 | { | |
408 | assert(_decoder_stack); | |
409 | BOOST_FOREACH(shared_ptr<data::decode::Decoder> dec, | |
410 | _decoder_stack->stack()) | |
411 | commit_decoder_probes(dec); | |
412 | ||
413 | _decoder_stack->begin_decode(); | |
4e5a4405 JH |
414 | } |
415 | ||
b9329558 | 416 | void DecodeTrace::on_new_decode_data() |
9cef9567 JH |
417 | { |
418 | if (_view) | |
419 | _view->update_viewport(); | |
420 | } | |
421 | ||
b9329558 | 422 | void DecodeTrace::delete_pressed() |
5ed1adf5 JH |
423 | { |
424 | on_delete(); | |
425 | } | |
426 | ||
b9329558 | 427 | void DecodeTrace::on_delete() |
c51482b3 JH |
428 | { |
429 | _session.remove_decode_signal(this); | |
430 | } | |
431 | ||
b9329558 | 432 | void DecodeTrace::on_probe_selected(int) |
4e5a4405 JH |
433 | { |
434 | commit_probes(); | |
435 | } | |
436 | ||
7491a29f JH |
437 | void DecodeTrace::on_stack_decoder(srd_decoder *decoder) |
438 | { | |
439 | assert(decoder); | |
440 | assert(_decoder_stack); | |
441 | _decoder_stack->push(shared_ptr<data::decode::Decoder>( | |
442 | new data::decode::Decoder(decoder))); | |
443 | _decoder_stack->begin_decode(); | |
37fd11b1 JH |
444 | |
445 | create_popup_form(); | |
7491a29f JH |
446 | } |
447 | ||
613d097c JH |
448 | void DecodeTrace::on_delete_decoder(int index) |
449 | { | |
450 | _decoder_stack->remove(index); | |
451 | ||
452 | // Update the popup | |
453 | create_popup_form(); | |
454 | ||
455 | _decoder_stack->begin_decode(); | |
456 | } | |
457 | ||
55d3603d JH |
458 | } // namespace view |
459 | } // namespace pv |