uic: standardize property lookups on QHash::value()

This is the pattern used throughout the rest of the uic codebase,
and is both more efficient (one lookup instead of two with an
additional contains() call) and will allow porting to another
container.

In writeSpacerItem(), also gently optimize string handling.

Change-Id: I20c7914089a699d2161d648b16d938e3a001ace3
Reviewed-by: Jarek Kobus <jaroslaw.kobus@qt.io>
This commit is contained in:
Marc Mutz 2017-05-31 09:03:20 +02:00
parent f68e4b8c31
commit 5b99ac4ead
2 changed files with 20 additions and 19 deletions

View File

@ -83,24 +83,29 @@ namespace {
int w = 0; int w = 0;
int h = 0; int h = 0;
if (properties.contains(QLatin1String("sizeHint"))) { if (const DomProperty *sh = properties.value(QLatin1String("sizeHint"))) {
const DomSize *sizeHint = properties.value(QLatin1String("sizeHint"))->elementSize(); const DomSize *sizeHint = sh->elementSize();
w = sizeHint->elementWidth(); w = sizeHint->elementWidth();
h = sizeHint->elementHeight(); h = sizeHint->elementHeight();
} }
output << w << ", " << h << ", "; output << w << ", " << h << ", ";
// size type // size type
QString sizeType = properties.contains(QLatin1String("sizeType")) ? QString sizeType;
properties.value(QLatin1String("sizeType"))->elementEnum() : if (const DomProperty *st = properties.value(QLatin1String("sizeType"))) {
QString::fromLatin1("Expanding"); const QString value = st->elementEnum();
if (value.startsWith(QLatin1String("QSizePolicy::")))
sizeType = value;
else
sizeType = QLatin1String("QSizePolicy::") + value;
} else {
sizeType = QStringLiteral("QSizePolicy::Expanding");
}
if (!sizeType.startsWith(QLatin1String("QSizePolicy::")))
sizeType.prepend(QLatin1String("QSizePolicy::"));
// orientation // orientation
bool isVspacer = false; bool isVspacer = false;
if (properties.contains(QLatin1String("orientation"))) { if (const DomProperty *o = properties.value(QLatin1String("orientation"))) {
const QString orientation = properties.value(QLatin1String("orientation"))->elementEnum(); const QString orientation = o->elementEnum();
if (orientation == QLatin1String("Qt::Vertical") || orientation == QLatin1String("Vertical")) isVspacer = true; if (orientation == QLatin1String("Qt::Vertical") || orientation == QLatin1String("Vertical")) isVspacer = true;
} }
@ -395,10 +400,8 @@ void WriteInitialization::LayoutDefaultHandler::writeProperty(int p, const QStri
int defaultStyleValue, bool suppressDefault, QTextStream &str) const int defaultStyleValue, bool suppressDefault, QTextStream &str) const
{ {
// User value // User value
const DomPropertyMap::const_iterator mit = properties.constFind(propertyName); if (const DomProperty *prop = properties.value(propertyName)) {
const bool found = mit != properties.constEnd(); const int value = prop->elementNumber();
if (found) {
const int value = mit.value()->elementNumber();
// Emulate the pre 4.3 behaviour: The value form default value was only used to determine // Emulate the pre 4.3 behaviour: The value form default value was only used to determine
// the default value, layout properties were always written // the default value, layout properties were always written
const bool useLayoutFunctionPre43 = !suppressDefault && (m_state[p] == (HasDefaultFunction|HasDefaultValue)) && value == m_defaultValues[p]; const bool useLayoutFunctionPre43 = !suppressDefault && (m_state[p] == (HasDefaultFunction|HasDefaultValue)) && value == m_defaultValues[p];
@ -883,7 +886,7 @@ void WriteInitialization::acceptLayout(DomLayout *node)
const QString varName = m_driver->findOrInsertLayout(node); const QString varName = m_driver->findOrInsertLayout(node);
const DomPropertyMap properties = propertyMap(node->elementProperty()); const DomPropertyMap properties = propertyMap(node->elementProperty());
const bool oldLayoutProperties = properties.constFind(QLatin1String("margin")) != properties.constEnd(); const bool oldLayoutProperties = properties.value(QLatin1String("margin")) != nullptr;
bool isGroupBox = false; bool isGroupBox = false;
@ -1148,9 +1151,7 @@ void WriteInitialization::writeProperties(const QString &varName,
if (m_uic->customWidgetsInfo()->extends(className, QLatin1String("QAxWidget"))) { if (m_uic->customWidgetsInfo()->extends(className, QLatin1String("QAxWidget"))) {
DomPropertyMap properties = propertyMap(lst); DomPropertyMap properties = propertyMap(lst);
if (properties.contains(QLatin1String("control"))) { if (DomProperty *p = properties.value(QLatin1String("control"))) {
DomProperty *p = properties.value(QLatin1String("control"));
Q_ASSERT( p );
m_output << m_indent << varName << "->setControl(" m_output << m_indent << varName << "->setControl("
<< writeString(toString(p->elementString()), m_dindent) << ");\n"; << writeString(toString(p->elementString()), m_dindent) << ");\n";
} }

View File

@ -52,11 +52,11 @@ void DatabaseInfo::acceptWidget(DomWidget *node)
{ {
QHash<QString, DomProperty*> properties = propertyMap(node->elementProperty()); QHash<QString, DomProperty*> properties = propertyMap(node->elementProperty());
DomProperty *frameworkCode = properties.value(QLatin1String("frameworkCode"), 0); DomProperty *frameworkCode = properties.value(QLatin1String("frameworkCode"));
if (frameworkCode && toBool(frameworkCode->elementBool()) == false) if (frameworkCode && toBool(frameworkCode->elementBool()) == false)
return; return;
DomProperty *db = properties.value(QLatin1String("database"), 0); DomProperty *db = properties.value(QLatin1String("database"));
if (db && db->elementStringList()) { if (db && db->elementStringList()) {
QStringList info = db->elementStringList()->elementString(); QStringList info = db->elementStringList()->elementString();