Header: Only allow dragging if all traces share a common ancestor
[pulseview.git] / pv / view / header.cpp
index 50eefc43cd765b1d0a300fb7fb622549b94044c8..202ae101f76f6ada1fd9d44573cd89b06bafab2b 100644 (file)
@@ -55,22 +55,17 @@ Header::Header(View &parent) :
        setFocusPolicy(Qt::ClickFocus);
        setMouseTracking(true);
 
-       connect(&_view.session(), SIGNAL(signals_changed()),
-               this, SLOT(on_signals_changed()));
-
        connect(&_view, SIGNAL(signals_moved()),
                this, SLOT(on_signals_moved()));
 }
 
 QSize Header::sizeHint() const
 {
-       int max_width = 0;
-
+       QRectF max_rect(-Padding, 0, Padding, 0);
        for (auto &i : _view)
                if (i->enabled())
-                       max_width = max(max_width, (int)i->label_rect(0).width());
-
-       return QSize(max_width + Padding + BaselineOffset, 0);
+                       max_rect = max_rect.united(i->label_rect(0));
+       return QSize(max_rect.width() + Padding + BaselineOffset, 0);
 }
 
 shared_ptr<RowItem> Header::get_mouse_over_row_item(const QPoint &pt)
@@ -89,6 +84,15 @@ void Header::clear_selection()
        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;
@@ -210,33 +214,40 @@ void Header::mouseMoveEvent(QMouseEvent *event)
                QApplication::startDragDistance())
                return;
 
-       // Move the signals if we are dragging
-       if (!_drag_row_items.empty())
-       {
-               _dragging = true;
-
-               const int delta = event->pos().y() - _mouse_down_point.y();
-
-               for (auto i = _drag_row_items.begin();
-                       i != _drag_row_items.end(); i++) {
-                       const std::shared_ptr<RowItem> row_item((*i).first);
-                       if (row_item) {
-                               const int y = (*i).second + delta;
-                               const int y_snap =
-                                       ((y + View::SignalSnapGridSize / 2) /
-                                               View::SignalSnapGridSize) *
-                                               View::SignalSnapGridSize;
-                               row_item->set_v_offset(y_snap);
-
-                               // Ensure the trace is selected
-                               row_item->select();
-                       }
-                       
-               }
+       // Check the list of dragging items is not empty
+       if (_drag_row_items.empty())
+               return;
+
+       // Check all the drag items share a common owner
+       const shared_ptr<RowItem> first_row_item(
+               _drag_row_items.front().first);
+       for (const auto &r : _drag_row_items) {
+               const shared_ptr<RowItem> row_item(r.first);
+               assert(row_item);
 
-               signals_moved();
+               if (row_item->owner() != first_row_item->owner())
+                       return;
        }
 
+       // Do the drag
+       _dragging = true;
+
+       const int delta = event->pos().y() - _mouse_down_point.y();
+
+       for (auto i = _drag_row_items.begin();
+               i != _drag_row_items.end(); i++) {
+               const std::shared_ptr<RowItem> row_item((*i).first);
+               if (row_item) {
+                       const int y = (*i).second + delta;
+                       row_item->set_v_offset(y);
+
+                       // Ensure the trace is selected
+                       row_item->select();
+               }
+       }
+
+       signals_moved();
+
        update();
 }
 
@@ -275,19 +286,6 @@ void Header::keyPressEvent(QKeyEvent *e)
        }
 }
 
-void Header::on_signals_changed()
-{
-       for (shared_ptr<RowItem> r : _view) {
-               assert(r);
-               connect(r.get(), SIGNAL(visibility_changed()),
-                       this, SLOT(on_trace_changed()));
-               connect(r.get(), SIGNAL(text_changed()),
-                       this, SLOT(on_trace_changed()));
-               connect(r.get(), SIGNAL(colour_changed()),
-                       this, SLOT(update()));
-       }
-}
-
 void Header::on_signals_moved()
 {
        update();