40 #ifndef __OPENFLUID_UICOMMON_UIHELPERS_HPP__
41 #define __OPENFLUID_UICOMMON_UIHELPERS_HPP__
47 #include <QInputDialog>
49 #if (QT_VERSION_MAJOR < 6)
52 #include <QRegularExpression>
56 #include <QApplication>
58 #include <QFileDialog>
59 #include <QMessageBox>
64 namespace openfluid {
namespace ui {
namespace common {
69 static std::random_device RD;
70 static std::mt19937 G(RD());
71 static std::uniform_int_distribution<> Distri255(0, 255);
73 return QColor(Distri255(G),Distri255(G),Distri255(G));
84 auto adjustColor = [](
const double ChannelValue)
86 double RelValue = ChannelValue/255;
87 if (RelValue <= 0.03928)
89 return RelValue/12.92;
94 return std::pow((RelValue + 0.055) / 1.055, 2.4);
97 return adjustColor(Color.red())*0.2126 + adjustColor(Color.green())*0.7152 + adjustColor(Color.blue())*0.0722;
105 #if (QT_VERSION_MAJOR < 6)
106 inline void fixLineEdit(QLineEdit* LineEdit,QRegExp SearchRegExp = QRegExp(
"[^\\w]"),QString NewStr =
"_")
108 inline void fixLineEdit(QLineEdit* LineEdit,QRegularExpression SearchRegExp = QRegularExpression(
"[^\\w]"),
109 QString NewStr =
"_")
112 int CPos = LineEdit->cursorPosition();
113 LineEdit->setText(LineEdit->text().replace(SearchRegExp,NewStr));
114 LineEdit->setCursorPosition(CPos);
124 return (qApp->devicePixelRatio() >= 2.0);
132 inline QIcon
getIcon(
const QString& IconName,
const QString& ResourcePath,
133 bool IsLight =
false,
bool AutoHiDPI =
true)
135 QString IconSuffix =
"dark";
139 IconSuffix =
"light";
142 QString TmpPath = QString(
":%1/icons/%2_%3.png").arg(ResourcePath).arg(IconName).arg(IconSuffix);
146 QString TmpHiDPIPath = QString(
":%1/icons/%2_%3@2x.png").arg(ResourcePath).arg(IconName).arg(IconSuffix);
148 if (QFile::exists(TmpHiDPIPath))
150 TmpPath = TmpHiDPIPath;
154 QIcon TmpIcon(TmpPath);
158 QString TmpLightPath = QString(
":%1/icons/%2_grayed.png").arg(ResourcePath).arg(IconName);
162 QString TmpLightHiDPIPath = QString(
":%1/icons/%2_grayed@2x.png").arg(ResourcePath).arg(IconName);
164 if (QFile::exists(TmpLightHiDPIPath))
166 TmpLightPath = TmpLightHiDPIPath;
169 TmpIcon.addPixmap(QPixmap(TmpLightPath),QIcon::Disabled);
180 inline QPixmap
getImage(
const QString& ImageName,
const QString& ResourcePath,
bool AutoHiDPI =
true)
182 QString TmpPath = QString(
":%1/images/%2.png").arg(ResourcePath).arg(ImageName);
186 QString TmpHiDPIPath = QString(
":%1/images/%2@2x.png").arg(ResourcePath).arg(ImageName);
188 if (QFile::exists(TmpHiDPIPath))
190 TmpPath = TmpHiDPIPath;
194 return QPixmap(TmpPath);
204 if (PathString.isEmpty())
209 QString FileToCreate =
"";
210 QString FileName = QFileDialog::getSaveFileName(Parent,
211 QApplication::translate(
"openfluid::ui::common",
"Create file"),
213 QApplication::translate(
"openfluid::ui::common",
"All files"));
214 if (!FileName.isEmpty() && !FileName.isNull())
216 FileToCreate = FileName;
219 if (!FileToCreate.isEmpty())
223 if (NewFilePath.isDirectory())
225 QMessageBox::critical(Parent,QApplication::translate(
"openfluid::ui::common",
"Error creating file"),
226 QApplication::translate(
"openfluid::ui::common",
"\"%1\" is an existing directory")
227 .arg(QString::fromStdString(NewFilePath.toNative())));
232 QMessageBox::critical(Parent,QApplication::translate(
"openfluid::ui::common",
"Error creating file"),
233 QApplication::translate(
"openfluid::ui::common",
234 "\"%1\" is not in the current ware main directory")
235 .arg(QString::fromStdString(NewFilePath.toNative())));
240 NewFilePath.makeFile();
QString createNewFile(QWidget *Parent, const QString &PathString)
Definition: UIHelpers.hpp:202
QPixmap getImage(const QString &ImageName, const QString &ResourcePath, bool AutoHiDPI=true)
Definition: UIHelpers.hpp:180
void fixLineEdit(QLineEdit *LineEdit, QRegExp SearchRegExp=QRegExp("[^\\w]"), QString NewStr="_")
Definition: UIHelpers.hpp:106
QIcon getIcon(const QString &IconName, const QString &ResourcePath, bool IsLight=false, bool AutoHiDPI=true)
Definition: UIHelpers.hpp:132
double computeLuminance(const QColor &Color)
Definition: UIHelpers.hpp:81
QColor getRandomColor()
Definition: UIHelpers.hpp:67
bool isHiDPIDevice()
Definition: UIHelpers.hpp:122
Definition: ApplicationException.hpp:47