]>
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/view/logicsignal.h> | |
45 | #include <pv/view/view.h> | |
46 | #include <pv/view/decode/annotation.h> | |
47 | #include <pv/widgets/decodergroupbox.h> | |
48 | #include <pv/widgets/decodermenu.h> | |
49 | ||
50 | using namespace boost; | |
51 | using namespace std; | |
52 | ||
53 | namespace pv { | |
54 | namespace view { | |
55 | ||
56 | const QColor DecodeTrace::DecodeColours[4] = { | |
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 | ||
63 | const QColor DecodeTrace::ErrorBgColour = QColor(0xEF, 0x29, 0x29); | |
64 | const QColor DecodeTrace::NoDecodeColour = QColor(0x88, 0x8A, 0x85); | |
65 | ||
66 | DecodeTrace::DecodeTrace(pv::SigSession &session, | |
67 | boost::shared_ptr<pv::data::DecoderStack> decoder_stack, int index) : | |
68 | Trace(session, QString(decoder_stack->stack().front()->decoder()->name)), | |
69 | _decoder_stack(decoder_stack), | |
70 | _delete_mapper(this) | |
71 | { | |
72 | assert(_decoder_stack); | |
73 | ||
74 | _colour = DecodeColours[index % countof(DecodeColours)]; | |
75 | ||
76 | connect(_decoder_stack.get(), SIGNAL(new_decode_data()), | |
77 | this, SLOT(on_new_decode_data())); | |
78 | connect(&_delete_mapper, SIGNAL(mapped(int)), | |
79 | this, SLOT(on_delete_decoder(int))); | |
80 | } | |
81 | ||
82 | bool DecodeTrace::enabled() const | |
83 | { | |
84 | return true; | |
85 | } | |
86 | ||
87 | const boost::shared_ptr<pv::data::DecoderStack>& DecodeTrace::decoder() const | |
88 | { | |
89 | return _decoder_stack; | |
90 | } | |
91 | ||
92 | void DecodeTrace::set_view(pv::view::View *view) | |
93 | { | |
94 | assert(view); | |
95 | Trace::set_view(view); | |
96 | } | |
97 | ||
98 | void DecodeTrace::paint_back(QPainter &p, int left, int right) | |
99 | { | |
100 | Trace::paint_back(p, left, right); | |
101 | paint_axis(p, get_y(), left, right); | |
102 | } | |
103 | ||
104 | void DecodeTrace::paint_mid(QPainter &p, int left, int right) | |
105 | { | |
106 | using namespace pv::view::decode; | |
107 | ||
108 | const double scale = _view->scale(); | |
109 | assert(scale > 0); | |
110 | ||
111 | double samplerate = _decoder_stack->samplerate(); | |
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() - | |
118 | _decoder_stack->get_start_time()) / scale; | |
119 | const double samples_per_pixel = samplerate * scale; | |
120 | ||
121 | QFontMetrics m(QApplication::font()); | |
122 | const int h = (m.boundingRect(QRect(), 0, "Tg").height() * 5) / 4; | |
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 | ||
137 | assert(_decoder_stack); | |
138 | vector< shared_ptr<Annotation> > annotations(_decoder_stack->annotations()); | |
139 | BOOST_FOREACH(shared_ptr<Annotation> a, annotations) { | |
140 | assert(a); | |
141 | a->paint(p, get_text_colour(), h, left, right, | |
142 | samples_per_pixel, pixels_offset, y); | |
143 | } | |
144 | ||
145 | draw_unresolved_period(p, h, left, right, | |
146 | samples_per_pixel, pixels_offset); | |
147 | } | |
148 | ||
149 | void DecodeTrace::populate_popup_form(QWidget *parent, QFormLayout *form) | |
150 | { | |
151 | using pv::data::decode::Decoder; | |
152 | ||
153 | assert(form); | |
154 | assert(parent); | |
155 | assert(_decoder_stack); | |
156 | ||
157 | // Add the standard options | |
158 | Trace::populate_popup_form(parent, form); | |
159 | ||
160 | // Add the decoder options | |
161 | _bindings.clear(); | |
162 | _probe_selectors.clear(); | |
163 | ||
164 | const list< shared_ptr<Decoder> >& stack = _decoder_stack->stack(); | |
165 | ||
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 | } | |
185 | ||
186 | // Add stacking button | |
187 | pv::widgets::DecoderMenu *const decoder_menu = | |
188 | new pv::widgets::DecoderMenu(parent); | |
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); | |
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); | |
199 | } | |
200 | ||
201 | QMenu* DecodeTrace::create_context_menu(QWidget *parent) | |
202 | { | |
203 | QMenu *const menu = Trace::create_context_menu(parent); | |
204 | ||
205 | menu->addSeparator(); | |
206 | ||
207 | QAction *const del = new QAction(tr("Delete"), this); | |
208 | del->setShortcuts(QKeySequence::Delete); | |
209 | connect(del, SIGNAL(triggered()), this, SLOT(on_delete())); | |
210 | menu->addAction(del); | |
211 | ||
212 | return menu; | |
213 | } | |
214 | ||
215 | void DecodeTrace::draw_error(QPainter &p, const QString &message, | |
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 | ||
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)) && | |
255 | ((data = logic_signal->logic_data()))) | |
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 | ||
288 | void DecodeTrace::create_decoder_form(int index, | |
289 | shared_ptr<data::decode::Decoder> &dec, QWidget *parent, | |
290 | QFormLayout *form) | |
291 | { | |
292 | const GSList *probe; | |
293 | ||
294 | assert(dec); | |
295 | const srd_decoder *const decoder = dec->decoder(); | |
296 | assert(decoder); | |
297 | ||
298 | pv::widgets::DecoderGroupBox *const group = | |
299 | new pv::widgets::DecoderGroupBox(decoder->name); | |
300 | ||
301 | _delete_mapper.setMapping(group, index); | |
302 | connect(group, SIGNAL(delete_decoder()), &_delete_mapper, SLOT(map())); | |
303 | ||
304 | QFormLayout *const decoder_form = new QFormLayout; | |
305 | group->add_layout(decoder_form); | |
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))); | |
314 | decoder_form->addRow(tr("<b>%1</b> (%2) *") | |
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))); | |
328 | decoder_form->addRow(tr("<b>%1</b> (%2)") | |
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)); | |
338 | binding->add_properties_to_form(decoder_form, true); | |
339 | ||
340 | _bindings.push_back(binding); | |
341 | ||
342 | form->addRow(group); | |
343 | } | |
344 | ||
345 | QComboBox* DecodeTrace::create_probe_selector( | |
346 | QWidget *parent, const shared_ptr<data::decode::Decoder> &dec, | |
347 | const srd_probe *const probe) | |
348 | { | |
349 | assert(dec); | |
350 | ||
351 | const vector< shared_ptr<Signal> > sigs = _session.get_signals(); | |
352 | ||
353 | assert(_decoder_stack); | |
354 | const map<const srd_probe*, | |
355 | shared_ptr<LogicSignal> >::const_iterator probe_iter = | |
356 | dec->probes().find(probe); | |
357 | ||
358 | QComboBox *selector = new QComboBox(parent); | |
359 | ||
360 | selector->addItem("-", qVariantFromValue((void*)NULL)); | |
361 | ||
362 | if (probe_iter == dec->probes().end()) | |
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 | ||
381 | void DecodeTrace::commit_decoder_probes(shared_ptr<data::decode::Decoder> &dec) | |
382 | { | |
383 | assert(dec); | |
384 | ||
385 | map<const srd_probe*, shared_ptr<LogicSignal> > probe_map; | |
386 | const vector< shared_ptr<Signal> > sigs = _session.get_signals(); | |
387 | ||
388 | BOOST_FOREACH(const ProbeSelector &s, _probe_selectors) | |
389 | { | |
390 | if(s._decoder != dec) | |
391 | break; | |
392 | ||
393 | const LogicSignal *const selection = | |
394 | (LogicSignal*)s._combo->itemData( | |
395 | s._combo->currentIndex()).value<void*>(); | |
396 | ||
397 | BOOST_FOREACH(shared_ptr<Signal> sig, sigs) | |
398 | if(sig.get() == selection) { | |
399 | probe_map[s._probe] = | |
400 | dynamic_pointer_cast<LogicSignal>(sig); | |
401 | break; | |
402 | } | |
403 | } | |
404 | ||
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(); | |
416 | } | |
417 | ||
418 | void DecodeTrace::on_new_decode_data() | |
419 | { | |
420 | if (_view) | |
421 | _view->update_viewport(); | |
422 | } | |
423 | ||
424 | void DecodeTrace::delete_pressed() | |
425 | { | |
426 | on_delete(); | |
427 | } | |
428 | ||
429 | void DecodeTrace::on_delete() | |
430 | { | |
431 | _session.remove_decode_signal(this); | |
432 | } | |
433 | ||
434 | void DecodeTrace::on_probe_selected(int) | |
435 | { | |
436 | commit_probes(); | |
437 | } | |
438 | ||
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(); | |
446 | ||
447 | create_popup_form(); | |
448 | } | |
449 | ||
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 | ||
460 | } // namespace view | |
461 | } // namespace pv |