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) :
67 QSize Header::sizeHint() const
69 QRectF max_rect(-Padding, 0, Padding, 0);
72 max_rect = max_rect.united(i->label_rect(QRect()));
73 return QSize(max_rect.width() + Padding + BaselineOffset, 0);
76 QSize Header::extended_size_hint() const
78 return sizeHint() + QSize(ViewItem::HighlightRadius, 0);
81 vector< shared_ptr<ViewItem> > Header::items()
83 return vector< shared_ptr<ViewItem> >(view_.begin(), view_.end());
86 shared_ptr<ViewItem> Header::get_mouse_over_item(const QPoint &pt)
88 const QRect r(0, 0, width() - BaselineOffset, height());
90 if (i->enabled() && i->label_rect(r).contains(pt))
92 return shared_ptr<RowItem>();
95 void Header::paintEvent(QPaintEvent*)
97 // The trace labels are not drawn with the arrows exactly on the
98 // left edge of the widget, because then the selection shadow
99 // would be clipped away.
100 const QRect rect(0, 0, width() - BaselineOffset, height());
102 vector< shared_ptr<RowItem> > row_items(
103 view_.begin(), view_.end());
105 stable_sort(row_items.begin(), row_items.end(),
106 [](const shared_ptr<RowItem> &a, const shared_ptr<RowItem> &b) {
107 return a->visual_v_offset() < b->visual_v_offset(); });
109 QPainter painter(this);
110 painter.setRenderHint(QPainter::Antialiasing);
112 for (const shared_ptr<RowItem> r : row_items)
116 const bool highlight = !item_dragging_ &&
117 r->label_rect(rect).contains(mouse_point_);
118 r->paint_label(painter, rect, highlight);
124 void Header::contextMenuEvent(QContextMenuEvent *event)
126 const shared_ptr<ViewItem> r = get_mouse_over_item(mouse_point_);
130 QMenu *menu = r->create_context_menu(this);
132 menu = new QMenu(this);
134 if (std::count_if(view_.begin(), view_.end(), item_selected) > 1)
136 menu->addSeparator();
138 QAction *const group = new QAction(tr("Group"), this);
139 QList<QKeySequence> shortcuts;
140 shortcuts.append(QKeySequence(Qt::ControlModifier | Qt::Key_G));
141 group->setShortcuts(shortcuts);
142 connect(group, SIGNAL(triggered()), this, SLOT(on_group()));
143 menu->addAction(group);
146 menu->exec(event->globalPos());
149 void Header::keyPressEvent(QKeyEvent *e)
153 MarginWidget::keyPressEvent(e);
155 if (e->key() == Qt::Key_G && e->modifiers() == Qt::ControlModifier)
157 else if (e->key() == Qt::Key_U && e->modifiers() == Qt::ControlModifier)
161 void Header::on_group()
163 vector< shared_ptr<RowItem> > selected_items(
164 make_filter_iterator(item_selected, view_.begin(), view_.end()),
165 make_filter_iterator(item_selected, view_.end(), view_.end()));
166 stable_sort(selected_items.begin(), selected_items.end(),
167 [](const shared_ptr<RowItem> &a, const shared_ptr<RowItem> &b) {
168 return a->visual_v_offset() < b->visual_v_offset(); });
170 shared_ptr<TraceGroup> group(new TraceGroup());
171 shared_ptr<RowItem> mouse_down_item(
172 std::dynamic_pointer_cast<RowItem>(mouse_down_item_));
173 shared_ptr<RowItem> focus_item(
174 mouse_down_item ? mouse_down_item : selected_items.front());
177 assert(focus_item->owner());
178 focus_item->owner()->add_child_item(group);
180 // Set the group v_offset here before reparenting
181 group->force_to_v_offset(focus_item->layout_v_offset() +
182 focus_item->v_extents().first);
184 for (size_t i = 0; i < selected_items.size(); i++) {
185 const shared_ptr<RowItem> &r = selected_items[i];
187 r->owner()->remove_child_item(r);
188 group->add_child_item(r);
190 // Put the items at 1-pixel offsets, so that restack will
191 // stack them in the right order
192 r->set_layout_v_offset(i);
196 void Header::on_ungroup()
201 for (const shared_ptr<RowItem> r : view_) {
202 const shared_ptr<TraceGroup> tg =
203 dynamic_pointer_cast<TraceGroup>(r);
204 if (tg && tg->selected()) {