X-Git-Url: https://sigrok.org/gitweb/?p=pulseview.git;a=blobdiff_plain;f=pv%2Fmainwindow.cpp;h=f64345cc532f9d33af4684ad1d7da43046aee7ce;hp=19c7da4c4bb895c8b48e37321793a146af52123c;hb=f4e57597347e47a4ea58fbdc7b0a22e07f1c0ede;hpb=3503810c0f74998d42e40a5658ce56e0755e0507 diff --git a/pv/mainwindow.cpp b/pv/mainwindow.cpp index 19c7da4c..f64345cc 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) : @@ -116,7 +118,7 @@ MainWindow::~MainWindow() // Remove the QMainWindow dock->setWidget(0); - const std::shared_ptr view = entry.second; + const std::shared_ptr view = entry.second; for (shared_ptr session : sessions_) if (session->has_view(view)) @@ -139,7 +141,7 @@ QAction* MainWindow::action_about() const return action_about_; } -shared_ptr MainWindow::get_active_view() const +shared_ptr MainWindow::get_active_view() const { // If there's only one view, use it... if (view_docks_.size() == 1) @@ -161,15 +163,13 @@ shared_ptr MainWindow::get_active_view() const if (entry.first.get() == dock) return entry.second; - return shared_ptr(); + return nullptr; } -shared_ptr MainWindow::add_view(const QString &title, - view::ViewType type, Session &session) +shared_ptr MainWindow::add_view(const QString &title, + views::ViewType type, Session &session) { - shared_ptr v; - - if (type == pv::view::TraceView) { + if (type == views::ViewTypeTrace) { shared_ptr dock = make_shared(title, this); dock->setObjectName(title); addDockWidget(Qt::TopDockWidgetArea, dock.get()); @@ -178,7 +178,8 @@ shared_ptr MainWindow::add_view(const QString &title, QMainWindow *dock_main = new QMainWindow(dock.get()); dock_main->setWindowFlags(Qt::Widget); // Remove Qt::Window flag - v = make_shared(session, dock_main); + shared_ptr v = + make_shared(session, dock_main); view_docks_[dock] = v; session.register_view(v); @@ -195,8 +196,9 @@ shared_ptr MainWindow::add_view(const QString &title, connect(close_btn, SIGNAL(clicked(bool)), this, SLOT(on_view_close_clicked())); - if (type == view::TraceView) { - connect(&session, SIGNAL(trigger_event(util::Timestamp)), v.get(), + if (type == views::ViewTypeTrace) { + connect(&session, SIGNAL(trigger_event(util::Timestamp)), + qobject_cast(v.get()), SLOT(trigger_event(util::Timestamp))); v->enable_sticky_scrolling(action_view_sticky_scrolling_->isChecked()); @@ -218,32 +220,36 @@ shared_ptr MainWindow::add_view(const QString &title, connect(v.get(), SIGNAL(always_zoom_to_fit_changed(bool)), main_bar.get(), SLOT(on_always_zoom_to_fit_changed(bool))); } + + return v; } - return v; + return nullptr; } shared_ptr MainWindow::add_session() { - int id = sessions_.size(); - QString name = tr("Untitled-%1").arg(id + 1); + static int last_session_id = 1; + QString name = tr("Untitled-%1").arg(last_session_id++); shared_ptr session = make_shared(device_manager_, name); - 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(add_view(const QString&, views::ViewType, Session*)), + this, SLOT(on_add_view(const QString&, views::ViewType, Session*))); + connect(session.get(), SIGNAL(name_changed()), + this, SLOT(on_session_name_changed())); sessions_.push_back(session); - shared_ptr main_view = - add_view(name, pv::view::TraceView, *session); + shared_ptr main_view = + add_view(name, views::ViewTypeTrace, *session); return session; } void MainWindow::remove_session(shared_ptr session) { - for (shared_ptr view : session->views()) { + for (shared_ptr view : session->views()) { // Find the dock the view is contained in and close it for (auto entry : view_docks_) if (entry.second == view) @@ -252,6 +258,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()) + setWindowTitle(WindowTitle); } void MainWindow::setup_ui() @@ -282,8 +293,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() @@ -364,7 +376,7 @@ bool MainWindow::restoreState(const QByteArray &state, int version) return false; } -void MainWindow::on_add_view(const QString &title, view::ViewType type, +void MainWindow::on_add_view(const QString &title, views::ViewType type, Session *session) { // We get a pointer and need a reference @@ -373,17 +385,55 @@ 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 for (std::shared_ptr s : sessions_) if (s.get() == session) - add_view(session->name(), pv::view::TraceView, *s); + add_view(session->name(), views::ViewTypeTrace, *s); } void MainWindow::on_view_close_clicked() @@ -400,7 +450,7 @@ void MainWindow::on_view_close_clicked() } // Get the view contained in the dock widget - shared_ptr view; + shared_ptr view; for (auto entry : view_docks_) if (entry.first.get() == dock) @@ -422,14 +472,18 @@ void MainWindow::on_view_close_clicked() void MainWindow::on_actionViewStickyScrolling_triggered() { - shared_ptr view = get_active_view(); + shared_ptr viewbase = get_active_view(); + views::TraceView::View* view = + qobject_cast(viewbase.get()); if (view) view->enable_sticky_scrolling(action_view_sticky_scrolling_->isChecked()); } void MainWindow::on_actionViewColouredBg_triggered() { - shared_ptr view = get_active_view(); + shared_ptr viewbase = get_active_view(); + views::TraceView::View* view = + qobject_cast(viewbase.get()); if (view) view->enable_coloured_bg(action_view_coloured_bg_->isChecked()); }