aboutsummaryrefslogtreecommitdiffstats
path: root/src/libs/utils/layoutbuilder.h
blob: 1f774ba1461dde86fc337c15d593081d8e222664 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
// Copyright (C) 2020 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#pragma once

#include <QList>
#include <QString>
#include <QtGlobal>

#include <optional>

#if defined(UTILS_LIBRARY)
#  define QTCREATOR_UTILS_EXPORT Q_DECL_EXPORT
#elif defined(UTILS_STATIC_LIBRARY)
#  define QTCREATOR_UTILS_EXPORT
#else
#  define QTCREATOR_UTILS_EXPORT Q_DECL_IMPORT
#endif

QT_BEGIN_NAMESPACE
class QLayout;
class QObject;
class QWidget;
template <class T> T qobject_cast(QObject *object);
QT_END_NAMESPACE

namespace Layouting {

// LayoutItem

class LayoutBuilder;
class LayoutItem;
using LayoutItems = QList<LayoutItem>;

class QTCREATOR_UTILS_EXPORT LayoutItem
{
public:
    using Setter = std::function<void(QObject *target)>;

    LayoutItem();
    ~LayoutItem();

    LayoutItem(const LayoutItem &t);
    LayoutItem &operator=(const LayoutItem &t) = default;

    template <class T> LayoutItem(const T &t)
    {
        if constexpr (std::is_base_of_v<LayoutItem, T>)
            LayoutItem::operator=(t);
        else
            createItem(this, t);
    }

    void attachTo(QWidget *w) const;
    QWidget *emerge();

    void addItem(const LayoutItem &item);
    void addItems(const LayoutItems &items);
    void addRow(const LayoutItems &items);

    std::function<void(LayoutBuilder &)> onAdd;
    std::function<void(LayoutBuilder &)> onExit;
    std::function<void(QObject *target)> setter;
    LayoutItems subItems;
};

// 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;
};

class QTCREATOR_UTILS_EXPORT Span
{
public:
    Span(int span, const LayoutItem &item) : span(span), item(item) {}
    const int span;
    LayoutItem item;
};

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 Flow : public LayoutItem
{
public:
    Flow(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);
};

class QTCREATOR_UTILS_EXPORT Widget : public LayoutItem
{
public:
    Widget(std::initializer_list<LayoutItem> items);
};

class QTCREATOR_UTILS_EXPORT Stack : public LayoutItem
{
public:
    Stack() : Stack({}) {}
    Stack(std::initializer_list<LayoutItem> items);
};

class QTCREATOR_UTILS_EXPORT Tab : public LayoutItem
{
public:
    Tab(const QString &tabName, const LayoutItem &item);
};

class QTCREATOR_UTILS_EXPORT Group : public LayoutItem
{
public:
    Group(std::initializer_list<LayoutItem> items);
};

class QTCREATOR_UTILS_EXPORT TextEdit : public LayoutItem
{
public:
    TextEdit(std::initializer_list<LayoutItem> items);
};

class QTCREATOR_UTILS_EXPORT PushButton : public LayoutItem
{
public:
    PushButton(std::initializer_list<LayoutItem> items);
};

class QTCREATOR_UTILS_EXPORT SpinBox : public LayoutItem
{
public:
    SpinBox(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:
    TabWidget(std::initializer_list<LayoutItem> items);
};

class QTCREATOR_UTILS_EXPORT Application : public LayoutItem
{
public:
    Application(std::initializer_list<LayoutItem> items);

    int exec(int &argc, char *argv[]);
};


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);


// "Singletons"

QTCREATOR_UTILS_EXPORT LayoutItem br();
QTCREATOR_UTILS_EXPORT LayoutItem st();
QTCREATOR_UTILS_EXPORT LayoutItem empty();
QTCREATOR_UTILS_EXPORT LayoutItem hr();
QTCREATOR_UTILS_EXPORT LayoutItem noMargin();
QTCREATOR_UTILS_EXPORT LayoutItem normalMargin();
QTCREATOR_UTILS_EXPORT LayoutItem withFormAlignment();

// "Setters"

QTCREATOR_UTILS_EXPORT LayoutItem title(const QString &title);
QTCREATOR_UTILS_EXPORT LayoutItem text(const QString &text);
QTCREATOR_UTILS_EXPORT LayoutItem tooltip(const QString &toolTip);
QTCREATOR_UTILS_EXPORT LayoutItem resize(int, int);
QTCREATOR_UTILS_EXPORT LayoutItem columnStretch(int column, int stretch);
QTCREATOR_UTILS_EXPORT LayoutItem spacing(int);
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"

QTCREATOR_UTILS_EXPORT LayoutItem onClicked(const std::function<void()> &,
                                            QObject *guard = nullptr);
QTCREATOR_UTILS_EXPORT LayoutItem onTextChanged(const std::function<void(const QString &)> &,
                                                QObject *guard = nullptr);
QTCREATOR_UTILS_EXPORT LayoutItem onValueChanged(const std::function<void(int)> &,
                                                QObject *guard = nullptr);

QTCREATOR_UTILS_EXPORT LayoutItem onTextChanged(ID &id, QVariant(*sig)(QObject *));

// Convenience

QTCREATOR_UTILS_EXPORT QWidget *createHr(QWidget *parent = nullptr);

template <class T>
LayoutItem bindTo(T **out)
{
    return [out](QObject *target) { *out = qobject_cast<T *>(target); };
}


} // Layouting