From: Joel Holdsworth Date: Sun, 2 Nov 2014 16:07:23 +0000 (+0000) Subject: Header: Added group command X-Git-Tag: pulseview-0.3.0~445 X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=73ba2d70ce244f3582045498ace31f54485c316f;p=pulseview.git Header: Added group command --- diff --git a/pv/view/header.cpp b/pv/view/header.cpp index c94e2d61..91a6ecfd 100644 --- a/pv/view/header.cpp +++ b/pv/view/header.cpp @@ -22,11 +22,14 @@ #include "view.h" #include "signal.h" +#include "tracegroup.h" #include "../sigsession.h" #include #include +#include + #include #include #include @@ -35,8 +38,10 @@ #include +using boost::make_filter_iterator; using std::max; using std::make_pair; +using std::min; using std::pair; using std::shared_ptr; using std::stable_sort; @@ -48,6 +53,11 @@ namespace view { const int Header::Padding = 12; const int Header::BaselineOffset = 5; +static bool item_selected(shared_ptr r) +{ + return r->selected(); +} + Header::Header(View &parent) : MarginWidget(parent), _dragging(false) @@ -265,9 +275,21 @@ void Header::contextMenuEvent(QContextMenuEvent *event) if (!r) return; - QMenu *const menu = r->create_context_menu(this); + QMenu *menu = r->create_context_menu(this); if (!menu) - return; + menu = new QMenu(this); + + if (std::count_if(_view.begin(), _view.end(), item_selected) > 1) + { + menu->addSeparator(); + + QAction *const group = new QAction(tr("Group"), this); + QList shortcuts; + shortcuts.append(QKeySequence(Qt::ControlModifier | Qt::Key_G)); + group->setShortcuts(shortcuts); + connect(group, SIGNAL(triggered()), this, SLOT(on_group())); + menu->addAction(group); + } menu->exec(event->globalPos()); } @@ -293,5 +315,38 @@ void Header::on_signals_moved() update(); } +void Header::on_group() +{ + vector< shared_ptr > selected_items( + make_filter_iterator(item_selected, _view.begin(), _view.end()), + make_filter_iterator(item_selected, _view.end(), _view.end())); + stable_sort(selected_items.begin(), selected_items.end(), + [](const shared_ptr &a, const shared_ptr &b) { + return a->visual_v_offset() < b->visual_v_offset(); }); + + shared_ptr group(new TraceGroup()); + shared_ptr focus_item( + _mouse_down_item ? _mouse_down_item : selected_items.front()); + + assert(focus_item); + assert(focus_item->owner()); + focus_item->owner()->add_child_item(group); + + // Set the group v_offset here before reparenting + group->force_to_v_offset(focus_item->layout_v_offset() + + focus_item->v_extents().first); + + for (size_t i = 0; i < selected_items.size(); i++) { + const shared_ptr &r = selected_items[i]; + assert(r->owner()); + r->owner()->remove_child_item(r); + group->add_child_item(r); + + // Put the items at 1-pixel offsets, so that restack will + // stack them in the right order + r->set_layout_v_offset(i); + } +} + } // namespace view } // namespace pv diff --git a/pv/view/header.h b/pv/view/header.h index 909c04ae..b982ad76 100644 --- a/pv/view/header.h +++ b/pv/view/header.h @@ -80,6 +80,8 @@ private: private Q_SLOTS: void on_signals_moved(); + void on_group(); + Q_SIGNALS: void signals_moved();