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