uic: Refactor reverse name lookup

Add a helper routine to look up the dom classes by attribute name and
change Driver::widgetByName() and Driver::actionByName() to use that
as does Driver::actionGroupByName() (all these functions are called
with names from the XML files).

Remove the name normalization in
WriteInitialization::findDeclaration() and refactor
WriteInitialization::acceptActionRef() to call findOrInsert() to
correctly use the unique name.

Task-number: PYSIDE-797
Change-Id: I34058361964719c442182faf798f055f11b40412
Reviewed-by: Cristian Maureira-Fredes <cristian.maureira-fredes@qt.io>
This commit is contained in:
Friedemann Kleint 2019-01-15 13:31:21 +01:00
parent 758f47eb5d
commit 9cd021f517
3 changed files with 37 additions and 28 deletions

View File

@ -1069,28 +1069,29 @@ void WriteInitialization::acceptActionRef(DomActionRef *node)
}
const QString varName = m_driver->findOrInsertWidget(m_widgetChain.top());
const bool isSeparator = actionName == QLatin1String("separator");
bool isMenu = false;
if (const DomWidget *w = m_driver->widgetByName(actionName)) {
isMenu = m_uic->isMenu(w->attributeClass());
} else if (!(m_driver->actionByName(actionName) || isSeparator)) {
fprintf(stderr, "%s: Warning: action `%s' not declared\n",
qPrintable(m_option.messagePrefix()),
actionName.toLatin1().data());
return;
}
if (m_widgetChain.top() && isSeparator) {
if (m_widgetChain.top() && actionName == QLatin1String("separator")) {
// separator is always reserved!
m_actionOut << m_indent << varName << "->addSeparator();\n";
return;
}
if (isMenu)
actionName += QLatin1String("->menuAction()");
const DomWidget *domWidget = m_driver->widgetByName(actionName);
if (domWidget && m_uic->isMenu(domWidget->attributeClass())) {
m_actionOut << m_indent << varName << "->addAction("
<< m_driver->findOrInsertWidget(domWidget) << "->menuAction());\n";
return;
}
m_actionOut << m_indent << varName << "->addAction(" << actionName << ");\n";
const DomAction *domAction = m_driver->actionByName(actionName);
if (!domAction) {
fprintf(stderr, "%s: Warning: action `%s' not declared\n",
qPrintable(m_option.messagePrefix()), qPrintable(actionName));
return;
}
m_actionOut << m_indent << varName << "->addAction("
<< m_driver->findOrInsertAction(domAction) << ");\n";
}
QString WriteInitialization::writeStringListProperty(const DomStringList *list) const
@ -2430,13 +2431,11 @@ QTextStream &WriteInitialization::autoTrOutput(const DomString *str, const QStri
QString WriteInitialization::findDeclaration(const QString &name)
{
const QString normalized = Driver::normalizedName(name);
if (const DomWidget *widget = m_driver->widgetByName(normalized))
if (const DomWidget *widget = m_driver->widgetByName(name))
return m_driver->findOrInsertWidget(widget);
if (const DomAction *action = m_driver->actionByName(normalized))
if (const DomAction *action = m_driver->actionByName(name))
return m_driver->findOrInsertAction(action);
if (const DomButtonGroup *group = m_driver->findButtonGroup(normalized))
if (const DomButtonGroup *group = m_driver->findButtonGroup(name))
return m_driver->findOrInsertButtonGroup(group);
return QString();
}

View File

@ -50,6 +50,17 @@ static inline QString actionGroupClass() { return QStringLiteral("QActionGroup")
static inline QString actionClass() { return QStringLiteral("QAction"); }
static inline QString buttonGroupClass() { return QStringLiteral("QButtonGroup"); }
template <class DomClass>
const DomClass *Driver::findByAttributeName(const DomObjectHash<DomClass> &domHash,
const QString &name) const
{
for (auto it = domHash.cbegin(), end = domHash.cend(); it != end; ++it) {
if (it.key()->attributeName() == name)
return it.key();
}
return nullptr;
}
template <class DomClass>
QString Driver::findOrInsert(DomObjectHash<DomClass> *domHash, const DomClass *dom,
const QString &className)
@ -111,11 +122,7 @@ QString Driver::findOrInsertButtonGroup(const DomButtonGroup *ui_group)
// Find a group by its non-uniqified name
const DomButtonGroup *Driver::findButtonGroup(const QString &attributeName) const
{
for (auto it = m_buttonGroups.cbegin(), end = m_buttonGroups.cend(); it != end; ++it) {
if (it.key()->attributeName() == attributeName)
return it.key();
}
return nullptr;
return findByAttributeName(m_buttonGroups, attributeName);
}
@ -288,17 +295,17 @@ bool Driver::uic(const QString &fileName, QTextStream *out)
const DomWidget *Driver::widgetByName(const QString &name) const
{
return m_widgets.key(name);
return findByAttributeName(m_widgets, name);
}
const DomActionGroup *Driver::actionGroupByName(const QString &name) const
{
return m_actionGroups.key(name);
return findByAttributeName(m_actionGroups, name);
}
const DomAction *Driver::actionByName(const QString &name) const
{
return m_actions.key(name);
return findByAttributeName(m_actions, name);
}
QT_END_NAMESPACE

View File

@ -94,6 +94,9 @@ public:
private:
template <class DomClass> using DomObjectHash = QHash<const DomClass *, QString>;
template <class DomClass>
const DomClass *findByAttributeName(const DomObjectHash<DomClass> &domHash,
const QString &name) const;
template <class DomClass>
QString findOrInsert(DomObjectHash<DomClass> *domHash, const DomClass *dom, const QString &className);