2 * This file is part of the PulseView project.
4 * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk>
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.
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.
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
22 #include <libsigrokdecode/libsigrokdecode.h>
27 #include <boost/foreach.hpp>
31 #include <QFormLayout>
35 #include "decodesignal.h"
37 #include <pv/sigsession.h>
38 #include <pv/data/decoder.h>
39 #include <pv/view/logicsignal.h>
40 #include <pv/view/view.h>
41 #include <pv/view/decode/annotation.h>
43 using namespace boost;
49 const QColor DecodeSignal::DecodeColours[4] = {
50 QColor(0xEF, 0x29, 0x29), // Red
51 QColor(0xFC, 0xE9, 0x4F), // Yellow
52 QColor(0x8A, 0xE2, 0x34), // Green
53 QColor(0x72, 0x9F, 0xCF) // Blue
56 const QColor DecodeSignal::ErrorBgColour = QColor(0xEF, 0x29, 0x29);
58 DecodeSignal::DecodeSignal(pv::SigSession &session,
59 boost::shared_ptr<pv::data::Decoder> decoder, int index) :
60 Trace(session, QString(decoder->decoder()->name)),
66 _colour = DecodeColours[index % countof(DecodeColours)];
68 connect(_decoder.get(), SIGNAL(new_decode_data()),
69 this, SLOT(on_new_decode_data()));
72 bool DecodeSignal::enabled() const
77 const boost::shared_ptr<pv::data::Decoder>& DecodeSignal::decoder() const
82 void DecodeSignal::set_view(pv::view::View *view)
85 Trace::set_view(view);
88 void DecodeSignal::paint_back(QPainter &p, int left, int right)
90 paint_axis(p, get_y(), left, right);
93 void DecodeSignal::paint_mid(QPainter &p, int left, int right)
95 using namespace pv::view::decode;
98 const QString err = _decoder->error_message();
100 draw_error(p, err, left, right);
105 const int y = get_y();
107 const double scale = _view->scale();
110 double samplerate = _decoder->get_samplerate();
112 // Show sample rate as 1Hz when it is unknown
113 if (samplerate == 0.0)
116 const double pixels_offset = (_view->offset() -
117 _decoder->get_start_time()) / scale;
118 const double samples_per_pixel = samplerate * scale;
121 vector< shared_ptr<Annotation> > annotations(_decoder->annotations());
122 BOOST_FOREACH(shared_ptr<Annotation> a, annotations) {
124 a->paint(p, get_text_colour(), _text_size.height(),
125 left, right, samples_per_pixel, pixels_offset, y);
129 void DecodeSignal::populate_popup_form(QWidget *parent, QFormLayout *form)
137 const srd_decoder *const decoder = _decoder->decoder();
141 Trace::populate_popup_form(parent, form);
143 form->addRow(new QLabel(tr("<h3>Probes</h3>"), parent));
145 _probe_selector_map.clear();
147 // Add the mandatory probes
148 for(probe = decoder->probes; probe; probe = probe->next) {
149 const struct srd_probe *const p =
150 (struct srd_probe *)probe->data;
151 QComboBox *const combo = create_probe_selector(parent, p);
152 connect(combo, SIGNAL(currentIndexChanged(int)),
153 this, SLOT(on_probe_selected(int)));
154 form->addRow(tr("<b>%1</b> (%2) *")
155 .arg(p->name).arg(p->desc), combo);
157 _probe_selector_map[p] = combo;
160 // Add the optional probes
161 for(probe = decoder->opt_probes; probe; probe = probe->next) {
162 const struct srd_probe *const p =
163 (struct srd_probe *)probe->data;
164 QComboBox *const combo = create_probe_selector(parent, p);
165 connect(combo, SIGNAL(currentIndexChanged(int)),
166 this, SLOT(on_probe_selected(int)));
167 form->addRow(tr("<b>%1</b> (%2)")
168 .arg(p->name).arg(p->desc), combo);
170 _probe_selector_map[p] = combo;
173 form->addRow(new QLabel(
174 tr("<i>* Required Probes</i>"), parent));
177 if (!_binding.properties().empty()) {
178 form->addRow(new QLabel(tr("<h3>Options</h3>"),
180 _binding.add_properties_to_form(form, true);
184 QMenu* DecodeSignal::create_context_menu(QWidget *parent)
186 QMenu *const menu = Trace::create_context_menu(parent);
188 menu->addSeparator();
190 QAction *const del = new QAction(tr("Delete"), this);
191 del->setShortcuts(QKeySequence::Delete);
192 connect(del, SIGNAL(triggered()), this, SLOT(on_delete()));
193 menu->addAction(del);
198 void DecodeSignal::draw_error(QPainter &p, const QString &message,
201 const int y = get_y();
203 p.setPen(ErrorBgColour.darker());
204 p.setBrush(ErrorBgColour);
206 const QRectF bounding_rect =
207 QRectF(left, INT_MIN / 2 + y, right - left, INT_MAX);
208 const QRectF text_rect = p.boundingRect(bounding_rect,
209 Qt::AlignCenter, message);
210 const float r = text_rect.height() / 4;
212 p.drawRoundedRect(text_rect.adjusted(-r, -r, r, r), r, r,
215 p.setPen(get_text_colour());
216 p.drawText(text_rect, message);
219 QComboBox* DecodeSignal::create_probe_selector(
220 QWidget *parent, const srd_probe *const probe)
222 const vector< shared_ptr<Signal> > sigs = _session.get_signals();
225 const map<const srd_probe*,
226 shared_ptr<LogicSignal> >::const_iterator probe_iter =
227 _decoder->probes().find(probe);
229 QComboBox *selector = new QComboBox(parent);
231 selector->addItem("-", qVariantFromValue((void*)NULL));
233 if (probe_iter == _decoder->probes().end())
234 selector->setCurrentIndex(0);
236 for(size_t i = 0; i < sigs.size(); i++) {
237 const shared_ptr<view::Signal> s(sigs[i]);
240 if (dynamic_pointer_cast<LogicSignal>(s) && s->enabled())
242 selector->addItem(s->get_name(),
243 qVariantFromValue((void*)s.get()));
244 if ((*probe_iter).second == s)
245 selector->setCurrentIndex(i + 1);
252 void DecodeSignal::commit_probes()
256 map<const srd_probe*, shared_ptr<LogicSignal> > probe_map;
257 const vector< shared_ptr<Signal> > sigs = _session.get_signals();
259 for(map<const srd_probe*, QComboBox*>::const_iterator i =
260 _probe_selector_map.begin();
261 i != _probe_selector_map.end(); i++)
263 const QComboBox *const combo = (*i).second;
264 const LogicSignal *const selection =
265 (LogicSignal*)combo->itemData(combo->currentIndex()).
268 BOOST_FOREACH(shared_ptr<Signal> s, sigs)
269 if(s.get() == selection) {
270 probe_map[(*i).first] =
271 dynamic_pointer_cast<LogicSignal>(s);
276 _decoder->set_probes(probe_map);
279 void DecodeSignal::on_new_decode_data()
282 _view->update_viewport();
285 void DecodeSignal::delete_pressed()
290 void DecodeSignal::on_delete()
292 _session.remove_decode_signal(this);
295 void DecodeSignal::on_probe_selected(int)