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) :
65 connect(&view_, SIGNAL(signals_moved()),
66 this, SLOT(on_signals_moved()));
69 QSize Header::sizeHint() const
71 QRectF max_rect(-Padding, 0, Padding, 0);
74 max_rect = max_rect.united(i->label_rect(QRect()));
75 return QSize(max_rect.width() + Padding + BaselineOffset, 0);
78 QSize Header::extended_size_hint() const
80 return sizeHint() + QSize(ViewItem::HighlightRadius, 0);
83 vector< shared_ptr<ViewItem> > Header::items()
85 return vector< shared_ptr<ViewItem> >(view_.begin(), view_.end());
88 shared_ptr<ViewItem> Header::get_mouse_over_item(const QPoint &pt)
90 const QRect r(0, 0, width() - BaselineOffset, height());
92 if (i->enabled() && i->label_rect(r).contains(pt))
94 return shared_ptr<RowItem>();
97 void Header::clear_selection()
104 void Header::paintEvent(QPaintEvent*)
106 // The trace labels are not drawn with the arrows exactly on the
107 // left edge of the widget, because then the selection shadow
108 // would be clipped away.
109 const QRect rect(0, 0, width() - BaselineOffset, height());
111 vector< shared_ptr<RowItem> > row_items(
112 view_.begin(), view_.end());
114 stable_sort(row_items.begin(), row_items.end(),
115 [](const shared_ptr<RowItem> &a, const shared_ptr<RowItem> &b) {
116 return a->visual_v_offset() < b->visual_v_offset(); });
118 QPainter painter(this);
119 painter.setRenderHint(QPainter::Antialiasing);
121 for (const shared_ptr<RowItem> r : row_items)
125 const bool highlight = !dragging_ &&
126 r->label_rect(rect).contains(mouse_point_);
127 r->paint_label(painter, rect, highlight);
133 void Header::mouseLeftPressEvent(QMouseEvent *event)
137 const bool ctrl_pressed =
138 QApplication::keyboardModifiers() & Qt::ControlModifier;
140 // Clear selection if control is not pressed and this item is unselected
141 if ((!mouse_down_item_ || !mouse_down_item_->selected()) &&
143 for (shared_ptr<RowItem> r : view_)
146 // Set the signal selection state if the item has been clicked
147 if (mouse_down_item_) {
149 mouse_down_item_->select(!mouse_down_item_->selected());
151 mouse_down_item_->select(true);
154 // Save the offsets of any signals which will be dragged
155 for (const shared_ptr<RowItem> r : view_)
163 void Header::mousePressEvent(QMouseEvent *event)
167 mouse_down_point_ = event->pos();
168 mouse_down_item_ = get_mouse_over_item(event->pos());
170 if (event->button() & Qt::LeftButton)
171 mouseLeftPressEvent(event);
174 void Header::mouseLeftReleaseEvent(QMouseEvent *event)
178 const bool ctrl_pressed =
179 QApplication::keyboardModifiers() & Qt::ControlModifier;
181 // Unselect everything if control is not pressed
182 const shared_ptr<ViewItem> mouse_over =
183 get_mouse_over_item(event->pos());
185 for (auto &r : view_)
189 view_.restack_all_row_items();
193 for (shared_ptr<RowItem> r : view_)
194 if (mouse_down_item_ != r)
197 if (mouse_down_item_)
198 show_popup(mouse_down_item_);
205 void Header::mouseReleaseEvent(QMouseEvent *event)
208 if (event->button() & Qt::LeftButton)
209 mouseLeftReleaseEvent(event);
211 mouse_down_item_ = nullptr;
214 void Header::mouseMoveEvent(QMouseEvent *event)
217 mouse_point_ = event->pos();
219 if (!(event->buttons() & Qt::LeftButton))
222 if ((event->pos() - mouse_down_point_).manhattanLength() <
223 QApplication::startDragDistance())
226 // Check all the drag items share a common owner
227 RowItemOwner *item_owner = nullptr;
228 for (shared_ptr<RowItem> r : view_)
231 item_owner = r->owner();
232 else if(item_owner != r->owner())
242 const QPoint delta = event->pos() - mouse_down_point_;
244 for (std::shared_ptr<RowItem> r : view_)
248 // Ensure the trace is selected
252 item_owner->restack_items();
253 for (const auto &r : *item_owner)
254 r->animate_to_layout_v_offset();
260 void Header::contextMenuEvent(QContextMenuEvent *event)
262 const shared_ptr<ViewItem> r = get_mouse_over_item(mouse_point_);
266 QMenu *menu = r->create_context_menu(this);
268 menu = new QMenu(this);
270 if (std::count_if(view_.begin(), view_.end(), item_selected) > 1)
272 menu->addSeparator();
274 QAction *const group = new QAction(tr("Group"), this);
275 QList<QKeySequence> shortcuts;
276 shortcuts.append(QKeySequence(Qt::ControlModifier | Qt::Key_G));
277 group->setShortcuts(shortcuts);
278 connect(group, SIGNAL(triggered()), this, SLOT(on_group()));
279 menu->addAction(group);
282 menu->exec(event->globalPos());
285 void Header::keyPressEvent(QKeyEvent *e)
289 if (e->key() == Qt::Key_Delete)
291 for (const shared_ptr<RowItem> r : view_)
295 else if (e->key() == Qt::Key_G && e->modifiers() == Qt::ControlModifier)
297 else if (e->key() == Qt::Key_U && e->modifiers() == Qt::ControlModifier)
301 void Header::on_signals_moved()
306 void Header::on_group()
308 vector< shared_ptr<RowItem> > selected_items(
309 make_filter_iterator(item_selected, view_.begin(), view_.end()),
310 make_filter_iterator(item_selected, view_.end(), view_.end()));
311 stable_sort(selected_items.begin(), selected_items.end(),
312 [](const shared_ptr<RowItem> &a, const shared_ptr<RowItem> &b) {
313 return a->visual_v_offset() < b->visual_v_offset(); });
315 shared_ptr<TraceGroup> group(new TraceGroup());
316 shared_ptr<RowItem> mouse_down_item(
317 std::dynamic_pointer_cast<RowItem>(mouse_down_item_));
318 shared_ptr<RowItem> focus_item(
319 mouse_down_item ? mouse_down_item : selected_items.front());
322 assert(focus_item->owner());
323 focus_item->owner()->add_child_item(group);
325 // Set the group v_offset here before reparenting
326 group->force_to_v_offset(focus_item->layout_v_offset() +
327 focus_item->v_extents().first);
329 for (size_t i = 0; i < selected_items.size(); i++) {
330 const shared_ptr<RowItem> &r = selected_items[i];
332 r->owner()->remove_child_item(r);
333 group->add_child_item(r);
335 // Put the items at 1-pixel offsets, so that restack will
336 // stack them in the right order
337 r->set_layout_v_offset(i);
341 void Header::on_ungroup()
346 for (const shared_ptr<RowItem> r : view_) {
347 const shared_ptr<TraceGroup> tg =
348 dynamic_pointer_cast<TraceGroup>(r);
349 if (tg && tg->selected()) {