]> sigrok.org Git - pulseview.git/commitdiff
Move file loading from MainBar to Session
authorSoeren Apel <redacted>
Fri, 25 Nov 2016 17:42:38 +0000 (18:42 +0100)
committerSoeren Apel <redacted>
Sun, 4 Dec 2016 13:37:38 +0000 (14:37 +0100)
pv/mainwindow.cpp
pv/session.cpp
pv/session.hpp
pv/toolbars/mainbar.cpp
pv/toolbars/mainbar.hpp

index 1738f9b16cfea0adfcfa65d6018c0bbffab68bf4..f777bfd6ca932da01450ef000c2e3aad0bd1a7cf 100644 (file)
@@ -87,7 +87,7 @@ MainWindow::MainWindow(DeviceManager &device_manager,
 
        if (!open_file_name.empty()) {
                shared_ptr<Session> session = add_session();
-               session->main_bar()->load_init_file(open_file_name, open_file_format);
+               session->load_init_file(open_file_name, open_file_format);
        }
 
        // Add empty default session if there aren't any sessions
@@ -107,8 +107,7 @@ MainWindow::MainWindow(DeviceManager &device_manager,
                }
 
                // ...and if there isn't any, just use demo then
-               session->main_bar()->select_device(other_device ?
-                       other_device : demo_device);
+               session->select_device(other_device ? other_device : demo_device);
        }
 }
 
index 2d7918b87c9ef347c7ca4392b8555d4d44203a38..773cf8047956d9cd062d5e3d65b3c2e11ecd0c7e 100644 (file)
@@ -46,6 +46,7 @@
 #include "data/decode/decoder.hpp"
 
 #include "devices/hardwaredevice.hpp"
+#include "devices/inputfile.hpp"
 #include "devices/sessionfile.hpp"
 
 #include "toolbars/mainbar.hpp"
@@ -72,6 +73,7 @@ using std::lock_guard;
 using std::list;
 using std::map;
 using std::mutex;
+using std::pair;
 using std::recursive_mutex;
 using std::set;
 using std::shared_ptr;
@@ -86,8 +88,10 @@ using sigrok::ConfigKey;
 using sigrok::DatafeedCallbackFunction;
 using sigrok::Error;
 using sigrok::Header;
+using sigrok::InputFormat;
 using sigrok::Logic;
 using sigrok::Meta;
+using sigrok::OutputFormat;
 using sigrok::Packet;
 using sigrok::PacketPayload;
 using sigrok::Session;
@@ -349,6 +353,19 @@ void Session::restore_settings(QSettings &settings)
        }
 }
 
+void Session::select_device(shared_ptr<devices::Device> device)
+{
+       try {
+               if (device)
+                       set_device(device);
+               else
+                       set_default_device();
+       } catch (const QString &e) {
+               main_bar_->session_error(tr("Failed to Select Device"),
+                       tr("Failed to Select Device"));
+       }
+}
+
 void Session::set_device(shared_ptr<devices::Device> device)
 {
        assert(device);
@@ -422,6 +439,63 @@ void Session::set_default_device()
        set_device((iter == devices.end()) ? devices.front() : *iter);
 }
 
