~oif-team/ubuntu/natty/qt4-x11/xi2.1

« back to all changes in this revision

Viewing changes to src/tools/uic3/ui3reader.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Adam Conrad
  • Date: 2005-08-24 04:09:09 UTC
  • Revision ID: james.westby@ubuntu.com-20050824040909-xmxe9jfr4a0w5671
Tags: upstream-4.0.0
ImportĀ upstreamĀ versionĀ 4.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
**
 
3
** Copyright (C) 1992-2005 Trolltech AS. All rights reserved.
 
4
**
 
5
** This file is part of the tools applications of the Qt Toolkit.
 
6
**
 
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.
 
10
**
 
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.
 
15
**
 
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.
 
20
**
 
21
** Contact info@trolltech.com if any conditions of this licensing are
 
22
** not clear to you.
 
23
**
 
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.
 
26
**
 
27
****************************************************************************/
 
28
 
 
29
#include "ui3reader.h"
 
30
#include "parser.h"
 
31
#include "domtool.h"
 
32
#include "ui4.h"
 
33
#include "widgetinfo.h"
 
34
#include "globaldefs.h"
 
35
#include "qt3to4.h"
 
36
 
 
37
#include <qfile.h>
 
38
#include <qdatetime.h>
 
39
#include <qregexp.h>
 
40
#include <stdio.h>
 
41
#include <stdlib.h>
 
42
#include <qdebug.h>
 
43
 
 
44
 
 
45
bool Ui3Reader::isMainWindow = false;
 
46
 
 
47
void Ui3Reader::errorInvalidProperty(const QString &propertyName, const QString &widgetName, const QString &widgetClass)
 
48
{
 
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());
 
51
}
 
52
 
 
53
QString Ui3Reader::getComment(const QDomNode& n)
 
54
{
 
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();
 
60
    }
 
61
    return QString();
 
62
}
 
63
 
 
64
QString Ui3Reader::mkBool(bool b)
 
65
{
 
66
    return b ? QLatin1String("true") : QLatin1String("false");
 
67
}
 
68
 
 
69
QString Ui3Reader::mkBool(const QString& s)
 
70
{
 
71
    return mkBool(s == QLatin1String("true") || s == QLatin1String("1"));
 
72
}
 
73
 
 
74
bool Ui3Reader::toBool(const QString& s)
 
75
{
 
76
    return s == QLatin1String("true") || s.toInt() != 0;
 
77
}
 
78
 
 
79
QString Ui3Reader::fixString(const QString &str, bool encode)
 
80
{
 
81
    QString s;
 
82
    if (!encode) {
 
83
        s = str;
 
84
        s.replace(QLatin1String("\\"), QLatin1String("\\\\"));
 
85
        s.replace(QLatin1String("\""), QLatin1String("\\\""));
 
86
        s.replace(QLatin1String("\r"), QLatin1String(""));
 
87
        s.replace(QLatin1String("\n"), QLatin1String("\\n\"\n\""));
 
88
    } else {
 
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);
 
93
    }
 
94
 
 
95
    return QLatin1String("\"") + s + QLatin1String("\"");
 
96
}
 
97
 
 
98
QString Ui3Reader::trcall(const QString& sourceText, const QString& comment)
 
99
{
 
100
    if (sourceText.isEmpty() && comment.isEmpty())
 
101
        return QLatin1String("QString()");
 
102
 
 
103
    QString t = trmacro;
 
104
    bool encode = false;
 
105
    if (t.isNull()) {
 
106
        t = QLatin1String("tr");
 
107
        for (int i = 0; i < (int) sourceText.length(); i++) {
 
108
            if (sourceText[i].unicode() >= 0x80) {
 
109
                t = QLatin1String("trUtf8");
 
110
                encode = true;
 
111
                break;
 
112
            }
 
113
        }
 
114
    }
 
115
 
 
116
    if (comment.isEmpty()) {
 
117
        return t + QLatin1String("(") + fixString(sourceText, encode) + QLatin1String(")");
 
118
    } else {
 
119
        return t + QLatin1String("(")
 
120
            + fixString(sourceText, encode)
 
121
            + QLatin1String(", ")
 
122
            + fixString(comment, encode) + QLatin1String(")");
 
123
    }
 
124
}
 
125
 
 
126
QString Ui3Reader::mkStdSet(const QString& prop)
 
127
{
 
128
    return QLatin1String("set") + prop[0].toUpper() + prop.mid(1);
 
129
}
 
130
 
 
131
void Ui3Reader::init()
 
