]> sigrok.org Git - pulseview.git/blame - pv/view/viewport.cpp
Update to new session API.
[pulseview.git] / pv / view / viewport.cpp
CommitLineData
6fa02541 1/*
b3f22de0 2 * This file is part of the PulseView project.
6fa02541
JH
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
d2344534
JH
21#include <cassert>
22
cdf7bea7
JH
23#include "view.h"
24#include "viewport.h"
6fa02541 25
8d634081 26#include "signal.h"
51e77110 27#include "../sigsession.h"
e3f65ace 28
7cd5faf8 29#include <QMouseEvent>
e9c41f8c 30
819f4c25
JH
31using std::max;
32using std::min;
f9abf97e 33using std::shared_ptr;
819f4c25 34using std::vector;
e3f65ace 35
cdf7bea7
JH
36namespace pv {
37namespace view {
e9c41f8c 38
cdf7bea7 39Viewport::Viewport(View &parent) :
64b60583 40 QWidget(&parent),
adb4b10c 41 _view(parent)
6fa02541 42{
d7002724 43 setMouseTracking(true);
64b60583
JH
44 setAutoFillBackground(true);
45 setBackgroundRole(QPalette::Base);
07204819 46
aca00b1e
JH
47 connect(&_view.session(), SIGNAL(signals_changed()),
48 this, SLOT(on_signals_changed()));
49
07204819
JH
50 connect(&_view, SIGNAL(signals_moved()),
51 this, SLOT(on_signals_moved()));
9f46d905
JH
52
53 // Trigger the initial event manually. The default device has signals
54 // which were created before this object came into being
55 on_signals_changed();
d7002724
JH
56}
57
cdf7bea7 58int Viewport::get_total_height() const
a429590b 59{
07204819 60 int h = 0;
38eeddea 61 const vector< shared_ptr<Trace> > traces(_view.get_traces());
d9aecf1f 62 for (const shared_ptr<Trace> t : traces) {
38eeddea
JH
63 assert(t);
64 h = max(t->get_v_offset() + View::SignalHeight, h);
adb4b10c
JH
65 }
66
07204819 67 return h;
a429590b
JH
68}
69
e314eca4 70void Viewport::paintEvent(QPaintEvent*)
d7002724 71{
38eeddea 72 const vector< shared_ptr<Trace> > traces(_view.get_traces());
3e46726a 73
64b60583 74 QPainter p(this);
ce6e73a8 75 p.setRenderHint(QPainter::Antialiasing);
3e46726a 76
0ba172cf
JH
77 if (_view.cursors_shown())
78 _view.cursors().draw_viewport_background(p, rect());
f76af637 79
3e46726a 80 // Plot the signal
d9aecf1f 81 for (const shared_ptr<Trace> t : traces)
3e46726a 82 {
38eeddea 83 assert(t);
fe08b6e8 84 t->paint_back(p, 0, width());
3e46726a
JH
85 }
86
d9aecf1f 87 for (const shared_ptr<Trace> t : traces)
fe08b6e8
JH
88 t->paint_mid(p, 0, width());
89
d9aecf1f 90 for (const shared_ptr<Trace> t : traces)
fe08b6e8
JH
91 t->paint_fore(p, 0, width());
92
0ba172cf
JH
93 if (_view.cursors_shown())
94 _view.cursors().draw_viewport_foreground(p, rect());
f76af637 95
64b60583 96 p.end();
6fa02541 97}
009e1503 98
cdf7bea7 99void Viewport::mousePressEvent(QMouseEvent *event)
7cd5faf8
JH
100{
101 assert(event);
1dd95c70
JH
102
103 _mouse_down_point = event->pos();
adb4b10c 104 _mouse_down_offset = _view.offset();
7cd5faf8
JH
105}
106
cdf7bea7 107void Viewport::mouseMoveEvent(QMouseEvent *event)
7cd5faf8
JH
108{
109 assert(event);
1dd95c70 110
333d5bbc 111 if (event->buttons() & Qt::LeftButton)
1dd95c70 112 {
adb4b10c
JH
113 _view.set_scale_offset(_view.scale(),
114 _mouse_down_offset +
115 (_mouse_down_point - event->pos()).x() *
116 _view.scale());
1dd95c70 117 }
7cd5faf8
JH
118}
119
903038a8
JH
120void Viewport::mouseDoubleClickEvent(QMouseEvent *event)
121{
122 assert(event);
123
124 if (event->buttons() & Qt::LeftButton)
125 _view.zoom(2.0, event->x());
126 else if (event->buttons() & Qt::RightButton)
127 _view.zoom(-2.0, event->x());
128}
129
cdf7bea7 130void Viewport::wheelEvent(QWheelEvent *event)
1dd95c70
JH
131{
132 assert(event);
ffd5cd20
AG
133
134 if (event->orientation() == Qt::Vertical) {
135 // Vertical scrolling is interpreted as zooming in/out
136 _view.zoom(event->delta() / 120, event->x());
23840406
AG
137 } else if (event->orientation() == Qt::Horizontal) {
138 // Horizontal scrolling is interpreted as moving left/right
139 _view.set_scale_offset(_view.scale(),
140 event->delta() * _view.scale()
141 + _view.offset());
ffd5cd20 142 }
7cd5faf8
JH
143}
144
aca00b1e
JH
145void Viewport::on_signals_changed()
146{
147 const vector< shared_ptr<Trace> > traces(_view.get_traces());
d9aecf1f 148 for (shared_ptr<Trace> t : traces) {
aca00b1e
JH
149 assert(t);
150 connect(t.get(), SIGNAL(visibility_changed()),
151 this, SLOT(update()));
152 }
153}
154
07204819
JH
155void Viewport::on_signals_moved()
156{
157 update();
158}
159
cdf7bea7
JH
160} // namespace view
161} // namespace pv