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.hpp"
30 #include <boost/iterator/filter_iterator.hpp>
32 #include <QApplication>
34 #include <QMouseEvent>
38 #include <pv/session.hpp>
39 #include <pv/widgets/popup.hpp>
41 using boost::make_filter_iterator;
42 using std::dynamic_pointer_cast;
47 using std::shared_ptr;
48 using std::stable_sort;
54 const int Header::Padding = 12;
55 const int Header::BaselineOffset = 5;
57 static bool item_selected(shared_ptr<RowItem> r)
62 Header::Header(View &parent) :
66 setFocusPolicy(Qt::ClickFocus);
67 setMouseTracking(true);
69 connect(&view_, SIGNAL(signals_moved()),
70 this, SLOT(on_signals_moved()));
73 QSize Header::sizeHint() const
75 QRectF max_rect(-Padding, 0, Padding, 0);
78 max_rect = max_rect.united(i->label_rect(QRect()));
79 return QSize(max_rect.width() + Padding + BaselineOffset, 0);
82 shared_ptr<RowItem> Header::get_mouse_over_row_item(const QPoint &pt)
84 const QRect r(BaselineOffset, 0, width() - BaselineOffset, height());
86 if (i->enabled() && i->label_rect(r).contains(pt))
88 return shared_ptr<RowItem>();
91 void Header::clear_selection()
98 void Header::show_popup(const shared_ptr<RowItem> &item)
100 using pv::widgets::Popup;
102 Popup *const p = item->create_popup(&view_);
106 const QPoint pt(width() - BaselineOffset, item->get_visual_y());
107 p->set_position(mapToGlobal(pt), Popup::Right);
111 void Header::paintEvent(QPaintEvent*)
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 QRect rect(BaselineOffset, 0, width() - BaselineOffset, height());
118 vector< shared_ptr<RowItem> > row_items(
119 view_.begin(), view_.end());
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(); });
125 QPainter painter(this);
126 painter.setRenderHint(QPainter::Antialiasing);
128 for (const shared_ptr<RowItem> r : row_items)
132 const bool highlight = !dragging_ &&
133 r->label_rect(rect).contains(mouse_point_);
134 r->paint_label(painter, rect, highlight);
140 void Header::mouseLeftPressEvent(QMouseEvent *event)
144 const bool ctrl_pressed =
145 QApplication::keyboardModifiers() & Qt::ControlModifier;
147 // Clear selection if control is not pressed and this item is unselected
148 if ((!mouse_down_item_ || !mouse_down_item_->selected()) &&
150 for (shared_ptr<RowItem> r : view_)
153 // Set the signal selection state if the item has been clicked
154 if (mouse_down_item_) {
156 mouse_down_item_->select(!mouse_down_item_->selected());
158 mouse_down_item_->select(true);
161 // Save the offsets of any signals which will be dragged
162 for (const shared_ptr<RowItem> r : view_)
170 void Header::mousePressEvent(QMouseEvent *event)
174 mouse_down_point_ = event->pos();
175 mouse_down_item_ = get_mouse_over_row_item(event->pos());
177 if (event->button() & Qt::LeftButton)
178 mouseLeftPressEvent(event);
181 void Header::mouseLeftReleaseEvent(QMouseEvent *event)
185 const bool ctrl_pressed =
186 QApplication::keyboardModifiers() & Qt::ControlModifier;
188 // Unselect everything if control is not pressed
189 const shared_ptr<RowItem> mouse_over =
190 get_mouse_over_row_item(event->pos());
192 for (auto &r : view_)
196 view_.restack_all_row_items();
200 for (shared_ptr<RowItem> r : view_)
201 if (mouse_down_item_ != r)
204 if (mouse_down_item_)
205 show_popup(mouse_down_item_);
212 void Header::mouseReleaseEvent(QMouseEvent *event)
215 if (event->button() & Qt::LeftButton)
216 mouseLeftReleaseEvent(event);
218 mouse_down_item_ = nullptr;
221 void Header::mouseMoveEvent(QMouseEvent *event)
224 mouse_point_ = event->pos();
226 if (!(event->buttons() & Qt::LeftButton))
229 if ((event->pos() - mouse_down_point_).manhattanLength() <
230 QApplication::startDragDistance())
233 // Check all the drag items share a common owner
234 RowItemOwner *item_owner = nullptr;
235 for (shared_ptr<RowItem> r : view_)
238 item_owner = r->owner();
239 else if(item_owner != r->owner())
249 const int delta = event->pos().y() - mouse_down_point_.y();
251 for (std::shared_ptr<RowItem> r : view_)
253 r->force_to_v_offset(r->drag_point().y() + delta);
255 // Ensure the trace is selected
259 item_owner->restack_items();
260 for (const auto &r : *item_owner)
261 r->animate_to_layout_v_offset();
267 void Header::leaveEvent(QEvent*)
269 mouse_point_ = QPoint(-1, -1);
273 void Header::contextMenuEvent(QContextMenuEvent *event)
275 const shared_ptr<RowItem> r = get_mouse_over_row_item(mouse_point_);
279 QMenu *menu = r->create_context_menu(this);
281 menu = new QMenu(this);
283 if (std::count_if(view_.begin(), view_.end(), item_selected) > 1)
285 menu->addSeparator();
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);
295 menu->exec(event->globalPos());
298 void Header::keyPressEvent(QKeyEvent *e)
302 if (e->key() == Qt::Key_Delete)
304 for (const shared_ptr<RowItem> r : view_)
308 else if (e->key() == Qt::Key_G && e->modifiers() == Qt::ControlModifier)
310 else if (e->key() == Qt::Key_U && e->modifiers() == Qt::ControlModifier)
314 void Header::on_signals_moved()
319 void Header::on_group()
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(); });
328 shared_ptr<TraceGroup> group(new TraceGroup());
329 shared_ptr<RowItem> focus_item(
330 mouse_down_item_ ? mouse_down_item_ : selected_items.front());
333 assert(focus_item->owner());
334 focus_item->owner()->add_child_item(group);
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);
340 for (size_t i = 0; i < selected_items.size(); i++) {
341 const shared_ptr<RowItem> &r = selected_items[i];
343 r->owner()->remove_child_item(r);
344 group->add_child_item(r);
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);
352 void Header::on_ungroup()
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()) {