Mantid
Loading...
Searching...
No Matches
QSettingsChangeAware.h
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2026 ISIS Rutherford Appleton Laboratory UKRI,
4// NScD Oak Ridge National Laboratory, European Spallation Source,
5// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
6// SPDX - License - Identifier: GPL - 3.0 +
7#pragma once
8
9#include <QSettings>
10#include <QString>
11#include <QVariant>
12#include <memory>
13
15
27public:
29 explicit QSettingsChangeAware(QSettings &settings) : m_settings(settings) {}
30
33
37 bool setValue(QString const &key, QVariant const &value) {
38 if (m_settings.contains(key) && valuesEqual(m_settings.value(key), value))
39 return false;
40
41 m_settings.setValue(key, value);
42 m_changed = true;
43 return true;
44 }
45
49 bool remove(QString const &key) {
50 if (!containsKeyOrChildren(key))
51 return false;
52
53 m_settings.remove(key);
54 m_changed = true;
55 return true;
56 }
57
58 [[nodiscard]] bool changed() const noexcept { return m_changed; }
59
60private:
61 static bool valuesEqual(QVariant current, QVariant const &requested) {
62 if (current == requested)
63 return true;
64 if (!current.isValid() || !requested.isValid())
65 return false;
66
67 if (!current.convert(requested.metaType()))
68 return false;
69 return current == requested;
70 }
71
72 bool containsKeyOrChildren(QString const &key) {
73 if (m_settings.contains(key))
74 return true;
75 if (key.isEmpty())
76 return !m_settings.allKeys().isEmpty();
77
78 m_settings.beginGroup(key);
79 auto const hasChildren = !m_settings.allKeys().isEmpty();
80 m_settings.endGroup();
81 return hasChildren;
82 }
83
84 std::unique_ptr<QSettings> m_ownedSettings;
85 QSettings &m_settings;
86 bool m_changed{false};
87};
88
89} // namespace MantidQt::MantidWidgets
double value
The value of the point.
Definition FitMW.cpp:51
A QSettings facade that applies only effective changes.
QSettingsChangeAware(QSettingsChangeAware const &)=delete
bool remove(QString const &key)
Remove a key or group only when it contains a value.
static bool valuesEqual(QVariant current, QVariant const &requested)
QSettingsChangeAware & operator=(QSettingsChangeAware const &)=delete
bool setValue(QString const &key, QVariant const &value)
Set a value only when the effective stored value differs.
STL namespace.