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