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

« back to all changes in this revision

Viewing changes to src/tools/uic/writeinitialization.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 "writeinitialization.h"
 
30
#include "driver.h"
 
31
#include "ui4.h"
 
32
#include "utils.h"
 
33
#include "uic.h"
 
34
#include "databaseinfo.h"
 
35
#include "globaldefs.h"
 
36
 
 
37
#include <qtextstream.h>
 
38
#include <qdebug.h>
 
39
 
 
40
#include <limits.h>
 
41
 
 
42
WriteInitialization::WriteInitialization(Uic *uic)
 
43
    : driver(uic->driver()), output(uic->output()), option(uic->option()),
 
44
      m_defaultMargin(INT_MIN), m_defaultSpacing(INT_MIN),
 
45
      refreshOut(&m_delayedInitialization, QIODevice::WriteOnly),
 
46
      actionOut(&m_delayedActionInitialization, QIODevice::WriteOnly)
 
47
{
 
48
    this->uic = uic;
 
49
}
 
50
 
 
51
void WriteInitialization::acceptUI(DomUI *node)
 
52
{
 
53
    m_registeredImages.clear();
 
54
    m_actionGroupChain.push(0);
 
55
    m_widgetChain.push(0);
 
56
    m_layoutChain.push(0);
 
57
 
 
58
    acceptLayoutDefault(node->elementLayoutDefault());
 
59
    acceptLayoutFunction(node->elementLayoutFunction());
 
60
 
 
61
    if (node->elementCustomWidgets())
 
62
        TreeWalker::acceptCustomWidgets(node->elementCustomWidgets());
 
63
 
 
64
    if (node->elementImages())
 
65
        TreeWalker::acceptImages(node->elementImages());
 
66
 
 
67
    if (option.generateImplemetation)
 
68
        output << "#include <" << driver->headerFileName() << ">\n\n";
 
69
 
 
70
    m_stdsetdef = true;
 
71
    if (node->hasAttributeStdSetDef())
 
72
        m_stdsetdef = node->attributeStdSetDef();
 
73
 
 
74
    QString className = node->elementClass() + option.postfix;
 
75
    m_generatedClass = className;
 
76
 
 
77
    QString varName = driver->findOrInsertWidget(node->elementWidget());
 
78
    m_registeredWidgets.insert(varName, node->elementWidget()); // register the main widget
 
79
 
 
80
    QString widgetClassName = node->elementWidget()->attributeClass();
 
81
 
 
82
    output << option.indent << "void " << "setupUi(" << widgetClassName << " *" << varName << ")\n"
 
83
           << option.indent << "{\n";
 
84
 
 
85
    QStringList connections = uic->databaseInfo()->connections();
 
86
    for (int i=0; i<connections.size(); ++i) {
 
87
        QString connection = connections.at(i);
 
88
 
 
89
        if (connection == QLatin1String("(default)"))
 
90
            continue;
 
91
 
 
92
        QString varConn = connection + QLatin1String("Connection");
 
93
        output << option.indent << varConn << " = QSqlDatabase::database(" << fixString(connection) << ");\n";
 
94
    }
 
95
 
 
96
    acceptWidget(node->elementWidget());
 
97
 
 
98
    for (int i=0; i<m_buddies.size(); ++i) {
 
99
        const Buddy &b = m_buddies.at(i);
 
100
 
 
101
        if (!m_registeredWidgets.contains(b.objName)) {
 
102
            fprintf(stderr, "'%s' isn't a valid widget\n", b.objName.toLatin1().data());
 
103
            continue;
 
104
        } else if (!m_registeredWidgets.contains(b.buddy)) {
 
105
            fprintf(stderr, "'%s' isn't a valid widget\n", b.buddy.toLatin1().data());
 
106
            continue;
 
107
        }
 
108
 
 
109
 
 
110
        output << option.indent << b.objName << "->setBuddy(" << b.buddy << ");\n";
 
111
    }
 
112
 
 
113
    if (node->elementTabStops())
 
114
        acceptTabStops(node->elementTabStops());
 
115
 
 
116
    if (m_delayedActionInitialization.size())
 
117
        output << "\n" << m_delayedActionInitialization;
 
118
 
 
119
    output << option.indent << "retranslateUi(" << varName << ");\n";
 
120
 
 
121
    if (node->elementConnections())
 
122
        acceptConnections(node->elementConnections());
 
123
 
 
124
    if (option.autoConnection)
 
125
        output << "\n" << option.indent << "QMetaObject::connectSlotsByName(" << varName << ");\n";
 
126
 
 
127
    output << option.indent << "} // setupUi\n\n";
 
128
 
 
129
    if (m_delayedActionInitialization.isEmpty()) {
 
130
        m_delayedInitialization += option.indent + QLatin1String("Q_UNUSED(") + varName + QLatin1String(");\n");
 
131
    }
 
132
 
 
133
    output << option.indent << "void " << "retranslateUi(" << widgetClassName << " *" << varName << ")\n"
 
134
           << option.indent << "{\n"
 
135
           << m_delayedInitialization
 
136
           << option.indent << "} // retranslateUi\n\n";
 
137
 
 
138
    m_layoutChain.pop();
 
139
    m_widgetChain.pop();
 
140
    m_actionGroupChain.pop();
 
141
}
 
142
 
 
143
void WriteInitialization::acceptWidget(DomWidget *node)
 
