From 3d6ff1cd572a55e92779420c2d1a708cdb003fe0 Mon Sep 17 00:00:00 2001 From: Soeren Apel Date: Sat, 25 Jan 2020 23:47:31 +0100 Subject: [PATCH] Fix various focus- and hotkey-related issues Also, the hotkeys '1' and '2' now show the cursor if it was hidden. --- pv/mainwindow.cpp | 30 +++++++++++++-------- pv/views/trace/standardbar.cpp | 2 +- pv/views/trace/tracetreeitemowner.cpp | 2 +- pv/views/trace/view.cpp | 39 ++++++++++++++++----------- pv/views/trace/view.hpp | 8 +++--- 5 files changed, 49 insertions(+), 32 deletions(-) diff --git a/pv/mainwindow.cpp b/pv/mainwindow.cpp index 1fb46e13..ceace369 100644 --- a/pv/mainwindow.cpp +++ b/pv/mainwindow.cpp @@ -222,6 +222,8 @@ shared_ptr MainWindow::add_view(views::ViewType type, } } + v->setFocus(); + return v; } @@ -257,7 +259,7 @@ shared_ptr MainWindow::add_subwindow( subwindows::SubWindowType type, Session &session) { GlobalSettings settings; - shared_ptr v; + shared_ptr w; QMainWindow *main_window = nullptr; for (auto& entry : session_windows_) @@ -288,14 +290,14 @@ shared_ptr MainWindow::add_subwindow( #ifdef ENABLE_DECODE if (type == subwindows::SubWindowTypeDecoderSelector) - v = make_shared(session, dock_main); + w = make_shared(session, dock_main); #endif - if (!v) + if (!w) return nullptr; - sub_windows_[dock] = v; - dock_main->setCentralWidget(v.get()); + sub_windows_[dock] = w; + dock_main->setCentralWidget(w.get()); dock->setWidget(dock_main); dock->setContextMenuPolicy(Qt::PreventContextMenu); @@ -312,13 +314,15 @@ shared_ptr MainWindow::add_subwindow( connect(close_btn, SIGNAL(clicked(bool)), this, SLOT(on_sub_window_close_clicked())); - if (v->has_toolbar()) - dock_main->addToolBar(v->create_toolbar(dock_main)); + if (w->has_toolbar()) + dock_main->addToolBar(w->create_toolbar(dock_main)); - if (v->minimum_width() > 0) - dock->setMinimumSize(v->minimum_width(), 0); + if (w->minimum_width() > 0) + dock->setMinimumSize(w->minimum_width(), 0); - return v; + w->setFocus(); + + return w; } shared_ptr MainWindow::add_session() @@ -349,7 +353,7 @@ shared_ptr MainWindow::add_session() window->setDockNestingEnabled(true); - shared_ptr main_view = add_view(views::ViewTypeTrace, *session); + add_view(views::ViewTypeTrace, *session); return session; } @@ -885,6 +889,10 @@ void MainWindow::on_sub_window_close_clicked() sub_windows_.erase(dock); dock->close(); + + // Restore focus to the last used main view + if (last_focused_session_) + last_focused_session_->main_view()->setFocus(); } void MainWindow::on_view_colored_bg_shortcut() diff --git a/pv/views/trace/standardbar.cpp b/pv/views/trace/standardbar.cpp index 22697506..48d92891 100644 --- a/pv/views/trace/standardbar.cpp +++ b/pv/views/trace/standardbar.cpp @@ -203,7 +203,7 @@ void StandardBar::on_actionViewShowCursors_triggered() const bool show = action_view_show_cursors_->isChecked(); if (show) - view_->centre_cursors(); + view_->center_cursors(); view_->show_cursors(show); } diff --git a/pv/views/trace/tracetreeitemowner.cpp b/pv/views/trace/tracetreeitemowner.cpp index ccf9b912..95ea8373 100644 --- a/pv/views/trace/tracetreeitemowner.cpp +++ b/pv/views/trace/tracetreeitemowner.cpp @@ -114,7 +114,7 @@ void TraceTreeItemOwner::restack_items() { vector> items(trace_tree_child_items()); - // Sort by the centre line of the extents + // Sort by the center line of the extents stable_sort(items.begin(), items.end(), [](const shared_ptr &a, const shared_ptr &b) { const auto aext = a->v_extents(); diff --git a/pv/views/trace/view.cpp b/pv/views/trace/view.cpp index c8a7a2b8..15e65127 100644 --- a/pv/views/trace/view.cpp +++ b/pv/views/trace/view.cpp @@ -208,7 +208,7 @@ View::View(Session &session, bool is_main_view, QMainWindow *parent) : // Set up local keyboard shortcuts zoom_in_shortcut_ = new QShortcut(QKeySequence(Qt::Key_Plus), this, - SLOT(on_zoom_in_shortcut_triggered()), nullptr, Qt::WidgetWithChildrenShortcut); + SLOT(on_zoom_in_shortcut_triggered()), nullptr, Qt::WidgetWithChildrenShortcut); zoom_in_shortcut_->setAutoRepeat(false); zoom_out_shortcut_ = new QShortcut(QKeySequence(Qt::Key_Minus), this, @@ -228,17 +228,20 @@ View::View(Session &session, bool is_main_view, QMainWindow *parent) : SLOT(on_scroll_to_end_shortcut_triggered()), nullptr, Qt::WidgetWithChildrenShortcut); end_shortcut_->setAutoRepeat(false); - grab_ruler_left_shortcut_ = new QShortcut(QKeySequence(Qt::Key_1), this); + grab_ruler_left_shortcut_ = new QShortcut(QKeySequence(Qt::Key_1), this, + nullptr, nullptr, Qt::WidgetWithChildrenShortcut); connect(grab_ruler_left_shortcut_, &QShortcut::activated, this, [=]{on_grab_ruler(1);}); grab_ruler_left_shortcut_->setAutoRepeat(false); - grab_ruler_right_shortcut_ = new QShortcut(QKeySequence(Qt::Key_2), this); + grab_ruler_right_shortcut_ = new QShortcut(QKeySequence(Qt::Key_2), this, + nullptr, nullptr, Qt::WidgetWithChildrenShortcut); connect(grab_ruler_right_shortcut_, &QShortcut::activated, this, [=]{on_grab_ruler(2);}); grab_ruler_right_shortcut_->setAutoRepeat(false); - cancel_grab_shortcut_ = new QShortcut(QKeySequence(Qt::Key_Escape), this); + cancel_grab_shortcut_ = new QShortcut(QKeySequence(Qt::Key_Escape), this, + nullptr, nullptr, Qt::WidgetWithChildrenShortcut); connect(cancel_grab_shortcut_, &QShortcut::activated, this, [=]{grabbed_widget_ = nullptr;}); cancel_grab_shortcut_->setAutoRepeat(false); @@ -874,7 +877,7 @@ void View::set_cursors(pv::util::Timestamp& first, pv::util::Timestamp& second) viewport_->update(); } -void View::centre_cursors() +void View::center_cursors() { assert(cursors_); @@ -1623,18 +1626,22 @@ void View::v_scroll_value_changed() void View::on_grab_ruler(int ruler_id) { - if (cursors_shown()) { - // Release the grabbed widget if its trigger hotkey was pressed twice - if (ruler_id == 1) - grabbed_widget_ = (grabbed_widget_ == cursors_->first().get()) ? - nullptr : cursors_->first().get(); - else - grabbed_widget_ = (grabbed_widget_ == cursors_->second().get()) ? - nullptr : cursors_->second().get(); - - if (grabbed_widget_) - grabbed_widget_->set_time(offset_ + mapFromGlobal(QCursor::pos()).x() * scale_); + if (!cursors_shown()) { + center_cursors(); + show_cursors(); } + + // Release the grabbed widget if its trigger hotkey was pressed twice + if (ruler_id == 1) + grabbed_widget_ = (grabbed_widget_ == cursors_->first().get()) ? + nullptr : cursors_->first().get(); + else + grabbed_widget_ = (grabbed_widget_ == cursors_->second().get()) ? + nullptr : cursors_->second().get(); + + if (grabbed_widget_) + grabbed_widget_->set_time(ruler_->get_absolute_time_from_x_pos( + mapFromGlobal(QCursor::pos()).x() - header_width())); } void View::signals_changed() diff --git a/pv/views/trace/view.hpp b/pv/views/trace/view.hpp index 040bac1b..2938d00f 100644 --- a/pv/views/trace/view.hpp +++ b/pv/views/trace/view.hpp @@ -271,7 +271,7 @@ public: bool colored_bg() const; /** - * Returns true if cursors are displayed. false otherwise. + * Returns true if cursors are displayed, false otherwise. */ bool cursors_shown() const; @@ -281,14 +281,16 @@ public: void show_cursors(bool show = true); /** - * Sets the cursors to the given offsets. You will still have to call show_cursors separately. + * Sets the cursors to the given offsets. + * You still have to call show_cursors() separately. */ void set_cursors(pv::util::Timestamp& first, pv::util::Timestamp& second); /** * Moves the cursors to a convenient position in the view. + * You still have to call show_cursors() separately. */ - void centre_cursors(); + void center_cursors(); /** * Returns a reference to the pair of cursors. -- 2.30.2