LayoutBuilder: Add support for VBoxLayout and HBoxLayout

Change-Id: Ieeef8244a05ffb1b642843c6471f92e2b154cf8a
Reviewed-by: David Schulz <david.schulz@qt.io>
This commit is contained in:
hjk
2021-02-18 14:18:34 +01:00
parent 0c1d44604a
commit 434c624b6f
2 changed files with 222 additions and 21 deletions

View File

@@ -31,6 +31,7 @@
#include <QString>
QT_BEGIN_NAMESPACE
class QBoxLayout;
class QFormLayout;
class QGridLayout;
class QLayout;
@@ -44,22 +45,29 @@ class BaseAspect;
class QTCREATOR_UTILS_EXPORT LayoutBuilder
{
public:
enum LayoutType { GridLayout, FormLayout };
enum LayoutType {
Form, // Plain QFormLayout, without contentMargins
Grid, // Plain QGridLayout, without contentMargins
HBox, // Plain QHBoxLayout, without contentMargins
VBox, // Plain QVBoxLayout, without contentMargins
HBoxWithMargins, // QHBoxLayout with margins
VBoxWithMargins, // QVBoxLayout with margins
// Compat
FormLayout = Form, // FIXME: Remove
GridLayout = Grid, // FIXME: Remove
};
enum Alignment { DefaultAlignment, AlignAsFormLabel };
explicit LayoutBuilder(QWidget *parent, LayoutType layoutType = FormLayout);
explicit LayoutBuilder(QLayout *layout); // Adds to existing layout.
~LayoutBuilder();
class QTCREATOR_UTILS_EXPORT LayoutItem
{
public:
LayoutItem();
LayoutItem(QLayout *layout, int span = 1, Alignment align = {});
LayoutItem(QWidget *widget, int span = 1, Alignment align = {});
LayoutItem(BaseAspect *aspect);
LayoutItem(const QString &text);
LayoutItem(BaseAspect *aspect, int span = 1, Alignment align = {}); // Remove
LayoutItem(BaseAspect &aspect, int span = 1, Alignment align = {});
LayoutItem(const QString &text, int span = 1, Alignment align = {});
LayoutItem(const LayoutBuilder &builder, int span = 1, Alignment align = {});
QLayout *layout = nullptr;
QWidget *widget = nullptr;
@@ -67,25 +75,109 @@ public:
QString text;
int span = 1;
Alignment align;
int space = 0;
int stretch = 0;
bool linebreak = false;
};
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 = {});
LayoutBuilder(const LayoutBuilder &) = delete;
LayoutBuilder(LayoutBuilder &&) = default;
LayoutBuilder &operator=(const LayoutBuilder &) = delete;
LayoutBuilder &operator=(LayoutBuilder &&) = default;
~LayoutBuilder();
LayoutBuilder &addItem(const LayoutItem &item);
LayoutBuilder &addItems(const QList<LayoutItem> &items);
LayoutBuilder &addItems(const LayoutItems &items);
LayoutBuilder &finishRow();
LayoutBuilder &addRow(const LayoutItem &item);
LayoutBuilder &addRow(const QList<LayoutItem> &items);
LayoutBuilder &addRow(const LayoutItems &items);
QLayout *layout() const;
QWidget *parentWidget() const;
void attachTo(QWidget *w, bool stretchAtBottom = true);
private:
void flushPendingFormItems();
void init(QWidget *parent, LayoutType layoutType);
QFormLayout *m_formLayout = nullptr;
QGridLayout *m_gridLayout = nullptr;
QList<LayoutItem> m_pendingFormItems;
QBoxLayout *m_boxLayout = nullptr;
LayoutItems m_pendingFormItems;
int m_currentGridRow = 0;
int m_currentGridColumn = 0;
};
namespace Layouting {
class QTCREATOR_UTILS_EXPORT Group : public LayoutBuilder
{
public:
Group(std::initializer_list<LayoutBuilder::LayoutItem> items);
Group &withTitle(const QString &title);
};
class QTCREATOR_UTILS_EXPORT Box : public LayoutBuilder
{
public:
Box(LayoutType type, const LayoutItems &items);
};
class QTCREATOR_UTILS_EXPORT Column : public Box
{
public:
Column(std::initializer_list<LayoutItem> items)
: Box(VBox, items)
{}
};
class QTCREATOR_UTILS_EXPORT Row : public Box
{
public:
Row(std::initializer_list<LayoutItem> items)
: Box(HBox, items)
{}
};
class QTCREATOR_UTILS_EXPORT Grid : public Box
{
public:
Grid(std::initializer_list<LayoutItem> items)
: Box(GridLayout, items)
{}
};
using Stretch = LayoutBuilder::Stretch;
using Space = LayoutBuilder::Space;
using Break = LayoutBuilder::Break;
}
} // namespace Utils