144
{
 
145
    QString className = node->attributeClass();
 
146
    QString varName = driver->findOrInsertWidget(node);
 
147
    m_registeredWidgets.insert(varName, node); // register the current widget
 
148
 
 
149
    QString parentWidget, parentClass;
 
150
    if (m_widgetChain.top()) {
 
151
        parentWidget = driver->findOrInsertWidget(m_widgetChain.top());
 
152
        parentClass = m_widgetChain.top()->attributeClass();
 
153
    }
 
154
 
 
155
    QString savedParentWidget = parentWidget;
 
156
 
 
157
    if (uic->isContainer(parentClass))
 
158
        parentWidget.clear();
 
159
 
 
160
    if (m_widgetChain.size() != 1)
 
161
        output << option.indent << varName << " = new " << uic->customWidgetsInfo()->realClassName(className) << "(" << parentWidget << ");\n";
 
162
 
 
163
    parentWidget = savedParentWidget;
 
164
 
 
165
    if (uic->customWidgetsInfo()->extends(className, QLatin1String("QComboBox"))) {
 
166
        initializeComboBox(node);
 
167
    } else if (uic->customWidgetsInfo()->extends(className, QLatin1String("QListWidget"))) {
 
168
        initializeListWidget(node);
 
169
    } else if (uic->customWidgetsInfo()->extends(className, QLatin1String("QTreeWidget"))) {
 
170
        initializeTreeWidget(node);
 
171
    } else if (uic->customWidgetsInfo()->extends(className, QLatin1String("QTableWidget"))) {
 
172
        initializeTableWidget(node);
 
173
    } else if (uic->customWidgetsInfo()->extends(className, QLatin1String("Q3ListBox"))) {
 
174
        initializeQ3ListBox(node);
 
175
    } else if (uic->customWidgetsInfo()->extends(className, QLatin1String("Q3ListView"))) {
 
176
        initializeQ3ListView(node);
 
177
    } else if (uic->customWidgetsInfo()->extends(className, QLatin1String("Q3IconView"))) {
 
178
        initializeQ3IconView(node);
 
179
    } else if (uic->customWidgetsInfo()->extends(className, QLatin1String("Q3Table"))) {
 
180
        initializeQ3Table(node);
 
181
    } else if (uic->customWidgetsInfo()->extends(className, QLatin1String("Q3DataTable"))) {
 
182
        initializeSqlDataTable(node);
 
183
    } else if (uic->customWidgetsInfo()->extends(className, QLatin1String("Q3DataBrowser"))) {
 
184
        initializeSqlDataBrowser(node);
 
185
    }
 
186
 
 
187
    if (uic->isButton(className)) {
 
188
        QHash<QString, DomProperty*> attributes = propertyMap(node->elementAttribute());
 
189
        if (attributes.contains(QLatin1String("buttonGroup"))) {
 
190
            DomProperty *prop = attributes.value(QLatin1String("buttonGroup"));
 
191
            QString groupName = toString(prop->elementString());
 
192
            if (!m_buttonGroups.contains(groupName)) {
 
193
                m_buttonGroups.insert(groupName, driver->findOrInsertName(groupName));
 
194
                QString g = m_buttonGroups.value(groupName);
 
195
                output << option.indent << "QButtonGroup *" << g << " = new QButtonGroup(" << m_generatedClass << ");\n";
 
196
            }
 
197
 
 
198
            QString g = m_buttonGroups.value(groupName);
 
199
            output << option.indent << g << "->addButton(" << varName << ");\n";
 
200
        }
 
201
    }
 
202
 
 
203
    writeProperties(varName, className, node->elementProperty());
 
204
 
 
205
    if (uic->customWidgetsInfo()->extends(className, QLatin1String("QMenu")) && parentWidget.size()) {
 
206
        initializeMenu(node, parentWidget);
 
207
    }
 
208
 
 
209
    if (node->elementLayout().isEmpty())
 
210
        m_layoutChain.push(0);
 
211
 
 
212
    m_widgetChain.push(node);
 
213
    m_layoutChain.push(0);
 
214
    TreeWalker::acceptWidget(node);
 
215
    m_layoutChain.pop();
 
216
    m_widgetChain.pop();
 
217
 
 
218
    QHash<QString, DomProperty*> attributes = propertyMap(node->elementAttribute());
 
219
 
 
220
    QString title = QLatin1String("Page");
 
221
    if (attributes.contains(QLatin1String("title")))
 
222
        title = toString(attributes.value(QLatin1String("title"))->elementString());
 
223
 
 
224
    QString label = QLatin1String("Page");
 
225
    if (attributes.contains(QLatin1String("label")))
 
226
        label = toString(attributes.value(QLatin1String("label"))->elementString());
 
227
 
 
228
    int id = -1;
 
229
    if (attributes.contains(QLatin1String("id")))
 
230
        id = attributes.value(QLatin1String("id"))->elementNumber();
 
231
 
 
232
    if (uic->customWidgetsInfo()->extends(parentClass, QLatin1String("QMainWindow"))
 
233
            || uic->customWidgetsInfo()->extends(parentClass, QLatin1String("Q3MainWindow"))) {
 
234
 
 
235
        if (uic->customWidgetsInfo()->extends(className, QLatin1String("QMenuBar"))) {
 
236
            if (!uic->customWidgetsInfo()->extends(parentClass, QLatin1String("Q3MainWindow")))
 
237
                output << option.indent << parentWidget << "->setMenuBar(" << varName <<");\n";
 
238
        } else if (uic->customWidgetsInfo()->extends(className, QLatin1String("QToolBar"))) {
 
239
            output << option.indent << parentWidget << "->addToolBar(" << varName << ");\n";
 
240
        } else if (uic->customWidgetsInfo()->extends(className, QLatin1String("QStatusBar"))) {
 
241
            output << option.indent << parentWidget << "->setStatusBar(" << varName << ");\n";
 
242
        } else if (className == QLatin1String("QWidget")) {
 
243
            output << option.indent << parentWidget << "->setCentralWidget(" << varName << ");\n";
 
244
        }
 
245
    }
 
246
 
 
247
    if (uic->customWidgetsInfo()->extends(parentClass, QLatin1String("QStackedWidget"))) {
 
248
        output << option.indent << parentWidget << "->addWidget(" << varName << ");\n";
 
249
    } else if (uic->customWidgetsInfo()->extends(parentClass, QLatin1String("QToolBar"))) {
 
250
        output << option.indent << parentWidget << "->addWidget(" << varName << ");\n";
 
251
    } else if (uic->customWidgetsInfo()->extends(parentClass, QLatin1String("Q3WidgetStack"))) {
 
252
        output << option.indent << parentWidget << "->addWidget(" << varName << ", " << id << ");\n";
 
253
    } else if (uic->customWidgetsInfo()->extends(parentClass, QLatin1String("QDockWidget"))) {
 
254
        output << option.indent << parentWidget << "->setWidget(" << varName << ");\n";
 
255
    } else if (uic->customWidgetsInfo()->extends(parentClass, QLatin1String("QToolBox"))) {
 
256
        output << option.indent << parentWidget << "->addItem(" << varName << ", " << trCall(label, className) << ");\n";
 
257
 
 
258
        refreshOut << option.indent << parentWidget << "->setItemText("
 
259
                   << parentWidget << "->indexOf(" << varName << "), " << trCall(label, className) << ");\n";
 
260
 
 
261
    } else if (uic->customWidgetsInfo()->extends(parentClass, QLatin1String("QTabWidget"))) {
 
262
        output << option.indent << parentWidget << "->addTab(" << varName << ", " << trCall(title, className) << ");\n";
 
263
 
 
264
        refreshOut << option.indent << parentWidget << "->setTabText("
 
265
                   << parentWidget << "->indexOf(" << varName << "), " << trCall(title, className) << ");\n";
 
266
 
 
267
    } else if (uic->customWidgetsInfo()->extends(parentClass, QLatin1String("Q3Wizard"))) {
 
268
        output << option.indent << parentWidget << "->addPage(" << varName << ", " << trCall(title, className) << ");\n";
 
269
 
 
270
        refreshOut << option.indent << parentWidget << "->setTitle("
 
271
                   << varName << ", " << trCall(title, className) << ");\n";
 
272
 
 
273
    }
 
274
 
 
275
    if (node->elementLayout().isEmpty())
 
276
        m_layoutChain.pop();
 
277
}
 
278
 
 
279
void WriteInitialization::acceptLayout(DomLayout *node)
 
280
{
 
281
    QString className = node->attributeClass();
 
282
    QString varName = driver->findOrInsertLayout(node);
 
283
 
 
284
    QHash<QString, DomProperty*> properties = propertyMap(node->elementProperty());
 
285
 
 
286
    bool isGroupBox = false;
 
287
    bool isMainWindow = false;
 
288
 
 
289
    if (m_widgetChain.top()) {
 
290
        QString parentWidget = m_widgetChain.top()->attributeClass();
 
291
 
 
292
        if (!m_layoutChain.top() && (uic->customWidgetsInfo()->extends(parentWidget, QLatin1String("Q3GroupBox"))
 
293
                        || uic->customWidgetsInfo()->extends(parentWidget, QLatin1String("Q3ButtonGroup")))) {
 
294
            QString parent = driver->findOrInsertWidget(m_widgetChain.top());
 
295
 
 
296
            isGroupBox = true;
 
297
 
 
298
            // special case for group box
 
299
 
 
300
            int margin = m_defaultMargin;
 
301
            int spacing = m_defaultSpacing;
 
302
 
 
303
            if (properties.contains(QLatin1String("margin")))
 
304
                margin = properties.value(QLatin1String("margin"))->elementNumber();
 
305
 
 
306
            if (properties.contains(QLatin1String("spacing")))
 
307
                spacing = properties.value(QLatin1String("spacing"))->elementNumber();
 
308
 
 
309
            output << option.indent << parent << "->setColumnLayout(0, Qt::Vertical);\n";
 
310
 
 
311
            if (spacing != INT_MIN) {
 
312
                QString value = QString::number(spacing);
 
313
                if (!m_spacingFunction.isEmpty() && spacing == m_defaultSpacing)
 
314
                    value = m_spacingFunction + QLatin1String("()");
 
315
 
 
316
                output << option.indent << parent << "->layout()->setSpacing(" << value << ");\n";
 
317
            }
 
318
 
 
319
            if (margin != INT_MIN) {
 
320
                QString value = QString::number(margin);
 
321
                if (!m_marginFunction.isEmpty() && spacing == m_defaultMargin)
 
322
                    value = m_marginFunction + QLatin1String("()");
 
323
 
 
324
                output << option.indent << parent << "->layout()->setMargin(" << margin << ");\n";
 
325
            }
 
326
 
 
327
        } else if (uic->isMainWindow(parentWidget)) {
 
328
            isMainWindow = true;
 
329
        }
 
330
    }
 
331
 
 
332
    output << option.indent << varName << " = new " << className << "(";
 
333
 
 
334
    if (isGroupBox) {
 
335
        output << driver->findOrInsertWidget(m_widgetChain.top()) << "->layout()";
 
336
    } else if (!m_layoutChain.top()) {
 
337
        output << driver->findOrInsertWidget(m_widgetChain.top());
 
338
    }
 
339
 
 
340
    output << ");\n";
 
341
 
 
342
    QList<DomProperty*> layoutProperties = node->elementProperty();
 
343
 
 
344
    if (isGroupBox) {
 
345
        output << option.indent << varName << "->setAlignment(Qt::AlignTop);\n";
 
346
    } else {
 
347
        int margin = m_defaultMargin;
 
348
        int spacing = m_defaultSpacing;
 
349
 
 
350
        if (properties.contains(QLatin1String("margin"))) {
 
351
            DomProperty *p = properties.value(QLatin1String("margin"));
 
352
            Q_ASSERT(p != 0);
 
353
 
 
354
            margin = properties.value(QLatin1String("margin"))->elementNumber();
 
355
            layoutProperties.removeAt(layoutProperties.indexOf(p));
 
356
        }
 
357
 
 
358
        if (properties.contains(QLatin1String("spacing"))) {
 
359
            DomProperty *p = properties.value(QLatin1String("spacing"));
 
360
            Q_ASSERT(p != 0);
 
361
 
 
362
            spacing = properties.value(QLatin1String("spacing"))->elementNumber();
 
363
            layoutProperties.removeAt(layoutProperties.indexOf(p));
 
364
        }
 
365
 
 
366
        if (spacing != INT_MIN) {
 
367
            QString value = QString::number(spacing);
 
368
            if (!m_spacingFunction.isEmpty() && spacing == m_defaultSpacing)
 
369
                value = m_spacingFunction + QLatin1String("()");
 
370
 
 
371
            output << option.indent << varName << "->setSpacing(" << value << ");\n";
 
372
        }
 
373
 
 
374
        if (margin != INT_MIN) {
 
375
            QString value = QString::number(margin);
 
376
            if (!m_marginFunction.isEmpty() && spacing == m_defaultMargin)
 
377
                value = m_marginFunction + QLatin1String("()");
 
378
 
 
379
            output << option.indent << varName << "->setMargin(" << margin << ");\n";
 
380
        }
 
381
    }
 
382
 
 
383
    writeProperties(varName, className, layoutProperties);
 
384
 
 
385
    m_layoutChain.push(node);
 
386
    TreeWalker::acceptLayout(node);
 
387
    m_layoutChain.pop();
 
388
}
 
