Qt 6.x
The Qt SDK
Loading...
Searching...
No Matches
qqmltoolingsettings.cpp
Go to the documentation of this file.
1// Copyright (C) 2021 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
3
5
6#include <algorithm>
7
8#include <QtCore/qdebug.h>
9#include <QtCore/qdir.h>
10#include <QtCore/qfileinfo.h>
11#include <QtCore/qset.h>
12#if QT_CONFIG(settings)
13#include <QtCore/qsettings.h>
14#endif
15#include <QtCore/qstandardpaths.h>
16
17using namespace Qt::StringLiterals;
18
20{
21 m_values[name] = defaultValue;
22}
23
24bool QQmlToolingSettings::read(const QString &settingsFilePath)
25{
26#if QT_CONFIG(settings)
27 if (!QFileInfo::exists(settingsFilePath))
28 return false;
29
30 if (m_currentSettingsPath == settingsFilePath)
31 return true;
32
33 QSettings settings(settingsFilePath, QSettings::IniFormat);
34
35 for (const QString &key : settings.allKeys())
36 m_values[key] = settings.value(key).toString();
37
38 m_currentSettingsPath = settingsFilePath;
39
40 return true;
41#else
42 return false;
43#endif
44}
45
47{
48#if QT_CONFIG(settings)
49 const QString path = QFileInfo(u".%1.ini"_s.arg(m_toolName)).absoluteFilePath();
50
52 for (auto it = m_values.constBegin(); it != m_values.constEnd(); ++it) {
53 settings.setValue(it.key(), it.value().isNull() ? QString() : it.value());
54 }
55
56 settings.sync();
57
59 qWarning() << "Failed to write default settings to" << path
60 << "Error:" << settings.status();
61 return false;
62 }
63
64 qInfo() << "Wrote default settings to" << path;
65 return true;
66#else
67 return false;
68#endif
69}
70
72{
73#if QT_CONFIG(settings)
74 QFileInfo fileInfo(path);
75 QDir dir(fileInfo.isDir() ? path : fileInfo.dir());
76
77 QSet<QString> dirs;
78
79 const QString settingsFileName = u".%1.ini"_s.arg(m_toolName);
80
81 while (dir.exists() && dir.isReadable()) {
82 const QString dirPath = dir.absolutePath();
83
84 if (m_seenDirectories.contains(dirPath)) {
85 const QString cachedIniPath = m_seenDirectories[dirPath];
86 if (cachedIniPath.isEmpty())
87 return false;
88
89 return read(cachedIniPath);
90 }
91
92 dirs << dirPath;
93
94 const QString iniFile = dir.absoluteFilePath(settingsFileName);
95
96 if (read(iniFile)) {
97 for (const QString &dir : std::as_const(dirs))
98 m_seenDirectories[dir] = iniFile;
99 return true;
100 }
101
102 if (!dir.cdUp())
103 break;
104 }
105
106 if (const QString iniFile = QStandardPaths::locate(QStandardPaths::GenericConfigLocation, u"%1.ini"_s.arg(m_toolName));
107 !iniFile.isEmpty()) {
108 if (read(iniFile)) {
109 for (const QString &dir : std::as_const(dirs))
110 m_seenDirectories[dir] = iniFile;
111 return true;
112 }
113 }
114
115 // No INI file found anywhere, record the failure so we won't have to traverse the entire
116 // filesystem again
117 for (const QString &dir : std::as_const(dirs))
118 m_seenDirectories[dir] = QString();
119
120#endif
121 return false;
122}
123
125{
126 return m_values.value(name);
127}
128
130{
131 if (!m_values.contains(name))
132 return false;
133
134 QVariant variant = m_values[name];
135
136 // Unset is encoded as an empty string
137 return !(variant.canConvert(QMetaType(QMetaType::QString)) && variant.toString().isEmpty());
138}
\inmodule QtCore
Definition qdir.h:19
\inmodule QtCore \reentrant
Definition qfileinfo.h:22
QString absoluteFilePath() const
Returns an absolute path including the file name.
bool isDir() const
Returns true if this object points to a directory or to a symbolic link to a directory.
QDir dir() const
Returns the path of the object's parent directory as a QDir object.
bool exists() const
Returns true if the file exists; otherwise returns false.
const_iterator constEnd() const noexcept
Returns a const \l{STL-style iterators}{STL-style iterator} pointing to the imaginary item after the ...
Definition qhash.h:1209
const_iterator constBegin() const noexcept
Returns a const \l{STL-style iterators}{STL-style iterator} pointing to the first item in the hash.
Definition qhash.h:1205
bool contains(const Key &key) const noexcept
Returns true if the hash contains an item with the key; otherwise returns false.
Definition qhash.h:991
T value(const Key &key) const noexcept
Definition qhash.h:1044
\inmodule QtCore
Definition qmetatype.h:320
bool search(const QString &path)
bool isSet(QString name) const
void addOption(const QString &name, const QVariant defaultValue=QVariant())
QVariant value(QString name) const
Definition qset.h:18
\inmodule QtCore
Definition qsettings.h:30
void setValue(QAnyStringView key, const QVariant &value)
Sets the value of setting key to value.
void sync()
Writes any unsaved changes to permanent storage, and reloads any settings that have been changed in t...
Status status() const
Returns a status code indicating the first error that was met by QSettings, or QSettings::NoError if ...
static QString locate(StandardLocation type, const QString &fileName, LocateOptions options=LocateFile)
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:127
QString arg(qlonglong a, int fieldwidth=0, int base=10, QChar fillChar=u' ') const
Definition qstring.cpp:8606
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition qstring.h:1083
\inmodule QtCore
Definition qvariant.h:64
QString toString() const
Returns the variant as a QString if the variant has a userType() including, but not limited to:
bool canConvert(QMetaType targetType) const
Definition qvariant.h:342
QSet< QString >::iterator it
EGLOutputLayerEXT EGLint EGLAttrib value
[5]
#define qInfo
Definition qlogging.h:161
#define qWarning
Definition qlogging.h:162
GLuint64 key
GLuint name
GLsizei const GLchar *const * path
static void allKeys(HKEY parentHandle, const QString &rSubKey, NameSet *result, REGSAM access=0)
QSettings settings("MySoft", "Star Runner")
[0]
QVariant variant
[1]
QString dir
[11]
char * toString(const MyType &t)
[31]