132
{
 
133
    outputFileName.clear();
 
134
    trmacro.clear();
 
135
    nofwd = false;
 
136
 
 
137
    fileName.clear();
 
138
    writeFunctImpl = true;
 
139
    defMargin = BOXLAYOUT_DEFAULT_MARGIN;
 
140
    defSpacing = BOXLAYOUT_DEFAULT_SPACING;
 
141
    externPixmaps = false;
 
142
    indent = QLatin1String("    "); // default indent
 
143
 
 
144
    item_used = cg_used = pal_used = 0;
 
145
 
 
146
    layouts.clear();
 
147
    layouts << QLatin1String("hbox") << QLatin1String("vbox") << QLatin1String("grid");
 
148
    tags = layouts;
 
149
    tags << QLatin1String("widget");
 
150
 
 
151
    nameOfClass.clear();
 
152
    namespaces.clear();
 
153
    bareNameOfClass.clear();
 
154
}
 
155
 
 
156
QDomElement Ui3Reader::parse(const QDomDocument &doc)
 
157
{
 
158
    root = doc.firstChild().toElement();
 
159
    widget = QDomElement();
 
160
 
 
161
    pixmapLoaderFunction = getPixmapLoaderFunction(doc.firstChild().toElement());
 
162
    nameOfClass = getFormClassName(doc.firstChild().toElement());
 
163
 
 
164
    uiFileVersion = doc.firstChild().toElement().attribute(QLatin1String("version"));
 
165
    stdsetdef = toBool(doc.firstChild().toElement().attribute(QLatin1String("stdsetdef")));
 
166
 
 
167
    if (doc.firstChild().isNull() || doc.firstChild().firstChild().isNull())
 
168
        return widget;
 
169
 
 
170
    QDomElement e = doc.firstChild().firstChild().toElement();
 
171
    while (!e.isNull()) {
 
172
        if (e.tagName() == QLatin1String("widget")) {
 
173
            widget = e;
 
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());
 
181
            bool ok;
 
182
            defSpacing.toInt(&ok);
 
183
            if (!ok) {
 
184
                QString buf = defSpacing.toString();
 
185
                defSpacing = buf.append(QLatin1String("()"));
 
186
            }
 
187
            defMargin = e.attribute(QLatin1String("margin"), defMargin.toString());
 
188
            defMargin.toInt(&ok);
 
189
            if (!ok) {
 
190
                QString buf = defMargin.toString();
 
191
                defMargin = buf.append(QLatin1String("()"));
 
192
            }
 
193
        }
 
194
        e = e.nextSibling().toElement();
 
195
    }
 
196
 
 
197
    return widget;
 
198
}
 
199
 
 
200
Ui3Reader::Ui3Reader(QTextStream &outStream)
 
201
   : out(outStream), trout(&languageChangeBody)
 
202
{
 
203
    m_porting = new Porting();
 
204
}
 
205
 
 
206
Ui3Reader::~Ui3Reader()
 
207
{
 
208
    delete m_porting;
 
209
}
 
210
 
 
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)
 
214
{
 
215
    init();
 
216
 
 
217
    fileName = fn;
 
218
    outputFileName = outputFn;
 
219
    trmacro = trm;
 
220
    nofwd = omitForwardDecls;
 
221
 
 
222
    QDomElement e = parse(doc);
 
223
 
 
224
    if (nameOfClass.isEmpty())
 
225
        nameOfClass = getObjectName(e);
 
226
    namespaces = nameOfClass.split(QLatin1String("::"));
 
227
    bareNameOfClass = namespaces.last();
 
228
    namespaces.removeLast();
 
229
 
 
230
    if (subcl) {
 
231
        if (decl)
 
232
            createSubDecl(e, subClass);
 
233
        else
 
234
            createSubImpl(e, subClass);
 
235
    } else {
 
236
        if (decl)
 
237
            createFormDecl(e);
 
238
        else
 
239
            createFormImpl(e);
 
240
    }
 
241
 
 
242
}
 
243
 
 
244
void Ui3Reader::generateUi4(const QString &fn, const QString &outputFn, QDomDocument doc)
 
245
{
 
246
    init();
 
247
 
 
248
    fileName = fn;
 
249
    outputFileName = outputFn;
 
250
 
 
251
    DomUI *ui = generateUi4(parse(doc));
 
252
    if (!ui)
 
253
        return;
 
254
 
 
255
    if (pixmapLoaderFunction.size())
 
256
        ui->setElementPixmapFunction(pixmapLoaderFunction);
 
257
 
 
258
    QDomDocument outputDoc;
 
259
    outputDoc.appendChild(ui->write(outputDoc));
 
260
    out << outputDoc.toString(2);
 
261
 
 
262
    delete ui;
 
263
}
 
264
 
 
265
void Ui3Reader::setTrMacro(const QString &trmacro)
 
266
{
 
267
    this->trmacro = trmacro;
 
268
}
 
269
 
 
270
void Ui3Reader::setForwardDeclarationsEnabled(bool b)
 
271
{
 
272
    nofwd = !b;
 
273
}
 
274
 
 
275
void Ui3Reader::setOutputFileName(const QString &fileName)
 
