2 * This file is part of the PulseView project.
4 * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk>
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.
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.
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
25 #include "tracegroup.h"
26 #include "../sigsession.h"
31 #include <boost/iterator/filter_iterator.hpp>
33 #include <QApplication>
35 #include <QMouseEvent>
39 #include <pv/widgets/popup.h>
41 using boost::make_filter_iterator;
46 using std::shared_ptr;
47 using std::stable_sort;
53 const int Header::Padding = 12;
54 const int Header::BaselineOffset = 5;
56 static bool item_selected(shared_ptr<RowItem> r)
61 Header::Header(View &parent) :
65 setFocusPolicy(Qt::ClickFocus);
66 setMouseTracking(true);
68 connect(&_view, SIGNAL(signals_moved()),
69 this, SLOT(on_signals_moved()));
72 QSize Header::sizeHint() const
74 QRectF max_rect(-Padding, 0, Padding, 0);
77 max_rect = max_rect.united(i->label_rect(0));
78 return QSize(max_rect.width() + Padding + BaselineOffset, 0);
81 shared_ptr<RowItem> Header::get_mouse_over_row_item(const QPoint &pt)
83 const int w = width() - BaselineOffset;
85 if (i->enabled() && i->label_rect(w).contains(pt))
87 return shared_ptr<RowItem>();
90 void Header::clear_selection()
97 void Header::show_popup(const shared_ptr<RowItem> &item)
99 using pv::widgets::Popup;
101 Popup *const p = item->create_popup(&_view);
105 const QPoint pt(width() - BaselineOffset, item->get_visual_y());
106 p->set_position(mapToGlobal(pt), Popup::Right);
110 void Header::paintEvent(QPaintEvent*)
112 // The trace labels are not drawn with the arrows exactly on the
113 // left edge of the widget, because then the selection shadow
114 // would be clipped away.
115 const int w = width() - BaselineOffset;
117 vector< shared_ptr<RowItem> > row_items(
118 _view.begin(), _view.end());
120 stable_sort(row_items.begin(), row_items.end(),
121 [](const shared_ptr<RowItem> &a, const shared_ptr<RowItem> &b) {
122 return a->visual_v_offset() < b->visual_v_offset(); });
124 QPainter painter(this);
125 painter.setRenderHint(QPainter::Antialiasing);
127 for (const shared_ptr<RowItem> r : row_items)
131 const bool highlight = !_dragging &&
132 r->label_rect(w).contains(_mouse_point);
133 r->paint_label(painter, w, highlight);
139 void Header::mouseLeftPressEvent(QMouseEvent *event)
143 const bool ctrl_pressed =
144 QApplication::keyboardModifiers() & Qt::ControlModifier;
146 // Clear selection if control is not pressed and this item is unselected
147 if ((!_mouse_down_item || !_mouse_down_item->selected()) &&
149 for (shared_ptr<RowItem> r : _view)
152 // Set the signal selection state if the item has been clicked
153 if (_mouse_down_item) {
155 _mouse_down_item->select(!_mouse_down_item->selected());
157 _mouse_down_item->select(true);
160 // Save the offsets of any signals which will be dragged
161 for (const shared_ptr<RowItem> r : _view)
169 void Header::mousePressEvent(QMouseEvent *event)
173 _mouse_down_point = event->pos();
174 _mouse_down_item = get_mouse_over_row_item(event->pos());
176 if (event->button() & Qt::LeftButton)
177 mouseLeftPressEvent(event);
180 void Header::mouseLeftReleaseEvent(QMouseEvent *event)
184 const bool ctrl_pressed =
185 QApplication::keyboardModifiers() & Qt::ControlModifier;
187 // Unselect everything if control is not pressed
188 const shared_ptr<RowItem> mouse_over =
189 get_mouse_over_row_item(event->pos());
191 for (auto &r : _view)
195 _view.restack_all_row_items();
199 for (shared_ptr<RowItem> r : _view)
200 if (_mouse_down_item != r)
203 if (_mouse_down_item)
204 show_popup(_mouse_down_item);
211 void Header::mouseReleaseEvent(QMouseEvent *event)
214 if (event->button() & Qt::LeftButton)
215 mouseLeftReleaseEvent(event);
217 _mouse_down_item = nullptr;
220 void Header::mouseMoveEvent(QMouseEvent *event)
223 _mouse_point = event->pos();
225 if (!(event->buttons() & Qt::LeftButton))
228 if ((event->pos() - _mouse_down_point).manhattanLength() <
229 QApplication::startDragDistance())
232 // Check all the drag items share a common owner
233 RowItemOwner *item_owner = nullptr;
234 for (shared_ptr<RowItem> r : _view)
237 item_owner = r->owner();
238 else if(item_owner != r->owner())
248 const int delta = event->pos().y() - _mouse_down_point.y();
250 for (std::shared_ptr<RowItem> r : _view)
252 r->force_to_v_offset(r->drag_point().y() + delta);
254 // Ensure the trace is selected
258 item_owner->restack_items();
259 for (const auto &r : *item_owner)
260 r->animate_to_layout_v_offset();
266 void Header::leaveEvent(QEvent*)
268 _mouse_point = QPoint(-1, -1);
272 void Header::contextMenuEvent(QContextMenuEvent *event)
274 const shared_ptr<RowItem> r = get_mouse_over_row_item(_mouse_point);
278 QMenu *menu = r->create_context_menu(this);
280 menu = new QMenu(this);
282 if (std::count_if(_view.begin(), _view.end(), item_selected) > 1)
284 menu->addSeparator();
286 QAction *const group = new QAction(tr("Group"), this);
287 QList<QKeySequence> shortcuts;
288 shortcuts.append(QKeySequence(Qt::ControlModifier | Qt::Key_G));
289 group->setShortcuts(shortcuts);
290 connect(group, SIGNAL(triggered()), this, SLOT(on_group()));
291 menu->addAction(group);
294 menu->exec(event->globalPos());
297 void Header::keyPressEvent(QKeyEvent *e)
305 for (const shared_ptr<RowItem> r : _view)
313 void Header::on_signals_moved()
318 void Header::on_group()
320 vector< shared_ptr<RowItem> > selected_items(
321 make_filter_iterator(item_selected, _view.begin(), _view.end()),
322 make_filter_iterator(item_selected, _view.end(), _view.end()));
323 stable_sort(selected_items.begin(), selected_items.end(),
324 [](const shared_ptr<RowItem> &a, const shared_ptr<RowItem> &b) {
325 return a->visual_v_offset() < b->visual_v_offset(); });
327 shared_ptr<TraceGroup> group(new TraceGroup());
328 shared_ptr<RowItem> focus_item(
329 _mouse_down_item ? _mouse_down_item : selected_items.front());
332 assert(focus_item->owner());
333 focus_item->owner()->add_child_item(group);
335 // Set the group v_offset here before reparenting
336 group->force_to_v_offset(focus_item->layout_v_offset() +
337 focus_item->v_extents().first);
339 for (size_t i = 0; i < selected_items.size(); i++) {
340 const shared_ptr<RowItem> &r = selected_items[i];
342 r->owner()->remove_child_item(r);
343 group->add_child_item(r);
345 // Put the items at 1-pixel offsets, so that restack will
346 // stack them in the right order
347 r->set_layout_v_offset(i);