]> sigrok.org Git - pulseview.git/commitdiff
Header: Moved drag functionality into SelectableItem
authorJoel Holdsworth <redacted>
Wed, 5 Nov 2014 23:04:19 +0000 (23:04 +0000)
committerJoel Holdsworth <redacted>
Wed, 19 Nov 2014 10:23:02 +0000 (10:23 +0000)
pv/view/header.cpp
pv/view/header.h
pv/view/rowitem.cpp
pv/view/rowitem.h
pv/view/selectableitem.cpp
pv/view/selectableitem.h
pv/view/timemarker.cpp
pv/view/timemarker.h

index 9ba4cf8f8814088749cf20b1909dee6cfec50e7e..b9261f9753d060bdaffbb48ab7c52a35d4a5210b 100644 (file)
@@ -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<RowItem> 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<RowItem> 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<RowItem> 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<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);
-
-               if (row_item->owner() != first_row_item->owner())
-                       return;
-       }
+       RowItemOwner *item_owner = nullptr;
+       for (shared_ptr<RowItem> 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<RowItem> row_item((*i).first);
-               if (row_item) {
-                       const int y = (*i).second + delta;
-                       row_item->set_v_offset(y);
+       for (std::shared_ptr<RowItem> 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();
 
index f190d50c919e518d81abdab31219ec2d8ec3d0f2..2b182978a905669d1145d9b06149fc8c4ce8aa4c 100644 (file)
@@ -91,9 +91,6 @@ private:
        QPoint _mouse_point;
        QPoint _mouse_down_point;
        bool _dragging;
-
-       std::list<std::pair<std::weak_ptr<RowItem>, int> >
-               _drag_row_items;
 };
 
 } // namespace view
index c78f9eef2cba0af4f22b4d9dca555801c98f7fc4..d850e321fe4fdcf0572065e268d6393fa3af8982 100644 (file)
@@ -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;
index 161e828917aec9ea8acb98ad8550898fd252c7c8..6b23162f5808a56c8526115fdfe2c8a7b7fb0b5d 100644 (file)
@@ -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.
index 3f4f6da92dbef767611c404c753210888b7247fe..6331575a4016b7afcf529688e8a66b1342cdefff 100644 (file)
@@ -20,6 +20,8 @@
 
 #include "selectableitem.h"
 
+#include <climits>
+
 #include <QApplication>
 #include <QMenu>
 #include <QPalette>
@@ -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;
index a062fa04223627fe9ac8a211c1887b58aba7d86b..d815bc7007507a2bbdb2d58fc675221cd5918cb0 100644 (file)
@@ -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
index a5d280bdd97a18cf1f276cc9843a1f1f4ea6b463..5434203507fb63823e8f78fb760c5ec84b331539 100644 (file)
@@ -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;
index ec34fb5668a1a89a6db2f03bc21753e8a0dfe4ce..63de74c41f150737c4465359feec37775832d4e0 100644 (file)
@@ -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.