forked from qt-creator/qt-creator
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:
@@ -348,15 +348,15 @@ LayoutBuilder &LayoutBuilder::addItem(const LayoutItem &item)
|
|||||||
m_gridLayout->addWidget(widget, m_currentGridRow, m_currentGridColumn, 1, item.span, align);
|
m_gridLayout->addWidget(widget, m_currentGridRow, m_currentGridColumn, 1, item.span, align);
|
||||||
}
|
}
|
||||||
m_currentGridColumn += item.span;
|
m_currentGridColumn += item.span;
|
||||||
if (item.linebreak)
|
if (item.specialType == SpecialType::Break)
|
||||||
finishRow();
|
finishRow();
|
||||||
} else if (m_boxLayout) {
|
} else if (m_boxLayout) {
|
||||||
if (auto widget = item.widget) {
|
if (auto widget = item.widget) {
|
||||||
m_boxLayout->addWidget(widget);
|
m_boxLayout->addWidget(widget);
|
||||||
} else if (item.stretch != 0) {
|
} else if (item.specialType == SpecialType::Stretch) {
|
||||||
m_boxLayout->addStretch(item.stretch);
|
m_boxLayout->addStretch(item.specialValue.toInt());
|
||||||
} else if (item.space != 0) {
|
} else if (item.specialType == SpecialType::Space) {
|
||||||
m_boxLayout->addSpacing(item.space);
|
m_boxLayout->addSpacing(item.specialValue.toInt());
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
m_pendingFormItems.append(item);
|
m_pendingFormItems.append(item);
|
||||||
@@ -383,14 +383,33 @@ void LayoutBuilder::attachTo(QWidget *w, bool stretchAtBottom)
|
|||||||
builder.addItem(Stretch());
|
builder.addItem(Stretch());
|
||||||
}
|
}
|
||||||
|
|
||||||
namespace Layouting {
|
LayoutBuilder::Break::Break()
|
||||||
|
|
||||||
Group::Group(std::initializer_list<LayoutItem> items)
|
|
||||||
: LayoutBuilder(new QGroupBox, VBoxWithMargins)
|
|
||||||
{
|
{
|
||||||
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)
|
Layouting::Group &Layouting::Group::withTitle(const QString &title)
|
||||||
{
|
{
|
||||||
if (auto box = qobject_cast<QGroupBox *>(parentWidget()))
|
if (auto box = qobject_cast<QGroupBox *>(parentWidget()))
|
||||||
@@ -398,6 +417,22 @@ Layouting::Group &Layouting::Group::withTitle(const QString &title)
|
|||||||
return *this;
|
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)
|
Box::Box(LayoutType type, const LayoutItems &items)
|
||||||
: LayoutBuilder(type, items)
|
: LayoutBuilder(type, items)
|
||||||
{}
|
{}
|
||||||
|
@@ -29,6 +29,7 @@
|
|||||||
|
|
||||||
#include <QList>
|
#include <QList>
|
||||||
#include <QString>
|
#include <QString>
|
||||||
|
#include <QVariant>
|
||||||
|
|
||||||
QT_BEGIN_NAMESPACE
|
QT_BEGIN_NAMESPACE
|
||||||
class QBoxLayout;
|
class QBoxLayout;
|
||||||
@@ -58,6 +59,16 @@ public:
|
|||||||
};
|
};
|
||||||
enum Alignment { DefaultAlignment, AlignAsFormLabel };
|
enum Alignment { DefaultAlignment, AlignAsFormLabel };
|
||||||
|
|
||||||
|
enum class SpecialType {
|
||||||
|
NotSpecial,
|
||||||
|
Align,
|
||||||
|
Space,
|
||||||
|
Span,
|
||||||
|
Stretch,
|
||||||
|
Break,
|
||||||
|
Title
|
||||||
|
};
|
||||||
|
|
||||||
class QTCREATOR_UTILS_EXPORT LayoutItem
|
class QTCREATOR_UTILS_EXPORT LayoutItem
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
@@ -72,34 +83,15 @@ public:
|
|||||||
QLayout *layout = nullptr;
|
QLayout *layout = nullptr;
|
||||||
QWidget *widget = nullptr;
|
QWidget *widget = nullptr;
|
||||||
BaseAspect *aspect = nullptr;
|
BaseAspect *aspect = nullptr;
|
||||||
QString text;
|
QString text; // FIXME: Use specialValue for that
|
||||||
int span = 1;
|
int span = 1;
|
||||||
Alignment align;
|
Alignment align;
|
||||||
int space = 0;
|
SpecialType specialType = SpecialType::NotSpecial;
|
||||||
int stretch = 0;
|
QVariant specialValue;
|
||||||
bool linebreak = false;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
using LayoutItems = QList<LayoutItem>;
|
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(QWidget *parent, LayoutType layoutType = Form);
|
||||||
explicit LayoutBuilder(QLayout *layout); // Adds to existing layout.
|
explicit LayoutBuilder(QLayout *layout); // Adds to existing layout.
|
||||||
explicit LayoutBuilder(LayoutType layoutType, const LayoutItems &items = {});
|
explicit LayoutBuilder(LayoutType layoutType, const LayoutItems &items = {});
|
||||||
@@ -123,6 +115,30 @@ public:
|
|||||||
|
|
||||||
void attachTo(QWidget *w, bool stretchAtBottom = true);
|
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:
|
private:
|
||||||
void flushPendingFormItems();
|
void flushPendingFormItems();
|
||||||
void init(QWidget *parent, LayoutType layoutType);
|
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 Stretch = LayoutBuilder::Stretch;
|
||||||
using Space = LayoutBuilder::Space;
|
using Space = LayoutBuilder::Space;
|
||||||
using Break = LayoutBuilder::Break;
|
using Break = LayoutBuilder::Break;
|
||||||
|
using Title = LayoutBuilder::Title;
|
||||||
|
|
||||||
}
|
}
|
||||||
} // namespace Utils
|
} // namespace Utils
|
||||||
|
Reference in New Issue
Block a user