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