]> sigrok.org Git - pulseview.git/blobdiff - pv/mainwindow.cpp
MainWindow: Don't return bogus view
[pulseview.git] / pv / mainwindow.cpp
index f8463257b5b882531692abc3c0ab4803efdcfa64..92c92d023225a7d5fa7cb5f54764d4e1218b2a12 100644 (file)
@@ -38,6 +38,7 @@
 
 #include "devicemanager.hpp"
 #include "util.hpp"
+#include "devices/hardwaredevice.hpp"
 #include "dialogs/about.hpp"
 #include "toolbars/mainbar.hpp"
 #include "view/view.hpp"
@@ -46,6 +47,7 @@
 #include <stdarg.h>
 #include <libsigrokcxx/libsigrokcxx.hpp>
 
+using std::dynamic_pointer_cast;
 using std::list;
 using std::make_shared;
 using std::map;
@@ -60,14 +62,13 @@ 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) :
        QMainWindow(parent),
        device_manager_(device_manager),
-       session_(device_manager),
-       open_file_name_(open_file_name),
-       open_file_format_(open_file_format),
        action_view_sticky_scrolling_(new QAction(this)),
        action_view_coloured_bg_(new QAction(this)),
        action_about_(new QAction(this))
@@ -76,6 +77,32 @@ MainWindow::MainWindow(DeviceManager &device_manager,
 
        setup_ui();
        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()
@@ -92,7 +119,10 @@ MainWindow::~MainWindow()
                dock->setWidget(0);
 
                const std::shared_ptr<pv::view::View> view = entry.second;
-               session_.deregister_view(view);
+
+               for (shared_ptr<Session> session : sessions_)
+                       if (session->has_view(view))
+                               session->deregister_view(view);
        }
 }
 
@@ -133,7 +163,7 @@ shared_ptr<pv::view::View> MainWindow::get_active_view() const
                if (entry.first.get() == dock)
                        return entry.second;
 
-       return shared_ptr<pv::view::View>();
+       return nullptr;
 }
 
 shared_ptr<pv::view::View> MainWindow::add_view(const QString &title,
@@ -158,36 +188,81 @@ shared_ptr<pv::view::View> 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<QAbstractButton*>
+                               ("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(),
                                SLOT(trigger_event(util::Timestamp)));
-                       connect(v.get(), SIGNAL(sticky_scrolling_changed(bool)), this,
-                               SLOT(sticky_scrolling_changed(bool)));
-                       connect(v.get(), SIGNAL(always_zoom_to_fit_changed(bool)), this,
-                               SLOT(always_zoom_to_fit_changed(bool)));
 
                        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,
-                                       open_file_name_, open_file_format_);
+                               main_bar = make_shared<MainBar>(session, *this);
                                dock_main->addToolBar(main_bar.get());
                                session.set_main_bar(main_bar);
 
-                               open_file_name_.clear();
-                               open_file_format_.clear();
+                               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());
+
+                       connect(v.get(), SIGNAL(always_zoom_to_fit_changed(bool)),
+                               main_bar.get(), SLOT(on_always_zoom_to_fit_changed(bool)));
                }
        }
 
        return v;
 }
 
+shared_ptr<Session> MainWindow::add_session()
+{
+       int id = sessions_.size();
+       QString name = tr("Untitled-%1").arg(id + 1);
+
+       shared_ptr<Session> session = make_shared<Session>(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<view::View> main_view =
+               add_view(name, pv::view::TraceView, *session);
+
+       return session;
+}
+
+void MainWindow::remove_session(shared_ptr<Session> session)
+{
+       for (shared_ptr<view::View> 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<Session> 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"));
@@ -214,52 +289,48 @@ void MainWindow::setup_ui()
        action_about_->setObjectName(QString::fromUtf8("actionAbout"));
        action_about_->setText(tr("&About..."));
 
-       // Set up the initial view
-       shared_ptr<view::View> main_view =
-               add_view(tr("Untitled"), pv::view::TraceView, session_);
+       setDockNestingEnabled(true);
 
-       // Set the title
-       setWindowTitle(tr("PulseView"));
+       connect(static_cast<QApplication *>(QCoreApplication::instance()),
+               SIGNAL(focusChanged(QWidget*, QWidget*)),
+               this, SLOT(on_focus_changed()));
 }
 
 void MainWindow::save_ui_settings()
 {
        QSettings settings;
-
-       map<string, string> dev_info;
-       list<string> key_list;
+       int id = 0;
 
        settings.beginGroup("MainWindow");
        settings.setValue("state", saveState());
        settings.setValue("geometry", saveGeometry());
        settings.endGroup();
 
-       if (session_.device()) {
-               settings.beginGroup("Device");
-               key_list.push_back("vendor");
-               key_list.push_back("model");
-               key_list.push_back("version");
-               key_list.push_back("serial_num");
-               key_list.push_back("connection_id");
-
-               dev_info = device_manager_.get_device_info(
-                       session_.device());
-
-               for (string key : key_list) {
-                       if (dev_info.count(key))
-                               settings.setValue(QString::fromUtf8(key.c_str()),
-                                               QString::fromUtf8(dev_info.at(key).c_str()));
-                       else
-                               settings.remove(QString::fromUtf8(key.c_str()));
+       for (shared_ptr<Session> session : sessions_) {
+               // Ignore sessions using the demo device or no device at all
+               if (session->device()) {
+                       shared_ptr<devices::HardwareDevice> device =
+                               dynamic_pointer_cast< devices::HardwareDevice >
+                               (session->device());
+
+                       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.endGroup();
        }
+
+       settings.setValue("sessions", id);
 }
 
 void MainWindow::restore_ui_settings()
 {
        QSettings settings;
+       int i, session_count;
 
        settings.beginGroup("MainWindow");
 
@@ -270,6 +341,15 @@ void MainWindow::restore_ui_settings()
                resize(1000, 720);
 
        settings.endGroup();
+
+       session_count = settings.value("sessions", 0).toInt();
+
+       for (i = 0; i < session_count; i++) {
+               settings.beginGroup("Session" + QString::number(i));
+               shared_ptr<Session> session = add_session();
+               session->restore_settings(settings);
+               settings.endGroup();
+       }
 }
 
 void MainWindow::closeEvent(QCloseEvent *event)
@@ -294,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<Session> s : sessions_)
+               if (s.get() == session)
+                       add_view(title, type, *s);
+}
+
+void MainWindow::on_focus_changed()
+{
+       shared_ptr<view::View> view;
+       bool title_set = false;
+
+       view = get_active_view();
+
+       for (shared_ptr<Session> 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<Session*>(QObject::sender());
+       assert(session);
+
+       for (shared_ptr<view::View> 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<Session> 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<QDockWidget*>(w);
+           if (dock)
+               break;
+           w = w->parent();
+       }
+
+       // Get the view contained in the dock widget
+       shared_ptr<view::View> view;
+
+       for (auto entry : view_docks_)
+               if (entry.first.get() == 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()) {
+                       remove_session(session);
+                       break;
+               } else
+                       session->deregister_view(view);
+       }
+}
+
 void MainWindow::on_actionViewStickyScrolling_triggered()
 {
        shared_ptr<pv::view::View> view = get_active_view();