]> sigrok.org Git - pulseview.git/blame_incremental - pv/view/viewport.cpp
Exposed View::_session with accessor function
[pulseview.git] / pv / view / viewport.cpp
... / ...
CommitLineData
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 "view.h"
22#include "viewport.h"
23
24#include "../../sigsession.h"
25#include "../../signal.h"
26
27#include "../../extdef.h"
28
29#include <QMouseEvent>
30#include <QTextStream>
31
32#include <math.h>
33
34#include <boost/foreach.hpp>
35
36using namespace boost;
37using namespace std;
38
39namespace pv {
40namespace view {
41
42const int Viewport::SignalHeight = 50;
43
44const int Viewport::MinorTickSubdivision = 4;
45const int Viewport::ScaleUnits[3] = {1, 2, 5};
46
47const QString Viewport::SIPrefixes[9] =
48 {"f", "p", "n", QChar(0x03BC), "m", "", "k", "M", "G"};
49const int Viewport::FirstSIPrefixPower = -15;
50
51Viewport::Viewport(View &parent) :
52 QGLWidget(&parent),
53 _view(parent)
54{
55 setMouseTracking(true);
56 setAutoFillBackground(false);
57}
58
59int Viewport::get_total_height() const
60{
61 int height = 0;
62 BOOST_FOREACH(const shared_ptr<Signal> s,
63 _view.session().get_signals()) {
64 assert(s);
65 height += SignalHeight;
66 }
67
68 return height;
69}
70
71void Viewport::initializeGL()
72{
73}
74
75void Viewport::resizeGL(int width, int height)
76{
77 setup_viewport(width, height);
78}
79
80void Viewport::paintEvent(QPaintEvent *event)
81{
82 int offset;
83
84 const vector< shared_ptr<Signal> > &sigs =
85 _view.session().get_signals();
86
87 // Prepare for OpenGL rendering
88 makeCurrent();
89 glMatrixMode(GL_MODELVIEW);
90 glPushMatrix();
91
92 setup_viewport(width(), height());
93
94 qglClearColor(Qt::white);
95 glClear(GL_COLOR_BUFFER_BIT);
96
97 // Plot the signal
98 glEnable(GL_SCISSOR_TEST);
99 glScissor(View::LabelMarginWidth, 0, width(), height());
100 offset = View::RulerHeight - _view.v_offset();
101 BOOST_FOREACH(const shared_ptr<Signal> s, sigs)
102 {
103 assert(s);
104
105 const QRect signal_rect(View::LabelMarginWidth, offset,
106 width() - View::LabelMarginWidth, SignalHeight);
107
108 s->paint(*this, signal_rect, _view.scale(), _view.offset());
109
110 offset += SignalHeight;
111 }
112
113 glDisable(GL_SCISSOR_TEST);
114
115 // Prepare for QPainter rendering
116 glMatrixMode(GL_MODELVIEW);
117 glPopMatrix();
118
119 QPainter painter(this);
120 painter.setRenderHint(QPainter::Antialiasing);
121
122 // Paint the labels
123 offset = View::RulerHeight - _view.v_offset();
124 BOOST_FOREACH(const shared_ptr<Signal> s, sigs)
125 {
126 assert(s);
127
128 const QRect label_rect(0, offset,
129 View::LabelMarginWidth, SignalHeight);
130 s->paint_label(painter, label_rect);
131
132 offset += SignalHeight;
133 }
134
135 // Paint the ruler
136 paint_ruler(painter);
137
138 painter.end();
139}
140
141void Viewport::mousePressEvent(QMouseEvent *event)
142{
143 assert(event);
144
145 _mouse_down_point = event->pos();
146 _mouse_down_offset = _view.offset();
147}
148
149void Viewport::mouseMoveEvent(QMouseEvent *event)
150{
151 assert(event);
152
153 if(event->buttons() & Qt::LeftButton)
154 {
155 _view.set_scale_offset(_view.scale(),
156 _mouse_down_offset +
157 (_mouse_down_point - event->pos()).x() *
158 _view.scale());
159 }
160}
161
162void Viewport::mouseReleaseEvent(QMouseEvent *event)
163{
164 assert(event);
165}
166
167void Viewport::wheelEvent(QWheelEvent *event)
168{
169 assert(event);
170 _view.zoom(event->delta() / 120, event->x() -
171 View::LabelMarginWidth);
172}
173
174void Viewport::setup_viewport(int width, int height)
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}
182
183void Viewport::paint_ruler(QPainter &p)
184{
185 const double MinSpacing = 80;
186
187 const double min_period = _view.scale() * MinSpacing;
188
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
196 {
197 tick_period = order_decimal * ScaleUnits[unit++];
198 } while(tick_period < min_period && unit < countof(ScaleUnits));
199
200 const int prefix = (order - FirstSIPrefixPower) / 3;
201 assert(prefix >= 0);
202 assert(prefix < countof(SIPrefixes));
203
204 const int text_height = p.boundingRect(0, 0, INT_MAX, INT_MAX,
205 Qt::AlignLeft | Qt::AlignTop, "8").height();
206
207 // Draw the tick marks
208 p.setPen(Qt::black);
209
210 const double minor_tick_period = tick_period / MinorTickSubdivision;
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);
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)
220 {
221 const double t = t0 + division * minor_tick_period;
222 const double x = (t - _view.offset()) / _view.scale() +
223 View::LabelMarginWidth;
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);
236 p.drawLine(x, text_height, x, View::RulerHeight);
237 }
238 else
239 {
240 // Draw a minor tick
241 p.drawLine(x,
242 (text_height + View::RulerHeight) / 2, x,
243 View::RulerHeight);
244 }
245
246 division++;
247 }
248}
249
250} // namespace view
251} // namespace pv