Files
qTier/utils.hpp
2024-05-22 01:19:23 -05:00

68 lines
1.4 KiB
C++

#ifndef UTILS_HPP
#define UTILS_HPP
// #include "tierrow.hpp"
#include <QWidget>
#include <QString>
//change this stupid name
inline void callOnMeAndChildren(QObject* obj, void (*func)(QObject*))
{
func(obj);
for (auto w : obj->children())
{
callOnMeAndChildren(w, func);
}
}
template <typename T>
void callOnMeAndChildren(QObject* obj, void (*func)(T*))
{
if (auto casted = dynamic_cast<T*>(obj))
{
func(casted);
}
for (auto w : obj->children())
{
callOnMeAndChildren(w, func);
}
}
inline QString makeBgColorString(QColor color)
{
return QString("background-color: rgba(%1,%2,%3,%4)")
.arg(QString::number(color.red()),
QString::number(color.green()),
QString::number(color.blue()),
QString::number(color.alphaF()));
}
template <typename T>
T* findParentOfType(QObject* obj)
{
auto parent = obj->parent();
if (!parent)
return nullptr;
else if (auto asT = dynamic_cast<T*>(parent))
{
return asT;
}
else
{
return findParentOfType<T>(parent);
}
}
inline QColor blend(QColor first, QColor second, float portionFirst)
{
const float& pf = portionFirst;
const float ps = 1 - pf;
return QColor(
first.red()*pf + second.red()*ps,
first.green()*pf + second.green()*ps,
first.blue()*pf + second.blue()*ps
);
}
#endif // UTILS_HPP