#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 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; } 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); } 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 chatterino