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 "cppwritedeclaration.h" |
5 | #include "cppwriteinitialization.h" |
6 | #include "driver.h" |
7 | #include "ui4.h" |
8 | #include "uic.h" |
9 | #include "databaseinfo.h" |
10 | #include "customwidgetsinfo.h" |
11 | |
12 | #include <qtextstream.h> |
13 | #include <qdebug.h> |
14 | |
15 | QT_BEGIN_NAMESPACE |
16 | |
17 | using namespace Qt::StringLiterals; |
18 | |
19 | namespace { |
20 | void openNameSpaces(const QStringList &namespaceList, QTextStream &output) |
21 | { |
22 | for (const QString &n : namespaceList) { |
23 | if (!n.isEmpty()) |
24 | output << "namespace " << n << " {\n" ; |
25 | } |
26 | } |
27 | |
28 | void closeNameSpaces(const QStringList &namespaceList, QTextStream &output) { |
29 | for (auto it = namespaceList.rbegin(), end = namespaceList.rend(); it != end; ++it) { |
30 | if (!it->isEmpty()) |
31 | output << "} // namespace " << *it << "\n" ; |
32 | } |
33 | } |
34 | } |
35 | |
36 | namespace CPP { |
37 | |
38 | WriteDeclaration::WriteDeclaration(Uic *uic) : |
39 | m_uic(uic), |
40 | m_driver(uic->driver()), |
41 | m_output(uic->output()), |
42 | m_option(uic->option()) |
43 | { |
44 | } |
45 | |
46 | void WriteDeclaration::acceptUI(DomUI *node) |
47 | { |
48 | QString qualifiedClassName = node->elementClass() + m_option.postfix; |
49 | QString className = qualifiedClassName; |
50 | |
51 | m_driver->findOrInsertWidget(ui_widget: node->elementWidget()); |
52 | |
53 | QString exportMacro = node->elementExportMacro(); |
54 | if (!exportMacro.isEmpty()) |
55 | exportMacro.append(c: u' '); |
56 | |
57 | QStringList namespaceList = qualifiedClassName.split(sep: "::"_L1 ); |
58 | if (namespaceList.size()) { |
59 | className = namespaceList.last(); |
60 | namespaceList.removeLast(); |
61 | } |
62 | |
63 | // This is a bit of the hack but covers the cases Qt in/without namespaces |
64 | // and User defined classes in/without namespaces. The "strange" case |
65 | // is a User using Qt-in-namespace having his own classes not in a namespace. |
66 | // In this case the generated Ui helper classes will also end up in |
67 | // the Qt namespace (which is harmless, but not "pretty") |
68 | const bool needsMacro = m_option.qtNamespace && |
69 | (namespaceList.size() == 0 || namespaceList[0] == "qdesigner_internal"_L1 ); |
70 | |
71 | if (needsMacro) |
72 | m_output << "QT_BEGIN_NAMESPACE\n\n" ; |
73 | |
74 | openNameSpaces(namespaceList, output&: m_output); |
75 | |
76 | if (namespaceList.size()) |
77 | m_output << "\n" ; |
78 | |
79 | m_output << "class " << exportMacro << m_option.prefix << className << "\n" |
80 | << "{\n" |
81 | << "public:\n" ; |
82 | |
83 | const QStringList connections = m_uic->databaseInfo()->connections(); |
84 | for (const QString &connection : connections) { |
85 | if (connection != "(default)"_L1 ) |
86 | m_output << m_option.indent << "QSqlDatabase " << connection << "Connection;\n" ; |
87 | } |
88 | |
89 | TreeWalker::acceptWidget(widget: node->elementWidget()); |
90 | if (const DomButtonGroups *domButtonGroups = node->elementButtonGroups()) |
91 | acceptButtonGroups(buttonGroups: domButtonGroups); |
92 | |
93 | m_output << "\n" ; |
94 | |
95 | WriteInitialization(m_uic).acceptUI(node); |
96 | |
97 | m_output << "};\n\n" ; |
98 | |
99 | closeNameSpaces(namespaceList, output&: m_output); |
100 | |
101 | if (namespaceList.size()) |
102 | m_output << "\n" ; |
103 | |
104 | if (m_option.generateNamespace && !m_option.prefix.isEmpty()) { |
105 | namespaceList.append(t: "Ui"_L1 ); |
106 | |
107 | openNameSpaces(namespaceList, output&: m_output); |
108 | |
109 | m_output << m_option.indent << "class " << exportMacro << className << ": public " << m_option.prefix << className << " {};\n" ; |
110 | |
111 | closeNameSpaces(namespaceList, output&: m_output); |
112 | |
113 | if (namespaceList.size()) |
114 | m_output << "\n" ; |
115 | } |
116 | |
117 | if (needsMacro) |
118 | m_output << "QT_END_NAMESPACE\n\n" ; |
119 | } |
120 | |
121 | void WriteDeclaration::acceptWidget(DomWidget *node) |
122 | { |
123 | QString className = u"QWidget"_s ; |
124 | if (node->hasAttributeClass()) |
125 | className = node->attributeClass(); |
126 | |
127 | m_output << m_option.indent << m_uic->customWidgetsInfo()->realClassName(className) << " *" << m_driver->findOrInsertWidget(ui_widget: node) << ";\n" ; |
128 | |
129 | TreeWalker::acceptWidget(widget: node); |
130 | } |
131 | |
132 | void WriteDeclaration::acceptSpacer(DomSpacer *node) |
133 | { |
134 | m_output << m_option.indent << "QSpacerItem *" << m_driver->findOrInsertSpacer(ui_spacer: node) << ";\n" ; |
135 | TreeWalker::acceptSpacer(spacer: node); |
136 | } |
137 | |
138 | void WriteDeclaration::acceptLayout(DomLayout *node) |
139 | { |
140 | QString className = u"QLayout"_s ; |
141 | if (node->hasAttributeClass()) |
142 | className = node->attributeClass(); |
143 | |
144 | m_output << m_option.indent << className << " *" << m_driver->findOrInsertLayout(ui_layout: node) << ";\n" ; |
145 | |
146 | TreeWalker::acceptLayout(layout: node); |
147 | } |
148 | |
149 | void WriteDeclaration::acceptActionGroup(DomActionGroup *node) |
150 | { |
151 | m_output << m_option.indent << "QActionGroup *" << m_driver->findOrInsertActionGroup(ui_group: node) << ";\n" ; |
152 | |
153 | TreeWalker::acceptActionGroup(actionGroup: node); |
154 | } |
155 | |
156 | void WriteDeclaration::acceptAction(DomAction *node) |
157 | { |
158 | m_output << m_option.indent << "QAction *" << m_driver->findOrInsertAction(ui_action: node) << ";\n" ; |
159 | |
160 | TreeWalker::acceptAction(action: node); |
161 | } |
162 | |
163 | void WriteDeclaration::acceptButtonGroup(const DomButtonGroup *buttonGroup) |
164 | { |
165 | m_output << m_option.indent << "QButtonGroup *" << m_driver->findOrInsertButtonGroup(ui_group: buttonGroup) << ";\n" ; |
166 | TreeWalker::acceptButtonGroup(buttonGroup); |
167 | } |
168 | |
169 | } // namespace CPP |
170 | |
171 | QT_END_NAMESPACE |
172 | |