276
{
 
277
    outputFileName = fileName;
 
278
}
 
279
 
 
280
/*! Extracts a pixmap loader function from \a e
 
281
 */
 
282
QString Ui3Reader::getPixmapLoaderFunction(const QDomElement& e)
 
283
{
 
284
    QDomElement n;
 
285
    for (n = e.firstChild().toElement(); !n.isNull(); n = n.nextSibling().toElement()) {
 
286
        if (n.tagName() == QLatin1String("pixmapfunction"))
 
287
            return n.firstChild().toText().data();
 
288
    }
 
289
    return QString();
 
290
}
 
291
 
 
292
 
 
293
/*! Extracts the forms class name from \a e
 
294
 */
 
295
QString Ui3Reader::getFormClassName(const QDomElement& e)
 
296
{
 
297
    QDomElement n;
 
298
    QString cn;
 
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();
 
302
            int i;
 
303
            while ((i = s.indexOf(QLatin1Char(' '))) != -1)
 
304
                s[i] = QLatin1Char('_');
 
305
            cn = s;
 
306
        }
 
307
    }
 
308
    return cn;
 
309
}
 
310
 
 
311
/*! Extracts a class name from \a e.
 
312
 */
 
313
QString Ui3Reader::getClassName(const QDomElement& e)
 
314
{
 
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");
 
320
 
 
321
    return fixClassName(s);
 
322
}
 
323
 
 
324
/*! Returns true if database framework code is generated, else false.
 
325
*/
 
326
 
 
327
bool Ui3Reader::isFrameworkCodeGenerated(const QDomElement& e)
 
328
{
 
329
    QDomElement n = getObjectProperty(e, QLatin1String("frameworkCode"));
 
330
    if (n.attribute(QLatin1String("name")) == QLatin1String("frameworkCode") &&
 
331
         !DomTool::elementToVariant(n.firstChild().toElement(), QVariant(true)).toBool())
 
332
        return false;
 
333
    return true;
 
334
}
 
335
 
 
336
/*! Extracts an object name from \a e. It's stored in the 'name'
 
337
 property.
 
338
 */
 
339
QString Ui3Reader::getObjectName(const QDomElement& e)
 
340
{
 
341
    QDomElement n = getObjectProperty(e, QLatin1String("name"));
 
342
    if (n.firstChild().toElement().tagName() == QLatin1String("cstring"))
 
343
        return n.firstChild().toElement().firstChild().toText().data();
 
344
    return QString();
 
345
}
 
346
 
 
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).
 
349
 */
 
350
QString Ui3Reader::getLayoutName(const QDomElement& e)
 
351
{
 
352
    QDomElement p = e.parentNode().toElement();
 
353
    QString name;
 
354
 
 
355
    if (getClassName(p) != QLatin1String("QLayoutWidget"))
 
356
        name = QLatin1String("Layout");
 
357
 
 
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();
 
362
    }
 
363
    return e.tagName();
 
364
}
 
365
 
 
366
 
 
367
QString Ui3Reader::getDatabaseInfo(const QDomElement& e, const QString& tag)
 
368
{
 
369
    QDomElement n;
 
370
    QDomElement n1;
 
371
    int child = 0;
 
372
    // database info is a stringlist stored in this order
 
373
    if (tag == QLatin1String("connection"))
 
374
        child = 0;
 
375
    else if (tag == QLatin1String("table"))
 
376
        child = 1;
 
377
    else if (tag == QLatin1String("field"))
 
378
        child = 2;
 
379
    else
 
380
        return QString();
 
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();
 
387
            if (n1.isNull())
 
388
                return QString();
 
389
            return n1.firstChild().toText().data();
 
390
    }
 
391
    return QString();
 
392
}
 
393
 
 
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
 
398
};
 
399
 
 
400
 
 
401
/*!
 
402
  Creates a colorgroup with name \a name from the color group \a cg
 
403
 */
 
404
void Ui3Reader::createColorGroupImpl(const QString& name, const QDomElement& e)
 
405
{
 
406
    int r = -1;
 
407
    QDomElement n = e.firstChild().toElement();
 
408
    QString color;
 
409
 
 
410
    Color white;
 
411
    white.init(255, 255, 255);
 
412
 
 
413
    Color black;
 
414
    black.init(0, 0, 0);
 
415
 
 
416
    while (!n.isNull()) {
 
417
        if (n.tagName() == QLatin1String("color")) {
 
418
            r++;
 
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);
 
422
            if (col == white)
 
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;
 
428
            }
 
429
        } else if (n.tagName() == QLatin1String("pixmap")) {
 
430
            QString pixmap = n.firstChild().toText().data();
 
431
            if (!pixmapLoaderFunction.isEmpty()) {
 
432
                pixmap.prepend(pixmapLoaderFunction
 
433
                    + QLatin1String("(")
 
434
                    + QLatin1String(externPixmaps ? "\"" : ""));
 
435
 
 
436
                pixmap.append(QLatin1String(externPixmaps ? "\"" : "") + QLatin1String(")"));
 
437
            }
 
438
            out << indent << name << ".setBrush(QColorGroup::"
 
439
                << ColorRole[r] << ", QBrush(" << color << ", " << pixmap << "));" << endl;
 
440
        }
 
