From: Soeren Apel Date: Thu, 19 Jan 2017 21:01:43 +0000 (+0100) Subject: Confirm with user when trying to close sessions with unsaved data X-Git-Tag: pulseview-0.4.0~205 X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=5ccfc97e20bbea19b9bc37905dd4cf63ee1f6303;p=pulseview.git Confirm with user when trying to close sessions with unsaved data --- diff --git a/pv/dialogs/storeprogress.cpp b/pv/dialogs/storeprogress.cpp index bd99cbad..b9c87ee7 100644 --- a/pv/dialogs/storeprogress.cpp +++ b/pv/dialogs/storeprogress.cpp @@ -21,6 +21,8 @@ #include +#include "pv/session.hpp" + #include "storeprogress.hpp" using std::map; @@ -42,6 +44,8 @@ StoreProgress::StoreProgress(const QString &file_name, { connect(&session_, SIGNAL(progress_updated()), this, SLOT(on_progress_updated())); + connect(&session_, SIGNAL(store_successful()), + &session, SLOT(on_data_saved())); } StoreProgress::~StoreProgress() diff --git a/pv/mainwindow.cpp b/pv/mainwindow.cpp index 8aaa1635..4aeecb26 100644 --- a/pv/mainwindow.cpp +++ b/pv/mainwindow.cpp @@ -477,8 +477,20 @@ std::shared_ptr MainWindow::get_tab_session(int index) const void MainWindow::closeEvent(QCloseEvent *event) { - save_ui_settings(); - event->accept(); + bool data_saved = true; + + for (auto entry : session_windows_) + if (!entry.first->data_saved()) + data_saved = false; + + if (!data_saved && (QMessageBox::question(this, tr("Confirmation"), + tr("There is unsaved data. Close anyway?"), + QMessageBox::Yes | QMessageBox::No) == QMessageBox::No)) { + event->ignore(); + } else { + save_ui_settings(); + event->accept(); + } } QMenu* MainWindow::createPopupMenu() @@ -681,11 +693,13 @@ void MainWindow::on_tab_changed(int index) void MainWindow::on_tab_close_requested(int index) { - // TODO Ask user if this is intended in case data is unsaved - shared_ptr session = get_tab_session(index); - if (session) + assert(session); + + if (session->data_saved() || (QMessageBox::question(this, tr("Confirmation"), + tr("This session contains unsaved data. Close it anyway?"), + QMessageBox::Yes | QMessageBox::No) == QMessageBox::Yes)) remove_session(session); } diff --git a/pv/session.cpp b/pv/session.cpp index 2e98c8f7..fcd0e135 100644 --- a/pv/session.cpp +++ b/pv/session.cpp @@ -105,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) { } @@ -172,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; @@ -851,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.")); } @@ -1064,4 +1077,9 @@ void Session::data_feed_in(shared_ptr device, } } +void Session::on_data_saved() +{ + data_saved_ = true; +} + } // namespace pv diff --git a/pv/session.hpp b/pv/session.hpp index bc1fbb0d..0f067a47 100644 --- a/pv/session.hpp +++ b/pv/session.hpp @@ -119,6 +119,11 @@ public: void set_main_bar(std::shared_ptr main_bar); + /** + * Indicates whether the captured data was saved to disk already or not + */ + bool data_saved() const; + void save_settings(QSettings &settings) const; void restore_settings(QSettings &settings); @@ -218,6 +223,7 @@ private: std::thread sampling_thread_; bool out_of_memory_; + bool data_saved_; Q_SIGNALS: void capture_state_changed(int state); @@ -237,6 +243,9 @@ Q_SIGNALS: void add_view(const QString &title, views::ViewType type, Session *session); + +public Q_SLOTS: + void on_data_saved(); }; } // namespace pv diff --git a/pv/storesession.cpp b/pv/storesession.cpp index 4167bdae..eaa17ea5 100644 --- a/pv/storesession.cpp +++ b/pv/storesession.cpp @@ -300,6 +300,7 @@ void StoreSession::store_proc(vector< shared_ptr > achannel_li // Zeroing the progress variables indicates completion units_stored_ = unit_count_ = 0; + store_successful(); progress_updated(); output_.reset(); diff --git a/pv/storesession.hpp b/pv/storesession.hpp index 23e25ec8..e89b3fd2 100644 --- a/pv/storesession.hpp +++ b/pv/storesession.hpp @@ -83,6 +83,8 @@ private: Q_SIGNALS: void progress_updated(); + void store_successful(); + private: const std::string file_name_; const std::shared_ptr output_format_;