src/tools/uic/cpp/cppwriteinitialization.cpp
changeset 0 1918ee327afb
child 3 41300fa6a67c
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the tools applications of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 #include "cppwriteinitialization.h"
       
    43 #include "cppwriteiconinitialization.h"
       
    44 #include "driver.h"
       
    45 #include "ui4.h"
       
    46 #include "utils.h"
       
    47 #include "uic.h"
       
    48 #include "databaseinfo.h"
       
    49 #include "globaldefs.h"
       
    50 
       
    51 #include <QtCore/QTextStream>
       
    52 #include <QtCore/QDebug>
       
    53 
       
    54 #include <ctype.h>
       
    55 
       
    56 QT_BEGIN_NAMESPACE
       
    57 
       
    58 namespace {
       
    59     // Fixup an enumeration name from class Qt.
       
    60     // They are currently stored as "BottomToolBarArea" instead of "Qt::BottomToolBarArea".
       
    61     // due to MO issues. This might be fixed in the future.
       
    62     void fixQtEnumerationName(QString& name) {
       
    63         static const QLatin1String prefix("Qt::");
       
    64         if (name.indexOf(prefix) != 0)
       
    65             name.prepend(prefix);
       
    66     }
       
    67     // figure out the toolbar area of a DOM attrib list.
       
    68     // By legacy, it is stored as an integer. As of 4.3.0, it is the enumeration value.
       
    69     QString toolBarAreaStringFromDOMAttributes(const CPP::WriteInitialization::DomPropertyMap &attributes) {
       
    70         const DomProperty *pstyle = attributes.value(QLatin1String("toolBarArea"));
       
    71         if (!pstyle)
       
    72             return QString();
       
    73 
       
    74         switch (pstyle->kind()) {
       
    75         case DomProperty::Number: {
       
    76             QString area = QLatin1String("static_cast<Qt::ToolBarArea>(");
       
    77             area += QString::number(pstyle->elementNumber());
       
    78             area += QLatin1String("), ");
       
    79             return area;
       
    80         }
       
    81         case DomProperty::Enum: {
       
    82             QString area = pstyle->elementEnum();
       
    83             fixQtEnumerationName(area);
       
    84             area += QLatin1String(", ");
       
    85             return area;
       
    86         }
       
    87         default:
       
    88             break;
       
    89         }
       
    90         return QString();
       
    91     }
       
    92 
       
    93     // Write a statement to create a spacer item.
       
    94     void writeSpacerItem(const DomSpacer *node, QTextStream &output) {
       
    95         const QHash<QString, DomProperty *> properties = propertyMap(node->elementProperty());
       
    96                 output << "new QSpacerItem(";
       
    97 
       
    98         if (properties.contains(QLatin1String("sizeHint"))) {
       
    99             const DomSize *sizeHint = properties.value(QLatin1String("sizeHint"))->elementSize();
       
   100             output << sizeHint->elementWidth() << ", " << sizeHint->elementHeight() << ", ";
       
   101         }
       
   102 
       
   103         // size type
       
   104         QString sizeType = properties.contains(QLatin1String("sizeType"))  ?
       
   105                            properties.value(QLatin1String("sizeType"))->elementEnum() :
       
   106                            QString::fromLatin1("Expanding");
       
   107 
       
   108         if (!sizeType.startsWith(QLatin1String("QSizePolicy::")))
       
   109             sizeType.prepend(QLatin1String("QSizePolicy::"));
       
   110         // orientation
       
   111         bool isVspacer = false;
       
   112         if (properties.contains(QLatin1String("orientation"))) {
       
   113             const QString orientation = properties.value(QLatin1String("orientation"))->elementEnum();
       
   114             if (orientation == QLatin1String("Qt::Vertical") || orientation == QLatin1String("Vertical"))  isVspacer = true;
       
   115         }
       
   116 
       
   117         if (isVspacer)
       
   118             output << "QSizePolicy::Minimum, " << sizeType << ')';
       
   119         else
       
   120             output << sizeType << ", QSizePolicy::Minimum)";
       
   121     }
       
   122 
       
   123 
       
   124     // Helper for implementing comparison functions for integers.
       
   125     int compareInt(int i1, int i2) {
       
   126         if (i1 < i2) return -1;
       
   127         if (i1 > i2) return  1;
       
   128         return  0;
       
   129     }
       
   130 
       
   131     // Write object->setFoo(x);
       
   132     template <class Value>
       
   133         void writeSetter(const QString &indent, const QString &varName,const QString &setter, Value v, QTextStream &str) {
       
   134             str << indent << varName << "->" << setter << '(' << v << ");\n";
       
   135         }
       
   136 
       
   137     void writeSetupUIScriptVariableDeclarations(const QString &indent, QTextStream &str)  {
       
   138         str << indent << "ScriptContext scriptContext;\n"
       
   139             << indent << "QWidgetList childWidgets;\n";
       
   140     }
       
   141 
       
   142     static inline bool isIconFormat44(const DomResourceIcon *i) {
       
   143         return i->hasElementNormalOff()   || i->hasElementNormalOn() ||
       
   144                i->hasElementDisabledOff() || i->hasElementDisabledOn() ||
       
   145                i->hasElementActiveOff()   || i->hasElementActiveOn() ||
       
   146                i->hasElementSelectedOff() || i->hasElementSelectedOn();
       
   147     }
       
   148 
       
   149     // Check on properties. Filter out empty legacy pixmap/icon properties
       
   150     // as Designer pre 4.4 used to remove missing resource references.
       
   151     // This can no longer be handled by the code as we have 'setIcon(QIcon())' as well as 'QIcon icon'
       
   152     static bool checkProperty(const QString &fileName, const DomProperty *p) {
       
   153         switch (p->kind()) {
       
   154         case DomProperty::IconSet:
       
   155             if (const DomResourceIcon *dri = p->elementIconSet()) {
       
   156                 if (!isIconFormat44(dri)) {
       
   157                     if (dri->text().isEmpty())  {
       
   158                         const QString msg = QString::fromUtf8("%1: An invalid icon property '%2' was encountered.").arg(fileName).arg(p->attributeName());
       
   159                         qWarning("%s", qPrintable(msg));
       
   160                         return false;
       
   161                     }
       
   162                 }
       
   163             }
       
   164             break;
       
   165         case DomProperty::Pixmap:
       
   166             if (const DomResourcePixmap *drp = p->elementPixmap())
       
   167                 if (drp->text().isEmpty()) {
       
   168                     const QString msg = QString::fromUtf8("%1: An invalid pixmap property '%2' was encountered.").arg(fileName).arg(p->attributeName());
       
   169                     qWarning("%s", qPrintable(msg));
       
   170                     return false;
       
   171                 }
       
   172             break;
       
   173         default:
       
   174             break;
       
   175         }
       
   176         return  true;
       
   177     }
       
   178 
       
   179     inline void openIfndef(QTextStream &str, const QString &symbol) { if (!symbol.isEmpty()) str << QLatin1String("#ifndef ") << symbol << endl;  }
       
   180     inline void closeIfndef(QTextStream &str, const QString &symbol) { if (!symbol.isEmpty()) str << QLatin1String("#endif // ") << symbol << endl; }
       
   181 
       
   182     const char *accessibilityDefineC = "QT_NO_ACCESSIBILITY";
       
   183     const char *toolTipDefineC = "QT_NO_TOOLTIP";
       
   184     const char *whatsThisDefineC = "QT_NO_WHATSTHIS";
       
   185     const char *statusTipDefineC = "QT_NO_STATUSTIP";
       
   186     const char *shortcutDefineC = "QT_NO_SHORTCUT";
       
   187 }
       
   188 
       
   189 namespace CPP {
       
   190 
       
   191 FontHandle::FontHandle(const DomFont *domFont) :
       
   192       m_domFont(domFont)
       
   193 {
       
   194 }
       
   195 
       
   196 int FontHandle::compare(const FontHandle &rhs) const
       
   197 {
       
   198     const QString family    = m_domFont->hasElementFamily()     ?     m_domFont->elementFamily() : QString();
       
   199     const QString rhsFamily = rhs.m_domFont->hasElementFamily() ? rhs.m_domFont->elementFamily() : QString();
       
   200 
       
   201     if (const int frc = family.compare(rhsFamily))
       
   202         return frc;
       
   203 
       
   204     const int pointSize    = m_domFont->hasElementPointSize()     ?     m_domFont->elementPointSize() : -1;
       
   205     const int rhsPointSize = rhs.m_domFont->hasElementPointSize() ? rhs.m_domFont->elementPointSize() : -1;
       
   206 
       
   207     if (const int crc = compareInt(pointSize, rhsPointSize))
       
   208         return crc;
       
   209 
       
   210     const int bold    = m_domFont->hasElementBold()     ? (m_domFont->elementBold()     ? 1 : 0) : -1;
       
   211     const int rhsBold = rhs.m_domFont->hasElementBold() ? (rhs.m_domFont->elementBold() ? 1 : 0) : -1;
       
   212     if (const int crc = compareInt(bold, rhsBold))
       
   213         return crc;
       
   214 
       
   215     const int italic    = m_domFont->hasElementItalic()     ? (m_domFont->elementItalic()     ? 1 : 0) : -1;
       
   216     const int rhsItalic = rhs.m_domFont->hasElementItalic() ? (rhs.m_domFont->elementItalic() ? 1 : 0) : -1;
       
   217     if (const int crc = compareInt(italic, rhsItalic))
       
   218         return crc;
       
   219 
       
   220     const int underline    = m_domFont->hasElementUnderline()     ? (m_domFont->elementUnderline()     ? 1 : 0) : -1;
       
   221     const int rhsUnderline = rhs.m_domFont->hasElementUnderline() ? (rhs.m_domFont->elementUnderline() ? 1 : 0) : -1;
       
   222     if (const int crc = compareInt(underline, rhsUnderline))
       
   223         return crc;
       
   224 
       
   225     const int weight    = m_domFont->hasElementWeight()     ?     m_domFont->elementWeight() : -1;
       
   226     const int rhsWeight = rhs.m_domFont->hasElementWeight() ? rhs.m_domFont->elementWeight() : -1;
       
   227     if (const int crc = compareInt(weight, rhsWeight))
       
   228         return crc;
       
   229 
       
   230     const int strikeOut    = m_domFont->hasElementStrikeOut()     ? (m_domFont->elementStrikeOut()     ? 1 : 0) : -1;
       
   231     const int rhsStrikeOut = rhs.m_domFont->hasElementStrikeOut() ? (rhs.m_domFont->elementStrikeOut() ? 1 : 0) : -1;
       
   232     if (const int crc = compareInt(strikeOut, rhsStrikeOut))
       
   233         return crc;
       
   234 
       
   235     const int kerning    = m_domFont->hasElementKerning()     ? (m_domFont->elementKerning()     ? 1 : 0) : -1;
       
   236     const int rhsKerning = rhs.m_domFont->hasElementKerning() ? (rhs.m_domFont->elementKerning() ? 1 : 0) : -1;
       
   237     if (const int crc = compareInt(kerning, rhsKerning))
       
   238         return crc;
       
   239 
       
   240     const int antialiasing    = m_domFont->hasElementAntialiasing()     ? (m_domFont->elementAntialiasing()     ? 1 : 0) : -1;
       
   241     const int rhsAntialiasing = rhs.m_domFont->hasElementAntialiasing() ? (rhs.m_domFont->elementAntialiasing() ? 1 : 0) : -1;
       
   242     if (const int crc = compareInt(antialiasing, rhsAntialiasing))
       
   243         return crc;
       
   244 
       
   245     const QString styleStrategy    = m_domFont->hasElementStyleStrategy()     ?     m_domFont->elementStyleStrategy() : QString();
       
   246     const QString rhsStyleStrategy = rhs.m_domFont->hasElementStyleStrategy() ? rhs.m_domFont->elementStyleStrategy() : QString();
       
   247 
       
   248     if (const int src = styleStrategy.compare(rhsStyleStrategy))
       
   249         return src;
       
   250 
       
   251     return 0;
       
   252 }
       
   253 
       
   254 IconHandle::IconHandle(const DomResourceIcon *domIcon) :
       
   255       m_domIcon(domIcon)
       
   256 {
       
   257 }
       
   258 
       
   259 int IconHandle::compare(const IconHandle &rhs) const
       
   260 {
       
   261     const QString normalOff    =     m_domIcon->hasElementNormalOff() ?     m_domIcon->elementNormalOff()->text() : QString();
       
   262     const QString rhsNormalOff = rhs.m_domIcon->hasElementNormalOff() ? rhs.m_domIcon->elementNormalOff()->text() : QString();
       
   263     if (const int comp = normalOff.compare(rhsNormalOff))
       
   264         return comp;
       
   265 
       
   266     const QString normalOn    =     m_domIcon->hasElementNormalOn() ?     m_domIcon->elementNormalOn()->text() : QString();
       
   267     const QString rhsNormalOn = rhs.m_domIcon->hasElementNormalOn() ? rhs.m_domIcon->elementNormalOn()->text() : QString();
       
   268     if (const int comp = normalOn.compare(rhsNormalOn))
       
   269         return comp;
       
   270 
       
   271     const QString disabledOff    =     m_domIcon->hasElementDisabledOff() ?     m_domIcon->elementDisabledOff()->text() : QString();
       
   272     const QString rhsDisabledOff = rhs.m_domIcon->hasElementDisabledOff() ? rhs.m_domIcon->elementDisabledOff()->text() : QString();
       
   273     if (const int comp = disabledOff.compare(rhsDisabledOff))
       
   274         return comp;
       
   275 
       
   276     const QString disabledOn    =     m_domIcon->hasElementDisabledOn() ?     m_domIcon->elementDisabledOn()->text() : QString();
       
   277     const QString rhsDisabledOn = rhs.m_domIcon->hasElementDisabledOn() ? rhs.m_domIcon->elementDisabledOn()->text() : QString();
       
   278     if (const int comp = disabledOn.compare(rhsDisabledOn))
       
   279         return comp;
       
   280 
       
   281     const QString activeOff    =     m_domIcon->hasElementActiveOff() ?     m_domIcon->elementActiveOff()->text() : QString();
       
   282     const QString rhsActiveOff = rhs.m_domIcon->hasElementActiveOff() ? rhs.m_domIcon->elementActiveOff()->text() : QString();
       
   283     if (const int comp = activeOff.compare(rhsActiveOff))
       
   284         return comp;
       
   285 
       
   286     const QString activeOn    =     m_domIcon->hasElementActiveOn() ?     m_domIcon->elementActiveOn()->text() : QString();
       
   287     const QString rhsActiveOn = rhs.m_domIcon->hasElementActiveOn() ? rhs.m_domIcon->elementActiveOn()->text() : QString();
       
   288     if (const int comp = activeOn.compare(rhsActiveOn))
       
   289         return comp;
       
   290 
       
   291     const QString selectedOff    =     m_domIcon->hasElementSelectedOff() ?     m_domIcon->elementSelectedOff()->text() : QString();
       
   292     const QString rhsSelectedOff = rhs.m_domIcon->hasElementSelectedOff() ? rhs.m_domIcon->elementSelectedOff()->text() : QString();
       
   293     if (const int comp = selectedOff.compare(rhsSelectedOff))
       
   294         return comp;
       
   295 
       
   296     const QString selectedOn    =     m_domIcon->hasElementSelectedOn() ?     m_domIcon->elementSelectedOn()->text() : QString();
       
   297     const QString rhsSelectedOn = rhs.m_domIcon->hasElementSelectedOn() ? rhs.m_domIcon->elementSelectedOn()->text() : QString();
       
   298     if (const int comp = selectedOn.compare(rhsSelectedOn))
       
   299         return comp;
       
   300     // Pre 4.4 Legacy
       
   301     if (const int comp = m_domIcon->text().compare(rhs.m_domIcon->text()))
       
   302         return comp;
       
   303 
       
   304     return 0;
       
   305 }
       
   306 
       
   307 
       
   308 #if defined(Q_OS_MAC) && defined(Q_CC_GNU) && (__GNUC__ == 3 && __GNUC_MINOR__ == 3)
       
   309 inline uint qHash(const SizePolicyHandle &handle) { return qHash(handle.m_domSizePolicy); }
       
   310 inline uint qHash(const FontHandle &handle) { return qHash(handle.m_domFont); }
       
   311 inline uint qHash(const IconHandle &handle) { return qHash(handle.m_domIcon); }
       
   312 #endif
       
   313 
       
   314 SizePolicyHandle::SizePolicyHandle(const DomSizePolicy *domSizePolicy) :
       
   315     m_domSizePolicy(domSizePolicy)
       
   316 {
       
   317 }
       
   318 
       
   319 int SizePolicyHandle::compare(const SizePolicyHandle &rhs) const
       
   320 {
       
   321 
       
   322     const int hSizeType    = m_domSizePolicy->hasElementHSizeType()     ? m_domSizePolicy->elementHSizeType()     : -1;
       
   323     const int rhsHSizeType = rhs.m_domSizePolicy->hasElementHSizeType() ? rhs.m_domSizePolicy->elementHSizeType() : -1;
       
   324     if (const int crc = compareInt(hSizeType, rhsHSizeType))
       
   325         return crc;
       
   326 
       
   327     const int vSizeType    = m_domSizePolicy->hasElementVSizeType()     ? m_domSizePolicy->elementVSizeType()     : -1;
       
   328     const int rhsVSizeType = rhs.m_domSizePolicy->hasElementVSizeType() ? rhs.m_domSizePolicy->elementVSizeType() : -1;
       
   329     if (const int crc = compareInt(vSizeType, rhsVSizeType))
       
   330         return crc;
       
   331 
       
   332     const int hStretch    =  m_domSizePolicy->hasElementHorStretch()     ? m_domSizePolicy->elementHorStretch()     : -1;
       
   333     const int rhsHStretch =  rhs.m_domSizePolicy->hasElementHorStretch() ? rhs.m_domSizePolicy->elementHorStretch() : -1;
       
   334     if (const int crc = compareInt(hStretch, rhsHStretch))
       
   335         return crc;
       
   336 
       
   337     const int vStretch    =  m_domSizePolicy->hasElementVerStretch()     ? m_domSizePolicy->elementVerStretch()     : -1;
       
   338     const int rhsVStretch =  rhs.m_domSizePolicy->hasElementVerStretch() ? rhs.m_domSizePolicy->elementVerStretch() : -1;
       
   339     if (const int crc = compareInt(vStretch, rhsVStretch))
       
   340         return crc;
       
   341 
       
   342     const QString attributeHSizeType    = m_domSizePolicy->hasAttributeHSizeType()     ? m_domSizePolicy->attributeHSizeType()     : QString();
       
   343     const QString rhsAttributeHSizeType = rhs.m_domSizePolicy->hasAttributeHSizeType() ? rhs.m_domSizePolicy->attributeHSizeType() : QString();
       
   344 
       
   345     if (const int hrc = attributeHSizeType.compare(rhsAttributeHSizeType))
       
   346         return hrc;
       
   347 
       
   348     const QString attributeVSizeType    = m_domSizePolicy->hasAttributeVSizeType()     ? m_domSizePolicy->attributeVSizeType()     : QString();
       
   349     const QString rhsAttributeVSizeType = rhs.m_domSizePolicy->hasAttributeVSizeType() ? rhs.m_domSizePolicy->attributeVSizeType() : QString();
       
   350 
       
   351     return attributeVSizeType.compare(rhsAttributeVSizeType);
       
   352 }
       
   353 
       
   354 // ---  WriteInitialization: LayoutDefaultHandler
       
   355 
       
   356 WriteInitialization::LayoutDefaultHandler::LayoutDefaultHandler()
       
   357 {
       
   358     qFill(m_state, m_state + NumProperties, 0u);
       
   359     qFill(m_defaultValues, m_defaultValues + NumProperties, 0);
       
   360 }
       
   361 
       
   362 
       
   363 
       
   364 void WriteInitialization::LayoutDefaultHandler::acceptLayoutDefault(DomLayoutDefault *node)
       
   365 {
       
   366     if (!node)
       
   367         return;
       
   368     if (node->hasAttributeMargin()) {
       
   369         m_state[Margin] |= HasDefaultValue;
       
   370         m_defaultValues[Margin] = node->attributeMargin();
       
   371     }
       
   372     if (node->hasAttributeSpacing()) {
       
   373         m_state[Spacing] |= HasDefaultValue;
       
   374         m_defaultValues[Spacing]  = node->attributeSpacing();
       
   375     }
       
   376 }
       
   377 
       
   378 void WriteInitialization::LayoutDefaultHandler::acceptLayoutFunction(DomLayoutFunction *node)
       
   379 {
       
   380     if (!node)
       
   381         return;
       
   382     if (node->hasAttributeMargin()) {
       
   383         m_state[Margin]     |= HasDefaultFunction;
       
   384         m_functions[Margin] =  node->attributeMargin();
       
   385         m_functions[Margin] += QLatin1String("()");
       
   386     }
       
   387     if (node->hasAttributeSpacing()) {
       
   388         m_state[Spacing]     |= HasDefaultFunction;
       
   389         m_functions[Spacing] =  node->attributeSpacing();
       
   390         m_functions[Spacing] += QLatin1String("()");
       
   391     }
       
   392 }
       
   393 
       
   394 static inline void writeContentsMargins(const QString &indent, const QString &objectName, int value, QTextStream &str)
       
   395 {
       
   396      QString contentsMargins;
       
   397      QTextStream(&contentsMargins) << value << ", " << value << ", " << value << ", " << value;
       
   398      writeSetter(indent, objectName, QLatin1String("setContentsMargins"), contentsMargins, str);
       
   399  }
       
   400 
       
   401 void WriteInitialization::LayoutDefaultHandler::writeProperty(int p, const QString &indent, const QString &objectName,
       
   402                                                               const DomPropertyMap &properties, const QString &propertyName, const QString &setter,
       
   403                                                               int defaultStyleValue, bool suppressDefault, QTextStream &str) const
       
   404 {
       
   405     // User value
       
   406     const DomPropertyMap::const_iterator mit = properties.constFind(propertyName);
       
   407     const bool found = mit != properties.constEnd();
       
   408     if (found) {
       
   409         const int value = mit.value()->elementNumber();
       
   410         // Emulate the pre 4.3 behaviour: The value form default value was only used to determine
       
   411         // the default value, layout properties were always written
       
   412         const bool useLayoutFunctionPre43 = !suppressDefault && (m_state[p] == (HasDefaultFunction|HasDefaultValue)) && value == m_defaultValues[p];
       
   413         if (!useLayoutFunctionPre43) {
       
   414             bool ifndefMac = (!(m_state[p] & (HasDefaultFunction|HasDefaultValue))
       
   415                              && value == defaultStyleValue);
       
   416             if (ifndefMac)
       
   417                 str << "#ifndef Q_OS_MAC\n";
       
   418             if (p == Margin) { // Use setContentsMargins for numeric values
       
   419                 writeContentsMargins(indent, objectName, value, str);
       
   420             } else {
       
   421                 writeSetter(indent, objectName, setter, value, str);
       
   422             }
       
   423             if (ifndefMac)
       
   424                 str << "#endif\n";
       
   425             return;
       
   426         }
       
   427     }
       
   428     if (suppressDefault)
       
   429         return;
       
   430     // get default.
       
   431     if (m_state[p] & HasDefaultFunction) {
       
   432         // Do not use setContentsMargins to avoid repetitive evaluations.
       
   433         writeSetter(indent, objectName, setter, m_functions[p], str);
       
   434         return;
       
   435     }
       
   436     if (m_state[p] & HasDefaultValue) {
       
   437         if (p == Margin) { // Use setContentsMargins for numeric values
       
   438             writeContentsMargins(indent, objectName, m_defaultValues[p], str);
       
   439         } else {
       
   440             writeSetter(indent, objectName, setter, m_defaultValues[p], str);
       
   441         }
       
   442     }
       
   443     return;
       
   444 }
       
   445 
       
   446 
       
   447 void WriteInitialization::LayoutDefaultHandler::writeProperties(const QString &indent, const QString &varName,
       
   448                                                                 const DomPropertyMap &properties, int marginType,
       
   449                                                                 bool suppressMarginDefault,
       
   450                                                                 QTextStream &str) const {
       
   451     // Write out properties and ignore the ones found in
       
   452     // subsequent writing of the property list.
       
   453     int defaultSpacing = marginType == WriteInitialization::Use43UiFile ? -1 : 6;
       
   454     writeProperty(Spacing, indent, varName, properties, QLatin1String("spacing"), QLatin1String("setSpacing"),
       
   455                   defaultSpacing, false, str);
       
   456     // We use 9 as TopLevelMargin, since Designer seem to always use 9.
       
   457     static const int layoutmargins[4] = {-1, 9, 9, 0};
       
   458     writeProperty(Margin,  indent, varName, properties, QLatin1String("margin"),  QLatin1String("setMargin"),
       
   459                   layoutmargins[marginType], suppressMarginDefault, str);
       
   460 }
       
   461 
       
   462 static bool needsTranslation(DomString *str)
       
   463 {
       
   464     if (!str)
       
   465         return false;
       
   466     return !str->hasAttributeNotr() || !toBool(str->attributeNotr());
       
   467 }
       
   468 
       
   469 // ---  WriteInitialization
       
   470 WriteInitialization::WriteInitialization(Uic *uic, bool activateScripts) :
       
   471       m_uic(uic),
       
   472       m_driver(uic->driver()), m_output(uic->output()), m_option(uic->option()),
       
   473       m_indent(m_option.indent + m_option.indent),
       
   474       m_dindent(m_indent + m_option.indent),
       
   475       m_stdsetdef(true),
       
   476       m_layoutMarginType(TopLevelMargin),
       
   477       m_mainFormUsedInRetranslateUi(false),
       
   478       m_delayedOut(&m_delayedInitialization, QIODevice::WriteOnly),
       
   479       m_refreshOut(&m_refreshInitialization, QIODevice::WriteOnly),
       
   480       m_actionOut(&m_delayedActionInitialization, QIODevice::WriteOnly),
       
   481       m_activateScripts(activateScripts), m_layoutWidget(false)
       
   482 {
       
   483 }
       
   484 
       
   485 void WriteInitialization::acceptUI(DomUI *node)
       
   486 {
       
   487     m_registeredImages.clear();
       
   488     m_actionGroupChain.push(0);
       
   489     m_widgetChain.push(0);
       
   490     m_layoutChain.push(0);
       
   491 
       
   492     acceptLayoutDefault(node->elementLayoutDefault());
       
   493     acceptLayoutFunction(node->elementLayoutFunction());
       
   494 
       
   495     if (node->elementCustomWidgets())
       
   496         TreeWalker::acceptCustomWidgets(node->elementCustomWidgets());
       
   497 
       
   498     if (node->elementImages())
       
   499         TreeWalker::acceptImages(node->elementImages());
       
   500 
       
   501     if (m_option.generateImplemetation)
       
   502         m_output << "#include <" << m_driver->headerFileName() << ">\n\n";
       
   503 
       
   504     m_stdsetdef = true;
       
   505     if (node->hasAttributeStdSetDef())
       
   506         m_stdsetdef = node->attributeStdSetDef();
       
   507 
       
   508     const QString className = node->elementClass() + m_option.postfix;
       
   509     m_generatedClass = className;
       
   510 
       
   511     const QString varName = m_driver->findOrInsertWidget(node->elementWidget());
       
   512     m_mainFormVarName = varName;
       
   513     m_registeredWidgets.insert(varName, node->elementWidget()); // register the main widget
       
   514 
       
   515     const QString widgetClassName = node->elementWidget()->attributeClass();
       
   516 
       
   517     m_output << m_option.indent << "void " << "setupUi(" << widgetClassName << " *" << varName << ")\n"
       
   518            << m_option.indent << "{\n";
       
   519 
       
   520     if (m_activateScripts)
       
   521         writeSetupUIScriptVariableDeclarations(m_indent, m_output);
       
   522 
       
   523     const QStringList connections = m_uic->databaseInfo()->connections();
       
   524     for (int i=0; i<connections.size(); ++i) {
       
   525         QString connection = connections.at(i);
       
   526 
       
   527         if (connection == QLatin1String("(default)"))
       
   528             continue;
       
   529 
       
   530         const QString varConn = connection + QLatin1String("Connection");
       
   531         m_output << m_indent << varConn << " = QSqlDatabase::database(" << fixString(connection, m_dindent) << ");\n";
       
   532     }
       
   533 
       
   534     acceptWidget(node->elementWidget());
       
   535 
       
   536     if (m_buddies.size() > 0)
       
   537         openIfndef(m_output, QLatin1String(shortcutDefineC));
       
   538     for (int i=0; i<m_buddies.size(); ++i) {
       
   539         const Buddy &b = m_buddies.at(i);
       
   540 
       
   541         if (!m_registeredWidgets.contains(b.objName)) {
       
   542             fprintf(stderr, "'%s' isn't a valid widget\n", b.objName.toLatin1().data());
       
   543             continue;
       
   544         } else if (!m_registeredWidgets.contains(b.buddy)) {
       
   545             fprintf(stderr, "'%s' isn't a valid widget\n", b.buddy.toLatin1().data());
       
   546             continue;
       
   547         }
       
   548 
       
   549         m_output << m_indent << b.objName << "->setBuddy(" << b.buddy << ");\n";
       
   550     }
       
   551     if (m_buddies.size() > 0)
       
   552         closeIfndef(m_output, QLatin1String(shortcutDefineC));
       
   553 
       
   554     if (node->elementTabStops())
       
   555         acceptTabStops(node->elementTabStops());
       
   556 
       
   557     if (m_delayedActionInitialization.size())
       
   558         m_output << "\n" << m_delayedActionInitialization;
       
   559 
       
   560     m_output << "\n" << m_indent << "retranslateUi(" << varName << ");\n";
       
   561 
       
   562     if (node->elementConnections())
       
   563         acceptConnections(node->elementConnections());
       
   564 
       
   565     if (!m_delayedInitialization.isEmpty())
       
   566         m_output << "\n" << m_delayedInitialization << "\n";
       
   567 
       
   568     if (m_option.autoConnection)
       
   569         m_output << "\n" << m_indent << "QMetaObject::connectSlotsByName(" << varName << ");\n";
       
   570 
       
   571     m_output << m_option.indent << "} // setupUi\n\n";
       
   572 
       
   573     if (!m_mainFormUsedInRetranslateUi) {
       
   574         m_refreshInitialization += m_indent;
       
   575         m_refreshInitialization += QLatin1String("Q_UNUSED(");
       
   576         m_refreshInitialization += varName ;
       
   577         m_refreshInitialization += QLatin1String(");\n");
       
   578     }
       
   579 
       
   580     m_output << m_option.indent << "void " << "retranslateUi(" << widgetClassName << " *" << varName << ")\n"
       
   581            << m_option.indent << "{\n"
       
   582            << m_refreshInitialization
       
   583            << m_option.indent << "} // retranslateUi\n\n";
       
   584 
       
   585     m_layoutChain.pop();
       
   586     m_widgetChain.pop();
       
   587     m_actionGroupChain.pop();
       
   588 }
       
   589 
       
   590 void WriteInitialization::addWizardPage(const QString &pageVarName, const DomWidget *page, const QString &parentWidget)
       
   591 {
       
   592     /* If the node has a (free-format) string "pageId" attribute (which could
       
   593      * an integer or an enumeration value), use setPage(), else addPage(). */
       
   594     QString id;
       
   595     const DomPropertyList attributes = page->elementAttribute();
       
   596     if (!attributes.empty()) {
       
   597         const DomPropertyList::const_iterator acend = attributes.constEnd();
       
   598         for (DomPropertyList::const_iterator it = attributes.constBegin(); it != acend; ++it)
       
   599             if ((*it)->attributeName() == QLatin1String("pageId")) {
       
   600                 if (const DomString *ds = (*it)->elementString())
       
   601                     id = ds->text();
       
   602                 break;
       
   603             }
       
   604     }
       
   605     if (id.isEmpty()) {
       
   606         m_output << m_indent << parentWidget << "->addPage(" << pageVarName << ");\n";
       
   607     } else {
       
   608         m_output << m_indent << parentWidget << "->setPage(" << id << ", " << pageVarName << ");\n";
       
   609     }
       
   610 }
       
   611 
       
   612 void WriteInitialization::acceptWidget(DomWidget *node)
       
   613 {
       
   614     m_layoutMarginType = m_widgetChain.count() == 1 ? TopLevelMargin : ChildMargin;
       
   615     const QString className = node->attributeClass();
       
   616     const QString varName = m_driver->findOrInsertWidget(node);
       
   617     m_registeredWidgets.insert(varName, node); // register the current widget
       
   618 
       
   619     QString parentWidget, parentClass;
       
   620     if (m_widgetChain.top()) {
       
   621         parentWidget = m_driver->findOrInsertWidget(m_widgetChain.top());
       
   622         parentClass = m_widgetChain.top()->attributeClass();
       
   623     }
       
   624 
       
   625     const QString savedParentWidget = parentWidget;
       
   626 
       
   627     if (m_uic->isContainer(parentClass) || m_uic->customWidgetsInfo()->extends(parentClass, QLatin1String("Q3ToolBar")))
       
   628         parentWidget.clear();
       
   629 
       
   630     if (m_widgetChain.size() != 1)
       
   631         m_output << m_indent << varName << " = new " << m_uic->customWidgetsInfo()->realClassName(className) << '(' << parentWidget << ");\n";
       
   632 
       
   633     parentWidget = savedParentWidget;
       
   634 
       
   635     if (m_uic->customWidgetsInfo()->extends(className, QLatin1String("Q3ComboBox"))) {
       
   636         initializeComboBox3(node);
       
   637     } else if (m_uic->customWidgetsInfo()->extends(className, QLatin1String("QComboBox"))) {
       
   638         initializeComboBox(node);
       
   639     } else if (m_uic->customWidgetsInfo()->extends(className, QLatin1String("QListWidget"))) {
       
   640         initializeListWidget(node);
       
   641     } else if (m_uic->customWidgetsInfo()->extends(className, QLatin1String("QTreeWidget"))) {
       
   642         initializeTreeWidget(node);
       
   643     } else if (m_uic->customWidgetsInfo()->extends(className, QLatin1String("QTableWidget"))) {
       
   644         initializeTableWidget(node);
       
   645     } else if (m_uic->customWidgetsInfo()->extends(className, QLatin1String("Q3ListBox"))) {
       
   646         initializeQ3ListBox(node);
       
   647     } else if (m_uic->customWidgetsInfo()->extends(className, QLatin1String("Q3ListView"))) {
       
   648         initializeQ3ListView(node);
       
   649     } else if (m_uic->customWidgetsInfo()->extends(className, QLatin1String("Q3IconView"))) {
       
   650         initializeQ3IconView(node);
       
   651     } else if (m_uic->customWidgetsInfo()->extends(className, QLatin1String("Q3Table"))) {
       
   652         initializeQ3Table(node);
       
   653     } else if (m_uic->customWidgetsInfo()->extends(className, QLatin1String("Q3DataTable"))) {
       
   654         initializeQ3SqlDataTable(node);
       
   655     } else if (m_uic->customWidgetsInfo()->extends(className, QLatin1String("Q3DataBrowser"))) {
       
   656         initializeQ3SqlDataBrowser(node);
       
   657     }
       
   658 
       
   659     if (m_uic->isButton(className))
       
   660         addButtonGroup(node, varName);
       
   661 
       
   662     writeProperties(varName, className, node->elementProperty());
       
   663 
       
   664     if (m_uic->customWidgetsInfo()->extends(className, QLatin1String("QMenu")) && parentWidget.size()) {
       
   665         initializeMenu(node, parentWidget);
       
   666     }
       
   667 
       
   668     if (node->elementLayout().isEmpty())
       
   669         m_layoutChain.push(0);
       
   670 
       
   671     m_layoutWidget = false;
       
   672     if (className == QLatin1String("QWidget") && !node->hasAttributeNative()) {
       
   673         if (m_widgetChain.top()
       
   674             && m_widgetChain.top()->attributeClass() != QLatin1String("QMainWindow")
       
   675             && !m_uic->isContainer(m_widgetChain.top()->attributeClass()))
       
   676             m_layoutWidget = true;
       
   677     }
       
   678     m_widgetChain.push(node);
       
   679     m_layoutChain.push(0);
       
   680     TreeWalker::acceptWidget(node);
       
   681     m_layoutChain.pop();
       
   682     m_widgetChain.pop();
       
   683     m_layoutWidget = false;
       
   684 
       
   685     const DomPropertyMap attributes = propertyMap(node->elementAttribute());
       
   686 
       
   687     const QString pageDefaultString = QLatin1String("Page");
       
   688 
       
   689     int id = -1;
       
   690     if (const DomProperty *pid = attributes.value(QLatin1String("id"))) {
       
   691         id = pid->elementNumber();
       
   692     }
       
   693 
       
   694     if (m_uic->customWidgetsInfo()->extends(parentClass, QLatin1String("QMainWindow"))
       
   695             || m_uic->customWidgetsInfo()->extends(parentClass, QLatin1String("Q3MainWindow"))) {
       
   696 
       
   697         if (m_uic->customWidgetsInfo()->extends(className, QLatin1String("QMenuBar"))) {
       
   698             if (!m_uic->customWidgetsInfo()->extends(parentClass, QLatin1String("Q3MainWindow")))
       
   699                 m_output << m_indent << parentWidget << "->setMenuBar(" << varName <<");\n";
       
   700         } else if (m_uic->customWidgetsInfo()->extends(className, QLatin1String("QToolBar"))) {
       
   701             m_output << m_indent << parentWidget << "->addToolBar("
       
   702                      << toolBarAreaStringFromDOMAttributes(attributes) << varName << ");\n";
       
   703 
       
   704             if (const DomProperty *pbreak = attributes.value(QLatin1String("toolBarBreak"))) {
       
   705                 if (pbreak->elementBool() == QLatin1String("true")) {
       
   706                     m_output << m_indent << parentWidget << "->insertToolBarBreak(" <<  varName << ");\n";
       
   707                 }
       
   708             }
       
   709 
       
   710         } else if (m_uic->customWidgetsInfo()->extends(className, QLatin1String("QDockWidget"))) {
       
   711             QString area;
       
   712             if (DomProperty *pstyle = attributes.value(QLatin1String("dockWidgetArea"))) {
       
   713                 area += QLatin1String("static_cast<Qt::DockWidgetArea>(");
       
   714                 area += QString::number(pstyle->elementNumber());
       
   715                 area += QLatin1String("), ");
       
   716             }
       
   717 
       
   718             m_output << m_indent << parentWidget << "->addDockWidget(" << area << varName << ");\n";
       
   719         } else if (m_uic->customWidgetsInfo()->extends(className, QLatin1String("QStatusBar"))) {
       
   720             m_output << m_indent << parentWidget << "->setStatusBar(" << varName << ");\n";
       
   721         } else if (className == QLatin1String("QWidget")) {
       
   722             m_output << m_indent << parentWidget << "->setCentralWidget(" << varName << ");\n";
       
   723         }
       
   724     }
       
   725 
       
   726     // Check for addPageMethod of a custom plugin first
       
   727     const QString addPageMethod = m_uic->customWidgetsInfo()->customWidgetAddPageMethod(parentClass);
       
   728     if (!addPageMethod.isEmpty()) {
       
   729         m_output << m_indent << parentWidget << "->" << addPageMethod << '(' << varName << ");\n";
       
   730     } else if (m_uic->customWidgetsInfo()->extends(parentClass, QLatin1String("QStackedWidget"))) {
       
   731         m_output << m_indent << parentWidget << "->addWidget(" << varName << ");\n";
       
   732     } else if (m_uic->customWidgetsInfo()->extends(parentClass, QLatin1String("QToolBar"))) {
       
   733         m_output << m_indent << parentWidget << "->addWidget(" << varName << ");\n";
       
   734     } else if (m_uic->customWidgetsInfo()->extends(parentClass, QLatin1String("Q3WidgetStack"))) {
       
   735         m_output << m_indent << parentWidget << "->addWidget(" << varName << ", " << id << ");\n";
       
   736     } else if (m_uic->customWidgetsInfo()->extends(parentClass, QLatin1String("QDockWidget"))) {
       
   737         m_output << m_indent << parentWidget << "->setWidget(" << varName << ");\n";
       
   738     } else if (m_uic->customWidgetsInfo()->extends(parentClass, QLatin1String("QScrollArea"))) {
       
   739         m_output << m_indent << parentWidget << "->setWidget(" << varName << ");\n";
       
   740     } else if (m_uic->customWidgetsInfo()->extends(parentClass, QLatin1String("QSplitter"))) {
       
   741         m_output << m_indent << parentWidget << "->addWidget(" << varName << ");\n";
       
   742     } else if (m_uic->customWidgetsInfo()->extends(parentClass, QLatin1String("QMdiArea"))) {
       
   743         m_output << m_indent << parentWidget << "->addSubWindow(" << varName << ");\n";
       
   744     } else if (m_uic->customWidgetsInfo()->extends(parentClass, QLatin1String("QWorkspace"))) {
       
   745         m_output << m_indent << parentWidget << "->addWindow(" << varName << ");\n";
       
   746     } else if (m_uic->customWidgetsInfo()->extends(parentClass, QLatin1String("QWizard"))) {
       
   747         addWizardPage(varName, node, parentWidget);
       
   748     } else if (m_uic->customWidgetsInfo()->extends(parentClass, QLatin1String("QToolBox"))) {
       
   749         QString icon;
       
   750         if (const DomProperty *picon = attributes.value(QLatin1String("icon"))) {
       
   751             icon += QLatin1String(", ") ;
       
   752             icon += iconCall(picon);
       
   753         }
       
   754 
       
   755         const DomProperty *plabel = attributes.value(QLatin1String("label"));
       
   756         DomString *plabelString = plabel ? plabel->elementString() : 0;
       
   757 
       
   758         m_output << m_indent << parentWidget << "->addItem(" << varName << icon << ", " << noTrCall(plabelString, pageDefaultString) << ");\n";
       
   759 
       
   760         autoTrOutput(plabelString, pageDefaultString) << m_indent << parentWidget << "->setItemText("
       
   761                    << parentWidget << "->indexOf(" << varName << "), " << autoTrCall(plabelString, pageDefaultString) << ");\n";
       
   762 
       
   763 #ifndef QT_NO_TOOLTIP
       
   764         if (DomProperty *ptoolTip = attributes.value(QLatin1String("toolTip"))) {
       
   765             autoTrOutput(ptoolTip->elementString()) << m_indent << parentWidget << "->setItemToolTip("
       
   766                        << parentWidget << "->indexOf(" << varName << "), " << autoTrCall(ptoolTip->elementString()) << ");\n";
       
   767         }
       
   768 #endif // QT_NO_TOOLTIP
       
   769     } else if (m_uic->customWidgetsInfo()->extends(parentClass, QLatin1String("QTabWidget"))) {
       
   770         QString icon;
       
   771         if (const DomProperty *picon = attributes.value(QLatin1String("icon"))) {
       
   772             icon += QLatin1String(", ");
       
   773             icon += iconCall(picon);
       
   774         }
       
   775 
       
   776         const DomProperty *ptitle = attributes.value(QLatin1String("title"));
       
   777         DomString *ptitleString = ptitle ? ptitle->elementString() : 0;
       
   778 
       
   779         m_output << m_indent << parentWidget << "->addTab(" << varName << icon << ", " << "QString());\n";
       
   780 
       
   781         autoTrOutput(ptitleString, pageDefaultString) << m_indent << parentWidget << "->setTabText("
       
   782                    << parentWidget << "->indexOf(" << varName << "), " << autoTrCall(ptitleString, pageDefaultString) << ");\n";
       
   783 
       
   784 #ifndef QT_NO_TOOLTIP
       
   785         if (const DomProperty *ptoolTip = attributes.value(QLatin1String("toolTip"))) {
       
   786             autoTrOutput(ptoolTip->elementString()) << m_indent << parentWidget << "->setTabToolTip("
       
   787                        << parentWidget << "->indexOf(" << varName << "), " << autoTrCall(ptoolTip->elementString()) << ");\n";
       
   788         }
       
   789 #endif // QT_NO_TOOLTIP
       
   790 #ifndef QT_NO_WHATSTHIS
       
   791         if (const DomProperty *pwhatsThis = attributes.value(QLatin1String("whatsThis"))) {
       
   792             autoTrOutput(pwhatsThis->elementString()) << m_indent << parentWidget << "->setTabWhatsThis("
       
   793                        << parentWidget << "->indexOf(" << varName << "), " << autoTrCall(pwhatsThis->elementString()) << ");\n";
       
   794         }
       
   795 #endif // QT_NO_WHATSTHIS
       
   796     } else if (m_uic->customWidgetsInfo()->extends(parentClass, QLatin1String("Q3Wizard"))) {
       
   797         const DomProperty *ptitle = attributes.value(QLatin1String("title"));
       
   798         DomString *ptitleString = ptitle ? ptitle->elementString() : 0;
       
   799 
       
   800         m_output << m_indent << parentWidget << "->addPage(" << varName << ", " << noTrCall(ptitleString, pageDefaultString) << ");\n";
       
   801 
       
   802         autoTrOutput(ptitleString, pageDefaultString) << m_indent << parentWidget << "->setTitle("
       
   803                    << varName << ", " << autoTrCall(ptitleString, pageDefaultString) << ");\n";
       
   804 
       
   805     }
       
   806 
       
   807     //
       
   808     // Special handling for qtableview/qtreeview fake header attributes
       
   809     //
       
   810     static QStringList realPropertyNames =
       
   811             (QStringList() << QLatin1String("visible")
       
   812                            << QLatin1String("cascadingSectionResizes")
       
   813                            << QLatin1String("defaultSectionSize")
       
   814                            << QLatin1String("highlightSections")
       
   815                            << QLatin1String("minimumSectionSize")
       
   816                            << QLatin1String("showSortIndicator")
       
   817                            << QLatin1String("stretchLastSection"));
       
   818 
       
   819     if (m_uic->customWidgetsInfo()->extends(className, QLatin1String("QTreeView"))
       
   820                || m_uic->customWidgetsInfo()->extends(className, QLatin1String("QTreeWidget"))) {
       
   821         DomPropertyList headerProperties;
       
   822         foreach (const QString &realPropertyName, realPropertyNames) {
       
   823             const QString upperPropertyName = realPropertyName.at(0).toUpper()
       
   824                                               + realPropertyName.mid(1);
       
   825             const QString fakePropertyName = QLatin1String("header") + upperPropertyName;
       
   826             if (DomProperty *fakeProperty = attributes.value(fakePropertyName)) {
       
   827                 fakeProperty->setAttributeName(realPropertyName);
       
   828                 headerProperties << fakeProperty;
       
   829             }
       
   830         }
       
   831         writeProperties(varName + QLatin1String("->header()"), QLatin1String("QHeaderView"),
       
   832                         headerProperties, WritePropertyIgnoreObjectName);
       
   833 
       
   834     } else if (m_uic->customWidgetsInfo()->extends(className, QLatin1String("QTableView"))
       
   835                || m_uic->customWidgetsInfo()->extends(className, QLatin1String("QTableWidget"))) {
       
   836 
       
   837         static QStringList headerPrefixes =
       
   838                 (QStringList() << QLatin1String("horizontalHeader")
       
   839                                << QLatin1String("verticalHeader"));
       
   840 
       
   841         foreach (const QString &headerPrefix, headerPrefixes) {
       
   842             DomPropertyList headerProperties;
       
   843             foreach (const QString &realPropertyName, realPropertyNames) {
       
   844                 const QString upperPropertyName = realPropertyName.at(0).toUpper()
       
   845                                                   + realPropertyName.mid(1);
       
   846                 const QString fakePropertyName = headerPrefix + upperPropertyName;
       
   847                 if (DomProperty *fakeProperty = attributes.value(fakePropertyName)) {
       
   848                     fakeProperty->setAttributeName(realPropertyName);
       
   849                     headerProperties << fakeProperty;
       
   850                 }
       
   851             }
       
   852             writeProperties(varName + QLatin1String("->") + headerPrefix + QLatin1String("()"),
       
   853                             QLatin1String("QHeaderView"),
       
   854                             headerProperties, WritePropertyIgnoreObjectName);
       
   855         }
       
   856     }
       
   857 
       
   858     if (node->elementLayout().isEmpty())
       
   859         m_layoutChain.pop();
       
   860 
       
   861     const QStringList zOrder = node->elementZOrder();
       
   862     for (int i = 0; i < zOrder.size(); ++i) {
       
   863         const QString name = zOrder.at(i);
       
   864 
       
   865         if (!m_registeredWidgets.contains(name)) {
       
   866             fprintf(stderr, "'%s' isn't a valid widget\n", name.toLatin1().data());
       
   867             continue;
       
   868         }
       
   869 
       
   870         if (name.isEmpty()) {
       
   871             continue;
       
   872         }
       
   873 
       
   874         m_output << m_indent << name << "->raise();\n";
       
   875     }
       
   876 }
       
   877 
       
   878 void WriteInitialization::addButtonGroup(const DomWidget *buttonNode, const QString &varName)
       
   879 {
       
   880     const DomPropertyMap attributes = propertyMap(buttonNode->elementAttribute());
       
   881     // Look up the button group name as specified in the attribute and find the uniquified name
       
   882     const DomProperty *prop = attributes.value(QLatin1String("buttonGroup"));
       
   883     if (!prop)
       
   884         return;
       
   885     const QString attributeName = toString(prop->elementString());
       
   886     const DomButtonGroup *group = m_driver->findButtonGroup(attributeName);
       
   887     // Legacy feature: Create missing groups on the fly as the UIC button group feature
       
   888     // was present before the actual Designer support (4.5)
       
   889     const bool createGroupOnTheFly = group == 0;
       
   890     if (createGroupOnTheFly) {
       
   891         DomButtonGroup *newGroup = new DomButtonGroup;
       
   892         newGroup->setAttributeName(attributeName);
       
   893         group = newGroup;
       
   894         fprintf(stderr, "Warning: Creating button group `%s'\n", attributeName.toLatin1().data());
       
   895     }
       
   896     const QString groupName = m_driver->findOrInsertButtonGroup(group);
       
   897     // Create on demand
       
   898     if (!m_buttonGroups.contains(groupName)) {
       
   899         const QString className = QLatin1String("QButtonGroup");
       
   900         m_output << m_indent;
       
   901         if (createGroupOnTheFly)
       
   902             m_output << className << " *";
       
   903         m_output << groupName << " = new " << className << '(' << m_mainFormVarName << ");\n";
       
   904         m_buttonGroups.insert(groupName);
       
   905         writeProperties(groupName, className, group->elementProperty());
       
   906     }
       
   907     m_output << m_indent << groupName << "->addButton(" << varName << ");\n";
       
   908 }
       
   909 
       
   910 void WriteInitialization::acceptLayout(DomLayout *node)
       
   911 {
       
   912     const QString className = node->attributeClass();
       
   913     const QString varName = m_driver->findOrInsertLayout(node);
       
   914 
       
   915     const DomPropertyMap properties = propertyMap(node->elementProperty());
       
   916     const bool oldLayoutProperties = properties.constFind(QLatin1String("margin")) != properties.constEnd();
       
   917 
       
   918     bool isGroupBox = false;
       
   919 
       
   920     if (m_widgetChain.top()) {
       
   921         const QString parentWidget = m_widgetChain.top()->attributeClass();
       
   922 
       
   923         if (!m_layoutChain.top() && (m_uic->customWidgetsInfo()->extends(parentWidget, QLatin1String("Q3GroupBox"))
       
   924                         || m_uic->customWidgetsInfo()->extends(parentWidget, QLatin1String("Q3ButtonGroup")))) {
       
   925             const QString parent = m_driver->findOrInsertWidget(m_widgetChain.top());
       
   926 
       
   927             isGroupBox = true;
       
   928             // special case for group box
       
   929 
       
   930             m_output << m_indent << parent << "->setColumnLayout(0, Qt::Vertical);\n";
       
   931             QString objectName = parent;
       
   932             objectName += QLatin1String("->layout()");
       
   933             int marginType = Use43UiFile;
       
   934             if (oldLayoutProperties)
       
   935                 marginType = m_layoutMarginType;
       
   936 
       
   937             m_LayoutDefaultHandler.writeProperties(m_indent,
       
   938                                     objectName, properties, marginType, false, m_output);
       
   939         }
       
   940     }
       
   941 
       
   942     m_output << m_indent << varName << " = new " << className << '(';
       
   943 
       
   944     if (!m_layoutChain.top() && !isGroupBox)
       
   945         m_output << m_driver->findOrInsertWidget(m_widgetChain.top());
       
   946 
       
   947     m_output << ");\n";
       
   948 
       
   949     if (isGroupBox) {
       
   950         const QString tempName = m_driver->unique(QLatin1String("boxlayout"));
       
   951         m_output << m_indent << "QBoxLayout *" << tempName << " = qobject_cast<QBoxLayout *>(" <<
       
   952                     m_driver->findOrInsertWidget(m_widgetChain.top()) << "->layout());\n";
       
   953         m_output << m_indent << "if (" << tempName << ")\n";
       
   954         m_output << m_dindent << tempName << "->addLayout(" << varName << ");\n";
       
   955     }
       
   956 
       
   957     if (isGroupBox) {
       
   958         m_output << m_indent << varName << "->setAlignment(Qt::AlignTop);\n";
       
   959     }  else {
       
   960         // Suppress margin on a read child layout
       
   961         const bool suppressMarginDefault = m_layoutChain.top();
       
   962         int marginType = Use43UiFile;
       
   963         if (oldLayoutProperties)
       
   964             marginType = m_layoutMarginType;
       
   965         m_LayoutDefaultHandler.writeProperties(m_indent, varName, properties, marginType, suppressMarginDefault, m_output);
       
   966     }
       
   967 
       
   968     m_layoutMarginType = SubLayoutMargin;
       
   969 
       
   970     DomPropertyList propList = node->elementProperty();
       
   971     if (m_layoutWidget) {
       
   972         bool left, top, right, bottom;
       
   973         left = top = right = bottom = false;
       
   974         for (int i = 0; i < propList.size(); ++i) {
       
   975             const DomProperty *p = propList.at(i);
       
   976             const QString propertyName = p->attributeName();
       
   977             if (propertyName == QLatin1String("leftMargin") && p->kind() == DomProperty::Number)
       
   978                 left = true;
       
   979             else if (propertyName == QLatin1String("topMargin") && p->kind() == DomProperty::Number)
       
   980                 top = true;
       
   981             else if (propertyName == QLatin1String("rightMargin") && p->kind() == DomProperty::Number)
       
   982                 right = true;
       
   983             else if (propertyName == QLatin1String("bottomMargin") && p->kind() == DomProperty::Number)
       
   984                 bottom = true;
       
   985         }
       
   986         if (!left) {
       
   987             DomProperty *p = new DomProperty();
       
   988             p->setAttributeName(QLatin1String("leftMargin"));
       
   989             p->setElementNumber(0);
       
   990             propList.append(p);
       
   991         }
       
   992         if (!top) {
       
   993             DomProperty *p = new DomProperty();
       
   994             p->setAttributeName(QLatin1String("topMargin"));
       
   995             p->setElementNumber(0);
       
   996             propList.append(p);
       
   997         }
       
   998         if (!right) {
       
   999             DomProperty *p = new DomProperty();
       
  1000             p->setAttributeName(QLatin1String("rightMargin"));
       
  1001             p->setElementNumber(0);
       
  1002             propList.append(p);
       
  1003         }
       
  1004         if (!bottom) {
       
  1005             DomProperty *p = new DomProperty();
       
  1006             p->setAttributeName(QLatin1String("bottomMargin"));
       
  1007             p->setElementNumber(0);
       
  1008             propList.append(p);
       
  1009         }
       
  1010         m_layoutWidget = false;
       
  1011     }
       
  1012 
       
  1013     writeProperties(varName, className, propList, WritePropertyIgnoreMargin|WritePropertyIgnoreSpacing);
       
  1014 
       
  1015     m_layoutChain.push(node);
       
  1016     TreeWalker::acceptLayout(node);
       
  1017     m_layoutChain.pop();
       
  1018 
       
  1019     // Stretch? (Unless we are compiling for UIC3)
       
  1020     const QString numberNull = QString(QLatin1Char('0'));
       
  1021     writePropertyList(varName, QLatin1String("setStretch"), node->attributeStretch(), numberNull);
       
  1022     writePropertyList(varName, QLatin1String("setRowStretch"), node->attributeRowStretch(), numberNull);
       
  1023     writePropertyList(varName, QLatin1String("setColumnStretch"), node->attributeColumnStretch(), numberNull);
       
  1024     writePropertyList(varName, QLatin1String("setColumnMinimumWidth"), node->attributeColumnMinimumWidth(), numberNull);
       
  1025     writePropertyList(varName, QLatin1String("setRowMinimumHeight"), node->attributeRowMinimumHeight(), numberNull);
       
  1026 }
       
  1027 
       
  1028 // Apply a comma-separated list of values using a function "setSomething(int idx, value)"
       
  1029 void WriteInitialization::writePropertyList(const QString &varName,
       
  1030                                             const QString &setFunction,
       
  1031                                             const QString &value,
       
  1032                                             const QString &defaultValue)
       
  1033 {
       
  1034     if (value.isEmpty())
       
  1035         return;
       
  1036     const QStringList list = value.split(QLatin1Char(','));
       
  1037     const int count =  list.count();
       
  1038     for (int i = 0; i < count; i++)
       
  1039         if (list.at(i) != defaultValue)
       
  1040             m_output << m_indent << varName << "->" << setFunction << '(' << i << ", " << list.at(i) << ");\n";
       
  1041 }
       
  1042 
       
  1043 void WriteInitialization::acceptSpacer(DomSpacer *node)
       
  1044 {
       
  1045     m_output << m_indent << m_driver->findOrInsertSpacer(node) << " = ";
       
  1046     writeSpacerItem(node, m_output);
       
  1047     m_output << ";\n";
       
  1048 }
       
  1049 
       
  1050 static inline QString formLayoutRole(int column, int colspan)
       
  1051 {
       
  1052     if (colspan > 1)
       
  1053         return QLatin1String("QFormLayout::SpanningRole");
       
  1054     return column == 0 ? QLatin1String("QFormLayout::LabelRole") : QLatin1String("QFormLayout::FieldRole");
       
  1055 }
       
  1056 
       
  1057 void WriteInitialization::acceptLayoutItem(DomLayoutItem *node)
       
  1058 {
       
  1059     TreeWalker::acceptLayoutItem(node);
       
  1060 
       
  1061     DomLayout *layout = m_layoutChain.top();
       
  1062 
       
  1063     if (!layout)
       
  1064         return;
       
  1065 
       
  1066     const QString layoutName = m_driver->findOrInsertLayout(layout);
       
  1067     const QString itemName = m_driver->findOrInsertLayoutItem(node);
       
  1068 
       
  1069     QString addArgs;
       
  1070     QString methodPrefix = QLatin1String("add"); //Consistent API-design galore!
       
  1071     if (layout->attributeClass() == QLatin1String("QGridLayout")) {
       
  1072         const int row = node->attributeRow();
       
  1073         const int col = node->attributeColumn();
       
  1074 
       
  1075         const int rowSpan = node->hasAttributeRowSpan() ? node->attributeRowSpan() : 1;
       
  1076         const int colSpan = node->hasAttributeColSpan() ? node->attributeColSpan() : 1;
       
  1077 
       
  1078         addArgs = QString::fromLatin1("%1, %2, %3, %4, %5").arg(itemName).arg(row).arg(col).arg(rowSpan).arg(colSpan);
       
  1079     } else {
       
  1080         if (layout->attributeClass() == QLatin1String("QFormLayout")) {
       
  1081             methodPrefix = QLatin1String("set");
       
  1082             const int row = node->attributeRow();
       
  1083             const int colSpan = node->hasAttributeColSpan() ? node->attributeColSpan() : 1;
       
  1084             const QString role = formLayoutRole(node->attributeColumn(), colSpan);
       
  1085             addArgs = QString::fromLatin1("%1, %2, %3").arg(row).arg(role).arg(itemName);
       
  1086         } else {
       
  1087             addArgs = itemName;
       
  1088         }
       
  1089     }
       
  1090 
       
  1091     // figure out "add" method
       
  1092     m_output << "\n" << m_indent << layoutName << "->";
       
  1093     switch (node->kind()) {
       
  1094     case DomLayoutItem::Widget:
       
  1095         m_output << methodPrefix << "Widget(" <<  addArgs;
       
  1096         break;
       
  1097     case DomLayoutItem::Layout:
       
  1098         m_output <<  methodPrefix << "Layout(" << addArgs;
       
  1099         break;
       
  1100     case DomLayoutItem::Spacer:
       
  1101         m_output << methodPrefix << "Item(" << addArgs;
       
  1102         break;
       
  1103     case DomLayoutItem::Unknown:
       
  1104         Q_ASSERT( 0 );
       
  1105         break;
       
  1106     }
       
  1107     m_output << ");\n\n";
       
  1108 }
       
  1109 
       
  1110 void WriteInitialization::acceptActionGroup(DomActionGroup *node)
       
  1111 {
       
  1112     const QString actionName = m_driver->findOrInsertActionGroup(node);
       
  1113     QString varName = m_driver->findOrInsertWidget(m_widgetChain.top());
       
  1114 
       
  1115     if (m_actionGroupChain.top())
       
  1116         varName = m_driver->findOrInsertActionGroup(m_actionGroupChain.top());
       
  1117 
       
  1118     m_output << m_indent << actionName << " = new QActionGroup(" << varName << ");\n";
       
  1119     writeProperties(actionName, QLatin1String("QActionGroup"), node->elementProperty());
       
  1120 
       
  1121     m_actionGroupChain.push(node);
       
  1122     TreeWalker::acceptActionGroup(node);
       
  1123     m_actionGroupChain.pop();
       
  1124 }
       
  1125 
       
  1126 void WriteInitialization::acceptAction(DomAction *node)
       
  1127 {
       
  1128     if (node->hasAttributeMenu())
       
  1129         return;
       
  1130 
       
  1131     const QString actionName = m_driver->findOrInsertAction(node);
       
  1132     m_registeredActions.insert(actionName, node);
       
  1133     QString varName = m_driver->findOrInsertWidget(m_widgetChain.top());
       
  1134 
       
  1135     if (m_actionGroupChain.top())
       
  1136         varName = m_driver->findOrInsertActionGroup(m_actionGroupChain.top());
       
  1137 
       
  1138     m_output << m_indent << actionName << " = new QAction(" << varName << ");\n";
       
  1139     writeProperties(actionName, QLatin1String("QAction"), node->elementProperty());
       
  1140 }
       
  1141 
       
  1142 void WriteInitialization::acceptActionRef(DomActionRef *node)
       
  1143 {
       
  1144     QString actionName = node->attributeName();
       
  1145     const bool isSeparator = actionName == QLatin1String("separator");
       
  1146     bool isMenu = false;
       
  1147 
       
  1148     QString varName = m_driver->findOrInsertWidget(m_widgetChain.top());
       
  1149 
       
  1150     if (actionName.isEmpty() || !m_widgetChain.top()) {
       
  1151         return;
       
  1152     } else if (m_driver->actionGroupByName(actionName)) {
       
  1153         return;
       
  1154     } else if (DomWidget *w = m_driver->widgetByName(actionName)) {
       
  1155         isMenu = m_uic->isMenu(w->attributeClass());
       
  1156         bool inQ3ToolBar = m_uic->customWidgetsInfo()->extends(m_widgetChain.top()->attributeClass(), QLatin1String("Q3ToolBar"));
       
  1157         if (!isMenu && inQ3ToolBar) {
       
  1158             m_actionOut << m_indent << actionName << "->setParent(" << varName << ");\n";
       
  1159             return;
       
  1160         }
       
  1161     } else if (!(m_driver->actionByName(actionName) || isSeparator)) {
       
  1162         fprintf(stderr, "Warning: action `%s' not declared\n", actionName.toLatin1().data());
       
  1163         return;
       
  1164     }
       
  1165 
       
  1166     if (m_widgetChain.top() && isSeparator) {
       
  1167         // separator is always reserved!
       
  1168         m_actionOut << m_indent << varName << "->addSeparator();\n";
       
  1169         return;
       
  1170     }
       
  1171 
       
  1172     if (isMenu)
       
  1173         actionName += QLatin1String("->menuAction()");
       
  1174 
       
  1175     m_actionOut << m_indent << varName << "->addAction(" << actionName << ");\n";
       
  1176 }
       
  1177 
       
  1178 void WriteInitialization::writeProperties(const QString &varName,
       
  1179                                           const QString &className,
       
  1180                                           const DomPropertyList &lst,
       
  1181                                           unsigned flags)
       
  1182 {
       
  1183     const bool isTopLevel = m_widgetChain.count() == 1;
       
  1184 
       
  1185     if (m_uic->customWidgetsInfo()->extends(className, QLatin1String("QAxWidget"))) {
       
  1186         DomPropertyMap properties = propertyMap(lst);
       
  1187         if (properties.contains(QLatin1String("control"))) {
       
  1188             DomProperty *p = properties.value(QLatin1String("control"));
       
  1189             m_output << m_indent << varName << "->setControl(QString::fromUtf8("
       
  1190                    << fixString(toString(p->elementString()), m_dindent) << "));\n";
       
  1191         }
       
  1192     }
       
  1193 
       
  1194     DomWidget *buttonGroupWidget = findWidget(QLatin1String("Q3ButtonGroup"));
       
  1195 
       
  1196     QString indent;
       
  1197     if (!m_widgetChain.top()) {
       
  1198         indent = m_option.indent;
       
  1199         m_output << m_indent << "if (" << varName << "->objectName().isEmpty())\n";
       
  1200     }
       
  1201     if (!(flags & WritePropertyIgnoreObjectName))
       
  1202         m_output << m_indent << indent << varName
       
  1203                 << "->setObjectName(QString::fromUtf8(" << fixString(varName, m_dindent) << "));\n";
       
  1204 
       
  1205     int leftMargin, topMargin, rightMargin, bottomMargin;
       
  1206     leftMargin = topMargin = rightMargin = bottomMargin = -1;
       
  1207     bool frameShadowEncountered = false;
       
  1208 
       
  1209     for (int i=0; i<lst.size(); ++i) {
       
  1210         const DomProperty *p = lst.at(i);
       
  1211         if (!checkProperty(m_option.inputFile, p))
       
  1212             continue;
       
  1213         const QString propertyName = p->attributeName();
       
  1214         QString propertyValue;
       
  1215 
       
  1216         // special case for the property `geometry': Do not use position
       
  1217         if (isTopLevel && propertyName == QLatin1String("geometry") && p->elementRect()) {
       
  1218             const DomRect *r = p->elementRect();
       
  1219             m_output << m_indent << varName << "->resize(" << r->elementWidth() << ", " << r->elementHeight() << ");\n";
       
  1220             continue;
       
  1221         } else if (propertyName == QLatin1String("buttonGroupId") && buttonGroupWidget) { // Q3ButtonGroup support
       
  1222             m_output << m_indent << m_driver->findOrInsertWidget(buttonGroupWidget) << "->insert("
       
  1223                    << varName << ", " << p->elementNumber() << ");\n";
       
  1224             continue;
       
  1225         } else if (propertyName == QLatin1String("currentRow") // QListWidget::currentRow
       
  1226                     && m_uic->customWidgetsInfo()->extends(className, QLatin1String("QListWidget"))) {
       
  1227             m_delayedOut << m_indent << varName << "->setCurrentRow("
       
  1228                        << p->elementNumber() << ");\n";
       
  1229             continue;
       
  1230         } else if (propertyName == QLatin1String("currentIndex") // set currentIndex later
       
  1231                     && (m_uic->customWidgetsInfo()->extends(className, QLatin1String("QComboBox"))
       
  1232                     || m_uic->customWidgetsInfo()->extends(className, QLatin1String("QStackedWidget"))
       
  1233                     || m_uic->customWidgetsInfo()->extends(className, QLatin1String("QTabWidget"))
       
  1234                     || m_uic->customWidgetsInfo()->extends(className, QLatin1String("QToolBox")))) {
       
  1235             m_delayedOut << m_indent << varName << "->setCurrentIndex("
       
  1236                        << p->elementNumber() << ");\n";
       
  1237             continue;
       
  1238         } else if (propertyName == QLatin1String("tabSpacing")
       
  1239                     && m_uic->customWidgetsInfo()->extends(className, QLatin1String("QToolBox"))) {
       
  1240             m_delayedOut << m_indent << varName << "->layout()->setSpacing("
       
  1241                        << p->elementNumber() << ");\n";
       
  1242             continue;
       
  1243         } else if (propertyName == QLatin1String("control") // ActiveQt support
       
  1244                     && m_uic->customWidgetsInfo()->extends(className, QLatin1String("QAxWidget"))) {
       
  1245             // already done ;)
       
  1246             continue;
       
  1247         } else if (propertyName == QLatin1String("database")
       
  1248                     && p->elementStringList()) {
       
  1249             // Sql support
       
  1250             continue;
       
  1251         } else if (propertyName == QLatin1String("frameworkCode")
       
  1252                     && p->kind() == DomProperty::Bool) {
       
  1253             // Sql support
       
  1254             continue;
       
  1255         } else if (propertyName == QLatin1String("orientation")
       
  1256                     && m_uic->customWidgetsInfo()->extends(className, QLatin1String("Line"))) {
       
  1257             // Line support
       
  1258             QString shape = QLatin1String("QFrame::HLine");
       
  1259             if (p->elementEnum() == QLatin1String("Qt::Vertical"))
       
  1260                 shape = QLatin1String("QFrame::VLine");
       
  1261 
       
  1262             m_output << m_indent << varName << "->setFrameShape(" << shape << ");\n";
       
  1263             // QFrame Default is 'Plain'. Make the line 'Sunken' unless otherwise specified
       
  1264             if (!frameShadowEncountered)
       
  1265                 m_output << m_indent << varName << "->setFrameShadow(QFrame::Sunken);\n";
       
  1266             continue;
       
  1267         } else if ((flags & WritePropertyIgnoreMargin)  && propertyName == QLatin1String("margin")) {
       
  1268             continue;
       
  1269         } else if ((flags & WritePropertyIgnoreSpacing) && propertyName == QLatin1String("spacing")) {
       
  1270             continue;
       
  1271         } else if (propertyName == QLatin1String("leftMargin") && p->kind() == DomProperty::Number) {
       
  1272             leftMargin = p->elementNumber();
       
  1273             continue;
       
  1274         } else if (propertyName == QLatin1String("topMargin") && p->kind() == DomProperty::Number) {
       
  1275             topMargin = p->elementNumber();
       
  1276             continue;
       
  1277         } else if (propertyName == QLatin1String("rightMargin") && p->kind() == DomProperty::Number) {
       
  1278             rightMargin = p->elementNumber();
       
  1279             continue;
       
  1280         } else if (propertyName == QLatin1String("bottomMargin") && p->kind() == DomProperty::Number) {
       
  1281             bottomMargin = p->elementNumber();
       
  1282             continue;
       
  1283         } else if (propertyName == QLatin1String("frameShadow"))
       
  1284             frameShadowEncountered = true;
       
  1285 
       
  1286         bool stdset = m_stdsetdef;
       
  1287         if (p->hasAttributeStdset())
       
  1288             stdset = p->attributeStdset();
       
  1289 
       
  1290         QString setFunction;
       
  1291 
       
  1292         if (stdset) {
       
  1293             setFunction = QLatin1String("->set");
       
  1294             setFunction += propertyName.left(1).toUpper();
       
  1295             setFunction += propertyName.mid(1);
       
  1296             setFunction += QLatin1Char('(');
       
  1297         } else {
       
  1298             setFunction = QLatin1String("->setProperty(\"");
       
  1299             setFunction += propertyName;
       
  1300             setFunction += QLatin1String("\", QVariant(");
       
  1301         }
       
  1302 
       
  1303         QString varNewName = varName;
       
  1304 
       
  1305         switch (p->kind()) {
       
  1306         case DomProperty::Bool: {
       
  1307             propertyValue = p->elementBool();
       
  1308             break;
       
  1309         }
       
  1310         case DomProperty::Color:
       
  1311             propertyValue = domColor2QString(p->elementColor());
       
  1312             break;
       
  1313         case DomProperty::Cstring:
       
  1314             if (propertyName == QLatin1String("buddy") && m_uic->customWidgetsInfo()->extends(className, QLatin1String("QLabel"))) {
       
  1315                 m_buddies.append(Buddy(varName, p->elementCstring()));
       
  1316             } else {
       
  1317                 if (stdset)
       
  1318                     propertyValue = fixString(p->elementCstring(), m_dindent);
       
  1319                 else {
       
  1320                     propertyValue = QLatin1String("QByteArray(");
       
  1321                     propertyValue += fixString(p->elementCstring(), m_dindent);
       
  1322                     propertyValue += QLatin1Char(')');
       
  1323                 }
       
  1324             }
       
  1325             break;
       
  1326         case DomProperty::Cursor:
       
  1327             propertyValue = QString::fromLatin1("QCursor(static_cast<Qt::CursorShape>(%1))")
       
  1328                             .arg(p->elementCursor());
       
  1329             break;
       
  1330         case DomProperty::CursorShape:
       
  1331             if (p->hasAttributeStdset() && !p->attributeStdset())
       
  1332                 varNewName += QLatin1String("->viewport()");
       
  1333             propertyValue = QString::fromLatin1("QCursor(Qt::%1)")
       
  1334                             .arg(p->elementCursorShape());
       
  1335             break;
       
  1336         case DomProperty::Enum:
       
  1337             propertyValue = p->elementEnum();
       
  1338             if (!propertyValue.contains(QLatin1String("::"))) {
       
  1339                 QString scope  = className;
       
  1340                 scope += QLatin1String("::");
       
  1341                 propertyValue.prepend(scope);
       
  1342             }
       
  1343             break;
       
  1344         case DomProperty::Set:
       
  1345             propertyValue = p->elementSet();
       
  1346             break;
       
  1347         case DomProperty::Font:
       
  1348             propertyValue = writeFontProperties(p->elementFont());
       
  1349             break;
       
  1350         case DomProperty::IconSet:
       
  1351             propertyValue = writeIconProperties(p->elementIconSet());
       
  1352             break;
       
  1353         case DomProperty::Pixmap:
       
  1354             propertyValue = pixCall(p);
       
  1355             break;
       
  1356         case DomProperty::Palette: {
       
  1357             const DomPalette *pal = p->elementPalette();
       
  1358             const QString paletteName = m_driver->unique(QLatin1String("palette"));
       
  1359             m_output << m_indent << "QPalette " << paletteName << ";\n";
       
  1360 
       
  1361             writeColorGroup(pal->elementActive(), QLatin1String("QPalette::Active"), paletteName);
       
  1362             writeColorGroup(pal->elementInactive(), QLatin1String("QPalette::Inactive"), paletteName);
       
  1363             writeColorGroup(pal->elementDisabled(), QLatin1String("QPalette::Disabled"), paletteName);
       
  1364 
       
  1365             propertyValue = paletteName;
       
  1366             break;
       
  1367         }
       
  1368         case DomProperty::Point: {
       
  1369             const DomPoint *po = p->elementPoint();
       
  1370             propertyValue = QString::fromLatin1("QPoint(%1, %2)")
       
  1371                             .arg(po->elementX()).arg(po->elementY());
       
  1372             break;
       
  1373         }
       
  1374         case DomProperty::PointF: {
       
  1375             const DomPointF *pof = p->elementPointF();
       
  1376             propertyValue = QString::fromLatin1("QPointF(%1, %2)")
       
  1377                             .arg(pof->elementX()).arg(pof->elementY());
       
  1378             break;
       
  1379         }
       
  1380         case DomProperty::Rect: {
       
  1381             const DomRect *r = p->elementRect();
       
  1382             propertyValue = QString::fromLatin1("QRect(%1, %2, %3, %4)")
       
  1383                             .arg(r->elementX()).arg(r->elementY())
       
  1384                             .arg(r->elementWidth()).arg(r->elementHeight());
       
  1385             break;
       
  1386         }
       
  1387         case DomProperty::RectF: {
       
  1388             const DomRectF *rf = p->elementRectF();
       
  1389             propertyValue = QString::fromLatin1("QRectF(%1, %2, %3, %4)")
       
  1390                             .arg(rf->elementX()).arg(rf->elementY())
       
  1391                             .arg(rf->elementWidth()).arg(rf->elementHeight());
       
  1392             break;
       
  1393         }
       
  1394         case DomProperty::Locale: {
       
  1395              const DomLocale *locale = p->elementLocale();
       
  1396              propertyValue = QString::fromLatin1("QLocale(QLocale::%1, QLocale::%2)")
       
  1397                              .arg(locale->attributeLanguage()).arg(locale->attributeCountry());
       
  1398             break;
       
  1399         }
       
  1400         case DomProperty::SizePolicy: {
       
  1401             const QString spName = writeSizePolicy( p->elementSizePolicy());
       
  1402             m_output << m_indent << spName << QString::fromLatin1(
       
  1403                 ".setHeightForWidth(%1->sizePolicy().hasHeightForWidth());\n")
       
  1404                 .arg(varName);
       
  1405 
       
  1406             propertyValue = spName;
       
  1407             break;
       
  1408         }
       
  1409         case DomProperty::Size: {
       
  1410              const DomSize *s = p->elementSize();
       
  1411               propertyValue = QString::fromLatin1("QSize(%1, %2)")
       
  1412                              .arg(s->elementWidth()).arg(s->elementHeight());
       
  1413             break;
       
  1414         }
       
  1415         case DomProperty::SizeF: {
       
  1416             const DomSizeF *sf = p->elementSizeF();
       
  1417              propertyValue = QString::fromLatin1("QSizeF(%1, %2)")
       
  1418                             .arg(sf->elementWidth()).arg(sf->elementHeight());
       
  1419             break;
       
  1420         }
       
  1421         case DomProperty::String: {
       
  1422             if (propertyName == QLatin1String("objectName")) {
       
  1423                 const QString v = p->elementString()->text();
       
  1424                 if (v == varName)
       
  1425                     break;
       
  1426 
       
  1427                 // ### qWarning("Deprecated: the property `objectName' is different from the variable name");
       
  1428             }
       
  1429 
       
  1430             propertyValue = autoTrCall(p->elementString());
       
  1431             break;
       
  1432         }
       
  1433         case DomProperty::Number:
       
  1434             propertyValue = QString::number(p->elementNumber());
       
  1435             break;
       
  1436         case DomProperty::UInt:
       
  1437             propertyValue = QString::number(p->elementUInt());
       
  1438             propertyValue += QLatin1Char('u');
       
  1439             break;
       
  1440         case DomProperty::LongLong:
       
  1441             propertyValue = QLatin1String("Q_INT64_C(");
       
  1442             propertyValue += QString::number(p->elementLongLong());
       
  1443             propertyValue += QLatin1Char(')');;
       
  1444             break;
       
  1445         case DomProperty::ULongLong:
       
  1446             propertyValue = QLatin1String("Q_UINT64_C(");
       
  1447             propertyValue += QString::number(p->elementULongLong());
       
  1448             propertyValue += QLatin1Char(')');
       
  1449             break;
       
  1450         case DomProperty::Float:
       
  1451             propertyValue = QString::number(p->elementFloat());
       
  1452             break;
       
  1453         case DomProperty::Double:
       
  1454             propertyValue = QString::number(p->elementDouble());
       
  1455             break;
       
  1456         case DomProperty::Char: {
       
  1457             const DomChar *c = p->elementChar();
       
  1458             propertyValue = QString::fromLatin1("QChar(%1)")
       
  1459                             .arg(c->elementUnicode());
       
  1460             break;
       
  1461         }
       
  1462         case DomProperty::Date: {
       
  1463             const DomDate *d = p->elementDate();
       
  1464             propertyValue = QString::fromLatin1("QDate(%1, %2, %3)")
       
  1465                             .arg(d->elementYear())
       
  1466                             .arg(d->elementMonth())
       
  1467                             .arg(d->elementDay());
       
  1468             break;
       
  1469         }
       
  1470         case DomProperty::Time: {
       
  1471             const DomTime *t = p->elementTime();
       
  1472             propertyValue = QString::fromLatin1("QTime(%1, %2, %3)")
       
  1473                             .arg(t->elementHour())
       
  1474                             .arg(t->elementMinute())
       
  1475                             .arg(t->elementSecond());
       
  1476             break;
       
  1477         }
       
  1478         case DomProperty::DateTime: {
       
  1479             const DomDateTime *dt = p->elementDateTime();
       
  1480             propertyValue = QString::fromLatin1("QDateTime(QDate(%1, %2, %3), QTime(%4, %5, %6))")
       
  1481                             .arg(dt->elementYear())
       
  1482                             .arg(dt->elementMonth())
       
  1483                             .arg(dt->elementDay())
       
  1484                             .arg(dt->elementHour())
       
  1485                             .arg(dt->elementMinute())
       
  1486                             .arg(dt->elementSecond());
       
  1487             break;
       
  1488         }
       
  1489         case DomProperty::StringList:
       
  1490             propertyValue = QLatin1String("QStringList()");
       
  1491             if (p->elementStringList()->elementString().size()) {
       
  1492                 const QStringList lst = p->elementStringList()->elementString();
       
  1493                 for (int i=0; i<lst.size(); ++i) {
       
  1494                     propertyValue += QLatin1String(" << QString::fromUtf8(");
       
  1495                     propertyValue += fixString(lst.at(i), m_dindent);
       
  1496                     propertyValue += QLatin1Char(')');
       
  1497                 }
       
  1498             }
       
  1499             break;
       
  1500 
       
  1501         case DomProperty::Url: {
       
  1502             const DomUrl* u = p->elementUrl();
       
  1503             propertyValue = QString::fromLatin1("QUrl(%1)")
       
  1504                             .arg(fixString(u->elementString()->text(), m_dindent));
       
  1505             break;
       
  1506         }
       
  1507         case DomProperty::Brush:
       
  1508             propertyValue = writeBrushInitialization(p->elementBrush());
       
  1509             break;
       
  1510         case DomProperty::Unknown:
       
  1511             break;
       
  1512         }
       
  1513 
       
  1514         if (propertyValue.size()) {
       
  1515             const char* defineC = 0;
       
  1516             if (propertyName == QLatin1String("toolTip"))
       
  1517                 defineC = toolTipDefineC;
       
  1518             else if (propertyName == QLatin1String("whatsThis"))
       
  1519                 defineC = whatsThisDefineC;
       
  1520             else if (propertyName == QLatin1String("statusTip"))
       
  1521                 defineC = statusTipDefineC;
       
  1522             else if (propertyName == QLatin1String("accessibleName") || propertyName == QLatin1String("accessibleDescription"))
       
  1523                 defineC = accessibilityDefineC;
       
  1524 
       
  1525             QTextStream &o = autoTrOutput(p->elementString());
       
  1526 
       
  1527             if (defineC)
       
  1528                 openIfndef(o, QLatin1String(defineC));
       
  1529             o << m_indent << varNewName << setFunction << propertyValue;
       
  1530             if (!stdset)
       
  1531                 o << ')';
       
  1532             o << ");\n";
       
  1533             if (defineC)
       
  1534                 closeIfndef(o, QLatin1String(defineC));
       
  1535 
       
  1536             if (varName == m_mainFormVarName && &o == &m_refreshOut) {
       
  1537                 // this is the only place (currently) where we output mainForm name to the retranslateUi().
       
  1538                 // Other places output merely instances of a certain class (which cannot be main form, e.g. QListWidget).
       
  1539                 m_mainFormUsedInRetranslateUi = true;
       
  1540             }
       
  1541         }
       
  1542     }
       
  1543     if (leftMargin != -1 || topMargin != -1 || rightMargin != -1 || bottomMargin != -1) {
       
  1544         QString objectName = varName;
       
  1545         if (m_widgetChain.top()) {
       
  1546             const QString parentWidget = m_widgetChain.top()->attributeClass();
       
  1547 
       
  1548             if (!m_layoutChain.top() && (m_uic->customWidgetsInfo()->extends(parentWidget, QLatin1String("Q3GroupBox"))
       
  1549                         || m_uic->customWidgetsInfo()->extends(parentWidget, QLatin1String("Q3ButtonGroup")))) {
       
  1550                 objectName = m_driver->findOrInsertWidget(m_widgetChain.top()) + QLatin1String("->layout()");
       
  1551             }
       
  1552         }
       
  1553         m_output << m_indent << objectName << QLatin1String("->setContentsMargins(")
       
  1554                  << leftMargin << QLatin1String(", ")
       
  1555                  << topMargin << QLatin1String(", ")
       
  1556                  << rightMargin << QLatin1String(", ")
       
  1557                  << bottomMargin << QLatin1String(");\n");
       
  1558     }
       
  1559 }
       
  1560 
       
  1561 QString  WriteInitialization::writeSizePolicy(const DomSizePolicy *sp)
       
  1562 {
       
  1563 
       
  1564     // check cache
       
  1565     const SizePolicyHandle sizePolicyHandle(sp);
       
  1566     const SizePolicyNameMap::const_iterator it = m_sizePolicyNameMap.constFind(sizePolicyHandle);
       
  1567     if ( it != m_sizePolicyNameMap.constEnd()) {
       
  1568         return it.value();
       
  1569     }
       
  1570 
       
  1571 
       
  1572     // insert with new name
       
  1573     const QString spName = m_driver->unique(QLatin1String("sizePolicy"));
       
  1574     m_sizePolicyNameMap.insert(sizePolicyHandle, spName);
       
  1575 
       
  1576     m_output << m_indent << "QSizePolicy " << spName;
       
  1577     do {
       
  1578         if (sp->hasElementHSizeType() && sp->hasElementVSizeType()) {
       
  1579             m_output << "(static_cast<QSizePolicy::Policy>(" << sp->elementHSizeType()
       
  1580                 << "), static_cast<QSizePolicy::Policy>(" << sp->elementVSizeType() << "));\n";
       
  1581             break;
       
  1582         }
       
  1583         if (sp->hasAttributeHSizeType() && sp->hasAttributeVSizeType()) {
       
  1584                 m_output << "(QSizePolicy::" << sp->attributeHSizeType() << ", QSizePolicy::"
       
  1585                 << sp->attributeVSizeType() << ");\n";
       
  1586             break;
       
  1587         }
       
  1588         m_output << ";\n";
       
  1589     } while (false);
       
  1590 
       
  1591     m_output << m_indent << spName << ".setHorizontalStretch("
       
  1592         << sp->elementHorStretch() << ");\n";
       
  1593     m_output << m_indent << spName << ".setVerticalStretch("
       
  1594         << sp->elementVerStretch() << ");\n";
       
  1595     return spName;
       
  1596 }
       
  1597 // Check for a font with the given properties in the FontPropertiesNameMap
       
  1598 // or create a new one. Returns the name.
       
  1599 
       
  1600 QString WriteInitialization::writeFontProperties(const DomFont *f)
       
  1601 {
       
  1602     // check cache
       
  1603     const FontHandle fontHandle(f);
       
  1604     const FontPropertiesNameMap::const_iterator it = m_fontPropertiesNameMap.constFind(fontHandle);
       
  1605     if ( it != m_fontPropertiesNameMap.constEnd()) {
       
  1606         return it.value();
       
  1607     }
       
  1608 
       
  1609     // insert with new name
       
  1610     const QString fontName = m_driver->unique(QLatin1String("font"));
       
  1611     m_fontPropertiesNameMap.insert(FontHandle(f), fontName);
       
  1612 
       
  1613     m_output << m_indent << "QFont " << fontName << ";\n";
       
  1614     if (f->hasElementFamily() && !f->elementFamily().isEmpty()) {
       
  1615         m_output << m_indent << fontName << ".setFamily(QString::fromUtf8(" << fixString(f->elementFamily(), m_dindent)
       
  1616             << "));\n";
       
  1617     }
       
  1618     if (f->hasElementPointSize() && f->elementPointSize() > 0) {
       
  1619          m_output << m_indent << fontName << ".setPointSize(" << f->elementPointSize()
       
  1620              << ");\n";
       
  1621     }
       
  1622 
       
  1623     if (f->hasElementBold()) {
       
  1624         m_output << m_indent << fontName << ".setBold("
       
  1625             << (f->elementBold() ? "true" : "false") << ");\n";
       
  1626     }
       
  1627     if (f->hasElementItalic()) {
       
  1628         m_output << m_indent << fontName << ".setItalic("
       
  1629             <<  (f->elementItalic() ? "true" : "false") << ");\n";
       
  1630     }
       
  1631     if (f->hasElementUnderline()) {
       
  1632         m_output << m_indent << fontName << ".setUnderline("
       
  1633             << (f->elementUnderline() ? "true" : "false") << ");\n";
       
  1634     }
       
  1635     if (f->hasElementWeight() && f->elementWeight() > 0) {
       
  1636         m_output << m_indent << fontName << ".setWeight("
       
  1637             << f->elementWeight() << ");" << endl;
       
  1638     }
       
  1639     if (f->hasElementStrikeOut()) {
       
  1640          m_output << m_indent << fontName << ".setStrikeOut("
       
  1641             << (f->elementStrikeOut() ? "true" : "false") << ");\n";
       
  1642     }
       
  1643     if (f->hasElementKerning()) {
       
  1644         m_output << m_indent << fontName << ".setKerning("
       
  1645             << (f->elementKerning() ? "true" : "false") << ");\n";
       
  1646     }
       
  1647     if (f->hasElementAntialiasing()) {
       
  1648         m_output << m_indent << fontName << ".setStyleStrategy("
       
  1649             << (f->elementAntialiasing() ? "QFont::PreferDefault" : "QFont::NoAntialias") << ");\n";
       
  1650     }
       
  1651     if (f->hasElementStyleStrategy()) {
       
  1652          m_output << m_indent << fontName << ".setStyleStrategy(QFont::"
       
  1653             << f->elementStyleStrategy() << ");\n";
       
  1654     }
       
  1655     return  fontName;
       
  1656 }
       
  1657 
       
  1658 QString WriteInitialization::writeIconProperties(const DomResourceIcon *i)
       
  1659 {
       
  1660     // check cache
       
  1661     const IconHandle iconHandle(i);
       
  1662     const IconPropertiesNameMap::const_iterator it = m_iconPropertiesNameMap.constFind(iconHandle);
       
  1663     if (it != m_iconPropertiesNameMap.constEnd()) {
       
  1664         return it.value();
       
  1665     }
       
  1666 
       
  1667     // insert with new name
       
  1668     const QString iconName = m_driver->unique(QLatin1String("icon"));
       
  1669     m_iconPropertiesNameMap.insert(IconHandle(i), iconName);
       
  1670     if (isIconFormat44(i)) {
       
  1671         const QString pixmap = QLatin1String("QPixmap");
       
  1672         m_output << m_indent << "QIcon " << iconName << ";\n";
       
  1673         if (i->hasElementNormalOff())
       
  1674             m_output << m_indent << iconName << ".addFile(QString::fromUtf8(" << fixString(i->elementNormalOff()->text(), m_dindent) << "), QSize(), QIcon::Normal, QIcon::Off);\n";
       
  1675         if (i->hasElementNormalOn())
       
  1676             m_output << m_indent << iconName << ".addFile(QString::fromUtf8(" << fixString(i->elementNormalOn()->text(), m_dindent) << "), QSize(), QIcon::Normal, QIcon::On);\n";
       
  1677         if (i->hasElementDisabledOff())
       
  1678             m_output << m_indent << iconName << ".addFile(QString::fromUtf8(" << fixString(i->elementDisabledOff()->text(), m_dindent) << "), QSize(), QIcon::Disabled, QIcon::Off);\n";
       
  1679         if (i->hasElementDisabledOn())
       
  1680             m_output << m_indent << iconName << ".addFile(QString::fromUtf8(" << fixString(i->elementDisabledOn()->text(), m_dindent) << "), QSize(), QIcon::Disabled, QIcon::On);\n";
       
  1681         if (i->hasElementActiveOff())
       
  1682             m_output << m_indent << iconName << ".addFile(QString::fromUtf8(" << fixString(i->elementActiveOff()->text(), m_dindent) << "), QSize(), QIcon::Active, QIcon::Off);\n";
       
  1683         if (i->hasElementActiveOn())
       
  1684             m_output << m_indent << iconName << ".addFile(QString::fromUtf8(" << fixString(i->elementActiveOn()->text(), m_dindent) << "), QSize(), QIcon::Active, QIcon::On);\n";
       
  1685         if (i->hasElementSelectedOff())
       
  1686             m_output << m_indent << iconName << ".addFile(QString::fromUtf8(" << fixString(i->elementSelectedOff()->text(), m_dindent) << "), QSize(), QIcon::Selected, QIcon::Off);\n";
       
  1687         if (i->hasElementSelectedOn())
       
  1688             m_output << m_indent << iconName << ".addFile(QString::fromUtf8(" << fixString(i->elementSelectedOn()->text(), m_dindent) << "), QSize(), QIcon::Selected, QIcon::On);\n";
       
  1689     } else { // pre-4.4 legacy
       
  1690         m_output <<  m_indent << "const QIcon " << iconName << " = " << pixCall(QLatin1String("QIcon"), i->text())<< ";\n";
       
  1691     }
       
  1692     return iconName;
       
  1693 }
       
  1694 
       
  1695 QString WriteInitialization::domColor2QString(const DomColor *c)
       
  1696 {
       
  1697     if (c->hasAttributeAlpha())
       
  1698         return QString::fromLatin1("QColor(%1, %2, %3, %4)")
       
  1699             .arg(c->elementRed())
       
  1700             .arg(c->elementGreen())
       
  1701             .arg(c->elementBlue())
       
  1702             .arg(c->attributeAlpha());
       
  1703     return QString::fromLatin1("QColor(%1, %2, %3)")
       
  1704         .arg(c->elementRed())
       
  1705         .arg(c->elementGreen())
       
  1706         .arg(c->elementBlue());
       
  1707 }
       
  1708 
       
  1709 void WriteInitialization::writeColorGroup(DomColorGroup *colorGroup, const QString &group, const QString &paletteName)
       
  1710 {
       
  1711     if (!colorGroup)
       
  1712         return;
       
  1713 
       
  1714     // old format
       
  1715     const QList<DomColor*> colors = colorGroup->elementColor();
       
  1716     for (int i=0; i<colors.size(); ++i) {
       
  1717         const DomColor *color = colors.at(i);
       
  1718 
       
  1719         m_output << m_indent << paletteName << ".setColor(" << group
       
  1720             << ", " << "static_cast<QPalette::ColorRole>(" << QString::number(i) << ')'
       
  1721             << ", " << domColor2QString(color)
       
  1722             << ");\n";
       
  1723     }
       
  1724 
       
  1725     // new format
       
  1726     const QList<DomColorRole *> colorRoles = colorGroup->elementColorRole();
       
  1727     QListIterator<DomColorRole *> itRole(colorRoles);
       
  1728     while (itRole.hasNext()) {
       
  1729         const DomColorRole *colorRole = itRole.next();
       
  1730         if (colorRole->hasAttributeRole()) {
       
  1731             const QString brushName = writeBrushInitialization(colorRole->elementBrush());
       
  1732             m_output << m_indent << paletteName << ".setBrush(" << group
       
  1733                 << ", " << "QPalette::" << colorRole->attributeRole()
       
  1734                 << ", " << brushName << ");\n";
       
  1735         }
       
  1736     }
       
  1737 }
       
  1738 
       
  1739 // Write initialization for brush unless it is found in the cache. Returns the name to use
       
  1740 // in an expression.
       
  1741 QString WriteInitialization::writeBrushInitialization(const DomBrush *brush)
       
  1742 {
       
  1743     // Simple solid, colored  brushes are cached
       
  1744     const bool solidColoredBrush = !brush->hasAttributeBrushStyle() || brush->attributeBrushStyle() == QLatin1String("SolidPattern");
       
  1745     uint rgb = 0;
       
  1746     if (solidColoredBrush) {
       
  1747         if (const DomColor *color = brush->elementColor()) {
       
  1748             rgb = ((color->elementRed() & 0xFF) << 24) |
       
  1749                   ((color->elementGreen() & 0xFF) << 16) |
       
  1750                   ((color->elementBlue() & 0xFF) << 8) |
       
  1751                   ((color->attributeAlpha() & 0xFF));
       
  1752             const ColorBrushHash::const_iterator cit = m_colorBrushHash.constFind(rgb);
       
  1753             if (cit != m_colorBrushHash.constEnd())
       
  1754                 return cit.value();
       
  1755         }
       
  1756     }
       
  1757     // Create and enter into cache if simple
       
  1758     const QString brushName = m_driver->unique(QLatin1String("brush"));
       
  1759     writeBrush(brush, brushName);
       
  1760     if (solidColoredBrush)
       
  1761         m_colorBrushHash.insert(rgb, brushName);
       
  1762     return brushName;
       
  1763 }
       
  1764 
       
  1765 void WriteInitialization::writeBrush(const DomBrush *brush, const QString &brushName)
       
  1766 {
       
  1767     QString style = QLatin1String("SolidPattern");
       
  1768     if (brush->hasAttributeBrushStyle())
       
  1769         style = brush->attributeBrushStyle();
       
  1770 
       
  1771     if (style == QLatin1String("LinearGradientPattern") ||
       
  1772             style == QLatin1String("RadialGradientPattern") ||
       
  1773             style == QLatin1String("ConicalGradientPattern")) {
       
  1774         const DomGradient *gradient = brush->elementGradient();
       
  1775         const QString gradientType = gradient->attributeType();
       
  1776         const QString gradientName = m_driver->unique(QLatin1String("gradient"));
       
  1777         if (gradientType == QLatin1String("LinearGradient")) {
       
  1778             m_output << m_indent << "QLinearGradient " << gradientName
       
  1779                 << '(' << gradient->attributeStartX()
       
  1780                 << ", " << gradient->attributeStartY()
       
  1781                 << ", " << gradient->attributeEndX()
       
  1782                 << ", " << gradient->attributeEndY() << ");\n";
       
  1783         } else if (gradientType == QLatin1String("RadialGradient")) {
       
  1784             m_output << m_indent << "QRadialGradient " << gradientName
       
  1785                 << '(' << gradient->attributeCentralX()
       
  1786                 << ", " << gradient->attributeCentralY()
       
  1787                 << ", " << gradient->attributeRadius()
       
  1788                 << ", " << gradient->attributeFocalX()
       
  1789                 << ", " << gradient->attributeFocalY() << ");\n";
       
  1790         } else if (gradientType == QLatin1String("ConicalGradient")) {
       
  1791             m_output << m_indent << "QConicalGradient " << gradientName
       
  1792                 << '(' << gradient->attributeCentralX()
       
  1793                 << ", " << gradient->attributeCentralY()
       
  1794                 << ", " << gradient->attributeAngle() << ");\n";
       
  1795         }
       
  1796 
       
  1797         m_output << m_indent << gradientName << ".setSpread(QGradient::"
       
  1798             << gradient->attributeSpread() << ");\n";
       
  1799 
       
  1800         if (gradient->hasAttributeCoordinateMode()) {
       
  1801             m_output << m_indent << gradientName << ".setCoordinateMode(QGradient::"
       
  1802                 << gradient->attributeCoordinateMode() << ");\n";
       
  1803         }
       
  1804 
       
  1805        const  QList<DomGradientStop *> stops = gradient->elementGradientStop();
       
  1806         QListIterator<DomGradientStop *> it(stops);
       
  1807         while (it.hasNext()) {
       
  1808             const DomGradientStop *stop = it.next();
       
  1809             const DomColor *color = stop->elementColor();
       
  1810             m_output << m_indent << gradientName << ".setColorAt("
       
  1811                 << stop->attributePosition() << ", "
       
  1812                 << domColor2QString(color) << ");\n";
       
  1813         }
       
  1814         m_output << m_indent << "QBrush " << brushName << '('
       
  1815             << gradientName << ");\n";
       
  1816     } else if (style == QLatin1String("TexturePattern")) {
       
  1817         const DomProperty *property = brush->elementTexture();
       
  1818         const QString iconValue = iconCall(property);
       
  1819 
       
  1820         m_output << m_indent << "QBrush " << brushName << " = QBrush("
       
  1821             << iconValue << ");\n";
       
  1822     } else {
       
  1823         const DomColor *color = brush->elementColor();
       
  1824         m_output << m_indent << "QBrush " << brushName << '('
       
  1825             << domColor2QString(color) << ");\n";
       
  1826 
       
  1827         m_output << m_indent << brushName << ".setStyle("
       
  1828             << "Qt::" << style << ");\n";
       
  1829     }
       
  1830 }
       
  1831 
       
  1832 void WriteInitialization::acceptCustomWidget(DomCustomWidget *node)
       
  1833 {
       
  1834     Q_UNUSED(node);
       
  1835 }
       
  1836 
       
  1837 void WriteInitialization::acceptCustomWidgets(DomCustomWidgets *node)
       
  1838 {
       
  1839     Q_UNUSED(node);
       
  1840 }
       
  1841 
       
  1842 void WriteInitialization::acceptTabStops(DomTabStops *tabStops)
       
  1843 {
       
  1844     QString lastName;
       
  1845 
       
  1846     const QStringList l = tabStops->elementTabStop();
       
  1847     for (int i=0; i<l.size(); ++i) {
       
  1848         const QString name = l.at(i);
       
  1849 
       
  1850         if (!m_registeredWidgets.contains(name)) {
       
  1851             fprintf(stderr, "'%s' isn't a valid widget\n", name.toLatin1().data());
       
  1852             continue;
       
  1853         }
       
  1854 
       
  1855         if (i == 0) {
       
  1856             lastName = name;
       
  1857             continue;
       
  1858         } else if (name.isEmpty() || lastName.isEmpty()) {
       
  1859             continue;
       
  1860         }
       
  1861 
       
  1862         m_output << m_indent << "QWidget::setTabOrder(" << lastName << ", " << name << ");\n";
       
  1863 
       
  1864         lastName = name;
       
  1865     }
       
  1866 }
       
  1867 
       
  1868 void WriteInitialization::initializeQ3ListBox(DomWidget *w)
       
  1869 {
       
  1870     const QString varName = m_driver->findOrInsertWidget(w);
       
  1871     const QString className = w->attributeClass();
       
  1872 
       
  1873     const QList<DomItem*> items = w->elementItem();
       
  1874 
       
  1875     if (items.isEmpty())
       
  1876         return;
       
  1877 
       
  1878     m_refreshOut << m_indent << varName << "->clear();\n";
       
  1879 
       
  1880     for (int i=0; i<items.size(); ++i) {
       
  1881         const DomItem *item = items.at(i);
       
  1882 
       
  1883         const DomPropertyMap properties = propertyMap(item->elementProperty());
       
  1884         const DomProperty *text = properties.value(QLatin1String("text"));
       
  1885         const DomProperty *pixmap = properties.value(QLatin1String("pixmap"));
       
  1886         if (!(text || pixmap))
       
  1887             continue;
       
  1888 
       
  1889         m_refreshOut << m_indent << varName << "->insertItem(";
       
  1890         if (pixmap) {
       
  1891             m_refreshOut << pixCall(pixmap);
       
  1892 
       
  1893             if (text)
       
  1894                 m_refreshOut << ", ";
       
  1895         }
       
  1896         if (text)
       
  1897             m_refreshOut << trCall(text->elementString());
       
  1898         m_refreshOut << ");\n";
       
  1899     }
       
  1900 }
       
  1901 
       
  1902 void WriteInitialization::initializeQ3IconView(DomWidget *w)
       
  1903 {
       
  1904     const QString varName = m_driver->findOrInsertWidget(w);
       
  1905     const QString className = w->attributeClass();
       
  1906 
       
  1907     const QList<DomItem*> items = w->elementItem();
       
  1908 
       
  1909     if (items.isEmpty())
       
  1910         return;
       
  1911 
       
  1912     m_refreshOut << m_indent << varName << "->clear();\n";
       
  1913 
       
  1914     for (int i=0; i<items.size(); ++i) {
       
  1915         const DomItem *item = items.at(i);
       
  1916 
       
  1917         const DomPropertyMap properties = propertyMap(item->elementProperty());
       
  1918         const DomProperty *text = properties.value(QLatin1String("text"));
       
  1919         const DomProperty *pixmap = properties.value(QLatin1String("pixmap"));
       
  1920         if (!(text || pixmap))
       
  1921             continue;
       
  1922 
       
  1923         const QString itemName = m_driver->unique(QLatin1String("__item"));
       
  1924         m_refreshOut << "\n";
       
  1925         m_refreshOut << m_indent << "Q3IconViewItem *" << itemName << " = new Q3IconViewItem(" << varName << ");\n";
       
  1926 
       
  1927         if (pixmap) {
       
  1928             m_refreshOut << m_indent << itemName << "->setPixmap(" << pixCall(pixmap) << ");\n";
       
  1929         }
       
  1930 
       
  1931         if (text) {
       
  1932             m_refreshOut << m_indent << itemName << "->setText(" << trCall(text->elementString()) << ");\n";
       
  1933         }
       
  1934     }
       
  1935 }
       
  1936 
       
  1937 void WriteInitialization::initializeQ3ListView(DomWidget *w)
       
  1938 {
       
  1939     const QString varName = m_driver->findOrInsertWidget(w);
       
  1940     const QString className = w->attributeClass();
       
  1941 
       
  1942     // columns
       
  1943     const QList<DomColumn*> columns = w->elementColumn();
       
  1944     for (int i=0; i<columns.size(); ++i) {
       
  1945         const DomColumn *column = columns.at(i);
       
  1946 
       
  1947         const DomPropertyMap properties = propertyMap(column->elementProperty());
       
  1948         const DomProperty *text = properties.value(QLatin1String("text"));
       
  1949         const DomProperty *pixmap = properties.value(QLatin1String("pixmap"));
       
  1950         const DomProperty *clickable = properties.value(QLatin1String("clickable"));
       
  1951         const DomProperty *resizable = properties.value(QLatin1String("resizable"));
       
  1952 
       
  1953         const QString txt = trCall(text->elementString());
       
  1954         m_output << m_indent << varName << "->addColumn(" << txt << ");\n";
       
  1955         m_refreshOut << m_indent << varName << "->header()->setLabel(" << i << ", " << txt << ");\n";
       
  1956 
       
  1957         if (pixmap) {
       
  1958             m_output << m_indent << varName << "->header()->setLabel("
       
  1959                    << varName << "->header()->count() - 1, " << pixCall(pixmap) << ", " << txt << ");\n";
       
  1960         }
       
  1961 
       
  1962         if (clickable != 0) {
       
  1963             m_output << m_indent << varName << "->header()->setClickEnabled(" << clickable->elementBool() << ", " << varName << "->header()->count() - 1);\n";
       
  1964         }
       
  1965 
       
  1966         if (resizable != 0) {
       
  1967             m_output << m_indent << varName << "->header()->setResizeEnabled(" << resizable->elementBool() << ", " << varName << "->header()->count() - 1);\n";
       
  1968         }
       
  1969     }
       
  1970 
       
  1971     if (w->elementItem().size()) {
       
  1972         m_refreshOut << m_indent << varName << "->clear();\n";
       
  1973 
       
  1974         initializeQ3ListViewItems(className, varName, w->elementItem());
       
  1975     }
       
  1976 }
       
  1977 
       
  1978 void WriteInitialization::initializeQ3ListViewItems(const QString &className, const QString &varName, const QList<DomItem *> &items)
       
  1979 {
       
  1980     if (items.isEmpty())
       
  1981         return;
       
  1982 
       
  1983     // items
       
  1984     for (int i=0; i<items.size(); ++i) {
       
  1985         const DomItem *item = items.at(i);
       
  1986 
       
  1987         const QString itemName = m_driver->unique(QLatin1String("__item"));
       
  1988         m_refreshOut << "\n";
       
  1989         m_refreshOut << m_indent << "Q3ListViewItem *" << itemName << " = new Q3ListViewItem(" << varName << ");\n";
       
  1990 
       
  1991         int textCount = 0, pixCount = 0;
       
  1992         const DomPropertyList properties = item->elementProperty();
       
  1993         for (int i=0; i<properties.size(); ++i) {
       
  1994             const DomProperty *p = properties.at(i);
       
  1995             if (p->attributeName() == QLatin1String("text"))
       
  1996                 m_refreshOut << m_indent << itemName << "->setText(" << textCount++ << ", "
       
  1997                            << trCall(p->elementString()) << ");\n";
       
  1998 
       
  1999             if (p->attributeName() == QLatin1String("pixmap"))
       
  2000                 m_refreshOut << m_indent << itemName << "->setPixmap(" << pixCount++ << ", "
       
  2001                            << pixCall(p) << ");\n";
       
  2002         }
       
  2003 
       
  2004         if (item->elementItem().size()) {
       
  2005             m_refreshOut << m_indent << itemName << "->setOpen(true);\n";
       
  2006             initializeQ3ListViewItems(className, itemName, item->elementItem());
       
  2007         }
       
  2008     }
       
  2009 }
       
  2010 
       
  2011 
       
  2012 void WriteInitialization::initializeQ3Table(DomWidget *w)
       
  2013 {
       
  2014     const QString varName = m_driver->findOrInsertWidget(w);
       
  2015     const QString className = w->attributeClass();
       
  2016 
       
  2017     // columns
       
  2018     const QList<DomColumn*> columns = w->elementColumn();
       
  2019 
       
  2020     for (int i=0; i<columns.size(); ++i) {
       
  2021         const DomColumn *column = columns.at(i);
       
  2022 
       
  2023         const DomPropertyMap properties = propertyMap(column->elementProperty());
       
  2024         const DomProperty *text = properties.value(QLatin1String("text"));
       
  2025         const DomProperty *pixmap = properties.value(QLatin1String("pixmap"));
       
  2026 
       
  2027         m_refreshOut << m_indent << varName << "->horizontalHeader()->setLabel(" << i << ", ";
       
  2028         if (pixmap) {
       
  2029             m_refreshOut << pixCall(pixmap) << ", ";
       
  2030         }
       
  2031         m_refreshOut << trCall(text->elementString()) << ");\n";
       
  2032     }
       
  2033 
       
  2034     // rows
       
  2035     const QList<DomRow*> rows = w->elementRow();
       
  2036     for (int i=0; i<rows.size(); ++i) {
       
  2037         const DomRow *row = rows.at(i);
       
  2038 
       
  2039         const DomPropertyMap properties = propertyMap(row->elementProperty());
       
  2040         const DomProperty *text = properties.value(QLatin1String("text"));
       
  2041         const DomProperty *pixmap = properties.value(QLatin1String("pixmap"));
       
  2042 
       
  2043         m_refreshOut << m_indent << varName << "->verticalHeader()->setLabel(" << i << ", ";
       
  2044         if (pixmap) {
       
  2045             m_refreshOut << pixCall(pixmap) << ", ";
       
  2046         }
       
  2047         m_refreshOut << trCall(text->elementString()) << ");\n";
       
  2048     }
       
  2049 
       
  2050 
       
  2051     //initializeQ3TableItems(className, varName, w->elementItem());
       
  2052 }
       
  2053 
       
  2054 void WriteInitialization::initializeQ3TableItems(const QString &className, const QString &varName, const QList<DomItem *> &items)
       
  2055 {
       
  2056     Q_UNUSED(className);
       
  2057     Q_UNUSED(varName);
       
  2058     Q_UNUSED(items);
       
  2059 }
       
  2060 
       
  2061 QString WriteInitialization::iconCall(const DomProperty *icon)
       
  2062 {
       
  2063     if (icon->kind() == DomProperty::IconSet)
       
  2064         return writeIconProperties(icon->elementIconSet());
       
  2065     return pixCall(icon);
       
  2066 }
       
  2067 
       
  2068 QString WriteInitialization::pixCall(const DomProperty *p) const
       
  2069 {
       
  2070     QString type, s;
       
  2071     switch (p->kind()) {
       
  2072     case DomProperty::IconSet:
       
  2073         type = QLatin1String("QIcon");
       
  2074         s = p->elementIconSet()->text();
       
  2075         break;
       
  2076     case DomProperty::Pixmap:
       
  2077         type = QLatin1String("QPixmap");
       
  2078         s = p->elementPixmap()->text();
       
  2079         break;
       
  2080     default:
       
  2081         qWarning() << "Warning: Unknown icon format encountered. The ui-file was generated with a too-recent version of Designer.";
       
  2082         return QLatin1String("QIcon()");
       
  2083         break;
       
  2084     }
       
  2085     return pixCall(type, s);
       
  2086 }
       
  2087 
       
  2088 QString WriteInitialization::pixCall(const QString &t, const QString &text) const
       
  2089 {
       
  2090     QString type = t;
       
  2091     if (text.isEmpty()) {
       
  2092         type += QLatin1String("()");
       
  2093         return type;
       
  2094     }
       
  2095     if (const DomImage *image = findImage(text)) {
       
  2096         if (m_option.extractImages) {
       
  2097             const QString format = image->elementData()->attributeFormat();
       
  2098             const QString extension = format.left(format.indexOf(QLatin1Char('.'))).toLower();
       
  2099             QString rc = QLatin1String("QPixmap(QString::fromUtf8(\":/");
       
  2100             rc += m_generatedClass;
       
  2101             rc += QLatin1String("/images/");
       
  2102             rc += text;
       
  2103             rc += QLatin1Char('.');
       
  2104             rc += extension;
       
  2105             rc += QLatin1String("\"))");
       
  2106             return rc;
       
  2107         }
       
  2108         QString rc = WriteIconInitialization::iconFromDataFunction();
       
  2109         rc += QLatin1Char('(');
       
  2110         rc += text;
       
  2111         rc += QLatin1String("_ID)");
       
  2112         return rc;
       
  2113     }
       
  2114 
       
  2115     QString pixFunc = m_uic->pixmapFunction();
       
  2116     if (pixFunc.isEmpty())
       
  2117         pixFunc = QLatin1String("QString::fromUtf8");
       
  2118 
       
  2119     type += QLatin1Char('(');
       
  2120     type += pixFunc;
       
  2121     type += QLatin1Char('(');
       
  2122     type += fixString(text, m_dindent);
       
  2123     type += QLatin1String("))");
       
  2124     return type;
       
  2125 }
       
  2126 
       
  2127 void WriteInitialization::initializeComboBox3(DomWidget *w)
       
  2128 {
       
  2129     const QList<DomItem*> items = w->elementItem();
       
  2130     if (items.empty())
       
  2131         return;
       
  2132     // Basic legacy Qt3 support, write out translatable text items, ignore pixmaps
       
  2133     const QString varName = m_driver->findOrInsertWidget(w);
       
  2134     const QString textProperty = QLatin1String("text");
       
  2135 
       
  2136     m_refreshOut << m_indent << varName << "->clear();\n";
       
  2137     m_refreshOut << m_indent << varName << "->insertStringList(QStringList()" << '\n';
       
  2138     const int itemCount = items.size();
       
  2139     for (int i = 0; i< itemCount; ++i) {
       
  2140         const DomItem *item = items.at(i);
       
  2141         if (const DomProperty *text = propertyMap(item->elementProperty()).value(textProperty))
       
  2142             m_refreshOut << m_indent << " << " << autoTrCall(text->elementString()) << "\n";
       
  2143     }
       
  2144     m_refreshOut << m_indent << ", 0);\n";
       
  2145 }
       
  2146 
       
  2147 void WriteInitialization::initializeComboBox(DomWidget *w)
       
  2148 {
       
  2149     const QString varName = m_driver->findOrInsertWidget(w);
       
  2150     const QString className = w->attributeClass();
       
  2151 
       
  2152     const QList<DomItem*> items = w->elementItem();
       
  2153 
       
  2154     if (items.isEmpty())
       
  2155         return;
       
  2156 
       
  2157     // If possible use qcombobox's addItems() which is much faster then a bunch of addItem() calls
       
  2158     bool makeStringListCall = true;
       
  2159     bool translatable = false;
       
  2160     QStringList list;
       
  2161     for (int i=0; i<items.size(); ++i) {
       
  2162         const DomItem *item = items.at(i);
       
  2163         const DomPropertyMap properties = propertyMap(item->elementProperty());
       
  2164         const DomProperty *text = properties.value(QLatin1String("text"));
       
  2165         const DomProperty *pixmap = properties.value(QLatin1String("icon"));
       
  2166         bool needsTr = needsTranslation(text->elementString());
       
  2167         if (pixmap != 0 || (i > 0 && translatable != needsTr)) {
       
  2168             makeStringListCall = false;
       
  2169             break;
       
  2170         }
       
  2171         translatable = needsTr;
       
  2172         list.append(autoTrCall(text->elementString()));  // fix me here
       
  2173     }
       
  2174 
       
  2175     if (makeStringListCall) {
       
  2176         QTextStream &o = translatable ? m_refreshOut : m_output;
       
  2177         if (translatable)
       
  2178             o << m_indent << varName << "->clear();\n";
       
  2179         o << m_indent << varName << "->insertItems(0, QStringList()" << '\n';
       
  2180         for (int i = 0; i < list.size(); ++i)
       
  2181             o << m_indent << " << " << list.at(i) << "\n";
       
  2182         o << m_indent << ");\n";
       
  2183     } else {
       
  2184         for (int i = 0; i < items.size(); ++i) {
       
  2185             const DomItem *item = items.at(i);
       
  2186             const DomPropertyMap properties = propertyMap(item->elementProperty());
       
  2187             const DomProperty *text = properties.value(QLatin1String("text"));
       
  2188             const DomProperty *icon = properties.value(QLatin1String("icon"));
       
  2189 
       
  2190             QString iconValue;
       
  2191             if (icon)
       
  2192                 iconValue = iconCall(icon);
       
  2193 
       
  2194             m_output << m_indent << varName << "->addItem(";
       
  2195             if (icon)
       
  2196                 m_output << iconValue << ", ";
       
  2197 
       
  2198             if (needsTranslation(text->elementString())) {
       
  2199                 m_output << "QString());\n";
       
  2200                 m_refreshOut << m_indent << varName << "->setItemText(" << i << ", " << trCall(text->elementString()) << ");\n";
       
  2201             } else {
       
  2202                 m_output << noTrCall(text->elementString()) << ");\n";
       
  2203             }
       
  2204         }
       
  2205         m_refreshOut << "\n";
       
  2206     }
       
  2207 }
       
  2208 
       
  2209 QString WriteInitialization::disableSorting(DomWidget *w, const QString &varName)
       
  2210 {
       
  2211     // turn off sortingEnabled to force programmatic item order (setItem())
       
  2212     QString tempName;
       
  2213     if (!w->elementItem().isEmpty()) {
       
  2214         tempName = m_driver->unique(QLatin1String("__sortingEnabled"));
       
  2215         m_refreshOut << "\n";
       
  2216         m_refreshOut << m_indent << "const bool " << tempName
       
  2217             << " = " << varName << "->isSortingEnabled();\n";
       
  2218         m_refreshOut << m_indent << varName << "->setSortingEnabled(false);\n";
       
  2219     }
       
  2220     return tempName;
       
  2221 }
       
  2222 
       
  2223 void WriteInitialization::enableSorting(DomWidget *w, const QString &varName, const QString &tempName)
       
  2224 {
       
  2225     if (!w->elementItem().isEmpty()) {
       
  2226         m_refreshOut << m_indent << varName << "->setSortingEnabled(" << tempName << ");\n\n";
       
  2227     }
       
  2228 }
       
  2229 
       
  2230 /*
       
  2231  * Initializers are just strings containing the function call and need to be prepended
       
  2232  * the line indentation and the object they are supposed to initialize.
       
  2233  * String initializers come with a preprocessor conditional (ifdef), so the code
       
  2234  * compiles with QT_NO_xxx. A null pointer means no conditional. String initializers
       
  2235  * are written to the retranslateUi() function, others to setupUi().
       
  2236  */
       
  2237 
       
  2238 
       
  2239 /*!
       
  2240     Create non-string inititializer.
       
  2241     \param value the value to initialize the attribute with. May be empty, in which case
       
  2242         the initializer is omitted.
       
  2243     See above for other parameters.
       
  2244 */
       
  2245 void WriteInitialization::addInitializer(Item *item,
       
  2246         const QString &name, int column, const QString &value, const QString &directive, bool translatable) const
       
  2247 {
       
  2248     if (!value.isEmpty())
       
  2249         item->addSetter(QLatin1String("->set") + name.at(0).toUpper() + name.mid(1) +
       
  2250                     QLatin1Char('(') + (column < 0 ? QString() : QString::number(column) +
       
  2251                     QLatin1String(", ")) + value + QLatin1String(");"), directive, translatable);
       
  2252 }
       
  2253 
       
  2254 /*!
       
  2255     Create string inititializer.
       
  2256     \param initializers in/out list of inializers
       
  2257     \param properties map property name -> property to extract data from
       
  2258     \param name the property to extract
       
  2259     \param col the item column to generate the initializer for. This is relevant for
       
  2260         tree widgets only. If it is -1, no column index will be generated.
       
  2261     \param ifdef preprocessor symbol for disabling compilation of this initializer
       
  2262 */
       
  2263 void WriteInitialization::addStringInitializer(Item *item,
       
  2264         const DomPropertyMap &properties, const QString &name, int column, const QString &directive) const
       
  2265 {
       
  2266     if (const DomProperty *p = properties.value(name)) {
       
  2267         DomString *str = p->elementString();
       
  2268         QString text = toString(str);
       
  2269         if (!text.isEmpty()) {
       
  2270             bool translatable = needsTranslation(str);
       
  2271             QString value = autoTrCall(str);
       
  2272             addInitializer(item, name, column, value, directive, translatable);
       
  2273         }
       
  2274     }
       
  2275 }
       
  2276 
       
  2277 void WriteInitialization::addBrushInitializer(Item *item,
       
  2278         const DomPropertyMap &properties, const QString &name, int column)
       
  2279 {
       
  2280     if (const DomProperty *p = properties.value(name)) {
       
  2281         if (p->elementBrush())
       
  2282             addInitializer(item, name, column, writeBrushInitialization(p->elementBrush()));
       
  2283         else if (p->elementColor())
       
  2284             addInitializer(item, name, column, domColor2QString(p->elementColor()));
       
  2285     }
       
  2286 }
       
  2287 
       
  2288 /*!
       
  2289     Create inititializer for a flag value in the Qt namespace.
       
  2290     If the named property is not in the map, the initializer is omitted.
       
  2291 */
       
  2292 void WriteInitialization::addQtFlagsInitializer(Item *item,
       
  2293         const DomPropertyMap &properties, const QString &name, int column) const
       
  2294 {
       
  2295     if (const DomProperty *p = properties.value(name)) {
       
  2296         QString v = p->elementSet();
       
  2297         if (!v.isEmpty()) {
       
  2298             v.replace(QLatin1Char('|'), QLatin1String("|Qt::"));
       
  2299             addInitializer(item, name, column, QLatin1String("Qt::") + v);
       
  2300         }
       
  2301     }
       
  2302 }
       
  2303 
       
  2304 /*!
       
  2305     Create inititializer for an enum value in the Qt namespace.
       
  2306     If the named property is not in the map, the initializer is omitted.
       
  2307 */
       
  2308 void WriteInitialization::addQtEnumInitializer(Item *item,
       
  2309         const DomPropertyMap &properties, const QString &name, int column) const
       
  2310 {
       
  2311     if (const DomProperty *p = properties.value(name)) {
       
  2312         QString v = p->elementEnum();
       
  2313         if (!v.isEmpty())
       
  2314             addInitializer(item, name, column, QLatin1String("Qt::") + v);
       
  2315     }
       
  2316 }
       
  2317 
       
  2318 /*!
       
  2319     Create inititializers for all common properties that may be bound to a column.
       
  2320 */
       
  2321 void WriteInitialization::addCommonInitializers(Item *item,
       
  2322         const DomPropertyMap &properties, int column)
       
  2323 {
       
  2324     if (const DomProperty *icon = properties.value(QLatin1String("icon")))
       
  2325         addInitializer(item, QLatin1String("icon"), column, iconCall(icon));
       
  2326     addBrushInitializer(item, properties, QLatin1String("foreground"), column);
       
  2327     addBrushInitializer(item, properties, QLatin1String("background"), column);
       
  2328     if (const DomProperty *font = properties.value(QLatin1String("font")))
       
  2329         addInitializer(item, QLatin1String("font"), column, writeFontProperties(font->elementFont()));
       
  2330     addQtFlagsInitializer(item, properties, QLatin1String("textAlignment"), column);
       
  2331     addQtEnumInitializer(item, properties, QLatin1String("checkState"), column);
       
  2332     addStringInitializer(item, properties, QLatin1String("text"), column);
       
  2333     addStringInitializer(item, properties, QLatin1String("toolTip"), column, QLatin1String(toolTipDefineC));
       
  2334     addStringInitializer(item, properties, QLatin1String("whatsThis"), column, QLatin1String(whatsThisDefineC));
       
  2335     addStringInitializer(item, properties, QLatin1String("statusTip"), column, QLatin1String(statusTipDefineC));
       
  2336 }
       
  2337 
       
  2338 void WriteInitialization::initializeListWidget(DomWidget *w)
       
  2339 {
       
  2340     const QString varName = m_driver->findOrInsertWidget(w);
       
  2341     const QString className = w->attributeClass();
       
  2342 
       
  2343     const QList<DomItem*> items = w->elementItem();
       
  2344 
       
  2345     if (items.isEmpty())
       
  2346         return;
       
  2347 
       
  2348     QString tempName = disableSorting(w, varName);
       
  2349     // items
       
  2350     // TODO: the generated code should be data-driven to reduce its size
       
  2351     for (int i = 0; i < items.size(); ++i) {
       
  2352         const DomItem *domItem = items.at(i);
       
  2353 
       
  2354         const DomPropertyMap properties = propertyMap(domItem->elementProperty());
       
  2355 
       
  2356         Item item(QLatin1String("QListWidgetItem"), m_indent, m_output, m_refreshOut, m_driver);
       
  2357         addQtFlagsInitializer(&item, properties, QLatin1String("flags"));
       
  2358         addCommonInitializers(&item, properties);
       
  2359 
       
  2360         item.writeSetupUi(varName);
       
  2361         item.writeRetranslateUi(varName + QLatin1String("->item(") + QString::number(i) + QLatin1Char(')'));
       
  2362     }
       
  2363     enableSorting(w, varName, tempName);
       
  2364 }
       
  2365 
       
  2366 void WriteInitialization::initializeTreeWidget(DomWidget *w)
       
  2367 {
       
  2368     const QString varName = m_driver->findOrInsertWidget(w);
       
  2369 
       
  2370     // columns
       
  2371     Item item(QLatin1String("QTreeWidgetItem"), m_indent, m_output, m_refreshOut, m_driver);
       
  2372 
       
  2373     const QList<DomColumn*> columns = w->elementColumn();
       
  2374     for (int i = 0; i < columns.size(); ++i) {
       
  2375         const DomColumn *column = columns.at(i);
       
  2376 
       
  2377         const DomPropertyMap properties = propertyMap(column->elementProperty());
       
  2378         addCommonInitializers(&item, properties, i);
       
  2379     }
       
  2380     const QString itemName = item.writeSetupUi(QString(), Item::DontConstruct);
       
  2381     item.writeRetranslateUi(varName + QLatin1String("->headerItem()"));
       
  2382     if (!itemName.isNull())
       
  2383         m_output << m_indent << varName << "->setHeaderItem(" << itemName << ");\n";
       
  2384 
       
  2385     if (w->elementItem().size() == 0)
       
  2386         return;
       
  2387 
       
  2388     QString tempName = disableSorting(w, varName);
       
  2389 
       
  2390     QList<Item *> items = initializeTreeWidgetItems(w->elementItem());
       
  2391     for (int i = 0; i < items.count(); i++) {
       
  2392         Item *itm = items[i];
       
  2393         itm->writeSetupUi(varName);
       
  2394         itm->writeRetranslateUi(varName + QLatin1String("->topLevelItem(") + QString::number(i) + QLatin1Char(')'));
       
  2395         delete itm;
       
  2396     }
       
  2397 
       
  2398     enableSorting(w, varName, tempName);
       
  2399 }
       
  2400 
       
  2401 /*!
       
  2402     Create and write out initializers for tree widget items.
       
  2403     This function makes sure that only needed items are fetched (subject to preprocessor
       
  2404     conditionals), that each item is fetched from its parent widget/item exactly once
       
  2405     and that no temporary variables are created for items that are needed only once. As
       
  2406     fetches are built top-down from the root, but determining how often and under which
       
  2407     conditions an item is needed needs to be done bottom-up, the whole process makes
       
  2408     two passes, storing the intermediate result in a recursive StringInitializerListMap.
       
  2409 */
       
  2410 QList<WriteInitialization::Item *> WriteInitialization::initializeTreeWidgetItems(const QList<DomItem *> &domItems)
       
  2411 {
       
  2412     // items
       
  2413     QList<Item *> items;
       
  2414 
       
  2415     for (int i = 0; i < domItems.size(); ++i) {
       
  2416         const DomItem *domItem = domItems.at(i);
       
  2417 
       
  2418         Item *item = new Item(QLatin1String("QTreeWidgetItem"), m_indent, m_output, m_refreshOut, m_driver);
       
  2419         items << item;
       
  2420 
       
  2421         QHash<QString, DomProperty *> map;
       
  2422 
       
  2423         int col = -1;
       
  2424         const DomPropertyList properties = domItem->elementProperty();
       
  2425         for (int j = 0; j < properties.size(); ++j) {
       
  2426             DomProperty *p = properties.at(j);
       
  2427             if (p->attributeName() == QLatin1String("text")) {
       
  2428                 if (!map.isEmpty()) {
       
  2429                     addCommonInitializers(item, map, col);
       
  2430                     map.clear();
       
  2431                 }
       
  2432                 col++;
       
  2433             }
       
  2434             map.insert(p->attributeName(), p);
       
  2435         }
       
  2436         addCommonInitializers(item, map, col);
       
  2437         // AbstractFromBuilder saves flags last, so they always end up in the last column's map.
       
  2438         addQtFlagsInitializer(item, map, QLatin1String("flags"));
       
  2439 
       
  2440         QList<Item *> subItems = initializeTreeWidgetItems(domItem->elementItem());
       
  2441         foreach (Item *subItem, subItems)
       
  2442             item->addChild(subItem);
       
  2443     }
       
  2444     return items;
       
  2445 }
       
  2446 
       
  2447 void WriteInitialization::initializeTableWidget(DomWidget *w)
       
  2448 {
       
  2449     const QString varName = m_driver->findOrInsertWidget(w);
       
  2450 
       
  2451     // columns
       
  2452     const QList<DomColumn *> columns = w->elementColumn();
       
  2453 
       
  2454     if (columns.size() != 0) {
       
  2455         m_output << m_indent << "if (" << varName << "->columnCount() < " << columns.size() << ")\n"
       
  2456             << m_dindent << varName << "->setColumnCount(" << columns.size() << ");\n";
       
  2457     }
       
  2458 
       
  2459     for (int i = 0; i < columns.size(); ++i) {
       
  2460         const DomColumn *column = columns.at(i);
       
  2461         if (!column->elementProperty().isEmpty()) {
       
  2462             const DomPropertyMap properties = propertyMap(column->elementProperty());
       
  2463 
       
  2464             Item item(QLatin1String("QTableWidgetItem"), m_indent, m_output, m_refreshOut, m_driver);
       
  2465             addCommonInitializers(&item, properties);
       
  2466 
       
  2467             QString itemName = item.writeSetupUi(QString(), Item::ConstructItemAndVariable);
       
  2468             item.writeRetranslateUi(varName + QLatin1String("->horizontalHeaderItem(") + QString::number(i) + QLatin1Char(')'));
       
  2469             m_output << m_indent << varName << "->setHorizontalHeaderItem(" << QString::number(i) << ", " << itemName << ");\n";
       
  2470         }
       
  2471     }
       
  2472 
       
  2473     // rows
       
  2474     const QList<DomRow *> rows = w->elementRow();
       
  2475 
       
  2476     if (rows.size() != 0) {
       
  2477         m_output << m_indent << "if (" << varName << "->rowCount() < " << rows.size() << ")\n"
       
  2478             << m_dindent << varName << "->setRowCount(" << rows.size() << ");\n";
       
  2479     }
       
  2480 
       
  2481     for (int i = 0; i < rows.size(); ++i) {
       
  2482         const DomRow *row = rows.at(i);
       
  2483         if (!row->elementProperty().isEmpty()) {
       
  2484             const DomPropertyMap properties = propertyMap(row->elementProperty());
       
  2485 
       
  2486             Item item(QLatin1String("QTableWidgetItem"), m_indent, m_output, m_refreshOut, m_driver);
       
  2487             addCommonInitializers(&item, properties);
       
  2488 
       
  2489             QString itemName = item.writeSetupUi(QString(), Item::ConstructItemAndVariable);
       
  2490             item.writeRetranslateUi(varName + QLatin1String("->verticalHeaderItem(") + QString::number(i) + QLatin1Char(')'));
       
  2491             m_output << m_indent << varName << "->setVerticalHeaderItem(" << QString::number(i) << ", " << itemName << ");\n";
       
  2492         }
       
  2493     }
       
  2494 
       
  2495     // items
       
  2496     QString tempName = disableSorting(w, varName);
       
  2497 
       
  2498     const QList<DomItem *> items = w->elementItem();
       
  2499 
       
  2500     for (int i = 0; i < items.size(); ++i) {
       
  2501         const DomItem *cell = items.at(i);
       
  2502         if (cell->hasAttributeRow() && cell->hasAttributeColumn() && !cell->elementProperty().isEmpty()) {
       
  2503             const int r = cell->attributeRow();
       
  2504             const int c = cell->attributeColumn();
       
  2505             const DomPropertyMap properties = propertyMap(cell->elementProperty());
       
  2506 
       
  2507             Item item(QLatin1String("QTableWidgetItem"), m_indent, m_output, m_refreshOut, m_driver);
       
  2508             addQtFlagsInitializer(&item, properties, QLatin1String("flags"));
       
  2509             addCommonInitializers(&item, properties);
       
  2510 
       
  2511             QString itemName = item.writeSetupUi(QString(), Item::ConstructItemAndVariable);
       
  2512             item.writeRetranslateUi(varName + QLatin1String("->item(") + QString::number(r) + QLatin1String(", ") + QString::number(c) + QLatin1Char(')'));
       
  2513             m_output << m_indent << varName << "->setItem(" << QString::number(r) << ", " << QString::number(c) << ", " << itemName << ");\n";
       
  2514         }
       
  2515     }
       
  2516     enableSorting(w, varName, tempName);
       
  2517 }
       
  2518 
       
  2519 QString WriteInitialization::trCall(const QString &str, const QString &commentHint) const
       
  2520 {
       
  2521     if (str.isEmpty())
       
  2522         return QLatin1String("QString()");
       
  2523 
       
  2524     QString result;
       
  2525     const QString comment = commentHint.isEmpty() ? QString(QLatin1Char('0')) : fixString(commentHint, m_dindent);
       
  2526 
       
  2527     if (m_option.translateFunction.isEmpty()) {
       
  2528         result = QLatin1String("QApplication::translate(\"");
       
  2529         result += m_generatedClass;
       
  2530         result += QLatin1Char('"');
       
  2531         result += QLatin1String(", ");
       
  2532     } else {
       
  2533         result = m_option.translateFunction;
       
  2534         result += QLatin1Char('(');
       
  2535     }
       
  2536 
       
  2537     result += fixString(str, m_dindent);
       
  2538     result += QLatin1String(", ");
       
  2539     result += comment;
       
  2540 
       
  2541     if (m_option.translateFunction.isEmpty()) {
       
  2542         result += QLatin1String(", ");
       
  2543         result += QLatin1String("QApplication::UnicodeUTF8");
       
  2544     }
       
  2545 
       
  2546     result += QLatin1Char(')');
       
  2547     return result;
       
  2548 }
       
  2549 
       
  2550 void WriteInitialization::initializeQ3SqlDataTable(DomWidget *w)
       
  2551 {
       
  2552     const DomPropertyMap properties = propertyMap(w->elementProperty());
       
  2553 
       
  2554     const DomProperty *frameworkCode = properties.value(QLatin1String("frameworkCode"), 0);
       
  2555     if (frameworkCode && toBool(frameworkCode->elementBool()) == false)
       
  2556         return;
       
  2557 
       
  2558     QString connection;
       
  2559     QString table;
       
  2560     QString field;
       
  2561 
       
  2562     const DomProperty *db = properties.value(QLatin1String("database"), 0);
       
  2563     if (db && db->elementStringList()) {
       
  2564         const QStringList info = db->elementStringList()->elementString();
       
  2565         connection = info.size() > 0 ? info.at(0) : QString();
       
  2566         table = info.size() > 1 ? info.at(1) : QString();
       
  2567         field = info.size() > 2 ? info.at(2) : QString();
       
  2568     }
       
  2569 
       
  2570     if (table.isEmpty() || connection.isEmpty()) {
       
  2571         fprintf(stderr, "invalid database connection\n");
       
  2572         return;
       
  2573     }
       
  2574 
       
  2575    const QString varName = m_driver->findOrInsertWidget(w);
       
  2576 
       
  2577     m_output << m_indent << "if (!" << varName << "->sqlCursor()) {\n";
       
  2578 
       
  2579     m_output << m_dindent << varName << "->setSqlCursor(";
       
  2580 
       
  2581     if (connection == QLatin1String("(default)")) {
       
  2582         m_output << "new Q3SqlCursor(" << fixString(table, m_dindent) << "), false, true);\n";
       
  2583     } else {
       
  2584         m_output << "new Q3SqlCursor(" << fixString(table, m_dindent) << ", true, " << connection << "Connection" << "), false, true);\n";
       
  2585     }
       
  2586     m_output << m_dindent << varName << "->refresh(Q3DataTable::RefreshAll);\n";
       
  2587     m_output << m_indent << "}\n";
       
  2588 }
       
  2589 
       
  2590 void WriteInitialization::initializeQ3SqlDataBrowser(DomWidget *w)
       
  2591 {
       
  2592     const DomPropertyMap properties = propertyMap(w->elementProperty());
       
  2593 
       
  2594     const DomProperty *frameworkCode = properties.value(QLatin1String("frameworkCode"), 0);
       
  2595     if (frameworkCode && toBool(frameworkCode->elementBool()) == false)
       
  2596         return;
       
  2597 
       
  2598     QString connection;
       
  2599     QString table;
       
  2600     QString field;
       
  2601 
       
  2602     const DomProperty *db = properties.value(QLatin1String("database"), 0);
       
  2603     if (db && db->elementStringList()) {
       
  2604         const QStringList info = db->elementStringList()->elementString();
       
  2605         connection = info.size() > 0 ? info.at(0) : QString();
       
  2606         table = info.size() > 1 ? info.at(1) : QString();
       
  2607         field = info.size() > 2 ? info.at(2) : QString();
       
  2608     }
       
  2609 
       
  2610     if (table.isEmpty() || connection.isEmpty()) {
       
  2611         fprintf(stderr, "invalid database connection\n");
       
  2612         return;
       
  2613     }
       
  2614 
       
  2615     const QString varName = m_driver->findOrInsertWidget(w);
       
  2616 
       
  2617     m_output << m_indent << "if (!" << varName << "->sqlCursor()) {\n";
       
  2618 
       
  2619     m_output << m_dindent << varName << "->setSqlCursor(";
       
  2620 
       
  2621     if (connection == QLatin1String("(default)")) {
       
  2622         m_output << "new Q3SqlCursor(" << fixString(table, m_dindent) << "), true);\n";
       
  2623     } else {
       
  2624         m_output << "new Q3SqlCursor(" << fixString(table, m_dindent) << ", true, " << connection << "Connection" << "), false, true);\n";
       
  2625     }
       
  2626     m_output << m_dindent << varName << "->refresh();\n";
       
  2627     m_output << m_indent << "}\n";
       
  2628 }
       
  2629 
       
  2630 void WriteInitialization::initializeMenu(DomWidget *w, const QString &/*parentWidget*/)
       
  2631 {
       
  2632     const QString menuName = m_driver->findOrInsertWidget(w);
       
  2633     const QString menuAction = menuName + QLatin1String("Action");
       
  2634 
       
  2635     const DomAction *action = m_driver->actionByName(menuAction);
       
  2636     if (action && action->hasAttributeMenu()) {
       
  2637         m_output << m_indent << menuAction << " = " << menuName << "->menuAction();\n";
       
  2638     }
       
  2639 }
       
  2640 
       
  2641 QString WriteInitialization::trCall(DomString *str, const QString &defaultString) const
       
  2642 {
       
  2643     QString value = defaultString;
       
  2644     QString comment;
       
  2645     if (str) {
       
  2646         value = toString(str);
       
  2647         comment = str->attributeComment();
       
  2648     }
       
  2649     return trCall(value, comment);
       
  2650 }
       
  2651 
       
  2652 QString WriteInitialization::noTrCall(DomString *str, const QString &defaultString) const
       
  2653 {
       
  2654     QString value = defaultString;
       
  2655     if (!str && defaultString.isEmpty())
       
  2656         return QString();
       
  2657     if (str)
       
  2658         value = str->text();
       
  2659     QString ret = QLatin1String("QString::fromUtf8(");
       
  2660     ret += fixString(value, m_dindent);
       
  2661     ret += QLatin1Char(')');
       
  2662     return ret;
       
  2663 }
       
  2664 
       
  2665 QString WriteInitialization::autoTrCall(DomString *str, const QString &defaultString) const
       
  2666 {
       
  2667     if ((!str && !defaultString.isEmpty()) || needsTranslation(str))
       
  2668         return trCall(str, defaultString);
       
  2669     return noTrCall(str, defaultString);
       
  2670 }
       
  2671 
       
  2672 QTextStream &WriteInitialization::autoTrOutput(DomString *str, const QString &defaultString)
       
  2673 {
       
  2674     if ((!str && !defaultString.isEmpty()) || needsTranslation(str))
       
  2675         return m_refreshOut;
       
  2676     return m_output;
       
  2677 }
       
  2678 
       
  2679 bool WriteInitialization::isValidObject(const QString &name) const
       
  2680 {
       
  2681     return m_registeredWidgets.contains(name)
       
  2682         || m_registeredActions.contains(name);
       
  2683 }
       
  2684 
       
  2685 QString WriteInitialization::findDeclaration(const QString &name)
       
  2686 {
       
  2687     const QString normalized = Driver::normalizedName(name);
       
  2688 
       
  2689     if (DomWidget *widget = m_driver->widgetByName(normalized))
       
  2690         return m_driver->findOrInsertWidget(widget);
       
  2691     if (DomAction *action = m_driver->actionByName(normalized))
       
  2692         return m_driver->findOrInsertAction(action);
       
  2693     if (const DomButtonGroup *group = m_driver->findButtonGroup(normalized))
       
  2694         return m_driver->findOrInsertButtonGroup(group);
       
  2695     return QString();
       
  2696 }
       
  2697 
       
  2698 void WriteInitialization::acceptConnection(DomConnection *connection)
       
  2699 {
       
  2700     const QString sender = findDeclaration(connection->elementSender());
       
  2701     const QString receiver = findDeclaration(connection->elementReceiver());
       
  2702 
       
  2703     if (sender.isEmpty() || receiver.isEmpty())
       
  2704         return;
       
  2705 
       
  2706     m_output << m_indent << "QObject::connect("
       
  2707         << sender
       
  2708         << ", "
       
  2709         << "SIGNAL(" << connection->elementSignal() << ')'
       
  2710         << ", "
       
  2711         << receiver
       
  2712         << ", "
       
  2713         << "SLOT(" << connection->elementSlot() << ')'
       
  2714         << ");\n";
       
  2715 }
       
  2716 
       
  2717 DomImage *WriteInitialization::findImage(const QString &name) const
       
  2718 {
       
  2719     return m_registeredImages.value(name);
       
  2720 }
       
  2721 
       
  2722 DomWidget *WriteInitialization::findWidget(const QLatin1String &widgetClass)
       
  2723 {
       
  2724     for (int i = m_widgetChain.count() - 1; i >= 0; --i) {
       
  2725         DomWidget *widget = m_widgetChain.at(i);
       
  2726 
       
  2727         if (widget && m_uic->customWidgetsInfo()->extends(widget->attributeClass(), widgetClass))
       
  2728             return widget;
       
  2729     }
       
  2730 
       
  2731     return 0;
       
  2732 }
       
  2733 
       
  2734 void WriteInitialization::acceptImage(DomImage *image)
       
  2735 {
       
  2736     if (!image->hasAttributeName())
       
  2737         return;
       
  2738 
       
  2739     m_registeredImages.insert(image->attributeName(), image);
       
  2740 }
       
  2741 
       
  2742 void WriteInitialization::acceptWidgetScripts(const DomScripts &widgetScripts, DomWidget *node, const  DomWidgets &childWidgets)
       
  2743 {
       
  2744     // Add the per-class custom scripts to the per-widget ones.
       
  2745     DomScripts scripts(widgetScripts);
       
  2746 
       
  2747     if (DomScript *customWidgetScript = m_uic->customWidgetsInfo()->customWidgetScript(node->attributeClass()))
       
  2748         scripts.push_front(customWidgetScript);
       
  2749 
       
  2750     if (scripts.empty())
       
  2751         return;
       
  2752 
       
  2753     // concatenate script snippets
       
  2754     QString script;
       
  2755     foreach (const DomScript *domScript, scripts) {
       
  2756         const QString snippet = domScript->text();
       
  2757         if (!snippet.isEmpty()) {
       
  2758             script += snippet.trimmed();
       
  2759             script += QLatin1Char('\n');
       
  2760         }
       
  2761     }
       
  2762     if (script.isEmpty())
       
  2763         return;
       
  2764 
       
  2765     // Build the list of children and insert call
       
  2766     m_output << m_indent << "childWidgets.clear();\n";
       
  2767     if (!childWidgets.empty()) {
       
  2768         m_output << m_indent <<  "childWidgets";
       
  2769         foreach (DomWidget *child, childWidgets) {
       
  2770             m_output << " << " << m_driver->findOrInsertWidget(child);
       
  2771         }
       
  2772         m_output << ";\n";
       
  2773     }
       
  2774     m_output << m_indent << "scriptContext.run(QString::fromUtf8("
       
  2775              << fixString(script, m_dindent) << "), "
       
  2776              << m_driver->findOrInsertWidget(node) << ", childWidgets);\n";
       
  2777 }
       
  2778 
       
  2779 
       
  2780 static void generateMultiDirectiveBegin(QTextStream &outputStream, const QSet<QString> &directives)
       
  2781 {
       
  2782     if (directives.isEmpty())
       
  2783         return;
       
  2784 
       
  2785     QMap<QString, bool> map; // bool is dummy. The idea is to sort that (always generate in the same order) by putting a set into a map
       
  2786     foreach (QString str, directives)
       
  2787         map[str] = true;
       
  2788 
       
  2789     if (map.size() == 1) {
       
  2790         outputStream << "#ifndef " << map.constBegin().key() << endl;
       
  2791         return;
       
  2792     }
       
  2793 
       
  2794     outputStream << "#if";
       
  2795     bool doOr = false;
       
  2796     foreach (QString str, map.keys()) {
       
  2797         if (doOr)
       
  2798             outputStream << " ||";
       
  2799         outputStream << " !defined(" << str << ')';
       
  2800         doOr = true;
       
  2801     }
       
  2802     outputStream << endl;
       
  2803 }
       
  2804 
       
  2805 static void generateMultiDirectiveEnd(QTextStream &outputStream, const QSet<QString> &directives)
       
  2806 {
       
  2807     if (directives.isEmpty())
       
  2808         return;
       
  2809 
       
  2810     outputStream << "#endif" << endl;
       
  2811 }
       
  2812 
       
  2813 WriteInitialization::Item::Item(const QString &itemClassName, const QString &indent, QTextStream &setupUiStream, QTextStream &retranslateUiStream, Driver *driver)
       
  2814     :
       
  2815     m_parent(0),
       
  2816     m_itemClassName(itemClassName),
       
  2817     m_indent(indent),
       
  2818     m_setupUiStream(setupUiStream),
       
  2819     m_retranslateUiStream(retranslateUiStream),
       
  2820     m_driver(driver)
       
  2821 {
       
  2822 
       
  2823 }
       
  2824 
       
  2825 WriteInitialization::Item::~Item()
       
  2826 {
       
  2827     foreach (Item *child, m_children)
       
  2828         delete child;
       
  2829 }
       
  2830 
       
  2831 QString WriteInitialization::Item::writeSetupUi(const QString &parent, Item::EmptyItemPolicy emptyItemPolicy)
       
  2832 {
       
  2833     if (emptyItemPolicy == Item::DontConstruct && m_setupUiData.policy == ItemData::DontGenerate)
       
  2834         return QString();
       
  2835 
       
  2836     bool generateMultiDirective = false;
       
  2837     if (emptyItemPolicy == Item::ConstructItemOnly && m_children.size() == 0) {
       
  2838         if (m_setupUiData.policy == ItemData::DontGenerate) {
       
  2839             m_setupUiStream << m_indent << "new " << m_itemClassName << '(' << parent << ");\n";
       
  2840                 return QString();
       
  2841         } else if (m_setupUiData.policy == ItemData::GenerateWithMultiDirective) {
       
  2842             generateMultiDirective = true;
       
  2843         }
       
  2844     }
       
  2845 
       
  2846     if (generateMultiDirective)
       
  2847         generateMultiDirectiveBegin(m_setupUiStream, m_setupUiData.directives);
       
  2848 
       
  2849     const QString uniqueName = m_driver->unique(QLatin1String("__") + m_itemClassName.toLower());
       
  2850     m_setupUiStream << m_indent << m_itemClassName << " *" << uniqueName << " = new " << m_itemClassName << '(' << parent << ");\n";
       
  2851 
       
  2852     if (generateMultiDirective) {
       
  2853         m_setupUiStream << "#else\n";
       
  2854         m_setupUiStream << m_indent << "new " << m_itemClassName << '(' << parent << ");\n";
       
  2855         generateMultiDirectiveEnd(m_setupUiStream, m_setupUiData.directives);
       
  2856     }
       
  2857 
       
  2858     QMultiMap<QString, QString>::ConstIterator it = m_setupUiData.setters.constBegin();
       
  2859     while (it != m_setupUiData.setters.constEnd()) {
       
  2860         openIfndef(m_setupUiStream, it.key());
       
  2861         m_setupUiStream << m_indent << uniqueName << it.value() << endl;
       
  2862         closeIfndef(m_setupUiStream, it.key());
       
  2863         ++it;
       
  2864     }
       
  2865     foreach (Item *child, m_children)
       
  2866         child->writeSetupUi(uniqueName);
       
  2867     return uniqueName;
       
  2868 }
       
  2869 
       
  2870 void WriteInitialization::Item::writeRetranslateUi(const QString &parentPath)
       
  2871 {
       
  2872     if (m_retranslateUiData.policy == ItemData::DontGenerate)
       
  2873         return;
       
  2874 
       
  2875     if (m_retranslateUiData.policy == ItemData::GenerateWithMultiDirective)
       
  2876         generateMultiDirectiveBegin(m_retranslateUiStream, m_retranslateUiData.directives);
       
  2877 
       
  2878     const QString uniqueName = m_driver->unique(QLatin1String("___") + m_itemClassName.toLower());
       
  2879     m_retranslateUiStream << m_indent << m_itemClassName << " *" << uniqueName << " = " << parentPath << ";\n";
       
  2880 
       
  2881     if (m_retranslateUiData.policy == ItemData::GenerateWithMultiDirective)
       
  2882         generateMultiDirectiveEnd(m_retranslateUiStream, m_retranslateUiData.directives);
       
  2883 
       
  2884     QString oldDirective;
       
  2885     QMultiMap<QString, QString>::ConstIterator it = m_retranslateUiData.setters.constBegin();
       
  2886     while (it != m_retranslateUiData.setters.constEnd()) {
       
  2887         const QString newDirective = it.key();
       
  2888         if (oldDirective != newDirective) {
       
  2889             closeIfndef(m_retranslateUiStream, oldDirective);
       
  2890             openIfndef(m_retranslateUiStream, newDirective);
       
  2891             oldDirective = newDirective;
       
  2892         }
       
  2893         m_retranslateUiStream << m_indent << uniqueName << it.value() << endl;
       
  2894         ++it;
       
  2895     }
       
  2896     closeIfndef(m_retranslateUiStream, oldDirective);
       
  2897 
       
  2898     for (int i = 0; i < m_children.size(); i++)
       
  2899         m_children[i]->writeRetranslateUi(uniqueName + QLatin1String("->child(") + QString::number(i) + QLatin1Char(')'));
       
  2900 }
       
  2901 
       
  2902 void WriteInitialization::Item::addSetter(const QString &setter, const QString &directive, bool translatable)
       
  2903 {
       
  2904     const ItemData::TemporaryVariableGeneratorPolicy newPolicy = directive.isNull() ? ItemData::Generate : ItemData::GenerateWithMultiDirective;
       
  2905     if (translatable) {
       
  2906         m_retranslateUiData.setters.insert(directive, setter);
       
  2907         if (ItemData::GenerateWithMultiDirective == newPolicy)
       
  2908             m_retranslateUiData.directives << directive;
       
  2909         if (m_retranslateUiData.policy < newPolicy)
       
  2910             m_retranslateUiData.policy = newPolicy;
       
  2911     } else {
       
  2912         m_setupUiData.setters.insert(directive, setter);
       
  2913         if (ItemData::GenerateWithMultiDirective == newPolicy)
       
  2914             m_setupUiData.directives << directive;
       
  2915         if (m_setupUiData.policy < newPolicy)
       
  2916             m_setupUiData.policy = newPolicy;
       
  2917     }
       
  2918 }
       
  2919 
       
  2920 void WriteInitialization::Item::addChild(Item *child)
       
  2921 {
       
  2922     m_children << child;
       
  2923     child->m_parent = this;
       
  2924 
       
  2925     Item *c = child;
       
  2926     Item *p = this;
       
  2927     while (p) {
       
  2928         p->m_setupUiData.directives |= c->m_setupUiData.directives;
       
  2929         p->m_retranslateUiData.directives |= c->m_retranslateUiData.directives;
       
  2930         if (p->m_setupUiData.policy < c->m_setupUiData.policy)
       
  2931             p->m_setupUiData.policy = c->m_setupUiData.policy;
       
  2932         if (p->m_retranslateUiData.policy < c->m_retranslateUiData.policy)
       
  2933             p->m_retranslateUiData.policy = c->m_retranslateUiData.policy;
       
  2934         c = p;
       
  2935         p = p->m_parent;
       
  2936     }
       
  2937 }
       
  2938 
       
  2939 
       
  2940 } // namespace CPP
       
  2941 
       
  2942 QT_END_NAMESPACE