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 namespace boost;
37 Viewport::Viewport(View &parent) :
41 setMouseTracking(true);
42 setAutoFillBackground(true);
43 setBackgroundRole(QPalette::Base);
46 int Viewport::get_total_height() const
49 BOOST_FOREACH(const shared_ptr<Signal> s,
50 _view.session().get_signals()) {
52 height += View::SignalHeight;
58 void Viewport::paintEvent(QPaintEvent *event)
60 const vector< shared_ptr<Signal> > &sigs =
61 _view.session().get_signals();
64 p.setRenderHint(QPainter::Antialiasing);
66 draw_cursors_background(p);
69 int offset = -_view.v_offset();
70 BOOST_FOREACH(const shared_ptr<Signal> s, sigs)
74 const QRect signal_rect(0, offset,
75 width(), View::SignalHeight);
77 s->paint(p, signal_rect, _view.scale(), _view.offset());
79 offset += View::SignalHeight;
82 draw_cursors_foreground(p);
87 void Viewport::mousePressEvent(QMouseEvent *event)
91 _mouse_down_point = event->pos();
92 _mouse_down_offset = _view.offset();
95 void Viewport::mouseMoveEvent(QMouseEvent *event)
99 if(event->buttons() & Qt::LeftButton)
101 _view.set_scale_offset(_view.scale(),
103 (_mouse_down_point - event->pos()).x() *
108 void Viewport::mouseReleaseEvent(QMouseEvent *event)
113 void Viewport::wheelEvent(QWheelEvent *event)
116 _view.zoom(event->delta() / 120, event->x());
119 void Viewport::draw_cursors_background(QPainter &p)
121 if(!_view.cursors_shown())
125 p.setBrush(QBrush(View::CursorAreaColour));
127 const pair<Cursor, Cursor> &c = _view.cursors();
128 const float x1 = (c.first.time() - _view.offset()) / _view.scale();
129 const float x2 = (c.second.time() - _view.offset()) / _view.scale();
130 const int l = (int)max(min(x1, x2), 0.0f);
131 const int r = (int)min(max(x1, x2), (float)width());
133 p.drawRect(l, 0, r - l, height());
136 void Viewport::draw_cursors_foreground(QPainter &p)
138 if(!_view.cursors_shown())
141 const QRect r = rect();
142 pair<Cursor, Cursor> &cursors = _view.cursors();
143 cursors.first.paint(p, r);
144 cursors.second.paint(p, r);