389
 
 
390
void WriteInitialization::acceptSpacer(DomSpacer *node)
 
391
{
 
392
    QHash<QString, DomProperty *> properties = propertyMap(node->elementProperty());
 
393
    QString varName = driver->findOrInsertSpacer(node);
 
394
 
 
395
    DomSize *sizeHint = properties.contains(QLatin1String("sizeHint"))
 
396
        ? properties.value(QLatin1String("sizeHint"))->elementSize() : 0;
 
397
 
 
398
    QString sizeType = properties.contains(QLatin1String("sizeType"))
 
399
        ? properties.value(QLatin1String("sizeType"))->elementEnum()
 
400
        : QString::fromLatin1("Expanding");
 
401
 
 
402
    QString orientation = properties.contains(QLatin1String("orientation"))
 
403
        ? properties.value(QLatin1String("orientation"))->elementEnum() : QString();
 
404
 
 
405
    bool isVspacer = orientation == QLatin1String("Qt::Vertical")
 
406
        || orientation == QLatin1String("Vertical");
 
407
 
 
408
    output << option.indent << varName << " = new QSpacerItem(";
 
409
 
 
410
    if (sizeHint)
 
411
        output << sizeHint->elementWidth() << ", " << sizeHint->elementHeight() << ", ";
 
412
 
 
413
    if (sizeType.startsWith(QLatin1String("QSizePolicy::")) == false)
 
414
        sizeType.prepend(QLatin1String("QSizePolicy::"));
 
415
 
 
416
    if (isVspacer)
 
417
        output << "QSizePolicy::Minimum, " << sizeType << ");\n";
 
418
    else
 
419
        output << sizeType << ", QSizePolicy::Minimum);\n";
 
420
 
 
421
    TreeWalker::acceptSpacer(node);
 
422
}
 
423
 
 
424
void WriteInitialization::acceptLayoutItem(DomLayoutItem *node)
 
425
{
 
426
    TreeWalker::acceptLayoutItem(node);
 
427
 
 
428
    DomLayout *layout = m_layoutChain.top();
 
429
 
 
430
    if (!layout)
 
431
        return;
 
432
 
 
433
    QString varName = driver->findOrInsertLayoutItem(node);
 
434
    QString layoutName = driver->findOrInsertLayout(layout);
 
435
 
 
436
    QString opt;
 
437
    if (layout->attributeClass() == QLatin1String("QGridLayout")) {
 
438
        int row = node->attributeRow();
 
439
        int col = node->attributeColumn();
 
440
 
 
441
        int rowSpan = 1;
 
442
        if (node->hasAttributeRowSpan())
 
443
            rowSpan = node->attributeRowSpan();
 
444
 
 
445
        int colSpan = 1;
 
446
        if (node->hasAttributeColSpan())
 
447
            colSpan = node->attributeColSpan();
 
448
 
 
449
        opt = QString::fromLatin1(", %1, %2, %3, %4").arg(row).arg(col).arg(rowSpan).arg(colSpan);
 
450
    }
 
451
 
 
452
    QString method = QLatin1String("addItem");
 
453
    switch (node->kind()) {
 
454
        case DomLayoutItem::Widget:
 
455
            method = QLatin1String("addWidget");
 
456
            break;
 
457
        case DomLayoutItem::Layout:
 
458
            method = QLatin1String("addLayout");
 
459
            break;
 
460
        case DomLayoutItem::Spacer:
 
461
            method = QLatin1String("addItem");
 
462
            break;
 
463
        case DomLayoutItem::Unknown:
 
464
            Q_ASSERT( 0 );
 
465
            break;
 
466
    }
 
467
 
 
468
    output << "\n" << option.indent << layoutName << "->" << method << "(" << varName << opt << ");\n\n";
 
469
}
 
470
 
 
471
void WriteInitialization::acceptActionGroup(DomActionGroup *node)
 
472
{
 
473
    QString actionName = driver->findOrInsertActionGroup(node);
 
474
    QString varName = driver->findOrInsertWidget(m_widgetChain.top());
 
475
 
 
476
    if (m_actionGroupChain.top())
 
477
        varName = driver->findOrInsertActionGroup(m_actionGroupChain.top());
 
478
 
 
479
    output << option.indent << actionName << " = new QActionGroup(" << varName << ");\n";
 
480
    writeProperties(actionName, QLatin1String("QActionGroup"), node->elementProperty());
 
481
 
 
482
    m_actionGroupChain.push(node);
 
483
    TreeWalker::acceptActionGroup(node);
 
484
    m_actionGroupChain.pop();
 
485
}
 
486
 
 
487
void WriteInitialization::acceptAction(DomAction *node)
 
488
{
 
489
    if (node->hasAttributeMenu())
 
490
        return;
 
491
 
 
492
    QString actionName = driver->findOrInsertAction(node);
 
493
    QString varName = driver->findOrInsertWidget(m_widgetChain.top());
 
494
 
 
495
    if (m_actionGroupChain.top())
 
496
        varName = driver->findOrInsertActionGroup(m_actionGroupChain.top());
 
497
 
 
498
    output << option.indent << actionName << " = new QAction(" << varName << ");\n";
 
499
    writeProperties(actionName, QLatin1String("QAction"), node->elementProperty());
 
500
}
 
501
 
 
502
void WriteInitialization::acceptActionRef(DomActionRef *node)
 
