layoutbuilder.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. // Copyright (C) 2020 The Qt Company Ltd.
  2. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
  3. #pragma once
  4. #include <QFormLayout>
  5. #include <QList>
  6. #include <QString>
  7. #include <QtGlobal>
  8. #include <optional>
  9. #define QTCREATOR_UTILS_EXPORT
  10. QT_BEGIN_NAMESPACE
  11. class QLayout;
  12. class QMargins;
  13. class QObject;
  14. class QWidget;
  15. template<class T>
  16. T qobject_cast(QObject *object);
  17. QT_END_NAMESPACE
  18. namespace Layouting {
  19. // LayoutItem
  20. class LayoutBuilder;
  21. class LayoutItem;
  22. using LayoutItems = QList<LayoutItem>;
  23. class QTCREATOR_UTILS_EXPORT LayoutItem
  24. {
  25. public:
  26. using Setter = std::function<void(QObject *target)>;
  27. LayoutItem();
  28. ~LayoutItem();
  29. LayoutItem(const LayoutItem &t);
  30. LayoutItem &operator=(const LayoutItem &t) = default;
  31. template<class T>
  32. LayoutItem(const T &t)
  33. {
  34. if constexpr (std::is_base_of_v<LayoutItem, T>)
  35. LayoutItem::operator=(t);
  36. else
  37. createItem(this, t);
  38. }
  39. void attachTo(QWidget *w) const;
  40. QWidget *emerge();
  41. void addItem(const LayoutItem &item);
  42. void addItems(const LayoutItems &items);
  43. void addRow(const LayoutItems &items);
  44. std::function<void(LayoutBuilder &)> onAdd;
  45. std::function<void(LayoutBuilder &)> onExit;
  46. std::function<void(QObject *target)> setter;
  47. LayoutItems subItems;
  48. };
  49. // Special items
  50. class QTCREATOR_UTILS_EXPORT Space
  51. {
  52. public:
  53. explicit Space(int space)
  54. : space(space)
  55. {}
  56. const int space;
  57. };
  58. class QTCREATOR_UTILS_EXPORT Stretch
  59. {
  60. public:
  61. explicit Stretch(int stretch = 1)
  62. : stretch(stretch)
  63. {}
  64. const int stretch;
  65. };
  66. class QTCREATOR_UTILS_EXPORT Span
  67. {
  68. public:
  69. Span(int span, const LayoutItem &item)
  70. : span(span)
  71. , item(item)
  72. {}
  73. const int span;
  74. LayoutItem item;
  75. };
  76. class QTCREATOR_UTILS_EXPORT Column : public LayoutItem
  77. {
  78. public:
  79. Column(std::initializer_list<LayoutItem> items);
  80. };
  81. class QTCREATOR_UTILS_EXPORT Row : public LayoutItem
  82. {
  83. public:
  84. Row(std::initializer_list<LayoutItem> items);
  85. };
  86. class QTCREATOR_UTILS_EXPORT Flow : public LayoutItem
  87. {
  88. public:
  89. Flow(std::initializer_list<LayoutItem> items);
  90. };
  91. class QTCREATOR_UTILS_EXPORT Grid : public LayoutItem
  92. {
  93. public:
  94. Grid()
  95. : Grid({})
  96. {}
  97. Grid(std::initializer_list<LayoutItem> items);
  98. };
  99. class QTCREATOR_UTILS_EXPORT Form : public LayoutItem
  100. {
  101. public:
  102. Form()
  103. : Form({})
  104. {}
  105. Form(std::initializer_list<LayoutItem> items);
  106. };
  107. class QTCREATOR_UTILS_EXPORT Widget : public LayoutItem
  108. {
  109. public:
  110. Widget(std::initializer_list<LayoutItem> items);
  111. };
  112. class QTCREATOR_UTILS_EXPORT Stack : public LayoutItem
  113. {
  114. public:
  115. Stack()
  116. : Stack({})
  117. {}
  118. Stack(std::initializer_list<LayoutItem> items);
  119. };
  120. class QTCREATOR_UTILS_EXPORT Tab : public LayoutItem
  121. {
  122. public:
  123. Tab(const QString &tabName, const LayoutItem &item);
  124. };
  125. class QTCREATOR_UTILS_EXPORT If : public LayoutItem
  126. {
  127. public:
  128. If(bool condition, const LayoutItems &item, const LayoutItems &other = {});
  129. };
  130. class QTCREATOR_UTILS_EXPORT Group : public LayoutItem
  131. {
  132. public:
  133. Group(std::initializer_list<LayoutItem> items);
  134. };
  135. class QTCREATOR_UTILS_EXPORT TextEdit : public LayoutItem
  136. {
  137. public:
  138. TextEdit(std::initializer_list<LayoutItem> items);
  139. };
  140. class QTCREATOR_UTILS_EXPORT PushButton : public LayoutItem
  141. {
  142. public:
  143. PushButton(std::initializer_list<LayoutItem> items);
  144. };
  145. class QTCREATOR_UTILS_EXPORT SpinBox : public LayoutItem
  146. {
  147. public:
  148. SpinBox(std::initializer_list<LayoutItem> items);
  149. };
  150. class QTCREATOR_UTILS_EXPORT Splitter : public LayoutItem
  151. {
  152. public:
  153. Splitter(std::initializer_list<LayoutItem> items);
  154. };
  155. class QTCREATOR_UTILS_EXPORT ToolBar : public LayoutItem
  156. {
  157. public:
  158. ToolBar(std::initializer_list<LayoutItem> items);
  159. };
  160. class QTCREATOR_UTILS_EXPORT TabWidget : public LayoutItem
  161. {
  162. public:
  163. TabWidget(std::initializer_list<LayoutItem> items);
  164. };
  165. class QTCREATOR_UTILS_EXPORT Application : public LayoutItem
  166. {
  167. public:
  168. Application(std::initializer_list<LayoutItem> items);
  169. int exec(int &argc, char *argv[]);
  170. };
  171. void QTCREATOR_UTILS_EXPORT createItem(LayoutItem *item,
  172. const std::function<void(QObject *target)> &t);
  173. void QTCREATOR_UTILS_EXPORT createItem(LayoutItem *item, QWidget *t);
  174. void QTCREATOR_UTILS_EXPORT createItem(LayoutItem *item, QLayout *t);
  175. void QTCREATOR_UTILS_EXPORT createItem(LayoutItem *item, LayoutItem (*t)());
  176. void QTCREATOR_UTILS_EXPORT createItem(LayoutItem *item, const QString &t);
  177. void QTCREATOR_UTILS_EXPORT createItem(LayoutItem *item, const Span &t);
  178. void QTCREATOR_UTILS_EXPORT createItem(LayoutItem *item, const Space &t);
  179. void QTCREATOR_UTILS_EXPORT createItem(LayoutItem *item, const Stretch &t);
  180. // "Singletons"
  181. QTCREATOR_UTILS_EXPORT LayoutItem br();
  182. QTCREATOR_UTILS_EXPORT LayoutItem st();
  183. QTCREATOR_UTILS_EXPORT LayoutItem empty();
  184. QTCREATOR_UTILS_EXPORT LayoutItem hr();
  185. QTCREATOR_UTILS_EXPORT LayoutItem noMargin();
  186. QTCREATOR_UTILS_EXPORT LayoutItem normalMargin();
  187. QTCREATOR_UTILS_EXPORT LayoutItem customMargin(const QMargins &margin);
  188. QTCREATOR_UTILS_EXPORT LayoutItem withFormAlignment();
  189. // "Setters"
  190. QTCREATOR_UTILS_EXPORT LayoutItem title(const QString &title);
  191. QTCREATOR_UTILS_EXPORT LayoutItem text(const QString &text);
  192. QTCREATOR_UTILS_EXPORT LayoutItem tooltip(const QString &toolTip);
  193. QTCREATOR_UTILS_EXPORT LayoutItem resize(int, int);
  194. QTCREATOR_UTILS_EXPORT LayoutItem columnStretch(int column, int stretch);
  195. QTCREATOR_UTILS_EXPORT LayoutItem rowStretch(int row, int stretch);
  196. QTCREATOR_UTILS_EXPORT LayoutItem spacing(int);
  197. QTCREATOR_UTILS_EXPORT LayoutItem windowTitle(const QString &windowTitle);
  198. QTCREATOR_UTILS_EXPORT LayoutItem fieldGrowthPolicy(QFormLayout::FieldGrowthPolicy policy);
  199. // "Getters"
  200. class ID
  201. {
  202. public:
  203. QObject *ob = nullptr;
  204. };
  205. QTCREATOR_UTILS_EXPORT LayoutItem id(ID &out);
  206. QTCREATOR_UTILS_EXPORT void setText(ID id, const QString &text);
  207. // "Signals"
  208. QTCREATOR_UTILS_EXPORT LayoutItem onClicked(const std::function<void()> &, QObject *guard = nullptr);
  209. QTCREATOR_UTILS_EXPORT LayoutItem onTextChanged(const std::function<void(const QString &)> &,
  210. QObject *guard = nullptr);
  211. QTCREATOR_UTILS_EXPORT LayoutItem onValueChanged(const std::function<void(int)> &,
  212. QObject *guard = nullptr);
  213. QTCREATOR_UTILS_EXPORT LayoutItem onTextChanged(ID &id, QVariant (*sig)(QObject *));
  214. // Convenience
  215. QTCREATOR_UTILS_EXPORT QWidget *createHr(QWidget *parent = nullptr);
  216. template<class T>
  217. LayoutItem bindTo(T **out)
  218. {
  219. return [out](QObject *target) { *out = qobject_cast<T *>(target); };
  220. }
  221. } // namespace Layouting