File: header.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 (215 lines) | stat: -rw-r--r-- 5,838 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
/*
 * This file is part of the PulseView project.
 *
 * Copyright (C) 2012 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 "header.hpp"
#include "view.hpp"

#include "signal.hpp"
#include "tracegroup.hpp"

#include <algorithm>
#include <cassert>

#include <boost/iterator/filter_iterator.hpp>

#include <QApplication>
#include <QMenu>
#include <QMouseEvent>
#include <QPainter>
#include <QRect>

#include <pv/session.hpp>
#include <pv/widgets/popup.hpp>

using boost::make_filter_iterator;

using std::count_if;
using std::dynamic_pointer_cast;
using std::shared_ptr;
using std::stable_sort;
using std::vector;

namespace pv {
namespace views {
namespace trace {

const int Header::Padding = 12;

static bool item_selected(shared_ptr<TraceTreeItem> r)
{
	return r->selected();
}

Header::Header(View &parent) :
	MarginWidget(parent)
{
}

QSize Header::sizeHint() const
{
	QRectF max_rect(-Padding, 0, Padding, 0);
	const vector<shared_ptr<TraceTreeItem>> items(
		view_.list_by_type<TraceTreeItem>());
	for (auto &i : items)
		if (i->enabled())
			max_rect = max_rect.united(i->label_rect(QRect()));
	return QSize(max_rect.width() + Padding, 0);
}

QSize Header::extended_size_hint() const
{
	return sizeHint() + QSize(ViewItem::HighlightRadius, 0);
}

vector< shared_ptr<ViewItem> > Header::items()
{
	const vector<shared_ptr<TraceTreeItem>> items(
		view_.list_by_type<TraceTreeItem>());
	return vector< shared_ptr<ViewItem> >(items.begin(), items.end());
}

shared_ptr<ViewItem> Header::get_mouse_over_item(const QPoint &pt)
{
	const QRect r(0, 0, width(), height());
	const vector<shared_ptr<TraceTreeItem>> items(
		view_.list_by_type<TraceTreeItem>());
	for (auto i = items.rbegin(); i != items.rend(); i++)
		if ((*i)->enabled() && (*i)->label_rect(r).contains(pt))
			return *i;
	return shared_ptr<TraceTreeItem>();
}

void Header::paintEvent(QPaintEvent*)
{
	const QRect rect(0, 0, width(), height());

	vector< shared_ptr<ViewItem> > items(view_.list_by_type<ViewItem>());

	stable_sort(items.begin(), items.end(),
		[](const shared_ptr<ViewItem> &a, const shared_ptr<ViewItem> &b) {
			return a->drag_point(QRect()).y() < b->drag_point(QRect()).y(); });

	QPainter painter(this);
	painter.setRenderHint(QPainter::Antialiasing);

	for (const shared_ptr<ViewItem>& r : items) {
		assert(r);

		const bool highlight = !item_dragging_ &&
			r->label_rect(rect).contains(mouse_point_);
		r->paint_label(painter, rect, highlight);
	}

	painter.end();
}

void Header::contextMenuEvent(QContextMenuEvent *event)
{
	const shared_ptr<ViewItem> r = get_mouse_over_item(mouse_point_);
	if (!r)
		return;

	QMenu *menu = r->create_header_context_menu(this);
	if (!menu)
		menu = new QMenu(this);

	const vector< shared_ptr<TraceTreeItem> > items(
		view_.list_by_type<TraceTreeItem>());
	if (count_if(items.begin(), items.end(), item_selected) > 1) {
		menu->addSeparator();

		QAction *const group = new QAction(tr("Group"), this);
		QList<QKeySequence> shortcuts;
		shortcuts.append(QKeySequence(Qt::ControlModifier | Qt::Key_G));
		group->setShortcuts(shortcuts);
		connect(group, SIGNAL(triggered()), this, SLOT(on_group()));
		menu->addAction(group);
	}

	menu->popup(event->globalPos());
}

void Header::keyPressEvent(QKeyEvent *event)
{
	assert(event);

	MarginWidget::keyPressEvent(event);

	if (event->key() == Qt::Key_G && event->modifiers() == Qt::ControlModifier)
		on_group();
	else if (event->key() == Qt::Key_U && event->modifiers() == Qt::ControlModifier)
		on_ungroup();
}

void Header::on_group()
{
	const vector< shared_ptr<TraceTreeItem> > items(
		view_.list_by_type<TraceTreeItem>());
	vector< shared_ptr<TraceTreeItem> > selected_items(
		make_filter_iterator(item_selected, items.begin(), items.end()),
		make_filter_iterator(item_selected, items.end(), items.end()));
	stable_sort(selected_items.begin(), selected_items.end(),
		[](const shared_ptr<TraceTreeItem> &a, const shared_ptr<TraceTreeItem> &b) {
			return a->visual_v_offset() < b->visual_v_offset(); });

	shared_ptr<TraceGroup> group(new TraceGroup());
	shared_ptr<TraceTreeItem> mouse_down_item(
		dynamic_pointer_cast<TraceTreeItem>(mouse_down_item_));
	shared_ptr<TraceTreeItem> focus_item(
		mouse_down_item ? mouse_down_item : selected_items.front());

	assert(focus_item);
	assert(focus_item->owner());
	focus_item->owner()->add_child_item(group);

	// Set the group v_offset here before reparenting
	group->force_to_v_offset(focus_item->layout_v_offset() +
		focus_item->v_extents().first);

	for (size_t i = 0; i < selected_items.size(); i++) {
		const shared_ptr<TraceTreeItem> &r = selected_items[i];
		assert(r->owner());
		r->owner()->remove_child_item(r);
		group->add_child_item(r);

		// Put the items at 1-pixel offsets, so that restack will
		// stack them in the right order
		r->set_layout_v_offset(i);
	}
}

void Header::on_ungroup()
{
	bool restart;
	do {
		restart = false;
		const vector< shared_ptr<TraceGroup> > groups(
			view_.list_by_type<TraceGroup>());
		for (const shared_ptr<TraceGroup>& tg : groups)
			if (tg->selected()) {
				tg->ungroup();
				restart = true;
				break;
			}
	} while (restart);
}

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