]> sigrok.org Git - pulseview.git/blob - pv/view/viewport.cpp
Replaced boost::function with std::function
[pulseview.git] / pv / view / viewport.cpp
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 <cassert>
22
23 #include "view.h"
24 #include "viewport.h"
25
26 #include "signal.h"
27 #include "../sigsession.h"
28
29 #include <QMouseEvent>
30
31 using std::max;
32 using std::min;
33 using std::shared_ptr;
34 using std::vector;
35
36 namespace pv {
37 namespace view {
38
39 Viewport::Viewport(View &parent) :
40         QWidget(&parent),
41         _view(parent)
42 {
43         setMouseTracking(true);
44         setAutoFillBackground(true);
45         setBackgroundRole(QPalette::Base);
46
47         connect(&_view.session(), SIGNAL(signals_changed()),
48                 this, SLOT(on_signals_changed()));
49
50         connect(&_view, SIGNAL(signals_moved()),
51                 this, SLOT(on_signals_moved()));
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();
56 }
57
58 int Viewport::get_total_height() const
59 {
60         int h = 0;
61         const vector< shared_ptr<Trace> > traces(_view.get_traces());
62         for (const shared_ptr<Trace> t : traces) {
63                 assert(t);
64                 h = max(t->get_v_offset() + View::SignalHeight, h);
65         }
66
67         return h;
68 }
69
70 void Viewport::paintEvent(QPaintEvent*)
71 {
72         const vector< shared_ptr<Trace> > traces(_view.get_traces());
73
74         QPainter p(this);
75         p.setRenderHint(QPainter::Antialiasing);
76
77         if (_view.cursors_shown())
78                 _view.cursors().draw_viewport_background(p, rect());
79
80         // Plot the signal
81         for (const shared_ptr<Trace> t : traces)
82         {
83                 assert(t);
84                 t->paint_back(p, 0, width());
85         }
86
87         for (const shared_ptr<Trace> t : traces)
88                 t->paint_mid(p, 0, width());
89
90         for (const shared_ptr<Trace> t : traces)
91                 t->paint_fore(p, 0, width());
92
93         if (_view.cursors_shown())
94                 _view.cursors().draw_viewport_foreground(p, rect());
95
96         p.end();
97 }
98
99 void Viewport::mousePressEvent(QMouseEvent *event)
100 {
101         assert(event);
102
103         _mouse_down_point = event->pos();
104         _mouse_down_offset = _view.offset();
105 }
106
107 void Viewport::mouseMoveEvent(QMouseEvent *event)
108 {
109         assert(event);
110
111         if (event->buttons() & Qt::LeftButton)
112         {
113                 _view.set_scale_offset(_view.scale(),
114                         _mouse_down_offset +
115                         (_mouse_down_point - event->pos()).x() *
116                         _view.scale());
117         }
118 }
119
120 void 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
130 void Viewport::wheelEvent(QWheelEvent *event)
131 {
132         assert(event);
133
134         if (event->orientation() == Qt::Vertical) {
135                 // Vertical scrolling is interpreted as zooming in/out
136                 _view.zoom(event->delta() / 120, event->x());
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());
142         }
143 }
144
145 void Viewport::on_signals_changed()
146 {
147         const vector< shared_ptr<Trace> > traces(_view.get_traces());
148         for (shared_ptr<Trace> t : traces) {
149                 assert(t);
150                 connect(t.get(), SIGNAL(visibility_changed()),
151                         this, SLOT(update()));
152         }
153 }
154
155 void Viewport::on_signals_moved()
156 {
157         update();
158 }
159
160 } // namespace view
161 } // namespace pv