2 * This file is part of the sigrok project.
4 * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk>
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.
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.
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
24 #include "../../sigsession.h"
25 #include "../../signal.h"
27 #include <QMouseEvent>
29 #include <boost/foreach.hpp>
31 using namespace boost;
37 Viewport::Viewport(View &parent) :
41 setMouseTracking(true);
42 setAutoFillBackground(false);
45 int Viewport::get_total_height() const
48 BOOST_FOREACH(const shared_ptr<Signal> s,
49 _view.session().get_signals()) {
51 height += View::SignalHeight;
57 void Viewport::initializeGL()
61 void Viewport::resizeGL(int width, int height)
63 setup_viewport(width, height);
66 void Viewport::paintEvent(QPaintEvent *event)
70 const vector< shared_ptr<Signal> > &sigs =
71 _view.session().get_signals();
73 // Prepare for OpenGL rendering
75 glMatrixMode(GL_MODELVIEW);
78 setup_viewport(width(), height());
80 qglClearColor(Qt::white);
81 glClear(GL_COLOR_BUFFER_BIT);
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)
91 const QRect signal_rect(0, offset,
92 width(), View::SignalHeight);
94 s->paint(*this, signal_rect, _view.scale(), _view.offset());
96 offset += View::SignalHeight;
99 glDisable(GL_SCISSOR_TEST);
101 // Prepare for QPainter rendering
102 glMatrixMode(GL_MODELVIEW);
105 QPainter painter(this);
109 void Viewport::mousePressEvent(QMouseEvent *event)
113 _mouse_down_point = event->pos();
114 _mouse_down_offset = _view.offset();
117 void Viewport::mouseMoveEvent(QMouseEvent *event)
121 if(event->buttons() & Qt::LeftButton)
123 _view.set_scale_offset(_view.scale(),
125 (_mouse_down_point - event->pos()).x() *
130 void Viewport::mouseReleaseEvent(QMouseEvent *event)
135 void Viewport::wheelEvent(QWheelEvent *event)
138 _view.zoom(event->delta() / 120, event->x());
141 void Viewport::setup_viewport(int width, int height)
143 glViewport(0, 0, (GLint)width, (GLint)height);
144 glMatrixMode(GL_PROJECTION);
146 glOrtho(0, width, height, 0, -1, 1);
147 glMatrixMode(GL_MODELVIEW);