From 20c99cfc69d3c7430817abd9a1f810698deb4a18 Mon Sep 17 00:00:00 2001 From: Soeren Apel Date: Thu, 11 Jun 2020 00:51:17 +0200 Subject: [PATCH] TabularDecView: Allow return/enter press and don't change scale --- pv/views/tabular_decoder/view.cpp | 10 ++++++++++ pv/views/tabular_decoder/view.hpp | 11 +++++++++-- pv/views/trace/view.cpp | 24 +++++++++++++++++------- 3 files changed, 36 insertions(+), 9 deletions(-) diff --git a/pv/views/tabular_decoder/view.cpp b/pv/views/tabular_decoder/view.cpp index 4715b4a9..91bcaea1 100644 --- a/pv/views/tabular_decoder/view.cpp +++ b/pv/views/tabular_decoder/view.cpp @@ -139,6 +139,14 @@ QSize CustomTableView::sizeHint() const return minimumSizeHint(); } +void CustomTableView::keyPressEvent(QKeyEvent *event) +{ + if ((event->key() == Qt::Key_Return) || (event->key() == Qt::Key_Enter)) + activatedByKey(currentIndex()); + else + QTableView::keyPressEvent(event); +} + View::View(Session &session, bool is_main_view, QMainWindow *parent) : ViewBase(session, is_main_view, parent), @@ -239,6 +247,8 @@ View::View(Session &session, bool is_main_view, QMainWindow *parent) : this, SLOT(on_table_item_clicked(const QModelIndex&))); connect(table_view_, SIGNAL(doubleClicked(const QModelIndex&)), this, SLOT(on_table_item_double_clicked(const QModelIndex&))); + connect(table_view_, SIGNAL(activatedByKey(const QModelIndex&)), + this, SLOT(on_table_item_double_clicked(const QModelIndex&))); connect(table_view_->horizontalHeader(), SIGNAL(customContextMenuRequested(const QPoint&)), this, SLOT(on_table_header_requested(const QPoint&))); diff --git a/pv/views/tabular_decoder/view.hpp b/pv/views/tabular_decoder/view.hpp index 2d0ce54a..3cdcac18 100644 --- a/pv/views/tabular_decoder/view.hpp +++ b/pv/views/tabular_decoder/view.hpp @@ -23,6 +23,7 @@ #include #include #include +#include #include #include #include @@ -130,8 +131,14 @@ class CustomTableView : public QTableView Q_OBJECT public: - QSize minimumSizeHint() const; - QSize sizeHint() const; + virtual QSize minimumSizeHint() const override; + virtual QSize sizeHint() const override; + +protected: + virtual void keyPressEvent(QKeyEvent *event) override; + +Q_SIGNALS: + void activatedByKey(const QModelIndex &index); }; diff --git a/pv/views/trace/view.cpp b/pv/views/trace/view.cpp index 43853359..e2d9365e 100644 --- a/pv/views/trace/view.cpp +++ b/pv/views/trace/view.cpp @@ -854,21 +854,31 @@ void View::zoom_fit(bool gui_state) void View::focus_on_range(uint64_t start_sample, uint64_t end_sample) { assert(viewport_); - const int w = viewport_->width(); + const uint64_t w = viewport_->width(); if (w <= 0) return; const double samplerate = session_.get_samplerate(); + const double samples_per_pixel = samplerate * scale_; + const uint64_t viewport_samples = w * samples_per_pixel; const uint64_t sample_delta = (end_sample - start_sample); // Note: We add 20% margin on the left and 5% on the right - const Timestamp delta = (sample_delta * 1.25) / samplerate; - - const Timestamp scale = max(min(delta / w, MaxScale), MinScale); - const Timestamp offset = (start_sample - sample_delta * 0.20) / samplerate; - - set_scale_offset(scale.convert_to(), offset); + const uint64_t ext_sample_delta = sample_delta * 1.25; + + // Check if we can keep the zoom level and just center the supplied range + if (viewport_samples >= ext_sample_delta) { + // Note: offset is the left edge of the view so to center, we subtract half the view width + const int64_t sample_offset = (start_sample + (sample_delta / 2) - (viewport_samples / 2)); + const Timestamp offset = sample_offset / samplerate; + set_scale_offset(scale_, offset); + } else { + const Timestamp offset = (start_sample - sample_delta * 0.20) / samplerate; + const Timestamp delta = ext_sample_delta / samplerate; + const Timestamp scale = max(min(delta / w, MaxScale), MinScale); + set_scale_offset(scale.convert_to(), offset); + } } void View::set_scale_offset(double scale, const Timestamp& offset) -- 2.30.2