Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qdesigner_widgetbox.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
"qdesigner_widgetbox_p.h"
6
#
include
"qdesigner_utils_p.h"
7
8
#
include
<
QtDesigner
/
private
/
ui4_p
.
h
>
9
10
#
include
<
QtCore
/
qregularexpression
.
h
>
11
#
include
<
QtCore
/
qdebug
.
h
>
12
#
include
<
QtCore
/
qxmlstream
.
h
>
13
#
include
<
QtCore
/
qshareddata
.
h
>
14
15
QT_BEGIN_NAMESPACE
16
17
using
namespace
Qt::StringLiterals;
18
19
class
QDesignerWidgetBoxWidgetData
:
public
QSharedData
20
{
21
public
:
22
QDesignerWidgetBoxWidgetData
(
const
QString &aname,
const
QString &xml,
23
const
QString &icon_name,
24
QDesignerWidgetBoxInterface::Widget::Type atype);
25
QString
m_name
;
26
QString
m_xml
;
27
QString
m_icon_name
;
28
QDesignerWidgetBoxInterface
::
Widget
::
Type
m_type
;
29
};
30
31
QDesignerWidgetBoxWidgetData
::
QDesignerWidgetBoxWidgetData
(
const
QString &aname,
32
const
QString &xml,
33
const
QString &icon_name,
34
QDesignerWidgetBoxInterface::Widget::Type atype) :
35
m_name
(
aname
),
m_xml
(
xml
),
m_icon_name
(
icon_name
),
m_type
(
atype
)
36
{
37
}
38
39
QDesignerWidgetBoxInterface::Widget::Widget(
const
QString &aname,
const
QString &xml,
40
const
QString &icon_name, Type atype) :
41
m_data(
new
QDesignerWidgetBoxWidgetData(aname, xml, icon_name, atype))
42
{
43
}
44
45
QDesignerWidgetBoxInterface::Widget::~Widget() =
default
;
46
47
QDesignerWidgetBoxInterface::Widget::Widget(
const
Widget &w) :
48
m_data(w.m_data)
49
{
50
}
51
52
QDesignerWidgetBoxInterface::Widget &QDesignerWidgetBoxInterface::Widget::operator=(
const
Widget &rhs)
53
{
54
if
(
this
!= &rhs) {
55
m_data = rhs.m_data;
56
}
57
return
*
this
;
58
}
59
60
QString QDesignerWidgetBoxInterface::Widget::name()
const
61
{
62
return
m_data->m_name;
63
}
64
65
void
QDesignerWidgetBoxInterface::Widget::setName(
const
QString &aname)
66
{
67
if
(m_data->m_name != aname)
68
m_data->m_name = aname;
69
}
70
71
QString QDesignerWidgetBoxInterface::Widget::domXml()
const
72
{
73
return
m_data->m_xml;
74
}
75
76
void
QDesignerWidgetBoxInterface::Widget::setDomXml(
const
QString &xml)
77
{
78
if
(m_data->m_xml != xml)
79
m_data->m_xml = xml;
80
}
81
82
QString QDesignerWidgetBoxInterface::Widget::iconName()
const
83
{
84
return
m_data->m_icon_name;
85
}
86
87
void
QDesignerWidgetBoxInterface::Widget::setIconName(
const
QString &icon_name)
88
{
89
if
(m_data->m_icon_name != icon_name)
90
m_data->m_icon_name = icon_name;
91
}
92
93
QDesignerWidgetBoxInterface::Widget::Type QDesignerWidgetBoxInterface::Widget::type()
const
94
{
95
return
m_data->m_type;
96
}
97
98
void
QDesignerWidgetBoxInterface::Widget::setType(Type atype)
99
{
100
if
(m_data->m_type != atype)
101
m_data->m_type = atype;
102
}
103
104
bool
QDesignerWidgetBoxInterface::Widget::isNull()
const
105
{
106
return
m_data->m_name.isEmpty();
107
}
108
109
namespace
qdesigner_internal
{
110
QDesignerWidgetBox
::
QDesignerWidgetBox
(
QWidget
*
parent
,
Qt
::
WindowFlags
flags
)
111
:
QDesignerWidgetBoxInterface
(
parent
,
flags
)
112
{
113
114
}
115
116
QDesignerWidgetBox
::
LoadMode
QDesignerWidgetBox
::
loadMode
()
const
117
{
118
return
m_loadMode
;
119
}
120
121
void
QDesignerWidgetBox
::
setLoadMode
(
LoadMode
lm
)
122
{
123
m_loadMode
=
lm
;
124
}
125
126
// Convenience to find a widget by class name
127
bool
QDesignerWidgetBox
::
findWidget
(
const
QDesignerWidgetBoxInterface
*
wbox
,
128
const
QString
&
className
,
129
const
QString
&
category
,
130
Widget
*
widgetData
)
131
{
132
// Note that entry names do not necessarily match the class name
133
// (at least, not for the standard widgets), so,
134
// look in the XML for the class name of the first widget to appear
135
QString
pattern
=
QStringLiteral
(
"^<widget\\s+class\\s*=\\s*\""
);
136
pattern
+=
className
;
137
pattern
+=
QStringLiteral
(
"\".*$"
);
138
const
QRegularExpression
regexp
(
pattern
);
139
Q_ASSERT
(
regexp
.
isValid
());
140
const
int
catCount
=
wbox
->
categoryCount
();
141
for
(
int
c
= 0;
c
<
catCount
;
c
++) {
142
const
Category
cat
=
wbox
->
category
(
c
);
143
if
(
category
.
isEmpty
() ||
cat
.
name
() ==
category
) {
144
const
int
widgetCount
=
cat
.
widgetCount
();
145
for
(
int
w
= 0;
w
<
widgetCount
;
w
++) {
146
const
Widget
widget
=
cat
.
widget
(
w
);
147
QString
xml
=
widget
.
domXml
();
// Erase the <ui> tag that can be present starting from 4.4
148
const
auto
widgetTagIndex
=
xml
.
indexOf
(
"<widget"_L1
);
149
if
(
widgetTagIndex
!= -1) {
150
xml
.
remove
(0,
widgetTagIndex
);
151
if
(
regexp
.
match
(
xml
).
hasMatch
()) {
152
*
widgetData
=
widget
;
153
return
true
;
154
}
155
}
156
}
157
}
158
}
159
return
false
;
160
}
161
162
// Convenience to create a Dom Widget from widget box xml code.
163
DomUI
*
QDesignerWidgetBox
::
xmlToUi
(
const
QString
&
name
,
const
QString
&
xml
,
bool
insertFakeTopLevel
,
164
QString
*
errorMessage
)
165
{
166
QXmlStreamReader
reader
(
xml
);
167
DomUI
*
ui
=
nullptr
;
168
169
// The xml description must either contain a root element "ui" with a child element "widget"
170
// or "widget" as the root element (4.3 legacy)
171
172
while
(!
reader
.
atEnd
()) {
173
if
(
reader
.
readNext
() ==
QXmlStreamReader
::
StartElement
) {
174
const
auto
name
=
reader
.
name
();
175
if
(
ui
) {
176
reader
.
raiseError
(
tr
(
"Unexpected element <%1>"
).
arg
(
name
.
toString
()));
177
continue
;
178
}
179
180
if
(
name
.
compare
(
"widget"_L1
,
Qt
::
CaseInsensitive
) == 0) {
// 4.3 legacy, wrap into DomUI
181
ui
=
new
DomUI
;
182
DomWidget
*
widget
=
new
DomWidget
;
183
widget
->
read
(
reader
);
184
ui
->
setElementWidget
(
widget
);
185
}
else
if
(
name
.
compare
(
"ui"_L1
,
Qt
::
CaseInsensitive
) == 0) {
// 4.4
186
ui
=
new
DomUI
;
187
ui
->
read
(
reader
);
188
}
else
{
189
reader
.
raiseError
(
tr
(
"Unexpected element <%1>"
).
arg
(
name
.
toString
()));
190
}
191
}
192
}
193
194
if
(
reader
.
hasError
()) {
195
delete
ui
;
196
*
errorMessage
=
tr
(
"A parse error occurred at line %1, column %2 of the XML code "
197
"specified for the widget %3: %4\n%5"
)
198
.
arg
(
reader
.
lineNumber
()).
arg
(
reader
.
columnNumber
())
199
.
arg
(
name
,
reader
.
errorString
(),
xml
);
200
return
nullptr
;
201
}
202
203
if
(!
ui
|| !
ui
->
elementWidget
()) {
204
delete
ui
;
205
*
errorMessage
=
tr
(
"The XML code specified for the widget %1 does not contain "
206
"any widget elements.\n%2"
).
arg
(
name
,
xml
);
207
return
nullptr
;
208
}
209
210
if
(
insertFakeTopLevel
) {
211
DomWidget
*
fakeTopLevel
=
new
DomWidget
;
212
fakeTopLevel
->
setAttributeClass
(u"QWidget"_s);
213
QList
<
DomWidget
*>
children
;
214
children
.
push_back
(
ui
->
takeElementWidget
());
215
fakeTopLevel
->
setElementWidget
(
children
);
216
ui
->
setElementWidget
(
fakeTopLevel
);
217
}
218
219
return
ui
;
220
}
221
222
// Convenience to create a Dom Widget from widget box xml code.
223
DomUI
*
QDesignerWidgetBox
::
xmlToUi
(
const
QString
&
name
,
const
QString
&
xml
,
bool
insertFakeTopLevel
)
224
{
225
QString
errorMessage
;
226
DomUI
*
rc
=
xmlToUi
(
name
,
xml
,
insertFakeTopLevel
, &
errorMessage
);
227
if
(!
rc
)
228
qdesigner_internal
::
designerWarning
(
errorMessage
);
229
return
rc
;
230
}
231
232
}
// namespace qdesigner_internal
233
234
QT_END_NAMESPACE
QDesignerWidgetBoxWidgetData
Definition
qdesigner_widgetbox.cpp:20
QDesignerWidgetBoxWidgetData::m_icon_name
QString m_icon_name
Definition
qdesigner_widgetbox.cpp:27
QDesignerWidgetBoxWidgetData::m_type
QDesignerWidgetBoxInterface::Widget::Type m_type
Definition
qdesigner_widgetbox.cpp:28
QDesignerWidgetBoxWidgetData::QDesignerWidgetBoxWidgetData
QDesignerWidgetBoxWidgetData(const QString &aname, const QString &xml, const QString &icon_name, QDesignerWidgetBoxInterface::Widget::Type atype)
Definition
qdesigner_widgetbox.cpp:31
QDesignerWidgetBoxWidgetData::m_name
QString m_name
Definition
qdesigner_widgetbox.cpp:25
QDesignerWidgetBoxWidgetData::m_xml
QString m_xml
Definition
qdesigner_widgetbox.cpp:26
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
qttools
src
designer
src
lib
shared
qdesigner_widgetbox.cpp
Generated on
for Qt by
1.16.1