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
customwidgetsinfo.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
5#include "driver.h"
6#include "ui4.h"
7#include "utils.h"
8
9#include <utility>
10
12
13using namespace Qt::StringLiterals;
14
16
17void CustomWidgetsInfo::acceptUI(DomUI *node)
18{
19 m_customWidgets.clear();
20
21 if (node->elementCustomWidgets())
22 acceptCustomWidgets(node->elementCustomWidgets());
23}
24
25void CustomWidgetsInfo::acceptCustomWidgets(DomCustomWidgets *node)
26{
28}
29
30void CustomWidgetsInfo::acceptCustomWidget(DomCustomWidget *node)
31{
32 if (node->elementClass().isEmpty())
33 return;
34
35 m_customWidgets.insert(node->elementClass(), node);
36}
37
38bool CustomWidgetsInfo::extends(const QString &classNameIn, QAnyStringView baseClassName) const
39{
40 if (classNameIn == baseClassName)
41 return true;
42
43 QString className = classNameIn;
44 while (const DomCustomWidget *c = customWidget(className)) {
45 const QString extends = c->elementExtends();
46 if (className == extends) // Faulty legacy custom widget entries exist.
47 return false;
48 if (extends == baseClassName)
49 return true;
50 className = extends;
51 }
52 return false;
53}
54
55bool CustomWidgetsInfo::extendsOneOf(const QString &classNameIn,
56 const QStringList &baseClassNames) const
57{
58 if (baseClassNames.contains(classNameIn))
59 return true;
60
61 QString className = classNameIn;
62 while (const DomCustomWidget *c = customWidget(className)) {
63 const QString extends = c->elementExtends();
64 if (className == extends) // Faulty legacy custom widget entries exist.
65 return false;
66 if (baseClassNames.contains(extends))
67 return true;
68 className = extends;
69 }
70 return false;
71}
72
73bool CustomWidgetsInfo::isCustomWidgetContainer(const QString &className) const
74{
75 if (const DomCustomWidget *dcw = m_customWidgets.value(className, nullptr))
76 if (dcw->hasElementContainer())
77 return dcw->elementContainer() != 0;
78 return false;
79}
80
81// FIXME in 7.0 - QTBUG-124241
82// Remove isAmbiguous logic when widget slots have been disambiguated.
83bool CustomWidgetsInfo::isAmbiguous(const QString &className, const QString &signature,
84 QMetaMethod::MethodType type) const
85{
86 using TypeMap = QHash<QString, QMetaMethod::MethodType>;
87 struct AmbiguousInClass {
88 QLatin1StringView className;
89 TypeMap methodMap;
90 };
91
92 static const QList<AmbiguousInClass> ambiguousList = {
93
94 {"QAction"_L1, {{"triggered"_L1, QMetaMethod::Signal}}},
95 {"QCommandLinkButton"_L1, {{"triggered"_L1, QMetaMethod::Signal},
96 {"clicked"_L1, QMetaMethod::Signal}}},
97 {"QPushButton"_L1, {{"triggered"_L1, QMetaMethod::Signal},
98 {"clicked"_L1, QMetaMethod::Signal}}},
99 {"QCheckBox"_L1, {{"triggered"_L1, QMetaMethod::Signal},
100 {"clicked"_L1, QMetaMethod::Signal}}},
101 {"QRadioButton"_L1, {{"triggered"_L1, QMetaMethod::Signal},
102 {"clicked"_L1, QMetaMethod::Signal}}},
103 {"QToolButton"_L1, {{"triggered"_L1, QMetaMethod::Signal},
104 {"clicked"_L1, QMetaMethod::Signal}}},
105 {"QLabel"_L1, {{"setNum"_L1, QMetaMethod::Slot}}},
106 {"QGraphicsView"_L1, {{"invalidateScene"_L1, QMetaMethod::Slot}}},
107 {"QListView"_L1, {{"dataChanged"_L1, QMetaMethod::Slot}}},
108 {"QColumnView"_L1, {{"dataChanged"_L1, QMetaMethod::Slot}}},
109 {"QListWidget"_L1, {{"dataChanged"_L1, QMetaMethod::Slot},
110 {"scrollToItem"_L1, QMetaMethod::Slot}}},
111 {"QTableView"_L1, {{"dataChanged"_L1, QMetaMethod::Slot}}},
112 {"QTableWidget"_L1, {{"dataChanged"_L1, QMetaMethod::Slot},
113 {"scrollToItem"_L1, QMetaMethod::Slot}}},
114 {"QTreeView"_L1, {{"dataChanged"_L1, QMetaMethod::Slot},
115 {"verticalScrollbarValueChanged"_L1, QMetaMethod::Slot},
116 {"expandRecursively"_L1, QMetaMethod::Slot}}},
117 {"QTreeWidget"_L1, {{"dataChanged"_L1, QMetaMethod::Slot},
118 {"verticalScrollbarValueChanged"_L1, QMetaMethod::Slot}
119 ,{"expandRecursively"_L1, QMetaMethod::Slot}
120 ,{"scrollToItem"_L1, QMetaMethod::Slot}}},
121 {"QUndoView"_L1, {{"dataChanged"_L1, QMetaMethod::Slot}}},
122 {"QLCDNumber"_L1, {{"display"_L1, QMetaMethod::Slot}}},
123 {"QMenuBar"_L1, {{"setVisible"_L1, QMetaMethod::Slot}}},
124 {"QTextBrowser"_L1, {{"setSource"_L1, QMetaMethod::Slot}}},
125
126 /*
127 The following widgets with ambiguities are not used in the widget designer:
128
129 {"QSplashScreen"_L1, {{"showMessage"_L1, QMetaMethod::Slot}}},
130 {"QCompleter"_L1, {{"activated"_L1, QMetaMethod::Signal},
131 {"highlighted"_L1, QMetaMethod::Signal}}},
132 {"QSystemTrayIcon"_L1, {{"showMessage"_L1, QMetaMethod::Slot}}},
133 {"QStyledItemDelegate"_L1, {{"closeEditor"_L1, QMetaMethod::Signal}}},
134 {"QErrorMessage"_L1, {{"showMessage"_L1, QMetaMethod::Slot}}},
135 {"QGraphicsDropShadowEffect"_L1, {{"setOffset"_L1, QMetaMethod::Slot}}},
136 {"QGraphicsScene"_L1, {{"invalidate"_L1, QMetaMethod::Slot}}},
137 {"QItemDelegate"_L1, {{"closeEditor"_L1, QMetaMethod::Signal}}}
138 */
139 };
140
141 for (auto it = ambiguousList.constBegin(); it != ambiguousList.constEnd(); ++it) {
142 if (extends(className, it->className)) {
143 const qsizetype pos = signature.indexOf(u'(');
144 const QString method = signature.left(pos);
145 const auto methodIterator = it->methodMap.find(method);
146 return methodIterator != it->methodMap.constEnd() && type == methodIterator.value();
147 }
148 }
149 return false;
150}
151
152// Is it ambiguous, resulting in different signals for Python
153// "QAbstractButton::clicked(checked=false)"
154bool CustomWidgetsInfo::isAmbiguousSignal(const QString &className,
155 const QString &signalSignature) const
156{
157 return isAmbiguous(className, signalSignature, QMetaMethod::Signal);
158}
159
160bool CustomWidgetsInfo::isAmbiguousSlot(const QString &className,
161 const QString &signalSignature) const
162{
163 return isAmbiguous(className, signalSignature, QMetaMethod::Slot);
164}
165
166QString CustomWidgetsInfo::realClassName(const QString &className)
167{
168 if (className == "Line"_L1)
169 return u"QFrame"_s;
170
171 return className;
172}
173
174QString CustomWidgetsInfo::customWidgetAddPageMethod(const QString &name) const
175{
176 if (DomCustomWidget *dcw = m_customWidgets.value(name, nullptr))
177 return dcw->elementAddPageMethod();
178 return {};
179}
180
181// add page methods for simple containers taking only the widget parameter
182QString CustomWidgetsInfo::simpleContainerAddPageMethod(const QString &name) const
183{
184 using AddPageMethod = std::pair<QString, QString>;
185
186 static const AddPageMethod addPageMethods[] = {
187 {u"QStackedWidget"_s, u"addWidget"_s},
188 {u"QToolBar"_s, u"addWidget"_s},
189 {u"QDockWidget"_s, u"setWidget"_s},
190 {u"QScrollArea"_s, u"setWidget"_s},
191 {u"QSplitter"_s, u"addWidget"_s},
192 {u"QMdiArea"_s, u"addSubWindow"_s}
193 };
194 for (const auto &m : addPageMethods) {
195 if (extends(name, m.first))
196 return m.second;
197 }
198 return {};
199}
200
201QT_END_NAMESPACE
bool isCustomWidgetContainer(const QString &className) const
bool isAmbiguousSlot(const QString &className, const QString &slotSignature) const
void acceptUI(DomUI *node) override
bool isAmbiguousSignal(const QString &className, const QString &signalSignature) const
bool extendsOneOf(const QString &className, const QStringList &baseClassNames) const
QString customWidgetAddPageMethod(const QString &name) const
bool extends(const QString &className, QAnyStringView baseClassName) const
QString simpleContainerAddPageMethod(const QString &name) const
void acceptCustomWidgets(DomCustomWidgets *node) override
void acceptCustomWidget(DomCustomWidget *node) override
Definition qlist.h:80
virtual void acceptCustomWidgets(DomCustomWidgets *customWidgets)