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, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 #include <QApplication>
22 #include <QMouseEvent>
23 #include <QTouchEvent>
25 #include "tracetreeitem.hpp"
27 #include "viewwidget.hpp"
30 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)
58 void ViewWidget::item_clicked(const shared_ptr<ViewItem> &item)
63 bool ViewWidget::accept_drag() const
65 const vector< shared_ptr<TimeItem> > items(view_.time_items());
66 const vector< shared_ptr<TraceTreeItem> > trace_tree_items(
67 view_.list_by_type<TraceTreeItem>());
69 const bool any_row_items_selected = any_of(
70 trace_tree_items.begin(), trace_tree_items.end(),
71 [](const shared_ptr<TraceTreeItem> &r) { return r->selected(); });
73 const bool any_time_items_selected = any_of(items.begin(), items.end(),
74 [](const shared_ptr<TimeItem> &i) { return i->selected(); });
76 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())
90 else if (any_time_items_selected && !any_row_items_selected)
95 // A background drag is beginning
99 bool ViewWidget::mouse_down() const
101 return mouse_down_point_.x() != INT_MIN &&
102 mouse_down_point_.y() != INT_MIN;
105 void ViewWidget::drag_items(const QPoint &delta)
107 bool item_dragged = false;
109 // Drag the row items
110 TraceTreeItemOwner *item_owner = nullptr;
111 const vector< shared_ptr<TraceTreeItem> > trace_tree_items(
112 view_.list_by_type<TraceTreeItem>());
113 for (shared_ptr<TraceTreeItem> r : trace_tree_items)
115 item_owner = r->owner();
118 // Ensure the trace is selected
124 item_owner->restack_items();
125 for (shared_ptr<TraceTreeItem> r : trace_tree_items)
126 r->animate_to_layout_v_offset();
129 // Drag the time items
130 const vector< shared_ptr<TimeItem> > items(view_.time_items());
131 for (auto &i : items)
137 // Do the background drag
142 void ViewWidget::drag()
146 void ViewWidget::drag_by(const QPoint &delta)
151 void ViewWidget::drag_release()
155 void ViewWidget::mouse_left_press_event(QMouseEvent *event)
159 const bool ctrl_pressed =
160 QApplication::keyboardModifiers() & Qt::ControlModifier;
162 // Clear selection if control is not pressed and this item is unselected
163 if ((!mouse_down_item_ || !mouse_down_item_->selected()) &&
167 // Set the signal selection state if the item has been clicked
168 if (mouse_down_item_) {
170 mouse_down_item_->select(!mouse_down_item_->selected());
172 mouse_down_item_->select(true);
175 // Save the offsets of any signals which will be dragged
176 bool item_dragged = false;
177 const auto items = this->items();
178 for (auto &i : items)
184 // Do the background drag
191 void ViewWidget::mouse_left_release_event(QMouseEvent *event)
195 auto items = this->items();
196 const bool ctrl_pressed =
197 QApplication::keyboardModifiers() & Qt::ControlModifier;
199 // Unselect everything if control is not pressed
200 const shared_ptr<ViewItem> mouse_over =
201 get_mouse_over_item(event->pos());
203 for (auto &i : items)
207 view_.restack_all_trace_tree_items();
211 for (shared_ptr<ViewItem> i : items)
212 if (mouse_down_item_ != i)
215 if (mouse_down_item_)
216 item_clicked(mouse_down_item_);
220 item_dragging_ = false;
223 bool ViewWidget::touch_event(QTouchEvent *e)
229 bool ViewWidget::event(QEvent *event)
231 switch (event->type()) {
232 case QEvent::TouchBegin:
233 case QEvent::TouchUpdate:
234 case QEvent::TouchEnd:
235 if (touch_event(static_cast<QTouchEvent *>(event)))
243 return QWidget::event(event);
246 void ViewWidget::mousePressEvent(QMouseEvent *event)
250 mouse_down_point_ = event->pos();
251 mouse_down_item_ = get_mouse_over_item(event->pos());
253 if (event->button() & Qt::LeftButton)
254 mouse_left_press_event(event);
257 void ViewWidget::mouseReleaseEvent(QMouseEvent *event)
260 if (event->button() & Qt::LeftButton)
261 mouse_left_release_event(event);
263 mouse_down_point_ = QPoint(INT_MIN, INT_MIN);
264 mouse_down_item_ = nullptr;
267 void ViewWidget::mouseMoveEvent(QMouseEvent *e)
270 mouse_point_ = e->pos();
273 item_hover(get_mouse_over_item(e->pos()));
274 else if (e->buttons() & Qt::LeftButton)
278 if ((e->pos() - mouse_down_point_).manhattanLength() <
279 QApplication::startDragDistance())
285 item_dragging_ = true;
289 drag_items(e->pos() - mouse_down_point_);
295 void ViewWidget::leaveEvent(QEvent*)
297 mouse_point_ = QPoint(-1, -1);