]> sigrok.org Git - pulseview.git/blame - sigview.cpp
Added ruler numbering
[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
38const int SigView::SignalHeight = 50;
3e46726a 39const int SigView::LabelMarginWidth = 70;
e9c41f8c
JH
40const int SigView::RulerHeight = 30;
41
42const int SigView::ScaleUnits[3] = {1, 2, 5};
009e1503 43
22016ad3
JH
44const QString SigView::SIPrefixes[9] =
45 {"f", "p", "n", QChar(0x03BC), "m", "", "k", "M", "G"};
46const int SigView::FirstSIPrefixPower = -15;
47
009e1503
JH
48SigView::SigView(SigSession &session, QWidget *parent) :
49 QGLWidget(parent),
3b18c57d 50 _session(session),
fb0d32cb 51 _scale(1e-6),
3b18c57d 52 _offset(0)
6fa02541 53{
009e1503
JH
54 connect(&_session, SIGNAL(dataUpdated()),
55 this, SLOT(dataUpdated()));
56
d7002724 57 setMouseTracking(true);
3e46726a 58 setAutoFillBackground(false);
d7002724
JH
59}
60
61void SigView::initializeGL()
62{
d7002724
JH
63}
64
65void SigView::resizeGL(int width, int height)
66{
3e46726a 67 setupViewport(width, height);
d7002724
JH
68}
69
3e46726a 70void SigView::paintEvent(QPaintEvent *event)
d7002724 71{
3e46726a 72 int offset;
e3f65ace 73
e3f65ace
JH
74 const vector< shared_ptr<Signal> > &sigs =
75 _session.get_signals();
3e46726a
JH
76
77 // Prepare for OpenGL rendering
78 makeCurrent();
79 glMatrixMode(GL_MODELVIEW);
80 glPushMatrix();
81
82 setupViewport(width(), height());
83
84 qglClearColor(Qt::white);
85 glClear(GL_COLOR_BUFFER_BIT);
86
87 // Plot the signal
e9c41f8c 88 offset = RulerHeight;
3e46726a
JH
89 BOOST_FOREACH(const shared_ptr<Signal> s, sigs)
90 {
91 assert(s);
92
93 const QRect signal_rect(LabelMarginWidth, offset,
94 width() - LabelMarginWidth, SignalHeight);
95
96 s->paint(*this, signal_rect, _scale, _offset);
97
98 offset += SignalHeight;
99 }
100
101 // Prepare for QPainter rendering
102 glMatrixMode(GL_MODELVIEW);
103 glPopMatrix();
104
105 QPainter painter(this);
106 painter.setRenderHint(QPainter::Antialiasing);
107
e9c41f8c
JH
108 // Paint the labels
109 offset = RulerHeight;
e3f65ace
JH
110 BOOST_FOREACH(const shared_ptr<Signal> s, sigs)
111 {
112 assert(s);
3e46726a
JH
113
114 const QRect label_rect(0, offset,
115 LabelMarginWidth, SignalHeight);
116 s->paint_label(painter, label_rect);
117
118 offset += SignalHeight;
e3f65ace 119 }
3e46726a 120
e9c41f8c
JH
121 // Paint the ruler
122 paintRuler(painter);
123
3e46726a 124 painter.end();
6fa02541 125}
009e1503
JH
126
127void SigView::dataUpdated()
128{
e3f65ace 129 update();
009e1503
JH
130}
131
7cd5faf8
JH
132void SigView::mouseMoveEvent(QMouseEvent *event)
133{
134 assert(event);
135}
136
137void SigView::mousePressEvent(QMouseEvent *event)
138{
139 assert(event);
140}
141
142void SigView::mouseReleaseEvent(QMouseEvent *event)
143{
144 assert(event);
145
fb0d32cb
JH
146 const double cursor_offset = _offset + _scale * (double)event->x();
147
7cd5faf8
JH
148 switch(event->button())
149 {
150 case Qt::LeftButton:
fb0d32cb 151 _scale *= 2.0 / 3.0;
7cd5faf8
JH
152 break;
153
154 case Qt::RightButton:
fb0d32cb 155 _scale *= 3.0 / 2.0;
7cd5faf8
JH
156 break;
157 }
158
fb0d32cb
JH
159 _offset = cursor_offset - _scale * (double)event->x();
160
3e46726a 161 update();
7cd5faf8
JH
162}
163
3e46726a
JH
164void SigView::setupViewport(int width, int height)
165{
166 glViewport(0, 0, (GLint)width, (GLint)height);
167 glMatrixMode(GL_PROJECTION);
168 glLoadIdentity();
169 glOrtho(0, width, height, 0, -1, 1);
170 glMatrixMode(GL_MODELVIEW);
171}
e9c41f8c
JH
172
173void SigView::paintRuler(QPainter &p)
174{
22016ad3 175 const double MinSpacing = 80;
e9c41f8c 176
e9c41f8c
JH
177 const double min_period = _scale * MinSpacing;
178
22016ad3
JH
179 const int order = (int)floorf(log10f(min_period));
180 const double order_decimal = pow(10, order);
181
182 int unit = 0;
183 double tick_period = 0.0f;
184
185 do
e9c41f8c 186 {
22016ad3
JH
187 tick_period = order_decimal * ScaleUnits[unit++];
188 } while(tick_period < min_period && unit < countof(ScaleUnits));
e9c41f8c 189
22016ad3
JH
190 const int prefix = (order - FirstSIPrefixPower) / 3;
191 assert(prefix >= 0);
192 assert(prefix < countof(SIPrefixes));
e9c41f8c 193
22016ad3
JH
194 const int text_height = p.boundingRect(0, 0, INT_MAX, INT_MAX,
195 Qt::AlignLeft | Qt::AlignTop, "8").height();
e9c41f8c 196
22016ad3 197 // Draw the tick marks
e9c41f8c
JH
198 p.setPen(Qt::black);
199
22016ad3
JH
200 double t = ceil(_offset / tick_period) * tick_period;
201 double x = 0.0;
202 while((x = (t - _offset) / _scale + LabelMarginWidth) < width())
e9c41f8c 203 {
22016ad3
JH
204 QString s;
205 QTextStream ts(&s);
206 ts << (t / order_decimal) << SIPrefixes[prefix] << "s";
207 p.drawText(x, 0, 0, text_height, Qt::AlignCenter | Qt::AlignTop |
208 Qt::TextDontClip, s);
209 p.drawLine(x, text_height, x, RulerHeight);
210 t += tick_period;
e9c41f8c
JH
211 }
212}