+void Session::load_init_file(const std::string &file_name,
+       const std::string &format)
+{
+       shared_ptr<InputFormat> input_format;
+
+       if (!format.empty()) {
+               const map<string, shared_ptr<InputFormat> > formats =
+                       device_manager_.context()->input_formats();
+               const auto iter = find_if(formats.begin(), formats.end(),
+                       [&](const pair<string, shared_ptr<InputFormat> > f) {
+                               return f.first == format; });
+               if (iter == formats.end()) {
+                       main_bar_->session_error(tr("Error"),
+                               tr("Unexpected input format: %s").arg(QString::fromStdString(format)));
+                       return;
+               }
+
+               input_format = (*iter).second;
+       }
+
+       load_file(QString::fromStdString(file_name), input_format);
+}
+
+void Session::load_file(QString file_name,
+       std::shared_ptr<sigrok::InputFormat> format,
+       const std::map<std::string, Glib::VariantBase> &options)
+{
+       const QString errorMessage(
+               QString("Failed to load file %1").arg(file_name));
+
+       try {
+               if (format)
+                       set_device(shared_ptr<devices::Device>(
+                               new devices::InputFile(
+                                       device_manager_.context(),
+                                       file_name.toStdString(),
+                                       format, options)));
+               else
+                       set_device(shared_ptr<devices::Device>(
+                               new devices::SessionFile(
+                                       device_manager_.context(),
+                                       file_name.toStdString())));
+       } catch (Error e) {
+               main_bar_->session_error(tr("Failed to load ") + file_name, e.what());
+               set_default_device();
+               main_bar_->update_device_list();
+               return;
+       }
+
+       main_bar_->update_device_list();
+
+       start_capture([&, errorMessage](QString infoMessage) {
+               main_bar_->session_error(errorMessage, infoMessage); });
+
+       set_name(QFileInfo(file_name).fileName());
+}
+
 Session::capture_state Session::get_capture_state() const
 {
        lock_guard<mutex> lock(sampling_mutex_);
index 95a50c21c73a098e4ab89f1e3060843d90d1e071..a764c7c3f1f52367fde2c803952dd697180e482d 100644 (file)
@@ -51,8 +51,10 @@ namespace sigrok {
 class Analog;
 class Channel;
 class Device;
+class InputFormat;
 class Logic;
 class Meta;
+class OutputFormat;
 class Packet;
 class Session;
 }
@@ -114,14 +116,19 @@ public:
 
        std::shared_ptr<views::ViewBase> main_view() const;
 
-       void set_main_bar(std::shared_ptr<pv::toolbars::MainBar> main_bar);
-
        std::shared_ptr<pv::toolbars::MainBar> main_bar() const;
 
+       void set_main_bar(std::shared_ptr<pv::toolbars::MainBar> main_bar);
+
        void save_settings(QSettings &settings) const;
 
        void restore_settings(QSettings &settings);
 
+       /**
+        * Attempts to set device instance, may fall back to demo if needed
+        */
+       void select_device(std::shared_ptr<devices::Device> device);
+
        /**
         * Sets device instance that will be used in the next capture session.
         */
@@ -129,6 +136,14 @@ public:
 
        void set_default_device();
 
+       void load_init_file(const std::string &file_name,
+               const std::string &format);
+
+       void load_file(QString file_name,
+               std::shared_ptr<sigrok::InputFormat> format = nullptr,
+               const std::map<std::string, Glib::VariantBase> &options =
+                       std::map<std::string, Glib::VariantBase>());
+
        capture_state get_capture_state() const;
 
        void start_capture(std::function<void (const QString)> error_handler);
index c31d0d33860021fe11fb2eacb020c3535a3a8de9..b670b75a6c41a2da6a117e4e7a55a205414f46a6 100644 (file)
@@ -64,7 +64,6 @@ using std::list;
 using std::map;
 using std::max;
 using std::min;
-using std::pair;
 using std::shared_ptr;
 using std::string;
 using std::vector;
@@ -343,47 +342,6 @@ void MainBar::reset_device_selector()
        device_selector_.reset();
 }
 
-void MainBar::select_device(shared_ptr<devices::Device> device)
-{
-       try {
-               if (device)
-                       session_.set_device(device);
-               else
-                       session_.set_default_device();
-       } catch (const QString &e) {
-               QMessageBox msg(this);
-               msg.setText(e);
-               msg.setInformativeText(tr("Failed to Select Device"));
-               msg.setStandardButtons(QMessageBox::Ok);
-               msg.setIcon(QMessageBox::Warning);
-               msg.exec();
-       }
-}
-
-void MainBar::load_init_file(const std::string &file_name,
-       const std::string &format)
-{
-       shared_ptr<InputFormat> input_format;
-
-       DeviceManager& device_manager = session_.device_manager();
-
-       if (!format.empty()) {
-               const map<string, shared_ptr<InputFormat> > formats =
-                       device_manager.context()->input_formats();
-               const auto iter = find_if(formats.begin(), formats.end(),
-                       [&](const pair<string, shared_ptr<InputFormat> > f) {
-                               return f.first == format; });
-               if (iter == formats.end()) {
-                       cerr << "Unexpected input format: " << format << endl;
-                       return;
-               }
-
-               input_format = (*iter).second;
-       }
-
-       load_file(QString::fromStdString(file_name), input_format);
-}
-
 QAction* MainBar::action_open() const
 {
        return action_open_;
@@ -429,42 +387,6 @@ QAction* MainBar::action_view_show_cursors() const
        return action_view_show_cursors_;
 }
 
