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