]> sigrok.org Git - pulseview.git/blame - sigview.cpp
Added initial toolbar
[pulseview.git] / sigview.cpp
CommitLineData
6fa02541
JH
1/*
2 * This file is part of the sigrok 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#include "sigview.h"
22
009e1503 23#include "sigsession.h"
e3f65ace
JH
24#include "signal.h"
25
e9c41f8c
JH
26#include "extdef.h"
27
7cd5faf8 28#include <QMouseEvent>
22016ad3 29#include <QTextStream>
7cd5faf8 30
e9c41f8c
JH
31#include <math.h>
32
e3f65ace
JH
33#include <boost/foreach.hpp>
34
35using namespace boost;
36using namespace std;
37
7ae27594
JH
38const double SigView::MaxScale = 1e9;
39const double SigView::MinScale = 1e-15;
40
e3f65ace 41const int SigView::SignalHeight = 50;
3e46726a 42const int SigView::LabelMarginWidth = 70;
e9c41f8c
JH
43const int SigView::RulerHeight = 30;
44
438440ea 45const int SigView::MinorTickSubdivision = 4;
e9c41f8c 46const int SigView::ScaleUnits[3] = {1, 2, 5};
009e1503 47
22016ad3
JH
48const QString SigView::SIPrefixes[9] =
49 {"f", "p", "n", QChar(0x03BC), "m", "", "k", "M", "G"};
50const int SigView::FirstSIPrefixPower = -15;
51
009e1503
JH
52SigView::SigView(SigSession &session, QWidget *parent) :
53 QGLWidget(parent),
3b18c57d 54 _session(session),
fb0d32cb 55 _scale(1e-6),
3b18c57d 56 _offset(0)
6fa02541 57{
04abfae9
JH
58 connect(&_session, SIGNAL(data_updated()),
59 this, SLOT(data_updated()));
009e1503 60
d7002724 61 setMouseTracking(true);
3e46726a 62 setAutoFillBackground(false);
d7002724
JH
63}
64
65void SigView::initializeGL()
66{
d7002724
JH
67}
68
69void SigView::resizeGL(int width, int height)
70{
04abfae9 71 setup_viewport(width, height);
d7002724
JH
72}
73
3e46726a 74void SigView::paintEvent(QPaintEvent *event)
d7002724 75{
3e46726a 76 int offset;
e3f65ace 77
e3f65ace
JH
78 const vector< shared_ptr<Signal> > &sigs =
79 _session.get_signals();
3e46726a
JH
80
81 // Prepare for OpenGL rendering
82 makeCurrent();
83 glMatrixMode(GL_MODELVIEW);
84 glPushMatrix();
85
04abfae9 86 setup_viewport(width(), height());
3e46726a
JH
87
88 qglClearColor(Qt::white);
89 glClear(GL_COLOR_BUFFER_BIT);
90
91 // Plot the signal
aeed8b41
JH
92 glEnable(GL_SCISSOR_TEST);
93 glScissor(LabelMarginWidth, 0, width(), height());
e9c41f8c 94 offset = RulerHeight;
3e46726a
JH
95 BOOST_FOREACH(const shared_ptr<Signal> s, sigs)
96 {
97 assert(s);
98
99 const QRect signal_rect(LabelMarginWidth, offset,
100 width() - LabelMarginWidth, SignalHeight);
101
102 s->paint(*this, signal_rect, _scale, _offset);
103
104 offset += SignalHeight;
105 }
106
aeed8b41
JH
107 glDisable(GL_SCISSOR_TEST);
108
3e46726a
JH
109 // Prepare for QPainter rendering
110 glMatrixMode(GL_MODELVIEW);
111 glPopMatrix();
112
113 QPainter painter(this);
114 painter.setRenderHint(QPainter::Antialiasing);
115
e9c41f8c
JH
116 // Paint the labels
117 offset = RulerHeight;
e3f65ace
JH
118 BOOST_FOREACH(const shared_ptr<Signal> s, sigs)
119 {
120 assert(s);
3e46726a
JH
121
122 const QRect label_rect(0, offset,
123 LabelMarginWidth, SignalHeight);
124 s->paint_label(painter, label_rect);
125
126 offset += SignalHeight;
e3f65ace 127 }
3e46726a 128
e9c41f8c 129 // Paint the ruler
04abfae9 130 paint_ruler(painter);
e9c41f8c 131
3e46726a 132 painter.end();
6fa02541 133}
009e1503 134
04abfae9 135void SigView::data_updated()
009e1503 136{
e3f65ace 137 update();
009e1503
JH
138}
139
1dd95c70 140void SigView::mousePressEvent(QMouseEvent *event)
7cd5faf8
JH
141{
142 assert(event);
1dd95c70
JH
143
144 _mouse_down_point = event->pos();
145 _mouse_down_offset = _offset;
7cd5faf8
JH
146}
147
1dd95c70 148void SigView::mouseMoveEvent(QMouseEvent *event)
7cd5faf8
JH
149{
150 assert(event);
1dd95c70
JH
151
152 if(event->buttons() & Qt::LeftButton)
153 {
154 _offset = _mouse_down_offset + (_mouse_down_point - event->pos()).x() * _scale;
155 update();
156 }
7cd5faf8
JH
157}
158
159void SigView::mouseReleaseEvent(QMouseEvent *event)
160{
161 assert(event);
1dd95c70 162}
7cd5faf8 163
1dd95c70
JH
164void SigView::wheelEvent(QWheelEvent *event)
165{
166 assert(event);
fb0d32cb 167
1dd95c70
JH
168 const double x = event->x() - LabelMarginWidth;
169 const double cursor_offset = _offset + _scale * x;
170 _scale *= powf(3.0/2.0, -event->delta() / 120);
7ae27594 171 _scale = max(min(_scale, MaxScale), MinScale);
1dd95c70 172 _offset = cursor_offset - _scale * x;
3e46726a 173 update();
7cd5faf8
JH
174}
175
04abfae9 176void SigView::setup_viewport(int width, int height)
3e46726a
JH
177{
178 glViewport(0, 0, (GLint)width, (GLint)height);
179 glMatrixMode(GL_PROJECTION);
180 glLoadIdentity();
181 glOrtho(0, width, height, 0, -1, 1);
182 glMatrixMode(GL_MODELVIEW);
183}
e9c41f8c 184
04abfae9 185void SigView::paint_ruler(QPainter &p)
e9c41f8c 186{
22016ad3 187 const double MinSpacing = 80;
e9c41f8c 188
e9c41f8c
JH
189 const double min_period = _scale * MinSpacing;
190
22016ad3
JH
191 const int order = (int)floorf(log10f(min_period));
192 const double order_decimal = pow(10, order);
193
194 int unit = 0;
195 double tick_period = 0.0f;
196
197 do
e9c41f8c 198 {
22016ad3
JH
199 tick_period = order_decimal * ScaleUnits[unit++];
200 } while(tick_period < min_period && unit < countof(ScaleUnits));
e9c41f8c 201
22016ad3
JH
202 const int prefix = (order - FirstSIPrefixPower) / 3;
203 assert(prefix >= 0);
204 assert(prefix < countof(SIPrefixes));
e9c41f8c 205
22016ad3
JH
206 const int text_height = p.boundingRect(0, 0, INT_MAX, INT_MAX,
207 Qt::AlignLeft | Qt::AlignTop, "8").height();
e9c41f8c 208
22016ad3 209 // Draw the tick marks
e9c41f8c
JH
210 p.setPen(Qt::black);
211
438440ea
JH
212 const double minor_tick_period = tick_period / MinorTickSubdivision;
213 const double first_major_division = floor(_offset / tick_period);
214 const double first_minor_division = ceil(_offset / minor_tick_period);
215 const double t0 = first_major_division * tick_period;
216
217 int division = (int)round(first_minor_division -
218 first_major_division * MinorTickSubdivision);
219 while(1)
e9c41f8c 220 {
438440ea
JH
221 const double t = t0 + division * minor_tick_period;
222 const double x = (t - _offset) / _scale + LabelMarginWidth;
223
224 if(x >= width())
225 break;
226
227 if(division % MinorTickSubdivision == 0)
228 {
229 // Draw a major tick
230 QString s;
231 QTextStream ts(&s);
232 ts << (t / order_decimal) << SIPrefixes[prefix] << "s";
233 p.drawText(x, 0, 0, text_height, Qt::AlignCenter | Qt::AlignTop |
234 Qt::TextDontClip, s);
235 p.drawLine(x, text_height, x, RulerHeight);
236 }
237 else
238 {
239 // Draw a minor tick
240 p.drawLine(x, (text_height + RulerHeight) / 2, x, RulerHeight);
241 }
242
243 division++;
e9c41f8c
JH
244 }
245}