]> sigrok.org Git - pulseview.git/blobdiff - pv/views/trace/view.cpp
Use QMenu->popup() instead of QMenu->exec() everywhere
[pulseview.git] / pv / views / trace / view.cpp
index 29c0675d63b5ca1f2db20da4008e88083c8ac110..375f5754c7750a5277e538044a8d6f122f27fb44 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,105 @@ 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)
 {
+       // Is snapping disabled?
        if (snap_distance_ == 0)
                return -1;
 
-       shared_ptr<Signal> signal = signal_under_mouse_cursor_;
+       struct entry_t {
+               entry_t(shared_ptr<Signal> s) :
+                       signal(s), delta(numeric_limits<int64_t>::max()), sample(-1), is_dense(false) {}
+               shared_ptr<Signal> signal;
+               int64_t delta;
+               int64_t sample;
+               bool is_dense;
+       };
 
-       if (!signal)
-               return -1;
+       vector<entry_t> list;
 
-       // 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);
+       // Create list of signals to consider
+       if (signal_under_mouse_cursor_)
+               list.emplace_back(signal_under_mouse_cursor_);
+       else
+               for (shared_ptr<Signal> s : signals_) {
+                       if (!s->enabled())
+                               continue;
 
-       // Query for nearest level changes
-       vector<data::LogicSegment::EdgePair> edges =
-               signal->get_nearest_level_changes(sample_num);
+                       list.emplace_back(s);
+               }
 
-       if (edges.size() != 2)
-               return -1;
+       // Get data for listed signals
+       for (entry_t &e : list) {
+               // Calculate sample number from cursor position
+               const double samples_per_pixel = e.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);
+
+               vector<data::LogicSegment::EdgePair> edges =
+                       e.signal->get_nearest_level_changes(sample_num);
+
+               if (edges.empty())
+                       continue;
+
+               // Check first edge
+               const int64_t first_sample_delta = abs(sample_num - edges.front().first);
+               const int64_t first_delta = first_sample_delta / samples_per_pixel;
+               e.delta = first_delta;
+               e.sample = edges.front().first;
+
+               // Check second edge if available
+               if (edges.size() == 2) {
+                       // Note: -1 because this is usually the right edge and sample points are left-aligned
+                       const int64_t second_sample_delta = abs(sample_num - edges.back().first - 1);
+                       const int64_t second_delta = second_sample_delta / samples_per_pixel;
+
+                       // If both edges are too close, we mark this signal as being dense
+                       if ((first_delta + second_delta) <= snap_distance_)
+                               e.is_dense = true;
+
+                       if (second_delta < first_delta) {
+                               e.delta = second_delta;
+                               e.sample = edges.back().first;
+                       }
+               }
+       }
 
-       // 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;
+       // Look for the best match: non-dense first, then dense
+       entry_t *match = nullptr;
 
-       const int64_t left_delta = left_sample_delta / samples_per_pixel;
-       const int64_t right_delta = right_sample_delta / samples_per_pixel;
+       for (entry_t &e : list) {
+               if (e.delta > snap_distance_ || e.is_dense)
+                       continue;
 
-       int64_t nearest = -1;
+               if (match) {
+                       if (e.delta < match->delta)
+                               match = &e;
+               } else
+                       match = &e;
+       }
+
+       if (!match) {
+               for (entry_t &e : list) {
+                       if (!e.is_dense)
+                               continue;
+
+                       if (match) {
+                               if (e.delta < match->delta)
+                                       match = &e;
+                       } else
+                               match = &e;
+               }
+       }
 
-       // 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 (match) {
+               // Somewhat ugly hack to make TimeItem::drag_by() work
+               signal_under_mouse_cursor_ = match->signal;
+
+               return match->sample;
+       }
 
-       return nearest;
+       return -1;
 }
 
 void View::restack_all_trace_tree_items()
@@ -1266,7 +1328,10 @@ bool View::eventFilter(QObject *object, QEvent *event)
                if (object == viewport_)
                        hover_point_ = mouse_event->pos();
                else if (object == ruler_)
-                       hover_point_ = QPoint(mouse_event->x(), 0);
+                       // Adjust the hover point's y coordinate so that it's relative to
+                       // the top of the viewport. The result may be negative.
+                       hover_point_ = QPoint(mouse_event->pos().x(),
+                               mouse_event->pos().y() - ruler_->sizeHint().height());
                else if (object == header_)
                        hover_point_ = QPoint(0, mouse_event->y());
                else
@@ -1317,7 +1382,7 @@ void View::contextMenuEvent(QContextMenuEvent *event)
 
        QMenu *menu = r->create_view_context_menu(this, pos);
        if (menu)
-               menu->exec(event->globalPos());
+               menu->popup(event->globalPos());
 }
 
 void View::resizeEvent(QResizeEvent* event)