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