2022-08-19 15:59:36 +02:00
|
|
|
// Copyright (C) 2020 The Qt Company Ltd.
|
2022-12-21 10:12:09 +01:00
|
|
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
|
2020-09-18 12:11:40 +02:00
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include <QList>
|
|
|
|
|
#include <QString>
|
2023-04-25 10:15:07 +02:00
|
|
|
#include <QtGlobal>
|
2020-09-18 12:11:40 +02:00
|
|
|
|
2022-08-26 10:30:00 +02:00
|
|
|
#include <optional>
|
|
|
|
|
|
2023-04-25 10:15:07 +02:00
|
|
|
#if defined(UTILS_LIBRARY)
|
|
|
|
|
# define QTCREATOR_UTILS_EXPORT Q_DECL_EXPORT
|
|
|
|
|
#else
|
|
|
|
|
# define QTCREATOR_UTILS_EXPORT Q_DECL_IMPORT
|
|
|
|
|
#endif
|
|
|
|
|
|
2020-09-18 12:11:40 +02:00
|
|
|
QT_BEGIN_NAMESPACE
|
|
|
|
|
class QLayout;
|
2023-04-27 08:24:43 +02:00
|
|
|
class QObject;
|
2020-09-18 12:11:40 +02:00
|
|
|
class QWidget;
|
2023-04-27 08:24:43 +02:00
|
|
|
template <class T> T qobject_cast(QObject *object);
|
2020-09-18 12:11:40 +02:00
|
|
|
QT_END_NAMESPACE
|
|
|
|
|
|
2023-04-25 10:15:07 +02:00
|
|
|
namespace Layouting {
|
2022-07-26 11:35:19 +02:00
|
|
|
|
|
|
|
|
enum AttachType {
|
|
|
|
|
WithMargins,
|
|
|
|
|
WithoutMargins,
|
2022-07-28 11:01:05 +02:00
|
|
|
WithFormAlignment, // Handle Grid similar to QFormLayout, i.e. use special alignment for the first column on Mac
|
2022-07-26 11:35:19 +02:00
|
|
|
};
|
|
|
|
|
|
2023-01-19 13:51:52 +01:00
|
|
|
class LayoutBuilder;
|
2023-04-24 16:40:01 +02:00
|
|
|
class LayoutItem;
|
2023-04-27 08:24:43 +02:00
|
|
|
class Span;
|
2023-04-24 16:40:01 +02:00
|
|
|
|
|
|
|
|
// Special items
|
|
|
|
|
|
|
|
|
|
class QTCREATOR_UTILS_EXPORT Space
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
explicit Space(int space) : space(space) {}
|
|
|
|
|
const int space;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class QTCREATOR_UTILS_EXPORT Stretch
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
explicit Stretch(int stretch = 1) : stretch(stretch) {}
|
|
|
|
|
const int stretch;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
2023-04-27 08:24:43 +02:00
|
|
|
// LayoutItem
|
2023-04-24 16:40:01 +02:00
|
|
|
|
2023-04-27 08:24:43 +02:00
|
|
|
using LayoutItems = QList<LayoutItem>;
|
2022-07-26 11:35:19 +02:00
|
|
|
|
2023-04-27 08:24:43 +02:00
|
|
|
void QTCREATOR_UTILS_EXPORT createItem(LayoutItem *item, const std::function<void(QObject *target)> &t);
|
|
|
|
|
void QTCREATOR_UTILS_EXPORT createItem(LayoutItem *item, QWidget *t);
|
|
|
|
|
void QTCREATOR_UTILS_EXPORT createItem(LayoutItem *item, QLayout *t);
|
|
|
|
|
void QTCREATOR_UTILS_EXPORT createItem(LayoutItem *item, LayoutItem(*t)());
|
|
|
|
|
void QTCREATOR_UTILS_EXPORT createItem(LayoutItem *item, const QString &t);
|
|
|
|
|
void QTCREATOR_UTILS_EXPORT createItem(LayoutItem *item, const Span &t);
|
|
|
|
|
void QTCREATOR_UTILS_EXPORT createItem(LayoutItem *item, const Space &t);
|
|
|
|
|
void QTCREATOR_UTILS_EXPORT createItem(LayoutItem *item, const Stretch &t);
|
2023-01-19 13:51:52 +01:00
|
|
|
|
|
|
|
|
class QTCREATOR_UTILS_EXPORT LayoutItem
|
2020-09-18 12:11:40 +02:00
|
|
|
{
|
|
|
|
|
public:
|
2021-03-11 19:02:42 +01:00
|
|
|
enum class AlignmentType {
|
|
|
|
|
DefaultAlignment,
|
|
|
|
|
AlignAsFormLabel,
|
2021-02-18 14:18:34 +01:00
|
|
|
};
|
2020-10-08 07:24:19 +02:00
|
|
|
|
2022-08-10 17:13:21 +02:00
|
|
|
using Setter = std::function<void(QObject *target)>;
|
2023-04-24 16:40:01 +02:00
|
|
|
|
2023-01-19 13:51:52 +01:00
|
|
|
LayoutItem();
|
2023-04-27 08:24:43 +02:00
|
|
|
~LayoutItem();
|
|
|
|
|
|
|
|
|
|
LayoutItem(const LayoutItem &t);
|
|
|
|
|
LayoutItem &operator=(const LayoutItem &t) = default;
|
2023-04-24 16:40:01 +02:00
|
|
|
|
|
|
|
|
template <class T> LayoutItem(const T &t)
|
|
|
|
|
{
|
2023-04-27 08:24:43 +02:00
|
|
|
if constexpr (std::is_base_of_v<LayoutItem, T>)
|
2023-04-24 16:40:01 +02:00
|
|
|
LayoutItem::operator=(t);
|
2023-04-27 08:24:43 +02:00
|
|
|
else
|
|
|
|
|
createItem(this, t);
|
2023-04-24 16:40:01 +02:00
|
|
|
}
|
|
|
|
|
|
2023-04-27 08:24:43 +02:00
|
|
|
void attachTo(QWidget *w, AttachType attachType = WithMargins) const;
|
|
|
|
|
QWidget *emerge(AttachType attachType = WithMargins);
|
|
|
|
|
|
|
|
|
|
void addItem(const LayoutItem &item);
|
|
|
|
|
void addItems(const LayoutItems &items);
|
|
|
|
|
void addRow(const LayoutItems &items);
|
|
|
|
|
void finishRow();
|
|
|
|
|
|
|
|
|
|
std::function<void(LayoutBuilder &)> onAdd;
|
|
|
|
|
std::function<void(LayoutBuilder &)> onExit;
|
|
|
|
|
std::function<void(QObject *target)> setter;
|
|
|
|
|
LayoutItems subItems;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class QTCREATOR_UTILS_EXPORT Span
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
Span(int span, const LayoutItem &item) : span(span), item(item) {}
|
|
|
|
|
const int span;
|
|
|
|
|
LayoutItem item;
|
|
|
|
|
};
|
2023-01-19 13:51:52 +01:00
|
|
|
|
2023-04-27 08:24:43 +02:00
|
|
|
class QTCREATOR_UTILS_EXPORT Column : public LayoutItem
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
Column(std::initializer_list<LayoutItem> items);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class QTCREATOR_UTILS_EXPORT Row : public LayoutItem
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
Row(std::initializer_list<LayoutItem> items);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class QTCREATOR_UTILS_EXPORT Grid : public LayoutItem
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
Grid() : Grid({}) {}
|
|
|
|
|
Grid(std::initializer_list<LayoutItem> items);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class QTCREATOR_UTILS_EXPORT Form : public LayoutItem
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
Form() : Form({}) {}
|
|
|
|
|
Form(std::initializer_list<LayoutItem> items);
|
|
|
|
|
};
|
2023-01-19 13:51:52 +01:00
|
|
|
|
2023-04-27 08:24:43 +02:00
|
|
|
class QTCREATOR_UTILS_EXPORT Stack : public LayoutItem
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
Stack() : Stack({}) {}
|
|
|
|
|
Stack(std::initializer_list<LayoutItem> items);
|
2023-01-19 13:51:52 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class QTCREATOR_UTILS_EXPORT Tab : public LayoutItem
|
|
|
|
|
{
|
|
|
|
|
public:
|
2023-04-27 08:24:43 +02:00
|
|
|
Tab(const QString &tabName, const LayoutItem &item);
|
2023-01-19 13:51:52 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class QTCREATOR_UTILS_EXPORT Group : public LayoutItem
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
Group(std::initializer_list<LayoutItem> items);
|
|
|
|
|
};
|
|
|
|
|
|
2023-04-25 16:58:21 +02:00
|
|
|
class QTCREATOR_UTILS_EXPORT TextEdit : public LayoutItem
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
TextEdit(std::initializer_list<LayoutItem> items);
|
|
|
|
|
};
|
|
|
|
|
|
2023-01-19 13:51:52 +01:00
|
|
|
class QTCREATOR_UTILS_EXPORT PushButton : public LayoutItem
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
PushButton(std::initializer_list<LayoutItem> items);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class QTCREATOR_UTILS_EXPORT Splitter : public LayoutItem
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
Splitter(std::initializer_list<LayoutItem> items);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class QTCREATOR_UTILS_EXPORT TabWidget : public LayoutItem
|
|
|
|
|
{
|
|
|
|
|
public:
|
2023-04-25 17:21:39 +02:00
|
|
|
TabWidget(std::initializer_list<LayoutItem> items);
|
2023-01-19 13:51:52 +01:00
|
|
|
};
|
|
|
|
|
|
2023-04-27 08:24:43 +02:00
|
|
|
class QTCREATOR_UTILS_EXPORT Application : public LayoutItem
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
Application(std::initializer_list<LayoutItem> items);
|
2022-07-22 18:54:04 +02:00
|
|
|
|
2023-04-27 08:24:43 +02:00
|
|
|
int exec(int &argc, char *argv[]);
|
|
|
|
|
};
|
2023-01-19 13:51:52 +01:00
|
|
|
|
2023-04-27 08:24:43 +02:00
|
|
|
// "Singletons"
|
2023-01-19 13:51:52 +01:00
|
|
|
|
2023-04-27 08:24:43 +02:00
|
|
|
QTCREATOR_UTILS_EXPORT LayoutItem br();
|
|
|
|
|
QTCREATOR_UTILS_EXPORT LayoutItem st();
|
|
|
|
|
QTCREATOR_UTILS_EXPORT LayoutItem empty();
|
|
|
|
|
QTCREATOR_UTILS_EXPORT LayoutItem hr();
|
|
|
|
|
|
|
|
|
|
// "Properties"
|
2023-04-25 17:21:39 +02:00
|
|
|
|
|
|
|
|
QTCREATOR_UTILS_EXPORT LayoutItem title(const QString &title);
|
|
|
|
|
QTCREATOR_UTILS_EXPORT LayoutItem text(const QString &text);
|
|
|
|
|
QTCREATOR_UTILS_EXPORT LayoutItem tooltip(const QString &toolTip);
|
2023-04-27 08:24:43 +02:00
|
|
|
QTCREATOR_UTILS_EXPORT LayoutItem resize(int, int);
|
|
|
|
|
QTCREATOR_UTILS_EXPORT LayoutItem spacing(int);
|
|
|
|
|
QTCREATOR_UTILS_EXPORT LayoutItem windowTitle(const QString &windowTitle);
|
|
|
|
|
QTCREATOR_UTILS_EXPORT LayoutItem onClicked(const std::function<void()> &,
|
|
|
|
|
QObject *guard = nullptr);
|
2023-01-19 13:51:52 +01:00
|
|
|
|
|
|
|
|
// Convenience
|
|
|
|
|
|
|
|
|
|
QTCREATOR_UTILS_EXPORT QWidget *createHr(QWidget *parent = nullptr);
|
|
|
|
|
|
2023-04-27 08:24:43 +02:00
|
|
|
template <class T>
|
|
|
|
|
LayoutItem bindTo(T **out)
|
|
|
|
|
{
|
|
|
|
|
return [out](QObject *target) { *out = qobject_cast<T *>(target); };
|
|
|
|
|
}
|
2023-01-19 13:51:52 +01:00
|
|
|
|
|
|
|
|
// LayoutBuilder
|
|
|
|
|
|
|
|
|
|
class QTCREATOR_UTILS_EXPORT LayoutBuilder
|
|
|
|
|
{
|
2023-04-27 08:24:43 +02:00
|
|
|
Q_DISABLE_COPY_MOVE(LayoutBuilder)
|
2023-04-23 10:02:04 +02:00
|
|
|
|
2023-01-19 13:51:52 +01:00
|
|
|
public:
|
2023-04-27 08:24:43 +02:00
|
|
|
LayoutBuilder();
|
2021-02-18 14:18:34 +01:00
|
|
|
~LayoutBuilder();
|
|
|
|
|
|
2023-04-27 08:24:43 +02:00
|
|
|
void addItem(const LayoutItem &item);
|
|
|
|
|
void addItems(const LayoutItems &items);
|
|
|
|
|
void addRow(const LayoutItems &items);
|
2020-09-18 12:11:40 +02:00
|
|
|
|
2023-04-27 08:24:43 +02:00
|
|
|
bool isForm() const;
|
2021-03-11 19:02:42 +01:00
|
|
|
|
2023-04-27 08:24:43 +02:00
|
|
|
struct Slice;
|
|
|
|
|
QList<Slice> stack;
|
2021-03-11 19:02:42 +01:00
|
|
|
};
|
2021-02-18 14:18:34 +01:00
|
|
|
|
2021-03-11 19:02:42 +01:00
|
|
|
class QTCREATOR_UTILS_EXPORT LayoutExtender : public LayoutBuilder
|
2021-02-18 14:18:34 +01:00
|
|
|
{
|
|
|
|
|
public:
|
2023-04-27 08:24:43 +02:00
|
|
|
explicit LayoutExtender(QLayout *layout, AttachType attachType);
|
2021-03-11 19:02:42 +01:00
|
|
|
~LayoutExtender();
|
2022-09-01 11:20:58 +02:00
|
|
|
};
|
|
|
|
|
|
2023-04-25 10:15:07 +02:00
|
|
|
} // Layouting
|