2018-01-12 23:09:05 +01:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <QHBoxLayout>
|
2018-04-25 20:35:32 +02:00
|
|
|
#include <QScrollArea>
|
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 {
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
class LayoutCreator
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
LayoutCreator(T *_item)
|
2018-07-06 19:23:47 +02:00
|
|
|
: item_(_item)
|
2018-01-12 23:09:05 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
T *operator->()
|
|
|
|
{
|
2018-07-06 19:23:47 +02:00
|
|
|
return this->item_;
|
2018-01-12 23:09:05 +01:00
|
|
|
}
|
|
|
|
|
2018-06-22 12:34:33 +02:00
|
|
|
T &operator*()
|
2018-04-18 09:12:29 +02:00
|
|
|
{
|
2018-07-06 19:23:47 +02:00
|
|
|
return *this->item_;
|
2018-04-18 09:12:29 +02:00
|
|
|
}
|
|
|
|
|
2018-01-12 23:09:05 +01:00
|
|
|
T *getElement()
|
|
|
|
{
|
2018-07-06 19:23:47 +02:00
|
|
|
return this->item_;
|
2018-01-12 23:09:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T2>
|
|
|
|
LayoutCreator<T2> append(T2 *_item)
|
|
|
|
{
|
2018-07-06 19:23:47 +02:00
|
|
|
this->addItem(this->getOrCreateLayout(), _item);
|
2018-01-12 23:09:05 +01:00
|
|
|
|
|
|
|
return LayoutCreator<T2>(_item);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T2, typename... Args>
|
|
|
|
LayoutCreator<T2> emplace(Args &&... args)
|
|
|
|
{
|
|
|
|
T2 *t = new T2(std::forward<Args>(args)...);
|
|
|
|
|
2018-07-06 19:23:47 +02:00
|
|
|
this->addItem(this->getOrCreateLayout(), t);
|
2018-01-12 23:09:05 +01:00
|
|
|
|
|
|
|
return LayoutCreator<T2>(t);
|
|
|
|
}
|
|
|
|
|
2018-04-25 20:35:32 +02:00
|
|
|
template <typename Q = T,
|
2018-08-06 21:17:03 +02:00
|
|
|
typename std::enable_if<std::is_base_of<QScrollArea, Q>::value,
|
|
|
|
int>::type = 0>
|
2018-04-25 20:35:32 +02:00
|
|
|
LayoutCreator<QWidget> emplaceScrollAreaWidget()
|
|
|
|
{
|
|
|
|
QWidget *widget = new QWidget;
|
2018-07-06 19:23:47 +02:00
|
|
|
this->item_->setWidget(widget);
|
2018-04-25 20:35:32 +02:00
|
|
|
return LayoutCreator<QWidget>(widget);
|
|
|
|
}
|
|
|
|
|
2018-01-23 21:33:49 +01:00
|
|
|
template <typename T2, typename Q = T,
|
2018-08-06 21:17:03 +02:00
|
|
|
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>
|
2018-01-23 21:33:49 +01:00
|
|
|
LayoutCreator<T2> setLayoutType()
|
|
|
|
{
|
|
|
|
T2 *layout = new T2;
|
|
|
|
|
2018-07-06 19:23:47 +02:00
|
|
|
this->item_->setLayout(layout);
|
2018-01-23 21:33:49 +01:00
|
|
|
|
|
|
|
return LayoutCreator<T2>(layout);
|
|
|
|
}
|
|
|
|
|
2018-01-12 23:09:05 +01:00
|
|
|
LayoutCreator<T> assign(T **ptr)
|
|
|
|
{
|
2018-07-06 19:23:47 +02:00
|
|
|
*ptr = this->item_;
|
2018-01-12 23:09:05 +01:00
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Q = T,
|
2018-08-06 21:17:03 +02:00
|
|
|
typename std::enable_if<std::is_base_of<QLayout, Q>::value,
|
|
|
|
int>::type = 0>
|
2018-01-12 23:09:05 +01:00
|
|
|
LayoutCreator<T> withoutMargin()
|
|
|
|
{
|
2018-07-06 19:23:47 +02:00
|
|
|
this->item_->setContentsMargins(0, 0, 0, 0);
|
2018-01-12 23:09:05 +01:00
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2018-10-31 19:45:51 +01:00
|
|
|
LayoutCreator<T> withoutSpacing()
|
|
|
|
{
|
|
|
|
this->item_->setSpacing(0);
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2018-04-18 09:12:29 +02:00
|
|
|
template <typename Q = T,
|
2018-08-06 21:17:03 +02:00
|
|
|
typename std::enable_if<std::is_base_of<QWidget, Q>::value,
|
|
|
|
int>::type = 0>
|
2018-04-18 09:12:29 +02:00
|
|
|
LayoutCreator<T> hidden()
|
|
|
|
{
|
2018-07-06 19:23:47 +02:00
|
|
|
this->item_->setVisible(false);
|
2018-04-18 09:12:29 +02:00
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2018-01-13 02:00:02 +01:00
|
|
|
template <typename Q = T, typename T2,
|
2018-08-06 21:17:03 +02:00
|
|
|
typename std::enable_if<std::is_same<QTabWidget, Q>::value,
|
|
|
|
int>::type = 0>
|
2018-01-13 02:00:02 +01:00
|
|
|
LayoutCreator<T2> appendTab(T2 *item, const QString &title)
|
|
|
|
{
|
2018-08-06 21:17:03 +02:00
|
|
|
static_assert(std::is_base_of<QLayout, T2>::value,
|
|
|
|
"needs to be QLayout");
|
2018-01-13 02:00:02 +01:00
|
|
|
|
|
|
|
QWidget *widget = new QWidget;
|
|
|
|
widget->setLayout(item);
|
|
|
|
|
2018-07-06 19:23:47 +02:00
|
|
|
this->item_->addTab(widget, title);
|
2018-01-13 02:00:02 +01:00
|
|
|
|
|
|
|
return LayoutCreator<T2>(item);
|
|
|
|
}
|
|
|
|
|
2018-01-12 23:09:05 +01:00
|
|
|
private:
|
2018-07-06 19:23:47 +02:00
|
|
|
T *item_;
|
2018-01-12 23:09:05 +01:00
|
|
|
|
|
|
|
template <typename T2,
|
2018-08-06 21:17:03 +02:00
|
|
|
typename std::enable_if<std::is_base_of<QWidget, T2>::value,
|
|
|
|
int>::type = 0>
|
2018-07-06 19:23:47 +02:00
|
|
|
void addItem(QLayout *layout, T2 *item)
|
2018-01-12 23:09:05 +01:00
|
|
|
{
|
|
|
|
layout->addWidget(item);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T2,
|
2018-08-06 21:17:03 +02:00
|
|
|
typename std::enable_if<std::is_base_of<QLayout, T2>::value,
|
|
|
|
int>::type = 0>
|
2018-07-06 19:23:47 +02:00
|
|
|
void addItem(QLayout *layout, T2 *item)
|
2018-01-12 23:09:05 +01:00
|
|
|
{
|
|
|
|
QWidget *widget = new QWidget();
|
|
|
|
widget->setLayout(item);
|
|
|
|
layout->addWidget(widget);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Q = T,
|
2018-08-06 21:17:03 +02:00
|
|
|
typename std::enable_if<std::is_base_of<QLayout, Q>::value,
|
|
|
|
int>::type = 0>
|
2018-01-12 23:09:05 +01:00
|
|
|
QLayout *getOrCreateLayout()
|
|
|
|
{
|
2018-07-06 19:23:47 +02:00
|
|
|
return this->item_;
|
2018-01-12 23:09:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Q = T,
|
2018-08-06 21:17:03 +02:00
|
|
|
typename std::enable_if<std::is_base_of<QWidget, Q>::value,
|
|
|
|
int>::type = 0>
|
2018-01-12 23:09:05 +01:00
|
|
|
QLayout *getOrCreateLayout()
|
|
|
|
{
|
2018-10-21 13:43:02 +02:00
|
|
|
if (!this->item_->layout())
|
|
|
|
{
|
2018-07-06 19:23:47 +02:00
|
|
|
this->item_->setLayout(new QHBoxLayout());
|
2018-01-12 23:09:05 +01:00
|
|
|
}
|
|
|
|
|
2018-07-06 19:23:47 +02:00
|
|
|
return this->item_->layout();
|
2018-01-12 23:09:05 +01:00
|
|
|
}
|
|
|
|
};
|
2018-04-03 02:55:32 +02:00
|
|
|
|
2018-01-12 23:09:05 +01:00
|
|
|
} // namespace chatterino
|