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