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
void Settings::accept()
{
+ GlobalSettings settings;
+ settings.stop_tracking();
+
QDialog::accept();
}
void Settings::reject()
{
+ GlobalSettings settings;
+ settings.undo_tracked_changes();
+
QDialog::reject();
}
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()
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
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
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