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