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>
60 #include <QDesktopServices>
67 namespace openfluid {
namespace ui {
namespace common {
72 static std::random_device RD;
73 static std::mt19937 G(RD());
74 static std::uniform_int_distribution<> Distri255(0, 255);
76 return QColor(Distri255(G),Distri255(G),Distri255(G));
87 auto adjustColor = [](
const double ChannelValue)
89 double RelValue = ChannelValue/255;
90 if (RelValue <= 0.03928)
92 return RelValue/12.92;
97 return std::pow((RelValue + 0.055) / 1.055, 2.4);
100 return adjustColor(Color.red())*0.2126 + adjustColor(Color.green())*0.7152 + adjustColor(Color.blue())*0.0722;
108 #if (QT_VERSION_MAJOR < 6)
109 inline void fixLineEdit(QLineEdit* LineEdit,QRegExp SearchRegExp = QRegExp(
"[^\\w]"),QString NewStr =
"_")
111 inline void fixLineEdit(QLineEdit* LineEdit,QRegularExpression SearchRegExp = QRegularExpression(
"[^\\w]"),
112 QString NewStr =
"_")
115 int CPos = LineEdit->cursorPosition();
116 LineEdit->setText(LineEdit->text().replace(SearchRegExp,NewStr));
117 LineEdit->setCursorPosition(CPos);
127 return (qApp->devicePixelRatio() >= 2.0);
135 inline QIcon
getIcon(
const QString& IconName,
const QString& ResourcePath,
136 bool IsLight =
false,
bool AutoHiDPI =
true)
138 QString IconSuffix =
"dark";
142 IconSuffix =
"light";
145 QString TmpPath = QString(
":%1/icons/%2_%3.png").arg(ResourcePath).arg(IconName).arg(IconSuffix);
149 QString TmpHiDPIPath = QString(
":%1/icons/%2_%3@2x.png").arg(ResourcePath).arg(IconName).arg(IconSuffix);
151 if (QFile::exists(TmpHiDPIPath))
153 TmpPath = TmpHiDPIPath;
157 QIcon TmpIcon(TmpPath);
161 QString TmpLightPath = QString(
":%1/icons/%2_grayed.png").arg(ResourcePath).arg(IconName);
165 QString TmpLightHiDPIPath = QString(
":%1/icons/%2_grayed@2x.png").arg(ResourcePath).arg(IconName);
167 if (QFile::exists(TmpLightHiDPIPath))
169 TmpLightPath = TmpLightHiDPIPath;
172 TmpIcon.addPixmap(QPixmap(TmpLightPath),QIcon::Disabled);
183 inline QPixmap
getImage(
const QString& ImageName,
const QString& ResourcePath,
bool AutoHiDPI =
true)
185 QString TmpPath = QString(
":%1/images/%2.png").arg(ResourcePath).arg(ImageName);
189 QString TmpHiDPIPath = QString(
":%1/images/%2@2x.png").arg(ResourcePath).arg(ImageName);
191 if (QFile::exists(TmpHiDPIPath))
193 TmpPath = TmpHiDPIPath;
197 return QPixmap(TmpPath);
207 if (PathString.isEmpty())
212 QString FileToCreate =
"";
213 QString FileName = QFileDialog::getSaveFileName(Parent,
214 QApplication::translate(
"openfluid::ui::common",
"Create file"),
216 QApplication::translate(
"openfluid::ui::common",
"All files"));
217 if (!FileName.isEmpty() && !FileName.isNull())
219 FileToCreate = FileName;
222 if (!FileToCreate.isEmpty())
226 if (NewFilePath.isDirectory())
228 QMessageBox::critical(Parent,QApplication::translate(
"openfluid::ui::common",
"Error creating file"),
229 QApplication::translate(
"openfluid::ui::common",
"\"%1\" is an existing directory")
230 .arg(QString::fromStdString(NewFilePath.toNative())));
235 QMessageBox::critical(Parent,QApplication::translate(
"openfluid::ui::common",
"Error creating file"),
236 QApplication::translate(
"openfluid::ui::common",
237 "\"%1\" is not in the current ware main directory")
238 .arg(QString::fromStdString(NewFilePath.toNative())));
243 NewFilePath.makeFile();
255 inline void openPath(std::string CurrentPath,
bool IsRemote=
false)
257 #if OPENFLUID_OS_ISWSL_FLAG == 1
258 std::string FullLocation = CurrentPath;
263 const auto OutLines = P.stdOutLines();
264 CurrentPath = OutLines[0];
265 FullLocation =
"file://"+CurrentPath;
268 bool Success = PR.
run();
273 for (
const auto& L : PR.stdOutLines())
277 for (
const auto& L : PR.stdErrLines())
286 CurrentURL = QUrl(QString::fromStdString(CurrentPath), QUrl::TolerantMode);
290 CurrentURL = QUrl::fromLocalFile(QString::fromStdString(CurrentPath));
292 QDesktopServices::openUrl(CurrentURL);
Definition: Process.hpp:68
void OPENFLUID_API info(const std::string &Context, const std::string &Msg)
void OPENFLUID_API error(const std::string &Context, const std::string &Msg)
void OPENFLUID_API debug(const std::string &Context, const std::string &Msg)
QString createNewFile(QWidget *Parent, const QString &PathString)
Definition: UIHelpers.hpp:205
void openPath(std::string CurrentPath, bool IsRemote=false)
Definition: UIHelpers.hpp:255
QPixmap getImage(const QString &ImageName, const QString &ResourcePath, bool AutoHiDPI=true)
Definition: UIHelpers.hpp:183
void fixLineEdit(QLineEdit *LineEdit, QRegExp SearchRegExp=QRegExp("[^\\w]"), QString NewStr="_")
Definition: UIHelpers.hpp:109
QIcon getIcon(const QString &IconName, const QString &ResourcePath, bool IsLight=false, bool AutoHiDPI=true)
Definition: UIHelpers.hpp:135
double computeLuminance(const QColor &Color)
Definition: UIHelpers.hpp:84
QColor getRandomColor()
Definition: UIHelpers.hpp:70
bool isHiDPIDevice()
Definition: UIHelpers.hpp:125
Definition: ApplicationException.hpp:47