From: Joel Holdsworth Date: Wed, 5 Nov 2014 23:04:19 +0000 (+0000) Subject: Header: Moved drag functionality into SelectableItem X-Git-Tag: pulseview-0.3.0~458 X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=0dda6fe595932b2e340930104fad8ac4fc574895;p=pulseview.git Header: Moved drag functionality into SelectableItem --- diff --git a/pv/view/header.cpp b/pv/view/header.cpp index 9ba4cf8f..b9261f97 100644 --- a/pv/view/header.cpp +++ b/pv/view/header.cpp @@ -123,12 +123,11 @@ void Header::paintEvent(QPaintEvent*) QPainter painter(this); painter.setRenderHint(QPainter::Antialiasing); - const bool dragging = !_drag_row_items.empty(); for (const shared_ptr r : row_items) { assert(r); - const bool highlight = !dragging && + const bool highlight = !_dragging && r->label_rect(w).contains(_mouse_point); r->paint_label(painter, w, highlight); } @@ -160,8 +159,7 @@ void Header::mouseLeftPressEvent(QMouseEvent *event) _mouse_down_point = event->pos(); for (const shared_ptr r : _view) if (r->selected()) - _drag_row_items.push_back( - make_pair(r, r->v_offset())); + r->drag(); selection_changed(); update(); @@ -185,6 +183,9 @@ void Header::mouseLeftReleaseEvent(QMouseEvent *event) const shared_ptr mouse_over = get_mouse_over_row_item(event->pos()); + for (auto &r : _view) + r->drag_release(); + if (_dragging) _view.normalize_layout(); else @@ -200,7 +201,6 @@ void Header::mouseLeftReleaseEvent(QMouseEvent *event) } _dragging = false; - _drag_row_items.clear(); } void Header::mouseReleaseEvent(QMouseEvent *event) @@ -222,37 +222,31 @@ void Header::mouseMoveEvent(QMouseEvent *event) QApplication::startDragDistance()) return; - // 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 first_row_item( - _drag_row_items.front().first); - for (const auto &r : _drag_row_items) { - const shared_ptr row_item(r.first); - assert(row_item); - - if (row_item->owner() != first_row_item->owner()) - return; - } + RowItemOwner *item_owner = nullptr; + for (shared_ptr r : _view) + if (r->dragging()) { + if (!item_owner) + item_owner = r->owner(); + else if(item_owner != r->owner()) + return; + } + + if (!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 row_item((*i).first); - if (row_item) { - const int y = (*i).second + delta; - row_item->set_v_offset(y); + for (std::shared_ptr r : _view) + if (r->dragging()) { + r->set_v_offset(r->drag_point().y() + delta); // Ensure the trace is selected - row_item->select(); + r->select(); } - } signals_moved(); diff --git a/pv/view/header.h b/pv/view/header.h index f190d50c..2b182978 100644 --- a/pv/view/header.h +++ b/pv/view/header.h @@ -91,9 +91,6 @@ private: QPoint _mouse_point; QPoint _mouse_down_point; bool _dragging; - - std::list, int> > - _drag_row_items; }; } // namespace view diff --git a/pv/view/rowitem.cpp b/pv/view/rowitem.cpp index c78f9eef..d850e321 100644 --- a/pv/view/rowitem.cpp +++ b/pv/view/rowitem.cpp @@ -60,6 +60,11 @@ int RowItem::get_y() const return _v_offset + _owner->owner_v_offset(); } +QPoint RowItem::point() const +{ + return QPoint(0, v_offset()); +} + void RowItem::paint_back(QPainter &p, int left, int right) { (void)p; diff --git a/pv/view/rowitem.h b/pv/view/rowitem.h index 161e8289..6b23162f 100644 --- a/pv/view/rowitem.h +++ b/pv/view/rowitem.h @@ -69,6 +69,11 @@ public: */ int get_y() const; + /** + * Gets the drag point of the row item. + */ + QPoint point() const; + /** * Paints the background layer of the trace with a QPainter * @param p the QPainter to paint into. diff --git a/pv/view/selectableitem.cpp b/pv/view/selectableitem.cpp index 3f4f6da9..6331575a 100644 --- a/pv/view/selectableitem.cpp +++ b/pv/view/selectableitem.cpp @@ -20,6 +20,8 @@ #include "selectableitem.h" +#include + #include #include #include @@ -31,7 +33,8 @@ const int SelectableItem::HighlightRadius = 6; SelectableItem::SelectableItem() : _context_parent(NULL), - _selected(false) + _selected(false), + _drag_point(INT_MIN, INT_MIN) { } @@ -45,6 +48,26 @@ void SelectableItem::select(bool select) _selected = select; } +bool SelectableItem::dragging() const +{ + return _drag_point.x() != INT_MIN && _drag_point.y() != INT_MIN; +} + +QPoint SelectableItem::drag_point() const +{ + return _drag_point; +} + +void SelectableItem::drag() +{ + _drag_point = point(); +} + +void SelectableItem::drag_release() +{ + _drag_point = QPoint(INT_MIN, INT_MIN); +} + QMenu* SelectableItem::create_context_menu(QWidget *parent) { _context_parent = parent; diff --git a/pv/view/selectableitem.h b/pv/view/selectableitem.h index a062fa04..d815bc70 100644 --- a/pv/view/selectableitem.h +++ b/pv/view/selectableitem.h @@ -58,6 +58,31 @@ public: */ void select(bool select = true); + /** + * Returns true if the item is being dragged. + */ + bool dragging() const; + + /** + * Retunrns the current drag point. + */ + QPoint drag_point() const; + + /** + * Sets this item into the dragged state. + */ + void drag(); + + /** + * Sets this item into the un-dragged state. + */ + void drag_release(); + + /** + * Get the drag point. + */ + virtual QPoint point() const = 0; + public: virtual QMenu* create_context_menu(QWidget *parent); @@ -73,6 +98,7 @@ protected: private: bool _selected; + QPoint _drag_point; }; } // namespace view diff --git a/pv/view/timemarker.cpp b/pv/view/timemarker.cpp index a5d280bd..54342035 100644 --- a/pv/view/timemarker.cpp +++ b/pv/view/timemarker.cpp @@ -50,6 +50,11 @@ float TimeMarker::get_x() const return (_time - _view.offset()) / _view.scale(); } +QPoint TimeMarker::point() const +{ + return QPoint(get_x(), 0); +} + void TimeMarker::set_time(double time) { _time = time; diff --git a/pv/view/timemarker.h b/pv/view/timemarker.h index ec34fb56..63de74c4 100644 --- a/pv/view/timemarker.h +++ b/pv/view/timemarker.h @@ -63,6 +63,11 @@ public: float get_x() const; + /** + * Gets the drag point of the row item. + */ + QPoint point() const; + /** * Paints the marker to the viewport. * @param p The painter to draw with.