Manual for OpenFLUID 2.1.11

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