503
{
 
504
    QString actionName = node->attributeName();
 
505
    bool isSeparator = actionName == QLatin1String("separator");
 
506
    bool isMenu = false;
 
507
 
 
508
    QString varName = driver->findOrInsertWidget(m_widgetChain.top());
 
509
 
 
510
    if (actionName.isEmpty() || !m_widgetChain.top()) {
 
511
        return;
 
512
    } else if (driver->actionGroupByName(actionName)) {
 
513
        return;
 
514
    } else if (DomWidget *w = driver->widgetByName(actionName)) {
 
515
        isMenu = uic->isMenu(w->attributeClass());
 
516
        bool inQ3ToolBar = uic->customWidgetsInfo()->extends(m_widgetChain.top()->attributeClass(), QLatin1String("Q3ToolBar"));
 
517
        if (!isMenu && inQ3ToolBar) {
 
518
            actionOut << option.indent << actionName << "->setParent(0);\n";
 
519
            actionOut << option.indent << actionName << "->setParent(" << varName << ");\n";
 
520
            return;
 
521
        }
 
522
    } else if (!(driver->actionByName(actionName) || isSeparator)) {
 
523
        fprintf(stderr, "Warning: action `%s' not declared\n", actionName.toLatin1().data());
 
524
        return;
 
525
    }
 
526
 
 
527
    if (m_widgetChain.top() && isSeparator) {
 
528
        // separator is always reserved!
 
529
        actionOut << option.indent << varName << "->addSeparator();\n";
 
530
        return;
 
531
    }
 
532
 
 
533
    if (isMenu)
 
534
        actionName += QLatin1String("->menuAction()");
 
535
 
 
536
    actionOut << option.indent << varName << "->addAction(" << actionName << ");\n";
 
537
}
 
538
 
 
539
void WriteInitialization::writeProperties(const QString &varName,
 
540
                                          const QString &className,
 
541
                                          const QList<DomProperty*> &lst)
 
542
{
 
543
    bool isTopLevel = m_widgetChain.count() == 1;
 
544
 
 
545
    if (uic->customWidgetsInfo()->extends(className, QLatin1String("QAxWidget"))) {
 
546
        QHash<QString, DomProperty*> properties = propertyMap(lst);
 
547
        if (properties.contains(QLatin1String("control"))) {
 
548
            DomProperty *p = properties.value(QLatin1String("control"));
 
549
            output << option.indent << varName << "->setControl(QString::fromUtf8("
 
550
                   << fixString(toString(p->elementString())) << "));\n";
 
551
        }
 
552
    }
 
553
 
 
554
    DomWidget *buttonGroupWidget = findWidget(QLatin1String("Q3ButtonGroup"));
 
555
 
 
556
    output << option.indent << varName << "->setObjectName(QString::fromUtf8(" << fixString(varName) << "));\n";
 
557
 
 
558
    for (int i=0; i<lst.size(); ++i) {
 
559
        DomProperty *p = lst.at(i);
 
560
        QString propertyName = p->attributeName();
 
561
        QString propertyValue;
 
562
 
 
563
        // special case for the property `geometry'
 
564
        if (isTopLevel && propertyName == QLatin1String("geometry") && p->elementRect()) {
 
565
            DomRect *r = p->elementRect();
 
566
            int w = r->elementWidth();
 
567
            int h = r->elementHeight();
 
568
            output << option.indent << varName << "->resize(QSize(" << w << ", " << h << ").expandedTo("
 
569
                << varName << "->minimumSizeHint()));\n";
 
570
            continue;
 
571
        } else if (propertyName == QLatin1String("buttonGroupId") && buttonGroupWidget) { // Q3ButtonGroup support
 
572
            output << option.indent << driver->findOrInsertWidget(buttonGroupWidget) << "->insert("
 
573
                   << varName << ", " << p->elementNumber() << ");\n";
 
574
            continue;
 
575
        } else if (propertyName == QLatin1String("control") // ActiveQt support
 
576
                    && uic->customWidgetsInfo()->extends(className, QLatin1String("QAxWidget"))) {
 
577
            // already done ;)
 
578
            continue;
 
579
        } else if (propertyName == QLatin1String("database")
 
580
                    && p->elementStringList()) {
 
581
            // Sql support
 
582
            continue;
 
583
        } else if (propertyName == QLatin1String("frameworkCode")
 
584
                    && p->kind() == DomProperty::Bool) {
 
585
            // Sql support
 
586
            continue;
 
587
        } else if (propertyName == QLatin1String("orientation")
 
588
                    && uic->customWidgetsInfo()->extends(className, QLatin1String("Line"))) {
 
589
            // Line support
 
590
            QString shape = QLatin1String("QFrame::HLine");
 
591
            if (p->elementEnum() == QLatin1String("Qt::Vertical"))
 
592
                shape = QLatin1String("QFrame::VLine");
 
593
 
 
594
            output << option.indent << varName << "->setFrameShape(" << shape << ");\n";
 
595
            continue;
 
596
        }
 
597
 
 
598
        bool stdset = m_stdsetdef;
 
599
        if (p->hasAttributeStdset())
 
600
            stdset = p->attributeStdset();
 
601
 
 
602
        QString setFunction = stdset
 
603
                ? QLatin1String("->set") + propertyName.left(1).toUpper() + propertyName.mid(1) + QLatin1String("(")
 
604
                : QLatin1String("->setProperty(\"") + propertyName + QLatin1String("\", QVariant(");
 
605
 
 
606
        switch (p->kind()) {
 
607
        case DomProperty::Bool: {
 
608
            propertyValue = p->elementBool();
 
609
            break;
 
610
        }
 
611
        case DomProperty::Color: {
 
612
            DomColor *c = p->elementColor();
 
613
            propertyValue = QString::fromLatin1("QColor(%1, %2, %3)")
 
614
                  .arg(c->elementRed())
 
615
                  .arg(c->elementGreen())
 
616
                  .arg(c->elementBlue()); }
 
617
            break;
 
618
        case DomProperty::Cstring:
 
619
            if (propertyName == QLatin1String("buddy") && uic->customWidgetsInfo()->extends(className, QLatin1String("QLabel"))) {
 
620
                m_buddies.append(Buddy(varName, p->elementCstring()));
 
621
            } else {
 
622
                propertyValue = fixString(p->elementCstring());
 
623
            }
 
624
            break;
 
625
        case DomProperty::Cursor:
 
626
            propertyValue = QString::fromLatin1("QCursor(static_cast<Qt::CursorShape>(%1))")
 
627
                            .arg(p->elementCursor());
 
628
            break;
 
629
        case DomProperty::Enum:
 
630
            propertyValue = p->elementEnum();
 
631
            if (!propertyValue.contains(QLatin1String("::")))
 
632
                propertyValue.prepend(className + QLatin1String(QLatin1String("::")));
 
633
            break;
 
634
        case DomProperty::Set:
 
635
            propertyValue = p->elementSet();
 
636
            break;
 
637
        case DomProperty::Font: {
 
638
            DomFont *f = p->elementFont();
 
639
            QString fontName = driver->unique(QLatin1String("font"));
 
640
            output << option.indent << "QFont " << fontName << ";\n";
 
641
            output << option.indent << fontName << ".setFamily(QString::fromUtf8(" << fixString(f->elementFamily())
 
642
                << "));\n";
 
643
            output << option.indent << fontName << ".setPointSize(" << f->elementPointSize()
 
644
                << ");\n";
 
645
            output << option.indent << fontName << ".setBold("
 
646
                << (f->elementBold() ? "true" : "false") << ");\n";
 
647
            output << option.indent << fontName << ".setItalic("
 
648
                <<  (f->elementItalic() ? "true" : "false") << ");\n";
 
649
            output << option.indent << fontName << ".setUnderline("
 
650
                << (f->elementUnderline() ? "true" : "false") << ");\n";
 
651
            output << option.indent << fontName << ".setWeight("
 
652
                << f->elementWeight() << ");" << endl;
 
653
            output << option.indent << fontName << ".setStrikeOut("
 
654
                << (f->elementStrikeOut() ? "true" : "false") << ");\n";
 
655
            propertyValue = fontName;
 
656
            break;
 
657
        }
 
658
        case DomProperty::IconSet:
 
659
            propertyValue = pixCall(p->elementIconSet());
 
660
            break;
 
661
 
 
662
        case DomProperty::Pixmap:
 
663
            propertyValue = pixCall(p->elementPixmap());
 
664
            break;
 
665
 
 
666
        case DomProperty::Palette: {
 
667
            DomPalette *pal = p->elementPalette();
 
668
            QString paletteName = driver->unique(QLatin1String("palette"));
 
669
            output << option.indent << "QPalette " << paletteName << ";\n";
 
670
 
 
671
            writeColorGroup(pal->elementActive(), QLatin1String("QPalette::Active"), paletteName);
 
672
            writeColorGroup(pal->elementInactive(), QLatin1String("QPalette::Inactive"), paletteName);
 
673
            writeColorGroup(pal->elementDisabled(), QLatin1String("QPalette::Disabled"), paletteName);
 
674
 
 
675
            propertyValue = paletteName;
 
676
            break;
 
677
        }
 
678
        case DomProperty::Point: {
 
679
            DomPoint *po = p->elementPoint();
 
680
            propertyValue = QString::fromLatin1("QPoint(%1, %2)")
 
681
                            .arg(po->elementX()).arg(po->elementY());
 
682
            break;
 
683
        }
 
684
        case DomProperty::Rect: {
 
685
            DomRect *r = p->elementRect();
 
686
            propertyValue = QString::fromLatin1("QRect(%1, %2, %3, %4)")
 
687
                            .arg(r->elementX()).arg(r->elementY())
 
688
                            .arg(r->elementWidth()).arg(r->elementHeight());
 
689
            break;
 
690
        }
 
691
        case DomProperty::SizePolicy: {
 
692
            DomSizePolicy *sp = p->elementSizePolicy();
 
693
            QString spName = driver->unique(QLatin1String("sizePolicy"));
 
694
            output << option.indent << "QSizePolicy " << spName << QString::fromLatin1(
 
695
                "((QSizePolicy::Policy)%1, (QSizePolicy::Policy)%2);\n")
 
696
                            .arg(sp->elementHSizeType())
 
697
                            .arg(sp->elementVSizeType());
 
698
            output << option.indent << spName << ".setHorizontalStretch("
 
699
                << sp->elementHorStretch() << ");\n";
 
700
            output << option.indent << spName << ".setVerticalStretch("
 
701
                << sp->elementVerStretch() << ");\n";
 
702
            output << option.indent << spName << QString::fromLatin1(
 
703
                ".setHeightForWidth(%1->sizePolicy().hasHeightForWidth());\n")
 
704
                .arg(varName);
 
705
 
 
706
            propertyValue = spName;
 
707
            break;
 
708
        }
 
709
        case DomProperty::Size: {
 
710
             DomSize *s = p->elementSize();
 
711
              propertyValue = QString::fromLatin1("QSize(%1, %2)")
 
712
                             .arg(s->elementWidth()).arg(s->elementHeight());
 
713
            break;
 
714
        }
 
715
        case DomProperty::String: {
 
716
            if (propertyName == QLatin1String("objectName")) {
 
717
                QString v = p->elementString()->text();
 
718
                if (v == varName)
 
719
                    break;
 
720
 
 
721
                // ### qWarning("Deprecated: the property `objectName' is different from the variable name");
 
722
            }
 
723
 
 
724
            if (p->elementString()->hasAttributeNotr()
 
725
                    && toBool(p->elementString()->attributeNotr())) {
 
726
                propertyValue = QLatin1String("QString::fromUtf8(")
 
727
                        + fixString(p->elementString()->text())
 
728
                        + QLatin1String(")");
 
729
            } else {
 
730
                propertyValue = trCall(p->elementString(), className);
 
731
            }
 
732
            break;
 
733
        }
 
734
        case DomProperty::Number:
 
735
            propertyValue = QString::number(p->elementNumber());
 
736
            break;
 
737
        case DomProperty::Float:
 
738
            propertyValue = QString::number(p->elementFloat());
 
739
            break;
 
740
        case DomProperty::Double:
 
741
            propertyValue = QString::number(p->elementDouble());
 
742
            break;
 
743
        case DomProperty::Date: {
 
744
            DomDate *d = p->elementDate();
 
745
            propertyValue = QString::fromLatin1("QDate(%1, %2, %3)")
 
746
                            .arg(d->elementYear())
 
747
                            .arg(d->elementMonth())
 
748
                            .arg(d->elementDay());
 
749
            break;
 
750
        }
 
751
        case DomProperty::Time: {
 
752
            DomTime *t = p->elementTime();
 
753
            propertyValue = QString::fromLatin1("QTime(%1, %2, %3)")
 
754
                            .arg(t->elementHour())
 
755
                            .arg(t->elementMinute())
 
756
                            .arg(t->elementSecond());
 
757
            break;
 
758
        }
 
759
        case DomProperty::DateTime: {
 
760
            DomDateTime *dt = p->elementDateTime();
 
761
            propertyValue = QString::fromLatin1("QDateTime(QDate(%1, %2, %3), QTime(%4, %5, %6))")
 
762
                            .arg(dt->elementYear())
 
763
                            .arg(dt->elementMonth())
 
764
                            .arg(dt->elementDay())
 
765
                            .arg(dt->elementHour())
 
766
                            .arg(dt->elementMinute())
 
767
                            .arg(dt->elementSecond());
 
768
            break;
 
769
        }
 
770
        case DomProperty::StringList:
 
771
            propertyValue = QLatin1String("QStringList()");
 
772
            if (p->elementStringList()->elementString().size()) {
 
773
                QStringList lst = p->elementStringList()->elementString();
 
774
                for (int i=0; i<lst.size(); ++i) {
 
775
                    propertyValue += QLatin1String(" << ") + fixString(lst.at(i));
 
776
                }
 
777
            }
 
778
            break;
 
779
 
 
780
        case DomProperty::Unknown:
 
781
            break;
 
782
        }
 
783
 
 
784
        if (propertyValue.size()) {
 
785
            QTextStream *o = &output;
 
786
 
 
787
            if (p->kind() == DomProperty::String
 
788
                    && (!p->elementString()->hasAttributeNotr() || !toBool(p->elementString()->attributeNotr())))
 
789
                o = &refreshOut;
 
790
 
 
791
            (*o) << option.indent << varName << setFunction << propertyValue;
 
792
            if (!stdset)
 
793
                (*o) << ")";
 
794
            (*o) << ");\n";
 
795
        }
 
796
    }
 
797
}
 
