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
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
16
17using namespace Qt::StringLiterals;
18
20{
21public:
22 QDesignerWidgetBoxWidgetData(const QString &aname, const QString &xml,
23 const QString &icon_name,
24 QDesignerWidgetBoxInterface::Widget::Type atype);
29};
30
32 const QString &xml,
33 const QString &icon_name,
34 QDesignerWidgetBoxInterface::Widget::Type atype) :
36{
37}
38
39QDesignerWidgetBoxInterface::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
45QDesignerWidgetBoxInterface::Widget::~Widget() = default;
46
47QDesignerWidgetBoxInterface::Widget::Widget(const Widget &w) :
48 m_data(w.m_data)
49{
50}
51
52QDesignerWidgetBoxInterface::Widget &QDesignerWidgetBoxInterface::Widget::operator=(const Widget &rhs)
53{
54 if (this != &rhs) {
55 m_data = rhs.m_data;
56 }
57 return *this;
58}
59
60QString QDesignerWidgetBoxInterface::Widget::name() const
61{
62 return m_data->m_name;
63}
64
65void QDesignerWidgetBoxInterface::Widget::setName(const QString &aname)
66{
67 if (m_data->m_name != aname)
68 m_data->m_name = aname;
69}
70
71QString QDesignerWidgetBoxInterface::Widget::domXml() const
72{
73 return m_data->m_xml;
74}
75
76void QDesignerWidgetBoxInterface::Widget::setDomXml(const QString &xml)
77{
78 if (m_data->m_xml != xml)
79 m_data->m_xml = xml;
80}
81
82QString QDesignerWidgetBoxInterface::Widget::iconName() const
83{
84 return m_data->m_icon_name;
85}
86
87void 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
93QDesignerWidgetBoxInterface::Widget::Type QDesignerWidgetBoxInterface::Widget::type() const
94{
95 return m_data->m_type;
96}
97
98void QDesignerWidgetBoxInterface::Widget::setType(Type atype)
99{
100 if (m_data->m_type != atype)
101 m_data->m_type = atype;
102}
103
104bool QDesignerWidgetBoxInterface::Widget::isNull() const
105{
106 return m_data->m_name.isEmpty();
107}
108
109namespace qdesigner_internal {
115
120
125
126// Convenience to find a widget by class name
128 const QString &className,
129 const QString &category,
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*\"");
137 pattern += QStringLiteral("\".*$");
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) {
151 if (regexp.match(xml).hasMatch()) {
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.
165{
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()) {
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;
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")
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) {
212 fakeTopLevel->setAttributeClass(u"QWidget"_s);
217 }
218
219 return ui;
220}
221
222// Convenience to create a Dom Widget from widget box xml code.
231
232} // namespace qdesigner_internal
233
234QT_END_NAMESPACE
QDesignerWidgetBoxInterface::Widget::Type m_type
QDesignerWidgetBoxWidgetData(const QString &aname, const QString &xml, const QString &icon_name, QDesignerWidgetBoxInterface::Widget::Type atype)
Combined button and popup list for selecting options.
Auxiliary methods to store/retrieve settings.