]> sigrok.org Git - pulseview.git/blame_incremental - pv/view/viewport.cpp
MainWindow: Use regular pointer for QDockWidgets
[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 <cassert>
22#include <cmath>
23#include <algorithm>
24#include <limits>
25
26#include "signal.hpp"
27#include "view.hpp"
28#include "viewitempaintparams.hpp"
29#include "viewport.hpp"
30
31#include <pv/session.hpp>
32
33#include <QMouseEvent>
34
35#include <QDebug>
36
37using std::abs;
38using std::back_inserter;
39using std::copy;
40using std::dynamic_pointer_cast;
41using std::max;
42using std::min;
43using std::none_of;
44using std::numeric_limits;
45using std::shared_ptr;
46using std::stable_sort;
47using std::vector;
48
49namespace pv {
50namespace views {
51namespace TraceView {
52
53Viewport::Viewport(View &parent) :
54 ViewWidget(parent),
55 pinch_zoom_active_(false)
56{
57 setAutoFillBackground(true);
58 setBackgroundRole(QPalette::Base);
59}
60
61shared_ptr<ViewItem> Viewport::get_mouse_over_item(const QPoint &pt)
62{
63 const ViewItemPaintParams pp(rect(), view_.scale(), view_.offset());
64 const vector< shared_ptr<ViewItem> > items(this->items());
65 for (auto i = items.rbegin(); i != items.rend(); i++)
66 if ((*i)->enabled() &&
67 (*i)->hit_box_rect(pp).contains(pt))
68 return *i;
69 return nullptr;
70}
71
72void Viewport::item_hover(const shared_ptr<ViewItem> &item)
73{
74 if (item && item->is_draggable())
75 setCursor(dynamic_pointer_cast<RowItem>(item) ?
76 Qt::SizeVerCursor : Qt::SizeHorCursor);
77 else
78 unsetCursor();
79}
80
81void Viewport::drag()
82{
83 drag_offset_ = view_.offset();
84 drag_v_offset_ = view_.owner_visual_v_offset();
85}
86
87void Viewport::drag_by(const QPoint &delta)
88{
89 if (drag_offset_ == boost::none)
90 return;
91
92 view_.set_scale_offset(view_.scale(),
93 (*drag_offset_ - delta.x() * view_.scale()));
94
95 view_.set_v_offset(-drag_v_offset_ - delta.y());
96}
97
98void Viewport::drag_release()
99{
100 drag_offset_ = boost::none;
101}
102
103vector< shared_ptr<ViewItem> > Viewport::items()
104{
105 vector< shared_ptr<ViewItem> > items;
106 const std::vector< shared_ptr<ViewItem> > view_items(
107 view_.list_by_type<ViewItem>());
108 copy(view_items.begin(), view_items.end(), back_inserter(items));
109 const vector< shared_ptr<TimeItem> > time_items(view_.time_items());
110 copy(time_items.begin(), time_items.end(), back_inserter(items));
111 return items;
112}
113
114bool Viewport::touch_event(QTouchEvent *event)
115{
116 QList<QTouchEvent::TouchPoint> touchPoints = event->touchPoints();
117
118 if (touchPoints.count() != 2) {
119 pinch_zoom_active_ = false;
120 return false;
121 }
122
123 const QTouchEvent::TouchPoint &touchPoint0 = touchPoints.first();
124 const QTouchEvent::TouchPoint &touchPoint1 = touchPoints.last();
125
126 if (!pinch_zoom_active_ ||
127 (event->touchPointStates() & Qt::TouchPointPressed)) {
128 pinch_offset0_ = (view_.offset() + view_.scale() * touchPoint0.pos().x()).convert_to<double>();
129 pinch_offset1_ = (view_.offset() + view_.scale() * touchPoint1.pos().x()).convert_to<double>();
130 pinch_zoom_active_ = true;
131 }
132
133 double w = touchPoint1.pos().x() - touchPoint0.pos().x();
134 if (abs(w) >= 1.0) {
135 const double scale =
136 fabs((pinch_offset1_ - pinch_offset0_) / w);
137 double offset = pinch_offset0_ - touchPoint0.pos().x() * scale;
138 if (scale > 0)
139 view_.set_scale_offset(scale, offset);
140 }
141
142 if (event->touchPointStates() & Qt::TouchPointReleased) {
143 pinch_zoom_active_ = false;
144
145 if (touchPoint0.state() & Qt::TouchPointReleased) {
146 // Primary touch released
147 drag_release();
148 } else {
149 // Update the mouse down fields so that continued
150 // dragging with the primary touch will work correctly
151 mouse_down_point_ = touchPoint0.pos().toPoint();
152 drag();
153 }
154 }
155
156 return true;
157}
158
159void Viewport::paintEvent(QPaintEvent*)
160{
161 vector< shared_ptr<RowItem> > row_items(view_.list_by_type<RowItem>());
162 assert(none_of(row_items.begin(), row_items.end(),
163 [](const shared_ptr<RowItem> &r) { return !r; }));
164
165 stable_sort(row_items.begin(), row_items.end(),
166 [](const shared_ptr<RowItem> &a, const shared_ptr<RowItem> &b) {
167 return a->point(QRect()).y() < b->point(QRect()).y(); });
168
169 const vector< shared_ptr<TimeItem> > time_items(view_.time_items());
170 assert(none_of(time_items.begin(), time_items.end(),
171 [](const shared_ptr<TimeItem> &t) { return !t; }));
172
173 QPainter p(this);
174 p.setRenderHint(QPainter::Antialiasing);
175
176 const ViewItemPaintParams pp(rect(), view_.scale(), view_.offset());
177
178 for (const shared_ptr<TimeItem> t : time_items)
179 t->paint_back(p, pp);
180 for (const shared_ptr<RowItem> r : row_items)
181 r->paint_back(p, pp);
182
183 for (const shared_ptr<TimeItem> t : time_items)
184 t->paint_mid(p, pp);
185 for (const shared_ptr<RowItem> r : row_items)
186 r->paint_mid(p, pp);
187
188 for (const shared_ptr<RowItem> r : row_items)
189 r->paint_fore(p, pp);
190
191 p.setRenderHint(QPainter::Antialiasing, false);
192 for (const shared_ptr<TimeItem> t : time_items)
193 t->paint_fore(p, pp);
194
195 p.end();
196}
197
198void Viewport::mouseDoubleClickEvent(QMouseEvent *event)
199{
200 assert(event);
201
202 if (event->buttons() & Qt::LeftButton)
203 view_.zoom(2.0, event->x());
204 else if (event->buttons() & Qt::RightButton)
205 view_.zoom(-2.0, event->x());
206}
207
208void Viewport::wheelEvent(QWheelEvent *event)
209{
210 assert(event);
211
212 if (event->orientation() == Qt::Vertical) {
213 if (event->modifiers() & Qt::ControlModifier) {
214 // Vertical scrolling with the control key pressed
215 // is intrepretted as vertical scrolling
216 view_.set_v_offset(-view_.owner_visual_v_offset() -
217 (event->delta() * height()) / (8 * 120));
218 } else {
219 // Vertical scrolling is interpreted as zooming in/out
220 view_.zoom(event->delta() / 120, event->x());
221 }
222 } else if (event->orientation() == Qt::Horizontal) {
223 // Horizontal scrolling is interpreted as moving left/right
224 view_.set_scale_offset(view_.scale(),
225 event->delta() * view_.scale() + view_.offset());
226 }
227}
228
229} // namespace TraceView
230} // namespace views
231} // namespace pv