]> sigrok.org Git - pulseview.git/blame_incremental - pv/view/viewport.cpp
pulseview.qrc: Add missing application-exit.png entry.
[pulseview.git] / pv / view / viewport.cpp
... / ...
CommitLineData
1/*
2 * This file is part of the PulseView 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
21#include "view.h"
22#include "viewport.h"
23
24#include "signal.h"
25#include "../sigsession.h"
26
27#include <QMouseEvent>
28
29#include <boost/foreach.hpp>
30
31using namespace boost;
32using namespace std;
33
34namespace pv {
35namespace view {
36
37Viewport::Viewport(View &parent) :
38 QWidget(&parent),
39 _view(parent)
40{
41 setMouseTracking(true);
42 setAutoFillBackground(true);
43 setBackgroundRole(QPalette::Base);
44
45 connect(&_view, SIGNAL(signals_moved()),
46 this, SLOT(on_signals_moved()));
47}
48
49int Viewport::get_total_height() const
50{
51 int h = 0;
52 const vector< shared_ptr<Signal> > sigs(
53 _view.session().get_signals());
54 BOOST_FOREACH(const shared_ptr<Signal> s, sigs) {
55 assert(s);
56 h = max(s->get_v_offset() + View::SignalHeight, h);
57 }
58
59 return h;
60}
61
62void Viewport::paintEvent(QPaintEvent *event)
63{
64 const vector< shared_ptr<Signal> > sigs(
65 _view.session().get_signals());
66
67 QPainter p(this);
68 p.setRenderHint(QPainter::Antialiasing);
69
70 draw_cursors_background(p);
71
72 // Plot the signal
73 const int v_offset = _view.v_offset();
74 BOOST_FOREACH(const shared_ptr<Signal> s, sigs)
75 {
76 assert(s);
77
78 const QRect signal_rect(0, s->get_v_offset() - v_offset,
79 width(), View::SignalHeight);
80
81 s->paint(p, signal_rect, _view.scale(), _view.offset());
82 }
83
84 draw_cursors_foreground(p);
85
86 p.end();
87}
88
89void Viewport::mousePressEvent(QMouseEvent *event)
90{
91 assert(event);
92
93 _mouse_down_point = event->pos();
94 _mouse_down_offset = _view.offset();
95}
96
97void Viewport::mouseMoveEvent(QMouseEvent *event)
98{
99 assert(event);
100
101 if (event->buttons() & Qt::LeftButton)
102 {
103 _view.set_scale_offset(_view.scale(),
104 _mouse_down_offset +
105 (_mouse_down_point - event->pos()).x() *
106 _view.scale());
107 }
108}
109
110void Viewport::mouseReleaseEvent(QMouseEvent *event)
111{
112 assert(event);
113}
114
115void Viewport::wheelEvent(QWheelEvent *event)
116{
117 assert(event);
118
119 if (event->orientation() == Qt::Vertical) {
120 // Vertical scrolling is interpreted as zooming in/out
121 _view.zoom(event->delta() / 120, event->x());
122 } else if (event->orientation() == Qt::Horizontal) {
123 // Horizontal scrolling is interpreted as moving left/right
124 _view.set_scale_offset(_view.scale(),
125 event->delta() * _view.scale()
126 + _view.offset());
127 }
128}
129
130void Viewport::draw_cursors_background(QPainter &p)
131{
132 if (!_view.cursors_shown())
133 return;
134
135 p.setPen(Qt::NoPen);
136 p.setBrush(QBrush(View::CursorAreaColour));
137
138 const pair<Cursor, Cursor> &c = _view.cursors();
139 const float x1 = (c.first.time() - _view.offset()) / _view.scale();
140 const float x2 = (c.second.time() - _view.offset()) / _view.scale();
141 const int l = (int)max(min(x1, x2), 0.0f);
142 const int r = (int)min(max(x1, x2), (float)width());
143
144 p.drawRect(l, 0, r - l, height());
145}
146
147void Viewport::draw_cursors_foreground(QPainter &p)
148{
149 if (!_view.cursors_shown())
150 return;
151
152 const QRect r = rect();
153 pair<Cursor, Cursor> &cursors = _view.cursors();
154 cursors.first.paint(p, r);
155 cursors.second.paint(p, r);
156}
157
158void Viewport::on_signals_moved()
159{
160 update();
161}
162
163} // namespace view
164} // namespace pv