Chatterino
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
LayoutCreator.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <QHBoxLayout>
4 #include <QScrollArea>
5 #include <QTabWidget>
6 #include <QWidget>
7 
8 #include <cassert>
9 #include <type_traits>
10 
11 namespace chatterino {
12 
13 template <class T>
15 {
16 public:
17  LayoutCreator(T *_item)
18  : item_(_item)
19  {
20  }
21 
23  {
24  return this->item_;
25  }
26 
27  T &operator*()
28  {
29  return *this->item_;
30  }
31 
33  {
34  return this->item_;
35  }
36 
37  template <typename T2>
39  {
40  this->addItem(this->getOrCreateLayout(), _item);
41 
42  return LayoutCreator<T2>(_item);
43  }
44 
45  template <typename T2, typename... Args>
46  // clang-format off
47  // clang-format can be enabled once clang-format v11+ has been installed in CI
49  // clang-format on
50  {
51  T2 *t = new T2(std::forward<Args>(args)...);
52 
53  this->addItem(this->getOrCreateLayout(), t);
54 
55  return LayoutCreator<T2>(t);
56  }
57 
58  template <typename Q = T,
59  typename std::enable_if<std::is_base_of<QScrollArea, Q>::value,
60  int>::type = 0>
62  {
63  QWidget *widget = new QWidget;
64  this->item_->setWidget(widget);
65  return LayoutCreator<QWidget>(widget);
66  }
67 
68  template <typename T2, typename Q = T,
69  typename std::enable_if<std::is_base_of<QWidget, Q>::value,
70  int>::type = 0,
71  typename std::enable_if<std::is_base_of<QLayout, T2>::value,
72  int>::type = 0>
74  {
75  T2 *layout = new T2;
76 
77  this->item_->setLayout(layout);
78 
79  return LayoutCreator<T2>(layout);
80  }
81 
83  {
84  *ptr = this->item_;
85 
86  return *this;
87  }
88 
89  template <typename Q = T,
90  typename std::enable_if<std::is_base_of<QLayout, Q>::value,
91  int>::type = 0>
93  {
94  this->item_->setContentsMargins(0, 0, 0, 0);
95 
96  return *this;
97  }
98 
100  {
101  this->item_->setSpacing(0);
102 
103  return *this;
104  }
105 
106  template <typename Q = T,
107  typename std::enable_if<std::is_base_of<QWidget, Q>::value,
108  int>::type = 0>
110  {
111  this->item_->setVisible(false);
112 
113  return *this;
114  }
115 
116  template <typename Q = T, typename T2,
117  typename std::enable_if<std::is_same<QTabWidget, Q>::value,
118  int>::type = 0>
119  LayoutCreator<T2> appendTab(T2 *item, const QString &title)
120  {
121  static_assert(std::is_base_of<QLayout, T2>::value,
122  "needs to be QLayout");
123 
124  QWidget *widget = new QWidget;
125  widget->setLayout(item);
126 
127  this->item_->addTab(widget, title);
128 
129  return LayoutCreator<T2>(item);
130  }
131 
132  template <typename Slot, typename Func>
133  LayoutCreator<T> connect(Slot slot, QObject *receiver, Func func)
134  {
135  QObject::connect(this->getElement(), slot, receiver, func);
136  return *this;
137  }
138 
139  template <typename Func>
140  LayoutCreator<T> onClick(QObject *receiver, Func func)
141  {
142  QObject::connect(this->getElement(), &T::clicked, receiver, func);
143  return *this;
144  }
145 
146 private:
147  T *item_;
148 
149  template <typename T2,
150  typename std::enable_if<std::is_base_of<QWidget, T2>::value,
151  int>::type = 0>
152  void addItem(QLayout *layout, T2 *item)
153  {
154  layout->addWidget(item);
155  }
156 
157  template <typename T2,
158  typename std::enable_if<std::is_base_of<QLayout, T2>::value,
159  int>::type = 0>
160  void addItem(QLayout *layout, T2 *item)
161  {
162  QWidget *widget = new QWidget();
163  widget->setLayout(item);
164  layout->addWidget(widget);
165  }
166 
167  template <typename Q = T,
168  typename std::enable_if<std::is_base_of<QLayout, Q>::value,
169  int>::type = 0>
170  QLayout *getOrCreateLayout()
171  {
172  return this->item_;
173  }
174 
175  template <typename Q = T,
176  typename std::enable_if<std::is_base_of<QWidget, Q>::value,
177  int>::type = 0>
178  QLayout *getOrCreateLayout()
179  {
180  if (!this->item_->layout())
181  {
182  this->item_->setLayout(new QHBoxLayout());
183  }
184 
185  return this->item_->layout();
186  }
187 };
188 
189 template <typename T, typename... Args>
190 // clang-format off
191 // clang-format can be enabled once clang-format v11+ has been installed in CI
193 // clang-format on
194 {
195  T *t = new T(std::forward<Args>(args)...);
196  t->setAttribute(Qt::WA_DeleteOnClose);
197  return LayoutCreator<T>(t);
198 }
199 
200 } // namespace chatterino
LayoutCreator< T2 > setLayoutType()
Definition: LayoutCreator.hpp:73
LayoutCreator< T > assign(T **ptr)
Definition: LayoutCreator.hpp:82
LayoutCreator< T > connect(Slot slot, QObject *receiver, Func func)
Definition: LayoutCreator.hpp:133
LayoutCreator(T *_item)
Definition: LayoutCreator.hpp:17
T * operator->()
Definition: LayoutCreator.hpp:22
LayoutCreator< T > makeDialog(Args &&...args)
Definition: LayoutCreator.hpp:192
Definition: Application.cpp:48
LayoutCreator< T > withoutMargin()
Definition: LayoutCreator.hpp:92
LayoutCreator< QWidget > emplaceScrollAreaWidget()
Definition: LayoutCreator.hpp:61
T & operator*()
Definition: LayoutCreator.hpp:27
Command line arguments passed to Chatterino.
Definition: Args.hpp:10
LayoutCreator< T2 > append(T2 *_item)
Definition: LayoutCreator.hpp:38
LayoutCreator< T > withoutSpacing()
Definition: LayoutCreator.hpp:99
T * getElement()
Definition: LayoutCreator.hpp:32
Definition: LayoutCreator.hpp:14
LayoutCreator< T > onClick(QObject *receiver, Func func)
Definition: LayoutCreator.hpp:140
LayoutCreator< T2 > appendTab(T2 *item, const QString &title)
Definition: LayoutCreator.hpp:119
LayoutCreator< T2 > emplace(Args &&...args)
Definition: LayoutCreator.hpp:48
LayoutCreator< T > hidden()
Definition: LayoutCreator.hpp:109