]> sigrok.org Git - pulseview.git/commitdiff
ViewWidget: Moved in mouse handlers
authorJoel Holdsworth <redacted>
Fri, 26 Dec 2014 23:42:53 +0000 (23:42 +0000)
committerJoel Holdsworth <redacted>
Mon, 29 Dec 2014 12:24:23 +0000 (12:24 +0000)
pv/view/marginwidget.cpp
pv/view/marginwidget.hpp
pv/view/viewport.cpp
pv/view/viewport.hpp
pv/view/viewwidget.cpp
pv/view/viewwidget.hpp

index fcbb31bf0174bb1944286005b11d0d978d715126..2d663cdc18b17a18740dfc7126b11da13f99e579 100644 (file)
@@ -18,7 +18,6 @@
  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
  */
 
-#include <QApplication>
 #include <QMenu>
 #include <QMouseEvent>
 
@@ -34,12 +33,15 @@ namespace pv {
 namespace view {
 
 MarginWidget::MarginWidget(View &parent) :
-       ViewWidget(parent),
-       dragging_(false)
+       ViewWidget(parent)
 {
        setAttribute(Qt::WA_NoSystemBackground, true);
-       setFocusPolicy(Qt::ClickFocus);
-       setMouseTracking(true);
+}
+
+void MarginWidget::item_clicked(const shared_ptr<ViewItem> &item)
+{
+       if (item && item->enabled())
+               show_popup(item);
 }
 
 void MarginWidget::show_popup(const shared_ptr<ViewItem> &item)
@@ -49,116 +51,6 @@ void MarginWidget::show_popup(const shared_ptr<ViewItem> &item)
                p->show();
 }
 
-void MarginWidget::mouse_left_press_event(QMouseEvent *event)
-{
-       (void)event;
-
-       const bool ctrl_pressed =
-               QApplication::keyboardModifiers() & Qt::ControlModifier;
-
-       // Clear selection if control is not pressed and this item is unselected
-       if ((!mouse_down_item_ || !mouse_down_item_->selected()) &&
-               !ctrl_pressed)
-               clear_selection();
-
-       // Set the signal selection state if the item has been clicked
-       if (mouse_down_item_) {
-               if (ctrl_pressed)
-                       mouse_down_item_->select(!mouse_down_item_->selected());
-               else
-                       mouse_down_item_->select(true);
-       }
-
-       // Save the offsets of any signals which will be dragged
-       const auto items = this->items();
-       for (auto &i : items)
-               if (i->selected())
-                       i->drag();
-
-       selection_changed();
-       update();
-}
-
-void MarginWidget::mouse_left_release_event(QMouseEvent *event)
-{
-       assert(event);
-
-       auto items = this->items();
-       const bool ctrl_pressed =
-               QApplication::keyboardModifiers() & Qt::ControlModifier;
-
-       // Unselect everything if control is not pressed
-       const shared_ptr<ViewItem> mouse_over =
-               get_mouse_over_item(event->pos());
-
-       for (auto &i : items)
-               i->drag_release();
-
-       if (dragging_)
-               view_.restack_all_row_items();
-       else
-       {
-               if (!ctrl_pressed) {
-                       for (shared_ptr<ViewItem> i : items)
-                               if (mouse_down_item_ != i)
-                                       i->select(false);
-
-                       if (mouse_down_item_)
-                               show_popup(mouse_down_item_);
-               }
-       }
-
-       dragging_ = false;
-}
-
-void MarginWidget::mousePressEvent(QMouseEvent *event)
-{
-       assert(event);
-
-       mouse_down_point_ = event->pos();
-       mouse_down_item_ = get_mouse_over_item(event->pos());
-
-       if (event->button() & Qt::LeftButton)
-               mouse_left_press_event(event);
-}
-
-void MarginWidget::mouseReleaseEvent(QMouseEvent *event)
-{
-       assert(event);
-       if (event->button() & Qt::LeftButton)
-               mouse_left_release_event(event);
-
-       mouse_down_item_ = nullptr;
-}
-
-void MarginWidget::mouseMoveEvent(QMouseEvent *event)
-{
-       assert(event);
-       mouse_point_ = event->pos();
-
-       if (!(event->buttons() & Qt::LeftButton))
-               return;
-
-       if ((event->pos() - mouse_down_point_).manhattanLength() <
-               QApplication::startDragDistance())
-               return;
-
-       if (!accept_drag())
-               return;
-
-       // Do the drag
-       dragging_ = true;
-       drag_items(event->pos() - mouse_down_point_);
-
-       update();
-}
-
-void MarginWidget::leaveEvent(QEvent*)
-{
-       mouse_point_ = QPoint(-1, -1);
-       update();
-}
-
 void MarginWidget::contextMenuEvent(QContextMenuEvent *event)
 {
        const shared_ptr<ViewItem> r = get_mouse_over_item(mouse_point_);
@@ -183,13 +75,5 @@ void MarginWidget::keyPressEvent(QKeyEvent *e)
        }
 }
 
