]> sigrok.org Git - pulseview.git/blobdiff - pv/mainwindow.cpp
MainWindow: Fix crash on closing last session
[pulseview.git] / pv / mainwindow.cpp
index 8787073d5875e1d82c64d826eb845b26e09c9b3c..a8f093bccd6eb051c533452b3e86b490625b2bff 100644 (file)
@@ -62,6 +62,8 @@ class ViewItem;
 
 using toolbars::MainBar;
 
+const QString MainWindow::WindowTitle = tr("PulseView");
+
 MainWindow::MainWindow(DeviceManager &device_manager,
        string open_file_name, string open_file_format,
        QWidget *parent) :
@@ -161,7 +163,7 @@ shared_ptr<pv::view::View> MainWindow::get_active_view() const
                if (entry.first.get() == dock)
                        return entry.second;
 
-       return shared_ptr<pv::view::View>();
+       return nullptr;
 }
 
 shared_ptr<pv::view::View> MainWindow::add_view(const QString &title,
@@ -232,6 +234,8 @@ shared_ptr<Session> MainWindow::add_session()
 
        connect(session.get(), SIGNAL(add_view(const QString&, view::ViewType, Session*)),
                this, SLOT(on_add_view(const QString&, view::ViewType, Session*)));
+       connect(session.get(), SIGNAL(name_changed()),
+               this, SLOT(on_session_name_changed()));
 
        sessions_.push_back(session);
 
@@ -252,6 +256,11 @@ void MainWindow::remove_session(shared_ptr<Session> session)
 
        sessions_.remove_if([&](shared_ptr<Session> s) {
                return s == session; });
+
+       // Update the window title if there is no view left to
+       // generate focus change events
+       if (sessions_.empty())
+               setWindowTitle(WindowTitle);
 }
 
 void MainWindow::setup_ui()
@@ -282,8 +291,9 @@ void MainWindow::setup_ui()
 
        setDockNestingEnabled(true);
 
-       // Set the title
-       setWindowTitle(tr("PulseView"));
+       connect(static_cast<QApplication *>(QCoreApplication::instance()),
+               SIGNAL(focusChanged(QWidget*, QWidget*)),
+               this, SLOT(on_focus_changed()));
 }
 
 void MainWindow::save_ui_settings()
@@ -297,7 +307,7 @@ void MainWindow::save_ui_settings()
        settings.endGroup();
 
        for (shared_ptr<Session> session : sessions_) {
-               // Ignore sessions using the demo device
+               // Ignore sessions using the demo device or no device at all
                if (session->device()) {
                        shared_ptr<devices::HardwareDevice> device =
                                dynamic_pointer_cast< devices::HardwareDevice >
@@ -306,12 +316,12 @@ void MainWindow::save_ui_settings()
                        if (device &&
                                device->hardware_device()->driver()->name() == "demo")
                                continue;
-               }
 
-               settings.beginGroup("Session" + QString::number(id++));
-               settings.remove("");  // Remove all keys in this group
-               session->save_settings(settings);
-               settings.endGroup();
+                       settings.beginGroup("Session" + QString::number(id++));
+                       settings.remove("");  // Remove all keys in this group
+                       session->save_settings(settings);
+                       settings.endGroup();
+               }
        }
 
        settings.setValue("sessions", id);
@@ -373,11 +383,49 @@ void MainWindow::on_add_view(const QString &title, view::ViewType type,
                        add_view(title, type, *s);
 }
 
+void MainWindow::on_focus_changed()
+{
+       shared_ptr<view::View> view;
+       bool title_set = false;
+
+       view = get_active_view();
+
+       for (shared_ptr<Session> session : sessions_) {
+               if (!session->has_view(view))
+                       continue;
+
+               setWindowTitle(session->name() + " - " + WindowTitle);
+               title_set = true;
+       }
+
+       if (!title_set)
+               setWindowTitle(WindowTitle);
+}
+
 void MainWindow::on_new_session()
 {
        add_session();
 }
 
+void MainWindow::on_session_name_changed()
+{
+       // Update the corresponding dock widget's name(s)
+       Session *session = qobject_cast<Session*>(QObject::sender());
+       assert(session);
+
+       for (shared_ptr<view::View> view : session->views()) {
+               // Get the dock that contains the view
+               for (auto entry : view_docks_)
+                       if (entry.second == view) {
+                               entry.first->setObjectName(session->name());
+                               entry.first->setWindowTitle(session->name());
+                       }
+       }
+
+       // Refresh window title if the affected session has focus
+       on_focus_changed();
+}
+
 void MainWindow::on_new_view(Session *session)
 {
        // We get a pointer and need a reference