comparison tools/duic/uic.cpp @ 1:e78566595089

initial import
author mandel
date Mon, 11 May 2009 16:01:50 +0000
parents
children
comparison
equal deleted inserted replaced
0:36fb74dc547d 1:e78566595089
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 "uic.h"
39 #include "ui4.h"
40 #include "driver.h"
41 #include "option.h"
42 #include "treewalker.h"
43 #include "validator.h"
44
45 #ifdef QT_UIC_D_GENERATOR
46 #include "dwriteincludes.h"
47 #include "dwritedeclaration.h"
48 #endif
49
50 #ifdef QT_UIC_JAVA_GENERATOR
51 #include "javawriteincludes.h"
52 #include "javawritedeclaration.h"
53 #endif
54
55 #include <QtXml/QDomDocument>
56 #include <QtCore/QFileInfo>
57 #include <QtCore/QRegExp>
58 #include <QtCore/QTextStream>
59 #include <QtCore/QDateTime>
60
61 #if defined Q_WS_WIN
62 #include <qt_windows.h>
63 #endif
64
65 QT_BEGIN_NAMESPACE
66
67 Uic::Uic(Driver *d)
68 : drv(d),
69 out(d->output()),
70 opt(d->option()),
71 info(d),
72 externalPix(true)
73 {
74 }
75
76 Uic::~Uic()
77 {
78 }
79
80 bool Uic::printDependencies()
81 {
82 QString fileName = opt.inputFile;
83
84 QFile f;
85 if (fileName.isEmpty())
86 f.open(stdin, QIODevice::ReadOnly);
87 else {
88 f.setFileName(fileName);
89 if (!f.open(QIODevice::ReadOnly))
90 return false;
91 }
92
93 QDomDocument doc; // ### generalize. share more code with the other tools!
94 if (!doc.setContent(&f))
95 return false;
96
97 QDomElement root = doc.firstChildElement();
98 DomUI *ui = new DomUI();
99 ui->read(root);
100
101 double version = ui->attributeVersion().toDouble();
102 if (version < 4.0) {
103 delete ui;
104
105 fprintf(stderr, "uic: File generated with too old version of Qt Designer\n");
106 return false;
107 }
108
109 if (DomIncludes *includes = ui->elementIncludes()) {
110 foreach (DomInclude *incl, includes->elementInclude()) {
111 QString file = incl->text();
112 if (file.isEmpty())
113 continue;
114
115 fprintf(stdout, "%s\n", file.toLocal8Bit().constData());
116 }
117 }
118
119 if (DomCustomWidgets *customWidgets = ui->elementCustomWidgets()) {
120 foreach (DomCustomWidget *customWidget, customWidgets->elementCustomWidget()) {
121 if (DomHeader *header = customWidget->elementHeader()) {
122 QString file = header->text();
123 if (file.isEmpty())
124 continue;
125
126 fprintf(stdout, "%s\n", file.toLocal8Bit().constData());
127 }
128 }
129 }
130
131 delete ui;
132
133 return true;
134 }
135
136 void Uic::writeCopyrightHeader(DomUI *ui)
137 {
138 QString comment = ui->elementComment();
139 if (comment.size())
140 out << "/*\n" << comment << "\n*/\n\n";
141
142 out << "/********************************************************************************\n";
143 out << "** Form generated from reading ui file '" << QFileInfo(opt.inputFile).fileName() << "'\n";
144 out << "**\n";
145 out << "** Created: " << QDateTime::currentDateTime().toString() << "\n";
146 out << "** " << QString::fromLatin1("by: QtD User Interface Compiler version %1\n").arg(QLatin1String(QT_VERSION_STR));
147 out << "**\n";
148 out << "** WARNING! All changes made in this file will be lost when recompiling ui file!\n";
149 out << "********************************************************************************/\n\n";
150 }
151
152 bool Uic::write(QIODevice *in)
153 {
154 QDomDocument doc;
155 if (!doc.setContent(in))
156 return false;
157
158 if (option().generator == Option::JavaGenerator || option().generator == Option::DGenerator) {
159 // the Java generator ignores header protection
160 opt.headerProtection = false;
161 }
162
163 QDomElement root = doc.firstChildElement();
164 DomUI *ui = new DomUI();
165 ui->read(root);
166
167 double version = ui->attributeVersion().toDouble();
168 if (version < 4.0) {
169 delete ui;
170
171 fprintf(stderr, "uic: File generated with too old version of Qt Designer\n");
172 return false;
173 }
174
175 QString language = ui->attributeLanguage();
176
177
178 bool rtn = false;
179
180 if (option().generator == Option::JavaGenerator) {
181 #ifdef QT_UIC_JAVA_GENERATOR
182 if (language.toLower() != QLatin1String("jambi")) {
183 fprintf(stderr, "uic: File is not a 'jambi' form\n");
184 return false;
185 }
186 rtn = jwrite (ui);
187 #else
188 fprintf(stderr, "uic: option to generate java code not compiled in\n");
189 #endif
190 } else {
191 #ifdef QT_UIC_D_GENERATOR
192 if (!language.isEmpty() && language.toLower() != QLatin1String("d")) {
193 fprintf(stderr, "uic: File is not a 'd' ui file, language=%s\n", qPrintable(language));
194 return false;
195 }
196
197 rtn = write (ui);
198 #else
199 fprintf(stderr, "uic: option to generate d code not compiled in\n");
200 #endif
201 }
202
203 delete ui;
204
205 return rtn;
206 }
207
208 #ifdef QT_UIC_D_GENERATOR
209 bool Uic::write(DomUI *ui)
210 {
211 using namespace D;
212
213 if (!ui || !ui->elementWidget())
214 return false;
215
216 if (opt.copyrightHeader)
217 writeCopyrightHeader(ui);
218
219 if (opt.headerProtection) {
220 writeHeaderProtectionStart();
221 out << "\n";
222 }
223
224 pixFunction = ui->elementPixmapFunction();
225 if (pixFunction == QLatin1String("QPixmap::fromMimeSource"))
226 pixFunction = QLatin1String("qPixmapFromMimeSource");
227
228 externalPix = ui->elementImages() == 0;
229
230 info.acceptUI(ui);
231 cWidgetsInfo.acceptUI(ui);
232 WriteIncludes writeIncludes(this);
233 writeIncludes.acceptUI(ui);
234
235 Validator(this).acceptUI(ui);
236 WriteDeclaration(this, writeIncludes.scriptsActivated()).acceptUI(ui);
237
238 if (opt.headerProtection)
239 writeHeaderProtectionEnd();
240
241 return true;
242 }
243 #endif
244
245 #ifdef QT_UIC_JAVA_GENERATOR
246 bool Uic::jwrite(DomUI *ui)
247 {
248 using namespace Java;
249
250 if (!ui || !ui->elementWidget())
251 return false;
252
253 if (opt.copyrightHeader)
254 writeCopyrightHeader(ui);
255
256 pixFunction = ui->elementPixmapFunction();
257 if (pixFunction == QLatin1String("QPixmap::fromMimeSource"))
258 pixFunction = QLatin1String("qPixmapFromMimeSource");
259
260 externalPix = ui->elementImages() == 0;
261
262 info.acceptUI(ui);
263 cWidgetsInfo.acceptUI(ui);
264 WriteIncludes(this).acceptUI(ui);
265
266 Validator(this).acceptUI(ui);
267 WriteDeclaration(this).acceptUI(ui);
268
269 return true;
270 }
271 #endif
272
273 #ifdef QT_UIC_D_GENERATOR
274
275 void Uic::writeHeaderProtectionStart()
276 {
277 QString h = drv->headerFileName();
278 out << "#ifndef " << h << "\n"
279 << "#define " << h << "\n";
280 }
281
282 void Uic::writeHeaderProtectionEnd()
283 {
284 QString h = drv->headerFileName();
285 out << "#endif // " << h << "\n";
286 }
287 #endif
288
289 bool Uic::isMainWindow(const QString &className) const
290 {
291 return customWidgetsInfo()->extends(className, QLatin1String("Q3MainWindow"))
292 || customWidgetsInfo()->extends(className, QLatin1String("QMainWindow"));
293 }
294
295 bool Uic::isToolBar(const QString &className) const
296 {
297 return customWidgetsInfo()->extends(className, QLatin1String("Q3ToolBar"))
298 || customWidgetsInfo()->extends(className, QLatin1String("QToolBar"));
299 }
300
301 bool Uic::isButton(const QString &className) const
302 {
303 return customWidgetsInfo()->extends(className, QLatin1String("QRadioButton"))
304 || customWidgetsInfo()->extends(className, QLatin1String("QToolButton"))
305 || customWidgetsInfo()->extends(className, QLatin1String("QCheckBox"))
306 || customWidgetsInfo()->extends(className, QLatin1String("QPushButton"));
307 }
308
309 bool Uic::isContainer(const QString &className) const
310 {
311 return customWidgetsInfo()->extends(className, QLatin1String("QStackedWidget"))
312 || customWidgetsInfo()->extends(className, QLatin1String("QToolBox"))
313 || customWidgetsInfo()->extends(className, QLatin1String("QTabWidget"))
314 || customWidgetsInfo()->extends(className, QLatin1String("QScrollArea"))
315 || customWidgetsInfo()->extends(className, QLatin1String("QMdiArea"))
316 || customWidgetsInfo()->extends(className, QLatin1String("QWizard"))
317 || customWidgetsInfo()->extends(className, QLatin1String("QDockWidget"));
318 }
319
320 bool Uic::isStatusBar(const QString &className) const
321 {
322 return customWidgetsInfo()->extends(className, QLatin1String("QStatusBar"));
323 }
324
325 bool Uic::isMenuBar(const QString &className) const
326 {
327 return customWidgetsInfo()->extends(className, QLatin1String("QMenuBar"));
328 }
329
330 bool Uic::isMenu(const QString &className) const
331 {
332 return customWidgetsInfo()->extends(className, QLatin1String("QMenu"))
333 || customWidgetsInfo()->extends(className, QLatin1String("QPopupMenu"));
334 }
335
336 QT_END_NAMESPACE