Documentation for OpenFLUID 2.2.0
QtHelpers.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 /**
34  @file QtHelpers.hpp
35 
36  @author Jean-Christophe FABRE <jean-christophe.fabre@inra.fr>
37  @author Armel THÖNI <armel.thoni@inrae.fr>
38  */
39 
40 
41 #ifndef __OPENFLUID_UI_QTHELPERS_HPP__
42 #define __OPENFLUID_UI_QTHELPERS_HPP__
43 
44 
45 #include <string>
46 #include <list>
47 #include <vector>
48 #include <set>
49 #include <map>
50 #include <regex>
51 
52 #include <QVariant>
53 #include <QString>
54 #include <QStringList>
55 #include <QDateTime>
56 #include <QPoint>
57 #include <QSize>
58 
59 #if (QT_VERSION_MAJOR < 6)
60 #else
61 #include <QRegularExpression>
62 #endif
63 
66 
67 
68 namespace openfluid { namespace ui {
69 
70 
71 /**
72  Transforms the given string to a string compatible with the INI files managed with QSettings.
73  @param[in] Str the string to transform
74  @return The transformed string
75 */
76 inline QString toIniCompatible(const std::string& Str)
77 {
78  return (QString::fromStdString(Str));
79 }
80 
81 
82 // =====================================================================
83 // =====================================================================
84 
85 
86 /**
87  Transforms the given variable from an INI file managed with QSettings to a string
88  @param[in] Var the variable to transform
89  @return The variable transformed into a string
90 */
91 inline std::string fromIniCompatible(const QVariant& Var)
92 {
93 #if (QT_VERSION_MAJOR < 6)
94  if (Var.type() == QVariant::StringList)
95  {
96 #else
97  if (Var.typeId() == QMetaType::QStringList)
98  {
99 #endif
100  return Var.toStringList().join(", ").toStdString();
101  }
102  else
103  {
104  return Var.toString().toStdString();
105  }
106 }
107 
108 
109 // =====================================================================
110 // =====================================================================
111 
112 
113 /**
114  Transforms a QStringList into a std::list of std::string
115  @param[in] StrList the QStringList to transform
116  @return The QStringList transformed into a std::list of std::string
117 */
118 inline std::list<std::string> toStdStringList(const QStringList& StrList)
119 {
120  std::list<std::string> TmpList;
121 
122  for (const auto& QStr : StrList)
123  {
124  TmpList.push_back(QStr.toStdString());
125  }
126 
127  return TmpList;
128 }
129 
130 
131 // =====================================================================
132 // =====================================================================
133 
134 
135 /**
136  Transforms a QStringList into a std::vector of std::string
137  @param[in] StrList the QStringList to transform
138  @return The QStringList transformed into a std::vector of std::string
139 */
140 inline std::vector<std::string> toStdStringVector(const QStringList& StrList)
141 {
142  std::vector<std::string> TmpVector;
143 
144  for (const auto& QStr : StrList)
145  {
146  TmpVector.push_back(QStr.toStdString());
147  }
148 
149  return TmpVector;
150 }
151 
152 
153 // =====================================================================
154 // =====================================================================
155 
156 
157 /**
158  Transforms a std::map of QString,QString into a std::map of std::string,std::string
159  @param[in] StrMap the map to transform
160  @return The std::map of QString,QString>transformed into a std::map of std::string,std::string
161 */
162 inline std::map<std::string,std::string> toStdStringMap(const std::map<QString,QString>& StrMap)
163 {
164  std::map<std::string,std::string> TmpMap;
165 
166  for (const auto& QStrStr : StrMap)
167  {
168  TmpMap[QStrStr.first.toStdString()] = QStrStr.second.toStdString();
169  }
170 
171  return TmpMap;
172 }
173 
174 
175 // =====================================================================
176 // =====================================================================
177 
178 
179 /**
180  Transforms an OpenFLUID DateTime to a QDateTime
181  @param[in] DT The OpenFLUID DateTime to transform
182  @return The DT transformed into QDateTime
183 */
184 inline QDateTime toQDateTime(const openfluid::core::DateTime& DT)
185 {
186  QDate D(DT.getYear(),DT.getMonth(),DT.getDay());
187  QTime T(DT.getHour(),DT.getMinute(),DT.getSecond());
188 
189  return QDateTime(D,T,Qt::UTC);
190 }
191 
192 
193 // =====================================================================
194 // =====================================================================
195 
196 
197 /**
198  Transforms a std::vector of std::string into a QStringList
199  @param[in] StrVect the std::vector of std::string to transform
200  @return The StrVect transformed into a QStringList
201 */
202 inline QStringList toQStringList(const std::vector<std::string>& StrVect)
203 {
204  QStringList QSL;
205 
206  for (const auto& Str : StrVect)
207  {
208  QSL.append(QString::fromStdString(Str));
209  }
210 
211  return QSL;
212 }
213 
214 
215 // =====================================================================
216 // =====================================================================
217 
218 
219 /**
220  Transforms a std::set of std::string into a QStringList
221  @param[in] StrSet the std::set of std::string to transform
222  @return The StrSet transformed into a QStringList
223 */
224 inline QStringList toQStringList(const std::set<std::string>& StrSet)
225 {
226  QStringList QSL;
227 
228  for (const auto& Str : StrSet)
229  {
230  QSL.append(QString::fromStdString(Str));
231  }
232 
233  return QSL;
234 }
235 
236 
237 // =====================================================================
238 // =====================================================================
239 
240 
241 /**
242  Transforms a std::list of std::string into a QStringList
243  @param[in] StrList the std::list of std::string to transform
244  @return The StrList transformed into a QStringList
245 */
246 inline QStringList toQStringList(const std::list<std::string>& StrList)
247 {
248  QStringList QSL;
249 
250  for (const auto& Str : StrList)
251  {
252  QSL.append(QString::fromStdString(Str));
253  }
254 
255  return QSL;
256 }
257 
258 
259 // =====================================================================
260 // =====================================================================
261 
262 
263 /**
264  Transforms a std::set of int into a QStringList
265  @param[in] IntSet the std::set of int to transform
266  @return The intSet transformed into a QStringList
267 */
268 inline QStringList toQStringList(const std::set<int>& IntSet)
269 {
270  QStringList QSL;
271 
272  for (const auto& Int : IntSet)
273  {
274  QSL.append(QString("%1").arg(Int));
275  }
276 
277  return QSL;
278 }
279 
280 
281 // =====================================================================
282 // =====================================================================
283 
284 
285 /**
286  Converts arguments string to list. It first splits the string using spaces that are not inclosed in quotes,
287  then remove enclosing quotes for each argument if any.
288  @param[in] ArgsStr the argument string to convert
289  @return The list of arguments
290 */
291 inline QStringList convertArgsStringToList(const QString& ArgsStr)
292 {
293  // converting string args as a list, without splitting spaces in quotes
294 #if (QT_VERSION_MAJOR < 6)
295  QStringList ArgsList = ArgsStr.split(QRegExp("\\s(?=(?:\"[^\"]*\"|[^\"])*$)"),QString::SkipEmptyParts);
296 #else
297  QStringList ArgsList = ArgsStr.split(QRegularExpression("\\s(?=(?:\"[^\"]*\"|[^\"])*$)"),Qt::SkipEmptyParts);
298 #endif
299 
300  // removing of quotes
301  for (auto& Arg : ArgsList)
302  {
303  if (Arg.startsWith("\"") && Arg.endsWith("\""))
304  {
305  Arg.remove(0,1);
306  Arg.remove(Arg.size()-1,1);
307  }
308  }
309 
310  return ArgsList;
311 }
312 
313 
314 // =====================================================================
315 // =====================================================================
316 
317 
318 /**
319  Escapes characters in the given string to be compatible with the XML format.
320  @param[in] Str the string to escape
321  @return The escaped string
322 */
323 inline QString escapeXMLEntities(const QString& Str)
324 {
325  QString EscapedStr = Str;
326  EscapedStr.replace("&","&amp;").replace(">","&gt;").replace("<","&lt;").replace("\"","&quot;").replace("'","&apos;");
327  return EscapedStr;
328 }
329 
330 
331 // =====================================================================
332 // =====================================================================
333 
334 
335 /**
336  Decodes XML entities to characters
337  @param[in] Str the string to decode
338  @return The decoded string
339 */
340 inline QString decodeXMLEntities(const QString& Str)
341 {
342  QString DecodedStr = Str;
343  DecodedStr.replace("&amp;","&").replace("&gt;",">").replace("&lt;","<").replace("&quot;","\"");
344  return DecodedStr;
345 }
346 
347 
348 // =====================================================================
349 // =====================================================================
350 
351 
352 /**
353  Converts a \@Point formatted string to a QPoint
354  @param[in] Str the \@Point formatted string (e.g. "\@Point(-127 53)")
355  @return the QPoint object
356 */
357 inline QPoint toQPoint(const std::string& Str)
358 {
359  auto Coords = openfluid::tools::fromGeometryString(Str,"Point");
360  return QPoint(Coords.first,Coords.second);
361 }
362 
363 
364 // =====================================================================
365 // =====================================================================
366 
367 
368 /**
369  Converts a QPoint to a \@Point formatted string
370  @param[in] Point the QPoint object to convert
371  @return the \@Point formatted string (e.g. "\@Point(-127 53)")
372 */
373 inline std::string fromQPoint(const QPoint& Point)
374 {
375  if (!Point.isNull())
376  {
377  return openfluid::tools::toGeometryString("Point",Point.x(),Point.y());
378  }
379 
380  return "";
381 }
382 
383 
384 // =====================================================================
385 // =====================================================================
386 
387 
388 /**
389  Converts a \@Size formatted string to a QSize
390  @param[in] Str the \@Size formatted string (e.g. "\@Size(1920 1080)")
391  @return the QSize object
392 */
393 inline QSize toQSize(const std::string& Str)
394 {
395  auto Size = openfluid::tools::fromGeometryString(Str,"Size");
396  return QSize(Size.first,Size.second);
397 }
398 
399 
400 // =====================================================================
401 // =====================================================================
402 
403 
404 /**
405  Converts a QSize to a \@Size formatted string
406  @param[in] Size the Size object to convert
407  @return the \@Size formatted string (e.g. "\@Size(1920 1080)")
408 */
409 inline std::string fromQSize(const QSize& Size)
410 {
411  if (!Size.isNull())
412  {
413  return openfluid::tools::toGeometryString("Size",Size.width(),Size.height());
414  }
415 
416  return "";
417 }
418 
419 } } // namespaces
420 
421 
422 #endif /* __OPENFLUID_UI_QTHELPERS_HPP__ */
Class for management of date and time information.
Definition: DateTime.hpp:88
int getYear() const
Definition: DateTime.hpp:211
int getSecond() const
Definition: DateTime.hpp:256
int getHour() const
Definition: DateTime.hpp:238
int getDay() const
Definition: DateTime.hpp:229
int getMonth() const
Definition: DateTime.hpp:220
int getMinute() const
Definition: DateTime.hpp:247
std::string OPENFLUID_API toGeometryString(const std::string &GeomInfo, int Value1, int Value2)
std::pair< int, int > OPENFLUID_API fromGeometryString(const std::string &Str, const std::string &GeomInfo)
QString escapeXMLEntities(const QString &Str)
Definition: QtHelpers.hpp:323
std::vector< std::string > toStdStringVector(const QStringList &StrList)
Definition: QtHelpers.hpp:140
std::string fromQSize(const QSize &Size)
Definition: QtHelpers.hpp:409
std::list< std::string > toStdStringList(const QStringList &StrList)
Definition: QtHelpers.hpp:118
std::string fromQPoint(const QPoint &Point)
Definition: QtHelpers.hpp:373
QString toIniCompatible(const std::string &Str)
Definition: QtHelpers.hpp:76
QString decodeXMLEntities(const QString &Str)
Definition: QtHelpers.hpp:340
QDateTime toQDateTime(const openfluid::core::DateTime &DT)
Definition: QtHelpers.hpp:184
QSize toQSize(const std::string &Str)
Definition: QtHelpers.hpp:393
QStringList convertArgsStringToList(const QString &ArgsStr)
Definition: QtHelpers.hpp:291
QPoint toQPoint(const std::string &Str)
Definition: QtHelpers.hpp:357
QStringList toQStringList(const std::vector< std::string > &StrVect)
Definition: QtHelpers.hpp:202
std::map< std::string, std::string > toStdStringMap(const std::map< QString, QString > &StrMap)
Definition: QtHelpers.hpp:162
std::string fromIniCompatible(const QVariant &Var)
Definition: QtHelpers.hpp:91
Definition: ApplicationException.hpp:47