|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
4 ** All rights reserved. |
|
5 ** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 ** |
|
7 ** This file is part of the tools applications of the Qt Toolkit. |
|
8 ** |
|
9 ** $QT_BEGIN_LICENSE:LGPL$ |
|
10 ** No Commercial Usage |
|
11 ** This file contains pre-release code and may not be distributed. |
|
12 ** You may use this file in accordance with the terms and conditions |
|
13 ** contained in the Technology Preview License Agreement accompanying |
|
14 ** this package. |
|
15 ** |
|
16 ** GNU Lesser General Public License Usage |
|
17 ** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 ** General Public License version 2.1 as published by the Free Software |
|
19 ** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 ** packaging of this file. Please review the following information to |
|
21 ** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 ** |
|
24 ** In addition, as a special exception, Nokia gives you certain additional |
|
25 ** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 ** |
|
28 ** If you have questions regarding the use of this file, please contact |
|
29 ** Nokia at qt-info@nokia.com. |
|
30 ** |
|
31 ** |
|
32 ** |
|
33 ** |
|
34 ** |
|
35 ** |
|
36 ** |
|
37 ** |
|
38 ** $QT_END_LICENSE$ |
|
39 ** |
|
40 ****************************************************************************/ |
|
41 |
|
42 #include "cppwritedeclaration.h" |
|
43 #include "cppwriteicondeclaration.h" |
|
44 #include "cppwriteinitialization.h" |
|
45 #include "cppwriteiconinitialization.h" |
|
46 #include "cppextractimages.h" |
|
47 #include "driver.h" |
|
48 #include "ui4.h" |
|
49 #include "uic.h" |
|
50 #include "databaseinfo.h" |
|
51 #include "customwidgetsinfo.h" |
|
52 |
|
53 #include <QtCore/QTextStream> |
|
54 #include <QtCore/QDebug> |
|
55 |
|
56 QT_BEGIN_NAMESPACE |
|
57 |
|
58 namespace { |
|
59 void openNameSpaces(const QStringList &namespaceList, QTextStream &output) { |
|
60 if (namespaceList.empty()) |
|
61 return; |
|
62 const QStringList::const_iterator cend = namespaceList.constEnd(); |
|
63 for (QStringList::const_iterator it = namespaceList.constBegin(); it != cend; ++it) { |
|
64 if (!it->isEmpty()) { |
|
65 output << "namespace " << *it << " {\n"; |
|
66 } |
|
67 } |
|
68 } |
|
69 |
|
70 void closeNameSpaces(const QStringList &namespaceList, QTextStream &output) { |
|
71 if (namespaceList.empty()) |
|
72 return; |
|
73 |
|
74 QListIterator<QString> it(namespaceList); |
|
75 it.toBack(); |
|
76 while (it.hasPrevious()) { |
|
77 const QString ns = it.previous(); |
|
78 if (!ns.isEmpty()) { |
|
79 output << "} // namespace " << ns << "\n"; |
|
80 } |
|
81 } |
|
82 } |
|
83 |
|
84 void writeScriptContextClass(const QString &indent, QTextStream &str) { |
|
85 str << indent << "class ScriptContext\n" |
|
86 << indent << "{\n" |
|
87 << indent << "public:\n" |
|
88 << indent << " void run(const QString &script, QWidget *widget, const QWidgetList &childWidgets)\n" |
|
89 << indent << " {\n" |
|
90 << indent << " QScriptValue widgetObject = scriptEngine.newQObject(widget);\n" |
|
91 << indent << " QScriptValue childWidgetArray = scriptEngine.newArray (childWidgets.size());\n" |
|
92 << indent << " for (int i = 0; i < childWidgets.size(); i++)\n" |
|
93 << indent << " childWidgetArray.setProperty(i, scriptEngine.newQObject(childWidgets[i]));\n" |
|
94 << indent << " QScriptContext *ctx = scriptEngine.pushContext();\n" |
|
95 << indent << " ctx ->activationObject().setProperty(QLatin1String(\"widget\"), widgetObject);\n" |
|
96 << indent << " ctx ->activationObject().setProperty(QLatin1String(\"childWidgets\"), childWidgetArray);\n\n" |
|
97 << indent << " scriptEngine.evaluate(script);\n" |
|
98 << indent << " if (scriptEngine.hasUncaughtException ()) {\n" |
|
99 << indent << " qWarning() << \"An exception occurred at line \" << scriptEngine.uncaughtExceptionLineNumber()\n" |
|
100 << indent << " << \" of the script for \" << widget->objectName() << \": \" << engineError() << '\\n'\n" |
|
101 << indent << " << script;\n" |
|
102 << indent << " }\n\n" |
|
103 << indent << " scriptEngine.popContext();\n" |
|
104 << indent << " }\n\n" |
|
105 << indent << "private:\n" |
|
106 << indent << " QString engineError()\n" |
|
107 << indent << " {\n" |
|
108 << indent << " QScriptValue error = scriptEngine.evaluate(\"Error\");\n" |
|
109 << indent << " return error.toString();\n" |
|
110 << indent << " }\n\n" |
|
111 << indent << " QScriptEngine scriptEngine;\n" |
|
112 << indent << "};\n\n"; |
|
113 } |
|
114 } |
|
115 |
|
116 namespace CPP { |
|
117 |
|
118 WriteDeclaration::WriteDeclaration(Uic *uic, bool activateScripts) : |
|
119 m_uic(uic), |
|
120 m_driver(uic->driver()), |
|
121 m_output(uic->output()), |
|
122 m_option(uic->option()), |
|
123 m_activateScripts(activateScripts) |
|
124 { |
|
125 } |
|
126 |
|
127 void WriteDeclaration::acceptUI(DomUI *node) |
|
128 { |
|
129 QString qualifiedClassName = node->elementClass() + m_option.postfix; |
|
130 QString className = qualifiedClassName; |
|
131 |
|
132 QString varName = m_driver->findOrInsertWidget(node->elementWidget()); |
|
133 QString widgetClassName = node->elementWidget()->attributeClass(); |
|
134 |
|
135 QString exportMacro = node->elementExportMacro(); |
|
136 if (!exportMacro.isEmpty()) |
|
137 exportMacro.append(QLatin1Char(' ')); |
|
138 |
|
139 QStringList namespaceList = qualifiedClassName.split(QLatin1String("::")); |
|
140 if (namespaceList.count()) { |
|
141 className = namespaceList.last(); |
|
142 namespaceList.removeLast(); |
|
143 } |
|
144 |
|
145 // This is a bit of the hack but covers the cases Qt in/without namespaces |
|
146 // and User defined classes in/without namespaces. The "strange" case |
|
147 // is a User using Qt-in-namespace having his own classes not in a namespace. |
|
148 // In this case the generated Ui helper classes will also end up in |
|
149 // the Qt namespace (which is harmless, but not "pretty") |
|
150 const bool needsMacro = namespaceList.count() == 0 |
|
151 || namespaceList[0] == QLatin1String("qdesigner_internal"); |
|
152 |
|
153 if (needsMacro) |
|
154 m_output << "QT_BEGIN_NAMESPACE\n\n"; |
|
155 |
|
156 openNameSpaces(namespaceList, m_output); |
|
157 |
|
158 if (namespaceList.count()) |
|
159 m_output << "\n"; |
|
160 |
|
161 m_output << "class " << exportMacro << m_option.prefix << className << "\n" |
|
162 << "{\n" |
|
163 << "public:\n"; |
|
164 |
|
165 const QStringList connections = m_uic->databaseInfo()->connections(); |
|
166 for (int i=0; i<connections.size(); ++i) { |
|
167 const QString connection = connections.at(i); |
|
168 |
|
169 if (connection == QLatin1String("(default)")) |
|
170 continue; |
|
171 |
|
172 m_output << m_option.indent << "QSqlDatabase " << connection << "Connection;\n"; |
|
173 } |
|
174 |
|
175 TreeWalker::acceptWidget(node->elementWidget()); |
|
176 if (const DomButtonGroups *domButtonGroups = node->elementButtonGroups()) |
|
177 acceptButtonGroups(domButtonGroups); |
|
178 |
|
179 m_output << "\n"; |
|
180 |
|
181 WriteInitialization(m_uic, m_activateScripts).acceptUI(node); |
|
182 |
|
183 if (node->elementImages()) { |
|
184 if (m_option.extractImages) { |
|
185 ExtractImages(m_uic->option()).acceptUI(node); |
|
186 } else { |
|
187 m_output << "\n" |
|
188 << "protected:\n" |
|
189 << m_option.indent << "enum IconID\n" |
|
190 << m_option.indent << "{\n"; |
|
191 WriteIconDeclaration(m_uic).acceptUI(node); |
|
192 |
|
193 m_output << m_option.indent << m_option.indent << "unknown_ID\n" |
|
194 << m_option.indent << "};\n"; |
|
195 |
|
196 WriteIconInitialization(m_uic).acceptUI(node); |
|
197 } |
|
198 } |
|
199 |
|
200 if (m_activateScripts) { |
|
201 m_output << "\nprivate:\n\n"; |
|
202 writeScriptContextClass(m_option.indent, m_output); |
|
203 } |
|
204 |
|
205 m_output << "};\n\n"; |
|
206 |
|
207 closeNameSpaces(namespaceList, m_output); |
|
208 |
|
209 if (namespaceList.count()) |
|
210 m_output << "\n"; |
|
211 |
|
212 if (m_option.generateNamespace && !m_option.prefix.isEmpty()) { |
|
213 namespaceList.append(QLatin1String("Ui")); |
|
214 |
|
215 openNameSpaces(namespaceList, m_output); |
|
216 |
|
217 m_output << m_option.indent << "class " << exportMacro << className << ": public " << m_option.prefix << className << " {};\n"; |
|
218 |
|
219 closeNameSpaces(namespaceList, m_output); |
|
220 |
|
221 if (namespaceList.count()) |
|
222 m_output << "\n"; |
|
223 } |
|
224 |
|
225 if (needsMacro) |
|
226 m_output << "QT_END_NAMESPACE\n\n"; |
|
227 } |
|
228 |
|
229 void WriteDeclaration::acceptWidget(DomWidget *node) |
|
230 { |
|
231 QString className = QLatin1String("QWidget"); |
|
232 if (node->hasAttributeClass()) |
|
233 className = node->attributeClass(); |
|
234 |
|
235 m_output << m_option.indent << m_uic->customWidgetsInfo()->realClassName(className) << " *" << m_driver->findOrInsertWidget(node) << ";\n"; |
|
236 |
|
237 TreeWalker::acceptWidget(node); |
|
238 } |
|
239 |
|
240 void WriteDeclaration::acceptSpacer(DomSpacer *node) |
|
241 { |
|
242 m_output << m_option.indent << "QSpacerItem *" << m_driver->findOrInsertSpacer(node) << ";\n"; |
|
243 TreeWalker::acceptSpacer(node); |
|
244 } |
|
245 |
|
246 void WriteDeclaration::acceptLayout(DomLayout *node) |
|
247 { |
|
248 QString className = QLatin1String("QLayout"); |
|
249 if (node->hasAttributeClass()) |
|
250 className = node->attributeClass(); |
|
251 |
|
252 m_output << m_option.indent << className << " *" << m_driver->findOrInsertLayout(node) << ";\n"; |
|
253 |
|
254 TreeWalker::acceptLayout(node); |
|
255 } |
|
256 |
|
257 void WriteDeclaration::acceptActionGroup(DomActionGroup *node) |
|
258 { |
|
259 m_output << m_option.indent << "QActionGroup *" << m_driver->findOrInsertActionGroup(node) << ";\n"; |
|
260 |
|
261 TreeWalker::acceptActionGroup(node); |
|
262 } |
|
263 |
|
264 void WriteDeclaration::acceptAction(DomAction *node) |
|
265 { |
|
266 m_output << m_option.indent << "QAction *" << m_driver->findOrInsertAction(node) << ";\n"; |
|
267 |
|
268 TreeWalker::acceptAction(node); |
|
269 } |
|
270 |
|
271 void WriteDeclaration::acceptButtonGroup(const DomButtonGroup *buttonGroup) |
|
272 { |
|
273 m_output << m_option.indent << "QButtonGroup *" << m_driver->findOrInsertButtonGroup(buttonGroup) << ";\n"; |
|
274 TreeWalker::acceptButtonGroup(buttonGroup); |
|
275 } |
|
276 |
|
277 } // namespace CPP |
|
278 |
|
279 QT_END_NAMESPACE |