LayoutBuilder: Introduce markers for space and stretch

... and groupbox titles.

Change-Id: Ie57d7857230692f599de8d362c570b9163626dee
Reviewed-by: David Schulz <david.schulz@qt.io>
This commit is contained in:
hjk
2021-03-02 12:48:46 +01:00
parent 6df27ccbe7
commit 687597152e
2 changed files with 93 additions and 32 deletions

View File

@@ -348,15 +348,15 @@ LayoutBuilder &LayoutBuilder::addItem(const LayoutItem &item)
m_gridLayout->addWidget(widget, m_currentGridRow, m_currentGridColumn, 1, item.span, align);
}
m_currentGridColumn += item.span;
if (item.linebreak)
if (item.specialType == SpecialType::Break)
finishRow();
} else if (m_boxLayout) {
if (auto widget = item.widget) {
m_boxLayout->addWidget(widget);
} else if (item.stretch != 0) {
m_boxLayout->addStretch(item.stretch);
} else if (item.space != 0) {
m_boxLayout->addSpacing(item.space);
} else if (item.specialType == SpecialType::Stretch) {
m_boxLayout->addStretch(item.specialValue.toInt());
} else if (item.specialType == SpecialType::Space) {
m_boxLayout->addSpacing(item.specialValue.toInt());
}
} else {
m_pendingFormItems.append(item);
@@ -383,14 +383,33 @@ void LayoutBuilder::attachTo(QWidget *w, bool stretchAtBottom)
builder.addItem(Stretch());
}
namespace Layouting {
Group::Group(std::initializer_list<LayoutItem> items)
: LayoutBuilder(new QGroupBox, VBoxWithMargins)
LayoutBuilder::Break::Break()
{
addItems(items);
specialType = LayoutBuilder::SpecialType::Break;
}
LayoutBuilder::Stretch::Stretch(int stretch)
{
specialType = LayoutBuilder::SpecialType::Stretch;
specialValue = stretch;
}
LayoutBuilder::Space::Space(int space)
{
specialType = LayoutBuilder::SpecialType::Space;
specialValue = space;
}
LayoutBuilder::Title::Title(const QString &title)
{
specialType = LayoutBuilder::SpecialType::Title;
specialValue = title;
}
// FIXME: Decide on which style to use:
// Group { Title(...), child1, child2, ...}; or
// Group { child1, child2, ... }.withTitle(...);
Layouting::Group &Layouting::Group::withTitle(const QString &title)
{
if (auto box = qobject_cast<QGroupBox *>(parentWidget()))
@@ -398,6 +417,22 @@ Layouting::Group &Layouting::Group::withTitle(const QString &title)
return *this;
}
namespace Layouting {
Group::Group(std::initializer_list<LayoutItem> items)
: LayoutBuilder(new QGroupBox, VBoxWithMargins)
{
for (const LayoutItem &item : items) {
if (item.specialType == LayoutBuilder::SpecialType::Title) {
auto box = qobject_cast<QGroupBox *>(parentWidget());
QTC_ASSERT(box, continue);
box->setTitle(item.specialValue.toString());
} else {
addItem(item);
}
}
}
Box::Box(LayoutType type, const LayoutItems &items)
: LayoutBuilder(type, items)
{}

View File

@@ -29,6 +29,7 @@
#include <QList>
#include <QString>
#include <QVariant>
QT_BEGIN_NAMESPACE
class QBoxLayout;
@@ -58,6 +59,16 @@ public:
};
enum Alignment { DefaultAlignment, AlignAsFormLabel };
enum class SpecialType {
NotSpecial,
Align,
Space,
Span,
Stretch,
Break,
Title
};
class QTCREATOR_UTILS_EXPORT LayoutItem
{
public:
@@ -72,34 +83,15 @@ public:
QLayout *layout = nullptr;
QWidget *widget = nullptr;
BaseAspect *aspect = nullptr;
QString text;
QString text; // FIXME: Use specialValue for that
int span = 1;
Alignment align;
int space = 0;
int stretch = 0;
bool linebreak = false;
SpecialType specialType = SpecialType::NotSpecial;
QVariant specialValue;
};
using LayoutItems = QList<LayoutItem>;
class QTCREATOR_UTILS_EXPORT Space : public LayoutItem
{
public:
explicit Space(int space_) { space = space_; }
};
class QTCREATOR_UTILS_EXPORT Stretch : public LayoutItem
{
public:
explicit Stretch(int stretch_ = 1) { stretch = stretch_; }
};
class QTCREATOR_UTILS_EXPORT Break : public LayoutItem
{
public:
Break() { linebreak = true; }
};
explicit LayoutBuilder(QWidget *parent, LayoutType layoutType = Form);
explicit LayoutBuilder(QLayout *layout); // Adds to existing layout.
explicit LayoutBuilder(LayoutType layoutType, const LayoutItems &items = {});
@@ -123,6 +115,30 @@ public:
void attachTo(QWidget *w, bool stretchAtBottom = true);
class QTCREATOR_UTILS_EXPORT Space : public LayoutItem
{
public:
explicit Space(int space);
};
class QTCREATOR_UTILS_EXPORT Stretch : public LayoutItem
{
public:
explicit Stretch(int stretch = 1);
};
class QTCREATOR_UTILS_EXPORT Break : public LayoutItem
{
public:
Break();
};
class QTCREATOR_UTILS_EXPORT Title : public LayoutBuilder::LayoutItem
{
public:
explicit Title(const QString &title);
};
private:
void flushPendingFormItems();
void init(QWidget *parent, LayoutType layoutType);
@@ -175,9 +191,19 @@ public:
{}
};
class QTCREATOR_UTILS_EXPORT Form : public Box
{
public:
Form(std::initializer_list<LayoutItem> items)
: Box(FormLayout, items)
{}
};
using Item = LayoutBuilder::LayoutItem;
using Stretch = LayoutBuilder::Stretch;
using Space = LayoutBuilder::Space;
using Break = LayoutBuilder::Break;
using Title = LayoutBuilder::Title;
}
} // namespace Utils