]> sigrok.org Git - pulseview.git/blobdiff - pv/view/header.cpp
Header: Renamed get_mouse_over_row_item to get_mouse_over_item
[pulseview.git] / pv / view / header.cpp
index 2ab8ed986568ba4bfeaa36043840b723541ce867..c19d10883af7fed3c77e3ee60d0f497faadb2958 100644 (file)
  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
  */
 
-#include "header.h"
-#include "view.h"
+#include "header.hpp"
+#include "view.hpp"
 
-#include "signal.h"
-#include "../sigsession.h"
+#include "signal.hpp"
+#include "tracegroup.hpp"
 
 #include <cassert>
 #include <algorithm>
 
+#include <boost/iterator/filter_iterator.hpp>
+
 #include <QApplication>
 #include <QMenu>
 #include <QMouseEvent>
 #include <QPainter>
 #include <QRect>
 
-#include <pv/widgets/popup.h>
+#include <pv/session.hpp>
+#include <pv/widgets/popup.hpp>
 
+using boost::make_filter_iterator;
+using std::dynamic_pointer_cast;
 using std::max;
 using std::make_pair;
+using std::min;
 using std::pair;
 using std::shared_ptr;
 using std::stable_sort;
@@ -48,60 +54,57 @@ namespace view {
 const int Header::Padding = 12;
 const int Header::BaselineOffset = 5;
 
-Header::Header(View &parent) :
-       MarginWidget(parent),
-       _dragging(false)
+static bool item_selected(shared_ptr<RowItem> r)
 {
-       setFocusPolicy(Qt::ClickFocus);
-       setMouseTracking(true);
+       return r->selected();
+}
 
-       connect(&_view, SIGNAL(signals_moved()),
+Header::Header(View &parent) :
+       MarginWidget(parent)
+{
+       connect(&view_, SIGNAL(signals_moved()),
                this, SLOT(on_signals_moved()));
 }
 
 QSize Header::sizeHint() const
 {
        QRectF max_rect(-Padding, 0, Padding, 0);
-       for (auto &i : _view)
+       for (auto &i : view_)
                if (i->enabled())
-                       max_rect = max_rect.united(i->label_rect(0));
+                       max_rect = max_rect.united(i->label_rect(QRect()));
        return QSize(max_rect.width() + Padding + BaselineOffset, 0);
 }
 
-shared_ptr<RowItem> Header::get_mouse_over_row_item(const QPoint &pt)
+QSize Header::extended_size_hint() const
+{
+       return sizeHint() + QSize(ViewItem::HighlightRadius, 0);
+}
+
+shared_ptr<RowItem> Header::get_mouse_over_item(const QPoint &pt)
 {
-       const int w = width() - BaselineOffset;
-       for (auto &i : _view)
-               if (i->enabled() && i->label_rect(w).contains(pt))
+       const QRect r(0, 0, width() - BaselineOffset, height());
+       for (auto &i : view_)
+               if (i->enabled() && i->label_rect(r).contains(pt))
                        return i;
        return shared_ptr<RowItem>();
 }
 
 void Header::clear_selection()
 {
-       for (auto &i : _view)
+       for (auto &i : view_)
                i->select(false);
        update();
 }
 
-void Header::signals_updated()
-{
-       for (shared_ptr<RowItem> r : _view) {
-               assert(r);
-               connect(r.get(), SIGNAL(appearance_changed()),
-                       this, SLOT(on_trace_changed()));
-       }
-}
-
 void Header::show_popup(const shared_ptr<RowItem> &item)
 {
        using pv::widgets::Popup;
 
-       Popup *const p = item->create_popup(&_view);
+       Popup *const p = item->create_popup(&view_);
        if (!p)
                return;
 
-       const QPoint pt(width() - BaselineOffset, item->get_y());
+       const QPoint pt(width() - BaselineOffset, item->get_visual_y());
        p->set_position(mapToGlobal(pt), Popup::Right);
        p->show();
 }
@@ -111,14 +114,14 @@ void Header::paintEvent(QPaintEvent*)
        // The trace labels are not drawn with the arrows exactly on the
        // left edge of the widget, because then the selection shadow
        // would be clipped away.
-       const int w = width() - BaselineOffset;
+       const QRect rect(0, 0, width() - BaselineOffset, height());
 
        vector< shared_ptr<RowItem> > row_items(
-               _view.begin(), _view.end());
+               view_.begin(), view_.end());
 
        stable_sort(row_items.begin(), row_items.end(),
                [](const shared_ptr<RowItem> &a, const shared_ptr<RowItem> &b) {
-                       return a->v_offset() < b->v_offset(); });
+                       return a->visual_v_offset() < b->visual_v_offset(); });
 
        QPainter painter(this);
        painter.setRenderHint(QPainter::Antialiasing);
