]>
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 | ||
31 | using namespace boost; | |
32 | using namespace std; | |
33 | ||
cdf7bea7 JH |
34 | namespace pv { |
35 | namespace view { | |
e9c41f8c | 36 | |
cdf7bea7 | 37 | Viewport::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 JH |
44 | |
45 | connect(&_view, SIGNAL(signals_moved()), | |
46 | this, SLOT(on_signals_moved())); | |
d7002724 JH |
47 | } |
48 | ||
cdf7bea7 | 49 | int Viewport::get_total_height() const |
a429590b | 50 | { |
07204819 | 51 | int h = 0; |
3868e5fa JH |
52 | const vector< shared_ptr<Signal> > sigs( |
53 | _view.session().get_signals()); | |
54 | BOOST_FOREACH(const shared_ptr<Signal> s, sigs) { | |
adb4b10c | 55 | assert(s); |
07204819 | 56 | h = max(s->get_v_offset() + View::SignalHeight, h); |
adb4b10c JH |
57 | } |
58 | ||
07204819 | 59 | return h; |
a429590b JH |
60 | } |
61 | ||
e314eca4 | 62 | void Viewport::paintEvent(QPaintEvent*) |
d7002724 | 63 | { |
3868e5fa JH |
64 | const vector< shared_ptr<Signal> > sigs( |
65 | _view.session().get_signals()); | |
3e46726a | 66 | |
64b60583 | 67 | QPainter p(this); |
ce6e73a8 | 68 | p.setRenderHint(QPainter::Antialiasing); |
3e46726a | 69 | |
f76af637 JH |
70 | draw_cursors_background(p); |
71 | ||
3e46726a | 72 | // Plot the signal |
2e575351 | 73 | const int v_offset = _view.v_offset(); |
3e46726a JH |
74 | BOOST_FOREACH(const shared_ptr<Signal> s, sigs) |
75 | { | |
76 | assert(s); | |
2658961b JH |
77 | s->paint(p, s->get_v_offset() - v_offset, 0, width(), |
78 | _view.scale(), _view.offset()); | |
3e46726a JH |
79 | } |
80 | ||
f76af637 JH |
81 | draw_cursors_foreground(p); |
82 | ||
64b60583 | 83 | p.end(); |
6fa02541 | 84 | } |
009e1503 | 85 | |
cdf7bea7 | 86 | void Viewport::mousePressEvent(QMouseEvent *event) |
7cd5faf8 JH |
87 | { |
88 | assert(event); | |
1dd95c70 JH |
89 | |
90 | _mouse_down_point = event->pos(); | |
adb4b10c | 91 | _mouse_down_offset = _view.offset(); |
7cd5faf8 JH |
92 | } |
93 | ||
cdf7bea7 | 94 | void Viewport::mouseMoveEvent(QMouseEvent *event) |
7cd5faf8 JH |
95 | { |
96 | assert(event); | |
1dd95c70 | 97 | |
333d5bbc | 98 | if (event->buttons() & Qt::LeftButton) |
1dd95c70 | 99 | { |
adb4b10c JH |
100 | _view.set_scale_offset(_view.scale(), |
101 | _mouse_down_offset + | |
102 | (_mouse_down_point - event->pos()).x() * | |
103 | _view.scale()); | |
1dd95c70 | 104 | } |
7cd5faf8 JH |
105 | } |
106 | ||
cdf7bea7 | 107 | void Viewport::mouseReleaseEvent(QMouseEvent *event) |
7cd5faf8 JH |
108 | { |
109 | assert(event); | |
1dd95c70 | 110 | } |
7cd5faf8 | 111 | |
cdf7bea7 | 112 | void Viewport::wheelEvent(QWheelEvent *event) |
1dd95c70 JH |
113 | { |
114 | assert(event); | |
ffd5cd20 AG |
115 | |
116 | if (event->orientation() == Qt::Vertical) { | |
117 | // Vertical scrolling is interpreted as zooming in/out | |
118 | _view.zoom(event->delta() / 120, event->x()); | |
23840406 AG |
119 | } else if (event->orientation() == Qt::Horizontal) { |
120 | // Horizontal scrolling is interpreted as moving left/right | |
121 | _view.set_scale_offset(_view.scale(), | |
122 | event->delta() * _view.scale() | |
123 | + _view.offset()); | |
ffd5cd20 | 124 | } |
7cd5faf8 JH |
125 | } |
126 | ||
f76af637 JH |
127 | void Viewport::draw_cursors_background(QPainter &p) |
128 | { | |
333d5bbc | 129 | if (!_view.cursors_shown()) |
f76af637 JH |
130 | return; |
131 | ||
132 | p.setPen(Qt::NoPen); | |
133 | p.setBrush(QBrush(View::CursorAreaColour)); | |
134 | ||
135 | const pair<Cursor, Cursor> &c = _view.cursors(); | |
136 | const float x1 = (c.first.time() - _view.offset()) / _view.scale(); | |
137 | const float x2 = (c.second.time() - _view.offset()) / _view.scale(); | |
138 | const int l = (int)max(min(x1, x2), 0.0f); | |
139 | const int r = (int)min(max(x1, x2), (float)width()); | |
140 | ||
141 | p.drawRect(l, 0, r - l, height()); | |
142 | } | |
143 | ||
144 | void Viewport::draw_cursors_foreground(QPainter &p) | |
145 | { | |
333d5bbc | 146 | if (!_view.cursors_shown()) |
f76af637 JH |
147 | return; |
148 | ||
149 | const QRect r = rect(); | |
150 | pair<Cursor, Cursor> &cursors = _view.cursors(); | |
151 | cursors.first.paint(p, r); | |
152 | cursors.second.paint(p, r); | |
153 | } | |
154 | ||
07204819 JH |
155 | void Viewport::on_signals_moved() |
156 | { | |
157 | update(); | |
158 | } | |
159 | ||
cdf7bea7 JH |
160 | } // namespace view |
161 | } // namespace pv |