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 | |
4 | #include "customwidgetsinfo.h" |
5 | #include "driver.h" |
6 | #include "ui4.h" |
7 | #include "utils.h" |
8 | |
9 | #include <utility> |
10 | |
11 | QT_BEGIN_NAMESPACE |
12 | |
13 | using namespace Qt::StringLiterals; |
14 | |
15 | CustomWidgetsInfo::CustomWidgetsInfo() = default; |
16 | |
17 | void CustomWidgetsInfo::acceptUI(DomUI *node) |
18 | { |
19 | m_customWidgets.clear(); |
20 | |
21 | if (node->elementCustomWidgets()) |
22 | acceptCustomWidgets(node: node->elementCustomWidgets()); |
23 | } |
24 | |
25 | void CustomWidgetsInfo::acceptCustomWidgets(DomCustomWidgets *node) |
26 | { |
27 | TreeWalker::acceptCustomWidgets(customWidgets: node); |
28 | } |
29 | |
30 | void CustomWidgetsInfo::acceptCustomWidget(DomCustomWidget *node) |
31 | { |
32 | if (node->elementClass().isEmpty()) |
33 | return; |
34 | |
35 | m_customWidgets.insert(key: node->elementClass(), value: node); |
36 | } |
37 | |
38 | bool 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(name: 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 | |
55 | bool CustomWidgetsInfo::extendsOneOf(const QString &classNameIn, |
56 | const QStringList &baseClassNames) const |
57 | { |
58 | if (baseClassNames.contains(str: classNameIn)) |
59 | return true; |
60 | |
61 | QString className = classNameIn; |
62 | while (const DomCustomWidget *c = customWidget(name: className)) { |
63 | const QString extends = c->elementExtends(); |
64 | if (className == extends) // Faulty legacy custom widget entries exist. |
65 | return false; |
66 | if (baseClassNames.contains(str: extends)) |
67 | return true; |
68 | className = extends; |
69 | } |
70 | return false; |
71 | } |
72 | |
73 | bool CustomWidgetsInfo::isCustomWidgetContainer(const QString &className) const |
74 | { |
75 | if (const DomCustomWidget *dcw = m_customWidgets.value(key: className, defaultValue: 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. |
83 | bool 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 | {.className: "QAction"_L1 , .methodMap: {{"triggered"_L1 , QMetaMethod::Signal}}}, |
95 | {.className: "QCommandLinkButton"_L1 , .methodMap: {{"triggered"_L1 , QMetaMethod::Signal}, |
96 | {"clicked"_L1 , QMetaMethod::Signal}}}, |
97 | {.className: "QPushButton"_L1 , .methodMap: {{"triggered"_L1 , QMetaMethod::Signal}, |
98 | {"clicked"_L1 , QMetaMethod::Signal}}}, |
99 | {.className: "QCheckBox"_L1 , .methodMap: {{"triggered"_L1 , QMetaMethod::Signal}, |
100 | {"clicked"_L1 , QMetaMethod::Signal}}}, |
101 | {.className: "QRadioButton"_L1 , .methodMap: {{"triggered"_L1 , QMetaMethod::Signal}, |
102 | {"clicked"_L1 , QMetaMethod::Signal}}}, |
103 | {.className: "QToolButton"_L1 , .methodMap: {{"triggered"_L1 , QMetaMethod::Signal}, |
104 | {"clicked"_L1 , QMetaMethod::Signal}}}, |
105 | {.className: "QLabel"_L1 , .methodMap: {{"setNum"_L1 , QMetaMethod::Slot}}}, |
106 | {.className: "QGraphicsView"_L1 , .methodMap: {{"invalidateScene"_L1 , QMetaMethod::Slot}}}, |
107 | {.className: "QListView"_L1 , .methodMap: {{"dataChanged"_L1 , QMetaMethod::Slot}}}, |
108 | {.className: "QColumnView"_L1 , .methodMap: {{"dataChanged"_L1 , QMetaMethod::Slot}}}, |
109 | {.className: "QListWidget"_L1 , .methodMap: {{"dataChanged"_L1 , QMetaMethod::Slot}, |
110 | {"scrollToItem"_L1 , QMetaMethod::Slot}}}, |
111 | {.className: "QTableView"_L1 , .methodMap: {{"dataChanged"_L1 , QMetaMethod::Slot}}}, |
112 | {.className: "QTableWidget"_L1 , .methodMap: {{"dataChanged"_L1 , QMetaMethod::Slot}, |
113 | {"scrollToItem"_L1 , QMetaMethod::Slot}}}, |
114 | {.className: "QTreeView"_L1 , .methodMap: {{"dataChanged"_L1 , QMetaMethod::Slot}, |
115 | {"verticalScrollbarValueChanged"_L1 , QMetaMethod::Slot}, |
116 | {"expandRecursively"_L1 , QMetaMethod::Slot}}}, |
117 | {.className: "QTreeWidget"_L1 , .methodMap: {{"dataChanged"_L1 , QMetaMethod::Slot}, |
118 | {"verticalScrollbarValueChanged"_L1 , QMetaMethod::Slot} |
119 | ,{"expandRecursively"_L1 , QMetaMethod::Slot} |
120 | ,{"scrollToItem"_L1 , QMetaMethod::Slot}}}, |
121 | {.className: "QUndoView"_L1 , .methodMap: {{"dataChanged"_L1 , QMetaMethod::Slot}}}, |
122 | {.className: "QLCDNumber"_L1 , .methodMap: {{"display"_L1 , QMetaMethod::Slot}}}, |
123 | {.className: "QMenuBar"_L1 , .methodMap: {{"setVisible"_L1 , QMetaMethod::Slot}}}, |
124 | {.className: "QTextBrowser"_L1 , .methodMap: {{"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(classNameIn: className, baseClassName: it->className)) { |
143 | const qsizetype pos = signature.indexOf(ch: u'('); |
144 | const QString method = signature.left(n: pos); |
145 | const auto methodIterator = it->methodMap.find(key: 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)" |
154 | bool CustomWidgetsInfo::isAmbiguousSignal(const QString &className, |
155 | const QString &signalSignature) const |
156 | { |
157 | return isAmbiguous(className, signature: signalSignature, type: QMetaMethod::Signal); |
158 | } |
159 | |
160 | bool CustomWidgetsInfo::isAmbiguousSlot(const QString &className, |
161 | const QString &signalSignature) const |
162 | { |
163 | return isAmbiguous(className, signature: signalSignature, type: QMetaMethod::Slot); |
164 | } |
165 | |
166 | QString CustomWidgetsInfo::realClassName(const QString &className) |
167 | { |
168 | if (className == "Line"_L1 ) |
169 | return u"QFrame"_s ; |
170 | |
171 | return className; |
172 | } |
173 | |
174 | QString CustomWidgetsInfo::customWidgetAddPageMethod(const QString &name) const |
175 | { |
176 | if (DomCustomWidget *dcw = m_customWidgets.value(key: name, defaultValue: nullptr)) |
177 | return dcw->elementAddPageMethod(); |
178 | return {}; |
179 | } |
180 | |
181 | // add page methods for simple containers taking only the widget parameter |
182 | QString 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(classNameIn: name, baseClassName: m.first)) |
196 | return m.second; |
197 | } |
198 | return {}; |
199 | } |
200 | |
201 | QT_END_NAMESPACE |
202 | |