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