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 "utils_global.h"
|
|
|
|
|
|
|
|
|
|
#include <QList>
|
|
|
|
|
#include <QString>
|
2021-03-02 12:48:46 +01:00
|
|
|
#include <QVariant>
|
2020-09-18 12:11:40 +02:00
|
|
|
|
2022-08-26 10:30:00 +02:00
|
|
|
#include <optional>
|
|
|
|
|
|
2020-09-18 12:11:40 +02:00
|
|
|
QT_BEGIN_NAMESPACE
|
|
|
|
|
class QLayout;
|
2023-01-11 14:43:41 +01:00
|
|
|
class QSplitter;
|
2022-11-18 10:44:10 +01:00
|
|
|
class QTabWidget;
|
2020-09-18 12:11:40 +02:00
|
|
|
class QWidget;
|
|
|
|
|
QT_END_NAMESPACE
|
|
|
|
|
|
|
|
|
|
namespace Utils {
|
|
|
|
|
class BaseAspect;
|
2021-03-18 18:23:21 +01:00
|
|
|
class BoolAspect;
|
2023-01-19 13:51:52 +01:00
|
|
|
} // Utils
|
2020-09-18 12:11:40 +02:00
|
|
|
|
2023-01-19 13:51:52 +01:00
|
|
|
namespace Utils::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;
|
2022-07-26 11:35:19 +02:00
|
|
|
|
2023-01-19 13:51:52 +01:00
|
|
|
// LayoutItem
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
2021-03-02 12:48:46 +01:00
|
|
|
enum class SpecialType {
|
|
|
|
|
NotSpecial,
|
|
|
|
|
Space,
|
|
|
|
|
Stretch,
|
|
|
|
|
Break,
|
2022-08-29 17:38:41 +02:00
|
|
|
HorizontalRule,
|
2021-03-02 12:48:46 +01:00
|
|
|
};
|
|
|
|
|
|
2022-08-10 17:13:21 +02:00
|
|
|
using Setter = std::function<void(QObject *target)>;
|
2023-01-19 13:51:52 +01:00
|
|
|
LayoutItem();
|
|
|
|
|
LayoutItem(QLayout *layout);
|
|
|
|
|
LayoutItem(QWidget *widget);
|
|
|
|
|
LayoutItem(BaseAspect *aspect); // Remove
|
|
|
|
|
LayoutItem(BaseAspect &aspect);
|
|
|
|
|
LayoutItem(const QString &text);
|
|
|
|
|
LayoutItem(const LayoutBuilder &builder);
|
|
|
|
|
LayoutItem(const Setter &setter) { this->setter = setter; }
|
|
|
|
|
|
|
|
|
|
QLayout *layout = nullptr;
|
|
|
|
|
QWidget *widget = nullptr;
|
|
|
|
|
BaseAspect *aspect = nullptr;
|
|
|
|
|
|
|
|
|
|
QString text; // FIXME: Use specialValue for that
|
|
|
|
|
int span = 1;
|
|
|
|
|
AlignmentType align = AlignmentType::DefaultAlignment;
|
|
|
|
|
Setter setter;
|
|
|
|
|
SpecialType specialType = SpecialType::NotSpecial;
|
|
|
|
|
QVariant specialValue;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class QTCREATOR_UTILS_EXPORT Space : public LayoutItem
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
explicit Space(int space);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class QTCREATOR_UTILS_EXPORT Span : public LayoutItem
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
Span(int span, const LayoutItem &item);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class QTCREATOR_UTILS_EXPORT Stretch : public LayoutItem
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
explicit Stretch(int stretch = 1);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class QTCREATOR_UTILS_EXPORT Tab : public LayoutItem
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
Tab(const QString &tabName, const LayoutBuilder &item);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class QTCREATOR_UTILS_EXPORT Break : public LayoutItem
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
Break();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class QTCREATOR_UTILS_EXPORT HorizontalRule : public LayoutItem
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
HorizontalRule();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class QTCREATOR_UTILS_EXPORT Group : public LayoutItem
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
Group(std::initializer_list<LayoutItem> items);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
Splitter(QSplitter *splitter, std::initializer_list<LayoutItem> items);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class QTCREATOR_UTILS_EXPORT TabWidget : public LayoutItem
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
TabWidget(std::initializer_list<Tab> tabs);
|
|
|
|
|
TabWidget(QTabWidget *tabWidget, std::initializer_list<Tab> tabs);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Singleton items.
|
2022-07-22 18:54:04 +02:00
|
|
|
|
2023-01-19 13:51:52 +01:00
|
|
|
QTCREATOR_UTILS_EXPORT extern Break br;
|
|
|
|
|
QTCREATOR_UTILS_EXPORT extern Stretch st;
|
|
|
|
|
QTCREATOR_UTILS_EXPORT extern Space empty;
|
|
|
|
|
QTCREATOR_UTILS_EXPORT extern HorizontalRule hr;
|
|
|
|
|
|
|
|
|
|
// "Properties"
|
|
|
|
|
|
|
|
|
|
QTCREATOR_UTILS_EXPORT LayoutItem::Setter title(const QString &title,
|
|
|
|
|
BoolAspect *checker = nullptr);
|
|
|
|
|
|
|
|
|
|
QTCREATOR_UTILS_EXPORT LayoutItem::Setter text(const QString &text);
|
|
|
|
|
QTCREATOR_UTILS_EXPORT LayoutItem::Setter tooltip(const QString &toolTip);
|
|
|
|
|
QTCREATOR_UTILS_EXPORT LayoutItem::Setter onClicked(const std::function<void()> &func,
|
|
|
|
|
QObject *guard = nullptr);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Convenience
|
|
|
|
|
|
|
|
|
|
QTCREATOR_UTILS_EXPORT QWidget *createHr(QWidget *parent = nullptr);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// LayoutBuilder
|
|
|
|
|
|
|
|
|
|
class QTCREATOR_UTILS_EXPORT LayoutBuilder
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
enum LayoutType {
|
|
|
|
|
HBoxLayout,
|
|
|
|
|
VBoxLayout,
|
|
|
|
|
FormLayout,
|
|
|
|
|
GridLayout,
|
|
|
|
|
StackLayout,
|
2021-02-18 14:18:34 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
using LayoutItems = QList<LayoutItem>;
|
|
|
|
|
|
|
|
|
|
explicit LayoutBuilder(LayoutType layoutType, const LayoutItems &items = {});
|
|
|
|
|
|
|
|
|
|
LayoutBuilder(const LayoutBuilder &) = delete;
|
|
|
|
|
LayoutBuilder(LayoutBuilder &&) = default;
|
|
|
|
|
LayoutBuilder &operator=(const LayoutBuilder &) = delete;
|
|
|
|
|
LayoutBuilder &operator=(LayoutBuilder &&) = default;
|
|
|
|
|
|
|
|
|
|
~LayoutBuilder();
|
|
|
|
|
|
2022-02-15 10:41:13 +01:00
|
|
|
LayoutBuilder &setSpacing(int spacing);
|
|
|
|
|
|
2020-09-18 04:54:41 +02:00
|
|
|
LayoutBuilder &addItem(const LayoutItem &item);
|
2021-02-18 14:18:34 +01:00
|
|
|
LayoutBuilder &addItems(const LayoutItems &items);
|
2020-09-18 12:11:40 +02:00
|
|
|
|
2020-09-18 04:54:41 +02:00
|
|
|
LayoutBuilder &finishRow();
|
2020-09-18 12:11:40 +02:00
|
|
|
LayoutBuilder &addRow(const LayoutItem &item);
|
2021-02-18 14:18:34 +01:00
|
|
|
LayoutBuilder &addRow(const LayoutItems &items);
|
2020-09-18 12:11:40 +02:00
|
|
|
|
2021-03-11 19:02:42 +01:00
|
|
|
LayoutType layoutType() const { return m_layoutType; }
|
2021-02-18 14:18:34 +01:00
|
|
|
|
2022-07-26 11:35:19 +02:00
|
|
|
void attachTo(QWidget *w, Layouting::AttachType attachType = Layouting::WithMargins) const;
|
|
|
|
|
QWidget *emerge(Layouting::AttachType attachType = Layouting::WithMargins);
|
2020-09-18 12:11:40 +02:00
|
|
|
|
2021-03-11 19:02:42 +01:00
|
|
|
protected:
|
2023-01-19 13:51:52 +01:00
|
|
|
friend class LayoutItem;
|
|
|
|
|
|
2021-03-11 19:02:42 +01:00
|
|
|
explicit LayoutBuilder(); // Adds to existing layout.
|
2020-09-18 12:11:40 +02:00
|
|
|
|
2022-02-15 10:41:13 +01:00
|
|
|
QLayout *createLayout() const;
|
2022-07-26 11:35:19 +02:00
|
|
|
void doLayout(QWidget *parent, Layouting::AttachType attachType) const;
|
2021-03-11 19:02:42 +01:00
|
|
|
|
|
|
|
|
LayoutItems m_items;
|
|
|
|
|
LayoutType m_layoutType;
|
2022-08-26 10:30:00 +02:00
|
|
|
std::optional<int> m_spacing;
|
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:
|
2022-07-28 11:01:05 +02:00
|
|
|
explicit LayoutExtender(QLayout *layout, Layouting::AttachType attachType);
|
2021-03-11 19:02:42 +01:00
|
|
|
~LayoutExtender();
|
2021-02-18 14:18:34 +01:00
|
|
|
|
2021-03-11 19:02:42 +01:00
|
|
|
private:
|
|
|
|
|
QLayout *m_layout = nullptr;
|
2022-07-28 11:01:05 +02:00
|
|
|
Layouting::AttachType m_attachType = {};
|
2021-02-18 14:18:34 +01:00
|
|
|
};
|
|
|
|
|
|
2021-03-11 19:02:42 +01:00
|
|
|
class QTCREATOR_UTILS_EXPORT Column : public LayoutBuilder
|
2021-02-18 14:18:34 +01:00
|
|
|
{
|
|
|
|
|
public:
|
2021-03-17 08:46:15 +01:00
|
|
|
Column() : LayoutBuilder(VBoxLayout) {}
|
2021-03-11 19:02:42 +01:00
|
|
|
Column(std::initializer_list<LayoutItem> items) : LayoutBuilder(VBoxLayout, items) {}
|
2021-02-18 14:18:34 +01:00
|
|
|
};
|
|
|
|
|
|
2021-03-11 19:02:42 +01:00
|
|
|
class QTCREATOR_UTILS_EXPORT Row : public LayoutBuilder
|
2021-02-18 14:18:34 +01:00
|
|
|
{
|
|
|
|
|
public:
|
2021-03-17 08:46:15 +01:00
|
|
|
Row() : LayoutBuilder(HBoxLayout) {}
|
2021-03-11 19:02:42 +01:00
|
|
|
Row(std::initializer_list<LayoutItem> items) : LayoutBuilder(HBoxLayout, items) {}
|
2021-02-18 14:18:34 +01:00
|
|
|
};
|
|
|
|
|
|
2021-03-11 19:02:42 +01:00
|
|
|
class QTCREATOR_UTILS_EXPORT Grid : public LayoutBuilder
|
2021-02-18 14:18:34 +01:00
|
|
|
{
|
|
|
|
|
public:
|
2021-03-17 08:46:15 +01:00
|
|
|
Grid() : LayoutBuilder(GridLayout) {}
|
2021-03-11 19:02:42 +01:00
|
|
|
Grid(std::initializer_list<LayoutItem> items) : LayoutBuilder(GridLayout, items) {}
|
2021-02-18 14:18:34 +01:00
|
|
|
};
|
|
|
|
|
|
2021-03-11 19:02:42 +01:00
|
|
|
class QTCREATOR_UTILS_EXPORT Form : public LayoutBuilder
|
2021-03-02 12:48:46 +01:00
|
|
|
{
|
|
|
|
|
public:
|
2021-03-17 08:46:15 +01:00
|
|
|
Form() : LayoutBuilder(FormLayout) {}
|
2021-03-11 19:02:42 +01:00
|
|
|
Form(std::initializer_list<LayoutItem> items) : LayoutBuilder(FormLayout, items) {}
|
2021-03-02 12:48:46 +01:00
|
|
|
};
|
|
|
|
|
|
2022-09-01 11:20:58 +02:00
|
|
|
class QTCREATOR_UTILS_EXPORT Stack : public LayoutBuilder
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
Stack() : LayoutBuilder(StackLayout) {}
|
|
|
|
|
Stack(std::initializer_list<LayoutItem> items) : LayoutBuilder(StackLayout, items) {}
|
|
|
|
|
};
|
|
|
|
|
|
2023-01-19 13:51:52 +01:00
|
|
|
} // Utils::Layouting
|