📄 cppwriteinitialization.cpp
字号:
/******************************************************************************** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.**** This file is part of the tools applications of the Qt Toolkit.**** This file may be used under the terms of the GNU General Public** License version 2.0 as published by the Free Software Foundation** and appearing in the file LICENSE.GPL included in the packaging of** this file. Please review the following information to ensure GNU** General Public Licensing requirements will be met:** http://trolltech.com/products/qt/licenses/licensing/opensource/**** If you are unsure which license is appropriate for your use, please** review the following information:** http://trolltech.com/products/qt/licenses/licensing/licensingoverview** or contact the sales department at sales@trolltech.com.**** In addition, as a special exception, Trolltech gives you certain** additional rights. These rights are described in the Trolltech GPL** Exception version 1.0, which can be found at** http://www.trolltech.com/products/qt/gplexception/ and in the file** GPL_EXCEPTION.txt in this package.**** In addition, as a special exception, Trolltech, as the sole copyright** holder for Qt Designer, grants users of the Qt/Eclipse Integration** plug-in the right for the Qt/Eclipse Integration to link to** functionality provided by Qt Designer and its related libraries.**** Trolltech reserves all rights not expressly granted herein.**** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.******************************************************************************/#include "cppwriteinitialization.h"#include "driver.h"#include "ui4.h"#include "utils.h"#include "uic.h"#include "databaseinfo.h"#include "globaldefs.h"#include <QTextStream>#include <QtDebug>namespace { // Fixup an enumeration name from class Qt. // They are currently stored as "BottomToolBarArea" instead of "Qt::BottomToolBarArea". // due to MO issues. This might be fixed in the future. void fixQtEnumerationName(QString& name) { static const QLatin1String prefix("Qt::"); if (name.indexOf(prefix) != 0) name.prepend(prefix); } // figure out the toolbar area of a DOM attrib list. // By legacy, it is stored as an integer. As of 4.3.0, it is the enumeration value. QString toolBarAreaStringFromDOMAttributes(const CPP::WriteInitialization::DomPropertyMap &attributes) { const DomProperty *pstyle = attributes.value(QLatin1String("toolBarArea")); if (!pstyle) return QString(); switch (pstyle->kind()) { case DomProperty::Number: { QString area = QLatin1String("static_cast<Qt::ToolBarArea>("); area += QString::number(pstyle->elementNumber()); area += QLatin1String("), "); return area; } case DomProperty::Enum: { QString area = pstyle->elementEnum(); fixQtEnumerationName(area); area += QLatin1String(", "); return area; } default: break; } return QString(); } // Write a statement to create a spacer item. void writeSpacerItem(const DomSpacer *node, QTextStream &output) { const QHash<QString, DomProperty *> properties = propertyMap(node->elementProperty()); output << "new QSpacerItem("; if (properties.contains(QLatin1String("sizeHint"))) { const DomSize *sizeHint = properties.value(QLatin1String("sizeHint"))->elementSize(); output << sizeHint->elementWidth() << ", " << sizeHint->elementHeight() << ", "; } // size type QString sizeType = properties.contains(QLatin1String("sizeType")) ? properties.value(QLatin1String("sizeType"))->elementEnum() : QString::fromLatin1("Expanding"); if (!sizeType.startsWith(QLatin1String("QSizePolicy::"))) sizeType.prepend(QLatin1String("QSizePolicy::")); // orientation bool isVspacer = false; if (properties.contains(QLatin1String("orientation"))) { const QString orientation = properties.value(QLatin1String("orientation"))->elementEnum(); if (orientation == QLatin1String("Qt::Vertical") || orientation == QLatin1String("Vertical")) isVspacer = true; } if (isVspacer) output << "QSizePolicy::Minimum, " << sizeType << ')'; else output << sizeType << ", QSizePolicy::Minimum)"; } // Helper for implementing comparison functions for integers. int compareInt(int i1, int i2) { if (i1 < i2) return -1; if (i1 > i2) return 1; return 0; } // Write object->setFoo(x); template <class Value> void writeSetter(const QString &indent, const QString &varName,const QString &setter, Value v, QTextStream &str) { str << indent << varName << "->" << setter << '(' << v << ");\n"; } void writeSetupUIScriptVariableDeclarations(const QString &indent, QTextStream &str) { str << indent << "ScriptContext scriptContext;\n" << indent << "QWidgetList childWidgets;\n"; }}namespace CPP {FontHandle::FontHandle(const DomFont *domFont) : m_domFont(domFont){}int FontHandle::compare(const FontHandle &rhs) const{ const QString family = m_domFont->hasElementFamily() ? m_domFont->elementFamily() : QString(); const QString rhsFamily = rhs.m_domFont->hasElementFamily() ? rhs.m_domFont->elementFamily() : QString(); if (const int frc = family.compare(rhsFamily)) return frc; const int pointSize = m_domFont->hasElementPointSize() ? m_domFont->elementPointSize() : -1; const int rhsPointSize = rhs.m_domFont->hasElementPointSize() ? rhs.m_domFont->elementPointSize() : -1; if (const int crc = compareInt(pointSize, rhsPointSize)) return crc; const int bold = m_domFont->hasElementBold() ? (m_domFont->elementBold() ? 1 : 0) : -1; const int rhsBold = rhs.m_domFont->hasElementBold() ? (rhs.m_domFont->elementBold() ? 1 : 0) : -1; if (const int crc = compareInt(bold, rhsBold)) return crc; const int italic = m_domFont->hasElementItalic() ? (m_domFont->elementItalic() ? 1 : 0) : -1; const int rhsItalic = rhs.m_domFont->hasElementItalic() ? (rhs.m_domFont->elementItalic() ? 1 : 0) : -1; if (const int crc = compareInt(italic, rhsItalic)) return crc; const int underline = m_domFont->hasElementUnderline() ? (m_domFont->elementUnderline() ? 1 : 0) : -1; const int rhsUnderline = rhs.m_domFont->hasElementUnderline() ? (rhs.m_domFont->elementUnderline() ? 1 : 0) : -1; if (const int crc = compareInt(underline, rhsUnderline)) return crc; const int weight = m_domFont->hasElementWeight() ? m_domFont->elementWeight() : -1; const int rhsWeight = rhs.m_domFont->hasElementWeight() ? rhs.m_domFont->elementWeight() : -1; if (const int crc = compareInt(weight, rhsWeight)) return crc; const int strikeOut = m_domFont->hasElementStrikeOut() ? (m_domFont->elementStrikeOut() ? 1 : 0) : -1; const int rhsStrikeOut = rhs.m_domFont->hasElementStrikeOut() ? (rhs.m_domFont->elementStrikeOut() ? 1 : 0) : -1; if (const int crc = compareInt(strikeOut, rhsStrikeOut)) return crc; const int kerning = m_domFont->hasElementKerning() ? (m_domFont->elementKerning() ? 1 : 0) : -1; const int rhsKerning = rhs.m_domFont->hasElementKerning() ? (rhs.m_domFont->elementKerning() ? 1 : 0) : -1; if (const int crc = compareInt(kerning, rhsKerning)) return crc; const int antialiasing = m_domFont->hasElementAntialiasing() ? (m_domFont->elementAntialiasing() ? 1 : 0) : -1; const int rhsAntialiasing = rhs.m_domFont->hasElementAntialiasing() ? (rhs.m_domFont->elementAntialiasing() ? 1 : 0) : -1; if (const int crc = compareInt(antialiasing, rhsAntialiasing)) return crc; const QString styleStrategy = m_domFont->hasElementStyleStrategy() ? m_domFont->elementStyleStrategy() : QString(); const QString rhsStyleStrategy = rhs.m_domFont->hasElementStyleStrategy() ? rhs.m_domFont->elementStyleStrategy() : QString(); if (const int src = styleStrategy.compare(rhsStyleStrategy)) return src; return 0;}#if defined(Q_OS_MAC) && defined(Q_CC_GNU) && (__GNUC__ == 3 && __GNUC_MINOR__ == 3)inline uint qHash(const SizePolicyHandle &handle) { return qHash(handle.m_domSizePolicy); }inline uint qHash(const FontHandle &handle) { return qHash(handle.m_domFont); }#endifSizePolicyHandle::SizePolicyHandle(const DomSizePolicy *domSizePolicy) : m_domSizePolicy(domSizePolicy){}int SizePolicyHandle::compare(const SizePolicyHandle &rhs) const{ const int hSizeType = m_domSizePolicy->hasElementHSizeType() ? m_domSizePolicy->elementHSizeType() : -1; const int rhsHSizeType = rhs.m_domSizePolicy->hasElementHSizeType() ? rhs.m_domSizePolicy->elementHSizeType() : -1; if (const int crc = compareInt(hSizeType, rhsHSizeType)) return crc; const int vSizeType = m_domSizePolicy->hasElementVSizeType() ? m_domSizePolicy->elementVSizeType() : -1; const int rhsVSizeType = rhs.m_domSizePolicy->hasElementVSizeType() ? rhs.m_domSizePolicy->elementVSizeType() : -1; if (const int crc = compareInt(vSizeType, rhsVSizeType)) return crc; const int hStretch = m_domSizePolicy->hasElementHorStretch() ? m_domSizePolicy->elementHorStretch() : -1; const int rhsHStretch = rhs.m_domSizePolicy->hasElementHorStretch() ? rhs.m_domSizePolicy->elementHorStretch() : -1; if (const int crc = compareInt(hStretch, rhsHStretch)) return crc; const int vStretch = m_domSizePolicy->hasElementVerStretch() ? m_domSizePolicy->elementVerStretch() : -1; const int rhsVStretch = rhs.m_domSizePolicy->hasElementVerStretch() ? rhs.m_domSizePolicy->elementVerStretch() : -1; if (const int crc = compareInt(vStretch, rhsVStretch)) return crc; const QString attributeHSizeType = m_domSizePolicy->hasAttributeHSizeType() ? m_domSizePolicy->attributeHSizeType() : QString(); const QString rhsAttributeHSizeType = rhs.m_domSizePolicy->hasAttributeHSizeType() ? rhs.m_domSizePolicy->attributeHSizeType() : QString(); if (const int hrc = attributeHSizeType.compare(rhsAttributeHSizeType)) return hrc; const QString attributeVSizeType = m_domSizePolicy->hasAttributeVSizeType() ? m_domSizePolicy->attributeVSizeType() : QString(); const QString rhsAttributeVSizeType = rhs.m_domSizePolicy->hasAttributeVSizeType() ? rhs.m_domSizePolicy->attributeVSizeType() : QString(); return attributeVSizeType.compare(rhsAttributeVSizeType);}// --- WriteInitialization: LayoutDefaultHandlerWriteInitialization::LayoutDefaultHandler::LayoutDefaultHandler(){ qFill(m_state, m_state + NumProperties, 0u); qFill(m_defaultValues, m_defaultValues + NumProperties, 0);}void WriteInitialization::LayoutDefaultHandler::acceptLayoutDefault(DomLayoutDefault *node){ if (!node) return; if (node->hasAttributeMargin()) { m_state[Margin] |= HasDefaultValue; m_defaultValues[Margin] = node->attributeMargin(); } if (node->hasAttributeSpacing()) { m_state[Spacing] |= HasDefaultValue; m_defaultValues[Spacing] = node->attributeSpacing(); }}void WriteInitialization::LayoutDefaultHandler::acceptLayoutFunction(DomLayoutFunction *node){ if (!node) return; if (node->hasAttributeMargin()) { m_state[Margin] |= HasDefaultFunction; m_functions[Margin] = node->attributeMargin(); m_functions[Margin] += QLatin1String("()"); } if (node->hasAttributeSpacing()) { m_state[Spacing] |= HasDefaultFunction; m_functions[Spacing] = node->attributeSpacing(); m_functions[Spacing] += QLatin1String("()"); }}void WriteInitialization::LayoutDefaultHandler::writeProperty(int p, const QString &indent, const QString &objectName, const DomPropertyMap &properties, const QString &propertyName, const QString &setter, int defaultStyleValue, bool suppressDefault, QTextStream &str) const{ // User value const DomPropertyMap::const_iterator mit = properties.constFind(propertyName); const bool found = mit != properties.constEnd(); if (found) { const int value = mit.value()->elementNumber(); // Emulate the pre 4.3 behaviour: The value form default value was only used to determine // the default value, layout properties were always written const bool useLayoutFunctionPre43 = !suppressDefault && (m_state[p] == (HasDefaultFunction|HasDefaultValue)) && value == m_defaultValues[p]; if (!useLayoutFunctionPre43) { bool ifndefMac = (!(m_state[p] & (HasDefaultFunction|HasDefaultValue)) && value == defaultStyleValue); if (ifndefMac) str << "#ifndef Q_OS_MAC\n"; writeSetter(indent, objectName, setter, value, str);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -