#pragma once #include #include #include #include #include #include namespace chatterino { namespace util { 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 LayoutCreator emplace(Args &&... args) { 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; } 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); } 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(); } }; } // namespace util } // namespace chatterino