-void MarginWidget::clear_selection()
-{
-       const auto items = this->items();
-       for (auto &i : items)
-               i->select(false);
-       update();
-}
-
 } // namespace view
 } // namespace pv
index 029d68e5214970d700c4e3e0240b8c79935044ba..0a24ad2ece705bde08e7c4be1af1421e680e9b7d 100644 (file)
@@ -48,18 +48,11 @@ public:
 
 protected:
        /**
-        * Gets the items in the margin widget.
+        * Indicates the event an a view item has been clicked.
+        * @param item the view item that has been clicked.
         */
-       virtual std::vector< std::shared_ptr<pv::view::ViewItem> > items() = 0;
-
-       /**
-        * Gets the first view item which has a label that contains @c pt .
-        * @param pt the point to search with.
-        * @return the view item that has been found, or and empty
-        *   @c shared_ptr if no item was found.
-        */
-       virtual std::shared_ptr<pv::view::ViewItem> get_mouse_over_item(
-               const QPoint &pt) = 0;
+       virtual void item_clicked(
+               const std::shared_ptr<pv::view::ViewItem> &item);
 
        /**
         * Shows the popup of a the specified @c ViewItem .
@@ -67,40 +60,10 @@ protected:
         */
        void show_popup(const std::shared_ptr<ViewItem> &item);
 
-       /**
-        * Handles left mouse button press events.
-        * @param event the mouse event that triggered this handler.
-        */
-       void mouse_left_press_event(QMouseEvent *event);
-
-       /**
-        * Handles left mouse button release events.
-        * @param event the mouse event that triggered this handler.
-        */
-       void mouse_left_release_event(QMouseEvent *event);
-
 protected:
-       void mousePressEvent(QMouseEvent * event);
-       void mouseReleaseEvent(QMouseEvent *event);
-       void mouseMoveEvent(QMouseEvent *event);
-
-       void leaveEvent(QEvent *event);
-
        virtual void contextMenuEvent(QContextMenuEvent *event);
 
        virtual void keyPressEvent(QKeyEvent *e);
-
-public Q_SLOTS:
-       void clear_selection();
-
-Q_SIGNALS:
-       void selection_changed();
-
-protected:
-       QPoint mouse_point_;
-       QPoint mouse_down_point_;
-       std::shared_ptr<ViewItem> mouse_down_item_;
-       bool dragging_;
 };
 
 } // namespace view
index c33af44b6ccc423f3f8dc13d579e0268e7aad3ee..83df7aa4d1eaaa9543add3896e2b6363457ba200 100644 (file)
@@ -32,6 +32,8 @@
 #include <QMouseEvent>
 
 using std::abs;
+using std::back_inserter;
+using std::copy;
 using std::max;
 using std::min;
 using std::none_of;
@@ -53,6 +55,24 @@ Viewport::Viewport(View &parent) :
        setBackgroundRole(QPalette::Base);
 }
 
