1
/****************************************************************************
3
** Copyright (C) 1992-2005 Trolltech AS. All rights reserved.
5
** This file is part of the tools applications of the Qt Toolkit.
7
** This file may be distributed under the terms of the Q Public License
8
** as defined by Trolltech AS of Norway and appearing in the file
9
** LICENSE.QPL included in the packaging of this file.
11
** This file may be distributed and/or modified under the terms of the
12
** GNU General Public License version 2 as published by the Free Software
13
** Foundation and appearing in the file LICENSE.GPL included in the
14
** packaging of this file.
16
** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
17
** information about Qt Commercial License Agreements.
18
** See http://www.trolltech.com/qpl/ for QPL licensing information.
19
** See http://www.trolltech.com/gpl/ for GPL licensing information.
21
** Contact info@trolltech.com if any conditions of this licensing are
24
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
25
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
27
****************************************************************************/
29
#include "ui3reader.h"
33
#include "widgetinfo.h"
34
#include "globaldefs.h"
38
#include <qdatetime.h>
45
bool Ui3Reader::isMainWindow = false;
47
void Ui3Reader::errorInvalidProperty(const QString &propertyName, const QString &widgetName, const QString &widgetClass)
49
fprintf(stderr, "uic3: property `%s' for widget `%s' of type `%s' is not supported\n",
50
propertyName.toLatin1().constData(), widgetName.toLatin1().constData(), widgetClass.toLatin1().constData());
53
QString Ui3Reader::getComment(const QDomNode& n)
55
QDomNode child = n.firstChild();
56
while (!child.isNull()) {
57
if (child.toElement().tagName() == QLatin1String("comment"))
58
return child.toElement().firstChild().toText().data();
59
child = child.nextSibling();
64
QString Ui3Reader::mkBool(bool b)
66
return b ? QLatin1String("true") : QLatin1String("false");
69
QString Ui3Reader::mkBool(const QString& s)
71
return mkBool(s == QLatin1String("true") || s == QLatin1String("1"));
74
bool Ui3Reader::toBool(const QString& s)
76
return s == QLatin1String("true") || s.toInt() != 0;
79
QString Ui3Reader::fixString(const QString &str, bool encode)
84
s.replace(QLatin1String("\\"), QLatin1String("\\\\"));
85
s.replace(QLatin1String("\""), QLatin1String("\\\""));
86
s.replace(QLatin1String("\r"), QLatin1String(""));
87
s.replace(QLatin1String("\n"), QLatin1String("\\n\"\n\""));
89
QByteArray utf8 = str.utf8();
90
const int l = utf8.length();
91
for (int i = 0; i < l; ++i)
92
s += QLatin1String("\\x") + QString::number((uchar)utf8[i], 16);
95
return QLatin1String("\"") + s + QLatin1String("\"");
98
QString Ui3Reader::trcall(const QString& sourceText, const QString& comment)
100
if (sourceText.isEmpty() && comment.isEmpty())
101
return QLatin1String("QString()");
106
t = QLatin1String("tr");
107
for (int i = 0; i < (int) sourceText.length(); i++) {
108
if (sourceText[i].unicode() >= 0x80) {
109
t = QLatin1String("trUtf8");
116
if (comment.isEmpty()) {
117
return t + QLatin1String("(") + fixString(sourceText, encode) + QLatin1String(")");
119
return t + QLatin1String("(")
120
+ fixString(sourceText, encode)
121
+ QLatin1String(", ")
122
+ fixString(comment, encode) + QLatin1String(")");
126
QString Ui3Reader::mkStdSet(const QString& prop)
128
return QLatin1String("set") + prop[0].toUpper() + prop.mid(1);
131
void Ui3Reader::init()
133
outputFileName.clear();
138
writeFunctImpl = true;
139
defMargin = BOXLAYOUT_DEFAULT_MARGIN;
140
defSpacing = BOXLAYOUT_DEFAULT_SPACING;
141
externPixmaps = false;
142
indent = QLatin1String(" "); // default indent
144
item_used = cg_used = pal_used = 0;
147
layouts << QLatin1String("hbox") << QLatin1String("vbox") << QLatin1String("grid");
149
tags << QLatin1String("widget");
153
bareNameOfClass.clear();
156
QDomElement Ui3Reader::parse(const QDomDocument &doc)
158
root = doc.firstChild().toElement();
159
widget = QDomElement();
161
pixmapLoaderFunction = getPixmapLoaderFunction(doc.firstChild().toElement());
162
nameOfClass = getFormClassName(doc.firstChild().toElement());
164
uiFileVersion = doc.firstChild().toElement().attribute(QLatin1String("version"));
165
stdsetdef = toBool(doc.firstChild().toElement().attribute(QLatin1String("stdsetdef")));
167
if (doc.firstChild().isNull() || doc.firstChild().firstChild().isNull())
170
QDomElement e = doc.firstChild().firstChild().toElement();
171
while (!e.isNull()) {
172
if (e.tagName() == QLatin1String("widget")) {
174
} else if (e.tagName() == QLatin1String("pixmapinproject")) {
175
externPixmaps = true;
176
} else if (e.tagName() == QLatin1String("layoutdefaults")) {
177
defSpacing = e.attribute(QLatin1String("spacing"), defSpacing.toString());
178
defMargin = e.attribute(QLatin1String("margin"), defMargin.toString());
179
} else if (e.tagName() == QLatin1String("layoutfunctions")) {
180
defSpacing = e.attribute(QLatin1String("spacing"), defSpacing.toString());
182
defSpacing.toInt(&ok);
184
QString buf = defSpacing.toString();
185
defSpacing = buf.append(QLatin1String("()"));
187
defMargin = e.attribute(QLatin1String("margin"), defMargin.toString());
188
defMargin.toInt(&ok);
190
QString buf = defMargin.toString();
191
defMargin = buf.append(QLatin1String("()"));
194
e = e.nextSibling().toElement();
200
Ui3Reader::Ui3Reader(QTextStream &outStream)
201
: out(outStream), trout(&languageChangeBody)
203
m_porting = new Porting();
206
Ui3Reader::~Ui3Reader()
211
void Ui3Reader::generate(const QString &fn, const QString &outputFn,
212
QDomDocument doc, bool decl, bool subcl, const QString &trm,
213
const QString& subClass, bool omitForwardDecls)
218
outputFileName = outputFn;
220
nofwd = omitForwardDecls;
222
QDomElement e = parse(doc);
224
if (nameOfClass.isEmpty())
225
nameOfClass = getObjectName(e);
226
namespaces = nameOfClass.split(QLatin1String("::"));
227
bareNameOfClass = namespaces.last();
228
namespaces.removeLast();
232
createSubDecl(e, subClass);
234
createSubImpl(e, subClass);
244
void Ui3Reader::generateUi4(const QString &fn, const QString &outputFn, QDomDocument doc)
249
outputFileName = outputFn;
251
DomUI *ui = generateUi4(parse(doc));
255
if (pixmapLoaderFunction.size())
256
ui->setElementPixmapFunction(pixmapLoaderFunction);
258
QDomDocument outputDoc;
259
outputDoc.appendChild(ui->write(outputDoc));
260
out << outputDoc.toString(2);
265
void Ui3Reader::setTrMacro(const QString &trmacro)
267
this->trmacro = trmacro;
270
void Ui3Reader::setForwardDeclarationsEnabled(bool b)
275
void Ui3Reader::setOutputFileName(const QString &fileName)
277
outputFileName = fileName;
280
/*! Extracts a pixmap loader function from \a e
282
QString Ui3Reader::getPixmapLoaderFunction(const QDomElement& e)
285
for (n = e.firstChild().toElement(); !n.isNull(); n = n.nextSibling().toElement()) {
286
if (n.tagName() == QLatin1String("pixmapfunction"))
287
return n.firstChild().toText().data();
293
/*! Extracts the forms class name from \a e
295
QString Ui3Reader::getFormClassName(const QDomElement& e)
299
for (n = e.firstChild().toElement(); !n.isNull(); n = n.nextSibling().toElement()) {
300
if (n.tagName() == QLatin1String("class")) {
301
QString s = n.firstChild().toText().data();
303
while ((i = s.indexOf(QLatin1Char(' '))) != -1)
304
s[i] = QLatin1Char('_');
311
/*! Extracts a class name from \a e.
313
QString Ui3Reader::getClassName(const QDomElement& e)
315
QString s = e.attribute(QLatin1String("class"));
316
if (s.isEmpty() && e.tagName() == QLatin1String("toolbar"))
317
s = QLatin1String(QLatin1String("QToolBar"));
318
else if (s.isEmpty() && e.tagName() == QLatin1String("menubar"))
319
s = QLatin1String("QMenuBar");
321
return fixClassName(s);
324
/*! Returns true if database framework code is generated, else false.
327
bool Ui3Reader::isFrameworkCodeGenerated(const QDomElement& e)
329
QDomElement n = getObjectProperty(e, QLatin1String("frameworkCode"));
330
if (n.attribute(QLatin1String("name")) == QLatin1String("frameworkCode") &&
331
!DomTool::elementToVariant(n.firstChild().toElement(), QVariant(true)).toBool())
336
/*! Extracts an object name from \a e. It's stored in the 'name'
339
QString Ui3Reader::getObjectName(const QDomElement& e)
341
QDomElement n = getObjectProperty(e, QLatin1String("name"));
342
if (n.firstChild().toElement().tagName() == QLatin1String("cstring"))
343
return n.firstChild().toElement().firstChild().toText().data();
347
/*! Extracts an layout name from \a e. It's stored in the 'name'
348
property of the preceeding sibling (the first child of a QLayoutWidget).
350
QString Ui3Reader::getLayoutName(const QDomElement& e)
352
QDomElement p = e.parentNode().toElement();
355
if (getClassName(p) != QLatin1String("QLayoutWidget"))
356
name = QLatin1String("Layout");
358
QDomElement n = getObjectProperty(p, QLatin1String("name"));
359
if (n.firstChild().toElement().tagName() == QLatin1String("cstring")) {
360
name.prepend(n.firstChild().toElement().firstChild().toText().data());
361
return name.split(QLatin1String("::")).last();
367
QString Ui3Reader::getDatabaseInfo(const QDomElement& e, const QString& tag)
372
// database info is a stringlist stored in this order
373
if (tag == QLatin1String("connection"))
375
else if (tag == QLatin1String("table"))
377
else if (tag == QLatin1String("field"))
381
n = getObjectProperty(e, QLatin1String("database"));
382
if (n.firstChild().toElement().tagName() == QLatin1String("stringlist")) {
383
// find correct stringlist entry
384
QDomElement n1 = n.firstChild().firstChild().toElement();
385
for (int i = 0; i < child && !n1.isNull(); ++i)
386
n1 = n1.nextSibling().toElement();
389
return n1.firstChild().toText().data();
394
static const char* const ColorRole[] = {
395
"Foreground", "Button", "Light", "Midlight", "Dark", "Mid",
396
"Text", "BrightText", "ButtonText", "Base", "Background", "Shadow",
397
"Highlight", "HighlightedText", "Link", "LinkVisited", 0
402
Creates a colorgroup with name \a name from the color group \a cg
404
void Ui3Reader::createColorGroupImpl(const QString& name, const QDomElement& e)
407
QDomElement n = e.firstChild().toElement();
411
white.init(255, 255, 255);
416
while (!n.isNull()) {
417
if (n.tagName() == QLatin1String("color")) {
419
Color col = DomTool::readColor(n);
420
color = QLatin1String("QColor(%1, %2, %3)");
421
color = color.arg(col.red).arg(col.green).arg(col.blue);
423
color = QLatin1String("white");
424
else if (col == black)
425
color = QLatin1String("black");
426
if (n.nextSibling().toElement().tagName() != QLatin1String("pixmap")) {
427
out << indent << name << ".setColor(QColorGroup::" << ColorRole[r] << ", " << color << ");" << endl;
429
} else if (n.tagName() == QLatin1String("pixmap")) {
430
QString pixmap = n.firstChild().toText().data();
431
if (!pixmapLoaderFunction.isEmpty()) {
432
pixmap.prepend(pixmapLoaderFunction
434
+ QLatin1String(externPixmaps ? "\"" : ""));
436
pixmap.append(QLatin1String(externPixmaps ? "\"" : "") + QLatin1String(")"));
438
out << indent << name << ".setBrush(QColorGroup::"
439
<< ColorRole[r] << ", QBrush(" << color << ", " << pixmap << "));" << endl;
441
n = n.nextSibling().toElement();
446
Auxiliary function to load a color group. The colorgroup must not
449
ColorGroup Ui3Reader::loadColorGroup(const QDomElement &e)
453
QDomElement n = e.firstChild().toElement();
455
while (!n.isNull()) {
456
if (n.tagName() == QLatin1String("color")) {
458
col = DomTool::readColor(n);
459
cg.append(qMakePair(r, col));
461
n = n.nextSibling().toElement();
466
/*! Returns true if the widget properties specify that it belongs to
467
the database \a connection and \a table.
470
bool Ui3Reader::isWidgetInTable(const QDomElement& e, const QString& connection, const QString& table)
472
QString conn = getDatabaseInfo(e, QLatin1String("connection"));
473
QString tab = getDatabaseInfo(e, QLatin1String("table"));
474
if (conn == connection && tab == table)
480
Registers all database connections, cursors and forms.
483
void Ui3Reader::registerDatabases(const QDomElement& e)
488
nl = e.parentNode().toElement().elementsByTagName(QLatin1String("widget"));
489
for (i = 0; i < (int) nl.length(); ++i) {
490
n = nl.item(i).toElement();
491
QString conn = getDatabaseInfo(n, QLatin1String("connection"));
492
QString tab = getDatabaseInfo(n, QLatin1String("table"));
493
QString fld = getDatabaseInfo(n, QLatin1String("field"));
494
if (!conn.isNull()) {
495
dbConnections += conn;
497
dbCursors[conn] += tab;
499
dbForms[conn] += tab;
506
Registers an object with name \a name.
508
The returned name is a valid variable identifier, as similar to \a
509
name as possible and guaranteed to be unique within the form.
511
\sa registeredName(), isObjectRegistered()
513
QString Ui3Reader::registerObject(const QString& name)
515
if (objectNames.isEmpty()) {
516
// some temporary variables we need
517
objectNames += QLatin1String("img");
518
objectNames += QLatin1String("item");
519
objectNames += QLatin1String("cg");
520
objectNames += QLatin1String("pal");
523
QString result = name;
525
while ((i = result.indexOf(QLatin1Char(' '))) != -1 ) {
526
result[i] = QLatin1Char('_');
529
if (objectNames.contains(result)) {
531
while (objectNames.contains(result + QLatin1String("_") + QString::number(i)))
533
result += QLatin1String("_");
534
result += QString::number(i);
536
objectNames += result;
537
objectMapper.insert(name, result);
542
Returns the registered name for the original name \a name
543
or \a name if \a name wasn't registered.
545
\sa registerObject(), isObjectRegistered()
547
QString Ui3Reader::registeredName(const QString& name)
549
if (!objectMapper.contains(name))
551
return objectMapper[name];
555
Returns whether the object \a name was registered yet or not.
557
bool Ui3Reader::isObjectRegistered(const QString& name)
559
return objectMapper.contains(name);
563
Unifies the entries in stringlist \a list. Should really be a QStringList feature.
565
QStringList Ui3Reader::unique(const QStringList& list)
571
for (QStringList::ConstIterator it = list.begin(); it != list.end(); ++it) {
572
if (!result.contains(*it))
578
bool Ui3Reader::isLayout(const QString& name) const
580
return layoutObjects.contains(name);