From d1c5585faa066a6693c24600cf8a1c42a285fe3b Mon Sep 17 00:00:00 2001 From: Friedemann Kleint Date: Tue, 10 Nov 2009 13:52:03 +0100 Subject: uic/Designer: Use correct margin defaults for custom page-based containers. Designer-formbuilder/uic incorrectly considered custom containers that do not inherit a known page-based container (say QTabWidget) as instances of QLayoutWidget, giving them a default margin of 0. Add a check for the custom widget -element to the checks for QLayoutWidget in uic/formbuilder. In the formbuilder, introduce a new data structure to store custom widget information instead of using 3 separate hashes. Adapt one autotest baseline (Qt 3). Reviewed-by: Jarek Kobus Task-number: QTBUG-5335 --- src/tools/uic/cpp/cppwriteinitialization.cpp | 9 ++-- src/tools/uic/customwidgetsinfo.cpp | 8 +++ src/tools/uic/customwidgetsinfo.h | 2 + src/tools/uic/uic.cpp | 5 ++ src/tools/uic/uic.h | 1 + tests/auto/uic/baseline/mainwindowbase.ui.h | 1 - .../designer/src/lib/uilib/abstractformbuilder.cpp | 19 +------ tools/designer/src/lib/uilib/formbuilder.cpp | 9 +++- tools/designer/src/lib/uilib/formbuilderextra.cpp | 62 ++++++++++++++-------- tools/designer/src/lib/uilib/formbuilderextra_p.h | 23 ++++---- 10 files changed, 84 insertions(+), 55 deletions(-) diff --git a/src/tools/uic/cpp/cppwriteinitialization.cpp b/src/tools/uic/cpp/cppwriteinitialization.cpp index 1690c2e..74a4b9d 100644 --- a/src/tools/uic/cpp/cppwriteinitialization.cpp +++ b/src/tools/uic/cpp/cppwriteinitialization.cpp @@ -670,10 +670,13 @@ void WriteInitialization::acceptWidget(DomWidget *node) m_layoutWidget = false; if (className == QLatin1String("QWidget") && !node->hasAttributeNative()) { - if (m_widgetChain.top() - && m_widgetChain.top()->attributeClass() != QLatin1String("QMainWindow") - && !m_uic->isContainer(m_widgetChain.top()->attributeClass())) + if (const DomWidget* parentWidget = m_widgetChain.top()) { + const QString parentClass = parentWidget->attributeClass(); + if (parentClass != QLatin1String("QMainWindow") + && !m_uic->isCustomWidgetContainer(parentClass) + && !m_uic->isContainer(parentClass)) m_layoutWidget = true; + } } m_widgetChain.push(node); m_layoutChain.push(0); diff --git a/src/tools/uic/customwidgetsinfo.cpp b/src/tools/uic/customwidgetsinfo.cpp index 70adba2..ca3fd19 100644 --- a/src/tools/uic/customwidgetsinfo.cpp +++ b/src/tools/uic/customwidgetsinfo.cpp @@ -88,6 +88,14 @@ bool CustomWidgetsInfo::extends(const QString &classNameIn, const QLatin1String return false; } +bool CustomWidgetsInfo::isCustomWidgetContainer(const QString &className) const +{ + if (const DomCustomWidget *dcw = m_customWidgets.value(className, 0)) + if (dcw->hasElementContainer()) + return dcw->elementContainer() != 0; + return false; +} + QString CustomWidgetsInfo::realClassName(const QString &className) const { if (className == QLatin1String("Line")) diff --git a/src/tools/uic/customwidgetsinfo.h b/src/tools/uic/customwidgetsinfo.h index 1471118..d5be5a2 100644 --- a/src/tools/uic/customwidgetsinfo.h +++ b/src/tools/uic/customwidgetsinfo.h @@ -78,6 +78,8 @@ public: bool extends(const QString &className, const QLatin1String &baseClassName) const; + bool isCustomWidgetContainer(const QString &className) const; + private: typedef QMap NameCustomWidgetMap; NameCustomWidgetMap m_customWidgets; diff --git a/src/tools/uic/uic.cpp b/src/tools/uic/uic.cpp index 32e5e3d..71b6ac1 100644 --- a/src/tools/uic/uic.cpp +++ b/src/tools/uic/uic.cpp @@ -363,6 +363,11 @@ bool Uic::isContainer(const QString &className) const || customWidgetsInfo()->extends(className, QLatin1String("QDockWidget")); } +bool Uic::isCustomWidgetContainer(const QString &className) const +{ + return customWidgetsInfo()->isCustomWidgetContainer(className); +} + bool Uic::isStatusBar(const QString &className) const { return customWidgetsInfo()->extends(className, QLatin1String("QStatusBar")); diff --git a/src/tools/uic/uic.h b/src/tools/uic/uic.h index 3e072a4..5175ea9 100644 --- a/src/tools/uic/uic.h +++ b/src/tools/uic/uic.h @@ -115,6 +115,7 @@ public: bool isStatusBar(const QString &className) const; bool isButton(const QString &className) const; bool isContainer(const QString &className) const; + bool isCustomWidgetContainer(const QString &className) const; bool isMenuBar(const QString &className) const; bool isMenu(const QString &className) const; diff --git a/tests/auto/uic/baseline/mainwindowbase.ui.h b/tests/auto/uic/baseline/mainwindowbase.ui.h index 46270a7..8472dbb 100644 --- a/tests/auto/uic/baseline/mainwindowbase.ui.h +++ b/tests/auto/uic/baseline/mainwindowbase.ui.h @@ -221,7 +221,6 @@ public: hboxLayout->setSpacing(4); hboxLayout->setContentsMargins(8, 8, 8, 8); hboxLayout->setObjectName(QString::fromUtf8("hboxLayout")); - hboxLayout->setContentsMargins(0, 0, 0, 0); helpview = new QTextEdit(widget); helpview->setObjectName(QString::fromUtf8("helpview")); helpview->setMinimumSize(QSize(200, 0)); diff --git a/tools/designer/src/lib/uilib/abstractformbuilder.cpp b/tools/designer/src/lib/uilib/abstractformbuilder.cpp index 24d2c01..eb3dc1c 100644 --- a/tools/designer/src/lib/uilib/abstractformbuilder.cpp +++ b/tools/designer/src/lib/uilib/abstractformbuilder.cpp @@ -285,23 +285,8 @@ void QAbstractFormBuilder::initialize(const DomUI *ui) if (!customWidgets.empty()) { QFormBuilderExtra *formBuilderPrivate = QFormBuilderExtra::instance(this); const DomCustomWidgetList::const_iterator cend = customWidgets.constEnd(); - for (DomCustomWidgetList::const_iterator it = customWidgets.constBegin(); it != cend; ++it) { - const DomCustomWidget *cw = *it; -#ifndef QT_FORMBUILDER_NO_SCRIPT - if (const DomScript *domScript = cw->elementScript()) { - const QString script = domScript->text(); - if (!script.isEmpty()) - formBuilderPrivate->storeCustomWidgetScript(cw->elementClass(), script); - } -#endif - const QString addPageMethod = cw->elementAddPageMethod(); - if (!addPageMethod.isEmpty()) - formBuilderPrivate->storeCustomWidgetAddPageMethod(cw->elementClass(), addPageMethod); - - const QString extends = cw->elementExtends(); - if (!extends.isEmpty()) - formBuilderPrivate->storeCustomWidgetBaseClass(cw->elementClass(), extends); - } + for (DomCustomWidgetList::const_iterator it = customWidgets.constBegin(); it != cend; ++it) + formBuilderPrivate->storeCustomWidgetData((*it)->elementClass(), *it); } } } diff --git a/tools/designer/src/lib/uilib/formbuilder.cpp b/tools/designer/src/lib/uilib/formbuilder.cpp index 0a722ba..2de8388 100644 --- a/tools/designer/src/lib/uilib/formbuilder.cpp +++ b/tools/designer/src/lib/uilib/formbuilder.cpp @@ -123,6 +123,8 @@ QWidget *QFormBuilder::create(DomWidget *ui_widget, QWidget *parentWidget) QFormBuilderExtra *fb = QFormBuilderExtra::instance(this); if (!fb->parentWidgetIsSet()) fb->setParentWidget(parentWidget); + // Is this a QLayoutWidget with a margin of 0: Not a known page-based + // container and no method for adding pages registered. fb->setProcessingLayoutWidget(false); if (ui_widget->attributeClass() == QFormBuilderStrings::instance().qWidgetClass && !ui_widget->hasAttributeNative() && parentWidget @@ -147,8 +149,11 @@ QWidget *QFormBuilder::create(DomWidget *ui_widget, QWidget *parentWidget) #ifndef QT_NO_DOCKWIDGET && !qobject_cast(parentWidget) #endif - ) - fb->setProcessingLayoutWidget(true); + ) { + const QString parentClassName = QLatin1String(parentWidget->metaObject()->className()); + if (!fb->isCustomWidgetContainer(parentClassName)) + fb->setProcessingLayoutWidget(true); + } return QAbstractFormBuilder::create(ui_widget, parentWidget); } diff --git a/tools/designer/src/lib/uilib/formbuilderextra.cpp b/tools/designer/src/lib/uilib/formbuilderextra.cpp index 0e803fb..ce71d59 100644 --- a/tools/designer/src/lib/uilib/formbuilderextra.cpp +++ b/tools/designer/src/lib/uilib/formbuilderextra.cpp @@ -65,6 +65,23 @@ void uiLibWarning(const QString &message) { qWarning("Designer: %s", qPrintable(message)); } + +QFormBuilderExtra::CustomWidgetData::CustomWidgetData() : + isContainer(false) +{ +} + +QFormBuilderExtra::CustomWidgetData::CustomWidgetData(const DomCustomWidget *dcw) : + addPageMethod(dcw->elementAddPageMethod()), + baseClass(dcw->elementExtends()), + isContainer(dcw->hasElementContainer() && dcw->elementContainer() != 0) +{ +#ifndef QT_FORMBUILDER_NO_SCRIPT + if (const DomScript *domScript = dcw->elementScript()) + script = domScript->text(); +#endif +} + QFormBuilderExtra::QFormBuilderExtra() : m_layoutWidget(false), m_resourceBuilder(0), @@ -85,8 +102,8 @@ void QFormBuilderExtra::clear() m_parentWidgetIsSet = false; #ifndef QT_FORMBUILDER_NO_SCRIPT m_FormScriptRunner.clearErrors(); - m_customWidgetScriptHash.clear(); #endif + m_customWidgetDataHash.clear(); m_buttonGroups.clear(); } @@ -160,45 +177,44 @@ QFormScriptRunner &QFormBuilderExtra::formScriptRunner() return m_FormScriptRunner; } -void QFormBuilderExtra::storeCustomWidgetScript(const QString &className, const QString &script) -{ - m_customWidgetScriptHash.insert(className, script); -} - QString QFormBuilderExtra::customWidgetScript(const QString &className) const { - const CustomWidgetScriptHash::const_iterator it = m_customWidgetScriptHash.constFind(className); - if ( it == m_customWidgetScriptHash.constEnd()) - return QString(); - return it.value(); + const QHash::const_iterator it = m_customWidgetDataHash.constFind(className); + if (it != m_customWidgetDataHash.constEnd()) + return it.value().script; + return QString(); } #endif -void QFormBuilderExtra::storeCustomWidgetBaseClass(const QString &className, const QString &baseClassName) +void QFormBuilderExtra::storeCustomWidgetData(const QString &className, const DomCustomWidget *d) { - m_customWidgetBaseClassHash.insert(className, baseClassName); + if (d) + m_customWidgetDataHash.insert(className, CustomWidgetData(d)); } QString QFormBuilderExtra::customWidgetBaseClass(const QString &className) const { - const QHash::const_iterator it = m_customWidgetBaseClassHash.constFind(className); - if (it == m_customWidgetBaseClassHash.constEnd()) - return QString(); - return it.value(); + const QHash::const_iterator it = m_customWidgetDataHash.constFind(className); + if (it != m_customWidgetDataHash.constEnd()) + return it.value().baseClass; + return QString(); } -void QFormBuilderExtra::storeCustomWidgetAddPageMethod(const QString &className, const QString &ct) +QString QFormBuilderExtra::customWidgetAddPageMethod(const QString &className) const { - m_customWidgetAddPageMethodHash.insert(className, ct); + const QHash::const_iterator it = m_customWidgetDataHash.constFind(className); + if (it != m_customWidgetDataHash.constEnd()) + return it.value().addPageMethod; + return QString(); } -QString QFormBuilderExtra::customWidgetAddPageMethod(const QString &className) const +bool QFormBuilderExtra::isCustomWidgetContainer(const QString &className) const { - const QHash::const_iterator it = m_customWidgetAddPageMethodHash.constFind(className); - if (it == m_customWidgetAddPageMethodHash.constEnd()) - return QString(); - return it.value(); + const QHash::const_iterator it = m_customWidgetDataHash.constFind(className); + if (it != m_customWidgetDataHash.constEnd()) + return it.value().isContainer; + return false; } namespace { diff --git a/tools/designer/src/lib/uilib/formbuilderextra_p.h b/tools/designer/src/lib/uilib/formbuilderextra_p.h index ce4d25b..3bcd1eb 100644 --- a/tools/designer/src/lib/uilib/formbuilderextra_p.h +++ b/tools/designer/src/lib/uilib/formbuilderextra_p.h @@ -82,6 +82,7 @@ namespace QFormInternal class DomButtonGroups; class DomButtonGroup; +class DomCustomWidget; class QAbstractFormBuilder; class QResourceBuilder; @@ -92,6 +93,16 @@ class QDESIGNER_UILIB_EXPORT QFormBuilderExtra QFormBuilderExtra(); ~QFormBuilderExtra(); public: + struct CustomWidgetData { + CustomWidgetData(); + explicit CustomWidgetData(const DomCustomWidget *dc); + + QString addPageMethod; + QString script; + QString baseClass; + bool isContainer; + }; + void clear(); bool applyPropertyInternally(QObject *o, const QString &propertyName, const QVariant &value); @@ -107,7 +118,6 @@ public: #ifndef QT_FORMBUILDER_NO_SCRIPT QFormScriptRunner &formScriptRunner(); - void storeCustomWidgetScript(const QString &className, const QString &script); QString customWidgetScript(const QString &className) const; #endif @@ -123,11 +133,10 @@ public: static QFormBuilderExtra *instance(const QAbstractFormBuilder *afb); static void removeInstance(const QAbstractFormBuilder *afb); - void storeCustomWidgetAddPageMethod(const QString &className, const QString &ct); + void storeCustomWidgetData(const QString &className, const DomCustomWidget *d); QString customWidgetAddPageMethod(const QString &className) const; - - void storeCustomWidgetBaseClass(const QString &className, const QString &baseClassName); QString customWidgetBaseClass(const QString &className) const; + bool isCustomWidgetContainer(const QString &className) const; // --- Hash used in creating button groups on demand. Store a map of name and pair of dom group and real group void registerButtonGroups(const DomButtonGroups *groups); @@ -169,13 +178,9 @@ private: #ifndef QT_FORMBUILDER_NO_SCRIPT QFormScriptRunner m_FormScriptRunner; - - typedef QHash CustomWidgetScriptHash; - CustomWidgetScriptHash m_customWidgetScriptHash; #endif - QHash m_customWidgetAddPageMethodHash; - QHash m_customWidgetBaseClassHash; + QHash m_customWidgetDataHash; ButtonGroupHash m_buttonGroups; -- cgit v0.12