]>
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 <QComboBox> | |
31 | #include <QFormLayout> | |
32 | #include <QLabel> | |
33 | #include <QMenu> | |
34 | #include <QPushButton> | |
35 | ||
36 | #include "decodetrace.h" | |
37 | ||
38 | #include <pv/sigsession.h> | |
39 | #include <pv/data/decoderstack.h> | |
40 | #include <pv/data/decode/decoder.h> | |
41 | #include <pv/view/logicsignal.h> | |
42 | #include <pv/view/view.h> | |
43 | #include <pv/view/decode/annotation.h> | |
44 | #include <pv/widgets/decodermenu.h> | |
45 | ||
46 | using namespace boost; | |
47 | using namespace std; | |
48 | ||
49 | namespace pv { | |
50 | namespace view { | |
51 | ||
52 | const QColor DecodeTrace::DecodeColours[4] = { | |
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 | ||
59 | const QColor DecodeTrace::ErrorBgColour = QColor(0xEF, 0x29, 0x29); | |
60 | ||
61 | DecodeTrace::DecodeTrace(pv::SigSession &session, | |
62 | boost::shared_ptr<pv::data::DecoderStack> decoder_stack, int index) : | |
63 | Trace(session, QString(decoder_stack->stack().front()->decoder()->name)), | |
64 | _decoder_stack(decoder_stack) | |
65 | { | |
66 | assert(_decoder_stack); | |
67 | ||
68 | _colour = DecodeColours[index % countof(DecodeColours)]; | |
69 | ||
70 | connect(_decoder_stack.get(), SIGNAL(new_decode_data()), | |
71 | this, SLOT(on_new_decode_data())); | |
72 | } | |
73 | ||
74 | bool DecodeTrace::enabled() const | |
75 | { | |
76 | return true; | |
77 | } | |
78 | ||
79 | const boost::shared_ptr<pv::data::DecoderStack>& DecodeTrace::decoder() const | |
80 | { | |
81 | return _decoder_stack; | |
82 | } | |
83 | ||
84 | void DecodeTrace::set_view(pv::view::View *view) | |
85 | { | |
86 | assert(view); | |
87 | Trace::set_view(view); | |
88 | } | |
89 | ||
90 | void DecodeTrace::paint_back(QPainter &p, int left, int right) | |
91 | { | |
92 | paint_axis(p, get_y(), left, right); | |
93 | } | |
94 | ||
95 | void DecodeTrace::paint_mid(QPainter &p, int left, int right) | |
96 | { | |
97 | using namespace pv::view::decode; | |
98 | ||
99 | assert(_decoder_stack); | |
100 | const QString err = _decoder_stack->error_message(); | |
101 | if (!err.isEmpty()) { | |
102 | draw_error(p, err, left, right); | |
103 | return; | |
104 | } | |
105 | ||
106 | assert(_view); | |
107 | const int y = get_y(); | |
108 | ||
109 | const double scale = _view->scale(); | |
110 | assert(scale > 0); | |
111 | ||
112 | double samplerate = _decoder_stack->get_samplerate(); | |
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() - | |
119 | _decoder_stack->get_start_time()) / scale; | |
120 | const double samples_per_pixel = samplerate * scale; | |
121 | ||
122 | assert(_decoder_stack); | |
123 | vector< shared_ptr<Annotation> > annotations(_decoder_stack->annotations()); | |
124 | BOOST_FOREACH(shared_ptr<Annotation> a, annotations) { | |
125 | assert(a); | |
126 | a->paint(p, get_text_colour(), _text_size.height(), | |
127 | left, right, samples_per_pixel, pixels_offset, y); | |
128 | } | |
129 | } | |
130 | ||
131 | void DecodeTrace::populate_popup_form(QWidget *parent, QFormLayout *form) | |
132 | { | |
133 | assert(form); | |
134 | assert(parent); | |
135 | assert(_decoder_stack); | |
136 | ||
137 | // Add the standard options | |
138 | Trace::populate_popup_form(parent, form); | |
139 | ||
140 | // Add the decoder options | |
141 | _bindings.clear(); | |
142 | _probe_selectors.clear(); | |
143 | ||
144 | BOOST_FOREACH(shared_ptr<data::decode::Decoder> dec, | |
145 | _decoder_stack->stack()) | |
146 | create_decoder_form(dec, parent, form); | |
147 | ||
148 | form->addRow(new QLabel( | |
149 | tr("<i>* Required Probes</i>"), parent)); | |
150 | ||
151 | // Add stacking button | |
152 | pv::widgets::DecoderMenu *const decoder_menu = | |
153 | new pv::widgets::DecoderMenu(parent); | |
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); | |
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); | |
164 | } | |
165 | ||
166 | QMenu* DecodeTrace::create_context_menu(QWidget *parent) | |
167 | { | |
168 | QMenu *const menu = Trace::create_context_menu(parent); | |
169 | ||
170 | menu->addSeparator(); | |
171 | ||
172 | QAction *const del = new QAction(tr("Delete"), this); | |
173 | del->setShortcuts(QKeySequence::Delete); | |
174 | connect(del, SIGNAL(triggered()), this, SLOT(on_delete())); | |
175 | menu->addAction(del); | |
176 | ||
177 | return menu; | |
178 | } | |
179 | ||
180 | void DecodeTrace::draw_error(QPainter &p, const QString &message, | |
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 | ||
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 | ||
248 | QComboBox* DecodeTrace::create_probe_selector( | |
249 | QWidget *parent, const shared_ptr<data::decode::Decoder> &dec, | |
250 | const srd_probe *const probe) | |
251 | { | |
252 | assert(dec); | |
253 | ||
254 | const vector< shared_ptr<Signal> > sigs = _session.get_signals(); | |
255 | ||
256 | assert(_decoder_stack); | |
257 | const map<const srd_probe*, | |
258 | shared_ptr<LogicSignal> >::const_iterator probe_iter = | |
259 | dec->probes().find(probe); | |
260 | ||
261 | QComboBox *selector = new QComboBox(parent); | |
262 | ||
263 | selector->addItem("-", qVariantFromValue((void*)NULL)); | |
264 | ||
265 | if (probe_iter == dec->probes().end()) | |
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 | ||
284 | void DecodeTrace::commit_decoder_probes(shared_ptr<data::decode::Decoder> &dec) | |
285 | { | |
286 | assert(dec); | |
287 | ||
288 | map<const srd_probe*, shared_ptr<LogicSignal> > probe_map; | |
289 | const vector< shared_ptr<Signal> > sigs = _session.get_signals(); | |
290 | ||
291 | BOOST_FOREACH(const ProbeSelector &s, _probe_selectors) | |
292 | { | |
293 | if(s._decoder != dec) | |
294 | break; | |
295 | ||
296 | const LogicSignal *const selection = | |
297 | (LogicSignal*)s._combo->itemData( | |
298 | s._combo->currentIndex()).value<void*>(); | |
299 | ||
300 | BOOST_FOREACH(shared_ptr<Signal> sig, sigs) | |
301 | if(sig.get() == selection) { | |
302 | probe_map[s._probe] = | |
303 | dynamic_pointer_cast<LogicSignal>(sig); | |
304 | break; | |
305 | } | |
306 | } | |
307 | ||
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(); | |
319 | } | |
320 | ||
321 | void DecodeTrace::on_new_decode_data() | |
322 | { | |
323 | if (_view) | |
324 | _view->update_viewport(); | |
325 | } | |
326 | ||
327 | void DecodeTrace::delete_pressed() | |
328 | { | |
329 | on_delete(); | |
330 | } | |
331 | ||
332 | void DecodeTrace::on_delete() | |
333 | { | |
334 | _session.remove_decode_signal(this); | |
335 | } | |
336 | ||
337 | void DecodeTrace::on_probe_selected(int) | |
338 | { | |
339 | commit_probes(); | |
340 | } | |
341 | ||
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(); | |
349 | } | |
350 | ||
351 | } // namespace view | |
352 | } // namespace pv |