#pragma once #include #include #include #include #include #include namespace chatterino { template class LayoutCreator { public: LayoutCreator(T *_item) : item_(_item) { } T *operator->() { return this->item_; } T &operator*() { return *this->item_; } T *getElement() { return this->item_; } template LayoutCreator append(T2 *_item) { this->addItem(this->getOrCreateLayout(), _item); return LayoutCreator(_item); } template // clang-format off // clang-format can be enabled once clang-format v11+ has been installed in CI LayoutCreator emplace(Args &&...args) // clang-format on { T2 *t = new T2(std::forward(args)...); this->addItem(this->getOrCreateLayout(), t); return LayoutCreator(t); } template ::value, int>::type = 0> LayoutCreator emplaceScrollAreaWidget() { QWidget *widget = new QWidget; this->item_->setWidget(widget); return LayoutCreator(widget); } template ::value, int>::type = 0, typename std::enable_if::value, int>::type = 0> LayoutCreator setLayoutType() { T2 *layout = new T2; this->item_->setLayout(layout); return LayoutCreator(layout); } LayoutCreator assign(T **ptr) { *ptr = this->item_; return *this; } template ::value, int>::type = 0> LayoutCreator withoutMargin() { this->item_->setContentsMargins(0, 0, 0, 0); return *this; } LayoutCreator withoutSpacing() { this->item_->setSpacing(0); return *this; } template ::value, int>::type = 0> LayoutCreator hidden() { this->item_->setVisible(false); return *this; } template ::value, int>::type = 0> LayoutCreator appendTab(T2 *item, const QString &title) { static_assert(std::is_base_of::value, "needs to be QLayout"); QWidget *widget = new QWidget; widget->setLayout(item); this->item_->addTab(widget, title); return LayoutCreator(item); } template LayoutCreator connect(Slot slot, QObject *receiver, Func func) { QObject::connect(this->getElement(), slot, receiver, func); return *this; } template LayoutCreator onClick(QObject *receiver, Func func) { QObject::connect(this->getElement(), &T::clicked, receiver, func); return *this; } private: T *item_; template ::value, int>::type = 0> void addItem(QLayout *layout, T2 *item) { layout->addWidget(item); } template ::value, int>::type = 0> void addItem(QLayout *layout, T2 *item) { QWidget *widget = new QWidget(); widget->setLayout(item); layout->addWidget(widget); } template ::value, int>::type = 0> QLayout *getOrCreateLayout() { return this->item_; } template ::value, int>::type = 0> QLayout *getOrCreateLayout() { if (!this->item_->layout()) { this->item_->setLayout(new QHBoxLayout()); } return this->item_->layout(); } }; template // clang-format off // clang-format can be enabled once clang-format v11+ has been installed in CI LayoutCreator makeDialog(Args &&...args) // clang-format on { T *t = new T(std::forward(args)...); t->setAttribute(Qt::WA_DeleteOnClose); return LayoutCreator(t); } } // namespace chatterino