]>
Commit | Line | Data |
---|---|---|
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 | |
e3f65ace JH |
29 | #include <boost/foreach.hpp> |
30 | ||
819f4c25 JH |
31 | using boost::shared_ptr; |
32 | using std::max; | |
33 | using std::min; | |
34 | using std::vector; | |
e3f65ace | 35 | |
cdf7bea7 JH |
36 | namespace pv { |
37 | namespace view { | |
e9c41f8c | 38 | |
cdf7bea7 | 39 | Viewport::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())); | |
d7002724 JH |
52 | } |
53 | ||
cdf7bea7 | 54 | int Viewport::get_total_height() const |
a429590b | 55 | { |
07204819 | 56 | int h = 0; |
38eeddea JH |
57 | const vector< shared_ptr<Trace> > traces(_view.get_traces()); |
58 | BOOST_FOREACH(const shared_ptr<Trace> t, traces) { | |
59 | assert(t); | |
60 | h = max(t->get_v_offset() + View::SignalHeight, h); | |
adb4b10c JH |
61 | } |
62 | ||
07204819 | 63 | return h; |
a429590b JH |
64 | } |
65 | ||
e314eca4 | 66 | void Viewport::paintEvent(QPaintEvent*) |
d7002724 | 67 | { |
38eeddea | 68 | const vector< shared_ptr<Trace> > traces(_view.get_traces()); |
3e46726a | 69 | |
64b60583 | 70 | QPainter p(this); |
ce6e73a8 | 71 | p.setRenderHint(QPainter::Antialiasing); |
3e46726a | 72 | |
0ba172cf JH |
73 | if (_view.cursors_shown()) |
74 | _view.cursors().draw_viewport_background(p, rect()); | |
f76af637 | 75 | |
3e46726a | 76 | // Plot the signal |
38eeddea | 77 | BOOST_FOREACH(const shared_ptr<Trace> t, traces) |
3e46726a | 78 | { |
38eeddea | 79 | assert(t); |
fe08b6e8 | 80 | t->paint_back(p, 0, width()); |
3e46726a JH |
81 | } |
82 | ||
fe08b6e8 JH |
83 | BOOST_FOREACH(const shared_ptr<Trace> t, traces) |
84 | t->paint_mid(p, 0, width()); | |
85 | ||
86 | BOOST_FOREACH(const shared_ptr<Trace> t, traces) | |
87 | t->paint_fore(p, 0, width()); | |
88 | ||
0ba172cf JH |
89 | if (_view.cursors_shown()) |
90 | _view.cursors().draw_viewport_foreground(p, rect()); | |
f76af637 | 91 | |
64b60583 | 92 | p.end(); |
6fa02541 | 93 | } |
009e1503 | 94 | |
cdf7bea7 | 95 | void Viewport::mousePressEvent(QMouseEvent *event) |
7cd5faf8 JH |
96 | { |
97 | assert(event); | |
1dd95c70 JH |
98 | |
99 | _mouse_down_point = event->pos(); | |
adb4b10c | 100 | _mouse_down_offset = _view.offset(); |
7cd5faf8 JH |
101 | } |
102 | ||
cdf7bea7 | 103 | void Viewport::mouseMoveEvent(QMouseEvent *event) |
7cd5faf8 JH |
104 | { |
105 | assert(event); | |
1dd95c70 | 106 | |
333d5bbc | 107 | if (event->buttons() & Qt::LeftButton) |
1dd95c70 | 108 | { |
adb4b10c JH |
109 | _view.set_scale_offset(_view.scale(), |
110 | _mouse_down_offset + | |
111 | (_mouse_down_point - event->pos()).x() * | |
112 | _view.scale()); | |
1dd95c70 | 113 | } |
7cd5faf8 JH |
114 | } |
115 | ||
903038a8 JH |
116 | void Viewport::mouseDoubleClickEvent(QMouseEvent *event) |
117 | { | |
118 | assert(event); | |
119 | ||
120 | if (event->buttons() & Qt::LeftButton) | |
121 | _view.zoom(2.0, event->x()); | |
122 | else if (event->buttons() & Qt::RightButton) | |
123 | _view.zoom(-2.0, event->x()); | |
124 | } | |
125 | ||
cdf7bea7 | 126 | void Viewport::wheelEvent(QWheelEvent *event) |
1dd95c70 JH |
127 | { |
128 | assert(event); | |
ffd5cd20 AG |
129 | |
130 | if (event->orientation() == Qt::Vertical) { | |
131 | // Vertical scrolling is interpreted as zooming in/out | |
132 | _view.zoom(event->delta() / 120, event->x()); | |
23840406 AG |
133 | } else if (event->orientation() == Qt::Horizontal) { |
134 | // Horizontal scrolling is interpreted as moving left/right | |
135 | _view.set_scale_offset(_view.scale(), | |
136 | event->delta() * _view.scale() | |
137 | + _view.offset()); | |
ffd5cd20 | 138 | } |
7cd5faf8 JH |
139 | } |
140 | ||
aca00b1e JH |
141 | void Viewport::on_signals_changed() |
142 | { | |
143 | const vector< shared_ptr<Trace> > traces(_view.get_traces()); | |
144 | BOOST_FOREACH(shared_ptr<Trace> t, traces) { | |
145 | assert(t); | |
146 | connect(t.get(), SIGNAL(visibility_changed()), | |
147 | this, SLOT(update())); | |
148 | } | |
149 | } | |
150 | ||
07204819 JH |
151 | void Viewport::on_signals_moved() |
152 | { | |
153 | update(); | |
154 | } | |
155 | ||
cdf7bea7 JH |
156 | } // namespace view |
157 | } // namespace pv |