+shared_ptr<ViewItem> Viewport::get_mouse_over_item(const QPoint &pt)
+{
+       const vector< shared_ptr<ViewItem> > items(this->items());
+       for (auto i = items.rbegin(); i != items.rend(); i++)
+               if ((*i)->enabled() &&
+                       (*i)->hit_box_rect(rect()).contains(pt))
+                       return *i;
+       return nullptr;
+}
+
+vector< shared_ptr<ViewItem> > Viewport::items()
+{
+       vector< shared_ptr<ViewItem> > items(view_.begin(), view_.end());
+       const vector< shared_ptr<TimeItem> > time_items(view_.time_items());
+       copy(time_items.begin(), time_items.end(), back_inserter(items));
+       return items;
+}
+
 void Viewport::paintEvent(QPaintEvent*)
 {
        vector< shared_ptr<RowItem> > row_items(view_.begin(), view_.end());
index 3cc59c9479d5e5a87abb48f078bfbface8d35aa4..be3e7d32b719a7d80ca87df69216fbde735b963f 100644 (file)
@@ -42,10 +42,23 @@ class Viewport : public ViewWidget
 public:
        explicit Viewport(View &parent);
 
-protected:
+private:
+       /**
+        * Gets the first view item which has a hit-box that contains @c pt .
+        * @param pt the point to search with.
+        * @return the view item that has been found, or and empty
+        *   @c shared_ptr if no item was found.
+        */
+       std::shared_ptr<pv::view::ViewItem> get_mouse_over_item(
+               const QPoint &pt);
+
+       /**
+        * Gets the items in the view widget.
+        */
+       std::vector< std::shared_ptr<pv::view::ViewItem> > items();
+
        void paintEvent(QPaintEvent *event);
 
-private:
        bool event(QEvent *event);
        void mousePressEvent(QMouseEvent *event);
        void mouseReleaseEvent(QMouseEvent *event);
index 9c4c56ff6f0e50d20f1ec3839ee9b4b8ebbc6f30..0b3b2fd1e233f962bc9f6b50417109e72582722b 100644 (file)
  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
  */
 
-#include <algorithm>
+#include <QApplication>
+#include <QMouseEvent>
 
 #include "rowitem.hpp"
-#include "timeitem.hpp"
 #include "view.hpp"
 #include "viewwidget.hpp"
 
@@ -34,8 +34,24 @@ namespace view {
 
 ViewWidget::ViewWidget(View &parent) :
        QWidget(&parent),
-       view_(parent)
+       view_(parent),
+       dragging_(false)
 {
+       setFocusPolicy(Qt::ClickFocus);
+       setMouseTracking(true);
+}
+
+void ViewWidget::clear_selection()
+{
+       const auto items = this->items();
+       for (auto &i : items)
+               i->select(false);
+       update();
+}
+
+void ViewWidget::item_clicked(const shared_ptr<ViewItem> &item)
+{
+       (void)item;
 }
 
 bool ViewWidget::accept_drag() const
@@ -96,5 +112,115 @@ void ViewWidget::drag_items(const QPoint &delta)
                        i->drag_by(delta);
 }
 
+void ViewWidget::mouse_left_press_event(QMouseEvent *event)
+{
+       (void)event;
+
+       const bool ctrl_pressed =
+               QApplication::keyboardModifiers() & Qt::ControlModifier;
+
+       // Clear selection if control is not pressed and this item is unselected
+       if ((!mouse_down_item_ || !mouse_down_item_->selected()) &&
+               !ctrl_pressed)
+               clear_selection();
+
+       // Set the signal selection state if the item has been clicked
+       if (mouse_down_item_) {
+               if (ctrl_pressed)
+                       mouse_down_item_->select(!mouse_down_item_->selected());
+               else
+                       mouse_down_item_->select(true);
+       }
+
+       // Save the offsets of any signals which will be dragged
+       const auto items = this->items();
+       for (auto &i : items)
+               if (i->selected())
+                       i->drag();
+
+       selection_changed();
+       update();
+}
+
+void ViewWidget::mouse_left_release_event(QMouseEvent *event)
+{
+       assert(event);
+
+       auto items = this->items();
+       const bool ctrl_pressed =
+               QApplication::keyboardModifiers() & Qt::ControlModifier;
+
+       // Unselect everything if control is not pressed
+       const shared_ptr<ViewItem> mouse_over =
+               get_mouse_over_item(event->pos());
+
+       for (auto &i : items)
+               i->drag_release();
+
+       if (dragging_)
+               view_.restack_all_row_items();
+       else
+       {
+               if (!ctrl_pressed) {
+                       for (shared_ptr<ViewItem> i : items)
+                               if (mouse_down_item_ != i)
+                                       i->select(false);
+
+                       if (mouse_down_item_)
+                               item_clicked(mouse_down_item_);
+               }
+       }
+
+       dragging_ = false;
+}
+
+void ViewWidget::mousePressEvent(QMouseEvent *event)
+{
+       assert(event);
+
+       mouse_down_point_ = event->pos();
+       mouse_down_item_ = get_mouse_over_item(event->pos());
+
+       if (event->button() & Qt::LeftButton)
+               mouse_left_press_event(event);
+}
+
+void ViewWidget::mouseReleaseEvent(QMouseEvent *event)
+{
+       assert(event);
+       if (event->button() & Qt::LeftButton)
+               mouse_left_release_event(event);
+
+       mouse_down_item_ = nullptr;
+}
+
+void ViewWidget::mouseMoveEvent(QMouseEvent *event)
+{
+       assert(event);
+       mouse_point_ = event->pos();
+
+       if (!(event->buttons() & Qt::LeftButton))
+               return;
+
+       if ((event->pos() - mouse_down_point_).manhattanLength() <
+               QApplication::startDragDistance())
+               return;
+
+       if (!accept_drag())
+               return;
+
+       // Do the drag
+       dragging_ = true;
+       drag_items(event->pos() - mouse_down_point_);
+
+       update();
+}
+
+void ViewWidget::leaveEvent(QEvent*)
+{
+       mouse_point_ = QPoint(-1, -1);
+       update();
+}
+
 } // namespace view
 } // namespace pv
