From 33e1afbe17cfa91c24adb775aba923f07fbeed01 Mon Sep 17 00:00:00 2001 From: Soeren Apel Date: Sun, 4 Sep 2016 00:15:42 +0200 Subject: [PATCH] Update main window and dock widget titles as session name changes --- pv/mainwindow.cpp | 52 +++++++++++++++++++++++++++++++++++++++-- pv/mainwindow.hpp | 6 +++++ pv/session.cpp | 4 +++- pv/toolbars/mainbar.cpp | 4 ++-- 4 files changed, 61 insertions(+), 5 deletions(-) diff --git a/pv/mainwindow.cpp b/pv/mainwindow.cpp index 19c7da4..787e1ac 100644 --- a/pv/mainwindow.cpp +++ b/pv/mainwindow.cpp @@ -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) : @@ -232,6 +234,8 @@ shared_ptr 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) sessions_.remove_if([&](shared_ptr s) { return s == session; }); + + // Update the window title if there is no view left to + // generate focus change events + if (sessions_.empty()) + on_session_name_changed(); } void MainWindow::setup_ui() @@ -282,8 +291,9 @@ void MainWindow::setup_ui() setDockNestingEnabled(true); - // Set the title - setWindowTitle(tr("PulseView")); + connect(static_cast(QCoreApplication::instance()), + SIGNAL(focusChanged(QWidget*, QWidget*)), + this, SLOT(on_focus_changed())); } void MainWindow::save_ui_settings() @@ -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; + bool title_set = false; + + view = get_active_view(); + + for (shared_ptr 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(QObject::sender()); + assert(session); + + for (shared_ptr 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 diff --git a/pv/mainwindow.hpp b/pv/mainwindow.hpp index 0d2db8c..3cd269f 100644 --- a/pv/mainwindow.hpp +++ b/pv/mainwindow.hpp @@ -57,6 +57,9 @@ class MainWindow : public QMainWindow { Q_OBJECT +private: + static const QString WindowTitle; + public: explicit MainWindow(DeviceManager &device_manager, std::string open_file_name = std::string(), @@ -96,7 +99,10 @@ private Q_SLOTS: void on_add_view(const QString &title, view::ViewType type, Session *session); + void on_focus_changed(); + void on_new_session(); + void on_session_name_changed(); void on_new_view(Session *session); void on_view_close_clicked(); diff --git a/pv/session.cpp b/pv/session.cpp index fc4a748..f6fb708 100644 --- a/pv/session.cpp +++ b/pv/session.cpp @@ -99,6 +99,7 @@ using Glib::Variant; namespace pv { Session::Session(DeviceManager &device_manager, QString name) : device_manager_(device_manager), + default_name_(name), name_(name), capture_state_(Stopped), cur_samplerate_(0) @@ -300,10 +301,11 @@ void Session::restore_settings(QSettings &settings) device = std::make_shared(device_manager_.context(), filename.toStdString()); set_device(device); - set_name(filename); // TODO Perform error handling start_capture([](QString infoMessage) { (void)infoMessage; }); + + set_name(QFileInfo(filename).fileName()); } } diff --git a/pv/toolbars/mainbar.cpp b/pv/toolbars/mainbar.cpp index a0f190f..483dd79 100644 --- a/pv/toolbars/mainbar.cpp +++ b/pv/toolbars/mainbar.cpp @@ -502,12 +502,12 @@ void MainBar::load_file(QString file_name, return; } - session_.set_name(QFileInfo(file_name).fileName()); - update_device_list(); session_.start_capture([&, errorMessage](QString infoMessage) { session_error(errorMessage, infoMessage); }); + + session_.set_name(QFileInfo(file_name).fileName()); } void MainBar::update_sample_rate_selector() -- 2.30.2