From: Joel Holdsworth Date: Mon, 29 Dec 2014 10:15:26 +0000 (+0000) Subject: Viewport: Implemented on-canvas drag X-Git-Tag: pulseview-0.3.0~296 X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=282905348863fe871a48c680bd12bc1dee364a43;p=pulseview.git Viewport: Implemented on-canvas drag --- diff --git a/pv/view/viewport.cpp b/pv/view/viewport.cpp index 0e89f3ad..2772c776 100644 --- a/pv/view/viewport.cpp +++ b/pv/view/viewport.cpp @@ -21,6 +21,7 @@ #include #include #include +#include #include "signal.hpp" #include "view.hpp" @@ -37,6 +38,7 @@ using std::copy; using std::max; using std::min; using std::none_of; +using std::numeric_limits; using std::shared_ptr; using std::stable_sort; using std::vector; @@ -46,7 +48,7 @@ namespace view { Viewport::Viewport(View &parent) : ViewWidget(parent), - mouse_down_valid_(false), + drag_offset_(numeric_limits::signaling_NaN()), pinch_zoom_active_(false) { setAutoFillBackground(true); @@ -63,6 +65,25 @@ shared_ptr Viewport::get_mouse_over_item(const QPoint &pt) return nullptr; } +void Viewport::drag() +{ + drag_offset_ = view_.offset(); +} + +void Viewport::drag_by(const QPoint &delta) +{ + if (isnan(drag_offset_)) + return; + + view_.set_scale_offset(view_.scale(), drag_offset_ - + delta.x() * view_.scale()); +} + +void Viewport::drag_release() +{ + drag_offset_ = numeric_limits::signaling_NaN(); +} + vector< shared_ptr > Viewport::items() { vector< shared_ptr > items(view_.begin(), view_.end()); @@ -104,13 +125,12 @@ bool Viewport::touch_event(QTouchEvent *event) if (touchPoint0.state() & Qt::TouchPointReleased) { // Primary touch released - mouse_down_valid_ = false; + drag_release(); } else { // Update the mouse down fields so that continued // dragging with the primary touch will work correctly mouse_down_point_ = touchPoint0.pos().toPoint(); - mouse_down_offset_ = view_.offset(); - mouse_down_valid_ = true; + drag(); } } @@ -154,43 +174,6 @@ void Viewport::paintEvent(QPaintEvent*) p.end(); } -void Viewport::mousePressEvent(QMouseEvent *event) -{ - assert(event); - - if (event->button() == Qt::LeftButton) { - mouse_down_point_ = event->pos(); - mouse_down_offset_ = view_.offset(); - mouse_down_valid_ = true; - } -} - -void Viewport::mouseReleaseEvent(QMouseEvent *event) -{ - assert(event); - - if (event->button() == Qt::LeftButton) - mouse_down_valid_ = false; -} - -void Viewport::mouseMoveEvent(QMouseEvent *event) -{ - assert(event); - - if (event->buttons() & Qt::LeftButton) { - if (!mouse_down_valid_) { - mouse_down_point_ = event->pos(); - mouse_down_offset_ = view_.offset(); - mouse_down_valid_ = true; - } - - view_.set_scale_offset(view_.scale(), - mouse_down_offset_ + - (mouse_down_point_ - event->pos()).x() * - view_.scale()); - } -} - void Viewport::mouseDoubleClickEvent(QMouseEvent *event) { assert(event); diff --git a/pv/view/viewport.hpp b/pv/view/viewport.hpp index 43905337..f43177d2 100644 --- a/pv/view/viewport.hpp +++ b/pv/view/viewport.hpp @@ -52,6 +52,22 @@ private: std::shared_ptr get_mouse_over_item( const QPoint &pt); + /** + * Sets this item into the dragged state. + */ + void drag(); + + /** + * Drag the background by the delta offset. + * @param delta the drag offset in pixels. + */ + void drag_by(const QPoint &delta); + + /** + * Sets this item into the un-dragged state. + */ + void drag_release(); + /** * Gets the items in the view widget. */ @@ -66,15 +82,11 @@ private: private: void paintEvent(QPaintEvent *event); - void mousePressEvent(QMouseEvent *event); - void mouseReleaseEvent(QMouseEvent *event); - void mouseMoveEvent(QMouseEvent *event); void mouseDoubleClickEvent(QMouseEvent * event); void wheelEvent(QWheelEvent *event); + private: - QPoint mouse_down_point_; - double mouse_down_offset_; - bool mouse_down_valid_; + double drag_offset_; double pinch_offset0_; double pinch_offset1_; diff --git a/pv/view/viewwidget.cpp b/pv/view/viewwidget.cpp index 816fbf51..a5174bb3 100644 --- a/pv/view/viewwidget.cpp +++ b/pv/view/viewwidget.cpp @@ -85,7 +85,8 @@ bool ViewWidget::accept_drag() const return true; } - return false; + // A background drag is beginning + return true; } bool ViewWidget::mouse_down() const @@ -96,6 +97,8 @@ bool ViewWidget::mouse_down() const void ViewWidget::drag_items(const QPoint &delta) { + bool item_dragged = false; + // Drag the row items RowItemOwner *item_owner = nullptr; for (std::shared_ptr r : view_) @@ -108,6 +111,7 @@ void ViewWidget::drag_items(const QPoint &delta) } if (item_owner) { + item_dragged = true; item_owner->restack_items(); for (const auto &r : *item_owner) r->animate_to_layout_v_offset(); @@ -116,8 +120,27 @@ void ViewWidget::drag_items(const QPoint &delta) // Drag the time items const vector< shared_ptr > items(view_.time_items()); for (auto &i : items) - if (i->dragging()) + if (i->dragging()) { i->drag_by(delta); + item_dragged = true; + } + + // Do the background drag + if (!item_dragged) + drag_by(delta); +} + +void ViewWidget::drag() +{ +} + +void ViewWidget::drag_by(const QPoint &delta) +{ + (void)delta; +} + +void ViewWidget::drag_release() +{ } void ViewWidget::mouse_left_press_event(QMouseEvent *event) @@ -141,10 +164,17 @@ void ViewWidget::mouse_left_press_event(QMouseEvent *event) } // Save the offsets of any signals which will be dragged + bool item_dragged = false; const auto items = this->items(); for (auto &i : items) - if (i->selected()) + if (i->selected()) { + item_dragged = true; i->drag(); + } + + // Do the background drag + if (!item_dragged) + drag(); selection_changed(); update(); diff --git a/pv/view/viewwidget.hpp b/pv/view/viewwidget.hpp index b6b1a5b4..6fd1ecef 100644 --- a/pv/view/viewwidget.hpp +++ b/pv/view/viewwidget.hpp @@ -65,6 +65,23 @@ protected: */ void drag_items(const QPoint &delta); + /** + * Sets this item into the dragged state. + */ + virtual void drag(); + + /** + * Drag the background by the delta offset. + * @param delta the drag offset in pixels. + * @remarks The default implementation does nothing. + */ + virtual void drag_by(const QPoint &delta); + + /** + * Sets this item into the un-dragged state. + */ + virtual void drag_release(); + /** * Gets the items in the view widget. */