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
deviceprofile.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
"deviceprofile_p.h"
6
7
#
include
<
QtDesigner
/
abstractformeditor
.
h
>
8
#
include
<
widgetfactory_p
.
h
>
9
#
include
<
qdesigner_utils_p
.
h
>
10
11
#
include
<
QtWidgets
/
qapplication
.
h
>
12
#
include
<
QtGui
/
qfont
.
h
>
13
#
include
<
QtWidgets
/
qstyle
.
h
>
14
#
include
<
QtWidgets
/
qstylefactory
.
h
>
15
#
include
<
QtWidgets
/
qapplication
.
h
>
16
17
#
include
<
QtGui
/
qscreen
.
h
>
18
19
#
include
<
QtCore
/
qshareddata
.
h
>
20
#
include
<
QtCore
/
qtextstream
.
h
>
21
22
#
include
<
QtCore
/
qxmlstream
.
h
>
23
24
25
static
const
char
dpiXPropertyC
[] =
"_q_customDpiX"
;
26
static
const
char
dpiYPropertyC
[] =
"_q_customDpiY"
;
27
28
QT_BEGIN_NAMESPACE
29
30
using
namespace
Qt::StringLiterals;
31
32
namespace
qdesigner_internal
{
33
34
// XML serialization
35
static
const
char
*
xmlVersionC
=
"1.0"
;
36
static
const
char
*
rootElementC
=
"deviceprofile"
;
37
static
constexpr
auto
nameElementC
=
"name"_L1
;
38
static
constexpr
auto
fontFamilyElementC
=
"fontfamily"_L1
;
39
static
constexpr
auto
fontPointSizeElementC
=
"fontpointsize"_L1
;
40
static
constexpr
auto
dPIXElementC
=
"dpix"_L1
;
41
static
constexpr
auto
dPIYElementC
=
"dpiy"_L1
;
42
static
constexpr
auto
styleElementC
=
"style"_L1
;
43
44
/* DeviceProfile:
45
* For preview purposes (preview, widget box, new form dialog), the
46
* QDesignerFormBuilder applies the settings to the form main container
47
* (Point being here that the DPI must be set directly for size calculations
48
* to be correct).
49
* For editing purposes, FormWindow applies the settings to the form container
50
* as not to interfere with the font settings of the form main container.
51
* In addition, the widgetfactory maintains the system settings style
52
* and applies it when creating widgets. */
53
54
// ---------------- DeviceProfileData
55
class
DeviceProfileData
:
public
QSharedData
{
56
public
:
57
DeviceProfileData
() =
default
;
58
void
fromSystem
();
59
void
clear
();
60
61
QString
m_fontFamily
;
62
QString
m_style
;
63
QString
m_name
;
64
int
m_fontPointSize
= -1;
65
int
m_dpiX
= -1;
66
int
m_dpiY
= -1;
67
};
68
69
void
DeviceProfileData
::
clear
()
70
{
71
m_fontPointSize
= -1;
72
m_dpiX
= 0;
73
m_dpiY
= 0;
74
m_name.clear();
75
m_style.clear();
76
}
77
78
void
DeviceProfileData
::
fromSystem
()
79
{
80
const
QFont appFont = QApplication::font();
81
m_fontFamily = appFont.family();
82
m_fontPointSize
= appFont.pointSize();
83
DeviceProfile::systemResolution(&m_dpiX, &m_dpiY);
84
m_style.clear();
85
}
86
87
// ---------------- DeviceProfile
88
DeviceProfile
::
DeviceProfile
() :
89
m_d
(
new
DeviceProfileData
)
90
{
91
}
92
93
DeviceProfile
::
DeviceProfile
(
const
DeviceProfile
&
o
) :
94
m_d
(
o
.
m_d
)
95
96
{
97
}
98
99
DeviceProfile
&
DeviceProfile
::
operator
=(
const
DeviceProfile
&
o
)
100
{
101
m_d
.
operator
=(
o
.
m_d
);
102
return
*
this
;
103
}
104
105
DeviceProfile
::~
DeviceProfile
() =
default
;
106
107
void
DeviceProfile
::
clear
()
108
{
109
m_d
->
clear
();
110
}
111
112
bool
DeviceProfile
::
isEmpty
()
const
113
{
114
return
m_d
->
m_name
.
isEmpty
();
115
}
116
117
QString
DeviceProfile
::
fontFamily
()
const
118
{
119
return
m_d
->
m_fontFamily
;
120
}
121
122
void
DeviceProfile
::
setFontFamily
(
const
QString
&
f
)
123
{
124
m_d
->
m_fontFamily
=
f
;
125
}
126
127
int
DeviceProfile
::
fontPointSize
()
const
128
{
129
return
m_d
->
m_fontPointSize
;
130
}
131
132
void
DeviceProfile
::
setFontPointSize
(
int
p
)
133
{
134
m_d
->
m_fontPointSize
=
p
;
135
}
136
137
QString
DeviceProfile
::
style
()
const
138
{
139
return
m_d
->
m_style
;
140
}
141
142
void
DeviceProfile
::
setStyle
(
const
QString
&
s
)
143
{
144
m_d
->
m_style
=
s
;
145
}
146
147
int
DeviceProfile
::
dpiX
()
const
148
{
149
return
m_d
->
m_dpiX
;
150
}
151
152
void
DeviceProfile
::
setDpiX
(
int
d
)
153
{
154
m_d
->
m_dpiX
=
d
;
155
}
156
157
int
DeviceProfile
::
dpiY
()
const
158
{
159
return
m_d
->
m_dpiY
;
160
}
161
162
void
DeviceProfile
::
setDpiY
(
int
d
)
163
{
164
m_d
->
m_dpiY
=
d
;
165
}
166
167
void
DeviceProfile
::
fromSystem
()
168
{
169
m_d
->
fromSystem
();
170
}
171
172
QString
DeviceProfile
::
name
()
const
173
{
174
return
m_d
->
m_name
;
175
}
176
177
void
DeviceProfile
::
setName
(
const
QString
&
n
)
178
{
179
m_d
->
m_name
=
n
;
180
}
181
182
void
DeviceProfile
::
systemResolution
(
int
*
dpiX
,
int
*
dpiY
)
183
{
184
auto
s
=
qApp
->
primaryScreen
();
185
*
dpiX
=
s
->
logicalDotsPerInchX
();
186
*
dpiY
=
s
->
logicalDotsPerInchY
();
187
}
188
189
class
FriendlyWidget
:
public
QWidget
{
190
friend
class
DeviceProfile;
191
};
192
193
void
DeviceProfile
::
widgetResolution
(
const
QWidget
*
w
,
int
*
dpiX
,
int
*
dpiY
)
194
{
195
const
FriendlyWidget
*
fw
=
static_cast
<
const
FriendlyWidget
*>(
w
);
196
*
dpiX
=
fw
->
metric
(
QPaintDevice
::
PdmDpiX
);
197
*
dpiY
=
fw
->
metric
(
QPaintDevice
::
PdmDpiY
);
198
}
199
200
QString
DeviceProfile
::
toString
()
const
201
{
202
const
DeviceProfileData
&
d
= *
m_d
;
203
QString
rc
;
204
QTextStream
(&
rc
) <<
"DeviceProfile:name="
<<
d
.
m_name
<<
" Font="
<<
d
.
m_fontFamily
<<
' '
205
<<
d
.
m_fontPointSize
<<
" Style="
<<
d
.
m_style
<<
" DPI="
<<
d
.
m_dpiX
<<
','
<<
d
.
m_dpiY
;
206
return
rc
;
207
}
208
209
// Apply font to widget
210
static
void
applyFont
(
const
QString &family,
int
size, DeviceProfile::ApplyMode am,
QWidget
*widget)
211
{
212
QFont currentFont = widget->font();
213
if
(currentFont.pointSize() == size && currentFont.family() == family)
214
return
;
215
switch
(am) {
216
case
DeviceProfile::ApplyFormParent:
217
// Invisible form parent: Apply all
218
widget->setFont(QFont(family, size));
219
break
;
220
case
DeviceProfile::ApplyPreview: {
221
// Preview: Apply only subproperties that have not been changed by designer properties
222
bool
apply =
false
;
223
const
uint resolve = currentFont.resolveMask();
224
if
(!(resolve & QFont::FamilyResolved)) {
225
currentFont.setFamily(family);
226
apply =
true
;
227
}
228
if
(!(resolve & QFont::SizeResolved)) {
229
currentFont.setPointSize(size);
230
apply =
true
;
231
}
232
if
(apply)
233
widget->setFont(currentFont);
234
}
235
break
;
236
}
237
}
238
239
void
DeviceProfile
::
applyDPI
(
int
dpiX
,
int
dpiY
,
QWidget
*
widget
)
240
{
241
int
sysDPIX
,
sysDPIY
;
// Set dynamic variables in case values are different from system DPI
242
systemResolution
(&
sysDPIX
, &
sysDPIY
);
243
if
(
dpiX
!=
sysDPIX
&&
dpiY
!=
sysDPIY
) {
244
widget
->
setProperty
(
dpiXPropertyC
,
QVariant
(
dpiX
));
245
widget
->
setProperty
(
dpiYPropertyC
,
QVariant
(
dpiY
));
246
}
247
}
248
249
void
DeviceProfile
::
apply
(
const
QDesignerFormEditorInterface
*
core
,
QWidget
*
widget
,
ApplyMode
am
)
const
250
{
251
if
(
isEmpty
())
252
return
;
253
254
const
DeviceProfileData
&
d
= *
m_d
;
255
256
if
(!
d
.
m_fontFamily
.
isEmpty
())
257
applyFont
(
d
.
m_fontFamily
,
d
.
m_fontPointSize
,
am
,
widget
);
258
259
applyDPI
(
d
.
m_dpiX
,
d
.
m_dpiY
,
widget
);
260
261
if
(!
d
.
m_style
.
isEmpty
()) {
262
if
(
WidgetFactory
*
wf
=
qobject_cast
<
qdesigner_internal
::
WidgetFactory
*>(
core
->
widgetFactory
()))
263
wf
->
applyStyleTopLevel
(
d
.
m_style
,
widget
);
264
}
265
}
266
267
bool
comparesEqual
(
const
DeviceProfile &lhs,
const
DeviceProfile &rhs)
noexcept
268
{
269
const
DeviceProfileData
&d = *lhs.m_d;
270
const
DeviceProfileData
&rhs_d = *rhs.m_d;
271
return
d
.
m_fontPointSize
== rhs_d
.
m_fontPointSize
&&
272
d
.
m_dpiX
== rhs_d
.
m_dpiX
&& d
.
m_dpiY
== rhs_d
.
m_dpiY
&& d.m_fontFamily == rhs_d.m_fontFamily &&
273
d.m_style == rhs_d.m_style && d.m_name == rhs_d.m_name;
274
}
275
276
static
inline
void
writeElement
(QXmlStreamWriter &writer,
const
QString &element,
const
QString &cdata)
277
{
278
writer.writeStartElement(element);
279
writer.writeCharacters(cdata);
280
writer.writeEndElement();
281
}
282
283
QString
DeviceProfile
::
toXml
()
const
284
{
285
const
DeviceProfileData
&
d
= *
m_d
;
286
QString
rc
;
287
QXmlStreamWriter
writer
(&
rc
);
288
writer
.
writeStartDocument
(
QLatin1StringView
(
xmlVersionC
));
289
writer
.
writeStartElement
(
QLatin1StringView
(
rootElementC
));
290
writeElement
(
writer
,
nameElementC
,
d
.
m_name
);
291
292
if
(!
d
.
m_fontFamily
.
isEmpty
())
293
writeElement
(
writer
,
fontFamilyElementC
,
d
.
m_fontFamily
);
294
if
(
d
.
m_fontPointSize
>= 0)
295
writeElement
(
writer
,
fontPointSizeElementC
,
QString
::
number
(
d
.
m_fontPointSize
));
296
if
(
d
.
m_dpiX
> 0)
297
writeElement
(
writer
,
dPIXElementC
,
QString
::
number
(
d
.
m_dpiX
));
298
if
(
d
.
m_dpiY
> 0)
299
writeElement
(
writer
,
dPIYElementC
,
QString
::
number
(
d
.
m_dpiY
));
300
if
(!
d
.
m_style
.
isEmpty
())
301
writeElement
(
writer
,
styleElementC
,
d
.
m_style
);
302
303
writer
.
writeEndElement
();
304
writer
.
writeEndDocument
();
305
return
rc
;
306
}
307
308
/* Switch stages when encountering a start element (state table) */
309
enum
ParseStage
{
ParseBeginning
,
ParseWithinRoot
,
310
ParseName
,
ParseFontFamily
,
ParseFontPointSize
,
ParseDPIX
,
ParseDPIY
,
ParseStyle
,
311
ParseError
};
312
313
static
ParseStage
nextStage
(
ParseStage
currentStage, QStringView startElement)
314
{
315
switch
(currentStage) {
316
case
ParseBeginning
:
317
if
(startElement == QLatin1StringView(
rootElementC
))
318
return
ParseWithinRoot
;
319
break
;
320
case
ParseWithinRoot
:
321
case
ParseName
:
322
case
ParseFontFamily
:
323
case
ParseFontPointSize
:
324
case
ParseDPIX
:
325
case
ParseDPIY
:
326
case
ParseStyle
:
327
if
(startElement == nameElementC)
328
return
ParseName
;
329
if
(startElement == fontFamilyElementC)
330
return
ParseFontFamily
;
331
if
(startElement == fontPointSizeElementC)
332
return
ParseFontPointSize
;
333
if
(startElement == dPIXElementC)
334
return
ParseDPIX
;
335
if
(startElement == dPIYElementC)
336
return
ParseDPIY
;
337
if
(startElement == styleElementC)
338
return
ParseStyle
;
339
break
;
340
case
ParseError
:
341
break
;
342
}
343
return
ParseError
;
344
}
345
346
static
bool
readIntegerElement
(QXmlStreamReader &reader,
int
*v)
347
{
348
const
QString e = reader.readElementText();
349
bool
ok;
350
*v = e.toInt(&ok);
351
//: Reading a number for an embedded device profile
352
if
(!ok)
353
reader.raiseError(QApplication::translate(
"DeviceProfile"
,
"'%1' is not a number."
).arg(e));
354
return
ok;
355
}
356
357
bool
DeviceProfile
::
fromXml
(
const
QString
&
xml
,
QString
*
errorMessage
)
358
{
359
DeviceProfileData
&
d
= *
m_d
;
360
d
.
fromSystem
();
361
362
QXmlStreamReader
reader
(
xml
);
363
364
ParseStage
ps
=
ParseBeginning
;
365
QXmlStreamReader
::
TokenType
tt
=
QXmlStreamReader
::
NoToken
;
366
int
iv
= 0;
367
do
{
368
tt
=
reader
.
readNext
();
369
if
(
tt
==
QXmlStreamReader
::
StartElement
) {
370
ps
=
nextStage
(
ps
,
reader
.
name
());
371
switch
(
ps
) {
372
case
ParseBeginning
:
373
case
ParseWithinRoot
:
374
break
;
375
case
ParseError
:
376
reader
.
raiseError
(
QApplication
::
translate
(
"DeviceProfile"
,
"An invalid tag <%1> was encountered."
).
arg
(
reader
.
name
().
toString
()));
377
tt
=
QXmlStreamReader
::
Invalid
;
378
break
;
379
case
ParseName
:
380
d
.
m_name
=
reader
.
readElementText
();
381
break
;
382
case
ParseFontFamily
:
383
d
.
m_fontFamily
=
reader
.
readElementText
();
384
break
;
385
case
ParseFontPointSize
:
386
if
(
readIntegerElement
(
reader
, &
iv
)) {
387
d
.
m_fontPointSize
=
iv
;
388
}
else
{
389
tt
=
QXmlStreamReader
::
Invalid
;
390
}
391
break
;
392
case
ParseDPIX
:
393
if
(
readIntegerElement
(
reader
, &
iv
)) {
394
d
.
m_dpiX
=
iv
;
395
}
else
{
396
tt
=
QXmlStreamReader
::
Invalid
;
397
}
398
break
;
399
case
ParseDPIY
:
400
if
(
readIntegerElement
(
reader
, &
iv
)) {
401
d
.
m_dpiY
=
iv
;
402
}
else
{
403
tt
=
QXmlStreamReader
::
Invalid
;
404
}
405
break
;
406
case
ParseStyle
:
407
d
.
m_style
=
reader
.
readElementText
();
408
break
;
409
}
410
}
411
}
while
(
tt
!=
QXmlStreamReader
::
Invalid
&&
tt
!=
QXmlStreamReader
::
EndDocument
);
412
413
if
(
reader
.
hasError
()) {
414
*
errorMessage
=
reader
.
errorString
();
415
return
false
;
416
}
417
418
return
true
;
419
}
420
}
421
422
QT_END_NAMESPACE
QPainter::QWidget
friend class QWidget
Definition
qpainter.h:432
qdesigner_internal::DeviceProfileData
Definition
deviceprofile.cpp:55
qdesigner_internal::DeviceProfileData::DeviceProfileData
DeviceProfileData()=default
qdesigner_internal::DeviceProfileData::m_fontPointSize
int m_fontPointSize
Definition
deviceprofile.cpp:64
qdesigner_internal::DeviceProfileData::m_fontFamily
QString m_fontFamily
Definition
deviceprofile.cpp:61
qdesigner_internal::DeviceProfileData::m_style
QString m_style
Definition
deviceprofile.cpp:62
qdesigner_internal::DeviceProfileData::m_name
QString m_name
Definition
deviceprofile.cpp:63
qdesigner_internal::DeviceProfileData::m_dpiX
int m_dpiX
Definition
deviceprofile.cpp:65
qdesigner_internal::DeviceProfileData::m_dpiY
int m_dpiY
Definition
deviceprofile.cpp:66
qdesigner_internal::DeviceProfileData::fromSystem
void fromSystem()
Definition
deviceprofile.cpp:78
qdesigner_internal::DeviceProfileData::clear
void clear()
Definition
deviceprofile.cpp:69
qdesigner_internal::FriendlyWidget
Definition
deviceprofile.cpp:189
dpiYPropertyC
static const char dpiYPropertyC[]
Definition
deviceprofile.cpp:26
dpiXPropertyC
static const char dpiXPropertyC[]
Definition
deviceprofile.cpp:25
qdesigner_internal
Auxiliary methods to store/retrieve settings.
Definition
buddyeditor.cpp:67
qdesigner_internal::writeElement
static void writeElement(QXmlStreamWriter &writer, const QString &element, const QString &cdata)
Definition
deviceprofile.cpp:276
qdesigner_internal::readIntegerElement
static bool readIntegerElement(QXmlStreamReader &reader, int *v)
Definition
deviceprofile.cpp:346
qdesigner_internal::rootElementC
static const char * rootElementC
Definition
deviceprofile.cpp:36
qdesigner_internal::styleElementC
static constexpr auto styleElementC
Definition
deviceprofile.cpp:42
qdesigner_internal::fontPointSizeElementC
static constexpr auto fontPointSizeElementC
Definition
deviceprofile.cpp:39
qdesigner_internal::dPIYElementC
static constexpr auto dPIYElementC
Definition
deviceprofile.cpp:41
qdesigner_internal::xmlVersionC
static const char * xmlVersionC
Definition
deviceprofile.cpp:35
qdesigner_internal::dPIXElementC
static constexpr auto dPIXElementC
Definition
deviceprofile.cpp:40
qdesigner_internal::fontFamilyElementC
static constexpr auto fontFamilyElementC
Definition
deviceprofile.cpp:38
qdesigner_internal::applyFont
static void applyFont(const QString &family, int size, DeviceProfile::ApplyMode am, QWidget *widget)
Definition
deviceprofile.cpp:210
qdesigner_internal::ParseStage
ParseStage
Definition
fontpropertymanager.cpp:385
qdesigner_internal::ParseDPIX
@ ParseDPIX
Definition
deviceprofile.cpp:310
qdesigner_internal::ParseBeginning
@ ParseBeginning
Definition
fontpropertymanager.cpp:385
qdesigner_internal::ParseError
@ ParseError
Definition
fontpropertymanager.cpp:386
qdesigner_internal::ParseDPIY
@ ParseDPIY
Definition
deviceprofile.cpp:310
qdesigner_internal::ParseFontFamily
@ ParseFontFamily
Definition
deviceprofile.cpp:310
qdesigner_internal::ParseFontPointSize
@ ParseFontPointSize
Definition
deviceprofile.cpp:310
qdesigner_internal::ParseName
@ ParseName
Definition
deviceprofile.cpp:310
qdesigner_internal::ParseWithinRoot
@ ParseWithinRoot
Definition
fontpropertymanager.cpp:385
qdesigner_internal::ParseStyle
@ ParseStyle
Definition
deviceprofile.cpp:310
qdesigner_internal::nextStage
static ParseStage nextStage(ParseStage currentStage, QStringView startElement)
Definition
deviceprofile.cpp:313
qdesigner_internal::nameElementC
static constexpr auto nameElementC
Definition
deviceprofile.cpp:37
qdesigner_internal::comparesEqual
bool comparesEqual(const DeviceProfile &lhs, const DeviceProfile &rhs) noexcept
Definition
deviceprofile.cpp:267
qttools
src
designer
src
lib
shared
deviceprofile.cpp
Generated on
for Qt by
1.18.0