Move the "new session" button to the main window's tab area
[pulseview.git] / pv / mainwindow.cpp
index 775f9dec90a53d628271f5580a829b60b2027035..53ff35a9d427dcc108343e318281b3ecf5dc6d67 100644 (file)
@@ -31,6 +31,7 @@
 #include <QApplication>
 #include <QCloseEvent>
 #include <QDockWidget>
+#include <QHBoxLayout>
 #include <QSettings>
 #include <QWidget>
 
@@ -203,8 +204,6 @@ shared_ptr<views::ViewBase> MainWindow::add_view(const QString &title,
                                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*)));
                        }
@@ -239,6 +238,8 @@ shared_ptr<Session> MainWindow::add_session()
        session_windows_[session] = window;
        session_selector_.addTab(window, name);
 
+       window->setDockNestingEnabled(true);
+
        shared_ptr<views::ViewBase> main_view =
                add_view(name, views::ViewTypeTrace, *session);
 
@@ -307,7 +308,29 @@ void MainWindow::setup_ui()
        action_about_->setObjectName(QString::fromUtf8("actionAbout"));
        action_about_->setText(tr("&About..."));
 
-       setDockNestingEnabled(true);
+       // Set up the tab area
+       new_session_button_ = new QToolButton();
+       new_session_button_->setIcon(QIcon::fromTheme("document-new",
+               QIcon(":/icons/document-new.png")));
+       new_session_button_->setAutoRaise(true);
+
+       QHBoxLayout* layout = new QHBoxLayout();
+       layout->setContentsMargins(2, 2, 2, 2);
+       layout->addWidget(new_session_button_);
+
+       QWidget* static_tab_widget_ = new QWidget();
+       static_tab_widget_->setLayout(layout);
+
+       session_selector_.setCornerWidget(static_tab_widget_, Qt::TopLeftCorner);
+
+       session_selector_.setTabsClosable(true);
+
+       connect(new_session_button_, SIGNAL(clicked(bool)),
+               this, SLOT(on_new_session_clicked()));
+
+       connect(&session_selector_, SIGNAL(tabCloseRequested(int)),
+               this, SLOT(on_tab_close_requested(int)));
+
 
        connect(static_cast<QApplication *>(QCoreApplication::instance()),
                SIGNAL(focusChanged(QWidget*, QWidget*)),
@@ -420,7 +443,7 @@ void MainWindow::on_focus_changed()
                setWindowTitle(WindowTitle);
 }
 
-void MainWindow::on_new_session()
+void MainWindow::on_new_session_clicked()
 {
        add_session();
 }
@@ -486,6 +509,18 @@ void MainWindow::on_view_close_clicked()
        }
 }
 
+void MainWindow::on_tab_close_requested(int index)
+{
+       // TODO Ask user if this is intended in case data is unsaved
+
+       // Find the session that belongs to this main window and remove it
+       for (auto entry : session_windows_)
+               if (entry.second == session_selector_.widget(index)) {
+                       remove_session(entry.first);
+                       break;
+               }
+}
+
 void MainWindow::on_actionViewStickyScrolling_triggered()
 {
        shared_ptr<views::ViewBase> viewbase = get_active_view();