From: Soeren Apel Date: Tue, 7 Mar 2017 21:14:49 +0000 (+0100) Subject: GlobalSettings: Implement undo function for the "cancel" button X-Git-Tag: pulseview-0.4.0~165 X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=2cca9ebfdc367aa8e4651cf063f0cdea8fccb798;p=pulseview.git GlobalSettings: Implement undo function for the "cancel" button --- diff --git a/pv/dialogs/settings.cpp b/pv/dialogs/settings.cpp index 8cb8d67d..d443935e 100644 --- a/pv/dialogs/settings.cpp +++ b/pv/dialogs/settings.cpp @@ -45,6 +45,10 @@ Settings::Settings(QWidget *parent) : connect(button_box, SIGNAL(accepted()), this, SLOT(accept())); connect(button_box, SIGNAL(rejected()), this, SLOT(reject())); + + // Start to record changes + GlobalSettings settings; + settings.start_tracking(); } QWidget *Settings::get_view_settings_form(QWidget *parent) const @@ -76,11 +80,17 @@ QWidget *Settings::get_view_settings_form(QWidget *parent) const void Settings::accept() { + GlobalSettings settings; + settings.stop_tracking(); + QDialog::accept(); } void Settings::reject() { + GlobalSettings settings; + settings.undo_tracked_changes(); + QDialog::reject(); } diff --git a/pv/globalsettings.cpp b/pv/globalsettings.cpp index 5da89ceb..d2482241 100644 --- a/pv/globalsettings.cpp +++ b/pv/globalsettings.cpp @@ -25,6 +25,8 @@ const QString GlobalSettings::Key_View_AlwaysZoomToFit = "View_AlwaysZoomToFit"; const QString GlobalSettings::Key_View_ColouredBG = "View_ColouredBG"; std::multimap< QString, std::function > GlobalSettings::callbacks_; +bool GlobalSettings::tracking_ = false; +std::map GlobalSettings::tracked_changes_; GlobalSettings::GlobalSettings() : QSettings() @@ -40,6 +42,11 @@ void GlobalSettings::register_change_handler(const QString key, void GlobalSettings::setValue(const QString &key, const QVariant &value) { + // Save previous value if we're tracking changes, + // not altering an already-existing saved setting + if (tracking_) + tracked_changes_.emplace(key, QSettings::value(key)); + QSettings::setValue(key, value); // Call all registered callbacks for this key @@ -49,5 +56,26 @@ void GlobalSettings::setValue(const QString &key, const QVariant &value) it->second(value); } +void GlobalSettings::start_tracking() +{ + tracking_ = true; + tracked_changes_.clear(); +} + +void GlobalSettings::stop_tracking() +{ + tracking_ = false; + tracked_changes_.clear(); +} + +void GlobalSettings::undo_tracked_changes() +{ + tracking_ = false; + + for (auto entry : tracked_changes_) + setValue(entry.first, entry.second); + + tracked_changes_.clear(); +} } // namespace pv diff --git a/pv/globalsettings.hpp b/pv/globalsettings.hpp index b7457963..9581e294 100644 --- a/pv/globalsettings.hpp +++ b/pv/globalsettings.hpp @@ -45,8 +45,29 @@ public: void setValue(const QString& key, const QVariant& value); + /** + * Begins the tracking of changes. All changes will + * be recorded until stop_tracking() is called. + * The change tracking is global and doesn't support nesting. + */ + void start_tracking(); + + /** + * Ends the tracking of changes without any changes to the settings. + */ + void stop_tracking(); + + /** + * Ends the tracking of changes, undoing the changes since the + * change tracking began. + */ + void undo_tracked_changes(); + private: static std::multimap< QString, std::function > callbacks_; + + static bool tracking_; + static std::map tracked_changes_; }; } // namespace pv