forked from qt-creator/qt-creator
Utils: Make LayoutBuilder::Setter a LayoutItem
Weird, but makes GroupBox implementation less special and allows several setters without resorting to using extra {...} there, which would be needed e.g. for title _and_ toolTip. Change-Id: Ie0e64a7bae15825292a473a1e973817be5c27fc4 Reviewed-by: Eike Ziller <eike.ziller@qt.io> Reviewed-by: <github-actions-qt-creator@cristianadam.eu> Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
This commit is contained in:
@@ -467,23 +467,31 @@ namespace Layouting {
|
||||
|
||||
// "Widgets"
|
||||
|
||||
Group::Group(const LayoutBuilder &innerLayout)
|
||||
: Group({}, innerLayout)
|
||||
{}
|
||||
|
||||
Group::Group(const LayoutBuilder::Setters &setters, const LayoutBuilder &innerLayout)
|
||||
static void applyItems(QWidget *widget, const QList<LayoutBuilder::LayoutItem> &items)
|
||||
{
|
||||
widget = new QGroupBox;
|
||||
innerLayout.attachTo(widget, AttachType::WithMargins);
|
||||
for (const LayoutBuilder::Setter &func : setters)
|
||||
func(widget);
|
||||
bool hadLayout = false;
|
||||
for (const LayoutBuilder::LayoutItem &item : items) {
|
||||
if (item.setter) {
|
||||
item.setter(widget);
|
||||
} else if (item.layout && !hadLayout) {
|
||||
hadLayout = true;
|
||||
widget->setLayout(item.layout);
|
||||
} else {
|
||||
QTC_CHECK(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
PushButton::PushButton(std::initializer_list<LayoutBuilder::Setter> setters)
|
||||
Group::Group(std::initializer_list<LayoutBuilder::LayoutItem> items)
|
||||
{
|
||||
widget = new QGroupBox;
|
||||
applyItems(widget, items);
|
||||
}
|
||||
|
||||
PushButton::PushButton(std::initializer_list<LayoutBuilder::LayoutItem> items)
|
||||
{
|
||||
widget = new QPushButton;
|
||||
for (const LayoutBuilder::Setter &func : setters)
|
||||
func(widget);
|
||||
applyItems(widget, items);
|
||||
}
|
||||
|
||||
// "Properties"
|
||||
|
@@ -75,7 +75,7 @@ public:
|
||||
Break,
|
||||
};
|
||||
|
||||
using Modifier = std::function<void(QLayout *)>;
|
||||
using Setter = std::function<void(QObject *target)>;
|
||||
|
||||
class QTCREATOR_UTILS_EXPORT LayoutItem
|
||||
{
|
||||
@@ -87,6 +87,7 @@ public:
|
||||
LayoutItem(BaseAspect &aspect);
|
||||
LayoutItem(const QString &text);
|
||||
LayoutItem(const LayoutBuilder &builder);
|
||||
LayoutItem(const Setter &setter) { this->setter = setter; }
|
||||
|
||||
QLayout *layout = nullptr;
|
||||
QWidget *widget = nullptr;
|
||||
@@ -94,6 +95,8 @@ public:
|
||||
|
||||
QString text; // FIXME: Use specialValue for that
|
||||
int span = 1;
|
||||
AlignmentType align = AlignmentType::DefaultAlignment;
|
||||
Setter setter;
|
||||
SpecialType specialType = SpecialType::NotSpecial;
|
||||
QVariant specialValue;
|
||||
};
|
||||
@@ -147,15 +150,6 @@ public:
|
||||
Break();
|
||||
};
|
||||
|
||||
using Setter = std::function<void(QObject *target)>;
|
||||
|
||||
class QTCREATOR_UTILS_EXPORT Setters : public std::vector<Setter>
|
||||
{
|
||||
public:
|
||||
using std::vector<Setter>::vector;
|
||||
Setters(const Setter &setter) { push_back(setter); }
|
||||
};
|
||||
|
||||
protected:
|
||||
explicit LayoutBuilder(); // Adds to existing layout.
|
||||
|
||||
@@ -191,14 +185,13 @@ QTCREATOR_UTILS_EXPORT LayoutBuilder::Setter onClicked(const std::function<void(
|
||||
class QTCREATOR_UTILS_EXPORT Group : public LayoutBuilder::LayoutItem
|
||||
{
|
||||
public:
|
||||
explicit Group(const LayoutBuilder &innerLayout);
|
||||
Group(const LayoutBuilder::Setters &setters, const LayoutBuilder &innerLayout);
|
||||
Group(std::initializer_list<LayoutItem> items);
|
||||
};
|
||||
|
||||
class QTCREATOR_UTILS_EXPORT PushButton : public LayoutBuilder::LayoutItem
|
||||
{
|
||||
public:
|
||||
PushButton(std::initializer_list<LayoutBuilder::Setter> setters);
|
||||
PushButton(std::initializer_list<LayoutItem> items);
|
||||
};
|
||||
|
||||
class QTCREATOR_UTILS_EXPORT Column : public LayoutBuilder
|
||||
@@ -232,9 +225,6 @@ public:
|
||||
using Space = LayoutBuilder::Space;
|
||||
using Span = LayoutBuilder::Span;
|
||||
|
||||
using Stretch = LayoutBuilder::Stretch; // FIXME: Remove
|
||||
using Break = LayoutBuilder::Break; // FIXME: Remove
|
||||
|
||||
QTCREATOR_UTILS_EXPORT extern LayoutBuilder::Break br;
|
||||
QTCREATOR_UTILS_EXPORT extern LayoutBuilder::Stretch st;
|
||||
QTCREATOR_UTILS_EXPORT extern LayoutBuilder::Space empty;
|
||||
|
Reference in New Issue
Block a user