]>
Commit | Line | Data |
---|---|---|
40aca27e JH |
1 | /* |
2 | * This file is part of the PulseView project. | |
3 | * | |
4 | * Copyright (C) 2014 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 | |
efdec55a | 17 | * along with this program; if not, see <http://www.gnu.org/licenses/>. |
40aca27e JH |
18 | */ |
19 | ||
e9e4e5e7 JH |
20 | #include <QApplication> |
21 | #include <QMouseEvent> | |
c9743553 | 22 | #include <QTouchEvent> |
b434cbaf | 23 | |
af503b10 | 24 | #include "tracetreeitem.hpp" |
40aca27e JH |
25 | #include "view.hpp" |
26 | #include "viewwidget.hpp" | |
27 | ||
b434cbaf JH |
28 | using std::any_of; |
29 | using std::shared_ptr; | |
30 | using std::vector; | |
31 | ||
40aca27e | 32 | namespace pv { |
f4e57597 | 33 | namespace views { |
1573bf16 | 34 | namespace trace { |
40aca27e JH |
35 | |
36 | ViewWidget::ViewWidget(View &parent) : | |
37 | QWidget(&parent), | |
e9e4e5e7 | 38 | view_(parent), |
803cdac4 | 39 | item_dragging_(false) |
40aca27e | 40 | { |
e9e4e5e7 | 41 | setFocusPolicy(Qt::ClickFocus); |
c9743553 | 42 | setAttribute(Qt::WA_AcceptTouchEvents, true); |
e9e4e5e7 JH |
43 | setMouseTracking(true); |
44 | } | |
45 | ||
46 | void ViewWidget::clear_selection() | |
47 | { | |
48 | const auto items = this->items(); | |
49 | for (auto &i : items) | |
50 | i->select(false); | |
e9e4e5e7 JH |
51 | } |
52 | ||
119c5c23 | 53 | void ViewWidget::item_hover(const shared_ptr<ViewItem> &item, QPoint pos) |
e8b969a9 JH |
54 | { |
55 | (void)item; | |
119c5c23 | 56 | (void)pos; |
e8b969a9 JH |
57 | } |
58 | ||
e9e4e5e7 JH |
59 | void ViewWidget::item_clicked(const shared_ptr<ViewItem> &item) |
60 | { | |
61 | (void)item; | |
40aca27e JH |
62 | } |
63 | ||
b434cbaf JH |
64 | bool ViewWidget::accept_drag() const |
65 | { | |
66 | const vector< shared_ptr<TimeItem> > items(view_.time_items()); | |
cc88566c JH |
67 | const vector< shared_ptr<TraceTreeItem> > trace_tree_items( |
68 | view_.list_by_type<TraceTreeItem>()); | |
b434cbaf | 69 | |
cc88566c JH |
70 | const bool any_row_items_selected = any_of( |
71 | trace_tree_items.begin(), trace_tree_items.end(), | |
af503b10 | 72 | [](const shared_ptr<TraceTreeItem> &r) { return r->selected(); }); |
b434cbaf JH |
73 | |
74 | const bool any_time_items_selected = any_of(items.begin(), items.end(), | |
75 | [](const shared_ptr<TimeItem> &i) { return i->selected(); }); | |
76 | ||
2ad82c2e | 77 | if (any_row_items_selected && !any_time_items_selected) { |
b434cbaf | 78 | // Check all the drag items share a common owner |
af503b10 | 79 | TraceTreeItemOwner *item_owner = nullptr; |
f4ab4b5c | 80 | for (const shared_ptr<TraceTreeItem>& r : trace_tree_items) |
b434cbaf JH |
81 | if (r->dragging()) { |
82 | if (!item_owner) | |
83 | item_owner = r->owner(); | |
f3290553 | 84 | else if (item_owner != r->owner()) |
b434cbaf JH |
85 | return false; |
86 | } | |
87 | ||
88 | return true; | |
2ad82c2e | 89 | } else if (any_time_items_selected && !any_row_items_selected) { |
b434cbaf JH |
90 | return true; |
91 | } | |
92 | ||
28290534 JH |
93 | // A background drag is beginning |
94 | return true; | |
b434cbaf JH |
95 | } |
96 | ||
1f1edc09 JH |
97 | bool ViewWidget::mouse_down() const |
98 | { | |
99 | return mouse_down_point_.x() != INT_MIN && | |
100 | mouse_down_point_.y() != INT_MIN; | |
101 | } | |
102 | ||
1dffa582 JH |
103 | void ViewWidget::drag_items(const QPoint &delta) |
104 | { | |
28290534 JH |
105 | bool item_dragged = false; |
106 | ||
1dffa582 | 107 | // Drag the row items |
af33d4cb SA |
108 | const vector< shared_ptr<ViewItem> > row_items( |
109 | view_.list_by_type<ViewItem>()); | |
110 | for (const shared_ptr<ViewItem>& r : row_items) | |
1dffa582 | 111 | if (r->dragging()) { |
1dffa582 JH |
112 | r->drag_by(delta); |
113 | ||
114 | // Ensure the trace is selected | |
115 | r->select(); | |
453b6d63 JH |
116 | |
117 | item_dragged = true; | |
1dffa582 JH |
118 | } |
119 | ||
453b6d63 JH |
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>()); | |
f4ab4b5c | 124 | for (const shared_ptr<TraceTreeItem>& i : trace_tree_items) |
453b6d63 JH |
125 | if (i->dragging()) |
126 | item_owner = i->owner(); | |
127 | ||
1dffa582 JH |
128 | if (item_owner) { |
129 | item_owner->restack_items(); | |
453b6d63 JH |
130 | for (shared_ptr<TraceTreeItem> i : trace_tree_items) |
131 | i->animate_to_layout_v_offset(); | |
1dffa582 JH |
132 | } |
133 | ||
134 | // Drag the time items | |
135 | const vector< shared_ptr<TimeItem> > items(view_.time_items()); | |
136 | for (auto &i : items) | |
28290534 | 137 | if (i->dragging()) { |
1dffa582 | 138 | i->drag_by(delta); |
28290534 JH |
139 | item_dragged = true; |
140 | } | |
141 | ||
142 | // Do the background drag | |
143 | if (!item_dragged) | |
144 | drag_by(delta); | |
145 | } | |
146 | ||
147 | void ViewWidget::drag() | |
148 | { | |
149 | } | |
150 | ||
151 | void ViewWidget::drag_by(const QPoint &delta) | |
152 | { | |
153 | (void)delta; | |
154 | } | |
155 | ||
156 | void ViewWidget::drag_release() | |
157 | { | |
1dffa582 JH |
158 | } |
159 | ||
e9e4e5e7 JH |
160 | void ViewWidget::mouse_left_press_event(QMouseEvent *event) |
161 | { | |
162 | (void)event; | |
163 | ||
164 | const bool ctrl_pressed = | |
165 | QApplication::keyboardModifiers() & Qt::ControlModifier; | |
166 | ||
167 | // Clear selection if control is not pressed and this item is unselected | |
168 | if ((!mouse_down_item_ || !mouse_down_item_->selected()) && | |
169 | !ctrl_pressed) | |
170 | clear_selection(); | |
171 | ||
172 | // Set the signal selection state if the item has been clicked | |
cbd9ec7f | 173 | if (mouse_down_item_ && mouse_down_item_->is_selectable(event->pos())) { |
e9e4e5e7 JH |
174 | if (ctrl_pressed) |
175 | mouse_down_item_->select(!mouse_down_item_->selected()); | |
176 | else | |
177 | mouse_down_item_->select(true); | |
178 | } | |
179 | ||
180 | // Save the offsets of any signals which will be dragged | |
28290534 | 181 | bool item_dragged = false; |
e9e4e5e7 JH |
182 | const auto items = this->items(); |
183 | for (auto &i : items) | |
119c5c23 | 184 | if (i->selected() && i->is_draggable(event->pos())) { |
28290534 | 185 | item_dragged = true; |
e9e4e5e7 | 186 | i->drag(); |
28290534 JH |
187 | } |
188 | ||
189 | // Do the background drag | |
190 | if (!item_dragged) | |
191 | drag(); | |
e9e4e5e7 JH |
192 | |
193 | selection_changed(); | |
e9e4e5e7 JH |
194 | } |
195 | ||
196 | void ViewWidget::mouse_left_release_event(QMouseEvent *event) | |
197 | { | |
198 | assert(event); | |
199 | ||
200 | auto items = this->items(); | |
201 | const bool ctrl_pressed = | |
202 | QApplication::keyboardModifiers() & Qt::ControlModifier; | |
203 | ||
204 | // Unselect everything if control is not pressed | |
205 | const shared_ptr<ViewItem> mouse_over = | |
206 | get_mouse_over_item(event->pos()); | |
207 | ||
208 | for (auto &i : items) | |
209 | i->drag_release(); | |
210 | ||
803cdac4 | 211 | if (item_dragging_) |
af503b10 | 212 | view_.restack_all_trace_tree_items(); |
2ad82c2e | 213 | else { |
e9e4e5e7 JH |
214 | if (!ctrl_pressed) { |
215 | for (shared_ptr<ViewItem> i : items) | |
216 | if (mouse_down_item_ != i) | |
217 | i->select(false); | |
218 | ||
219 | if (mouse_down_item_) | |
220 | item_clicked(mouse_down_item_); | |
221 | } | |
222 | } | |
223 | ||
803cdac4 | 224 | item_dragging_ = false; |
e9e4e5e7 JH |
225 | } |
226 | ||
d9ea9628 | 227 | bool ViewWidget::touch_event(QTouchEvent *event) |
c9743553 | 228 | { |
d9ea9628 UH |
229 | (void)event; |
230 | ||
c9743553 JH |
231 | return false; |
232 | } | |
233 | ||
234 | bool ViewWidget::event(QEvent *event) | |
235 | { | |
236 | switch (event->type()) { | |
237 | case QEvent::TouchBegin: | |
238 | case QEvent::TouchUpdate: | |
239 | case QEvent::TouchEnd: | |
240 | if (touch_event(static_cast<QTouchEvent *>(event))) | |
241 | return true; | |
242 | break; | |
243 | ||
244 | default: | |
245 | break; | |
246 | } | |
247 | ||
248 | return QWidget::event(event); | |
249 | } | |
250 | ||
e9e4e5e7 JH |
251 | void ViewWidget::mousePressEvent(QMouseEvent *event) |
252 | { | |
253 | assert(event); | |
254 | ||
8c9f62a5 | 255 | if (event->button() & Qt::LeftButton) { |
a47b5a3f SA |
256 | if (event->modifiers() & Qt::ShiftModifier) |
257 | view_.show_cursors(false); | |
258 | ||
8c9f62a5 | 259 | mouse_down_point_ = event->pos(); |
a47b5a3f | 260 | mouse_down_offset_ = view_.offset() + event->pos().x() * view_.scale(); |
8c9f62a5 SA |
261 | mouse_down_item_ = get_mouse_over_item(event->pos()); |
262 | mouse_left_press_event(event); | |
263 | } | |
264 | ||
265 | /* Don't forward right click events as they will open context menus when | |
2d25fc47 SA |
266 | * used on trace labels. Those menus prevent ViewWidget::mouseReleaseEvent() |
267 | * to be triggered upon button release, making mouse_down_item_ | |
268 | * hold the last reference to a view item that might have been deleted | |
269 | * from the context menu, preventing it from being freed as intended. | |
8c9f62a5 | 270 | * TODO Remove this once context menus are handled separately |
2d25fc47 | 271 | */ |
8c9f62a5 | 272 | if (event->button() & Qt::RightButton) |
2d25fc47 | 273 | mouse_down_point_ = event->pos(); |
e9e4e5e7 JH |
274 | } |
275 | ||
276 | void ViewWidget::mouseReleaseEvent(QMouseEvent *event) | |
277 | { | |
278 | assert(event); | |
2d25fc47 | 279 | |
e9e4e5e7 JH |
280 | if (event->button() & Qt::LeftButton) |
281 | mouse_left_release_event(event); | |
282 | ||
1f1edc09 | 283 | mouse_down_point_ = QPoint(INT_MIN, INT_MIN); |
e9e4e5e7 JH |
284 | mouse_down_item_ = nullptr; |
285 | } | |
286 | ||
710c2a18 | 287 | void ViewWidget::keyReleaseEvent(QKeyEvent *event) |
288 | { | |
289 | // Update mouse_modifiers_ also if modifiers change, but pointer doesn't move | |
9f094349 | 290 | if ((mouse_point_.x() >= 0) && (mouse_point_.y() >= 0)) // mouse is inside |
710c2a18 | 291 | mouse_modifiers_ = event->modifiers(); |
292 | update(); | |
293 | } | |
294 | ||
295 | void ViewWidget::keyPressEvent(QKeyEvent *event) | |
296 | { | |
297 | // Update mouse_modifiers_ also if modifiers change, but pointer doesn't move | |
9f094349 | 298 | if ((mouse_point_.x() >= 0) && (mouse_point_.y() >= 0)) // mouse is inside |
710c2a18 | 299 | mouse_modifiers_ = event->modifiers(); |
300 | update(); | |
301 | } | |
302 | ||
d9ea9628 | 303 | void ViewWidget::mouseMoveEvent(QMouseEvent *event) |
e9e4e5e7 | 304 | { |
d9ea9628 UH |
305 | assert(event); |
306 | mouse_point_ = event->pos(); | |
710c2a18 | 307 | mouse_modifiers_ = event->modifiers(); |
e9e4e5e7 | 308 | |
a47b5a3f | 309 | if (!event->buttons()) |
119c5c23 | 310 | item_hover(get_mouse_over_item(event->pos()), event->pos()); |
e9e4e5e7 | 311 | |
a47b5a3f SA |
312 | if (event->buttons() & Qt::LeftButton) { |
313 | if (event->modifiers() & Qt::ShiftModifier) { | |
314 | // Cursor drag | |
315 | pv::util::Timestamp current_offset = view_.offset() + event->pos().x() * view_.scale(); | |
4eba9990 | 316 | |
a47b5a3f SA |
317 | const int drag_distance = qAbs(current_offset.convert_to<double>() - |
318 | mouse_down_offset_.convert_to<double>()) / view_.scale(); | |
4eba9990 | 319 | |
a47b5a3f SA |
320 | if (drag_distance > QApplication::startDragDistance()) { |
321 | view_.show_cursors(true); | |
322 | view_.set_cursors(mouse_down_offset_, current_offset); | |
323 | } else | |
324 | view_.show_cursors(false); | |
4eba9990 | 325 | |
a47b5a3f SA |
326 | } else { |
327 | if (!item_dragging_) { | |
328 | if ((event->pos() - mouse_down_point_).manhattanLength() < | |
329 | QApplication::startDragDistance()) | |
330 | return; | |
4eba9990 | 331 | |
a47b5a3f SA |
332 | if (!accept_drag()) |
333 | return; | |
4eba9990 | 334 | |
a47b5a3f SA |
335 | item_dragging_ = true; |
336 | } | |
e9e4e5e7 | 337 | |
a47b5a3f SA |
338 | // Do the drag |
339 | drag_items(event->pos() - mouse_down_point_); | |
340 | } | |
e8b969a9 | 341 | } |
cad96374 SA |
342 | |
343 | // Force a repaint of the widget to update highlighted parts | |
344 | update(); | |
e9e4e5e7 JH |
345 | } |
346 | ||
347 | void ViewWidget::leaveEvent(QEvent*) | |
348 | { | |
cad96374 SA |
349 | bool cursor_above_widget = rect().contains(mapFromGlobal(QCursor::pos())); |
350 | ||
351 | // We receive leaveEvent also when the widget loses focus even when | |
352 | // the mouse cursor hasn't moved at all - e.g. when the popup shows. | |
353 | // However, we don't want to reset mouse_position_ when the mouse is | |
354 | // still above this widget as doing so would break the context menu | |
355 | if (!cursor_above_widget) | |
356 | mouse_point_ = QPoint(INT_MIN, INT_MIN); | |
357 | ||
710c2a18 | 358 | mouse_modifiers_ = Qt::NoModifier; |
359 | item_hover(nullptr, QPoint()); | |
e9e4e5e7 JH |
360 | update(); |
361 | } | |
362 | ||
1573bf16 | 363 | } // namespace trace |
f4e57597 | 364 | } // namespace views |
40aca27e | 365 | } // namespace pv |