2 * This file is part of the PulseView 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
25 #include "../sigsession.h"
27 #include <QMouseEvent>
29 #include <boost/foreach.hpp>
31 using boost::shared_ptr;
39 Viewport::Viewport(View &parent) :
43 setMouseTracking(true);
44 setAutoFillBackground(true);
45 setBackgroundRole(QPalette::Base);
47 connect(&_view.session(), SIGNAL(signals_changed()),
48 this, SLOT(on_signals_changed()));
50 connect(&_view, SIGNAL(signals_moved()),
51 this, SLOT(on_signals_moved()));
54 int Viewport::get_total_height() const
57 const vector< shared_ptr<Trace> > traces(_view.get_traces());
58 BOOST_FOREACH(const shared_ptr<Trace> t, traces) {
60 h = max(t->get_v_offset() + View::SignalHeight, h);
66 void Viewport::paintEvent(QPaintEvent*)
68 const vector< shared_ptr<Trace> > traces(_view.get_traces());
71 p.setRenderHint(QPainter::Antialiasing);
73 if (_view.cursors_shown())
74 _view.cursors().draw_viewport_background(p, rect());
77 BOOST_FOREACH(const shared_ptr<Trace> t, traces)
80 t->paint_back(p, 0, width());
83 BOOST_FOREACH(const shared_ptr<Trace> t, traces)
84 t->paint_mid(p, 0, width());
86 BOOST_FOREACH(const shared_ptr<Trace> t, traces)
87 t->paint_fore(p, 0, width());
89 if (_view.cursors_shown())
90 _view.cursors().draw_viewport_foreground(p, rect());
95 void Viewport::mousePressEvent(QMouseEvent *event)
99 _mouse_down_point = event->pos();
100 _mouse_down_offset = _view.offset();
103 void Viewport::mouseMoveEvent(QMouseEvent *event)
107 if (event->buttons() & Qt::LeftButton)
109 _view.set_scale_offset(_view.scale(),
111 (_mouse_down_point - event->pos()).x() *
116 void Viewport::mouseDoubleClickEvent(QMouseEvent *event)
120 if (event->buttons() & Qt::LeftButton)
121 _view.zoom(2.0, event->x());
122 else if (event->buttons() & Qt::RightButton)
123 _view.zoom(-2.0, event->x());
126 void Viewport::wheelEvent(QWheelEvent *event)
130 if (event->orientation() == Qt::Vertical) {
131 // Vertical scrolling is interpreted as zooming in/out
132 _view.zoom(event->delta() / 120, event->x());
133 } else if (event->orientation() == Qt::Horizontal) {
134 // Horizontal scrolling is interpreted as moving left/right
135 _view.set_scale_offset(_view.scale(),
136 event->delta() * _view.scale()
141 void Viewport::on_signals_changed()
143 const vector< shared_ptr<Trace> > traces(_view.get_traces());
144 BOOST_FOREACH(shared_ptr<Trace> t, traces) {
146 connect(t.get(), SIGNAL(visibility_changed()),
147 this, SLOT(update()));
151 void Viewport::on_signals_moved()