layoutbuilder.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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> // C++17 feature, commented out for C++14 compatibility
  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. static_assert(!std::is_same<T, LayoutItem>::value || std::is_base_of<LayoutItem, T>::value,
  35. "Type must be convertible to LayoutItem or handled by createItem");
  36. createItemHelper(this, t, std::is_base_of<LayoutItem, T>{});
  37. }
  38. private:
  39. template<class T>
  40. void createItemHelper(LayoutItem *item, const T &t, std::true_type)
  41. {
  42. LayoutItem::operator=(t);
  43. }
  44. template<class T>
  45. void createItemHelper(LayoutItem *item, const T &t, std::false_type)
  46. {
  47. createItem(item, t);
  48. }
  49. public:
  50. void attachTo(QWidget *w) const;
  51. QWidget *emerge();
  52. void addItem(const LayoutItem &item);
  53. void addItems(const LayoutItems &items);
  54. void addRow(const LayoutItems &items);
  55. std::function<void(LayoutBuilder &)> onAdd;
  56. std::function<void(LayoutBuilder &)> onExit;
  57. std::function<void(QObject *target)> setter;
  58. LayoutItems subItems;
  59. };
  60. // Special items
  61. class QTCREATOR_UTILS_EXPORT Space
  62. {
  63. public:
  64. explicit Space(int space)
  65. : space(space)
  66. {}
  67. const int space;
  68. };
  69. class QTCREATOR_UTILS_EXPORT Stretch
  70. {
  71. public:
  72. explicit Stretch(int stretch = 1)
  73. : stretch(stretch)
  74. {}
  75. const int stretch;
  76. };
  77. class QTCREATOR_UTILS_EXPORT Span
  78. {
  79. public:
  80. Span(int span, const LayoutItem &item)
  81. : span(span)
  82. , item(item)
  83. {}
  84. const int span;
  85. LayoutItem item;
  86. };
  87. class QTCREATOR_UTILS_EXPORT Column : public LayoutItem
  88. {
  89. public:
  90. Column(std::initializer_list<LayoutItem> items);
  91. };
  92. class QTCREATOR_UTILS_EXPORT Row : public LayoutItem
  93. {
  94. public:
  95. Row(std::initializer_list<LayoutItem> items);
  96. };
  97. class QTCREATOR_UTILS_EXPORT Flow : public LayoutItem
  98. {
  99. public:
  100. Flow(std::initializer_list<LayoutItem> items);
  101. };
  102. class QTCREATOR_UTILS_EXPORT Grid : public LayoutItem
  103. {
  104. public:
  105. Grid()
  106. : Grid({})
  107. {}
  108. Grid(std::initializer_list<LayoutItem> items);
  109. };
  110. class QTCREATOR_UTILS_EXPORT Form : public LayoutItem
  111. {
  112. public:
  113. Form()
  114. : Form({})
  115. {}
  116. Form(std::initializer_list<LayoutItem> items);
  117. };
  118. class QTCREATOR_UTILS_EXPORT Widget : public LayoutItem
  119. {
  120. public:
  121. Widget(std::initializer_list<LayoutItem> items);
  122. };
  123. class QTCREATOR_UTILS_EXPORT Stack : public LayoutItem
  124. {
  125. public:
  126. Stack()
  127. : Stack({})
  128. {}
  129. Stack(std::initializer_list<LayoutItem> items);
  130. };
  131. class QTCREATOR_UTILS_EXPORT Tab : public LayoutItem
  132. {
  133. public:
  134. Tab(const QString &tabName, const LayoutItem &item);
  135. };
  136. class QTCREATOR_UTILS_EXPORT If : public LayoutItem
  137. {
  138. public:
  139. If(bool condition, const LayoutItems &item, const LayoutItems &other = {});
  140. };
  141. class QTCREATOR_UTILS_EXPORT Group : public LayoutItem
  142. {
  143. public:
  144. Group(std::initializer_list<LayoutItem> items);
  145. };
  146. class QTCREATOR_UTILS_EXPORT TextEdit : public LayoutItem
  147. {
  148. public:
  149. TextEdit(std::initializer_list<LayoutItem> items);
  150. };
  151. class QTCREATOR_UTILS_EXPORT PushButton : public LayoutItem
  152. {
  153. public:
  154. PushButton(std::initializer_list<LayoutItem> items);
  155. };
  156. class QTCREATOR_UTILS_EXPORT SpinBox : public LayoutItem
  157. {
  158. public:
  159. SpinBox(std::initializer_list<LayoutItem> items);
  160. };
  161. class QTCREATOR_UTILS_EXPORT Splitter : public LayoutItem
  162. {
  163. public:
  164. Splitter(std::initializer_list<LayoutItem> items);
  165. };
  166. class QTCREATOR_UTILS_EXPORT ToolBar : public LayoutItem
  167. {
  168. public:
  169. ToolBar(std::initializer_list<LayoutItem> items);
  170. };
  171. class QTCREATOR_UTILS_EXPORT TabWidget : public LayoutItem
  172. {
  173. public:
  174. TabWidget(std::initializer_list<LayoutItem> items);
  175. };
  176. class QTCREATOR_UTILS_EXPORT Application : public LayoutItem
  177. {
  178. public:
  179. Application(std::initializer_list<LayoutItem> items);
  180. int exec(int &argc, char *argv[]);
  181. };
  182. void QTCREATOR_UTILS_EXPORT createItem(LayoutItem *item,
  183. const std::function<void(QObject *target)> &t);
  184. void QTCREATOR_UTILS_EXPORT createItem(LayoutItem *item, QWidget *t);
  185. void QTCREATOR_UTILS_EXPORT createItem(LayoutItem *item, QLayout *t);
  186. void QTCREATOR_UTILS_EXPORT createItem(LayoutItem *item, LayoutItem (*t)());
  187. void QTCREATOR_UTILS_EXPORT createItem(LayoutItem *item, const QString &t);
  188. void QTCREATOR_UTILS_EXPORT createItem(LayoutItem *item, const Span &t);
  189. void QTCREATOR_UTILS_EXPORT createItem(LayoutItem *item, const Space &t);
  190. void QTCREATOR_UTILS_EXPORT createItem(LayoutItem *item, const Stretch &t);
  191. // "Singletons"
  192. QTCREATOR_UTILS_EXPORT LayoutItem br();
  193. QTCREATOR_UTILS_EXPORT LayoutItem st();
  194. QTCREATOR_UTILS_EXPORT LayoutItem empty();
  195. QTCREATOR_UTILS_EXPORT LayoutItem hr();
  196. QTCREATOR_UTILS_EXPORT LayoutItem noMargin();
  197. QTCREATOR_UTILS_EXPORT LayoutItem normalMargin();
  198. QTCREATOR_UTILS_EXPORT LayoutItem customMargin(const QMargins &margin);
  199. QTCREATOR_UTILS_EXPORT LayoutItem withFormAlignment();
  200. // "Setters"
  201. QTCREATOR_UTILS_EXPORT LayoutItem title(const QString &title);
  202. QTCREATOR_UTILS_EXPORT LayoutItem text(const QString &text);
  203. QTCREATOR_UTILS_EXPORT LayoutItem tooltip(const QString &toolTip);
  204. QTCREATOR_UTILS_EXPORT LayoutItem resize(int, int);
  205. QTCREATOR_UTILS_EXPORT LayoutItem columnStretch(int column, int stretch);
  206. QTCREATOR_UTILS_EXPORT LayoutItem spacing(int);
  207. QTCREATOR_UTILS_EXPORT LayoutItem windowTitle(const QString &windowTitle);
  208. QTCREATOR_UTILS_EXPORT LayoutItem fieldGrowthPolicy(QFormLayout::FieldGrowthPolicy policy);
  209. // "Getters"
  210. class ID
  211. {
  212. public:
  213. QObject *ob = nullptr;
  214. };
  215. QTCREATOR_UTILS_EXPORT LayoutItem id(ID &out);
  216. QTCREATOR_UTILS_EXPORT void setText(ID id, const QString &text);
  217. // "Signals"
  218. QTCREATOR_UTILS_EXPORT LayoutItem onClicked(const std::function<void()> &, QObject *guard = nullptr);
  219. QTCREATOR_UTILS_EXPORT LayoutItem onTextChanged(const std::function<void(const QString &)> &,
  220. QObject *guard = nullptr);
  221. QTCREATOR_UTILS_EXPORT LayoutItem onValueChanged(const std::function<void(int)> &,
  222. QObject *guard = nullptr);
  223. QTCREATOR_UTILS_EXPORT LayoutItem onTextChanged(ID &id, QVariant (*sig)(QObject *));
  224. // Convenience
  225. QTCREATOR_UTILS_EXPORT QWidget *createHr(QWidget *parent = nullptr);
  226. template<class T>
  227. LayoutItem bindTo(T **out)
  228. {
  229. return [out](QObject *target) { *out = qobject_cast<T *>(target); };
  230. }
  231. } // namespace Layouting