]> sigrok.org Git - pulseview.git/commitdiff
View: Fix snap-to-edge when using all signals
authorSoeren Apel <redacted>
Fri, 5 Oct 2018 16:56:11 +0000 (18:56 +0200)
committerSoeren Apel <redacted>
Fri, 5 Oct 2018 16:58:21 +0000 (18:58 +0200)
pv/views/trace/view.cpp

index d2d61ceda5b08b186a0820815b5f73d53bcfdc38..e68c16a8dc6013634e2add3f90025a4d25e84dda 100644 (file)
@@ -891,17 +891,14 @@ int64_t View::get_nearest_level_change(const QPoint &p)
                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_))
+               if ((left_delta < right_delta) && (left_delta <= snap_distance_))
                        nearest_sample = nearest_edges.front().first;
-               if ((left_delta >= right_delta) && (right_delta < snap_distance_))
+               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;
-
+               int64_t nearest_delta = numeric_limits<int64_t>::max();
                for (shared_ptr<Signal> s : signals_) {
                        if (!s->enabled())
                                continue;
@@ -916,8 +913,6 @@ int64_t View::get_nearest_level_change(const QPoint &p)
 
                        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;
@@ -926,26 +921,17 @@ int64_t View::get_nearest_level_change(const QPoint &p)
                        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;
+                       if ((left_delta < nearest_delta) || (right_delta < nearest_delta)) {
+                               nearest_delta = min(left_delta, right_delta);
+
+                               if (nearest_delta <= snap_distance_)
+                                       nearest_sample = (nearest_delta == left_delta) ?
+                                               edges.front().first : edges.back().first;
 
                                // Somewhat ugly hack to make TimeItem::drag_by() work
                                signal_under_mouse_cursor_ = s;
                        }
                }
-
-               if (!edges_found)
-                       return -1;
-
-               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_sample;