@@ -127,9 +130,9 @@ void Header::paintEvent(QPaintEvent*)
        {
                assert(r);
 
-               const bool highlight = !_dragging &&
-                       r->label_rect(w).contains(_mouse_point);
-               r->paint_label(painter, w, highlight);
+               const bool highlight = !dragging_ &&
+                       r->label_rect(rect).contains(mouse_point_);
+               r->paint_label(painter, rect, highlight);
        }
 
        painter.end();
@@ -143,21 +146,21 @@ void Header::mouseLeftPressEvent(QMouseEvent *event)
                QApplication::keyboardModifiers() & Qt::ControlModifier;
 
        // Clear selection if control is not pressed and this item is unselected
-       if ((!_mouse_down_item || !_mouse_down_item->selected()) &&
+       if ((!mouse_down_item_ || !mouse_down_item_->selected()) &&
                !ctrl_pressed)
-               for (shared_ptr<RowItem> r : _view)
+               for (shared_ptr<RowItem> r : view_)
                        r->select(false);
 
        // Set the signal selection state if the item has been clicked
-       if (_mouse_down_item) {
+       if (mouse_down_item_) {
                if (ctrl_pressed)
-                       _mouse_down_item->select(!_mouse_down_item->selected());
+                       mouse_down_item_->select(!mouse_down_item_->selected());
                else
-                       _mouse_down_item->select(true);
+                       mouse_down_item_->select(true);
        }
 
        // Save the offsets of any signals which will be dragged
-       for (const shared_ptr<RowItem> r : _view)
+       for (const shared_ptr<RowItem> r : view_)
                if (r->selected())
                        r->drag();
 
@@ -169,8 +172,8 @@ void Header::mousePressEvent(QMouseEvent *event)
 {
        assert(event);
 
-       _mouse_down_point = event->pos();
-       _mouse_down_item = get_mouse_over_row_item(event->pos());
+       mouse_down_point_ = event->pos();
+       mouse_down_item_ = get_mouse_over_item(event->pos());
 
        if (event->button() & Qt::LeftButton)
                mouseLeftPressEvent(event);
@@ -185,26 +188,26 @@ void Header::mouseLeftReleaseEvent(QMouseEvent *event)
 
        // Unselect everything if control is not pressed
        const shared_ptr<RowItem> mouse_over =
-               get_mouse_over_row_item(event->pos());
+               get_mouse_over_item(event->pos());
 
-       for (auto &r : _view)
+       for (auto &r : view_)
                r->drag_release();
 
-       if (_dragging)
-               _view.normalize_layout();
+       if (dragging_)
+               view_.restack_all_row_items();
        else
        {
                if (!ctrl_pressed) {
-                       for (shared_ptr<RowItem> r : _view)
-                               if (_mouse_down_item != r)
+                       for (shared_ptr<RowItem> r : view_)
+                               if (mouse_down_item_ != r)
                                        r->select(false);
 
-                       if (_mouse_down_item)
-                               show_popup(_mouse_down_item);
+                       if (mouse_down_item_)
+                               show_popup(mouse_down_item_);
                }
        }
 
-       _dragging = false;
+       dragging_ = false;
 }
 
 void Header::mouseReleaseEvent(QMouseEvent *event)
@@ -213,24 +216,24 @@ void Header::mouseReleaseEvent(QMouseEvent *event)
        if (event->button() & Qt::LeftButton)
                mouseLeftReleaseEvent(event);
 
-       _mouse_down_item = nullptr;
+       mouse_down_item_ = nullptr;
 }
 
 void Header::mouseMoveEvent(QMouseEvent *event)
 {
        assert(event);
-       _mouse_point = event->pos();
+       mouse_point_ = event->pos();
 
        if (!(event->buttons() & Qt::LeftButton))
                return;
 
-       if ((event->pos() - _mouse_down_point).manhattanLength() <
+       if ((event->pos() - mouse_down_point_).manhattanLength() <
                QApplication::startDragDistance())
                return;
 
        // Check all the drag items share a common owner
        RowItemOwner *item_owner = nullptr;
-       for (shared_ptr<RowItem> r : _view)
+       for (shared_ptr<RowItem> r : view_)
                if (r->dragging()) {
                        if (!item_owner)
                                item_owner = r->owner();
@@ -242,18 +245,21 @@ void Header::mouseMoveEvent(QMouseEvent *event)
                return;
 
        // Do the drag
-       _dragging = true;
+       dragging_ = true;
 
-       const int delta = event->pos().y() - _mouse_down_point.y();
+       const int delta = event->pos().y() - mouse_down_point_.y();
 
-       for (std::shared_ptr<RowItem> r : _view)
+       for (std::shared_ptr<RowItem> r : view_)
                if (r->dragging()) {
-                       r->set_v_offset(r->drag_point().y() + delta);
+                       r->force_to_v_offset(r->drag_point().y() + delta);
 
                        // Ensure the trace is selected
                        r->select();
                }
 
+       item_owner->restack_items();
+       for (const auto &r : *item_owner)
+               r->animate_to_layout_v_offset();
        signals_moved();
 
        update();
@@ -261,19 +267,31 @@ void Header::mouseMoveEvent(QMouseEvent *event)
 
 void Header::leaveEvent(QEvent*)
 {
-       _mouse_point = QPoint(-1, -1);
+       mouse_point_ = QPoint(-1, -1);
        update();
 }
 
 void Header::contextMenuEvent(QContextMenuEvent *event)
 {
-       const shared_ptr<RowItem> r = get_mouse_over_row_item(_mouse_point);
+       const shared_ptr<RowItem> r = get_mouse_over_item(mouse_point_);
        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<QKeySequence> 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());
 }
@@ -282,16 +300,16 @@ void Header::keyPressEvent(QKeyEvent *e)
 {
        assert(e);
 
-       switch (e->key())
-       {
-       case Qt::Key_Delete:
+       if (e->key() == Qt::Key_Delete)
        {
-               for (const shared_ptr<RowItem> r : _view)
+               for (const shared_ptr<RowItem> r : view_)
                        if (r->selected())
                                r->delete_pressed();
-               break;
-       }
        }
+       else if (e->key() == Qt::Key_G && e->modifiers() == Qt::ControlModifier)
+               on_group();
+       else if (e->key() == Qt::Key_U && e->modifiers() == Qt::ControlModifier)
+               on_ungroup();
 }
 
 void Header::on_signals_moved()
@@ -299,10 +317,54 @@ void Header::on_signals_moved()
        update();
 }
 
-void Header::on_trace_changed()
+void Header::on_group()
 {
-       update();
-       geometry_updated();
+       vector< shared_ptr<RowItem> > 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<RowItem> &a, const shared_ptr<RowItem> &b) {
+                       return a->visual_v_offset() < b->visual_v_offset(); });
+
+       shared_ptr<TraceGroup> group(new TraceGroup());
+       shared_ptr<RowItem> 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<RowItem> &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);
+       }
+}
+
+void Header::on_ungroup()
+{
+       bool restart;
+       do {
+               restart = false;
+               for (const shared_ptr<RowItem> r : view_) {
+                       const shared_ptr<TraceGroup> tg =
+                               dynamic_pointer_cast<TraceGroup>(r);
+                       if (tg && tg->selected()) {
+                               tg->ungroup();
+                               restart = true;
+                               break;
+                       }
+               }
+       } while(restart);
 }
 
 } // namespace view