Remove unused "using" declarations.
[pulseview.git] / pv / view / viewport.cpp
blobd3f25aa6ba2bb82e07aa0afd1a389f6a13f9c6f9
1 /*
2 * This file is part of the PulseView project.
4 * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk>
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.
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.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
20 #include <cassert>
21 #include <cmath>
22 #include <algorithm>
23 #include <limits>
25 #include "signal.hpp"
26 #include "view.hpp"
27 #include "viewitempaintparams.hpp"
28 #include "viewport.hpp"
30 #include <pv/session.hpp>
32 #include <QMouseEvent>
34 #include <QDebug>
36 using std::abs;
37 using std::back_inserter;
38 using std::copy;
39 using std::dynamic_pointer_cast;
40 using std::none_of; // Used in assert()s.
41 using std::shared_ptr;
42 using std::stable_sort;
43 using std::vector;
45 namespace pv {
46 namespace views {
47 namespace TraceView {
49 Viewport::Viewport(View &parent) :
50 ViewWidget(parent),
51 pinch_zoom_active_(false)
53 setAutoFillBackground(true);
54 setBackgroundRole(QPalette::Base);
57 shared_ptr<ViewItem> Viewport::get_mouse_over_item(const QPoint &pt)
59 const ViewItemPaintParams pp(rect(), view_.scale(), view_.offset());
60 const vector< shared_ptr<ViewItem> > items(this->items());
61 for (auto i = items.rbegin(); i != items.rend(); i++)
62 if ((*i)->enabled() &&
63 (*i)->hit_box_rect(pp).contains(pt))
64 return *i;
65 return nullptr;
68 void Viewport::item_hover(const shared_ptr<ViewItem> &item)
70 if (item && item->is_draggable())
71 setCursor(dynamic_pointer_cast<RowItem>(item) ?
72 Qt::SizeVerCursor : Qt::SizeHorCursor);
73 else
74 unsetCursor();
77 void Viewport::drag()
79 drag_offset_ = view_.offset();
80 drag_v_offset_ = view_.owner_visual_v_offset();
83 void Viewport::drag_by(const QPoint &delta)
85 if (drag_offset_ == boost::none)
86 return;
88 view_.set_scale_offset(view_.scale(),
89 (*drag_offset_ - delta.x() * view_.scale()));
91 view_.set_v_offset(-drag_v_offset_ - delta.y());
94 void Viewport::drag_release()
96 drag_offset_ = boost::none;
99 vector< shared_ptr<ViewItem> > Viewport::items()
101 vector< shared_ptr<ViewItem> > items;
102 const vector< shared_ptr<ViewItem> > view_items(
103 view_.list_by_type<ViewItem>());
104 copy(view_items.begin(), view_items.end(), back_inserter(items));
105 const vector< shared_ptr<TimeItem> > time_items(view_.time_items());
106 copy(time_items.begin(), time_items.end(), back_inserter(items));
107 return items;
110 bool Viewport::touch_event(QTouchEvent *event)
112 QList<QTouchEvent::TouchPoint> touchPoints = event->touchPoints();
114 if (touchPoints.count() != 2) {
115 pinch_zoom_active_ = false;
116 return false;
119 const QTouchEvent::TouchPoint &touchPoint0 = touchPoints.first();
120 const QTouchEvent::TouchPoint &touchPoint1 = touchPoints.last();
122 if (!pinch_zoom_active_ ||
123 (event->touchPointStates() & Qt::TouchPointPressed)) {
124 pinch_offset0_ = (view_.offset() + view_.scale() * touchPoint0.pos().x()).convert_to<double>();
125 pinch_offset1_ = (view_.offset() + view_.scale() * touchPoint1.pos().x()).convert_to<double>();
126 pinch_zoom_active_ = true;
129 double w = touchPoint1.pos().x() - touchPoint0.pos().x();
130 if (abs(w) >= 1.0) {
131 const double scale =
132 fabs((pinch_offset1_ - pinch_offset0_) / w);
133 double offset = pinch_offset0_ - touchPoint0.pos().x() * scale;
134 if (scale > 0)
135 view_.set_scale_offset(scale, offset);
138 if (event->touchPointStates() & Qt::TouchPointReleased) {
139 pinch_zoom_active_ = false;
141 if (touchPoint0.state() & Qt::TouchPointReleased) {
142 // Primary touch released
143 drag_release();
144 } else {
145 // Update the mouse down fields so that continued
146 // dragging with the primary touch will work correctly
147 mouse_down_point_ = touchPoint0.pos().toPoint();
148 drag();
152 return true;
155 void Viewport::paintEvent(QPaintEvent*)
157 vector< shared_ptr<RowItem> > row_items(view_.list_by_type<RowItem>());
158 assert(none_of(row_items.begin(), row_items.end(),
159 [](const shared_ptr<RowItem> &r) { return !r; }));
161 stable_sort(row_items.begin(), row_items.end(),
162 [](const shared_ptr<RowItem> &a, const shared_ptr<RowItem> &b) {
163 return a->point(QRect()).y() < b->point(QRect()).y(); });
165 const vector< shared_ptr<TimeItem> > time_items(view_.time_items());
166 assert(none_of(time_items.begin(), time_items.end(),
167 [](const shared_ptr<TimeItem> &t) { return !t; }));
169 QPainter p(this);
170 p.setRenderHint(QPainter::Antialiasing);
172 const ViewItemPaintParams pp(rect(), view_.scale(), view_.offset());
174 for (const shared_ptr<TimeItem> t : time_items)
175 t->paint_back(p, pp);
176 for (const shared_ptr<RowItem> r : row_items)
177 r->paint_back(p, pp);
179 for (const shared_ptr<TimeItem> t : time_items)
180 t->paint_mid(p, pp);
181 for (const shared_ptr<RowItem> r : row_items)
182 r->paint_mid(p, pp);
184 for (const shared_ptr<RowItem> r : row_items)
185 r->paint_fore(p, pp);
187 p.setRenderHint(QPainter::Antialiasing, false);
188 for (const shared_ptr<TimeItem> t : time_items)
189 t->paint_fore(p, pp);
191 p.end();
194 void Viewport::mouseDoubleClickEvent(QMouseEvent *event)
196 assert(event);
198 if (event->buttons() & Qt::LeftButton)
199 view_.zoom(2.0, event->x());
200 else if (event->buttons() & Qt::RightButton)
201 view_.zoom(-2.0, event->x());
204 void Viewport::wheelEvent(QWheelEvent *event)
206 assert(event);
208 if (event->orientation() == Qt::Vertical) {
209 if (event->modifiers() & Qt::ControlModifier) {
210 // Vertical scrolling with the control key pressed
211 // is intrepretted as vertical scrolling
212 view_.set_v_offset(-view_.owner_visual_v_offset() -
213 (event->delta() * height()) / (8 * 120));
214 } else {
215 // Vertical scrolling is interpreted as zooming in/out
216 view_.zoom(event->delta() / 120, event->x());
218 } else if (event->orientation() == Qt::Horizontal) {
219 // Horizontal scrolling is interpreted as moving left/right
220 view_.set_scale_offset(view_.scale(),
221 event->delta() * view_.scale() + view_.offset());
225 } // namespace TraceView
226 } // namespace views
227 } // namespace pv