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