798
 
 
799
QString WriteInitialization::domColor2QString(DomColor *c)
 
800
{
 
801
    return QString::fromLatin1("QColor(%1, %2, %3)")
 
802
        .arg(c->elementRed())
 
803
        .arg(c->elementGreen())
 
804
        .arg(c->elementBlue());
 
805
}
 
806
 
 
807
void WriteInitialization::writeColorGroup(DomColorGroup *colorGroup, const QString &group, const QString &paletteName)
 
808
{
 
809
    if (!colorGroup)
 
810
        return;
 
811
 
 
812
    QList<DomColor*> colors = colorGroup->elementColor();
 
813
    for (int i=0; i<colors.size(); ++i) {
 
814
        DomColor *color = colors.at(i);
 
815
 
 
816
        output << option.indent << paletteName << ".setColor(" << group
 
817
            << ", " << "static_cast<QPalette::ColorRole>(" << i << ")"
 
818
            << ", " << domColor2QString(color)
 
819
            << ");\n";
 
820
    }
 
821
}
 
822
 
 
823
void WriteInitialization::acceptCustomWidget(DomCustomWidget *node)
 
824
{
 
825
    Q_UNUSED(node);
 
826
}
 
827
 
 
828
void WriteInitialization::acceptCustomWidgets(DomCustomWidgets *node)
 
829
{
 
830
    Q_UNUSED(node);
 
831
}
 
832
 
 
833
void WriteInitialization::acceptTabStops(DomTabStops *tabStops)
 
834
{
 
835
    QString lastName;
 
836
 
 
837
    QStringList l = tabStops->elementTabStop();
 
838
    for (int i=0; i<l.size(); ++i) {
 
839
        QString name = l.at(i);
 
840
 
 
841
        if (!m_registeredWidgets.contains(name)) {
 
842
            fprintf(stderr, "'%s' isn't a valid widget\n", name.toLatin1().data());
 
843
            continue;
 
844
        }
 
845
 
 
846
        if (i == 0) {
 
847
            lastName = name;
 
848
            continue;
 
849
        } else if (name.isEmpty() || lastName.isEmpty()) {
 
850
            continue;
 
851
        }
 
852
 
 
853
        output << option.indent << "QWidget::setTabOrder(" << lastName << ", " << name << ");\n";
 
854
 
 
855
        lastName = name;
 
856
    }
 
857
}
 
858
 
 
859
QString WriteInitialization::translate(const QString &text, const QString &className) const
 
860
{
 
861
    if (option.translateFunction.size())
 
862
        return option.translateFunction + QLatin1String("(") + text + QLatin1String(")");
 
863
 
 
864
    Q_UNUSED(className);
 
865
 
 
866
    return QLatin1String("QApplication::translate(\"") + m_generatedClass + QLatin1String("\", ") + text + QLatin1String(")");
 
867
}
 