index 3b22441a9e0b16f22df4cb0ffdaa7a30dd131230..fc7247627a8783f1a55bf81dc31526da151c8032 100644 (file)
 #ifndef PULSEVIEW_PV_VIEWWIDGET_H
 #define PULSEVIEW_PV_VIEWWIDGET_H
 
+#include <memory>
+
 #include <QWidget>
 
 namespace pv {
 namespace view {
 
 class View;
+class ViewItem;
 
 class ViewWidget : public QWidget
 {
@@ -35,6 +38,14 @@ class ViewWidget : public QWidget
 protected:
        ViewWidget(View &parent);
 
+       /**
+        * Indicates the event an a view item has been clicked.
+        * @param item the view item that has been clicked.
+        * @remarks the default implementation does nothing.
+        */
+       virtual void item_clicked(
+               const std::shared_ptr<pv::view::ViewItem> &item);
+
        /**
         * Returns true if the selection of row items allows dragging.
         * @return Returns true if the drag is acceptable.
@@ -47,8 +58,51 @@ protected:
         */
        void drag_items(const QPoint &delta);
 
+       /**
+        * Gets the items in the view widget.
+        */
+       virtual std::vector< std::shared_ptr<pv::view::ViewItem> > items() = 0;
+
+       /**
+        * Gets the first view item which has a hit-box that contains @c pt .
+        * @param pt the point to search with.
+        * @return the view item that has been found, or and empty
+        *   @c shared_ptr if no item was found.
+        */
+       virtual std::shared_ptr<pv::view::ViewItem> get_mouse_over_item(
+               const QPoint &pt) = 0;
+
+       /**
+        * Handles left mouse button press events.
+        * @param event the mouse event that triggered this handler.
+        */
+       void mouse_left_press_event(QMouseEvent *event);
+
+       /**
+        * Handles left mouse button release events.
+        * @param event the mouse event that triggered this handler.
+        */
+       void mouse_left_release_event(QMouseEvent *event);
+
+protected:
+       void mousePressEvent(QMouseEvent * event);
+       void mouseReleaseEvent(QMouseEvent *event);
+       void mouseMoveEvent(QMouseEvent *event);
+
+       void leaveEvent(QEvent *event);
+
+public Q_SLOTS:
+       void clear_selection();
+
+Q_SIGNALS:
+       void selection_changed();
+
 protected:
        pv::view::View &view_;
+       QPoint mouse_point_;
+       QPoint mouse_down_point_;
+       std::shared_ptr<ViewItem> mouse_down_item_;
+       bool dragging_;
 };
 
 } // namespace view