]> sigrok.org Git - pulseview.git/blobdiff - pv/mainwindow.cpp
Fix various focus- and hotkey-related issues
[pulseview.git] / pv / mainwindow.cpp
index c5387beb0a21b0ed943324b926cb093b77b48a3f..ceace369f982158b26668ed38d4e836655e84f2d 100644 (file)
@@ -40,6 +40,7 @@
 
 #include "mainwindow.hpp"
 
+#include "application.hpp"
 #include "devicemanager.hpp"
 #include "devices/hardwaredevice.hpp"
 #include "dialogs/settings.hpp"
@@ -71,7 +72,6 @@ MainWindow::MainWindow(DeviceManager &device_manager, QWidget *parent) :
        QMainWindow(parent),
        device_manager_(device_manager),
        session_selector_(this),
-       session_state_mapper_(this),
        icon_red_(":/icons/status-red.svg"),
        icon_green_(":/icons/status-green.svg"),
        icon_grey_(":/icons/status-grey.svg")
@@ -222,6 +222,8 @@ shared_ptr<views::ViewBase> MainWindow::add_view(views::ViewType type,
                }
        }
 
+       v->setFocus();
+
        return v;
 }
 
@@ -257,7 +259,7 @@ shared_ptr<subwindows::SubWindowBase> MainWindow::add_subwindow(
        subwindows::SubWindowType type, Session &session)
 {
        GlobalSettings settings;
-       shared_ptr<subwindows::SubWindowBase> v;
+       shared_ptr<subwindows::SubWindowBase> w;
 
        QMainWindow *main_window = nullptr;
        for (auto& entry : session_windows_)
@@ -288,14 +290,14 @@ shared_ptr<subwindows::SubWindowBase> MainWindow::add_subwindow(
 
 #ifdef ENABLE_DECODE
        if (type == subwindows::SubWindowTypeDecoderSelector)
-               v = make_shared<subwindows::decoder_selector::SubWindow>(session, dock_main);
+               w = make_shared<subwindows::decoder_selector::SubWindow>(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<subwindows::SubWindowBase> 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<Session> MainWindow::add_session()
@@ -334,9 +338,8 @@ shared_ptr<Session> MainWindow::add_session()
                this, SLOT(on_session_name_changed()));
        connect(session.get(), SIGNAL(device_changed()),
                this, SLOT(on_session_device_changed()));
-       session_state_mapper_.setMapping(session.get(), session.get());
        connect(session.get(), SIGNAL(capture_state_changed(int)),
-               &session_state_mapper_, SLOT(map()));
+               this, SLOT(on_session_capture_state_changed(int)));
 
        sessions_.push_back(session);
 
@@ -350,7 +353,7 @@ shared_ptr<Session> MainWindow::add_session()
 
        window->setDockNestingEnabled(true);
 
-       shared_ptr<views::ViewBase> main_view = add_view(views::ViewTypeTrace, *session);
+       add_view(views::ViewTypeTrace, *session);
 
        return session;
 }
@@ -552,8 +555,6 @@ void MainWindow::setup_ui()
                this, SLOT(on_new_session_clicked()));
        connect(run_stop_button_, SIGNAL(clicked(bool)),
                this, SLOT(on_run_stop_clicked()));
-       connect(&session_state_mapper_, SIGNAL(mapped(QObject*)),
-               this, SLOT(on_capture_state_changed(QObject*)));
        connect(settings_button_, SIGNAL(clicked(bool)),
                this, SLOT(on_settings_clicked()));
 
@@ -693,7 +694,7 @@ void MainWindow::on_focused_session_changed(shared_ptr<Session> session)
        setWindowTitle(session->name() + " - " + WindowTitle);
 
        // Update the state of the run/stop button, too
-       on_capture_state_changed(session.get());
+       update_acq_button(session.get());
 }
 
 void MainWindow::on_new_session_clicked()
@@ -767,16 +768,19 @@ void MainWindow::on_session_device_changed()
        update_acq_button(session);
 }
 
-void MainWindow::on_capture_state_changed(QObject *obj)
+void MainWindow::on_session_capture_state_changed(int state)
 {
-       Session *caller = qobject_cast<Session*>(obj);
+       (void)state;
+
+       Session *session = qobject_cast<Session*>(QObject::sender());
+       assert(session);
 
        // Ignore if caller is not the currently focused session
        // unless there is only one session
-       if ((sessions_.size() > 1) && (caller != last_focused_session_.get()))
+       if ((sessions_.size() > 1) && (session != last_focused_session_.get()))
                return;
 
-       update_acq_button(caller);
+       update_acq_button(session);
 }
 
 void MainWindow::on_new_view(Session *session, int view_type)
@@ -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()