2 * This file is part of the PulseView project.
4 * Copyright (C) 2014 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 <QApplication>
21 #include <QMouseEvent>
22 #include <QTouchEvent>
24 #include "tracetreeitem.hpp"
26 #include "viewwidget.hpp"
29 using std::shared_ptr;
36 ViewWidget::ViewWidget(View &parent) :
41 setFocusPolicy(Qt::ClickFocus);
42 setAttribute(Qt::WA_AcceptTouchEvents, true);
43 setMouseTracking(true);
46 void ViewWidget::clear_selection()
48 const auto items = this->items();
53 void ViewWidget::item_hover(const shared_ptr<ViewItem> &item, QPoint pos)
59 void ViewWidget::item_clicked(const shared_ptr<ViewItem> &item)
64 bool ViewWidget::accept_drag() const
66 const vector< shared_ptr<TimeItem> > items(view_.time_items());
67 const vector< shared_ptr<TraceTreeItem> > trace_tree_items(
68 view_.list_by_type<TraceTreeItem>());
70 const bool any_row_items_selected = any_of(
71 trace_tree_items.begin(), trace_tree_items.end(),
72 [](const shared_ptr<TraceTreeItem> &r) { return r->selected(); });
74 const bool any_time_items_selected = any_of(items.begin(), items.end(),
75 [](const shared_ptr<TimeItem> &i) { return i->selected(); });
77 if (any_row_items_selected && !any_time_items_selected) {
78 // Check all the drag items share a common owner
79 TraceTreeItemOwner *item_owner = nullptr;
80 for (shared_ptr<TraceTreeItem> r : trace_tree_items)
83 item_owner = r->owner();
84 else if (item_owner != r->owner())
89 } else if (any_time_items_selected && !any_row_items_selected) {
93 // A background drag is beginning
97 bool ViewWidget::mouse_down() const
99 return mouse_down_point_.x() != INT_MIN &&
100 mouse_down_point_.y() != INT_MIN;
103 void ViewWidget::drag_items(const QPoint &delta)
105 bool item_dragged = false;
107 // Drag the row items
108 const vector< shared_ptr<RowItem> > row_items(
109 view_.list_by_type<RowItem>());
110 for (shared_ptr<RowItem> r : row_items)
114 // Ensure the trace is selected
120 // If an item is being dragged, update the stacking
121 TraceTreeItemOwner *item_owner = nullptr;
122 const vector< shared_ptr<TraceTreeItem> > trace_tree_items(
123 view_.list_by_type<TraceTreeItem>());
124 for (shared_ptr<TraceTreeItem> i : trace_tree_items)
126 item_owner = i->owner();
129 item_owner->restack_items();
130 for (shared_ptr<TraceTreeItem> i : trace_tree_items)
131 i->animate_to_layout_v_offset();
134 // Drag the time items
135 const vector< shared_ptr<TimeItem> > items(view_.time_items());
136 for (auto &i : items)
142 // Do the background drag
147 void ViewWidget::drag()
151 void ViewWidget::drag_by(const QPoint &delta)
156 void ViewWidget::drag_release()
160 void ViewWidget::mouse_left_press_event(QMouseEvent *event)
164 const bool ctrl_pressed =
165 QApplication::keyboardModifiers() & Qt::ControlModifier;
167 // Clear selection if control is not pressed and this item is unselected
168 if ((!mouse_down_item_ || !mouse_down_item_->selected()) &&
172 // Set the signal selection state if the item has been clicked
173 if (mouse_down_item_ && mouse_down_item_->is_selectable(event->pos())) {
175 mouse_down_item_->select(!mouse_down_item_->selected());
177 mouse_down_item_->select(true);
180 // Save the offsets of any signals which will be dragged
181 bool item_dragged = false;
182 const auto items = this->items();
183 for (auto &i : items)
184 if (i->selected() && i->is_draggable(event->pos())) {
189 // Do the background drag
196 void ViewWidget::mouse_left_release_event(QMouseEvent *event)
200 auto items = this->items();
201 const bool ctrl_pressed =
202 QApplication::keyboardModifiers() & Qt::ControlModifier;
204 // Unselect everything if control is not pressed
205 const shared_ptr<ViewItem> mouse_over =
206 get_mouse_over_item(event->pos());
208 for (auto &i : items)
212 view_.restack_all_trace_tree_items();
215 for (shared_ptr<ViewItem> i : items)
216 if (mouse_down_item_ != i)
219 if (mouse_down_item_)
220 item_clicked(mouse_down_item_);
224 item_dragging_ = false;
227 bool ViewWidget::touch_event(QTouchEvent *event)
234 bool ViewWidget::event(QEvent *event)
236 switch (event->type()) {
237 case QEvent::TouchBegin:
238 case QEvent::TouchUpdate:
239 case QEvent::TouchEnd:
240 if (touch_event(static_cast<QTouchEvent *>(event)))
248 return QWidget::event(event);
251 void ViewWidget::mousePressEvent(QMouseEvent *event)
255 /* Ignore right click events as they will open context menus when
256 * used on trace labels. Those menus prevent ViewWidget::mouseReleaseEvent()
257 * to be triggered upon button release, making mouse_down_item_
258 * hold the last reference to a view item that might have been deleted
259 * from the context menu, preventing it from being freed as intended.
261 if (event->button() & Qt::LeftButton) {
262 mouse_down_point_ = event->pos();
263 mouse_down_item_ = get_mouse_over_item(event->pos());
264 mouse_left_press_event(event);
268 void ViewWidget::mouseReleaseEvent(QMouseEvent *event)
272 if (event->button() & Qt::LeftButton)
273 mouse_left_release_event(event);
275 mouse_down_point_ = QPoint(INT_MIN, INT_MIN);
276 mouse_down_item_ = nullptr;
279 void ViewWidget::mouseMoveEvent(QMouseEvent *event)
282 mouse_point_ = event->pos();
284 if (!event->buttons())
285 item_hover(get_mouse_over_item(event->pos()), event->pos());
286 else if (event->buttons() & Qt::LeftButton) {
287 if (!item_dragging_) {
288 if ((event->pos() - mouse_down_point_).manhattanLength() <
289 QApplication::startDragDistance())
295 item_dragging_ = true;
299 drag_items(event->pos() - mouse_down_point_);
303 void ViewWidget::leaveEvent(QEvent*)
305 mouse_point_ = QPoint(-1, -1);