-void MainBar::load_file(QString file_name,
-       std::shared_ptr<sigrok::InputFormat> format,
-       const std::map<std::string, Glib::VariantBase> &options)
-{
-       DeviceManager& device_manager = session_.device_manager();
-
-       const QString errorMessage(
-               QString("Failed to load file %1").arg(file_name));
-
-       try {
-               if (format)
-                       session_.set_device(shared_ptr<devices::Device>(
-                               new devices::InputFile(
-                                       device_manager.context(),
-                                       file_name.toStdString(),
-                                       format, options)));
-               else
-                       session_.set_device(shared_ptr<devices::Device>(
-                               new devices::SessionFile(
-                                       device_manager.context(),
-                                       file_name.toStdString())));
-       } catch (Error e) {
-               show_session_error(tr("Failed to load ") + file_name, e.what());
-               session_.set_default_device();
-               update_device_list();
-               return;
-       }
-
-       update_device_list();
-
-       session_.start_capture([&, errorMessage](QString infoMessage) {
-               session_error(errorMessage, infoMessage); });
-
-       session_.set_name(QFileInfo(file_name).fileName());
-}
-
 void MainBar::update_sample_rate_selector()
 {
        Glib::VariantContainerBase gvar_dict;
@@ -871,7 +793,7 @@ void MainBar::import_file(shared_ptr<InputFormat> format)
                options = dlg.options();
        }
 
-       load_file(file_name, format, options);
+       session_.load_file(file_name, format, options);
 
        const QString abs_path = QFileInfo(file_name).absolutePath();
        settings.setValue(SettingOpenDirectory, abs_path);
@@ -885,7 +807,7 @@ void MainBar::on_device_selected()
                return;
        }
 
-       select_device(device);
+       session_.select_device(device);
 }
 
 void MainBar::on_device_changed()
@@ -929,7 +851,7 @@ void MainBar::on_actionOpen_triggered()
                        "All Files (*.*)"));
 
        if (!file_name.isEmpty()) {
-               load_file(file_name);
+               session_.load_file(file_name);
 
                const QString abs_path = QFileInfo(file_name).absolutePath();
                settings.setValue(SettingOpenDirectory, abs_path);
@@ -956,7 +878,7 @@ void MainBar::on_actionConnect_triggered()
        // If the user selected a device, select it in the device list. Select the
        // current device otherwise.
        if (dlg.exec())
-               select_device(dlg.get_selected_device());
+               session_.select_device(dlg.get_selected_device());
 
        update_device_list();
 }
index e5a7adce5483969690015e655606ef09b69318be..95959e1757212ae7d45738a60cdf649ddee9f02a 100644 (file)
@@ -88,11 +88,6 @@ public:
 
        void reset_device_selector();
 
-       void select_device(std::shared_ptr<devices::Device> device);
-
-       void load_init_file(const std::string &file_name,
-               const std::string &format);
-
        QAction* action_new_view() const;
        QAction* action_open() const;
        QAction* action_save_as() const;
@@ -105,16 +100,13 @@ public:
        QAction* action_view_zoom_one_to_one() const;
        QAction* action_view_show_cursors() const;
 
+       void session_error(const QString text, const QString info_text);
+
 private:
        void run_stop();
 
        void select_init_device();
 
-       void load_file(QString file_name,
-               std::shared_ptr<sigrok::InputFormat> format = nullptr,
-               const std::map<std::string, Glib::VariantBase> &options =
-                       std::map<std::string, Glib::VariantBase>());
-
        void save_selection_to_file();
 
        void update_sample_rate_selector();
@@ -124,8 +116,6 @@ private:
        void commit_sample_rate();
        void commit_sample_count();
 
-       void session_error(const QString text, const QString info_text);
-
        QAction *const action_new_view_;
        QAction *const action_open_;
        QAction *const action_save_as_;