]> sigrok.org Git - pulseview.git/blame_incremental - pv/view/viewport.cpp
Added snap to grid drag behaviour
[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
46int Viewport::get_total_height() const
47{
48 int height = 0;
49 BOOST_FOREACH(const shared_ptr<Signal> s,
50 _view.session().get_signals()) {
51 assert(s);
52 height += View::SignalHeight;
53 }
54
55 return height;
56}
57
58void Viewport::paintEvent(QPaintEvent *event)
59{
60 const vector< shared_ptr<Signal> > &sigs =
61 _view.session().get_signals();
62
63 QPainter p(this);
64 p.setRenderHint(QPainter::Antialiasing);
65
66 draw_cursors_background(p);
67
68 // Plot the signal
69 const int v_offset = _view.v_offset();
70 BOOST_FOREACH(const shared_ptr<Signal> s, sigs)
71 {
72 assert(s);
73
74 const QRect signal_rect(0, s->get_v_offset() - v_offset,
75 width(), View::SignalHeight);
76
77 s->paint(p, signal_rect, _view.scale(), _view.offset());
78 }
79
80 draw_cursors_foreground(p);
81
82 p.end();
83}
84
85void Viewport::mousePressEvent(QMouseEvent *event)
86{
87 assert(event);
88
89 _mouse_down_point = event->pos();
90 _mouse_down_offset = _view.offset();
91}
92
93void Viewport::mouseMoveEvent(QMouseEvent *event)
94{
95 assert(event);
96
97 if(event->buttons() & Qt::LeftButton)
98 {
99 _view.set_scale_offset(_view.scale(),
100 _mouse_down_offset +
101 (_mouse_down_point - event->pos()).x() *
102 _view.scale());
103 }
104}
105
106void Viewport::mouseReleaseEvent(QMouseEvent *event)
107{
108 assert(event);
109}
110
111void Viewport::wheelEvent(QWheelEvent *event)
112{
113 assert(event);
114 _view.zoom(event->delta() / 120, event->x());
115}
116
117void Viewport::draw_cursors_background(QPainter &p)
118{
119 if(!_view.cursors_shown())
120 return;
121
122 p.setPen(Qt::NoPen);
123 p.setBrush(QBrush(View::CursorAreaColour));
124
125 const pair<Cursor, Cursor> &c = _view.cursors();
126 const float x1 = (c.first.time() - _view.offset()) / _view.scale();
127 const float x2 = (c.second.time() - _view.offset()) / _view.scale();
128 const int l = (int)max(min(x1, x2), 0.0f);
129 const int r = (int)min(max(x1, x2), (float)width());
130
131 p.drawRect(l, 0, r - l, height());
132}
133
134void Viewport::draw_cursors_foreground(QPainter &p)
135{
136 if(!_view.cursors_shown())
137 return;
138
139 const QRect r = rect();
140 pair<Cursor, Cursor> &cursors = _view.cursors();
141 cursors.first.paint(p, r);
142 cursors.second.paint(p, r);
143}
144
145} // namespace view
146} // namespace pv