1
0
mirror of https://github.com/KDE/latte-dock.git synced 2024-12-23 13:33:50 +03:00

utilities functions

This commit is contained in:
Johan Smith Agudelo Rodriguez 2016-12-25 18:09:56 -05:00
parent 0c1db869d4
commit c79cbac0a6
2 changed files with 74 additions and 0 deletions

1
liblattedock/extras.cpp Normal file
View File

@ -0,0 +1 @@
#include "extras.h"

73
liblattedock/extras.h Normal file
View File

@ -0,0 +1,73 @@
#ifndef EXTRAS_H
#define EXTRAS_H
#include <QObject>
#include <QString>
#include <QStringBuilder>
#include <QRect>
#include <QMetaEnum>
#include <QMetaType>
#include <Plasma>
#include <type_traits>
#include <numeric>
#include <memory>
#if __GLIBCXX__ <= 20150623
namespace std {
template<class T, class... Args>
unique_ptr<T> make_unique(Args &&... args)
{
return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
}
}
#endif
/*!
* @brief convert a QRect to a QString with format `(<x>, <y>) <width>x<height>`
*/
inline QString qRectToStr(const QRect &r)
{
return "(" % QString::number(r.x()) % ", " % QString::number(r.y()) % ") "
% QString::number(r.width()) % "x" % QString::number(r.height());
}
/*!
* @brief convert a `Q_ENUM` to c-string
*/
template<typename EnumType>
inline const char *qEnumToStr(EnumType value)
{
return QMetaEnum::fromType<EnumType>().valueToKey(value);
}
/*!
* @brief convert a `Q_ENUMS` of `Plasma::Types::Location` to c-string
*/
inline const char *qEnumToStr(Plasma::Types::Location Enum)
{
static const int Index = Plasma::Types::staticMetaObject.indexOfEnumerator("Location");
return Plasma::Types::staticMetaObject.enumerator(Index).valueToKey(Enum);
}
/*!
* @brief convert a `Q_ENUMS` of `Plasma::Types::FormFactor` to c-string
*/
inline const char *qEnumToStr(Plasma::Types::FormFactor Enum)
{
static const int Index = Plasma::Types::staticMetaObject.indexOfEnumerator("FormFactor");
return Plasma::Types::staticMetaObject.enumerator(Index).valueToKey(Enum);
}
/*!
* @brief machine epsilon
*/
template<class T>
typename std::enable_if<!std::is_integral<T>(), bool>::type almost_equal(T x, T y, int ulp)
{
return std::abs(x - y) < std::numeric_limits<T>::epsilon() * std::abs(x + y) * ulp
|| std::abs(x - y) < std::numeric_limits<T>::min();
}
#endif // EXTRAS_H