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