Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
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 <QtCore/qdebug.h>
7#include <QtCore/qdir.h>
8#include <QtCore/qfileinfo.h>
9#include <QtCore/qset.h>
10#if QT_CONFIG(settings)
11#include <QtCore/qsettings.h>
12#endif
13#include <QtCore/qstandardpaths.h>
14
15using namespace Qt::StringLiterals;
16
18{
19 if (defaultValue.isValid()) {
20 m_values[name] = defaultValue;
21 }
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 Q_UNUSED(settingsFilePath);
43 return false;
44#endif
45}
46
48{
49#if QT_CONFIG(settings)
50 const QString path = QFileInfo(u".%1.ini"_s.arg(m_toolName)).absoluteFilePath();
51
53 for (auto it = m_values.constBegin(); it != m_values.constEnd(); ++it) {
54 settings.setValue(it.key(), it.value().isNull() ? QString() : it.value());
55 }
56
57 settings.sync();
58
60 qWarning() << "Failed to write default settings to" << path
61 << "Error:" << settings.status();
62 return false;
63 }
64
65 qInfo() << "Wrote default settings to" << path;
66 return true;
67#else
68 return false;
69#endif
70}
71
73{
74#if QT_CONFIG(settings)
75 QFileInfo fileInfo(path);
76 QDir dir(fileInfo.isDir() ? path : fileInfo.dir());
77
78 QSet<QString> dirs;
79
80 const QString settingsFileName = u".%1.ini"_s.arg(m_toolName);
81
82 while (dir.exists() && dir.isReadable()) {
83 const QString dirPath = dir.absolutePath();
84
85 if (m_seenDirectories.contains(dirPath)) {
86 const QString cachedIniPath = m_seenDirectories[dirPath];
87 if (cachedIniPath.isEmpty())
88 return false;
89
90 return read(cachedIniPath);
91 }
92
93 dirs << dirPath;
94
95 const QString iniFile = dir.absoluteFilePath(settingsFileName);
96
97 if (read(iniFile)) {
98 for (const QString &dir : std::as_const(dirs))
99 m_seenDirectories[dir] = iniFile;
100 return true;
101 }
102
103 if (!dir.cdUp())
104 break;
105 }
106
107 if (const QString iniFile = QStandardPaths::locate(QStandardPaths::GenericConfigLocation, u"%1.ini"_s.arg(m_toolName));
108 !iniFile.isEmpty()) {
109 if (read(iniFile)) {
110 for (const QString &dir : std::as_const(dirs))
111 m_seenDirectories[dir] = iniFile;
112 return true;
113 }
114 }
115
116 // No INI file found anywhere, record the failure so we won't have to traverse the entire
117 // filesystem again
118 for (const QString &dir : std::as_const(dirs))
119 m_seenDirectories[dir] = QString();
120
121#endif
122 Q_UNUSED(path);
123 return false;
124}
125
127{
128 return m_values.value(name);
129}
130
132{
133 if (!m_values.contains(name))
134 return false;
135
136 QVariant variant = m_values[name];
137
138 // Unset is encoded as an empty string
139 return !(variant.canConvert(QMetaType(QMetaType::QString)) && variant.toString().isEmpty());
140}
\inmodule QtCore
Definition qdir.h:20
QString absoluteFilePath() const
bool isDir() const
Returns true if this object points to a directory or to a symbolic link to a directory.
QDir dir() const
Returns a QDir object representing the path of the parent directory of the file system entry that thi...
bool exists() const
Returns true if the file system entry this QFileInfo refers to 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:1219
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:1215
bool contains(const Key &key) const noexcept
Returns true if the hash contains an item with the key; otherwise returns false.
Definition qhash.h:1007
T value(const Key &key) const noexcept
Definition qhash.h:1054
\inmodule QtCore
Definition qmetatype.h:341
bool search(const QString &path)
bool isSet(QString name) const
void addOption(const QString &name, const QVariant defaultValue=QVariant())
QVariant value(QString name) const
\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:129
bool isEmpty() const noexcept
Returns true if the string has no characters; otherwise returns false.
Definition qstring.h:192
QString arg(qlonglong a, int fieldwidth=0, int base=10, QChar fillChar=u' ') const
Definition qstring.cpp:8870
\inmodule QtCore
Definition qvariant.h:65
bool isValid() const
Returns true if the storage type of this variant is not QMetaType::UnknownType; otherwise returns fal...
Definition qvariant.h:714
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:345
QSet< QString >::iterator it
EGLOutputLayerEXT EGLint EGLAttrib value
[5]
#define qInfo
Definition qlogging.h:165
#define qWarning
Definition qlogging.h:166
GLuint64 key
GLuint name
GLsizei const GLchar *const * path
static void allKeys(HKEY parentHandle, const QString &rSubKey, NameSet *result, REGSAM access=0)
#define Q_UNUSED(x)
QSettings settings("MySoft", "Star Runner")
[0]
QVariant variant
[1]
QString dir
[11]
char * toString(const MyType &t)
[31]