Mantid
Loading...
Searching...
No Matches
AlgorithmInputHistory.cpp
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2018 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//----------------------------------
8// Includes
9//----------------------------------
13
14#include <QSettings>
15#include <utility>
16
17using namespace MantidQt::API;
18
19//----------------------------------
20// Public member functions
21//----------------------------------
22
24 : m_lastInput(std::move(lastInput)), m_previousDirectory(std::move(previousDirectory)) {}
25
29
31
36 : m_lastInput(), m_previousDirectory(""), m_algorithmsGroup(settingsGroup), m_dirKey("LastDirectory") {
37 // Fill the stored map from the QSettings information
39}
40
45 // Can't write the history out here since, in Linux, the singletons are
46 // destroyed after
47 // the QApplication object and then we get a crash
48}
49
57void AbstractAlgorithmInputHistory::storeNewValue(const QString &algName, const std::pair<QString, QString> &property) {
58 m_lastInput[algName][property.first] = property.second;
59}
60
65 if (m_lastInput.contains(algName))
66 m_lastInput[algName].clear();
67}
68
74QString AbstractAlgorithmInputHistory::previousInput(const QString &algName, const QString &propName) const {
75 if (!m_lastInput.contains(algName))
76 return "";
77
78 if (m_lastInput.value(algName).contains(propName))
79 return m_lastInput.value(algName).value(propName);
80 else
81 return "";
82}
83
91
95
100 QSettings settings;
101 saveSettings(settings, captureSettings());
102}
103
105 auto prefix = m_algorithmsGroup;
106 if (!prefix.isEmpty() && !prefix.endsWith('/'))
107 prefix.append('/');
108
110 QString previousDirectory;
111 for (auto const &qualifiedName : storage.allKeys()) {
112 if (!qualifiedName.startsWith(prefix))
113 continue;
114 auto const relativeName = qualifiedName.mid(prefix.size());
115 if (relativeName == m_dirKey) {
116 previousDirectory = storage.value(qualifiedName).toString();
117 continue;
118 }
119 auto const separator = relativeName.indexOf('/');
120 if (separator <= 0 || relativeName.indexOf('/', separator + 1) >= 0)
121 continue;
122 auto const value = storage.value(qualifiedName).toString();
123 if (!value.isEmpty())
124 inputHistory[relativeName.left(separator)].insert(relativeName.mid(separator + 1), value);
125 }
126 return AlgorithmInputHistorySettings(std::move(inputHistory), std::move(previousDirectory));
127}
128
133
137
139 const AlgorithmInputHistorySettings &settings) const {
141 auto prefix = m_algorithmsGroup;
142 if (!prefix.isEmpty() && !prefix.endsWith('/'))
143 prefix.append('/');
144 QHashIterator<QString, QHash<QString, QString>> inputHistory(settings.lastInput());
145 while (inputHistory.hasNext()) {
146 inputHistory.next();
147 auto const algorithmName = prefix + inputHistory.key();
148 auto const algorithmPrefix = algorithmName + '/';
149 auto const &algorithmSettings = inputHistory.value();
150 for (auto const &storedKey : storage.allKeys()) {
151 auto const settingName = storedKey.mid(algorithmPrefix.size());
152 if (storedKey == algorithmName ||
153 (storedKey.startsWith(algorithmPrefix) && !algorithmSettings.contains(settingName)))
154 writer.remove(storedKey);
155 }
156 for (auto itr = algorithmSettings.cbegin(); itr != algorithmSettings.cend(); ++itr)
157 writer.setValue(algorithmPrefix + itr.key(), itr.value());
158 }
159 writer.setValue(prefix + m_dirKey, settings.previousDirectory());
160}
161
162//----------------------------------
163// Private member functions
164//----------------------------------
165
double value
The value of the point.
Definition FitMW.cpp:51
void save() const
Save the values stored here to persistent storage.
void initializeSettings()
Load any values that are available from persistent storage.
void setPreviousDirectory(const QString &lastdir)
Set the directory that was accessed when the previous open file dialog was used.
virtual ~AbstractAlgorithmInputHistory() override=0
Abstract destructor.
AlgorithmInputHistorySettings captureSettings() const override
Capture current history without persistent I/O.
QHash< QString, QHash< QString, QString > > m_lastInput
A map indexing the algorithm name and a list of property name:value pairs.
QString m_previousDirectory
The directory that last used by an open file dialog.
void restoreSettings(const AlgorithmInputHistorySettings &settings) override
Apply an already-read snapshot without persistent I/O.
QString m_algorithmsGroup
The string denoting the group (in the QSettings) where the algorithm properties are stored.
QString m_dirKey
The string denoting the key for the previous dir storage.
const QString & getPreviousDirectory() const
Get the directory that was accessed when the previous open file dialog was used.
void clearAlgorithmInput(const QString &algName)
Clear values for a particular algorithm.
void saveSettings(QSettings &storage, const AlgorithmInputHistorySettings &settings) const
Persist an explicit snapshot.
void storeNewValue(const QString &algName, const std::pair< QString, QString > &property)
Update the old values that are stored here.
AbstractAlgorithmInputHistory(const AbstractAlgorithmInputHistory &)=delete
QString previousInput(const QString &algName, const QString &propName) const
Retrieve an old parameter value.
AlgorithmInputHistorySettings readSettings(const QSettings &storage) const
Query persistent storage without changing the history.
AlgorithmInputHistorySettings(InputHistory lastInput={}, QString previousDirectory={})
A QSettings facade that applies only effective changes.
bool remove(QString const &key)
Remove a key or group only when it contains a value.
bool setValue(QString const &key, QVariant const &value)
Set a value only when the effective stored value differs.
STL namespace.