File: viewwidget.cpp

package info (click to toggle)
pulseview 0.4.2-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,092 kB
  • sloc: cpp: 25,958; xml: 215; java: 42; makefile: 2
file content (366 lines) | stat: -rw-r--r-- 9,221 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
/*
 * This file is part of the PulseView project.
 *
 * Copyright (C) 2014 Joel Holdsworth <joel@airwebreathe.org.uk>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
 */

#include <QApplication>
#include <QMouseEvent>
#include <QTouchEvent>

#include "tracetreeitem.hpp"
#include "view.hpp"
#include "viewwidget.hpp"

using std::any_of;
using std::shared_ptr;
using std::vector;

namespace pv {
namespace views {
namespace trace {

ViewWidget::ViewWidget(View &parent) :
	QWidget(&parent),
	view_(parent),
	item_dragging_(false)
{
	setFocusPolicy(Qt::ClickFocus);
	setAttribute(Qt::WA_AcceptTouchEvents, true);
	setMouseTracking(true);
}

void ViewWidget::clear_selection()
{
	const auto items = this->items();
	for (auto &i : items)
		i->select(false);
}

void ViewWidget::item_hover(const shared_ptr<ViewItem> &item, QPoint pos)
{
	(void)item;
	(void)pos;
}

void ViewWidget::item_clicked(const shared_ptr<ViewItem> &item)
{
	(void)item;
}

bool ViewWidget::accept_drag() const
{
	const vector< shared_ptr<TimeItem> > items(view_.time_items());
	const vector< shared_ptr<TraceTreeItem> > trace_tree_items(
		view_.list_by_type<TraceTreeItem>());

	const bool any_row_items_selected = any_of(
		trace_tree_items.begin(), trace_tree_items.end(),
		[](const shared_ptr<TraceTreeItem> &r) { return r->selected(); });

	const bool any_time_items_selected = any_of(items.begin(), items.end(),
		[](const shared_ptr<TimeItem> &i) { return i->selected(); });

	if (any_row_items_selected && !any_time_items_selected) {
		// Check all the drag items share a common owner
		TraceTreeItemOwner *item_owner = nullptr;
		for (const shared_ptr<TraceTreeItem>& r : trace_tree_items)
			if (r->dragging()) {
				if (!item_owner)
					item_owner = r->owner();
				else if (item_owner != r->owner())
					return false;
			}

		return true;
	} else if (any_time_items_selected && !any_row_items_selected) {
		return true;
	}

	// A background drag is beginning
	return true;
}

bool ViewWidget::mouse_down() const
{
	return mouse_down_point_.x() != INT_MIN &&
		mouse_down_point_.y() != INT_MIN;
}

void ViewWidget::drag_items(const QPoint &delta)
{
	bool item_dragged = false;

	// Drag the row items
	const vector< shared_ptr<ViewItem> > row_items(
		view_.list_by_type<ViewItem>());
	for (const shared_ptr<ViewItem>& r : row_items)
		if (r->dragging()) {
			r->drag_by(delta);

			// Ensure the trace is selected
			r->select();

			item_dragged = true;
		}

	// If an item is being dragged, update the stacking
	TraceTreeItemOwner *item_owner = nullptr;
	const vector< shared_ptr<TraceTreeItem> > trace_tree_items(
		view_.list_by_type<TraceTreeItem>());
	for (const shared_ptr<TraceTreeItem>& i : trace_tree_items)
		if (i->dragging())
			item_owner = i->owner();

	if (item_owner) {
		item_owner->restack_items();
		for (shared_ptr<TraceTreeItem> i : trace_tree_items)
			i->animate_to_layout_v_offset();
	}

	// Drag the time items
	const vector< shared_ptr<TimeItem> > items(view_.time_items());
	for (auto &i : items)
		if (i->dragging()) {
			i->drag_by(delta);
			item_dragged = true;
		}

	// Do the background drag
	if (!item_dragged)
		drag_by(delta);
}

void ViewWidget::drag()
{
}

void ViewWidget::drag_by(const QPoint &delta)
{
	(void)delta;
}

void ViewWidget::drag_release()
{
}

void ViewWidget::mouse_left_press_event(QMouseEvent *event)
{
	(void)event;

	const bool ctrl_pressed =
		QApplication::keyboardModifiers() & Qt::ControlModifier;

	// Clear selection if control is not pressed and this item is unselected
	if ((!mouse_down_item_ || !mouse_down_item_->selected()) &&
		!ctrl_pressed)
		clear_selection();

	// Set the signal selection state if the item has been clicked
	if (mouse_down_item_ && mouse_down_item_->is_selectable(event->pos())) {
		if (ctrl_pressed)
			mouse_down_item_->select(!mouse_down_item_->selected());
		else
			mouse_down_item_->select(true);
	}

	// Save the offsets of any signals which will be dragged
	bool item_dragged = false;
	const auto items = this->items();
	for (auto &i : items)
		if (i->selected() && i->is_draggable(event->pos())) {
			item_dragged = true;
			i->drag();
		}

	// Do the background drag
	if (!item_dragged)
		drag();

	selection_changed();
}

void ViewWidget::mouse_left_release_event(QMouseEvent *event)
{
	assert(event);

	auto items = this->items();
	const bool ctrl_pressed =
		QApplication::keyboardModifiers() & Qt::ControlModifier;

	// Unselect everything if control is not pressed
	const shared_ptr<ViewItem> mouse_over =
		get_mouse_over_item(event->pos());

	for (auto &i : items)
		i->drag_release();

	if (item_dragging_)
		view_.restack_all_trace_tree_items();
	else {
		if (!ctrl_pressed) {
			for (shared_ptr<ViewItem> i : items)
				if (mouse_down_item_ != i)
					i->select(false);

			if (mouse_down_item_)
				item_clicked(mouse_down_item_);
		}
	}

	item_dragging_ = false;
}

bool ViewWidget::touch_event(QTouchEvent *event)
{
	(void)event;

	return false;
}

bool ViewWidget::event(QEvent *event)
{
	switch (event->type()) {
	case QEvent::TouchBegin:
	case QEvent::TouchUpdate:
	case QEvent::TouchEnd:
		if (touch_event(static_cast<QTouchEvent *>(event)))
			return true;
		break;

	default:
		break;
	}

	return QWidget::event(event);
}

void ViewWidget::mousePressEvent(QMouseEvent *event)
{
	assert(event);

	if (event->button() & Qt::LeftButton) {
		if (event->modifiers() & Qt::ShiftModifier)
			view_.show_cursors(false);

		mouse_down_point_ = event->pos();
		mouse_down_offset_ = view_.offset() + event->pos().x() * view_.scale();
		mouse_down_item_ = get_mouse_over_item(event->pos());
		mouse_left_press_event(event);
	}

	/* Don't forward right click events as they will open context menus when
	 * used on trace labels. Those menus prevent ViewWidget::mouseReleaseEvent()
	 * to be triggered upon button release, making mouse_down_item_
	 * hold the last reference to a view item that might have been deleted
	 * from the context menu, preventing it from being freed as intended.
	 * TODO Remove this once context menus are handled separately
	 */
	if (event->button() & Qt::RightButton)
		mouse_down_point_ = event->pos();
}

void ViewWidget::mouseReleaseEvent(QMouseEvent *event)
{
	assert(event);

	if (event->button() & Qt::LeftButton)
		mouse_left_release_event(event);

	mouse_down_point_ = QPoint(INT_MIN, INT_MIN);
	mouse_down_item_ = nullptr;
}

void ViewWidget::keyReleaseEvent(QKeyEvent *event)
{
	// Update mouse_modifiers_ also if modifiers change, but pointer doesn't move
	if ((mouse_point_.x() >= 0) && (mouse_point_.y() >= 0)) // mouse is inside
		mouse_modifiers_ = event->modifiers();
	update();
}

void ViewWidget::keyPressEvent(QKeyEvent *event)
{
	// Update mouse_modifiers_ also if modifiers change, but pointer doesn't move
	if ((mouse_point_.x() >= 0) && (mouse_point_.y() >= 0)) // mouse is inside
		mouse_modifiers_ = event->modifiers();
	update();
}

void ViewWidget::mouseMoveEvent(QMouseEvent *event)
{
	assert(event);
	mouse_point_ = event->pos();
	mouse_modifiers_ = event->modifiers();

	if (!event->buttons())
		item_hover(get_mouse_over_item(event->pos()), event->pos());

	if (event->buttons() & Qt::LeftButton) {
		if (event->modifiers() & Qt::ShiftModifier) {
			// Cursor drag
			pv::util::Timestamp current_offset = view_.offset() + event->pos().x() * view_.scale();

			const int drag_distance = qAbs(current_offset.convert_to<double>() -
				mouse_down_offset_.convert_to<double>()) / view_.scale();

			if (drag_distance > QApplication::startDragDistance()) {
				view_.show_cursors(true);
				view_.set_cursors(mouse_down_offset_, current_offset);
			} else
				view_.show_cursors(false);

		} else {
			if (!item_dragging_) {
				if ((event->pos() - mouse_down_point_).manhattanLength() <
					QApplication::startDragDistance())
					return;

				if (!accept_drag())
					return;

				item_dragging_ = true;
			}

			// Do the drag
			drag_items(event->pos() - mouse_down_point_);
		}
	}

	// Force a repaint of the widget to update highlighted parts
	update();
}

void ViewWidget::leaveEvent(QEvent*)
{
	bool cursor_above_widget = rect().contains(mapFromGlobal(QCursor::pos()));

	// We receive leaveEvent also when the widget loses focus even when
	// the mouse cursor hasn't moved at all - e.g. when the popup shows.
	// However, we don't want to reset mouse_position_ when the mouse is
	// still above this widget as doing so would break the context menu
	if (!cursor_above_widget)
		mouse_point_ = QPoint(INT_MIN, INT_MIN);

	mouse_modifiers_ = Qt::NoModifier;
	item_hover(nullptr, QPoint());

	update();
}

} // namespace trace
} // namespace views
} // namespace pv