]> sigrok.org Git - pulseview.git/commitdiff
GlobalSettings: Implement undo function for the "cancel" button
authorSoeren Apel <redacted>
Tue, 7 Mar 2017 21:14:49 +0000 (22:14 +0100)
committerUwe Hermann <redacted>
Tue, 7 Mar 2017 22:31:41 +0000 (23:31 +0100)
pv/dialogs/settings.cpp
pv/globalsettings.cpp
pv/globalsettings.hpp

index 8cb8d67de31ad58958464bb1f27e72afa665cddd..d443935efb765e2c88553b3540f6d6dc90e2f785 100644 (file)
@@ -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();
 }
 
index 5da89ceba030bf66df5757d4ef0a318a5dc61735..d24822413038c5a63be439c1d1038b53fd49d1e4 100644 (file)
@@ -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<void(QVariant)> > GlobalSettings::callbacks_;
+bool GlobalSettings::tracking_ = false;
+std::map<QString, QVariant> 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
index b7457963da88e462cdaa1a2e8202f5ae2a919fb0..9581e294ba658518e393f7e5966324e9a92f3433 100644 (file)
@@ -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<void(QVariant)> > callbacks_;
+
+       static bool tracking_;
+       static std::map<QString, QVariant> tracked_changes_;
 };
 
 } // namespace pv