+ restore_ui_settings();
+
+ if (!open_file_name.empty()) {
+ shared_ptr<Session> session = add_session();
+ session->main_bar()->load_init_file(open_file_name, open_file_format);
+ }
+
+ // Add empty default session if there aren't any sessions
+ if (sessions_.size() == 0) {
+ shared_ptr<Session> session = add_session();
+
+ map<string, string> dev_info;
+ shared_ptr<devices::HardwareDevice> other_device, demo_device;
+
+ // Use any available device that's not demo
+ for (shared_ptr<devices::HardwareDevice> dev : device_manager_.devices()) {
+ if (dev->hardware_device()->driver()->name() == "demo") {
+ demo_device = dev;
+ } else {
+ other_device = dev;
+ }
+ }
+
+ // ...and if there isn't any, just use demo then
+ session->main_bar()->select_device(other_device ?
+ other_device : demo_device);
+ }
+}
+
+MainWindow::~MainWindow()
+{
+ while (!sessions_.empty())
+ remove_session(sessions_.front());
+}
+
+QAction* MainWindow::action_view_sticky_scrolling() const
+{
+ return action_view_sticky_scrolling_;
+}
+
+QAction* MainWindow::action_view_coloured_bg() const
+{
+ return action_view_coloured_bg_;
+}
+
+QAction* MainWindow::action_about() const
+{
+ return action_about_;
+}
+
+shared_ptr<views::ViewBase> MainWindow::get_active_view() const
+{
+ // If there's only one view, use it...
+ if (view_docks_.size() == 1)
+ return view_docks_.begin()->second;
+
+ // ...otherwise find the dock widget the widget with focus is contained in
+ QObject *w = QApplication::focusWidget();
+ QDockWidget *dock = 0;
+
+ while (w) {
+ dock = qobject_cast<QDockWidget*>(w);
+ if (dock)
+ break;
+ w = w->parent();
+ }
+
+ // Get the view contained in the dock widget
+ for (auto entry : view_docks_)
+ if (entry.first == dock)
+ return entry.second;
+
+ return nullptr;
+}
+
+shared_ptr<views::ViewBase> 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) {
+ QDockWidget* dock = new QDockWidget(title, main_window);
+ dock->setObjectName(title);
+ main_window->addDockWidget(Qt::TopDockWidgetArea, dock);
+
+ // Insert a QMainWindow into the dock widget to allow for a tool bar
+ QMainWindow *dock_main = new QMainWindow(dock);
+ dock_main->setWindowFlags(Qt::Widget); // Remove Qt::Window flag
+
+ shared_ptr<views::TraceView::View> v =
+ make_shared<views::TraceView::View>(session, dock_main);
+ view_docks_[dock] = v;
+ session.register_view(v);
+
+ dock_main->setCentralWidget(v.get());
+ dock->setWidget(dock_main);
+
+ dock->setFeatures(QDockWidget::DockWidgetMovable |
+ QDockWidget::DockWidgetFloatable | QDockWidget::DockWidgetClosable);
+
+ QAbstractButton *close_btn =
+ dock->findChildren<QAbstractButton*>
+ ("qt_dockwidget_closebutton").front();
+
+ connect(close_btn, SIGNAL(clicked(bool)),
+ this, SLOT(on_view_close_clicked()));
+
+ if (type == views::ViewTypeTrace) {
+ connect(&session, SIGNAL(trigger_event(util::Timestamp)),
+ qobject_cast<views::ViewBase*>(v.get()),
+ SLOT(trigger_event(util::Timestamp)));
+
+ v->enable_sticky_scrolling(action_view_sticky_scrolling_->isChecked());
+ v->enable_coloured_bg(action_view_coloured_bg_->isChecked());
+
+ shared_ptr<MainBar> main_bar = session.main_bar();
+ if (!main_bar) {
+ main_bar = make_shared<MainBar>(session, *this);
+ dock_main->addToolBar(main_bar.get());
+ session.set_main_bar(main_bar);
+
+ connect(main_bar.get(), SIGNAL(new_view(Session*)),
+ this, SLOT(on_new_view(Session*)));
+ }
+ main_bar->action_view_show_cursors()->setChecked(v->cursors_shown());
+
+ connect(v.get(), SIGNAL(always_zoom_to_fit_changed(bool)),
+ main_bar.get(), SLOT(on_always_zoom_to_fit_changed(bool)));
+ }
+
+ return v;
+ }
+
+ return nullptr;
+}
+
+shared_ptr<Session> MainWindow::add_session()
+{
+ static int last_session_id = 1;
+ QString name = tr("Untitled-%1").arg(last_session_id++);
+
+ shared_ptr<Session> session = make_shared<Session>(device_manager_, name);
+
+ 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()));
+ session_state_mapper_.setMapping(session.get(), session.get());
+ connect(session.get(), SIGNAL(capture_state_changed(int)),
+ &session_state_mapper_, SLOT(map()));
+
+ sessions_.push_back(session);
+
+ QMainWindow *window = new QMainWindow();
+ window->setWindowFlags(Qt::Widget); // Remove Qt::Window flag
+ session_windows_[session] = window;
+
+ int index = session_selector_.addTab(window, name);
+ session_selector_.setCurrentIndex(index);
+
+ window->setDockNestingEnabled(true);
+
+ shared_ptr<views::ViewBase> main_view =
+ add_view(name, views::ViewTypeTrace, *session);
+
+ return session;
+}
+
+void MainWindow::remove_session(shared_ptr<Session> session)
+{
+ int h = new_session_button_->height();
+
+ for (shared_ptr<views::ViewBase> view : session->views()) {
+ // Find the dock the view is contained in and remove it
+ for (auto entry : view_docks_)
+ 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<Session> s) {
+ return s == session; });
+
+ if (sessions_.empty()) {
+ // When there are no more tabs, the height of the QTabWidget
+ // drops to zero. We must prevent this to keep the static
+ // widgets visible
+ for (QWidget *w : static_tab_widget_->findChildren<QWidget*>())
+ w->setMinimumHeight(h);
+
+ int margin = static_tab_widget_->layout()->contentsMargins().bottom();
+ static_tab_widget_->setMinimumHeight(h + 2 * margin);
+ session_selector_.setMinimumHeight(h + 2 * margin);
+
+ // Update the window title if there is no view left to
+ // generate focus change events
+ setWindowTitle(WindowTitle);