Manual for OpenFLUID 2.1.10

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 */
37 
38 
39 #ifndef __OPENFLUID_UICOMMON_UIHELPERS_HPP__
40 #define __OPENFLUID_UICOMMON_UIHELPERS_HPP__
41 
42 
43 #include <cmath>
44 
45 #include <QColor>
46 #include <QRegExp>
47 #include <QLineEdit>
48 #include <QIcon>
49 #include <QApplication>
50 #include <QFile>
51 
52 
53 namespace openfluid { namespace ui { namespace common {
54 
55 
56 inline QColor getRandomColor()
57 {
58  return QColor(qrand() % 256,qrand() % 256,qrand() % 256);
59 }
60 
61 
62 // =====================================================================
63 // =====================================================================
64 
65 
66 inline double computeLuminance(const QColor& Color)
67 {
68  // Reference: https://www.w3.org/WAI/GL/wiki/Relative_luminance
69  auto adjustColor = [](const double ChannelValue)
70  {
71  double RelValue = ChannelValue/255;
72  if (RelValue <= 0.03928)
73  {
74  return RelValue/12.92;
75  }
76  else
77  {
78  // low-gamma adjust coefficient
79  return std::pow((RelValue + 0.055) / 1.055, 2.4);
80  }
81  };
82  return adjustColor(Color.red())*0.2126 + adjustColor(Color.green())*0.7152 + adjustColor(Color.blue())*0.0722;
83 }
84 
85 
86 // =====================================================================
87 // =====================================================================
88 
89 
90 inline void fixLineEdit(QLineEdit* LineEdit,QRegExp SearchRegExp = QRegExp("[^\\w]"),QString NewStr = "_")
91 {
92  int CPos = LineEdit->cursorPosition();
93  LineEdit->setText(LineEdit->text().replace(SearchRegExp,NewStr));
94  LineEdit->setCursorPosition(CPos);
95 }
96 
97 
98 // =====================================================================
99 // =====================================================================
100 
101 
102 inline bool isHiDPIDevice()
103 {
104  return (qApp->devicePixelRatio() >= 2.0);
105 }
106 
107 
108 // =====================================================================
109 // =====================================================================
110 
111 
112 inline QIcon getIcon(const QString& IconName,const QString& ResourcePath,
113  bool IsLight = false,bool AutoHiDPI = true)
114 {
115  QString IconSuffix = "dark";
116 
117  if (IsLight)
118  {
119  IconSuffix = "light";
120  }
121 
122  QString TmpPath = QString(":%1/icons/%2_%3.png").arg(ResourcePath).arg(IconName).arg(IconSuffix);
123 
124  if (AutoHiDPI && isHiDPIDevice())
125  {
126  QString TmpHiDPIPath = QString(":%1/icons/%2_%3@2x.png").arg(ResourcePath).arg(IconName).arg(IconSuffix);
127 
128  if (QFile::exists(TmpHiDPIPath))
129  {
130  TmpPath = TmpHiDPIPath;
131  }
132  }
133 
134  QIcon TmpIcon(TmpPath);
135 
136  if (IsLight)
137  {
138  QString TmpLightPath = QString(":%1/icons/%2_grayed.png").arg(ResourcePath).arg(IconName);
139 
140  if (AutoHiDPI && isHiDPIDevice())
141  {
142  QString TmpLightHiDPIPath = QString(":%1/icons/%2_grayed@2x.png").arg(ResourcePath).arg(IconName);
143 
144  if (QFile::exists(TmpLightHiDPIPath))
145  {
146  TmpLightPath = TmpLightHiDPIPath;
147  }
148  }
149  TmpIcon.addPixmap(QPixmap(TmpLightPath),QIcon::Disabled);
150  }
151 
152  return TmpIcon;
153 }
154 
155 
156 // =====================================================================
157 // =====================================================================
158 
159 
160 inline QPixmap getImage(const QString& ImageName,const QString& ResourcePath,bool AutoHiDPI = true)
161 {
162  QString TmpPath = QString(":%1/images/%2.png").arg(ResourcePath).arg(ImageName);
163 
164  if (AutoHiDPI && isHiDPIDevice())
165  {
166  QString TmpHiDPIPath = QString(":%1/images/%2@2x.png").arg(ResourcePath).arg(ImageName);
167 
168  if (QFile::exists(TmpHiDPIPath))
169  {
170  TmpPath = TmpHiDPIPath;
171  }
172  }
173 
174  return QPixmap(TmpPath);
175 }
176 
177 
178 } } } // namespaces
179 
180 
181 #endif /* __OPENFLUID_UICOMMON_UIHELPERS_HPP__ */
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