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 <QColor>
44 #include <QRegExp>
45 #include <QLineEdit>
46 #include <QIcon>
47 #include <QApplication>
48 #include <QFile>
49 
50 
51 namespace openfluid { namespace ui { namespace common {
52 
53 
54 inline QColor getRandomColor()
55 {
56  return QColor(qrand() % 256,qrand() % 256,qrand() % 256);
57 }
58 
59 
60 // =====================================================================
61 // =====================================================================
62 
63 
64 inline void fixLineEdit(QLineEdit* LineEdit,QRegExp SearchRegExp = QRegExp("[^\\w]"),QString NewStr = "_")
65 {
66  int CPos = LineEdit->cursorPosition();
67  LineEdit->setText(LineEdit->text().replace(SearchRegExp,NewStr));
68  LineEdit->setCursorPosition(CPos);
69 }
70 
71 
72 // =====================================================================
73 // =====================================================================
74 
75 
76 inline bool isHiDPIDevice()
77 {
78  return (qApp->devicePixelRatio() >= 2.0);
79 }
80 
81 
82 // =====================================================================
83 // =====================================================================
84 
85 
86 inline QIcon getIcon(const QString& IconName,const QString& ResourcePath,
87  bool IsLight = false,bool AutoHiDPI = true)
88 {
89  QString IconSuffix = "dark";
90 
91  if (IsLight)
92  {
93  IconSuffix = "light";
94  }
95 
96  QString TmpPath = QString(":%1/icons/%2_%3.png").arg(ResourcePath).arg(IconName).arg(IconSuffix);
97 
98  if (AutoHiDPI && isHiDPIDevice())
99  {
100  QString TmpHiDPIPath = QString(":%1/icons/%2_%3@2x.png").arg(ResourcePath).arg(IconName).arg(IconSuffix);
101 
102  if (QFile::exists(TmpHiDPIPath))
103  {
104  TmpPath = TmpHiDPIPath;
105  }
106  }
107 
108  QIcon TmpIcon(TmpPath);
109 
110  if (IsLight)
111  {
112  QString TmpLightPath = QString(":%1/icons/%2_grayed.png").arg(ResourcePath).arg(IconName);
113 
114  if (AutoHiDPI && isHiDPIDevice())
115  {
116  QString TmpLightHiDPIPath = QString(":%1/icons/%2_grayed@2x.png").arg(ResourcePath).arg(IconName);
117 
118  if (QFile::exists(TmpLightHiDPIPath))
119  {
120  TmpLightPath = TmpLightHiDPIPath;
121  }
122  }
123  TmpIcon.addPixmap(QPixmap(TmpLightPath),QIcon::Disabled);
124  }
125 
126  return TmpIcon;
127 }
128 
129 
130 // =====================================================================
131 // =====================================================================
132 
133 
134 inline QPixmap getImage(const QString& ImageName,const QString& ResourcePath,bool AutoHiDPI = true)
135 {
136  QString TmpPath = QString(":%1/images/%2.png").arg(ResourcePath).arg(ImageName);
137 
138  if (AutoHiDPI && isHiDPIDevice())
139  {
140  QString TmpHiDPIPath = QString(":%1/images/%2@2x.png").arg(ResourcePath).arg(ImageName);
141 
142  if (QFile::exists(TmpHiDPIPath))
143  {
144  TmpPath = TmpHiDPIPath;
145  }
146  }
147 
148  return QPixmap(TmpPath);
149 }
150 
151 
152 } } } // namespaces
153 
154 
155 #endif /* __OPENFLUID_UICOMMON_UIHELPERS_HPP__ */
QColor getRandomColor()
Definition: UIHelpers.hpp:54
void fixLineEdit(QLineEdit *LineEdit, QRegExp SearchRegExp=QRegExp("[^\]"), QString NewStr="_")
Definition: UIHelpers.hpp:64
QIcon getIcon(const QString &IconName, const QString &ResourcePath, bool IsLight=false, bool AutoHiDPI=true)
Definition: UIHelpers.hpp:86
bool isHiDPIDevice()
Definition: UIHelpers.hpp:76
QPixmap getImage(const QString &ImageName, const QString &ResourcePath, bool AutoHiDPI=true)
Definition: UIHelpers.hpp:134
Definition: ApplicationException.hpp:47