1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2016 The Qt Company Ltd. |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the tools applications of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:GPL-EXCEPT$ |
9 | ** Commercial License Usage |
10 | ** Licensees holding valid commercial Qt licenses may use this file in |
11 | ** accordance with the commercial license agreement provided with the |
12 | ** Software or, alternatively, in accordance with the terms contained in |
13 | ** a written agreement between you and The Qt Company. For licensing terms |
14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at https://www.qt.io/contact-us. |
16 | ** |
17 | ** GNU General Public License Usage |
18 | ** Alternatively, this file may be used under the terms of the GNU |
19 | ** General Public License version 3 as published by the Free Software |
20 | ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT |
21 | ** included in the packaging of this file. Please review the following |
22 | ** information to ensure the GNU General Public License requirements will |
23 | ** be met: https://www.gnu.org/licenses/gpl-3.0.html. |
24 | ** |
25 | ** $QT_END_LICENSE$ |
26 | ** |
27 | ****************************************************************************/ |
28 | |
29 | #include "cppwritedeclaration.h" |
30 | #include "cppwriteinitialization.h" |
31 | #include "driver.h" |
32 | #include "ui4.h" |
33 | #include "uic.h" |
34 | #include "databaseinfo.h" |
35 | #include "customwidgetsinfo.h" |
36 | |
37 | #include <qtextstream.h> |
38 | #include <qdebug.h> |
39 | |
40 | QT_BEGIN_NAMESPACE |
41 | |
42 | namespace { |
43 | void openNameSpaces(const QStringList &namespaceList, QTextStream &output) |
44 | { |
45 | for (const QString &n : namespaceList) { |
46 | if (!n.isEmpty()) |
47 | output << "namespace " << n << " {\n" ; |
48 | } |
49 | } |
50 | |
51 | void closeNameSpaces(const QStringList &namespaceList, QTextStream &output) { |
52 | for (auto it = namespaceList.rbegin(), end = namespaceList.rend(); it != end; ++it) { |
53 | if (!it->isEmpty()) |
54 | output << "} // namespace " << *it << "\n" ; |
55 | } |
56 | } |
57 | } |
58 | |
59 | namespace CPP { |
60 | |
61 | WriteDeclaration::WriteDeclaration(Uic *uic) : |
62 | m_uic(uic), |
63 | m_driver(uic->driver()), |
64 | m_output(uic->output()), |
65 | m_option(uic->option()) |
66 | { |
67 | } |
68 | |
69 | void WriteDeclaration::acceptUI(DomUI *node) |
70 | { |
71 | QString qualifiedClassName = node->elementClass() + m_option.postfix; |
72 | QString className = qualifiedClassName; |
73 | |
74 | m_driver->findOrInsertWidget(node->elementWidget()); |
75 | |
76 | QString exportMacro = node->elementExportMacro(); |
77 | if (!exportMacro.isEmpty()) |
78 | exportMacro.append(QLatin1Char(' ')); |
79 | |
80 | QStringList namespaceList = qualifiedClassName.split(QLatin1String("::" )); |
81 | if (namespaceList.count()) { |
82 | className = namespaceList.last(); |
83 | namespaceList.removeLast(); |
84 | } |
85 | |
86 | // This is a bit of the hack but covers the cases Qt in/without namespaces |
87 | // and User defined classes in/without namespaces. The "strange" case |
88 | // is a User using Qt-in-namespace having his own classes not in a namespace. |
89 | // In this case the generated Ui helper classes will also end up in |
90 | // the Qt namespace (which is harmless, but not "pretty") |
91 | const bool needsMacro = namespaceList.count() == 0 |
92 | || namespaceList[0] == QLatin1String("qdesigner_internal" ); |
93 | |
94 | if (needsMacro) |
95 | m_output << "QT_BEGIN_NAMESPACE\n\n" ; |
96 | |
97 | openNameSpaces(namespaceList, m_output); |
98 | |
99 | if (namespaceList.count()) |
100 | m_output << "\n" ; |
101 | |
102 | m_output << "class " << exportMacro << m_option.prefix << className << "\n" |
103 | << "{\n" |
104 | << "public:\n" ; |
105 | |
106 | const QStringList connections = m_uic->databaseInfo()->connections(); |
107 | for (const QString &connection : connections) { |
108 | if (connection != QLatin1String("(default)" )) |
109 | m_output << m_option.indent << "QSqlDatabase " << connection << "Connection;\n" ; |
110 | } |
111 | |
112 | TreeWalker::acceptWidget(node->elementWidget()); |
113 | if (const DomButtonGroups *domButtonGroups = node->elementButtonGroups()) |
114 | acceptButtonGroups(domButtonGroups); |
115 | |
116 | m_output << "\n" ; |
117 | |
118 | WriteInitialization(m_uic).acceptUI(node); |
119 | |
120 | m_output << "};\n\n" ; |
121 | |
122 | closeNameSpaces(namespaceList, m_output); |
123 | |
124 | if (namespaceList.count()) |
125 | m_output << "\n" ; |
126 | |
127 | if (m_option.generateNamespace && !m_option.prefix.isEmpty()) { |
128 | namespaceList.append(QLatin1String("Ui" )); |
129 | |
130 | openNameSpaces(namespaceList, m_output); |
131 | |
132 | m_output << m_option.indent << "class " << exportMacro << className << ": public " << m_option.prefix << className << " {};\n" ; |
133 | |
134 | closeNameSpaces(namespaceList, m_output); |
135 | |
136 | if (namespaceList.count()) |
137 | m_output << "\n" ; |
138 | } |
139 | |
140 | if (needsMacro) |
141 | m_output << "QT_END_NAMESPACE\n\n" ; |
142 | } |
143 | |
144 | void WriteDeclaration::acceptWidget(DomWidget *node) |
145 | { |
146 | QString className = QLatin1String("QWidget" ); |
147 | if (node->hasAttributeClass()) |
148 | className = node->attributeClass(); |
149 | |
150 | m_output << m_option.indent << m_uic->customWidgetsInfo()->realClassName(className) << " *" << m_driver->findOrInsertWidget(node) << ";\n" ; |
151 | |
152 | TreeWalker::acceptWidget(node); |
153 | } |
154 | |
155 | void WriteDeclaration::acceptSpacer(DomSpacer *node) |
156 | { |
157 | m_output << m_option.indent << "QSpacerItem *" << m_driver->findOrInsertSpacer(node) << ";\n" ; |
158 | TreeWalker::acceptSpacer(node); |
159 | } |
160 | |
161 | void WriteDeclaration::acceptLayout(DomLayout *node) |
162 | { |
163 | QString className = QLatin1String("QLayout" ); |
164 | if (node->hasAttributeClass()) |
165 | className = node->attributeClass(); |
166 | |
167 | m_output << m_option.indent << className << " *" << m_driver->findOrInsertLayout(node) << ";\n" ; |
168 | |
169 | TreeWalker::acceptLayout(node); |
170 | } |
171 | |
172 | void WriteDeclaration::acceptActionGroup(DomActionGroup *node) |
173 | { |
174 | m_output << m_option.indent << "QActionGroup *" << m_driver->findOrInsertActionGroup(node) << ";\n" ; |
175 | |
176 | TreeWalker::acceptActionGroup(node); |
177 | } |
178 | |
179 | void WriteDeclaration::acceptAction(DomAction *node) |
180 | { |
181 | m_output << m_option.indent << "QAction *" << m_driver->findOrInsertAction(node) << ";\n" ; |
182 | |
183 | TreeWalker::acceptAction(node); |
184 | } |
185 | |
186 | void WriteDeclaration::acceptButtonGroup(const DomButtonGroup *buttonGroup) |
187 | { |
188 | m_output << m_option.indent << "QButtonGroup *" << m_driver->findOrInsertButtonGroup(buttonGroup) << ";\n" ; |
189 | TreeWalker::acceptButtonGroup(buttonGroup); |
190 | } |
191 | |
192 | } // namespace CPP |
193 | |
194 | QT_END_NAMESPACE |
195 | |