]> sigrok.org Git - pulseview.git/blob - pv/view/decodetrace.cpp
51b17e5e14269d236acdde3dba578d39b36c47f5
[pulseview.git] / pv / view / decodetrace.cpp
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/view/logicsignal.h>
41 #include <pv/view/view.h>
42 #include <pv/view/decode/annotation.h>
43 #include <pv/widgets/decodermenu.h>
44
45 using namespace boost;
46 using namespace std;
47
48 namespace pv {
49 namespace view {
50
51 const QColor DecodeTrace::DecodeColours[4] = {
52         QColor(0xEF, 0x29, 0x29),       // Red
53         QColor(0xFC, 0xE9, 0x4F),       // Yellow
54         QColor(0x8A, 0xE2, 0x34),       // Green
55         QColor(0x72, 0x9F, 0xCF)        // Blue
56 };
57
58 const QColor DecodeTrace::ErrorBgColour = QColor(0xEF, 0x29, 0x29);
59
60 DecodeTrace::DecodeTrace(pv::SigSession &session,
61         boost::shared_ptr<pv::data::DecoderStack> decoder_stack, int index) :
62         Trace(session, QString(decoder_stack->decoder()->name)),
63         _decoder_stack(decoder_stack),
64         _binding(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         const GSList *probe;
134
135         assert(form);
136         assert(parent);
137         assert(_decoder_stack);
138
139         const srd_decoder *const decoder = _decoder_stack->decoder();
140
141         assert(decoder);
142
143         Trace::populate_popup_form(parent, form);
144
145         form->addRow(new QLabel(tr("<h3>Probes</h3>"), parent));
146
147         _probe_selector_map.clear();
148
149         // Add the mandatory probes
150         for(probe = decoder->probes; probe; probe = probe->next) {
151                 const struct srd_probe *const p =
152                         (struct srd_probe *)probe->data;
153                 QComboBox *const combo = create_probe_selector(parent, p);
154                 connect(combo, SIGNAL(currentIndexChanged(int)),
155                         this, SLOT(on_probe_selected(int)));
156                 form->addRow(tr("<b>%1</b> (%2) *")
157                         .arg(p->name).arg(p->desc), combo);
158
159                 _probe_selector_map[p] = combo;
160         }
161
162         // Add the optional probes
163         for(probe = decoder->opt_probes; probe; probe = probe->next) {
164                 const struct srd_probe *const p =
165                         (struct srd_probe *)probe->data;
166                 QComboBox *const combo = create_probe_selector(parent, p);
167                 connect(combo, SIGNAL(currentIndexChanged(int)),
168                         this, SLOT(on_probe_selected(int)));
169                 form->addRow(tr("<b>%1</b> (%2)")
170                         .arg(p->name).arg(p->desc), combo);
171
172                 _probe_selector_map[p] = combo;
173         }
174
175         form->addRow(new QLabel(
176                 tr("<i>* Required Probes</i>"), parent));
177
178         // Add the options
179         if (!_binding.properties().empty()) {
180                 form->addRow(new QLabel(tr("<h3>Options</h3>"),
181                         parent));
182                 _binding.add_properties_to_form(form, true);
183         }
184
185         // Add stacking button
186         QPushButton *const stack_button =
187                 new QPushButton(tr("Stack DecoderStack"), parent);
188         pv::widgets::DecoderMenu *const decoder_menu =
189                 new pv::widgets::DecoderMenu(parent);
190         stack_button->setMenu(decoder_menu);
191
192         QHBoxLayout *stack_button_box = new QHBoxLayout;
193         stack_button_box->addWidget(stack_button, 0, Qt::AlignRight);
194         form->addRow(stack_button_box);
195 }
196
197 QMenu* DecodeTrace::create_context_menu(QWidget *parent)
198 {
199         QMenu *const menu = Trace::create_context_menu(parent);
200
201         menu->addSeparator();
202
203         QAction *const del = new QAction(tr("Delete"), this);
204         del->setShortcuts(QKeySequence::Delete);
205         connect(del, SIGNAL(triggered()), this, SLOT(on_delete()));
206         menu->addAction(del);
207
208         return menu;
209 }
210
211 void DecodeTrace::draw_error(QPainter &p, const QString &message,
212         int left, int right)
213 {
214         const int y = get_y();
215
216         p.setPen(ErrorBgColour.darker());
217         p.setBrush(ErrorBgColour);
218
219         const QRectF bounding_rect =
220                 QRectF(left, INT_MIN / 2 + y, right - left, INT_MAX);
221         const QRectF text_rect = p.boundingRect(bounding_rect,
222                 Qt::AlignCenter, message);
223         const float r = text_rect.height() / 4;
224
225         p.drawRoundedRect(text_rect.adjusted(-r, -r, r, r), r, r,
226                 Qt::AbsoluteSize);
227
228         p.setPen(get_text_colour());
229         p.drawText(text_rect, message);
230 }
231
232 QComboBox* DecodeTrace::create_probe_selector(
233         QWidget *parent, const srd_probe *const probe)
234 {
235         const vector< shared_ptr<Signal> > sigs = _session.get_signals();
236
237         assert(_decoder_stack);
238         const map<const srd_probe*,
239                 shared_ptr<LogicSignal> >::const_iterator probe_iter =
240                 _decoder_stack->probes().find(probe);
241
242         QComboBox *selector = new QComboBox(parent);
243
244         selector->addItem("-", qVariantFromValue((void*)NULL));
245
246         if (probe_iter == _decoder_stack->probes().end())
247                 selector->setCurrentIndex(0);
248
249         for(size_t i = 0; i < sigs.size(); i++) {
250                 const shared_ptr<view::Signal> s(sigs[i]);
251                 assert(s);
252
253                 if (dynamic_pointer_cast<LogicSignal>(s) && s->enabled())
254                 {
255                         selector->addItem(s->get_name(),
256                                 qVariantFromValue((void*)s.get()));
257                         if ((*probe_iter).second == s)
258                                 selector->setCurrentIndex(i + 1);
259                 }
260         }
261
262         return selector;
263 }
264
265 void DecodeTrace::commit_probes()
266 {
267         assert(_decoder_stack);
268
269         map<const srd_probe*, shared_ptr<LogicSignal> > probe_map;
270         const vector< shared_ptr<Signal> > sigs = _session.get_signals();
271
272         for(map<const srd_probe*, QComboBox*>::const_iterator i =
273                 _probe_selector_map.begin();
274                 i != _probe_selector_map.end(); i++)
275         {
276                 const QComboBox *const combo = (*i).second;
277                 const LogicSignal *const selection =
278                         (LogicSignal*)combo->itemData(combo->currentIndex()).
279                         value<void*>();
280
281                 BOOST_FOREACH(shared_ptr<Signal> s, sigs)
282                         if(s.get() == selection) {
283                                 probe_map[(*i).first] =
284                                         dynamic_pointer_cast<LogicSignal>(s);
285                                 break;
286                         }
287         }
288
289         _decoder_stack->set_probes(probe_map);
290 }
291
292 void DecodeTrace::on_new_decode_data()
293 {
294         if (_view)
295                 _view->update_viewport();
296 }
297
298 void DecodeTrace::delete_pressed()
299 {
300         on_delete();
301 }
302
303 void DecodeTrace::on_delete()
304 {
305         _session.remove_decode_signal(this);
306 }
307
308 void DecodeTrace::on_probe_selected(int)
309 {
310         commit_probes();
311 }
312
313 } // namespace view
314 } // namespace pv