868
 
 
869
void WriteInitialization::acceptLayoutDefault(DomLayoutDefault *node)
 
870
{
 
871
    m_defaultMargin = INT_MIN;
 
872
    m_defaultSpacing = INT_MIN;
 
873
 
 
874
    if (!node)
 
875
        return;
 
876
 
 
877
    if (node->hasAttributeMargin())
 
878
        m_defaultMargin = node->attributeMargin();
 
879
 
 
880
    if (node->hasAttributeSpacing())
 
881
        m_defaultSpacing = node->attributeSpacing();
 
882
}
 
883
 
 
884
void WriteInitialization::acceptLayoutFunction(DomLayoutFunction *node)
 
885
{
 
886
    m_marginFunction.clear();
 
887
    m_spacingFunction.clear();
 
888
 
 
889
    if (!node)
 
890
        return;
 
891
 
 
892
    if (node->hasAttributeMargin())
 
893
        m_marginFunction = node->attributeMargin();
 
894
 
 
895
    if (node->hasAttributeSpacing())
 
896
        m_spacingFunction = node->attributeSpacing();
 
897
}
 
898
 
 
899
void WriteInitialization::initializeQ3ListBox(DomWidget *w)
 
900
{
 
901
    QString varName = driver->findOrInsertWidget(w);
 
902
    QString className = w->attributeClass();
 
903
 
 
904
    QList<DomItem*> items = w->elementItem();
 
905
 
 
906
    if (items.isEmpty())
 
907
        return;
 
908
 
 
909
    refreshOut << option.indent << varName << "->clear();\n";
 
910
 
 
911
    for (int i=0; i<items.size(); ++i) {
 
912
        DomItem *item = items.at(i);
 
913
 
 
914
        QHash<QString, DomProperty*> properties = propertyMap(item->elementProperty());
 
915
        DomProperty *text = properties.value(QLatin1String("text"));
 
916
        DomProperty *pixmap = properties.value(QLatin1String("pixmap"));
 
917
        if (!(text || pixmap))
 
918
            continue;
 
919
 
 
920
        refreshOut << option.indent << varName << "->insertItem(";
 
921
        if (pixmap) {
 
922
            refreshOut << pixCall(pixmap->elementPixmap());
 
923
 
 
924
            if (text)
 
925
                refreshOut << ", ";
 
926
        }
 
927
        refreshOut << trCall(text->elementString(), className) << ");\n";
 
928
    }
 
929
}
 
930
 
 
931
void WriteInitialization::initializeQ3IconView(DomWidget *w)
 
932
{
 
933
    QString varName = driver->findOrInsertWidget(w);
 
934
    QString className = w->attributeClass();
 
935
 
 
936
    QList<DomItem*> items = w->elementItem();
 
937
 
 
938
    if (items.isEmpty())
 
939
        return;
 
940
 
 
941
    refreshOut << option.indent << varName << "->clear();\n";
 
942
 
 
943
    for (int i=0; i<items.size(); ++i) {
 
944
        DomItem *item = items.at(i);
 
945
 
 
946
        QHash<QString, DomProperty*> properties = propertyMap(item->elementProperty());
 
947
        DomProperty *text = properties.value(QLatin1String("text"));
 
948
        DomProperty *pixmap = properties.value(QLatin1String("pixmap"));
 
949
        if (!(text || pixmap))
 
950
            continue;
 
951
 
 
952
        QString itemName = driver->unique(QLatin1String("__item"));
 
953
        refreshOut << "\n";
 
954
        refreshOut << option.indent << "Q3IconViewItem *" << itemName << " = new Q3IconViewItem(" << varName << ");\n";
 
955
 
 
956
        if (pixmap) {
 
957
            refreshOut << option.indent << itemName << "->setPixmap(" << pixCall(pixmap->elementPixmap()) << ");\n";
 
958
        }
 
959
 
 
960
        if (text) {
 
961
            refreshOut << option.indent << itemName << "->setText(" << trCall(text->elementString(), className) << ");\n";
 
962
        }
 
963
    }
 
964
}
 
965
 
 
966
void WriteInitialization::initializeQ3ListView(DomWidget *w)
 
967
{
 
968
    QString varName = driver->findOrInsertWidget(w);
 
969
    QString className = w->attributeClass();
 
970
 
 
971
    // columns
 
972
    QList<DomColumn*> columns = w->elementColumn();
 
973
    for (int i=0; i<columns.size(); ++i) {
 
974
        DomColumn *column = columns.at(i);
 
975
 
 
976
        QHash<QString, DomProperty*> properties = propertyMap(column->elementProperty());
 
977
        DomProperty *text = properties.value(QLatin1String("text"));
 
978
        DomProperty *pixmap = properties.value(QLatin1String("pixmap"));
 
979
        DomProperty *clickable = properties.value(QLatin1String("clickable"));
 
980
        DomProperty *resizable = properties.value(QLatin1String("resizable"));
 
981
 
 
982
        QString txt = trCall(text->elementString(), className);
 
983
        output << option.indent << varName << "->addColumn(" << txt << ");\n";
 
984
        refreshOut << option.indent << varName << "->header()->setLabel(" << i << ", " << txt << ");\n";
 
985
 
 
986
        if (pixmap) {
 
987
            output << option.indent << varName << "->header()->setLabel("
 
988
                   << varName << "->header()->count() - 1, " << pixCall(pixmap->elementIconSet()) << ", " << txt << ");\n";
 
989
        }
 
990
 
 
991
        if (clickable != 0) {
 
992
            output << option.indent << varName << "->header()->setClickEnabled(" << clickable->elementBool() << ", " << varName << "->header()->count() - 1);\n";
 
993
        }
 
994
 
 
995
        if (resizable != 0) {
 
996
            output << option.indent << varName << "->header()->setResizeEnabled(" << resizable->elementBool() << ", " << varName << "->header()->count() - 1);\n";
 
997
        }
 
998
    }
 
999
 
 
1000
    if (w->elementItem().size()) {
 
1001
        refreshOut << option.indent << varName << "->clear();\n";
 
1002
 
 
1003
        initializeQ3ListViewItems(className, varName, w->elementItem());
 
1004
    }
 
1005
}
 
1006
 
 
1007
void WriteInitialization::initializeQ3ListViewItems(const QString &className, const QString &varName, const QList<DomItem *> &items)
 
1008
{
 
1009
    if (items.isEmpty())
 
1010
        return;
 
1011
 
 
1012
    // items
 
1013
    for (int i=0; i<items.size(); ++i) {
 
1014
        DomItem *item = items.at(i);
 
1015
 
 
1016
        QString itemName = driver->unique(QLatin1String("__item"));
 
1017
        refreshOut << "\n";
 
1018
        refreshOut << option.indent << "Q3ListViewItem *" << itemName << " = new Q3ListViewItem(" << varName << ");\n";
 
1019
 
 
1020
        int textCount = 0, pixCount = 0;
 
1021
        QList<DomProperty*> properties = item->elementProperty();
 
1022
        for (int i=0; i<properties.size(); ++i) {
 
1023
            DomProperty *p = properties.at(i);
 
1024
            if (p->attributeName() == QLatin1String("text"))
 
1025
                refreshOut << option.indent << itemName << "->setText(" << textCount++ << ", "
 
1026
                           << trCall(p->elementString(), className) << ");\n";
 
1027
 
 
1028
            if (p->attributeName() == QLatin1String("pixmap"))
 
1029
                refreshOut << option.indent << itemName << "->setPixmap(" << pixCount++ << ", "
 
1030
                           << pixCall(p->elementPixmap()) << ");\n";
 
1031
        }
 
1032
 
 
1033
        if (item->elementItem().size()) {
 
1034
            refreshOut << option.indent << itemName << "->setOpen(true);\n";
 
1035
            initializeQ3ListViewItems(className, itemName, item->elementItem());
 
1036
        }
 
1037
    }
 
1038
}
 
1039
 
 
1040
void WriteInitialization::initializeTreeWidgetItems(const QString &className, const QString &varName, const QList<DomItem *> &items)
 
