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