]> sigrok.org Git - pulseview.git/commitdiff
Main(Window): Let the UI and sessions be restored after w.show()
authorSoeren Apel <redacted>
Fri, 9 Jun 2017 19:57:21 +0000 (21:57 +0200)
committerSoeren Apel <redacted>
Fri, 9 Jun 2017 19:57:21 +0000 (21:57 +0200)
main.cpp
pv/mainwindow.cpp
pv/mainwindow.hpp

index e5472c2a0b14e9eacf32d380223019dd007947a7..ce7db5fadcc2a18a05d567a0ac284cfe7065595f 100644 (file)
--- a/main.cpp
+++ b/main.cpp
@@ -177,10 +177,17 @@ int main(int argc, char *argv[])
                        pv::DeviceManager device_manager(context);
 
                        // Initialise the main window
-                       pv::MainWindow w(device_manager, open_file, open_file_format,
-                               restore_sessions);
+                       pv::MainWindow w(device_manager);
                        w.show();
 
+                       if (restore_sessions)
+                               w.restore_sessions();
+
+                       if (!open_file.empty())
+                               w.add_session_with_file(open_file, open_file_format);
+                       else
+                               w.add_default_session();
+
 #ifdef ENABLE_SIGNALS
                        if (SignalHandler::prepare_signals()) {
                                SignalHandler *const handler =
index 9a3f197d4c2174edd3a9b814b9b340d1fc695fce..786d07e28987d1fe7af3943447eb4ddd492687fa 100644 (file)
@@ -68,9 +68,7 @@ using toolbars::MainBar;
 
 const QString MainWindow::WindowTitle = tr("PulseView");
 
-MainWindow::MainWindow(DeviceManager &device_manager,
-       string open_file_name, string open_file_format,
-       bool restore_sessions, QWidget *parent) :
+MainWindow::MainWindow(DeviceManager &device_manager, QWidget *parent) :
        QMainWindow(parent),
        device_manager_(device_manager),
        session_selector_(this),
@@ -92,32 +90,7 @@ MainWindow::MainWindow(DeviceManager &device_manager,
                bind(&MainWindow::on_settingViewShowAnalogMinorGrid_changed, this, _1));
 
        setup_ui();
-       restore_ui_settings(restore_sessions);
-
-       if (!open_file_name.empty()) {
-               shared_ptr<Session> session = add_session();
-               session->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->select_device(other_device ? other_device : demo_device);
-       }
+       restore_ui_settings();
 }
 
 MainWindow::~MainWindow()
@@ -347,6 +320,78 @@ void MainWindow::remove_session(shared_ptr<Session> session)
        }
 }
 
+void MainWindow::add_session_with_file(string open_file_name,
+       string open_file_format)
+{
+       shared_ptr<Session> session = add_session();
+       session->load_init_file(open_file_name, open_file_format);
+}
+
+void MainWindow::add_default_session()
+{
+       // Only add the default session if there would be no session otherwise
+       if (sessions_.size() > 0)
+               return;
+
+       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->select_device(other_device ? other_device : demo_device);
+}
+
+void MainWindow::save_sessions()
+{
+       QSettings settings;
+       int id = 0;
+
+       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.setValue("sessions", id);
+}
+
+void MainWindow::restore_sessions()
+{
+       QSettings settings;
+       int i, session_count;
+
+       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::setup_ui()
 {
        setObjectName(QString::fromUtf8("MainWindow"));
@@ -438,35 +483,14 @@ void MainWindow::setup_ui()
 void MainWindow::save_ui_settings()
 {
        QSettings settings;
-       int id = 0;
 
        settings.beginGroup("MainWindow");
        settings.setValue("state", saveState());
        settings.setValue("geometry", saveGeometry());
        settings.endGroup();
-
-       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.setValue("sessions", id);
 }
 
-void MainWindow::restore_ui_settings(bool restore_sessions)
+void MainWindow::restore_ui_settings()
 {
        QSettings settings;
 
@@ -479,19 +503,6 @@ void MainWindow::restore_ui_settings(bool restore_sessions)
                resize(1000, 720);
 
        settings.endGroup();
-
-       if (restore_sessions) {
-               int i, session_count;
-
-               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();
-               }
-       }
 }
 
 shared_ptr<Session> MainWindow::get_tab_session(int index) const
@@ -518,6 +529,7 @@ void MainWindow::closeEvent(QCloseEvent *event)
                event->ignore();
        } else {
                save_ui_settings();
+               save_sessions();
                event->accept();
        }
 }
index ab0f871e924ad7d90c7203ab91a502f32ea3344d..71cad57817a0554ad832b8710e0dc39dbe8fcc91 100644 (file)
@@ -70,9 +70,6 @@ private:
 
 public:
        explicit MainWindow(DeviceManager &device_manager,
-               string open_file_name = string(),
-               string open_file_format = string(),
-               bool restore_sessions = true,
                QWidget *parent = nullptr);
 
        ~MainWindow();
@@ -88,12 +85,18 @@ public:
 
        void remove_session(shared_ptr<Session> session);
 
+       void add_session_with_file(string open_file_name, string open_file_format);
+
+       void add_default_session();
+
+       void save_sessions();
+       void restore_sessions();
+
 private:
        void setup_ui();
 
        void save_ui_settings();
-
-       void restore_ui_settings(bool restore_sessions);
+       void restore_ui_settings();
 
        shared_ptr<Session> get_tab_session(int index) const;