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>
32 #include "decodesignal.h"
34 #include <pv/sigsession.h>
35 #include <pv/data/decoder.h>
36 #include <pv/view/view.h>
37 #include <pv/view/decode/annotation.h>
39 using namespace boost;
45 const QColor DecodeSignal::DecodeColours[4] = {
46 QColor(0xEF, 0x29, 0x29), // Red
47 QColor(0xFC, 0xE9, 0x4F), // Yellow
48 QColor(0x8A, 0xE2, 0x34), // Green
49 QColor(0x72, 0x9F, 0xCF) // Blue
52 const QColor DecodeSignal::ErrorBgColour = QColor(0xEF, 0x29, 0x29);
54 DecodeSignal::DecodeSignal(pv::SigSession &session,
55 boost::shared_ptr<pv::data::Decoder> decoder, int index) :
56 Trace(session, QString(decoder->get_decoder()->name)),
61 _colour = DecodeColours[index % countof(DecodeColours)];
63 connect(_decoder.get(), SIGNAL(new_decode_data()),
64 this, SLOT(on_new_decode_data()));
67 bool DecodeSignal::enabled() const
72 const boost::shared_ptr<pv::data::Decoder>& DecodeSignal::decoder() const
77 void DecodeSignal::set_view(pv::view::View *view)
80 Trace::set_view(view);
83 void DecodeSignal::paint_back(QPainter &p, int left, int right)
85 paint_axis(p, get_y(), left, right);
88 void DecodeSignal::paint_mid(QPainter &p, int left, int right)
90 using namespace pv::view::decode;
93 const QString err = _decoder->error_message();
95 draw_error(p, err, left, right);
100 const int y = get_y();
102 const double scale = _view->scale();
105 double samplerate = _decoder->get_samplerate();
107 // Show sample rate as 1Hz when it is unknown
108 if (samplerate == 0.0)
111 const double pixels_offset = (_view->offset() -
112 _decoder->get_start_time()) / scale;
113 const double samples_per_pixel = samplerate * scale;
116 vector< shared_ptr<Annotation> > annotations(_decoder->annotations());
117 BOOST_FOREACH(shared_ptr<Annotation> a, annotations) {
119 a->paint(p, get_text_colour(), _text_size.height(),
120 left, right, samples_per_pixel, pixels_offset, y);
124 QMenu* DecodeSignal::create_context_menu(QWidget *parent)
126 QMenu *const menu = Trace::create_context_menu(parent);
128 menu->addSeparator();
130 QAction *const del = new QAction(tr("Delete"), this);
131 del->setShortcuts(QKeySequence::Delete);
132 connect(del, SIGNAL(triggered()), this, SLOT(on_delete()));
133 menu->addAction(del);
138 void DecodeSignal::draw_error(QPainter &p, const QString &message,
141 const int y = get_y();
143 p.setPen(ErrorBgColour.darker());
144 p.setBrush(ErrorBgColour);
146 const QRectF bounding_rect =
147 QRectF(left, INT_MIN / 2 + y, right - left, INT_MAX);
148 const QRectF text_rect = p.boundingRect(bounding_rect,
149 Qt::AlignCenter, message);
150 const float r = text_rect.height() / 4;
152 p.drawRoundedRect(text_rect.adjusted(-r, -r, r, r), r, r,
155 p.setPen(get_text_colour());
156 p.drawText(text_rect, message);
159 void DecodeSignal::on_new_decode_data()
162 _view->update_viewport();
165 void DecodeSignal::delete_pressed()
170 void DecodeSignal::on_delete()
172 _session.remove_decode_signal(this);