]> sigrok.org Git - pulseview.git/blame - pv/view/decodesignal.cpp
Added decode error display
[pulseview.git] / pv / view / decodesignal.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>
b213ef09 30#include <QMenu>
c51482b3 31
55d3603d
JH
32#include "decodesignal.h"
33
06bb4e6a 34#include <pv/sigsession.h>
119aff65 35#include <pv/data/decoder.h>
e0fc5810 36#include <pv/view/view.h>
9472f447 37#include <pv/view/decode/annotation.h>
119aff65 38
55d3603d
JH
39using namespace boost;
40using namespace std;
41
42namespace pv {
43namespace view {
44
06bb4e6a
JH
45const 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
50};
51
ad50ac1a
JH
52const QColor DecodeSignal::ErrorBgColour = QColor(0xEF, 0x29, 0x29);
53
119aff65 54DecodeSignal::DecodeSignal(pv::SigSession &session,
06bb4e6a 55 boost::shared_ptr<pv::data::Decoder> decoder, int index) :
119aff65
JH
56 Trace(session, QString(decoder->get_decoder()->name)),
57 _decoder(decoder)
55d3603d 58{
e0fc5810
JH
59 assert(_decoder);
60
06bb4e6a 61 _colour = DecodeColours[index % countof(DecodeColours)];
9cef9567
JH
62
63 connect(_decoder.get(), SIGNAL(new_decode_data()),
64 this, SLOT(on_new_decode_data()));
55d3603d
JH
65}
66
67bool DecodeSignal::enabled() const
68{
69 return true;
70}
71
b6b267bb
JH
72const boost::shared_ptr<pv::data::Decoder>& DecodeSignal::decoder() const
73{
74 return _decoder;
75}
76
e0fc5810
JH
77void DecodeSignal::set_view(pv::view::View *view)
78{
79 assert(view);
80 Trace::set_view(view);
81}
82
fe08b6e8
JH
83void DecodeSignal::paint_back(QPainter &p, int left, int right)
84{
85 paint_axis(p, get_y(), left, right);
86}
87
88void DecodeSignal::paint_mid(QPainter &p, int left, int right)
55d3603d 89{
9472f447
JH
90 using namespace pv::view::decode;
91
ad50ac1a
JH
92 assert(_decoder);
93 const QString err = _decoder->error_message();
94 if (!err.isEmpty()) {
95 draw_error(p, err, left, right);
96 return;
97 }
98
9472f447 99 assert(_view);
fe08b6e8 100 const int y = get_y();
9472f447
JH
101
102 const double scale = _view->scale();
103 assert(scale > 0);
104
105 double samplerate = _decoder->get_samplerate();
106
107 // Show sample rate as 1Hz when it is unknown
108 if (samplerate == 0.0)
109 samplerate = 1.0;
110
111 const double pixels_offset = (_view->offset() -
112 _decoder->get_start_time()) / scale;
113 const double samples_per_pixel = samplerate * scale;
114
115 assert(_decoder);
116 vector< shared_ptr<Annotation> > annotations(_decoder->annotations());
117 BOOST_FOREACH(shared_ptr<Annotation> a, annotations) {
118 assert(a);
f9abdc01
JH
119 a->paint(p, get_text_colour(), _text_size.height(),
120 left, right, samples_per_pixel, pixels_offset, y);
9472f447 121 }
55d3603d
JH
122}
123
c51482b3
JH
124QMenu* DecodeSignal::create_context_menu(QWidget *parent)
125{
126 QMenu *const menu = Trace::create_context_menu(parent);
127
128 menu->addSeparator();
129
130 QAction *const del = new QAction(tr("Delete"), this);
a2d21018 131 del->setShortcuts(QKeySequence::Delete);
c51482b3
JH
132 connect(del, SIGNAL(triggered()), this, SLOT(on_delete()));
133 menu->addAction(del);
134
135 return menu;
136}
137
ad50ac1a
JH
138void DecodeSignal::draw_error(QPainter &p, const QString &message,
139 int left, int right)
140{
141 const int y = get_y();
142
143 p.setPen(ErrorBgColour.darker());
144 p.setBrush(ErrorBgColour);
145
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;
151
152 p.drawRoundedRect(text_rect.adjusted(-r, -r, r, r), r, r,
153 Qt::AbsoluteSize);
154
155 p.setPen(get_text_colour());
156 p.drawText(text_rect, message);
157}
158
9cef9567
JH
159void DecodeSignal::on_new_decode_data()
160{
161 if (_view)
162 _view->update_viewport();
163}
164
5ed1adf5
JH
165void DecodeSignal::delete_pressed()
166{
167 on_delete();
168}
169
c51482b3
JH
170void DecodeSignal::on_delete()
171{
172 _session.remove_decode_signal(this);
173}
174
55d3603d
JH
175} // namespace view
176} // namespace pv