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//----------------------------------
12
13#include <QSettings>
14#include <QStringList>
15#include <utility>
16
17using namespace MantidQt::API;
18
19//----------------------------------
20// Public member functions
21//----------------------------------
22
27 : m_lastInput(), m_previousDirectory(""), m_algorithmsGroup(settingsGroup), m_dirKey("LastDirectory") {
28 // Fill the stored map from the QSettings information
29 load();
30}
31
36 // Can't write the history out here since, in Linux, the singletons are
37 // destroyed after
38 // the QApplication object and then we get a crash
39}
40
48void AbstractAlgorithmInputHistory::storeNewValue(const QString &algName, const QPair<QString, QString> &property) {
49 m_lastInput[algName][property.first] = property.second;
50}
51
56 if (m_lastInput.contains(algName))
57 m_lastInput[algName].clear();
58}
59
65QString AbstractAlgorithmInputHistory::previousInput(const QString &algName, const QString &propName) const {
66 if (!m_lastInput.contains(algName))
67 return "";
68
69 if (m_lastInput.value(algName).contains(propName))
70 return m_lastInput.value(algName).value(propName);
71 else
72 return "";
73}
74
82
86
91 QSettings settings;
92 this->writeSettings(settings);
93}
94
95void AbstractAlgorithmInputHistory::readSettings(const QSettings &storage) {
96 // unfortunately QSettings does not allow const when using beginGroup and
97 // endGroup
98 m_lastInput.clear();
99 const_cast<QSettings &>(storage).beginGroup(m_algorithmsGroup);
100 // QStringList algorithms = settings.childGroups();
101 QListIterator<QString> algNames(storage.childGroups());
102
103 // Each property is a key of the algorithm group
104 while (algNames.hasNext()) {
105 QHash<QString, QString> algorithmProperties;
106 QString group = algNames.next();
107 const_cast<QSettings &>(storage).beginGroup(group);
108 QListIterator<QString> properties(storage.childKeys());
109 while (properties.hasNext()) {
110 QString propName = properties.next();
111 QString value = storage.value(propName).toString();
112 if (!value.isEmpty())
113 algorithmProperties.insert(propName, value);
114 }
115 m_lastInput.insert(group, algorithmProperties);
116 const_cast<QSettings &>(storage).endGroup();
117 }
118
119 // The previous dir
120 m_previousDirectory = storage.value(m_dirKey).toString();
121
122 const_cast<QSettings &>(storage).endGroup();
123}
124
125void AbstractAlgorithmInputHistory::writeSettings(QSettings &storage) const {
126 storage.beginGroup(m_algorithmsGroup);
127 QHashIterator<QString, QHash<QString, QString>> inputHistory(m_lastInput);
128 while (inputHistory.hasNext()) {
129 inputHistory.next();
130 storage.beginGroup(inputHistory.key());
131 // Remove all keys for this group that exist at the moment
132 storage.remove("");
133 QHash<QString, QString>::const_iterator iend = inputHistory.value().end();
134 for (QHash<QString, QString>::const_iterator itr = inputHistory.value().begin(); itr != iend; ++itr) {
135 storage.setValue(itr.key(), itr.value());
136 }
137 storage.endGroup();
138 }
139
140 // Store the previous directory
141 storage.setValue(m_dirKey, m_previousDirectory);
142
143 storage.endGroup();
144}
145
146//----------------------------------
147// Private member functions
148//----------------------------------
149
155 QSettings settings;
156 this->readSettings(settings);
157}
double value
The value of the point.
Definition: FitMW.cpp:51
void save() const
Save the values stored here to persistent storage.
void setPreviousDirectory(const QString &lastdir)
Set the directory that was accessed when the previous open file dialog was used.
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 load()
Load any values that are available from persistent storage.
void writeSettings(QSettings &storage) const override
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 readSettings(const QSettings &storage) override
AbstractAlgorithmInputHistory(const AbstractAlgorithmInputHistory &)=delete
QString previousInput(const QString &algName, const QString &propName) const
Retrieve an old parameter value.
void storeNewValue(const QString &algName, const QPair< QString, QString > &property)
Update the old values that are stored here.
virtual ~AbstractAlgorithmInputHistory()=0
Abstract destructor.