From 71ec25895f2e2e8813d4a63e29b92d63aa13cc97 Mon Sep 17 00:00:00 2001 From: hjk Date: Fri, 15 Jul 2022 09:24:41 +0200 Subject: [PATCH] Utils: Centralize adding layoutitems in LayoutBuilder As a side-effect, this fixes adding stretch and space in the the dummy vboxes for formlayout fields with more than two items that was accidentally missing before. Change-Id: I18e4755ccd3cd9e1de761b70dc621b17376277dc Reviewed-by: Eike Ziller --- src/libs/utils/layoutbuilder.cpp | 37 +++++++++++++++++--------------- 1 file changed, 20 insertions(+), 17 deletions(-) diff --git a/src/libs/utils/layoutbuilder.cpp b/src/libs/utils/layoutbuilder.cpp index b40e3de5d6f..ecd54e16bc2 100644 --- a/src/libs/utils/layoutbuilder.cpp +++ b/src/libs/utils/layoutbuilder.cpp @@ -160,6 +160,23 @@ static QWidget *widgetForItem(QLayoutItem *item) return nullptr; } +static void addItemToBoxLayout(QBoxLayout *layout, const LayoutBuilder::LayoutItem &item) +{ + if (QWidget *w = item.widget) { + layout->addWidget(w); + } else if (QLayout *l = item.layout) { + layout->addLayout(l); + } else if (item.specialType == LayoutBuilder::SpecialType::Stretch) { + layout->addStretch(item.specialValue.toInt()); + } else if (item.specialType == LayoutBuilder::SpecialType::Space) { + layout->addSpacing(item.specialValue.toInt()); + } else if (!item.text.isEmpty()) { + layout->addWidget(new QLabel(item.text)); + } else { + QTC_CHECK(false); + } +} + static void flushPendingFormItems(QFormLayout *formLayout, LayoutBuilder::LayoutItems &pendingFormItems) { @@ -172,14 +189,8 @@ static void flushPendingFormItems(QFormLayout *formLayout, if (pendingFormItems.size() > 2) { auto hbox = new QHBoxLayout; setMargins(false, hbox); - for (int i = 1; i < pendingFormItems.size(); ++i) { - if (QWidget *w = pendingFormItems.at(i).widget) - hbox->addWidget(w); - else if (QLayout *l = pendingFormItems.at(i).layout) - hbox->addLayout(l); - else - QTC_CHECK(false); - } + for (int i = 1; i < pendingFormItems.size(); ++i) + addItemToBoxLayout(hbox, pendingFormItems.at(i)); while (pendingFormItems.size() >= 2) pendingFormItems.pop_back(); pendingFormItems.append(LayoutBuilder::LayoutItem(hbox)); @@ -256,15 +267,7 @@ static void doLayoutHelper(QLayout *layout, gridLayout->addLayout(item.layout, currentGridRow, currentGridColumn, 1, item.span, align); currentGridColumn += item.span; } else if (boxLayout) { - if (widget) { - boxLayout->addWidget(widget); - } else if (item.layout) { - boxLayout->addLayout(item.layout); - } else if (item.specialType == LayoutBuilder::SpecialType::Stretch) { - boxLayout->addStretch(item.specialValue.toInt()); - } else if (item.specialType == LayoutBuilder::SpecialType::Space) { - boxLayout->addSpacing(item.specialValue.toInt()); - } + addItemToBoxLayout(boxLayout, item); } else { pendingFormItems.append(item); }