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>
31 using std::shared_ptr;
37 Viewport::Viewport(View &parent) :
41 setMouseTracking(true);
42 setAutoFillBackground(true);
43 setBackgroundRole(QPalette::Base);
45 connect(&_view.session(), SIGNAL(signals_changed()),
46 this, SLOT(on_signals_changed()));
48 connect(&_view, SIGNAL(signals_moved()),
49 this, SLOT(on_signals_moved()));
51 // Trigger the initial event manually. The default device has signals
52 // which were created before this object came into being
56 int Viewport::get_total_height() const
59 const vector< shared_ptr<Trace> > traces(_view.get_traces());
60 for (const shared_ptr<Trace> t : traces) {
62 h = max(t->get_v_offset() + View::SignalHeight, h);
68 void Viewport::paintEvent(QPaintEvent*)
70 const vector< shared_ptr<Trace> > traces(_view.get_traces());
73 p.setRenderHint(QPainter::Antialiasing);
75 if (_view.cursors_shown())
76 _view.cursors().draw_viewport_background(p, rect());
79 for (const shared_ptr<Trace> t : traces)
82 t->paint_back(p, 0, width());
85 for (const shared_ptr<Trace> t : traces)
86 t->paint_mid(p, 0, width());
88 for (const shared_ptr<Trace> t : traces)
89 t->paint_fore(p, 0, width());
91 if (_view.cursors_shown())
92 _view.cursors().draw_viewport_foreground(p, rect());
97 void Viewport::mousePressEvent(QMouseEvent *event)
101 _mouse_down_point = event->pos();
102 _mouse_down_offset = _view.offset();
105 void Viewport::mouseMoveEvent(QMouseEvent *event)
109 if (event->buttons() & Qt::LeftButton)
111 _view.set_scale_offset(_view.scale(),
113 (_mouse_down_point - event->pos()).x() *
118 void Viewport::mouseDoubleClickEvent(QMouseEvent *event)
122 if (event->buttons() & Qt::LeftButton)
123 _view.zoom(2.0, event->x());
124 else if (event->buttons() & Qt::RightButton)
125 _view.zoom(-2.0, event->x());
128 void Viewport::wheelEvent(QWheelEvent *event)
132 if (event->orientation() == Qt::Vertical) {
133 // Vertical scrolling is interpreted as zooming in/out
134 _view.zoom(event->delta() / 120, event->x());
135 } else if (event->orientation() == Qt::Horizontal) {
136 // Horizontal scrolling is interpreted as moving left/right
137 _view.set_scale_offset(_view.scale(),
138 event->delta() * _view.scale()
143 void Viewport::on_signals_changed()
145 const vector< shared_ptr<Trace> > traces(_view.get_traces());
146 for (shared_ptr<Trace> t : traces) {
148 connect(t.get(), SIGNAL(visibility_changed()),
149 this, SLOT(update()));
153 void Viewport::on_signals_moved()