Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Toggle main menu visibility
Loading...
Searching...
No Matches
shared_settings.cpp
Go to the documentation of this file.
1
// Copyright (C) 2016 The Qt Company Ltd.
2
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
3
// Qt-Security score:significant reason:default
4
5
#
include
"shared_settings_p.h"
6
#
include
"grid_p.h"
7
#
include
"previewmanager_p.h"
8
#
include
"qdesigner_utils_p.h"
9
10
#
include
<
actioneditor_p
.
h
>
11
12
#
include
<
QtDesigner
/
abstractformeditor
.
h
>
13
#
include
<
QtDesigner
/
abstractsettings
.
h
>
14
15
#
include
<
QtCore
/
qstringlist
.
h
>
16
#
include
<
QtCore
/
qdir
.
h
>
17
#
include
<
QtCore
/
qvariant
.
h
>
18
#
include
<
QtCore
/
qcoreapplication
.
h
>
19
#
include
<
QtCore
/
qsize
.
h
>
20
21
QT_BEGIN_NAMESPACE
22
23
using
namespace
Qt::StringLiterals;
24
25
static
constexpr
auto
defaultGridKey
=
"defaultGrid"_L1
;
26
static
constexpr
auto
previewKey
=
"Preview"_L1
;
27
static
constexpr
auto
enabledKey
=
"Enabled"_L1
;
28
static
constexpr
auto
userDeviceSkinsKey
=
"UserDeviceSkins"_L1
;
29
static
constexpr
auto
zoomKey
=
"zoom"_L1
;
30
static
constexpr
auto
zoomEnabledKey
=
"zoomEnabled"_L1
;
31
static
constexpr
auto
deviceProfileIndexKey
=
"DeviceProfileIndex"_L1
;
32
static
constexpr
auto
deviceProfilesKey
=
"DeviceProfiles"_L1
;
33
static
constexpr
auto
formTemplatePathsKey
=
"FormTemplatePaths"_L1
;
34
static
constexpr
auto
formTemplateKey
=
"FormTemplate"_L1
;
35
static
constexpr
auto
newFormSizeKey
=
"NewFormSize"_L1
;
36
static
constexpr
auto
namingModeKey
=
"naming"_L1
;
37
static
constexpr
auto
underScoreNamingMode
=
"underscore"_L1
;
38
static
constexpr
auto
camelCaseNamingMode
=
"camelcase"_L1
;
39
40
static
bool
checkTemplatePath
(
const
QString &path,
bool
create)
41
{
42
QDir current(QDir::current());
43
if
(current.exists(path))
44
return
true
;
45
46
if
(!create)
47
return
false
;
48
49
if
(current.mkpath(path))
50
return
true
;
51
52
qdesigner_internal
::designerWarning(QCoreApplication::translate(
"QDesignerSharedSettings"
,
"The template path %1 could not be created."
).arg(path));
53
return
false
;
54
}
55
56
namespace
qdesigner_internal
{
57
58
QDesignerSharedSettings
::
QDesignerSharedSettings
(
QDesignerFormEditorInterface
*
core
)
59
:
m_settings
(
core
->
settingsManager
())
60
{
61
}
62
63
Grid
QDesignerSharedSettings
::
defaultGrid
()
const
64
{
65
Grid
grid
;
66
const
QVariantMap
defaultGridMap
67
=
m_settings
->
value
(
defaultGridKey
,
QVariantMap
()).
toMap
();
68
if
(!
defaultGridMap
.
isEmpty
())
69
grid
.
fromVariantMap
(
defaultGridMap
);
70
return
grid
;
71
}
72
73
void
QDesignerSharedSettings
::
setDefaultGrid
(
const
Grid
&
grid
)
74
{
75
m_settings
->
setValue
(
defaultGridKey
,
grid
.
toVariantMap
());
76
}
77
78
const
QStringList
&
QDesignerSharedSettings
::
defaultFormTemplatePaths
()
79
{
80
static
QStringList
rc
;
81
if
(
rc
.
isEmpty
()) {
82
// Ensure default form template paths
83
const
auto
templatePath
=
"/templates"_L1
;
84
// home
85
QString
path
=
dataDirectory
() +
templatePath
;
86
if
(
checkTemplatePath
(
path
,
true
))
87
rc
+=
path
;
88
89
// designer/bin: Might be owned by root in some installations, do not force it.
90
path
=
qApp
->
applicationDirPath
();
91
path
+=
templatePath
;
92
if
(
checkTemplatePath
(
path
,
false
))
93
rc
+=
path
;
94
}
95
return
rc
;
96
}
97
98
// Migrate templates from $HOME/.designer to standard paths in Qt 7
99
// ### FIXME Qt 8: Remove (QTBUG-96005)
100
void
QDesignerSharedSettings
::
migrateTemplates
()
101
{
102
const
QString
templatePath
= u"/templates"_s;
103
QString
path
=
dataDirectory
() +
templatePath
;
104
if
(
QFileInfo
::
exists
(
path
))
105
return
;
106
if
(!
QDir
().
mkpath
(
path
))
107
return
;
108
QString
legacyPath
=
legacyDataDirectory
() +
templatePath
;
109
if
(!
QFileInfo
::
exists
(
path
))
110
return
;
111
const
auto
&
files
=
QDir
(
legacyPath
).
entryInfoList
(
QDir
::
Files
|
QDir
::
NoSymLinks
|
QDir
::
Readable
);
112
for
(
const
auto
&
file
:
files
) {
113
const
QString
newPath
=
path
+ u'/' +
file
.
fileName
();
114
QFile
::
copy
(
file
.
absoluteFilePath
(),
newPath
);
115
}
116
}
117
118
QStringList
QDesignerSharedSettings
::
formTemplatePaths
()
const
119
{
120
return
m_settings
->
value
(
formTemplatePathsKey
,
121
defaultFormTemplatePaths
()).
toStringList
();
122
}
123
124
void
QDesignerSharedSettings
::
setFormTemplatePaths
(
const
QStringList
&
paths
)
125
{
126
m_settings
->
setValue
(
formTemplatePathsKey
,
paths
);
127
}
128
129
QString
QDesignerSharedSettings
::
formTemplate
()
const
130
{
131
return
m_settings
->
value
(
formTemplateKey
).
toString
();
132
}
133
134
void
QDesignerSharedSettings
::
setFormTemplate
(
const
QString
&
t
)
135
{
136
m_settings
->
setValue
(
formTemplateKey
,
t
);
137
}
138
139
void
QDesignerSharedSettings
::
setAdditionalFormTemplatePaths
(
const
QStringList
&
additionalPaths
)
140
{
141
// merge template paths
142
QStringList
templatePaths
=
defaultFormTemplatePaths
();
143
templatePaths
+=
additionalPaths
;
144
setFormTemplatePaths
(
templatePaths
);
145
}
146
147
QStringList
QDesignerSharedSettings
::
additionalFormTemplatePaths
()
const
148
{
149
// get template paths excluding internal ones
150
QStringList
rc
=
formTemplatePaths
();
151
for
(
const
QString
&
internalTemplatePath
:
defaultFormTemplatePaths
()) {
152
const
int
index
=
rc
.
indexOf
(
internalTemplatePath
);
153
if
(
index
!= -1)
154
rc
.
removeAt
(
index
);
155
}
156
return
rc
;
157
}
158
159
QSize
QDesignerSharedSettings
::
newFormSize
()
const
160
{
161
return
m_settings
->
value
(
newFormSizeKey
,
QSize
(0, 0)).
toSize
();
162
}
163
164
void
QDesignerSharedSettings
::
setNewFormSize
(
const
QSize
&
s
)
165
{
166
if
(
s
.
isNull
()) {
167
m_settings
->
remove
(
newFormSizeKey
);
168
}
else
{
169
m_settings
->
setValue
(
newFormSizeKey
,
s
);
170
}
171
}
172
173
174
PreviewConfiguration
QDesignerSharedSettings
::
customPreviewConfiguration
()
const
175
{
176
PreviewConfiguration
configuration
;
177
configuration
.
fromSettings
(
previewKey
,
m_settings
);
178
return
configuration
;
179
}
180
181
void
QDesignerSharedSettings
::
setCustomPreviewConfiguration
(
const
PreviewConfiguration
&
configuration
)
182
{
183
configuration
.
toSettings
(
previewKey
,
m_settings
);
184
}
185
186
bool
QDesignerSharedSettings
::
isCustomPreviewConfigurationEnabled
()
const
187
{
188
m_settings
->
beginGroup
(
previewKey
);
189
bool
isEnabled
=
m_settings
->
value
(
enabledKey
,
false
).
toBool
();
190
m_settings
->
endGroup
();
191
return
isEnabled
;
192
}
193
194
void
QDesignerSharedSettings
::
setCustomPreviewConfigurationEnabled
(
bool
enabled
)
195
{
196
m_settings
->
beginGroup
(
previewKey
);
197
m_settings
->
setValue
(
enabledKey
,
enabled
);
198
m_settings
->
endGroup
();
199
}
200
201
QStringList
QDesignerSharedSettings
::
userDeviceSkins
()
const
202
{
203
m_settings
->
beginGroup
(
previewKey
);
204
QStringList
userDeviceSkins
205
=
m_settings
->
value
(
userDeviceSkinsKey
,
QStringList
()).
toStringList
();
206
m_settings
->
endGroup
();
207
return
userDeviceSkins
;
208
}
209
210
void
QDesignerSharedSettings
::
setUserDeviceSkins
(
const
QStringList
&
userDeviceSkins
)
211
{
212
m_settings
->
beginGroup
(
previewKey
);
213
m_settings
->
setValue
(
userDeviceSkinsKey
,
userDeviceSkins
);
214
m_settings
->
endGroup
();
215
}
216
217
int
QDesignerSharedSettings
::
zoom
()
const
218
{
219
return
m_settings
->
value
(
zoomKey
, 100).
toInt
();
220
}
221
222
void
QDesignerSharedSettings
::
setZoom
(
int
z
)
223
{
224
m_settings
->
setValue
(
zoomKey
,
QVariant
(
z
));
225
}
226
227
ObjectNamingMode
QDesignerSharedSettings
::
objectNamingMode
()
const
228
{
229
const
QString
value
=
m_settings
->
value
(
namingModeKey
).
toString
();
230
return
value
==
camelCaseNamingMode
231
?
qdesigner_internal
::
CamelCase
:
qdesigner_internal
::
Underscore
;
232
}
233
234
void
QDesignerSharedSettings
::
setObjectNamingMode
(
ObjectNamingMode
n
)
235
{
236
const
QString
value
=
n
==
qdesigner_internal
::
CamelCase
237
?
camelCaseNamingMode
:
underScoreNamingMode
;
238
m_settings
->
setValue
(
namingModeKey
,
QVariant
(
value
));
239
}
240
241
bool
QDesignerSharedSettings
::
zoomEnabled
()
const
242
{
243
return
m_settings
->
value
(
zoomEnabledKey
,
false
).
toBool
();
244
}
245
246
void
QDesignerSharedSettings
::
setZoomEnabled
(
bool
v
)
247
{
248
m_settings
->
setValue
(
zoomEnabledKey
,
v
);
249
}
250
251
DeviceProfile
QDesignerSharedSettings
::
currentDeviceProfile
()
const
252
{
253
return
deviceProfileAt
(
currentDeviceProfileIndex
());
254
}
255
256
void
QDesignerSharedSettings
::
setCurrentDeviceProfileIndex
(
int
i
)
257
{
258
m_settings
->
setValue
(
deviceProfileIndexKey
,
i
);
259
}
260
261
int
QDesignerSharedSettings
::
currentDeviceProfileIndex
()
const
262
{
263
return
m_settings
->
value
(
deviceProfileIndexKey
, -1).
toInt
();
264
}
265
266
static
inline
QString
msgWarnDeviceProfileXml
(
const
QString &msg)
267
{
268
return
QCoreApplication::translate(
"QDesignerSharedSettings"
,
"An error has been encountered while parsing device profile XML: %1"
).arg(msg);
269
}
270
271
DeviceProfile
QDesignerSharedSettings
::
deviceProfileAt
(
int
idx
)
const
272
{
273
DeviceProfile
rc
;
274
if
(
idx
< 0)
275
return
rc
;
276
const
QStringList
xmls
=
deviceProfileXml
();
277
if
(
idx
>=
xmls
.
size
())
278
return
rc
;
279
QString
errorMessage
;
280
if
(!
rc
.
fromXml
(
xmls
.
at
(
idx
), &
errorMessage
)) {
281
rc
.
clear
();
282
designerWarning
(
msgWarnDeviceProfileXml
(
errorMessage
));
283
}
284
return
rc
;
285
}
286
287
QStringList
QDesignerSharedSettings
::
deviceProfileXml
()
const
288
{
289
return
m_settings
->
value
(
deviceProfilesKey
,
QStringList
()).
toStringList
();
290
}
291
292
QDesignerSharedSettings
::
DeviceProfileList
QDesignerSharedSettings
::
deviceProfiles
()
const
293
{
294
DeviceProfileList
rc
;
295
const
QStringList
xmls
=
deviceProfileXml
();
296
if
(
xmls
.
isEmpty
())
297
return
rc
;
298
// De-serialize
299
QString
errorMessage
;
300
DeviceProfile
dp
;
301
for
(
const
auto
&
xml
:
xmls
) {
302
if
(
dp
.
fromXml
(
xml
, &
errorMessage
)) {
303
rc
.
push_back
(
dp
);
304
}
else
{
305
designerWarning
(
msgWarnDeviceProfileXml
(
errorMessage
));
306
}
307
}
308
return
rc
;
309
}
310
311
void
QDesignerSharedSettings
::
setDeviceProfiles
(
const
DeviceProfileList
&
dpl
)
312
{
313
QStringList
l
;
314
for
(
const
auto
&
dp
:
dpl
)
315
l
.
push_back
(
dp
.
toXml
());
316
m_settings
->
setValue
(
deviceProfilesKey
,
l
);
317
}
318
}
319
320
QT_END_NAMESPACE
QT_BEGIN_NAMESPACE
Combined button and popup list for selecting options.
Definition
qsequentialanimationgroup.cpp:47
qdesigner_internal
Auxiliary methods to store/retrieve settings.
Definition
buddyeditor.cpp:67
qdesigner_internal::msgWarnDeviceProfileXml
static QString msgWarnDeviceProfileXml(const QString &msg)
Definition
shared_settings.cpp:266
zoomKey
static constexpr auto zoomKey
Definition
shared_settings.cpp:29
deviceProfileIndexKey
static constexpr auto deviceProfileIndexKey
Definition
shared_settings.cpp:31
formTemplatePathsKey
static constexpr auto formTemplatePathsKey
Definition
shared_settings.cpp:33
userDeviceSkinsKey
static constexpr auto userDeviceSkinsKey
Definition
shared_settings.cpp:28
previewKey
static constexpr auto previewKey
Definition
shared_settings.cpp:26
underScoreNamingMode
static constexpr auto underScoreNamingMode
Definition
shared_settings.cpp:37
defaultGridKey
static constexpr auto defaultGridKey
Definition
shared_settings.cpp:25
deviceProfilesKey
static constexpr auto deviceProfilesKey
Definition
shared_settings.cpp:32
camelCaseNamingMode
static constexpr auto camelCaseNamingMode
Definition
shared_settings.cpp:38
checkTemplatePath
static bool checkTemplatePath(const QString &path, bool create)
Definition
shared_settings.cpp:40
enabledKey
static constexpr auto enabledKey
Definition
shared_settings.cpp:27
namingModeKey
static constexpr auto namingModeKey
Definition
shared_settings.cpp:36
newFormSizeKey
static constexpr auto newFormSizeKey
Definition
shared_settings.cpp:35
zoomEnabledKey
static constexpr auto zoomEnabledKey
Definition
shared_settings.cpp:30
formTemplateKey
static constexpr auto formTemplateKey
Definition
shared_settings.cpp:34
qttools
src
designer
src
lib
shared
shared_settings.cpp
Generated on
for Qt by
1.18.0