2018-01-12 23:09:05 +01:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <QHBoxLayout>
|
2018-01-13 02:00:02 +01:00
|
|
|
#include <QTabWidget>
|
2018-01-12 23:09:05 +01:00
|
|
|
#include <QWidget>
|
|
|
|
|
|
|
|
#include <cassert>
|
|
|
|
#include <type_traits>
|
|
|
|
|
|
|
|
namespace chatterino {
|
|
|
|
namespace util {
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
class LayoutCreator
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
LayoutCreator(T *_item)
|
|
|
|
: item(_item)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
T *operator->()
|
|
|
|
{
|
|
|
|
return this->item;
|
|
|
|
}
|
|
|
|
|
2018-04-18 09:12:29 +02:00
|
|
|
T *operator*()
|
|
|
|
{
|
|
|
|
return this->item;
|
|
|
|
}
|
|
|
|
|
2018-01-12 23:09:05 +01:00
|
|
|
T *getElement()
|
|
|
|
{
|
|
|
|
return this->item;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T2>
|
|
|
|
LayoutCreator<T2> append(T2 *_item)
|
|
|
|
{
|
|
|
|
this->_addItem(this->getOrCreateLayout(), _item);
|
|
|
|
|
|
|
|
return LayoutCreator<T2>(_item);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T2, typename... Args>
|
|
|
|
LayoutCreator<T2> emplace(Args &&... args)
|
|
|
|
{
|
|
|
|
T2 *t = new T2(std::forward<Args>(args)...);
|
|
|
|
|
|
|
|
this->_addItem(this->getOrCreateLayout(), t);
|
|
|
|
|
|
|
|
return LayoutCreator<T2>(t);
|
|
|
|
}
|
|
|
|
|
2018-01-23 21:33:49 +01:00
|
|
|
template <typename T2, typename Q = T,
|
|
|
|
typename std::enable_if<std::is_base_of<QWidget, Q>::value, int>::type = 0,
|
|
|
|
typename std::enable_if<std::is_base_of<QLayout, T2>::value, int>::type = 0>
|
|
|
|
LayoutCreator<T2> setLayoutType()
|
|
|
|
{
|
|
|
|
T2 *layout = new T2;
|
|
|
|
|
|
|
|
this->item->setLayout(layout);
|
|
|
|
|
|
|
|
return LayoutCreator<T2>(layout);
|
|
|
|
}
|
|
|
|
|
2018-01-12 23:09:05 +01:00
|
|
|
LayoutCreator<T> assign(T **ptr)
|
|
|
|
{
|
|
|
|
*ptr = this->item;
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Q = T,
|
|
|
|
typename std::enable_if<std::is_base_of<QLayout, Q>::value, int>::type = 0>
|
|
|
|
LayoutCreator<T> withoutMargin()
|
|
|
|
{
|
|
|
|
this->item->setContentsMargins(0, 0, 0, 0);
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2018-04-18 09:12:29 +02:00
|
|
|
template <typename Q = T,
|
|
|
|
typename std::enable_if<std::is_base_of<QWidget, Q>::value, int>::type = 0>
|
|
|
|
LayoutCreator<T> hidden()
|
|
|
|
{
|
|
|
|
this->item->setVisible(false);
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2018-01-13 02:00:02 +01:00
|
|
|
template <typename Q = T, typename T2,
|
|
|
|
typename std::enable_if<std::is_same<QTabWidget, Q>::value, int>::type = 0>
|
|
|
|
LayoutCreator<T2> appendTab(T2 *item, const QString &title)
|
|
|
|
{
|
|
|
|
static_assert(std::is_base_of<QLayout, T2>::value, "needs to be QLayout");
|
|
|
|
|
|
|
|
QWidget *widget = new QWidget;
|
|
|
|
widget->setLayout(item);
|
|
|
|
|
|
|
|
this->item->addTab(widget, title);
|
|
|
|
|
|
|
|
return LayoutCreator<T2>(item);
|
|
|
|
}
|
|
|
|
|
2018-01-12 23:09:05 +01:00
|
|
|
private:
|
|
|
|
T *item;
|
|
|
|
|
|
|
|
template <typename T2,
|
|
|
|
typename std::enable_if<std::is_base_of<QWidget, T2>::value, int>::type = 0>
|
|
|
|
void _addItem(QLayout *layout, T2 *item)
|
|
|
|
{
|
|
|
|
layout->addWidget(item);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T2,
|
|
|
|
typename std::enable_if<std::is_base_of<QLayout, T2>::value, int>::type = 0>
|
|
|
|
void _addItem(QLayout *layout, T2 *item)
|
|
|
|
{
|
|
|
|
QWidget *widget = new QWidget();
|
|
|
|
widget->setLayout(item);
|
|
|
|
layout->addWidget(widget);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Q = T,
|
|
|
|
typename std::enable_if<std::is_base_of<QLayout, Q>::value, int>::type = 0>
|
|
|
|
QLayout *getOrCreateLayout()
|
|
|
|
{
|
|
|
|
return this->item;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Q = T,
|
|
|
|
typename std::enable_if<std::is_base_of<QWidget, Q>::value, int>::type = 0>
|
|
|
|
QLayout *getOrCreateLayout()
|
|
|
|
{
|
|
|
|
if (!this->item->layout()) {
|
|
|
|
this->item->setLayout(new QHBoxLayout());
|
|
|
|
}
|
|
|
|
|
|
|
|
return this->item->layout();
|
|
|
|
}
|
|
|
|
};
|
2018-04-03 02:55:32 +02:00
|
|
|
|
2018-01-12 23:09:05 +01:00
|
|
|
} // namespace util
|
|
|
|
} // namespace chatterino
|