Layouting: Introduce Ids for Items

Intenally just wrapping a 'bindTo' result, but less trigger potential
for pointer related peladophobia

Change-Id: I25171a2675fb0474ce97c04552ac1cf5ffd6ee56
Reviewed-by: Alessandro Portale <alessandro.portale@qt.io>
Reviewed-by: hjk <hjk@qt.io>
This commit is contained in:
hjk
2023-05-05 15:19:58 +02:00
parent 7581dcd369
commit 62f3d29be4
6 changed files with 60 additions and 24 deletions

View File

@@ -694,6 +694,19 @@ LayoutItem columnStretch(int column, int stretch)
}; };
} }
// Id based setters
LayoutItem id(Id &out)
{
return [&out](QObject *target) { out.ob = target; };
}
void setText(Id id, const QString &text)
{
if (auto textEdit = qobject_cast<QTextEdit *>(id.ob))
textEdit->setText(text);
}
// Signals // Signals
LayoutItem onClicked(const std::function<void ()> &func, QObject *guard) LayoutItem onClicked(const std::function<void ()> &func, QObject *guard)

View File

@@ -190,7 +190,7 @@ QTCREATOR_UTILS_EXPORT LayoutItem noMargin();
QTCREATOR_UTILS_EXPORT LayoutItem normalMargin(); QTCREATOR_UTILS_EXPORT LayoutItem normalMargin();
QTCREATOR_UTILS_EXPORT LayoutItem withFormAlignment(); QTCREATOR_UTILS_EXPORT LayoutItem withFormAlignment();
// "Properties" // "Setters"
QTCREATOR_UTILS_EXPORT LayoutItem title(const QString &title); QTCREATOR_UTILS_EXPORT LayoutItem title(const QString &title);
QTCREATOR_UTILS_EXPORT LayoutItem text(const QString &text); QTCREATOR_UTILS_EXPORT LayoutItem text(const QString &text);
@@ -200,6 +200,19 @@ QTCREATOR_UTILS_EXPORT LayoutItem columnStretch(int column, int stretch);
QTCREATOR_UTILS_EXPORT LayoutItem spacing(int); QTCREATOR_UTILS_EXPORT LayoutItem spacing(int);
QTCREATOR_UTILS_EXPORT LayoutItem windowTitle(const QString &windowTitle); QTCREATOR_UTILS_EXPORT LayoutItem windowTitle(const QString &windowTitle);
// "Getters"
class Id
{
public:
QObject *ob = nullptr;
};
QTCREATOR_UTILS_EXPORT LayoutItem id(Id &out);
QTCREATOR_UTILS_EXPORT void setText(Id id, const QString &text);
// "Signals" // "Signals"
QTCREATOR_UTILS_EXPORT LayoutItem onClicked(const std::function<void()> &, QTCREATOR_UTILS_EXPORT LayoutItem onClicked(const std::function<void()> &,
@@ -209,6 +222,8 @@ QTCREATOR_UTILS_EXPORT LayoutItem onTextChanged(const std::function<void(const Q
QTCREATOR_UTILS_EXPORT LayoutItem onValueChanged(const std::function<void(int)> &, QTCREATOR_UTILS_EXPORT LayoutItem onValueChanged(const std::function<void(int)> &,
QObject *guard = nullptr); QObject *guard = nullptr);
QTCREATOR_UTILS_EXPORT LayoutItem onTextChanged(Id &id, QVariant(*sig)(QObject *));
// Convenience // Convenience
QTCREATOR_UTILS_EXPORT QWidget *createHr(QWidget *parent = nullptr); QTCREATOR_UTILS_EXPORT QWidget *createHr(QWidget *parent = nullptr);
@@ -219,4 +234,5 @@ LayoutItem bindTo(T **out)
return [out](QObject *target) { *out = qobject_cast<T *>(target); }; return [out](QObject *target) { *out = qobject_cast<T *>(target); };
} }
} // Layouting } // Layouting

View File

@@ -48,7 +48,6 @@
using namespace ProjectExplorer; using namespace ProjectExplorer;
using namespace Utils; using namespace Utils;
using namespace Layouting;
namespace CMakeProjectManager { namespace CMakeProjectManager {
@@ -104,7 +103,7 @@ private:
// KitAspectWidget interface // KitAspectWidget interface
void makeReadOnly() override { m_comboBox->setEnabled(false); } void makeReadOnly() override { m_comboBox->setEnabled(false); }
void addToLayout(LayoutItem &builder) override void addToLayout(Layouting::LayoutItem &builder) override
{ {
addMutableAction(m_comboBox); addMutableAction(m_comboBox);
builder.addItem(m_comboBox); builder.addItem(m_comboBox);
@@ -349,7 +348,7 @@ private:
// KitAspectWidget interface // KitAspectWidget interface
void makeReadOnly() override { m_changeButton->setEnabled(false); } void makeReadOnly() override { m_changeButton->setEnabled(false); }
void addToLayout(LayoutItem &parent) override void addToLayout(Layouting::LayoutItem &parent) override
{ {
addMutableAction(m_label); addMutableAction(m_label);
parent.addItem(m_label); parent.addItem(m_label);
@@ -890,7 +889,7 @@ public:
private: private:
// KitAspectWidget interface // KitAspectWidget interface
void addToLayout(LayoutItem &parent) override void addToLayout(Layouting::LayoutItem &parent) override
{ {
addMutableAction(m_summaryLabel); addMutableAction(m_summaryLabel);
parent.addItem(m_summaryLabel); parent.addItem(m_summaryLabel);

View File

@@ -341,12 +341,13 @@ public:
auto addMenu = new QMenu(addButton); auto addMenu = new QMenu(addButton);
addMenu->addAction(Tr::tr("Files in Directories"), this, [this] { addMenu->addAction(Tr::tr("Files in Directories"), this, [this] {
addCustomFilter(new DirectoryFilter(Id(Constants::CUSTOM_DIRECTORY_FILTER_BASEID) addCustomFilter(new DirectoryFilter(Utils::Id(Constants::CUSTOM_DIRECTORY_FILTER_BASEID)
.withSuffix(m_customFilters.size() + 1))); .withSuffix(m_customFilters.size() + 1)));
}); });
addMenu->addAction(Tr::tr("URL Template"), this, [this] { addMenu->addAction(Tr::tr("URL Template"), this, [this] {
auto filter = new UrlLocatorFilter( auto filter = new UrlLocatorFilter(
Id(Constants::CUSTOM_URL_FILTER_BASEID).withSuffix(m_customFilters.size() + 1)); Utils::Id(Constants::CUSTOM_URL_FILTER_BASEID)
.withSuffix(m_customFilters.size() + 1));
filter->setIsCustomFilter(true); filter->setIsCustomFilter(true);
addCustomFilter(filter); addCustomFilter(filter);
}); });

View File

@@ -34,7 +34,6 @@
#include <QVBoxLayout> #include <QVBoxLayout>
using namespace Utils; using namespace Utils;
using namespace Layouting;
namespace ProjectExplorer { namespace ProjectExplorer {
@@ -65,7 +64,7 @@ public:
private: private:
void makeReadOnly() override { m_chooser->setReadOnly(true); } void makeReadOnly() override { m_chooser->setReadOnly(true); }
void addToLayout(LayoutItem &builder) override void addToLayout(Layouting::LayoutItem &builder) override
{ {
addMutableAction(m_chooser); addMutableAction(m_chooser);
builder.addItem(Layouting::Span(2, m_chooser)); builder.addItem(Layouting::Span(2, m_chooser));
@@ -142,7 +141,7 @@ void SysRootKitAspect::addToMacroExpander(Kit *kit, MacroExpander *expander) con
}); });
} }
Id SysRootKitAspect::id() Utils::Id SysRootKitAspect::id()
{ {
return "PE.Profile.SysRoot"; return "PE.Profile.SysRoot";
} }
@@ -231,7 +230,7 @@ public:
} }
private: private:
void addToLayout(LayoutItem &builder) override void addToLayout(Layouting::LayoutItem &builder) override
{ {
addMutableAction(m_mainWidget); addMutableAction(m_mainWidget);
builder.addItem(m_mainWidget); builder.addItem(m_mainWidget);
@@ -760,7 +759,7 @@ public:
~DeviceTypeKitAspectWidget() override { delete m_comboBox; } ~DeviceTypeKitAspectWidget() override { delete m_comboBox; }
private: private:
void addToLayout(LayoutItem &builder) override void addToLayout(Layouting::LayoutItem &builder) override
{ {
addMutableAction(m_comboBox); addMutableAction(m_comboBox);
builder.addItem(m_comboBox); builder.addItem(m_comboBox);
@@ -896,7 +895,7 @@ public:
} }
private: private:
void addToLayout(LayoutItem &builder) override void addToLayout(Layouting::LayoutItem &builder) override
{ {
addMutableAction(m_comboBox); addMutableAction(m_comboBox);
builder.addItem(m_comboBox); builder.addItem(m_comboBox);
@@ -1156,7 +1155,7 @@ public:
} }
private: private:
void addToLayout(LayoutItem &builder) override void addToLayout(Layouting::LayoutItem &builder) override
{ {
addMutableAction(m_comboBox); addMutableAction(m_comboBox);
builder.addItem(m_comboBox); builder.addItem(m_comboBox);
@@ -1387,7 +1386,7 @@ public:
} }
private: private:
void addToLayout(LayoutItem &builder) override void addToLayout(Layouting::LayoutItem &builder) override
{ {
addMutableAction(m_mainWidget); addMutableAction(m_mainWidget);
builder.addItem(m_mainWidget); builder.addItem(m_mainWidget);

View File

@@ -1,12 +1,16 @@
// Copyright (C) 2023 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
#include "layoutbuilder.h" #include "layoutbuilder.h"
#include <QApplication> #include <QApplication>
#include <QDebug>
using namespace Layouting; using namespace Layouting;
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
Id textId;
Application app Application app
{ {
resize(600, 400), resize(600, 400),
@@ -14,19 +18,23 @@ int main(int argc, char *argv[])
Column { Column {
TextEdit { TextEdit {
id(textId),
text("Hallo") text("Hallo")
}, },
Row {
SpinBox { SpinBox {
text("Quit"), onTextChanged([&](const QString &text) { setText(textId, text); })
onTextChanged([](const QString &text) { qDebug() << text; })
}, },
Stretch(),
PushButton { PushButton {
text("Quit"), text("Quit"),
onClicked([] { QApplication::quit(); }) onClicked([] { QApplication::quit(); })
}, },
} }
}
}; };
return app.exec(argc, argv); return app.exec(argc, argv);