]> sigrok.org Git - pulseview.git/blob - pv/view/viewport.cpp
Initial work moving ruler into the pv::view::Ruler widget
[pulseview.git] / pv / view / viewport.cpp
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 <QMouseEvent>
28
29 #include <boost/foreach.hpp>
30
31 using namespace boost;
32 using namespace std;
33
34 namespace pv {
35 namespace view {
36
37 Viewport::Viewport(View &parent) :
38         QGLWidget(&parent),
39         _view(parent)
40 {
41         setMouseTracking(true);
42         setAutoFillBackground(false);
43 }
44
45 int Viewport::get_total_height() const
46 {
47         int height = 0;
48         BOOST_FOREACH(const shared_ptr<Signal> s,
49                 _view.session().get_signals()) {
50                 assert(s);
51                 height += View::SignalHeight;
52         }
53
54         return height;
55 }
56
57 void Viewport::initializeGL()
58 {
59 }
60
61 void Viewport::resizeGL(int width, int height)
62 {
63         setup_viewport(width, height);
64 }
65
66 void Viewport::paintEvent(QPaintEvent *event)
67 {
68         int offset;
69
70         const vector< shared_ptr<Signal> > &sigs =
71                 _view.session().get_signals();
72
73         // Prepare for OpenGL rendering
74         makeCurrent();
75         glMatrixMode(GL_MODELVIEW);
76         glPushMatrix();
77
78         setup_viewport(width(), height());
79
80         qglClearColor(Qt::white);
81         glClear(GL_COLOR_BUFFER_BIT);
82
83         // Plot the signal
84         glEnable(GL_SCISSOR_TEST);
85         glScissor(0, 0, width(), height());
86         offset = -_view.v_offset();
87         BOOST_FOREACH(const shared_ptr<Signal> s, sigs)
88         {
89                 assert(s);
90
91                 const QRect signal_rect(0, offset,
92                         width(), View::SignalHeight);
93
94                 s->paint(*this, signal_rect, _view.scale(), _view.offset());
95
96                 offset += View::SignalHeight;
97         }
98
99         glDisable(GL_SCISSOR_TEST);
100
101         // Prepare for QPainter rendering
102         glMatrixMode(GL_MODELVIEW);
103         glPopMatrix();
104
105         QPainter painter(this);
106         painter.end();
107 }
108
109 void Viewport::mousePressEvent(QMouseEvent *event)
110 {
111         assert(event);
112
113         _mouse_down_point = event->pos();
114         _mouse_down_offset = _view.offset();
115 }
116
117 void Viewport::mouseMoveEvent(QMouseEvent *event)
118 {
119         assert(event);
120
121         if(event->buttons() & Qt::LeftButton)
122         {
123                 _view.set_scale_offset(_view.scale(),
124                         _mouse_down_offset +
125                         (_mouse_down_point - event->pos()).x() *
126                         _view.scale());
127         }
128 }
129
130 void Viewport::mouseReleaseEvent(QMouseEvent *event)
131 {
132         assert(event);
133 }
134
135 void Viewport::wheelEvent(QWheelEvent *event)
136 {
137         assert(event);
138         _view.zoom(event->delta() / 120, event->x());
139 }
140
141 void Viewport::setup_viewport(int width, int height)
142 {
143         glViewport(0, 0, (GLint)width, (GLint)height);
144         glMatrixMode(GL_PROJECTION);
145         glLoadIdentity();
146         glOrtho(0, width, height, 0, -1, 1);
147         glMatrixMode(GL_MODELVIEW);
148 }
149
150 } // namespace view
151 } // namespace pv