Mantid
Loading...
Searching...
No Matches
QSettingsHelper.h
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2019 ISIS Rutherford Appleton Laboratory UKRI,
4// NScD Oak Ridge National Laboratory, European Spallation Source
5// & Institut Laue - Langevin
6// SPDX - License - Identifier: GPL - 3.0 +
7#pragma once
8
9#include <QMetaType>
10#include <QSettings>
11#include <QString>
12#include <QStringList>
13#include <QVariant>
14#include <map>
15#include <string>
16#include <typeinfo>
17
18namespace MantidQt {
19namespace MantidWidgets {
20namespace QSettingsHelper {
21
29template <typename T> T getSetting(std::string const &settingGroup, std::string const &settingName) {
30 QSettings settings;
31 settings.beginGroup(QString::fromStdString(settingGroup));
32 auto const settingValue = settings.value(QString::fromStdString(settingName));
33 settings.endGroup();
34
35 return settingValue.value<T>();
36}
37
45template <typename T> std::map<std::string, T> getSettingsAsMap(std::string const &settingGroup) {
46 std::map<std::string, T> settingsMap;
47 QSettings settings;
48 settings.beginGroup(QString::fromStdString(settingGroup));
49 QStringList settingNames = settings.allKeys();
50 std::string templateTypeName = typeid(T).name();
51 for (auto &settingName : settingNames) {
52 if (settingName.endsWith("/type")) {
53 std::string settingTypeName = settings.value(settingName).toString().toStdString();
54 if (settingTypeName == templateTypeName) {
55 auto settingValueName = settingName.replace(QString("/type"), QString("/value"));
56 auto setting = settings.value(settingValueName);
57 auto strippedSettingName = settingName.remove(QString("/value"));
58 settingsMap[strippedSettingName.toStdString()] = setting.value<T>();
59 }
60 }
61 }
62 settings.endGroup();
63
64 return settingsMap;
65}
66
75template <typename T> void setSetting(std::string const &settingGroup, std::string const &settingName, T const &value) {
76 QSettings settings;
77 settings.beginGroup(QString::fromStdString(settingGroup));
78 settings.setValue(QString::fromStdString(settingName).append("/value"), value);
79 settings.setValue(QString::fromStdString(settingName).append("/type"), typeid(value).name());
80 settings.endGroup();
81}
82
83} // namespace QSettingsHelper
84} // namespace MantidWidgets
85} // namespace MantidQt
double value
The value of the point.
Definition: FitMW.cpp:51
std::map< std::string, T > getSettingsAsMap(std::string const &settingGroup)
Loads a map of settings with the same type.
T getSetting(std::string const &settingGroup, std::string const &settingName)
Loads an individual setting from disk.
void setSetting(std::string const &settingGroup, std::string const &settingName, T const &value)
Sets the value of a specified setting.
The AlgorithmProgressDialogPresenter keeps track of the running algorithms and displays a progress ba...