]> sigrok.org Git - pulseview.git/blame_incremental - pv/view/header.cpp
Header: Added on_ungroup method
[pulseview.git] / pv / view / header.cpp
... / ...
CommitLineData
1/*
2 * This file is part of the PulseView project.
3 *
4 * Copyright (C) 2012 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 "header.h"
22#include "view.h"
23
24#include "signal.h"
25#include "tracegroup.h"
26#include "../sigsession.h"
27
28#include <cassert>
29#include <algorithm>
30
31#include <boost/iterator/filter_iterator.hpp>
32
33#include <QApplication>
34#include <QMenu>
35#include <QMouseEvent>
36#include <QPainter>
37#include <QRect>
38
39#include <pv/widgets/popup.h>
40
41using boost::make_filter_iterator;
42using std::dynamic_pointer_cast;
43using std::max;
44using std::make_pair;
45using std::min;
46using std::pair;
47using std::shared_ptr;
48using std::stable_sort;
49using std::vector;
50
51namespace pv {
52namespace view {
53
54const int Header::Padding = 12;
55const int Header::BaselineOffset = 5;
56
57static bool item_selected(shared_ptr<RowItem> r)
58{
59 return r->selected();
60}
61
62Header::Header(View &parent) :
63 MarginWidget(parent),
64 _dragging(false)
65{
66 setFocusPolicy(Qt::ClickFocus);
67 setMouseTracking(true);
68
69 connect(&_view, SIGNAL(signals_moved()),
70 this, SLOT(on_signals_moved()));
71}
72
73QSize Header::sizeHint() const
74{
75 QRectF max_rect(-Padding, 0, Padding, 0);
76 for (auto &i : _view)
77 if (i->enabled())
78 max_rect = max_rect.united(i->label_rect(0));
79 return QSize(max_rect.width() + Padding + BaselineOffset, 0);
80}
81
82shared_ptr<RowItem> Header::get_mouse_over_row_item(const QPoint &pt)
83{
84 const int w = width() - BaselineOffset;
85 for (auto &i : _view)
86 if (i->enabled() && i->label_rect(w).contains(pt))
87 return i;
88 return shared_ptr<RowItem>();
89}
90
91void Header::clear_selection()
92{
93 for (auto &i : _view)
94 i->select(false);
95 update();
96}
97
98void Header::show_popup(const shared_ptr<RowItem> &item)
99{
100 using pv::widgets::Popup;
101
102 Popup *const p = item->create_popup(&_view);
103 if (!p)
104 return;
105
106 const QPoint pt(width() - BaselineOffset, item->get_visual_y());
107 p->set_position(mapToGlobal(pt), Popup::Right);
108 p->show();
109}
110
111void Header::paintEvent(QPaintEvent*)
112{
113 // The trace labels are not drawn with the arrows exactly on the
114 // left edge of the widget, because then the selection shadow
115 // would be clipped away.
116 const int w = width() - BaselineOffset;
117
118 vector< shared_ptr<RowItem> > row_items(
119 _view.begin(), _view.end());
120
121 stable_sort(row_items.begin(), row_items.end(),
122 [](const shared_ptr<RowItem> &a, const shared_ptr<RowItem> &b) {
123 return a->visual_v_offset() < b->visual_v_offset(); });
124
125 QPainter painter(this);
126 painter.setRenderHint(QPainter::Antialiasing);
127
128 for (const shared_ptr<RowItem> r : row_items)
129 {
130 assert(r);
131
132 const bool highlight = !_dragging &&
133 r->label_rect(w).contains(_mouse_point);
134 r->paint_label(painter, w, highlight);
135 }
136
137 painter.end();
138}
139
140void Header::mouseLeftPressEvent(QMouseEvent *event)
141{
142 (void)event;
143
144 const bool ctrl_pressed =
145 QApplication::keyboardModifiers() & Qt::ControlModifier;
146
147 // Clear selection if control is not pressed and this item is unselected
148 if ((!_mouse_down_item || !_mouse_down_item->selected()) &&
149 !ctrl_pressed)
150 for (shared_ptr<RowItem> r : _view)
151 r->select(false);
152
153 // Set the signal selection state if the item has been clicked
154 if (_mouse_down_item) {
155 if (ctrl_pressed)
156 _mouse_down_item->select(!_mouse_down_item->selected());
157 else
158 _mouse_down_item->select(true);
159 }
160
161 // Save the offsets of any signals which will be dragged
162 for (const shared_ptr<RowItem> r : _view)
163 if (r->selected())
164 r->drag();
165
166 selection_changed();
167 update();
168}
169
170void Header::mousePressEvent(QMouseEvent *event)
171{
172 assert(event);
173
174 _mouse_down_point = event->pos();
175 _mouse_down_item = get_mouse_over_row_item(event->pos());
176
177 if (event->button() & Qt::LeftButton)
178 mouseLeftPressEvent(event);
179}
180
181void Header::mouseLeftReleaseEvent(QMouseEvent *event)
182{
183 assert(event);
184
185 const bool ctrl_pressed =
186 QApplication::keyboardModifiers() & Qt::ControlModifier;
187
188 // Unselect everything if control is not pressed
189 const shared_ptr<RowItem> mouse_over =
190 get_mouse_over_row_item(event->pos());
191
192 for (auto &r : _view)
193 r->drag_release();
194
195 if (_dragging)
196 _view.restack_all_row_items();
197 else
198 {
199 if (!ctrl_pressed) {
200 for (shared_ptr<RowItem> r : _view)
201 if (_mouse_down_item != r)
202 r->select(false);
203
204 if (_mouse_down_item)
205 show_popup(_mouse_down_item);
206 }
207 }
208
209 _dragging = false;
210}
211
212void Header::mouseReleaseEvent(QMouseEvent *event)
213{
214 assert(event);
215 if (event->button() & Qt::LeftButton)
216 mouseLeftReleaseEvent(event);
217
218 _mouse_down_item = nullptr;
219}
220
221void Header::mouseMoveEvent(QMouseEvent *event)
222{
223 assert(event);
224 _mouse_point = event->pos();
225
226 if (!(event->buttons() & Qt::LeftButton))
227 return;
228
229 if ((event->pos() - _mouse_down_point).manhattanLength() <
230 QApplication::startDragDistance())
231 return;
232
233 // Check all the drag items share a common owner
234 RowItemOwner *item_owner = nullptr;
235 for (shared_ptr<RowItem> r : _view)
236 if (r->dragging()) {
237 if (!item_owner)
238 item_owner = r->owner();
239 else if(item_owner != r->owner())
240 return;
241 }
242
243 if (!item_owner)
244 return;
245
246 // Do the drag
247 _dragging = true;
248
249 const int delta = event->pos().y() - _mouse_down_point.y();
250
251 for (std::shared_ptr<RowItem> r : _view)
252 if (r->dragging()) {
253 r->force_to_v_offset(r->drag_point().y() + delta);
254
255 // Ensure the trace is selected
256 r->select();
257 }
258
259 item_owner->restack_items();
260 for (const auto &r : *item_owner)
261 r->animate_to_layout_v_offset();
262 signals_moved();
263
264 update();
265}
266
267void Header::leaveEvent(QEvent*)
268{
269 _mouse_point = QPoint(-1, -1);
270 update();
271}
272
273void Header::contextMenuEvent(QContextMenuEvent *event)
274{
275 const shared_ptr<RowItem> r = get_mouse_over_row_item(_mouse_point);
276 if (!r)
277 return;
278
279 QMenu *menu = r->create_context_menu(this);
280 if (!menu)
281 menu = new QMenu(this);
282
283 if (std::count_if(_view.begin(), _view.end(), item_selected) > 1)
284 {
285 menu->addSeparator();
286
287 QAction *const group = new QAction(tr("Group"), this);
288 QList<QKeySequence> shortcuts;
289 shortcuts.append(QKeySequence(Qt::ControlModifier | Qt::Key_G));
290 group->setShortcuts(shortcuts);
291 connect(group, SIGNAL(triggered()), this, SLOT(on_group()));
292 menu->addAction(group);
293 }
294
295 menu->exec(event->globalPos());
296}
297
298void Header::keyPressEvent(QKeyEvent *e)
299{
300 assert(e);
301
302 switch (e->key())
303 {
304 case Qt::Key_Delete:
305 {
306 for (const shared_ptr<RowItem> r : _view)
307 if (r->selected())
308 r->delete_pressed();
309 break;
310 }
311 }
312}
313
314void Header::on_signals_moved()
315{
316 update();
317}
318
319void Header::on_group()
320{
321 vector< shared_ptr<RowItem> > selected_items(
322 make_filter_iterator(item_selected, _view.begin(), _view.end()),
323 make_filter_iterator(item_selected, _view.end(), _view.end()));
324 stable_sort(selected_items.begin(), selected_items.end(),
325 [](const shared_ptr<RowItem> &a, const shared_ptr<RowItem> &b) {
326 return a->visual_v_offset() < b->visual_v_offset(); });
327
328 shared_ptr<TraceGroup> group(new TraceGroup());
329 shared_ptr<RowItem> focus_item(
330 _mouse_down_item ? _mouse_down_item : selected_items.front());
331
332 assert(focus_item);
333 assert(focus_item->owner());
334 focus_item->owner()->add_child_item(group);
335
336 // Set the group v_offset here before reparenting
337 group->force_to_v_offset(focus_item->layout_v_offset() +
338 focus_item->v_extents().first);
339
340 for (size_t i = 0; i < selected_items.size(); i++) {
341 const shared_ptr<RowItem> &r = selected_items[i];
342 assert(r->owner());
343 r->owner()->remove_child_item(r);
344 group->add_child_item(r);
345
346 // Put the items at 1-pixel offsets, so that restack will
347 // stack them in the right order
348 r->set_layout_v_offset(i);
349 }
350}
351
352void Header::on_ungroup()
353{
354 bool restart;
355 do {
356 restart = false;
357 for (const shared_ptr<RowItem> r : _view) {
358 const shared_ptr<TraceGroup> tg =
359 dynamic_pointer_cast<TraceGroup>(r);
360 if (tg && tg->selected()) {
361 tg->ungroup();
362 restart = true;
363 break;
364 }
365 }
366 } while(restart);
367}
368
369} // namespace view
370} // namespace pv