#pragma once #include #include #include #include namespace chatterino { namespace util { template class LayoutCreator { public: LayoutCreator(T *_item) : item(_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); } 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; } 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