39 #ifndef __OPENFLUID_UICOMMON_UIHELPERS_HPP__
40 #define __OPENFLUID_UICOMMON_UIHELPERS_HPP__
50 #include <QApplication>
54 namespace openfluid {
namespace ui {
namespace common {
59 static std::random_device RD;
60 static std::mt19937 G(RD());
61 static std::uniform_int_distribution<> Distri255(0, 255);
63 return QColor(Distri255(G),Distri255(G),Distri255(G));
74 auto adjustColor = [](
const double ChannelValue)
76 double RelValue = ChannelValue/255;
77 if (RelValue <= 0.03928)
79 return RelValue/12.92;
84 return std::pow((RelValue + 0.055) / 1.055, 2.4);
87 return adjustColor(Color.red())*0.2126 + adjustColor(Color.green())*0.7152 + adjustColor(Color.blue())*0.0722;
95 inline void fixLineEdit(QLineEdit* LineEdit,QRegExp SearchRegExp = QRegExp(
"[^\\w]"),QString NewStr =
"_")
97 int CPos = LineEdit->cursorPosition();
98 LineEdit->setText(LineEdit->text().replace(SearchRegExp,NewStr));
99 LineEdit->setCursorPosition(CPos);
109 return (qApp->devicePixelRatio() >= 2.0);
117 inline QIcon
getIcon(
const QString& IconName,
const QString& ResourcePath,
118 bool IsLight =
false,
bool AutoHiDPI =
true)
120 QString IconSuffix =
"dark";
124 IconSuffix =
"light";
127 QString TmpPath = QString(
":%1/icons/%2_%3.png").arg(ResourcePath).arg(IconName).arg(IconSuffix);
131 QString TmpHiDPIPath = QString(
":%1/icons/%2_%3@2x.png").arg(ResourcePath).arg(IconName).arg(IconSuffix);
133 if (QFile::exists(TmpHiDPIPath))
135 TmpPath = TmpHiDPIPath;
139 QIcon TmpIcon(TmpPath);
143 QString TmpLightPath = QString(
":%1/icons/%2_grayed.png").arg(ResourcePath).arg(IconName);
147 QString TmpLightHiDPIPath = QString(
":%1/icons/%2_grayed@2x.png").arg(ResourcePath).arg(IconName);
149 if (QFile::exists(TmpLightHiDPIPath))
151 TmpLightPath = TmpLightHiDPIPath;
154 TmpIcon.addPixmap(QPixmap(TmpLightPath),QIcon::Disabled);
165 inline QPixmap
getImage(
const QString& ImageName,
const QString& ResourcePath,
bool AutoHiDPI =
true)
167 QString TmpPath = QString(
":%1/images/%2.png").arg(ResourcePath).arg(ImageName);
171 QString TmpHiDPIPath = QString(
":%1/images/%2@2x.png").arg(ResourcePath).arg(ImageName);
173 if (QFile::exists(TmpHiDPIPath))
175 TmpPath = TmpHiDPIPath;
179 return QPixmap(TmpPath);