1041
{
 
1042
    if (items.isEmpty())
 
1043
        return;
 
1044
 
 
1045
    // items
 
1046
    for (int i=0; i<items.size(); ++i) {
 
1047
        DomItem *item = items.at(i);
 
1048
 
 
1049
        QString itemName = driver->unique(QLatin1String("__item"));
 
1050
        refreshOut << "\n";
 
1051
        refreshOut << option.indent << "QTreeWidgetItem *" << itemName << " = new QTreeWidgetItem(" << varName << ");\n";
 
1052
 
 
1053
        int textCount = 0, pixCount = 0;
 
1054
        QList<DomProperty*> properties = item->elementProperty();
 
1055
        for (int i=0; i<properties.size(); ++i) {
 
1056
            DomProperty *p = properties.at(i);
 
1057
            if (p->attributeName() == QLatin1String("text"))
 
1058
                refreshOut << option.indent << itemName << "->setText(" << textCount++ << ", "
 
1059
                           << trCall(p->elementString(), className) << ");\n";
 
1060
 
 
1061
            if (p->attributeName() == QLatin1String("icon"))
 
1062
                refreshOut << option.indent << itemName << "->setIcon(" << pixCount++ << ", "
 
1063
                           << pixCall(p->elementIconSet()) << ");\n";
 
1064
        }
 
1065
 
 
1066
        if (item->elementItem().size()) {
 
1067
            refreshOut << option.indent << varName << "->setExpanded(" << itemName << ", " << "true);\n";
 
1068
            initializeTreeWidgetItems(className, itemName, item->elementItem());
 
1069
        }
 
1070
    }
 
1071
}
 
1072
 
 
1073
 
 
1074
void WriteInitialization::initializeQ3Table(DomWidget *w)
 
1075
{
 
1076
    QString varName = driver->findOrInsertWidget(w);
 
1077
    QString className = w->attributeClass();
 
1078
 
 
1079
    // columns
 
1080
    QList<DomColumn*> columns = w->elementColumn();
 
1081
    output << option.indent << varName << "->setNumCols(" << columns.size() << ");\n";
 
1082
 
 
1083
    for (int i=0; i<columns.size(); ++i) {
 
1084
        DomColumn *column = columns.at(i);
 
1085
 
 
1086
        QHash<QString, DomProperty*> properties = propertyMap(column->elementProperty());
 
1087
        DomProperty *text = properties.value(QLatin1String("text"));
 
1088
        DomProperty *pixmap = properties.value(QLatin1String("pixmap"));
 
1089
 
 
1090
        refreshOut << option.indent << varName << "->horizontalHeader()->setLabel(" << i << ", ";
 
1091
        if (pixmap) {
 
1092
            refreshOut << pixCall(pixmap->elementPixmap()) << ", ";
 
1093
        }
 
1094
        refreshOut << trCall(text->elementString(), className) << ");\n";
 
1095
    }
 
1096
 
 
1097
    // rows
 
1098
    QList<DomRow*> rows = w->elementRow();
 
1099
    refreshOut << option.indent << varName << "->setNumRows(" << rows.size() << ");\n";
 
1100
 
 
1101
    for (int i=0; i<rows.size(); ++i) {
 
1102
        DomRow *row = rows.at(i);
 
1103
 
 
1104
        QHash<QString, DomProperty*> properties = propertyMap(row->elementProperty());
 
1105
        DomProperty *text = properties.value(QLatin1String("text"));
 
1106
        DomProperty *pixmap = properties.value(QLatin1String("pixmap"));
 
1107
 
 
1108
        refreshOut << option.indent << varName << "->verticalHeader()->setLabel(" << i << ", ";
 
1109
        if (pixmap) {
 
1110
            refreshOut << pixCall(pixmap->elementPixmap()) << ", ";
 
1111
        }
 
1112
        refreshOut << trCall(text->elementString(), className) << ");\n";
 
1113
    }
 
1114
 
 
1115
 
 
1116
    //initializeQ3TableItems(className, varName, w->elementItem());
 
1117
}
 
1118
 
 
1119
void WriteInitialization::initializeQ3TableItems(const QString &className, const QString &varName, const QList<DomItem *> &items)
 
1120
{
 
1121
    Q_UNUSED(className);
 
1122
    Q_UNUSED(varName);
 
1123
    Q_UNUSED(items);
 
1124
}
 
1125
 
 
1126
QString WriteInitialization::pixCall(DomResourcePixmap *r) const
 
1127
{
 
1128
    QString s = r->text();
 
1129
 
 
1130
    if (s.isEmpty())
 
1131
        return QLatin1String("QPixmap()");
 
1132
    else if (findImage(s) != 0)
 
1133
        return QLatin1String("icon(") + s + QLatin1String("_ID)");
 
1134
 
 
1135
    QString pixFunc = uic->pixmapFunction();
 
1136
    if (pixFunc.isEmpty())
 
1137
        pixFunc = QLatin1String("QString::fromUtf8");
 
1138
 
 
1139
    return QLatin1String("QPixmap(") + pixFunc + QLatin1String("(") + fixString(s) + QLatin1String(")") + QLatin1String(")");
 
1140
}
 
1141
 
 
1142
void WriteInitialization::initializeComboBox(DomWidget *w)
 
1143
{
 
1144
    QString varName = driver->findOrInsertWidget(w);
 
1145
    QString className = w->attributeClass();
 
1146
 
 
1147
    QList<DomItem*> items = w->elementItem();
 
1148
 
 
1149
    if (items.isEmpty())
 
1150
        return;
 
1151
 
 
1152
    for (int i=0; i<items.size(); ++i) {
 
1153
        DomItem *item = items.at(i);
 
1154
 
 
1155
        QHash<QString, DomProperty*> properties = propertyMap(item->elementProperty());
 
1156
        DomProperty *text = properties.value(QLatin1String("text"));
 
1157
        DomProperty *pixmap = properties.value(QLatin1String("icon"));
 
1158
        if (!(text || pixmap))
 
1159
            continue;
 
1160
 
 
1161
        refreshOut << option.indent << varName << "->addItem(";
 
1162
 
 
1163
        if (pixmap != 0) {
 
1164
            DomResourcePixmap *pix = pixmap->elementIconSet();
 
1165
            if (pix == 0)
 
1166
                pix = pixmap->elementPixmap(); // ### for q3support code!
 
1167
 
 
1168
            Q_ASSERT(pix != 0);
 
1169
 
 
1170
            refreshOut << pixCall(pix);
 
1171
 
 
1172
            if (text)
 
1173
                refreshOut << ", ";
 
1174
        }
 
1175
 
 
1176
        refreshOut << trCall(text->elementString(), className) << ");\n";
 
1177
    }
 
1178
}
 
1179
 
 
1180
void WriteInitialization::initializeListWidget(DomWidget *w)
 
1181
{
 
1182
    QString varName = driver->findOrInsertWidget(w);
 
1183
    QString className = w->attributeClass();
 
1184
 
 
1185
    QList<DomItem*> items = w->elementItem();
 
1186
 
 
1187
    if (items.isEmpty())
 
1188
        return;
 
1189
 
 
1190
    refreshOut << option.indent << varName << "->clear();\n";
 
1191
 
 
1192
    // items
 
1193
    for (int i=0; i<items.size(); ++i) {
 
1194
        DomItem *item = items.at(i);
 
1195
 
 
1196
        QString itemName = driver->unique(QLatin1String("__item"));
 
1197
        refreshOut << "\n";
 
1198
        refreshOut << option.indent << "QListWidgetItem *" << itemName << " = new QListWidgetItem(" << varName << ");\n";
 
1199
 
 
1200
        QList<DomProperty*> properties = item->elementProperty();
 
1201
        for (int i=0; i<properties.size(); ++i) {
 
1202
            DomProperty *p = properties.at(i);
 
1203
 
 
1204
            if (p->attributeName() == QLatin1String("text"))
 
1205
                refreshOut << option.indent << itemName << "->setText(" << trCall(p->elementString(), className) << ");\n";
 
1206
 
 
1207
            if (p->attributeName() == QLatin1String("icon"))
 
1208
                refreshOut << option.indent << itemName << "->setIcon(" << pixCall(p->elementIconSet()) << ");\n";
 
1209
        }
 
1210
    }
 
1211
}
 
1212
 
 
1213
void WriteInitialization::initializeTreeWidget(DomWidget *w)
 
