]> sigrok.org Git - pulseview.git/commitdiff
Fix #1292/1294 by snapping to any edge when not hovering over a signal
authorSoeren Apel <redacted>
Wed, 3 Oct 2018 19:52:00 +0000 (21:52 +0200)
committerSoeren Apel <redacted>
Wed, 3 Oct 2018 20:05:53 +0000 (22:05 +0200)
1292: TimeItem instances now snap to any edge when not hovering over a
      signal, including the ruler itself. Hence, there is always an area
      where one can hover without selecting a signal, even if the entire
      view is full of signals.
      Cursors also show the desired behavior: the left edge snaps while
      the right does not. Hence, the width is kept constant. If the user
      wants to snap the right edge to a signal edge, this can be achieved
      by dragging the right edge separately.

1294: TimeItem instances now snap to any edge - before, they were snapping
      only to edges when hovering over a signal.

pv/views/trace/view.cpp
pv/views/trace/view.hpp

index b8bbe35798825b1c1ff129fd5bae798f55219ddc..d2d61ceda5b08b186a0820815b5f73d53bcfdc38 100644 (file)
@@ -79,6 +79,7 @@ using std::max;
 using std::make_pair;
 using std::make_shared;
 using std::min;
+using std::numeric_limits;
 using std::pair;
 using std::set;
 using std::set_difference;
@@ -859,44 +860,95 @@ const QPoint& View::hover_point() const
        return hover_point_;
 }
 
-int64_t View::get_nearest_level_change(const QPoint &p) const
+int64_t View::get_nearest_level_change(const QPoint &p)
 {
        if (snap_distance_ == 0)
                return -1;
 
        shared_ptr<Signal> signal = signal_under_mouse_cursor_;
 
-       if (!signal)
-               return -1;
+       vector<data::LogicSegment::EdgePair> nearest_edges;
+       int64_t nearest_sample = -1;
 
-       // Calculate sample number from cursor position
-       const double samples_per_pixel = signal->base()->get_samplerate() * scale();
-       const int64_t x_offset = offset().convert_to<double>() / scale();
-       const int64_t sample_num = max(((x_offset + p.x()) * samples_per_pixel), 0.0);
+       if (signal) {
+               // Determine nearest edge from specific signal
 
-       // Query for nearest level changes
-       vector<data::LogicSegment::EdgePair> edges =
-               signal->get_nearest_level_changes(sample_num);
+               // Calculate sample number from cursor position
+               const double samples_per_pixel = signal->base()->get_samplerate() * scale();
+               const int64_t x_offset = offset().convert_to<double>() / scale();
+               const int64_t sample_num = max(((x_offset + p.x()) * samples_per_pixel), 0.0);
 
-       if (edges.size() != 2)
-               return -1;
+               nearest_edges = signal->get_nearest_level_changes(sample_num);
+
+               if (nearest_edges.size() != 2)
+                       return -1;
+
+               // We received absolute sample numbers, make them relative
+               const int64_t left_sample_delta = sample_num - nearest_edges.front().first;
+               const int64_t right_sample_delta = nearest_edges.back().first - sample_num - 1;
+
+               const int64_t left_delta = left_sample_delta / samples_per_pixel;
+               const int64_t right_delta = right_sample_delta / samples_per_pixel;
+
+               // Only use closest left or right edge if they're close to the cursor
+               if ((left_delta < right_delta) && (left_delta < snap_distance_))
+                       nearest_sample = nearest_edges.front().first;
+               if ((left_delta >= right_delta) && (right_delta < snap_distance_))
+                       nearest_sample = nearest_edges.back().first;
+       } else {
+               // Determine nearest edge from all signals
+
+               int64_t nearest_left_delta = numeric_limits<int64_t>::max();
+               int64_t nearest_right_delta = numeric_limits<int64_t>::max();
+               bool edges_found = false;
+
+               for (shared_ptr<Signal> s : signals_) {
+                       if (!s->enabled())
+                               continue;
 
-       // We received absolute sample numbers, make them relative
-       const int64_t left_sample_delta = sample_num - edges.front().first;
-       const int64_t right_sample_delta = edges.back().first - sample_num - 1;
+                       // Calculate sample number from cursor position
+                       const double samples_per_pixel = s->base()->get_samplerate() * scale();
+                       const int64_t x_offset = offset().convert_to<double>() / scale();
+                       const int64_t sample_num = max(((x_offset + p.x()) * samples_per_pixel), 0.0);
 
-       const int64_t left_delta = left_sample_delta / samples_per_pixel;
-       const int64_t right_delta = right_sample_delta / samples_per_pixel;
+                       vector<data::LogicSegment::EdgePair> edges =
+                               s->get_nearest_level_changes(sample_num);
 
-       int64_t nearest = -1;
+                       if (edges.size() != 2)
+                               continue;
+                       else
+                               edges_found = true;
+
+                       // We received absolute sample numbers, make them relative
+                       const int64_t left_sample_delta = sample_num - edges.front().first;
+                       const int64_t right_sample_delta = edges.back().first - sample_num - 1;
+
+                       const int64_t left_delta = left_sample_delta / samples_per_pixel;
+                       const int64_t right_delta = right_sample_delta / samples_per_pixel;
+
+                       if ((left_delta < nearest_left_delta) || (right_delta < nearest_right_delta)) {
+                               nearest_edges = edges;
+                               nearest_left_delta = left_delta;
+                               nearest_right_delta = right_delta;
+
+                               // Somewhat ugly hack to make TimeItem::drag_by() work
+                               signal_under_mouse_cursor_ = s;
+                       }
+               }
+
+               if (!edges_found)
+                       return -1;
 
-       // Only use closest left or right edge if they're close to the cursor
-       if ((left_delta < right_delta) && (left_delta < snap_distance_))
-               nearest = edges.front().first;
-       if ((left_delta >= right_delta) && (right_delta < snap_distance_))
-               nearest = edges.back().first;
+               if ((nearest_left_delta < nearest_right_delta) &&
+                       (nearest_left_delta < snap_distance_))
+                       nearest_sample = nearest_edges.front().first;
+
+               if ((nearest_left_delta >= nearest_right_delta) &&
+                       (nearest_right_delta < snap_distance_))
+                       nearest_sample = nearest_edges.back().first;
+       }
 
-       return nearest;
+       return nearest_sample;
 }
 
 void View::restack_all_trace_tree_items()
index 7df0f3de23bfdebf9439f7f7fc02248e603f50d4..4bde31d6dd17e3f6a5dd417a3397ebb98a3ded77 100644 (file)
@@ -310,7 +310,7 @@ public:
         * @param p The current position of the mouse cursor
         * @return The sample number of the nearest level change or -1 if none
         */
-       int64_t get_nearest_level_change(const QPoint &p) const;
+       int64_t get_nearest_level_change(const QPoint &p);
 
        void restack_all_trace_tree_items();