From: Soeren Apel Date: Fri, 25 Nov 2016 17:42:38 +0000 (+0100) Subject: Move file loading from MainBar to Session X-Git-Tag: pulseview-0.4.0~220 X-Git-Url: https://sigrok.org/gitweb/?p=pulseview.git;a=commitdiff_plain;h=fd22c71c1a9cc470b53c71c0ee131a4b2d645f80 Move file loading from MainBar to Session --- diff --git a/pv/mainwindow.cpp b/pv/mainwindow.cpp index 1738f9b1..f777bfd6 100644 --- a/pv/mainwindow.cpp +++ b/pv/mainwindow.cpp @@ -87,7 +87,7 @@ MainWindow::MainWindow(DeviceManager &device_manager, if (!open_file_name.empty()) { shared_ptr 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); } } diff --git a/pv/session.cpp b/pv/session.cpp index 2d7918b8..773cf804 100644 --- a/pv/session.cpp +++ b/pv/session.cpp @@ -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 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 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 input_format; + + if (!format.empty()) { + const map > formats = + device_manager_.context()->input_formats(); + const auto iter = find_if(formats.begin(), formats.end(), + [&](const pair > 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 format, + const std::map &options) +{ + const QString errorMessage( + QString("Failed to load file %1").arg(file_name)); + + try { + if (format) + set_device(shared_ptr( + new devices::InputFile( + device_manager_.context(), + file_name.toStdString(), + format, options))); + else + set_device(shared_ptr( + 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 lock(sampling_mutex_); diff --git a/pv/session.hpp b/pv/session.hpp index 95a50c21..a764c7c3 100644 --- a/pv/session.hpp +++ b/pv/session.hpp @@ -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 main_view() const; - void set_main_bar(std::shared_ptr main_bar); - std::shared_ptr main_bar() const; + void set_main_bar(std::shared_ptr 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 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 format = nullptr, + const std::map &options = + std::map()); + capture_state get_capture_state() const; void start_capture(std::function error_handler); diff --git a/pv/toolbars/mainbar.cpp b/pv/toolbars/mainbar.cpp index c31d0d33..b670b75a 100644 --- a/pv/toolbars/mainbar.cpp +++ b/pv/toolbars/mainbar.cpp @@ -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 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 input_format; - - DeviceManager& device_manager = session_.device_manager(); - - if (!format.empty()) { - const map > formats = - device_manager.context()->input_formats(); - const auto iter = find_if(formats.begin(), formats.end(), - [&](const pair > 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 format, - const std::map &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( - new devices::InputFile( - device_manager.context(), - file_name.toStdString(), - format, options))); - else - session_.set_device(shared_ptr( - 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 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(); } diff --git a/pv/toolbars/mainbar.hpp b/pv/toolbars/mainbar.hpp index e5a7adce..95959e17 100644 --- a/pv/toolbars/mainbar.hpp +++ b/pv/toolbars/mainbar.hpp @@ -88,11 +88,6 @@ public: void reset_device_selector(); - void select_device(std::shared_ptr 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 format = nullptr, - const std::map &options = - std::map()); - 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_;