1214
{
 
1215
    QString varName = driver->findOrInsertWidget(w);
 
1216
    QString className = w->attributeClass();
 
1217
 
 
1218
    // columns
 
1219
    QList<DomColumn*> columns = w->elementColumn();
 
1220
    for (int i=0; i<columns.size(); ++i) {
 
1221
        DomColumn *column = columns.at(i);
 
1222
 
 
1223
        QHash<QString, DomProperty*> properties = propertyMap(column->elementProperty());
 
1224
        DomProperty *text = properties.value(QLatin1String("text"));
 
1225
        DomProperty *icon = properties.value(QLatin1String("icon"));
 
1226
 
 
1227
        QString txt = trCall(text->elementString(), className);
 
1228
        refreshOut << option.indent << varName << "->headerItem()->setText(" << i << ", " << txt << ");\n";
 
1229
 
 
1230
        if (icon != 0 && icon->elementIconSet()) {
 
1231
            output << option.indent << varName << "->headerItem()->setIcon("
 
1232
                   << varName << "->headerItem()->childCount() - 1, " << pixCall(icon->elementIconSet()) << ");\n";
 
1233
        }
 
1234
    }
 
1235
 
 
1236
    if (w->elementItem().size()) {
 
1237
        refreshOut << option.indent << varName << "->clear();\n";
 
1238
 
 
1239
        initializeTreeWidgetItems(className, varName, w->elementItem());
 
1240
    }
 
1241
}
 
1242
 
 
1243
void WriteInitialization::initializeTableWidget(DomWidget *w)
 
1244
{
 
1245
    // ### not implemented yet
 
1246
    Q_UNUSED(w);
 
1247
}
 
1248
 
 
1249
QString WriteInitialization::trCall(const QString &str, const QString &className) const
 
1250
{
 
1251
    return translate(fixString(str), className);
 
1252
}
 
1253
 
 
1254
void WriteInitialization::initializeSqlDataTable(DomWidget *w)
 
1255
{
 
1256
    QHash<QString, DomProperty*> properties = propertyMap(w->elementProperty());
 
1257
 
 
1258
    DomProperty *frameworkCode = properties.value(QLatin1String("frameworkCode"), 0);
 
1259
    if (frameworkCode && toBool(frameworkCode->elementBool()) == false)
 
1260
        return;
 
1261
 
 
1262
    QString connection;
 
1263
    QString table;
 
1264
    QString field;
 
1265
 
 
1266
    DomProperty *db = properties.value(QLatin1String("database"), 0);
 
1267
    if (db && db->elementStringList()) {
 
1268
        QStringList info = db->elementStringList()->elementString();
 
1269
        connection = info.size() > 0 ? info.at(0) : QString();
 
1270
        table = info.size() > 1 ? info.at(1) : QString();
 
1271
        field = info.size() > 2 ? info.at(2) : QString();
 
1272
    }
 
1273
 
 
1274
    if (table.isEmpty() || connection.isEmpty()) {
 
1275
        fprintf(stderr, "invalid database connection\n");
 
1276
        return;
 
1277
    }
 
1278
 
 
1279
    QString varName = driver->findOrInsertWidget(w);
 
1280
 
 
1281
    output << option.indent << "if (!" << varName << "->sqlCursor()) {\n";
 
1282
 
 
1283
    output << option.indent << option.indent << varName << "->setSqlCursor(";
 
1284
 
 
1285
    if (connection == QLatin1String("(default)")) {
 
1286
        output << "new QSqlCursor(" << fixString(table) << "), false, true);\n";
 
1287
    } else {
 
1288
        output << "new QSqlCursor(" << fixString(table) << ", true, " << connection << "Connection" << "), false, true);\n";
 
1289
    }
 
1290
    output << option.indent << option.indent << varName << "->refresh(QDataTable::RefreshAll);\n";
 
1291
    output << option.indent << "}\n";
 
1292
}
 
1293
 
 
1294
void WriteInitialization::initializeSqlDataBrowser(DomWidget *w)
 
1295
{
 
1296
    QHash<QString, DomProperty*> properties = propertyMap(w->elementProperty());
 
1297
 
 
1298
    DomProperty *frameworkCode = properties.value(QLatin1String("frameworkCode"), 0);
 
1299
    if (frameworkCode && toBool(frameworkCode->elementBool()) == false)
 
1300
        return;
 
1301
 
 
1302
    QString connection;
 
1303
    QString table;
 
1304
    QString field;
 
1305
 
 
1306
    DomProperty *db = properties.value(QLatin1String("database"), 0);
 
1307
    if (db && db->elementStringList()) {
 
1308
        QStringList info = db->elementStringList()->elementString();
 
1309
        connection = info.size() > 0 ? info.at(0) : QString();
 
1310
        table = info.size() > 1 ? info.at(1) : QString();
 
1311
        field = info.size() > 2 ? info.at(2) : QString();
 
1312
    }
 
1313
 
 
1314
    if (table.isEmpty() || connection.isEmpty()) {
 
1315
        fprintf(stderr, "invalid database connection\n");
 
1316
        return;
 
1317
    }
 
1318
 
 
1319
    QString varName = driver->findOrInsertWidget(w);
 
1320
 
 
1321
    output << option.indent << "if (!" << varName << "->sqlCursor()) {\n";
 
1322
 
 
1323
    output << option.indent << option.indent << varName << "->setSqlCursor(";
 
1324
 
 
1325
    if (connection == QLatin1String("(default)")) {
 
1326
        output << "new QSqlCursor(" << fixString(table) << "), true);\n";
 
1327
    } else {
 
1328
        output << "new QSqlCursor(" << fixString(table) << ", true, " << connection << "Connection" << "), false, true);\n";
 
1329
    }
 
1330
    output << option.indent << option.indent << varName << "->refresh();\n";
 
1331
    output << option.indent << "}\n";
 
1332
}
 
1333
 
 
1334
void WriteInitialization::initializeMenu(DomWidget *w, const QString &/*parentWidget*/)
 
1335
{
 
1336
    QString menuName = driver->findOrInsertWidget(w);
 
1337
    QString menuAction = menuName + QLatin1String("Action");
 
1338
 
 
1339
    DomAction *action = driver->actionByName(menuAction);
 
1340
    if (action && action->hasAttributeMenu()) {
 
1341
        output << option.indent << menuAction << " = " << menuName << "->menuAction();\n";
 
1342
    }
 
1343
}
 
1344
 
 
1345
QString WriteInitialization::trCall(const DomString *str, const QString &className) const
 
1346
{
 
1347
    return trCall(toString(str), className);
 
1348
}
 
1349
 
 
1350
void WriteInitialization::acceptConnection(DomConnection *connection)
 
1351
{
 
1352
    if (!m_registeredWidgets.contains(connection->elementSender())
 
1353
            || !m_registeredWidgets.contains(connection->elementReceiver()))
 
1354
        return;
 
1355
 
 
1356
    output << option.indent << "QObject::connect("
 
1357
        << connection->elementSender()
 
1358
        << ", "
 
1359
        << "SIGNAL(" << connection->elementSignal() << ")"
 
1360
        << ", "
 
1361
        << connection->elementReceiver()
 
1362
        << ", "
 
1363
        << "SLOT(" << connection->elementSlot() << ")"
 
1364
        << ");\n";
 
1365
}
 
1366
 
 
1367
DomImage *WriteInitialization::findImage(const QString &name) const
 
1368
{
 
1369
    return m_registeredImages.value(name);
 
1370
}
 
1371
 
 
1372
DomWidget *WriteInitialization::findWidget(const QString &widgetClass)
 
1373
{
 
1374
    for (int i = m_widgetChain.count() - 1; i >= 0; --i) {
 
1375
        DomWidget *widget = m_widgetChain.at(i);
 
1376
 
 
1377
        if (widget && uic->customWidgetsInfo()->extends(widget->attributeClass(), widgetClass))
 
1378
            return widget;
 
1379
    }
 
1380
 
 
1381
    return 0;
 
1382
}
 
1383
 
 
1384
void WriteInitialization::acceptImage(DomImage *image)
 
1385
{
 
1386
    if (!image->hasAttributeName())
 
1387
        return;
 
1388
 
 
1389
    m_registeredImages.insert(image->attributeName(), image);
 
1390
}
 
1391