X-Git-Url: https://sigrok.org/gitweb/?p=pulseview.git;a=blobdiff_plain;f=pv%2Fmainwindow.cpp;h=92c92d023225a7d5fa7cb5f54764d4e1218b2a12;hp=45d805a8d6f87f2c08eb649cbdf930c70dca48df;hb=82f8a42ba990894356fedd141bae2579ff5373a8;hpb=aecae05c598b1fe914253b980aa6ad43b85f562b diff --git a/pv/mainwindow.cpp b/pv/mainwindow.cpp index 45d805a8..92c92d02 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) : @@ -161,7 +163,7 @@ 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, @@ -186,7 +188,14 @@ shared_ptr MainWindow::add_view(const QString &title, dock->setWidget(dock_main); dock->setFeatures(QDockWidget::DockWidgetMovable | - QDockWidget::DockWidgetFloatable); + QDockWidget::DockWidgetFloatable | QDockWidget::DockWidgetClosable); + + QAbstractButton *close_btn = + dock->findChildren + ("qt_dockwidget_closebutton").front(); + + 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(), @@ -200,6 +209,11 @@ shared_ptr MainWindow::add_view(const QString &title, main_bar = make_shared(session, *this); dock_main->addToolBar(main_bar.get()); session.set_main_bar(main_bar); + + connect(main_bar.get(), SIGNAL(new_session()), + this, SLOT(on_new_session())); + connect(main_bar.get(), SIGNAL(new_view(Session*)), + this, SLOT(on_new_view(Session*))); } main_bar->action_view_show_cursors()->setChecked(v->cursors_shown()); @@ -218,6 +232,11 @@ shared_ptr MainWindow::add_session() 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(name_changed()), + this, SLOT(on_session_name_changed())); + sessions_.push_back(session); shared_ptr main_view = @@ -226,6 +245,24 @@ shared_ptr MainWindow::add_session() return session; } +void MainWindow::remove_session(shared_ptr session) +{ + 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) + entry.first->close(); + } + + 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() { setObjectName(QString::fromUtf8("MainWindow")); @@ -252,8 +289,11 @@ void MainWindow::setup_ui() action_about_->setObjectName(QString::fromUtf8("actionAbout")); action_about_->setText(tr("&About...")); - // Set the title - setWindowTitle(tr("PulseView")); + setDockNestingEnabled(true); + + connect(static_cast(QCoreApplication::instance()), + SIGNAL(focusChanged(QWidget*, QWidget*)), + this, SLOT(on_focus_changed())); } void MainWindow::save_ui_settings() @@ -267,20 +307,21 @@ void MainWindow::save_ui_settings() settings.endGroup(); for (shared_ptr session : sessions_) { - // Ignore sessions using the demo device + // Ignore sessions using the demo device or no device at all if (session->device()) { shared_ptr device = dynamic_pointer_cast< devices::HardwareDevice > (session->device()); - if (device->hardware_device()->driver()->name() == "demo") + 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); @@ -333,6 +374,100 @@ bool MainWindow::restoreState(const QByteArray &state, int version) return false; } +void MainWindow::on_add_view(const QString &title, view::ViewType type, + Session *session) +{ + // We get a pointer and need a reference + for (std::shared_ptr s : sessions_) + if (s.get() == session) + 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); +} + +void MainWindow::on_view_close_clicked() +{ + // Find the dock widget that contains the close button that was clicked + QObject *w = QObject::sender(); + QDockWidget *dock = 0; + + while (w) { + dock = qobject_cast(w); + if (dock) + break; + w = w->parent(); + } + + // Get the view contained in the dock widget + shared_ptr view; + + for (auto entry : view_docks_) + if (entry.first.get() == dock) + view = entry.second; + + // Deregister the view + for (shared_ptr session : sessions_) { + if (!session->has_view(view)) + continue; + + // Also destroy the entire session if its main view is closing + if (view == session->main_view()) { + remove_session(session); + break; + } else + session->deregister_view(view); + } +} + void MainWindow::on_actionViewStickyScrolling_triggered() { shared_ptr view = get_active_view();