X-Git-Url: https://sigrok.org/gitweb/?p=pulseview.git;a=blobdiff_plain;f=pv%2Fsession.cpp;h=97741ebf7b944a4c6507bd1a4e64c1d8128a5d5b;hp=2d7918b87c9ef347c7ca4392b8555d4d44203a38;hb=26a883ede0bcf68d087eda5dd2082890d36c7aef;hpb=2d25fc473d6dc5d578f45f8d6bb061639afec405 diff --git a/pv/session.cpp b/pv/session.cpp index 2d7918b8..97741ebf 100644 --- a/pv/session.cpp +++ b/pv/session.cpp @@ -14,8 +14,7 @@ * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * along with this program; if not, see . */ #ifdef _WIN32 @@ -46,6 +45,7 @@ #include "data/decode/decoder.hpp" #include "devices/hardwaredevice.hpp" +#include "devices/inputfile.hpp" #include "devices/sessionfile.hpp" #include "toolbars/mainbar.hpp" @@ -72,6 +72,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 +87,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; @@ -102,7 +105,8 @@ Session::Session(DeviceManager &device_manager, QString name) : default_name_(name), name_(name), capture_state_(Stopped), - cur_samplerate_(0) + cur_samplerate_(0), + data_saved_(true) { } @@ -169,6 +173,11 @@ shared_ptr Session::main_bar() const return main_bar_; } +bool Session::data_saved() const +{ + return data_saved_; +} + void Session::save_settings(QSettings &settings) const { map dev_info; @@ -349,6 +358,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 +444,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_); @@ -778,6 +857,13 @@ void Session::sample_thread_proc(function error_handler) assert(0); } + // We now have unsaved data unless we just "captured" from a file + shared_ptr file_device = + dynamic_pointer_cast(device_); + + if (!file_device) + data_saved_ = false; + if (out_of_memory_) error_handler(tr("Out of memory, acquisition stopped.")); } @@ -841,8 +927,6 @@ void Session::feed_in_logic(shared_ptr logic) { lock_guard lock(data_mutex_); - const size_t sample_count = logic->data_length() / logic->unit_size(); - if (!logic_data_) { // The only reason logic_data_ would not have been created is // if it was not possible to determine the signals when the @@ -856,8 +940,7 @@ void Session::feed_in_logic(shared_ptr logic) // Create a new data segment cur_logic_segment_ = shared_ptr( - new data::LogicSegment( - logic, cur_samplerate_, sample_count)); + new data::LogicSegment(logic, cur_samplerate_)); logic_data_->push_segment(cur_logic_segment_); // @todo Putting this here means that only listeners querying @@ -902,8 +985,7 @@ void Session::feed_in_analog(shared_ptr analog) // Create a segment, keep it in the maps of channels segment = shared_ptr( - new data::AnalogSegment( - cur_samplerate_, sample_count)); + new data::AnalogSegment(cur_samplerate_)); cur_analog_segments_[channel] = segment; // Find the analog data associated with the channel @@ -991,4 +1073,9 @@ void Session::data_feed_in(shared_ptr device, } } +void Session::on_data_saved() +{ + data_saved_ = true; +} + } // namespace pv