+void MainWindow::session_error(const QString text, const QString info_text)
+{
+ QMetaObject::invokeMethod(this, "show_session_error",
+ Qt::QueuedConnection, Q_ARG(QString, text),
+ Q_ARG(QString, info_text));
+}
+
+void MainWindow::show_session_error(const QString text, const QString info_text)
+{
+ QMessageBox msg(this);
+ msg.setText(text);
+ msg.setInformativeText(info_text);
+ msg.setStandardButtons(QMessageBox::Ok);
+ msg.setIcon(QMessageBox::Warning);
+ msg.exec();
+}
+
+void MainWindow::on_add_view(const QString &title, views::ViewType type,
+ Session *session)
+{
+ // We get a pointer and need a reference
+ for (std::shared_ptr<Session> s : sessions_)
+ if (s.get() == session)
+ add_view(title, type, *s);
+}
+
+void MainWindow::on_focus_changed()
+{
+ shared_ptr<views::ViewBase> view = get_active_view();
+
+ if (view) {
+ for (shared_ptr<Session> session : sessions_) {
+ if (session->has_view(view)) {
+ if (session != last_focused_session_) {
+ // Activate correct tab if necessary
+ shared_ptr<Session> tab_session = get_tab_session(
+ session_selector_.currentIndex());
+ if (tab_session != session)
+ session_selector_.setCurrentWidget(
+ session_windows_.at(session));
+
+ on_focused_session_changed(session);
+ }
+
+ break;
+ }
+ }
+ }
+
+ if (sessions_.empty())
+ setWindowTitle(WindowTitle);
+}
+
+void MainWindow::on_focused_session_changed(shared_ptr<Session> session)
+{
+ last_focused_session_ = session;
+
+ setWindowTitle(session->name() + " - " + WindowTitle);
+
+ // Update the state of the run/stop button, too
+ on_capture_state_changed(session.get());
+}
+
+void MainWindow::on_new_session_clicked()
+{
+ add_session();
+}
+
+void MainWindow::on_run_stop_clicked()
+{
+ shared_ptr<Session> session = last_focused_session_;
+
+ if (!session)
+ return;
+
+ switch (session->get_capture_state()) {
+ case Session::Stopped:
+ session->start_capture([&](QString message) {
+ session_error("Capture failed", message); });
+ break;
+ case Session::AwaitingTrigger:
+ case Session::Running:
+ session->stop_capture();
+ break;
+ }
+}
+
+void MainWindow::on_session_name_changed()
+{
+ // Update the corresponding dock widget's name(s)
+ Session *session = qobject_cast<Session*>(QObject::sender());
+ assert(session);
+
+ for (shared_ptr<views::ViewBase> 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());
+ }
+ }
+
+ // Update the tab widget by finding the main window and the tab from that
+ for (auto entry : session_windows_)
+ if (entry.first.get() == session) {
+ QMainWindow *window = entry.second;
+ const int index = session_selector_.indexOf(window);
+ session_selector_.setTabText(index, session->name());
+ }
+
+ // Refresh window title if the affected session has focus
+ if (session == last_focused_session_.get())
+ setWindowTitle(session->name() + " - " + WindowTitle);
+}
+
+void MainWindow::on_capture_state_changed(QObject *obj)
+{
+ Session *caller = qobject_cast<Session*>(obj);
+
+ // Ignore if caller is not the currently focused session
+ // unless there is only one session
+ if ((sessions_.size() > 1) && (caller != last_focused_session_.get()))
+ return;
+
+ int state = caller->get_capture_state();
+
+ const QIcon *icons[] = {&icon_grey_, &icon_red_, &icon_green_};
+ run_stop_button_->setIcon(*icons[state]);
+ run_stop_button_->setText((state == pv::Session::Stopped) ?
+ tr("Run") : tr("Stop"));
+}
+
+void MainWindow::on_new_view(Session *session)
+{
+ // We get a pointer and need a reference
+ for (std::shared_ptr<Session> s : sessions_)
+ if (s.get() == session)
+ add_view(session->name(), views::ViewTypeTrace, *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<QDockWidget*>(w);
+ if (dock)
+ break;
+ w = w->parent();
+ }
+
+ // Get the view contained in the dock widget
+ shared_ptr<views::ViewBase> view;
+
+ for (auto entry : view_docks_)
+ if (entry.first == dock)
+ view = entry.second;
+
+ // Deregister the view
+ for (shared_ptr<Session> session : sessions_) {
+ if (!session->has_view(view))
+ continue;
+
+ // Also destroy the entire session if its main view is closing...
+ if (view == session->main_view()) {
+ // ...but only if data is saved or the user confirms closing
+ if (session->data_saved() || (QMessageBox::question(this, tr("Confirmation"),
+ tr("This session contains unsaved data. Close it anyway?"),
+ QMessageBox::Yes | QMessageBox::No) == QMessageBox::Yes))
+ remove_session(session);
+ break;
+ } else
+ // All other views can be closed at any time as no data will be lost
+ remove_view(view);
+ }
+}
+
+void MainWindow::on_tab_changed(int index)
+{
+ shared_ptr<Session> session = get_tab_session(index);
+
+ if (session)
+ on_focused_session_changed(session);
+}
+
+void MainWindow::on_tab_close_requested(int index)
+{
+ shared_ptr<Session> session = get_tab_session(index);
+
+ assert(session);
+
+ if (session->data_saved() || (QMessageBox::question(this, tr("Confirmation"),
+ tr("This session contains unsaved data. Close it anyway?"),
+ QMessageBox::Yes | QMessageBox::No) == QMessageBox::Yes))
+ remove_session(session);
+}
+