From 8c47c2a08ec69fe45a14f6c2334befcb2e074790 Mon Sep 17 00:00:00 2001 From: Friedemann Kleint Date: Fri, 2 Nov 2018 15:20:43 +0100 Subject: uic: Refactor CustomWidgetsInfo::extends() Add a extendsOneOf() helper that takes a QStringList to be searched and simplify the code accordingly. In WriteInitialization::acceptWidget(), move the variable CustomWidgetsInfo *cwi up and reuse everywhere to shorten code. Task-number: PYSIDE-797 Change-Id: I331e135b6aa58dbbd413ca151eb67b3eb92f09c6 Reviewed-by: Jarek Kobus --- src/tools/uic/cpp/cppwriteincludes.cpp | 12 +++---- src/tools/uic/cpp/cppwriteinitialization.cpp | 54 ++++++++++++++++------------ src/tools/uic/customwidgetsinfo.cpp | 18 ++++++++++ src/tools/uic/customwidgetsinfo.h | 1 + src/tools/uic/uic.cpp | 32 +++++++++-------- 5 files changed, 75 insertions(+), 42 deletions(-) diff --git a/src/tools/uic/cpp/cppwriteincludes.cpp b/src/tools/uic/cpp/cppwriteincludes.cpp index 74546bb01d..0ba49627c0 100644 --- a/src/tools/uic/cpp/cppwriteincludes.cpp +++ b/src/tools/uic/cpp/cppwriteincludes.cpp @@ -214,14 +214,14 @@ void WriteIncludes::add(const QString &className, bool determineHeader, const QS m_knownClasses.insert(className); const CustomWidgetsInfo *cwi = m_uic->customWidgetsInfo(); - if (cwi->extends(className, QLatin1String("QTreeView")) - || cwi->extends(className, QLatin1String("QTreeWidget")) - || cwi->extends(className, QLatin1String("QTableView")) - || cwi->extends(className, QLatin1String("QTableWidget"))) { + static const QStringList treeViewsWithHeaders = { + QLatin1String("QTreeView"), QLatin1String("QTreeWidget"), + QLatin1String("QTableView"), QLatin1String("QTableWidget") + }; + if (cwi->extendsOneOf(className, treeViewsWithHeaders)) add(QLatin1String("QHeaderView")); - } - if (!m_laidOut && m_uic->customWidgetsInfo()->extends(className, QLatin1String("QToolBox"))) + if (!m_laidOut && cwi->extends(className, QLatin1String("QToolBox"))) add(QLatin1String("QLayout")); // spacing property of QToolBox) if (className == QLatin1String("Line")) { // ### hmm, deprecate me! diff --git a/src/tools/uic/cpp/cppwriteinitialization.cpp b/src/tools/uic/cpp/cppwriteinitialization.cpp index 9db20a5395..dbe4860d7e 100644 --- a/src/tools/uic/cpp/cppwriteinitialization.cpp +++ b/src/tools/uic/cpp/cppwriteinitialization.cpp @@ -608,18 +608,23 @@ void WriteInitialization::acceptWidget(DomWidget *node) if (m_uic->isContainer(parentClass)) parentWidget.clear(); - if (m_widgetChain.size() != 1) - m_output << m_indent << varName << " = new " << m_uic->customWidgetsInfo()->realClassName(className) << '(' << parentWidget << ");\n"; + const auto *cwi = m_uic->customWidgetsInfo(); + + if (m_widgetChain.size() != 1) { + m_output << m_indent << varName << " = new " << cwi->realClassName(className) + << '(' << parentWidget << ");\n"; + } parentWidget = savedParentWidget; - if (m_uic->customWidgetsInfo()->extends(className, QLatin1String("QComboBox"))) { + + if (cwi->extends(className, QLatin1String("QComboBox"))) { initializeComboBox(node); - } else if (m_uic->customWidgetsInfo()->extends(className, QLatin1String("QListWidget"))) { + } else if (cwi->extends(className, QLatin1String("QListWidget"))) { initializeListWidget(node); - } else if (m_uic->customWidgetsInfo()->extends(className, QLatin1String("QTreeWidget"))) { + } else if (cwi->extends(className, QLatin1String("QTreeWidget"))) { initializeTreeWidget(node); - } else if (m_uic->customWidgetsInfo()->extends(className, QLatin1String("QTableWidget"))) { + } else if (cwi->extends(className, QLatin1String("QTableWidget"))) { initializeTableWidget(node); } @@ -629,7 +634,7 @@ void WriteInitialization::acceptWidget(DomWidget *node) writeProperties(varName, className, node->elementProperty()); if (!parentWidget.isEmpty() - && m_uic->customWidgetsInfo()->extends(className, QLatin1String("QMenu"))) { + && cwi->extends(className, QLatin1String("QMenu"))) { initializeMenu(node, parentWidget); } @@ -657,10 +662,10 @@ void WriteInitialization::acceptWidget(DomWidget *node) const QString pageDefaultString = QLatin1String("Page"); - if (m_uic->customWidgetsInfo()->extends(parentClass, QLatin1String("QMainWindow"))) { - if (m_uic->customWidgetsInfo()->extends(className, QLatin1String("QMenuBar"))) { + if (cwi->extends(parentClass, QLatin1String("QMainWindow"))) { + if (cwi->extends(className, QLatin1String("QMenuBar"))) { m_output << m_indent << parentWidget << "->setMenuBar(" << varName <<");\n"; - } else if (m_uic->customWidgetsInfo()->extends(className, QLatin1String("QToolBar"))) { + } else if (cwi->extends(className, QLatin1String("QToolBar"))) { m_output << m_indent << parentWidget << "->addToolBar(" << toolBarAreaStringFromDOMAttributes(attributes) << varName << ");\n"; @@ -670,7 +675,7 @@ void WriteInitialization::acceptWidget(DomWidget *node) } } - } else if (m_uic->customWidgetsInfo()->extends(className, QLatin1String("QDockWidget"))) { + } else if (cwi->extends(className, QLatin1String("QDockWidget"))) { m_output << m_indent << parentWidget << "->addDockWidget("; if (DomProperty *pstyle = attributes.value(QLatin1String("dockWidgetArea"))) m_output << "Qt::" << language::dockWidgetArea(pstyle->elementNumber()) << ", "; @@ -683,9 +688,9 @@ void WriteInitialization::acceptWidget(DomWidget *node) } // Check for addPageMethod of a custom plugin first - QString addPageMethod = m_uic->customWidgetsInfo()->customWidgetAddPageMethod(parentClass); + QString addPageMethod = cwi->customWidgetAddPageMethod(parentClass); if (addPageMethod.isEmpty()) - addPageMethod = m_uic->customWidgetsInfo()->simpleContainerAddPageMethod(parentClass); + addPageMethod = cwi->simpleContainerAddPageMethod(parentClass); if (!addPageMethod.isEmpty()) { m_output << m_indent << parentWidget << "->" << addPageMethod << '(' << varName << ");\n"; } else if (m_uic->customWidgetsInfo()->extends(parentClass, QLatin1String("QWizard"))) { @@ -753,8 +758,14 @@ void WriteInitialization::acceptWidget(DomWidget *node) QLatin1String("stretchLastSection"), }; - if (m_uic->customWidgetsInfo()->extends(className, QLatin1String("QTreeView")) - || m_uic->customWidgetsInfo()->extends(className, QLatin1String("QTreeWidget"))) { + static const QStringList trees = { + QLatin1String("QTreeView"), QLatin1String("QTreeWidget") + }; + static const QStringList tables = { + QLatin1String("QTableView"), QLatin1String("QTableWidget") + }; + + if (cwi->extendsOneOf(className, trees)) { DomPropertyList headerProperties; for (auto realPropertyName : realPropertyNames) { const QString fakePropertyName = QLatin1String("header") @@ -767,9 +778,7 @@ void WriteInitialization::acceptWidget(DomWidget *node) writeProperties(varName + QLatin1String("->header()"), QLatin1String("QHeaderView"), headerProperties, WritePropertyIgnoreObjectName); - } else if (m_uic->customWidgetsInfo()->extends(className, QLatin1String("QTableView")) - || m_uic->customWidgetsInfo()->extends(className, QLatin1String("QTableWidget"))) { - + } else if (cwi->extendsOneOf(className, tables)) { static const QLatin1String headerPrefixes[] = { QLatin1String("horizontalHeader"), QLatin1String("verticalHeader"), @@ -1166,11 +1175,12 @@ void WriteInitialization::writeProperties(const QString &varName, << p->elementNumber() << ");\n"; continue; } + static const QStringList currentIndexWidgets = { + QLatin1String("QComboBox"), QLatin1String("QStackedWidget"), + QLatin1String("QTabWidget"), QLatin1String("QToolBox") + }; if (propertyName == QLatin1String("currentIndex") // set currentIndex later - && (m_uic->customWidgetsInfo()->extends(className, QLatin1String("QComboBox")) - || m_uic->customWidgetsInfo()->extends(className, QLatin1String("QStackedWidget")) - || m_uic->customWidgetsInfo()->extends(className, QLatin1String("QTabWidget")) - || m_uic->customWidgetsInfo()->extends(className, QLatin1String("QToolBox")))) { + && (m_uic->customWidgetsInfo()->extendsOneOf(className, currentIndexWidgets))) { m_delayedOut << m_indent << varName << "->setCurrentIndex(" << p->elementNumber() << ");\n"; continue; diff --git a/src/tools/uic/customwidgetsinfo.cpp b/src/tools/uic/customwidgetsinfo.cpp index 4afdf74d08..d6a409152b 100644 --- a/src/tools/uic/customwidgetsinfo.cpp +++ b/src/tools/uic/customwidgetsinfo.cpp @@ -75,6 +75,24 @@ bool CustomWidgetsInfo::extends(const QString &classNameIn, QLatin1String baseCl return false; } +bool CustomWidgetsInfo::extendsOneOf(const QString &classNameIn, + const QStringList &baseClassNames) const +{ + if (baseClassNames.contains(classNameIn)) + return true; + + QString className = classNameIn; + while (const DomCustomWidget *c = customWidget(className)) { + const QString extends = c->elementExtends(); + if (className == extends) // Faulty legacy custom widget entries exist. + return false; + if (baseClassNames.contains(extends)) + return true; + className = extends; + } + return false; +} + bool CustomWidgetsInfo::isCustomWidgetContainer(const QString &className) const { if (const DomCustomWidget *dcw = m_customWidgets.value(className, 0)) diff --git a/src/tools/uic/customwidgetsinfo.h b/src/tools/uic/customwidgetsinfo.h index a4278b1aca..8a10999027 100644 --- a/src/tools/uic/customwidgetsinfo.h +++ b/src/tools/uic/customwidgetsinfo.h @@ -57,6 +57,7 @@ public: QString realClassName(const QString &className) const; bool extends(const QString &className, QLatin1String baseClassName) const; + bool extendsOneOf(const QString &className, const QStringList &baseClassNames) const; bool isCustomWidgetContainer(const QString &className) const; diff --git a/src/tools/uic/uic.cpp b/src/tools/uic/uic.cpp index b426d33e5c..225dc6aeb2 100644 --- a/src/tools/uic/uic.cpp +++ b/src/tools/uic/uic.cpp @@ -245,28 +245,32 @@ void Uic::writeHeaderProtectionEnd() bool Uic::isButton(const QString &className) const { - return customWidgetsInfo()->extends(className, QLatin1String("QRadioButton")) - || customWidgetsInfo()->extends(className, QLatin1String("QToolButton")) - || customWidgetsInfo()->extends(className, QLatin1String("QCheckBox")) - || customWidgetsInfo()->extends(className, QLatin1String("QPushButton")) - || customWidgetsInfo()->extends(className, QLatin1String("QCommandLinkButton")); + static const QStringList buttons = { + QLatin1String("QRadioButton"), QLatin1String("QToolButton"), + QLatin1String("QCheckBox"), QLatin1String("QPushButton"), + QLatin1String("QCommandLinkButton") + }; + return customWidgetsInfo()->extendsOneOf(className, buttons); } bool Uic::isContainer(const QString &className) const { - return customWidgetsInfo()->extends(className, QLatin1String("QStackedWidget")) - || customWidgetsInfo()->extends(className, QLatin1String("QToolBox")) - || customWidgetsInfo()->extends(className, QLatin1String("QTabWidget")) - || customWidgetsInfo()->extends(className, QLatin1String("QScrollArea")) - || customWidgetsInfo()->extends(className, QLatin1String("QMdiArea")) - || customWidgetsInfo()->extends(className, QLatin1String("QWizard")) - || customWidgetsInfo()->extends(className, QLatin1String("QDockWidget")); + static const QStringList containers = { + QLatin1String("QStackedWidget"), QLatin1String("QToolBox"), + QLatin1String("QTabWidget"), QLatin1String("QScrollArea"), + QLatin1String("QMdiArea"), QLatin1String("QWizard"), + QLatin1String("QDockWidget") + }; + + return customWidgetsInfo()->extendsOneOf(className, containers); } bool Uic::isMenu(const QString &className) const { - return customWidgetsInfo()->extends(className, QLatin1String("QMenu")) - || customWidgetsInfo()->extends(className, QLatin1String("QPopupMenu")); + static const QStringList menus = { + QLatin1String("QMenu"), QLatin1String("QPopupMenu") + }; + return customWidgetsInfo()->extendsOneOf(className, menus); } QT_END_NAMESPACE -- cgit v1.2.3