]> sigrok.org Git - pulseview.git/blob - pv/view/viewwidget.cpp
ViewWidget: Fixed glitching when drag is within the manhattan distance
[pulseview.git] / pv / view / viewwidget.cpp
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
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
19  */
20
21 #include <QApplication>
22 #include <QMouseEvent>
23 #include <QTouchEvent>
24
25 #include "rowitem.hpp"
26 #include "view.hpp"
27 #include "viewwidget.hpp"
28
29 using std::any_of;
30 using std::shared_ptr;
31 using std::vector;
32
33 namespace pv {
34 namespace view {
35
36 ViewWidget::ViewWidget(View &parent) :
37         QWidget(&parent),
38         view_(parent),
39         item_dragging_(false)
40 {
41         setFocusPolicy(Qt::ClickFocus);
42         setAttribute(Qt::WA_AcceptTouchEvents, true);
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);
51         update();
52 }
53
54 void ViewWidget::item_clicked(const shared_ptr<ViewItem> &item)
55 {
56         (void)item;
57 }
58
59 bool ViewWidget::accept_drag() const
60 {
61         const vector< shared_ptr<TimeItem> > items(view_.time_items());
62
63         const bool any_row_items_selected = any_of(view_.begin(), view_.end(),
64                 [](const shared_ptr<RowItem> &r) { return r->selected(); });
65
66         const bool any_time_items_selected = any_of(items.begin(), items.end(),
67                 [](const shared_ptr<TimeItem> &i) { return i->selected(); });
68
69         if (any_row_items_selected && !any_time_items_selected)
70         {
71                 // Check all the drag items share a common owner
72                 RowItemOwner *item_owner = nullptr;
73                 for (shared_ptr<RowItem> r : view_)
74                         if (r->dragging()) {
75                                 if (!item_owner)
76                                         item_owner = r->owner();
77                                 else if(item_owner != r->owner())
78                                         return false;
79                         }
80
81                 return true;
82         }
83         else if (any_time_items_selected && !any_row_items_selected)
84         {
85                 return true;
86         }
87
88         // A background drag is beginning
89         return true;
90 }
91
92 bool ViewWidget::mouse_down() const
93 {
94         return mouse_down_point_.x() != INT_MIN &&
95                 mouse_down_point_.y() != INT_MIN;
96 }
97
98 void ViewWidget::drag_items(const QPoint &delta)
99 {
100         bool item_dragged = false;
101
102         // Drag the row items
103         RowItemOwner *item_owner = nullptr;
104         for (std::shared_ptr<RowItem> r : view_)
105                 if (r->dragging()) {
106                         item_owner = r->owner();
107                         r->drag_by(delta);
108
109                         // Ensure the trace is selected
110                         r->select();
111                 }
112
113         if (item_owner) {
114                 item_dragged = true;
115                 item_owner->restack_items();
116                 for (const auto &r : *item_owner)
117                         r->animate_to_layout_v_offset();
118         }
119
120         // Drag the time items
121         const vector< shared_ptr<TimeItem> > items(view_.time_items());
122         for (auto &i : items)
123                 if (i->dragging()) {
124                         i->drag_by(delta);
125                         item_dragged = true;
126                 }
127
128         // Do the background drag
129         if (!item_dragged)
130                 drag_by(delta);
131 }
132
133 void ViewWidget::drag()
134 {
135 }
136
137 void ViewWidget::drag_by(const QPoint &delta)
138 {
139         (void)delta;
140 }
141
142 void ViewWidget::drag_release()
143 {
144 }
145
146 void ViewWidget::mouse_left_press_event(QMouseEvent *event)
147 {
148         (void)event;
149
150         const bool ctrl_pressed =
151                 QApplication::keyboardModifiers() & Qt::ControlModifier;
152
153         // Clear selection if control is not pressed and this item is unselected
154         if ((!mouse_down_item_ || !mouse_down_item_->selected()) &&
155                 !ctrl_pressed)
156                 clear_selection();
157
158         // Set the signal selection state if the item has been clicked
159         if (mouse_down_item_) {
160                 if (ctrl_pressed)
161                         mouse_down_item_->select(!mouse_down_item_->selected());
162                 else
163                         mouse_down_item_->select(true);
164         }
165
166         // Save the offsets of any signals which will be dragged
167         bool item_dragged = false;
168         const auto items = this->items();
169         for (auto &i : items)
170                 if (i->selected()) {
171                         item_dragged = true;
172                         i->drag();
173                 }
174
175         // Do the background drag
176         if (!item_dragged)
177                 drag();
178
179         selection_changed();
180         update();
181 }
182
183 void ViewWidget::mouse_left_release_event(QMouseEvent *event)
184 {
185         assert(event);
186
187         auto items = this->items();
188         const bool ctrl_pressed =
189                 QApplication::keyboardModifiers() & Qt::ControlModifier;
190
191         // Unselect everything if control is not pressed
192         const shared_ptr<ViewItem> mouse_over =
193                 get_mouse_over_item(event->pos());
194
195         for (auto &i : items)
196                 i->drag_release();
197
198         if (item_dragging_)
199                 view_.restack_all_row_items();
200         else
201         {
202                 if (!ctrl_pressed) {
203                         for (shared_ptr<ViewItem> i : items)
204                                 if (mouse_down_item_ != i)
205                                         i->select(false);
206
207                         if (mouse_down_item_)
208                                 item_clicked(mouse_down_item_);
209                 }
210         }
211
212         item_dragging_ = false;
213 }
214
215 bool ViewWidget::touch_event(QTouchEvent *e)
216 {
217         (void)e;
218         return false;
219 }
220
221 bool ViewWidget::event(QEvent *event)
222 {
223         switch (event->type()) {
224         case QEvent::TouchBegin:
225         case QEvent::TouchUpdate:
226         case QEvent::TouchEnd:
227                 if (touch_event(static_cast<QTouchEvent *>(event)))
228                         return true;
229                 break;
230
231         default:
232                 break;
233         }
234
235         return QWidget::event(event);
236 }
237
238 void ViewWidget::mousePressEvent(QMouseEvent *event)
239 {
240         assert(event);
241
242         mouse_down_point_ = event->pos();
243         mouse_down_item_ = get_mouse_over_item(event->pos());
244
245         if (event->button() & Qt::LeftButton)
246                 mouse_left_press_event(event);
247 }
248
249 void ViewWidget::mouseReleaseEvent(QMouseEvent *event)
250 {
251         assert(event);
252         if (event->button() & Qt::LeftButton)
253                 mouse_left_release_event(event);
254
255         mouse_down_point_ = QPoint(INT_MIN, INT_MIN);
256         mouse_down_item_ = nullptr;
257 }
258
259 void ViewWidget::mouseMoveEvent(QMouseEvent *event)
260 {
261         assert(event);
262         mouse_point_ = event->pos();
263
264         if (!(event->buttons() & Qt::LeftButton))
265                 return;
266
267         if (!item_dragging_)
268         {
269                 if ((event->pos() - mouse_down_point_).manhattanLength() <
270                         QApplication::startDragDistance())
271                         return;
272
273                 if (!accept_drag())
274                         return;
275
276                 item_dragging_ = true;
277         }
278
279         // Do the drag
280         drag_items(event->pos() - mouse_down_point_);
281
282         update();
283 }
284
285 void ViewWidget::leaveEvent(QEvent*)
286 {
287         mouse_point_ = QPoint(-1, -1);
288         update();
289 }
290
291 } // namespace view
292 } // namespace pv