Documentation for OpenFLUID 2.2.0
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
UIHelpers.hpp
Go to the documentation of this file.
1 /*
2 
3  This file is part of OpenFLUID software
4  Copyright(c) 2007, INRA - Montpellier SupAgro
5 
6 
7  == GNU General Public License Usage ==
8 
9  OpenFLUID is free software: you can redistribute it and/or modify
10  it under the terms of the GNU General Public License as published by
11  the Free Software Foundation, either version 3 of the License, or
12  (at your option) any later version.
13 
14  OpenFLUID is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  GNU General Public License for more details.
18 
19  You should have received a copy of the GNU General Public License
20  along with OpenFLUID. If not, see <http://www.gnu.org/licenses/>.
21 
22 
23  == Other Usage ==
24 
25  Other Usage means a use of OpenFLUID that is inconsistent with the GPL
26  license, and requires a written agreement between You and INRA.
27  Licensees for Other Usage of OpenFLUID may use this file in accordance
28  with the terms contained in the written agreement between You and INRA.
29 
30 */
31 
32 /**
33  @file UIHelpers.hpp
34 
35  @author Jean-Christophe FABRE <jean-christophe.fabre@inra.fr>
36  @author Armel THÖNI <armel.thoni@inrae.fr>
37 */
38 
39 
40 #ifndef __OPENFLUID_UICOMMON_UIHELPERS_HPP__
41 #define __OPENFLUID_UICOMMON_UIHELPERS_HPP__
42 
43 
44 #include <cmath>
45 #include <random>
46 
47 #include <QInputDialog>
48 #include <QColor>
49 #if (QT_VERSION_MAJOR < 6)
50 #include <QRegExp>
51 #else
52 #include <QRegularExpression>
53 #endif
54 #include <QLineEdit>
55 #include <QIcon>
56 #include <QApplication>
57 #include <QFile>
58 #include <QFileDialog>
59 #include <QMessageBox>
60 
62 
63 
64 namespace openfluid { namespace ui { namespace common {
65 
66 
67 inline QColor getRandomColor()
68 {
69  static std::random_device RD;
70  static std::mt19937 G(RD());
71  static std::uniform_int_distribution<> Distri255(0, 255);
72 
73  return QColor(Distri255(G),Distri255(G),Distri255(G));
74 }
75 
76 
77 // =====================================================================
78 // =====================================================================
79 
80 
81 inline double computeLuminance(const QColor& Color)
82 {
83  // Reference: https://www.w3.org/WAI/GL/wiki/Relative_luminance
84  auto adjustColor = [](const double ChannelValue)
85  {
86  double RelValue = ChannelValue/255;
87  if (RelValue <= 0.03928)
88  {
89  return RelValue/12.92;
90  }
91  else
92  {
93  // low-gamma adjust coefficient
94  return std::pow((RelValue + 0.055) / 1.055, 2.4);
95  }
96  };
97  return adjustColor(Color.red())*0.2126 + adjustColor(Color.green())*0.7152 + adjustColor(Color.blue())*0.0722;
98 }
99 
100 
101 // =====================================================================
102 // =====================================================================
103 
104 
105 #if (QT_VERSION_MAJOR < 6)
106 inline void fixLineEdit(QLineEdit* LineEdit,QRegExp SearchRegExp = QRegExp("[^\\w]"),QString NewStr = "_")
107 #else
108 inline void fixLineEdit(QLineEdit* LineEdit,QRegularExpression SearchRegExp = QRegularExpression("[^\\w]"),
109  QString NewStr = "_")
110 #endif
111 {
112  int CPos = LineEdit->cursorPosition();
113  LineEdit->setText(LineEdit->text().replace(SearchRegExp,NewStr));
114  LineEdit->setCursorPosition(CPos);
115 }
116 
117 
118 // =====================================================================
119 // =====================================================================
120 
121 
122 inline bool isHiDPIDevice()
123 {
124  return (qApp->devicePixelRatio() >= 2.0);
125 }
126 
127 
128 // =====================================================================
129 // =====================================================================
130 
131 
132 inline QIcon getIcon(const QString& IconName,const QString& ResourcePath,
133  bool IsLight = false,bool AutoHiDPI = true)
134 {
135  QString IconSuffix = "dark";
136 
137  if (IsLight)
138  {
139  IconSuffix = "light";
140  }
141 
142  QString TmpPath = QString(":%1/icons/%2_%3.png").arg(ResourcePath).arg(IconName).arg(IconSuffix);
143 
144  if (AutoHiDPI && isHiDPIDevice())
145  {
146  QString TmpHiDPIPath = QString(":%1/icons/%2_%3@2x.png").arg(ResourcePath).arg(IconName).arg(IconSuffix);
147 
148  if (QFile::exists(TmpHiDPIPath))
149  {
150  TmpPath = TmpHiDPIPath;
151  }
152  }
153 
154  QIcon TmpIcon(TmpPath);
155 
156  if (IsLight)
157  {
158  QString TmpLightPath = QString(":%1/icons/%2_grayed.png").arg(ResourcePath).arg(IconName);
159 
160  if (AutoHiDPI && isHiDPIDevice())
161  {
162  QString TmpLightHiDPIPath = QString(":%1/icons/%2_grayed@2x.png").arg(ResourcePath).arg(IconName);
163 
164  if (QFile::exists(TmpLightHiDPIPath))
165  {
166  TmpLightPath = TmpLightHiDPIPath;
167  }
168  }
169  TmpIcon.addPixmap(QPixmap(TmpLightPath),QIcon::Disabled);
170  }
171 
172  return TmpIcon;
173 }
174 
175 
176 // =====================================================================
177 // =====================================================================
178 
179 
180 inline QPixmap getImage(const QString& ImageName,const QString& ResourcePath,bool AutoHiDPI = true)
181 {
182  QString TmpPath = QString(":%1/images/%2.png").arg(ResourcePath).arg(ImageName);
183 
184  if (AutoHiDPI && isHiDPIDevice())
185  {
186  QString TmpHiDPIPath = QString(":%1/images/%2@2x.png").arg(ResourcePath).arg(ImageName);
187 
188  if (QFile::exists(TmpHiDPIPath))
189  {
190  TmpPath = TmpHiDPIPath;
191  }
192  }
193 
194  return QPixmap(TmpPath);
195 }
196 
197 
198 // =====================================================================
199 // =====================================================================
200 
201 
202 inline QString createNewFile(QWidget* Parent, const QString& PathString)
203 {
204  if (PathString.isEmpty())
205  {
206  return "";
207  }
208 
209  QString FileToCreate = "";
210  QString FileName = QFileDialog::getSaveFileName(Parent,
211  QApplication::translate("openfluid::ui::common","Create file"),
212  PathString,
213  QApplication::translate("openfluid::ui::common","All files"));
214  if (!FileName.isEmpty() && !FileName.isNull())
215  {
216  FileToCreate = FileName;
217  }
218 
219  if (!FileToCreate.isEmpty())
220  {
221  auto NewFilePath = openfluid::tools::Path(FileToCreate.toStdString());
222 
223  if (NewFilePath.isDirectory())
224  {
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())));
228  }
229  // Checks if new path included in ref path
230  else if (!openfluid::tools::Path(PathString.toStdString()).contains(FileToCreate.toStdString()))
231  {
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())));
236  return "";
237  }
238  else
239  {
240  NewFilePath.makeFile();
241  }
242  return FileToCreate;
243  }
244  return "";
245 }
246 
247 
248 } } } // namespaces
249 
250 
251 #endif /* __OPENFLUID_UICOMMON_UIHELPERS_HPP__ */
Definition: FilesystemPath.hpp:62
bool contains(const std::string &Path) const
FilesystemPath Path
Definition: FilesystemPath.hpp:308
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