]> sigrok.org Git - pulseview.git/blame_incremental - pv/view/viewport.cpp
Replaced BOOST_FOREACH with C++11 range-based for loops
[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
29using boost::shared_ptr;
30using std::max;
31using std::min;
32using std::vector;
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.session(), SIGNAL(signals_changed()),
46 this, SLOT(on_signals_changed()));
47
48 connect(&_view, SIGNAL(signals_moved()),
49 this, SLOT(on_signals_moved()));
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();
54}
55
56int Viewport::get_total_height() const
57{
58 int h = 0;
59 const vector< shared_ptr<Trace> > traces(_view.get_traces());
60 for (const shared_ptr<Trace> t : traces) {
61 assert(t);
62 h = max(t->get_v_offset() + View::SignalHeight, h);
63 }
64
65 return h;
66}
67
68void Viewport::paintEvent(QPaintEvent*)
69{
70 const vector< shared_ptr<Trace> > traces(_view.get_traces());
71
72 QPainter p(this);
73 p.setRenderHint(QPainter::Antialiasing);
74
75 if (_view.cursors_shown())
76 _view.cursors().draw_viewport_background(p, rect());
77
78 // Plot the signal
79 for (const shared_ptr<Trace> t : traces)
80 {
81 assert(t);
82 t->paint_back(p, 0, width());
83 }
84
85 for (const shared_ptr<Trace> t : traces)
86 t->paint_mid(p, 0, width());
87
88 for (const shared_ptr<Trace> t : traces)
89 t->paint_fore(p, 0, width());
90
91 if (_view.cursors_shown())
92 _view.cursors().draw_viewport_foreground(p, rect());
93
94 p.end();
95}
96
97void Viewport::mousePressEvent(QMouseEvent *event)
98{
99 assert(event);
100
101 _mouse_down_point = event->pos();
102 _mouse_down_offset = _view.offset();
103}
104
105void Viewport::mouseMoveEvent(QMouseEvent *event)
106{
107 assert(event);
108
109 if (event->buttons() & Qt::LeftButton)
110 {
111 _view.set_scale_offset(_view.scale(),
112 _mouse_down_offset +
113 (_mouse_down_point - event->pos()).x() *
114 _view.scale());
115 }
116}
117
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
128void Viewport::wheelEvent(QWheelEvent *event)
129{
130 assert(event);
131
132 if (event->orientation() == Qt::Vertical) {
133 // Vertical scrolling is interpreted as zooming in/out
134 _view.zoom(event->delta() / 120, event->x());
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());
140 }
141}
142
143void Viewport::on_signals_changed()
144{
145 const vector< shared_ptr<Trace> > traces(_view.get_traces());
146 for (shared_ptr<Trace> t : traces) {
147 assert(t);
148 connect(t.get(), SIGNAL(visibility_changed()),
149 this, SLOT(update()));
150 }
151}
152
153void Viewport::on_signals_moved()
154{
155 update();
156}
157
158} // namespace view
159} // namespace pv