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