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