39 #ifndef __OPENFLUID_UICOMMON_UIHELPERS_HPP__ 40 #define __OPENFLUID_UICOMMON_UIHELPERS_HPP__ 49 #include <QApplication> 53 namespace openfluid {
namespace ui {
namespace common {
58 return QColor(qrand() % 256,qrand() % 256,qrand() % 256);
69 auto adjustColor = [](
const double ChannelValue)
71 double RelValue = ChannelValue/255;
72 if (RelValue <= 0.03928)
74 return RelValue/12.92;
79 return std::pow((RelValue + 0.055) / 1.055, 2.4);
82 return adjustColor(Color.red())*0.2126 + adjustColor(Color.green())*0.7152 + adjustColor(Color.blue())*0.0722;
90 inline void fixLineEdit(QLineEdit* LineEdit,QRegExp SearchRegExp = QRegExp(
"[^\\w]"),QString NewStr =
"_")
92 int CPos = LineEdit->cursorPosition();
93 LineEdit->setText(LineEdit->text().replace(SearchRegExp,NewStr));
94 LineEdit->setCursorPosition(CPos);
104 return (qApp->devicePixelRatio() >= 2.0);
112 inline QIcon
getIcon(
const QString& IconName,
const QString& ResourcePath,
113 bool IsLight =
false,
bool AutoHiDPI =
true)
115 QString IconSuffix =
"dark";
119 IconSuffix =
"light";
122 QString TmpPath = QString(
":%1/icons/%2_%3.png").arg(ResourcePath).arg(IconName).arg(IconSuffix);
126 QString TmpHiDPIPath = QString(
":%1/icons/%2_%3@2x.png").arg(ResourcePath).arg(IconName).arg(IconSuffix);
128 if (QFile::exists(TmpHiDPIPath))
130 TmpPath = TmpHiDPIPath;
134 QIcon TmpIcon(TmpPath);
138 QString TmpLightPath = QString(
":%1/icons/%2_grayed.png").arg(ResourcePath).arg(IconName);
142 QString TmpLightHiDPIPath = QString(
":%1/icons/%2_grayed@2x.png").arg(ResourcePath).arg(IconName);
144 if (QFile::exists(TmpLightHiDPIPath))
146 TmpLightPath = TmpLightHiDPIPath;
149 TmpIcon.addPixmap(QPixmap(TmpLightPath),QIcon::Disabled);
160 inline QPixmap
getImage(
const QString& ImageName,
const QString& ResourcePath,
bool AutoHiDPI =
true)
162 QString TmpPath = QString(
":%1/images/%2.png").arg(ResourcePath).arg(ImageName);
166 QString TmpHiDPIPath = QString(
":%1/images/%2@2x.png").arg(ResourcePath).arg(ImageName);
168 if (QFile::exists(TmpHiDPIPath))
170 TmpPath = TmpHiDPIPath;
174 return QPixmap(TmpPath);
Definition: ApplicationException.hpp:47
void fixLineEdit(QLineEdit *LineEdit, QRegExp SearchRegExp=QRegExp("[^\]"), QString NewStr="_")
Definition: UIHelpers.hpp:90
QIcon getIcon(const QString &IconName, const QString &ResourcePath, bool IsLight=false, bool AutoHiDPI=true)
Definition: UIHelpers.hpp:112
QPixmap getImage(const QString &ImageName, const QString &ResourcePath, bool AutoHiDPI=true)
Definition: UIHelpers.hpp:160
QColor getRandomColor()
Definition: UIHelpers.hpp:56
bool isHiDPIDevice()
Definition: UIHelpers.hpp:102
double computeLuminance(const QColor &Color)
Definition: UIHelpers.hpp:66