]> sigrok.org Git - pulseview.git/blame - pv/view/decodetrace.cpp
Renamed pv::data::Decoder to DecoderStack
[pulseview.git] / pv / view / decodetrace.cpp
CommitLineData
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
21extern "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>
4e5a4405 40#include <pv/view/logicsignal.h>
e0fc5810 41#include <pv/view/view.h>
9472f447 42#include <pv/view/decode/annotation.h>
ce94e4fd 43#include <pv/widgets/decodermenu.h>
119aff65 44
55d3603d
JH
45using namespace boost;
46using namespace std;
47
48namespace pv {
49namespace view {
50
b9329558 51const QColor DecodeTrace::DecodeColours[4] = {
06bb4e6a
JH
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
b9329558 58const QColor DecodeTrace::ErrorBgColour = QColor(0xEF, 0x29, 0x29);
ad50ac1a 59
b9329558 60DecodeTrace::DecodeTrace(pv::SigSession &session,
6e89374a
JH
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)
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 74bool DecodeTrace::enabled() const
55d3603d
JH
75{
76 return true;
77}
78
6e89374a 79const boost::shared_ptr<pv::data::DecoderStack>& DecodeTrace::decoder() const
b6b267bb 80{
6e89374a 81 return _decoder_stack;
b6b267bb
JH
82}
83
b9329558 84void DecodeTrace::set_view(pv::view::View *view)
e0fc5810
JH
85{
86 assert(view);
87 Trace::set_view(view);
88}
89
b9329558 90void DecodeTrace::paint_back(QPainter &p, int left, int right)
fe08b6e8
JH
91{
92 paint_axis(p, get_y(), left, right);
93}
94
b9329558 95void 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 131void DecodeTrace::populate_popup_form(QWidget *parent, QFormLayout *form)
4e5a4405
JH
132{
133 const GSList *probe;
134
135 assert(form);
136 assert(parent);
6e89374a 137 assert(_decoder_stack);
4e5a4405 138
6e89374a 139 const srd_decoder *const decoder = _decoder_stack->decoder();
4e5a4405
JH
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 }
ce94e4fd
JH
184
185 // Add stacking button
186 QPushButton *const stack_button =
6e89374a 187 new QPushButton(tr("Stack DecoderStack"), parent);
ce94e4fd
JH
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);
4e5a4405
JH
195}
196
b9329558 197QMenu* DecodeTrace::create_context_menu(QWidget *parent)
c51482b3
JH
198{
199 QMenu *const menu = Trace::create_context_menu(parent);
200
201 menu->addSeparator();
202
203 QAction *const del = new QAction(tr("Delete"), this);
a2d21018 204 del->setShortcuts(QKeySequence::Delete);
c51482b3
JH
205 connect(del, SIGNAL(triggered()), this, SLOT(on_delete()));
206 menu->addAction(del);
207
208 return menu;
209}
210
b9329558 211void DecodeTrace::draw_error(QPainter &p, const QString &message,
ad50ac1a
JH
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
b9329558 232QComboBox* DecodeTrace::create_probe_selector(
4e5a4405
JH
233 QWidget *parent, const srd_probe *const probe)
234{
235 const vector< shared_ptr<Signal> > sigs = _session.get_signals();
236
6e89374a 237 assert(_decoder_stack);
4e5a4405
JH
238 const map<const srd_probe*,
239 shared_ptr<LogicSignal> >::const_iterator probe_iter =
6e89374a 240 _decoder_stack->probes().find(probe);
4e5a4405
JH
241
242 QComboBox *selector = new QComboBox(parent);
243
244 selector->addItem("-", qVariantFromValue((void*)NULL));
245
6e89374a 246 if (probe_iter == _decoder_stack->probes().end())
4e5a4405
JH
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
b9329558 265void DecodeTrace::commit_probes()
4e5a4405 266{
6e89374a 267 assert(_decoder_stack);
4e5a4405
JH
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
6e89374a 289 _decoder_stack->set_probes(probe_map);
4e5a4405
JH
290}
291
b9329558 292void DecodeTrace::on_new_decode_data()
9cef9567
JH
293{
294 if (_view)
295 _view->update_viewport();
296}
297
b9329558 298void DecodeTrace::delete_pressed()
5ed1adf5
JH
299{
300 on_delete();
301}
302
b9329558 303void DecodeTrace::on_delete()
c51482b3
JH
304{
305 _session.remove_decode_signal(this);
306}
307
b9329558 308void DecodeTrace::on_probe_selected(int)
4e5a4405
JH
309{
310 commit_probes();
311}
312
55d3603d
JH
313} // namespace view
314} // namespace pv