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/>.
27 #include "viewitempaintparams.hpp"
28 #include "viewport.hpp"
30 #include <pv/session.hpp>
32 #include <QMouseEvent>
37 using std::back_inserter;
39 using std::dynamic_pointer_cast;
43 using std::numeric_limits;
44 using std::shared_ptr;
45 using std::stable_sort;
52 Viewport::Viewport(View &parent) :
54 pinch_zoom_active_(false)
56 setAutoFillBackground(true);
57 setBackgroundRole(QPalette::Base);
60 shared_ptr<ViewItem> Viewport::get_mouse_over_item(const QPoint &pt)
62 const ViewItemPaintParams pp(rect(), view_.scale(), view_.offset());
63 const vector< shared_ptr<ViewItem> > items(this->items());
64 for (auto i = items.rbegin(); i != items.rend(); i++)
65 if ((*i)->enabled() &&
66 (*i)->hit_box_rect(pp).contains(pt))
71 void Viewport::item_hover(const shared_ptr<ViewItem> &item)
73 if (item && item->is_draggable())
74 setCursor(dynamic_pointer_cast<RowItem>(item) ?
75 Qt::SizeVerCursor : Qt::SizeHorCursor);
82 drag_offset_ = view_.offset();
83 drag_v_offset_ = view_.owner_visual_v_offset();
86 void Viewport::drag_by(const QPoint &delta)
88 if (drag_offset_ == boost::none)
91 view_.set_scale_offset(view_.scale(),
92 (*drag_offset_ - delta.x() * view_.scale()));
94 view_.set_v_offset(-drag_v_offset_ - delta.y());
97 void Viewport::drag_release()
99 drag_offset_ = boost::none;
102 vector< shared_ptr<ViewItem> > Viewport::items()
104 vector< shared_ptr<ViewItem> > items;
105 const std::vector< shared_ptr<ViewItem> > view_items(
106 view_.list_by_type<ViewItem>());
107 copy(view_items.begin(), view_items.end(), back_inserter(items));
108 const vector< shared_ptr<TimeItem> > time_items(view_.time_items());
109 copy(time_items.begin(), time_items.end(), back_inserter(items));
113 bool Viewport::touch_event(QTouchEvent *event)
115 QList<QTouchEvent::TouchPoint> touchPoints = event->touchPoints();
117 if (touchPoints.count() != 2) {
118 pinch_zoom_active_ = false;
122 const QTouchEvent::TouchPoint &touchPoint0 = touchPoints.first();
123 const QTouchEvent::TouchPoint &touchPoint1 = touchPoints.last();
125 if (!pinch_zoom_active_ ||
126 (event->touchPointStates() & Qt::TouchPointPressed)) {
127 pinch_offset0_ = (view_.offset() + view_.scale() * touchPoint0.pos().x()).convert_to<double>();
128 pinch_offset1_ = (view_.offset() + view_.scale() * touchPoint1.pos().x()).convert_to<double>();
129 pinch_zoom_active_ = true;
132 double w = touchPoint1.pos().x() - touchPoint0.pos().x();
135 fabs((pinch_offset1_ - pinch_offset0_) / w);
136 double offset = pinch_offset0_ - touchPoint0.pos().x() * scale;
138 view_.set_scale_offset(scale, offset);
141 if (event->touchPointStates() & Qt::TouchPointReleased) {
142 pinch_zoom_active_ = false;
144 if (touchPoint0.state() & Qt::TouchPointReleased) {
145 // Primary touch released
148 // Update the mouse down fields so that continued
149 // dragging with the primary touch will work correctly
150 mouse_down_point_ = touchPoint0.pos().toPoint();
158 void Viewport::paintEvent(QPaintEvent*)
160 vector< shared_ptr<RowItem> > row_items(view_.list_by_type<RowItem>());
161 assert(none_of(row_items.begin(), row_items.end(),
162 [](const shared_ptr<RowItem> &r) { return !r; }));
164 stable_sort(row_items.begin(), row_items.end(),
165 [](const shared_ptr<RowItem> &a, const shared_ptr<RowItem> &b) {
166 return a->point(QRect()).y() < b->point(QRect()).y(); });
168 const vector< shared_ptr<TimeItem> > time_items(view_.time_items());
169 assert(none_of(time_items.begin(), time_items.end(),
170 [](const shared_ptr<TimeItem> &t) { return !t; }));
173 p.setRenderHint(QPainter::Antialiasing);
175 const ViewItemPaintParams pp(rect(), view_.scale(), view_.offset());
177 for (const shared_ptr<TimeItem> t : time_items)
178 t->paint_back(p, pp);
179 for (const shared_ptr<RowItem> r : row_items)
180 r->paint_back(p, pp);
182 for (const shared_ptr<TimeItem> t : time_items)
184 for (const shared_ptr<RowItem> r : row_items)
187 for (const shared_ptr<RowItem> r : row_items)
188 r->paint_fore(p, pp);
190 p.setRenderHint(QPainter::Antialiasing, false);
191 for (const shared_ptr<TimeItem> t : time_items)
192 t->paint_fore(p, pp);
197 void Viewport::mouseDoubleClickEvent(QMouseEvent *event)
201 if (event->buttons() & Qt::LeftButton)
202 view_.zoom(2.0, event->x());
203 else if (event->buttons() & Qt::RightButton)
204 view_.zoom(-2.0, event->x());
207 void Viewport::wheelEvent(QWheelEvent *event)
211 if (event->orientation() == Qt::Vertical) {
212 if (event->modifiers() & Qt::ControlModifier) {
213 // Vertical scrolling with the control key pressed
214 // is intrepretted as vertical scrolling
215 view_.set_v_offset(-view_.owner_visual_v_offset() -
216 (event->delta() * height()) / (8 * 120));
218 // Vertical scrolling is interpreted as zooming in/out
219 view_.zoom(event->delta() / 120, event->x());
221 } else if (event->orientation() == Qt::Horizontal) {
222 // Horizontal scrolling is interpreted as moving left/right
223 view_.set_scale_offset(view_.scale(),
224 event->delta() * view_.scale() + view_.offset());
228 } // namespace TraceView