From: Soeren Apel Date: Thu, 29 Sep 2016 17:36:48 +0000 (+0200) Subject: MainWindow: Use a QTabWidget to display the QDockWidgets X-Git-Tag: pulseview-0.4.0~238 X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=3b84fd0b5f02dedfb6f7160477a63467b613c502;p=pulseview.git MainWindow: Use a QTabWidget to display the QDockWidgets --- diff --git a/pv/mainwindow.cpp b/pv/mainwindow.cpp index f64345cc..2f82cb19 100644 --- a/pv/mainwindow.cpp +++ b/pv/mainwindow.cpp @@ -69,6 +69,7 @@ MainWindow::MainWindow(DeviceManager &device_manager, QWidget *parent) : QMainWindow(parent), device_manager_(device_manager), + session_selector_(this), action_view_sticky_scrolling_(new QAction(this)), action_view_coloured_bg_(new QAction(this)), action_about_(new QAction(this)) @@ -107,23 +108,8 @@ MainWindow::MainWindow(DeviceManager &device_manager, MainWindow::~MainWindow() { - for (auto entry : view_docks_) { - - const std::shared_ptr dock = entry.first; - - // Remove view from the dock widget's QMainWindow - QMainWindow *dock_main = dynamic_cast(dock->widget()); - dock_main->setCentralWidget(0); - - // Remove the QMainWindow - dock->setWidget(0); - - const std::shared_ptr view = entry.second; - - for (shared_ptr session : sessions_) - if (session->has_view(view)) - session->deregister_view(view); - } + while (!sessions_.empty()) + remove_session(sessions_.front()); } QAction* MainWindow::action_view_sticky_scrolling() const @@ -169,10 +155,17 @@ shared_ptr MainWindow::get_active_view() const shared_ptr MainWindow::add_view(const QString &title, views::ViewType type, Session &session) { + QMainWindow *main_window; + for (auto entry : session_windows_) + if (entry.first.get() == &session) + main_window = entry.second; + + assert(main_window); + if (type == views::ViewTypeTrace) { - shared_ptr dock = make_shared(title, this); + shared_ptr dock = make_shared(title, main_window); dock->setObjectName(title); - addDockWidget(Qt::TopDockWidgetArea, dock.get()); + main_window->addDockWidget(Qt::TopDockWidgetArea, dock.get()); // Insert a QMainWindow into the dock widget to allow for a tool bar QMainWindow *dock_main = new QMainWindow(dock.get()); @@ -241,6 +234,11 @@ shared_ptr MainWindow::add_session() sessions_.push_back(session); + QMainWindow *window = new QMainWindow(); + window->setWindowFlags(Qt::Widget); // Remove Qt::Window flag + session_windows_[session] = window; + session_selector_.addTab(window, name); + shared_ptr main_view = add_view(name, views::ViewTypeTrace, *session); @@ -250,12 +248,28 @@ shared_ptr MainWindow::add_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 + // Find the dock the view is contained in and remove it for (auto entry : view_docks_) - if (entry.second == view) - entry.first->close(); + if (entry.second == view) { + // Remove the view from the session + session->deregister_view(view); + + // Remove the view from its parent; otherwise, Qt will + // call deleteLater() on it, which causes a double free + // since the shared_ptr in view_docks_ doesn't know + // that Qt keeps a pointer to the view around + entry.second->setParent(0); + + // Remove this entry from the container + view_docks_.erase(entry.first); + } } + QMainWindow *window = session_windows_.at(session); + session_selector_.removeTab(session_selector_.indexOf(window)); + + session_windows_.erase(session); + sessions_.remove_if([&](shared_ptr s) { return s == session; }); @@ -269,6 +283,8 @@ void MainWindow::setup_ui() { setObjectName(QString::fromUtf8("MainWindow")); + setCentralWidget(&session_selector_); + // Set the window icon QIcon icon; icon.addFile(QString(":/icons/sigrok-logo-notext.png")); diff --git a/pv/mainwindow.hpp b/pv/mainwindow.hpp index ddad6705..e9cecfb1 100644 --- a/pv/mainwindow.hpp +++ b/pv/mainwindow.hpp @@ -26,6 +26,7 @@ #include #include +#include #include "session.hpp" #include "views/viewbase.hpp" @@ -120,6 +121,10 @@ private: std::map< std::shared_ptr, std::shared_ptr > view_docks_; + std::map< std::shared_ptr, QMainWindow*> session_windows_; + + QTabWidget session_selector_; + QAction *const action_view_sticky_scrolling_; QAction *const action_view_coloured_bg_; QAction *const action_about_;