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