441
        n = n.nextSibling().toElement();
 
442
    }
 
443
}
 
444
 
 
445
/*!
 
446
  Auxiliary function to load a color group. The colorgroup must not
 
447
  contain pixmaps.
 
448
 */
 
449
ColorGroup Ui3Reader::loadColorGroup(const QDomElement &e)
 
450
{
 
451
    ColorGroup cg;
 
452
    int r = -1;
 
453
    QDomElement n = e.firstChild().toElement();
 
454
    Color col;
 
455
    while (!n.isNull()) {
 
456
        if (n.tagName() == QLatin1String("color")) {
 
457
            r++;
 
458
            col = DomTool::readColor(n);
 
459
            cg.append(qMakePair(r, col));
 
460
        }
 
461
        n = n.nextSibling().toElement();
 
462
    }
 
463
    return cg;
 
464
}
 
465
 
 
466
/*!  Returns true if the widget properties specify that it belongs to
 
467
  the database \a connection and \a table.
 
468
*/
 
469
 
 
470
bool Ui3Reader::isWidgetInTable(const QDomElement& e, const QString& connection, const QString& table)
 
471
{
 
472
    QString conn = getDatabaseInfo(e, QLatin1String("connection"));
 
473
    QString tab = getDatabaseInfo(e, QLatin1String("table"));
 
474
    if (conn == connection && tab == table)
 
475
        return true;
 
476
    return false;
 
477
}
 
478
 
 
479
/*!
 
480
  Registers all database connections, cursors and forms.
 
481
*/
 
482
 
 
483
void Ui3Reader::registerDatabases(const QDomElement& e)
 
484
{
 
485
    QDomElement n;
 
486
    QDomNodeList nl;
 
487
    int i;
 
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;
 
496
            if (!tab.isNull()) {
 
497
                dbCursors[conn] += tab;
 
498
                if (!fld.isNull())
 
499
                    dbForms[conn] += tab;
 
500
            }
 
501
        }
 
502
    }
 
503
}
 
504
 
 
505
/*!
 
506
  Registers an object with name \a name.
 
507
 
 
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.
 
510
 
 
511
  \sa registeredName(), isObjectRegistered()
 
512
 */
 
513
QString Ui3Reader::registerObject(const QString& name)
 
514
{
 
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");
 
521
    }
 
522
 
 
523
    QString result = name;
 
524
    int i;
 
525
    while ((i = result.indexOf(QLatin1Char(' '))) != -1 ) {
 
526
        result[i] = QLatin1Char('_');
 
527
    }
 
528
 
 
529
    if (objectNames.contains(result)) {
 
530
        int i = 2;
 
531
        while (objectNames.contains(result + QLatin1String("_") + QString::number(i)))
 
532
            i++;
 
533
        result += QLatin1String("_");
 
534
        result += QString::number(i);
 
535
    }
 
536
    objectNames += result;
 
537
    objectMapper.insert(name, result);
 
538
    return result;
 
539
}
 
540
 
 
541
/*!
 
542
  Returns the registered name for the original name \a name
 
543
  or \a name if \a name  wasn't registered.
 
544
 
 
545
  \sa registerObject(), isObjectRegistered()
 
546
 */
 
547
QString Ui3Reader::registeredName(const QString& name)
 
548
{
 
549
    if (!objectMapper.contains(name))
 
550
        return name;
 
551
    return objectMapper[name];
 
552
}
 
553
 
 
554
/*!
 
555
  Returns whether the object \a name was registered yet or not.
 
556
 */
 
557
bool Ui3Reader::isObjectRegistered(const QString& name)
 
558
{
 
559
    return objectMapper.contains(name);
 
560
}
 
561
 
 
562
/*!
 
563
  Unifies the entries in stringlist \a list. Should really be a QStringList feature.
 
564
 */
 
565
QStringList Ui3Reader::unique(const QStringList& list)
 
566
{
 
567
    if (list.isEmpty())
 
568
        return list;
 
569
 
 
570
    QStringList result;
 
571
    for (QStringList::ConstIterator it = list.begin(); it != list.end(); ++it) {
 
572
        if (!result.contains(*it))
 
573
            result += *it;
 
574
    }
 
575
    return result;
 
576
}
 
577
 
 
578
bool Ui3Reader::isLayout(const QString& name) const
 
579
{
 
580
    return layoutObjects.contains(name);
 
581
}