mirror-chatterino2/src/util/LayoutCreator.hpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

201 lines
4.9 KiB
C++
Raw Normal View History

2018-01-12 23:09:05 +01:00
#pragma once
2018-01-12 23:09:05 +01:00
#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>
2018-01-12 23:09:05 +01:00
#include <cassert>
#include <type_traits>
2018-01-12 23:09:05 +01:00
namespace chatterino {
2018-01-12 23:09:05 +01:00
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
{
}
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
}
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
}
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);
}
2018-01-12 23:09:05 +01:00
template <typename T2, typename... Args>
// clang-format off
// clang-format can be enabled once clang-format v11+ has been installed in CI
LayoutCreator<T2> emplace(Args &&...args)
// clang-format on
2018-01-12 23:09:05 +01:00
{
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,
typename std::enable_if<std::is_base_of<QScrollArea, Q>::value,
int>::type = 0>
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,
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;
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;
}
2018-01-12 23:09:05 +01:00
template <typename Q = T,
typename std::enable_if<std::is_base_of<QLayout, Q>::value,
int>::type = 0>
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);
2018-10-31 19:45:51 +01:00
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()
{
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,
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");
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);
}
2019-09-18 13:03:16 +02:00
template <typename Slot, typename Func>
LayoutCreator<T> connect(Slot slot, QObject *receiver, Func func)
{
QObject::connect(this->getElement(), slot, receiver, func);
return *this;
}
template <typename Func>
LayoutCreator<T> onClick(QObject *receiver, Func func)
{
QObject::connect(this->getElement(), &T::clicked, receiver, func);
return *this;
}
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,
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);
}
2018-01-12 23:09:05 +01:00
template <typename T2,
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);
}
2018-01-12 23:09:05 +01:00
template <typename Q = T,
typename std::enable_if<std::is_base_of<QLayout, Q>::value,
int>::type = 0>
QLayout *getOrCreateLayout()
{
2018-07-06 19:23:47 +02:00
return this->item_;
2018-01-12 23:09:05 +01:00
}
2018-01-12 23:09:05 +01:00
template <typename Q = T,
typename std::enable_if<std::is_base_of<QWidget, Q>::value,
int>::type = 0>
QLayout *getOrCreateLayout()
{
2018-07-06 19:23:47 +02:00
if (!this->item_->layout())
{
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
}
};
2019-09-18 13:03:16 +02:00
template <typename T, typename... Args>
// clang-format off
// clang-format can be enabled once clang-format v11+ has been installed in CI
LayoutCreator<T> makeDialog(Args &&...args)
// clang-format on
2019-09-18 13:03:16 +02:00
{
T *t = new T(std::forward<Args>(args)...);
t->setAttribute(Qt::WA_DeleteOnClose);
return LayoutCreator<T>(t);
}
2018-01-12 23:09:05 +01:00
} // namespace chatterino