ctkUtils.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*=========================================================================
  2. Library: CTK
  3. Copyright (c) Kitware Inc.
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0.txt
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. =========================================================================*/
  14. #ifndef __ctkUtils_h
  15. #define __ctkUtils_h
  16. // Qt includes
  17. #include <QStringList>
  18. #include <QDateTime>
  19. // STD includes
  20. #include <vector>
  21. #include "ctkCoreExport.h"
  22. namespace ctk {
  23. ///
  24. /// \ingroup Core
  25. /// Convert a QStringList to Vector of char*
  26. /// Caller will be responsible to delete the content of the vector
  27. void CTK_CORE_EXPORT qListToSTLVector(const QStringList& list, std::vector<char*>& vector);
  28. ///
  29. /// \ingroup Core
  30. /// Convert a QStringList to a Vector of string
  31. void CTK_CORE_EXPORT qListToSTLVector(const QStringList& list, std::vector<std::string>& vector);
  32. ///
  33. /// \ingroup Core
  34. /// Convert a Vector of string to QStringList
  35. void CTK_CORE_EXPORT stlVectorToQList(const std::vector<std::string>& vector, QStringList& list);
  36. ///
  37. /// \ingroup Core
  38. /// Convert a nameFilter to a list of file extensions:
  39. /// "Images (*.png *.jpg *.tiff)" -> "*.png", "*.jpg", "*.tiff"
  40. /// Note: the nameFilter can be a simple wildcard "*.jpg" in that case, it
  41. /// will simply return it.
  42. /// \sa nameFiltersToExtensions
  43. QStringList CTK_CORE_EXPORT nameFilterToExtensions(const QString& nameFilter);
  44. ///
  45. /// \ingroup Core
  46. /// Convert a nameFilter to a list of file extensions:
  47. /// "Images (*.png *.jpg *.tiff)", "Text (*.txt)" -> "*.png", "*.jpg", "*.tiff", "*.txt"
  48. QStringList CTK_CORE_EXPORT nameFiltersToExtensions(const QStringList& nameFilters);
  49. ///
  50. /// \ingroup Core
  51. /// Convert a wildcar extension filter ("*.jpg") into a regular expression string
  52. /// "*.jpg" -> ".*\\.jpg?$"
  53. QString CTK_CORE_EXPORT extensionToRegExp(const QString& extension);
  54. ///
  55. /// \ingroup Core
  56. /// Convert a list of wildcar extension filters ("*.jpg")
  57. /// into a regular expression string
  58. /// "*.jpg", "*.txt" -> "(.*\\.jpg?$|.*\\.txt?$)"
  59. QRegExp CTK_CORE_EXPORT nameFiltersToRegExp(const QStringList& nameFilters);
  60. ///
  61. /// \ingroup Core
  62. /// Return a "smart" number of decimals needed to display (in a gui) a floating
  63. /// number.
  64. /// e.g. significantDecimals(120.01) returns 2
  65. /// significantDecimals(123456.1333333) returns 3
  66. /// significantDecimals(123456.26999999999999996) returns 2
  67. /// See more cases in the test ctkUtilsSignificantDecimalsTest1
  68. int CTK_CORE_EXPORT significantDecimals(double value);
  69. ///
  70. /// \ingroup Core
  71. /// Return the order of magnitude of a number.
  72. /// e.g.: orderOfMagnitude(1) returns 0
  73. /// orderOfMagnitude(10) returns 1
  74. /// orderOfMagnitude(99) returns 1
  75. /// orderOfMagnitude(101) returns 2
  76. /// orderOfMagnitude(0.1) returns -1
  77. /// orderOfMagnitude(0.15) returns -1
  78. /// orderOfMagnitude(0.) returns NaN
  79. /// See more cases in the test ctkUtilsOrderOfMagnitudeTest1
  80. int CTK_CORE_EXPORT orderOfMagnitude(double value);
  81. ///
  82. /// \ingroup Core
  83. /// Return the order of magnitude of a number.
  84. /// e.g.: closestPowerOfTen(11) returns 10
  85. /// closestPowerOfTen(-40) returns -10
  86. /// closestPowerOfTen(99) returns 100
  87. /// closestPowerOfTen(0.012) returns 0.010
  88. /// closestPowerOfTen(0.) returns 0
  89. /// See more cases in the test ctkUtilsClosestPowerOfTenTest1
  90. double CTK_CORE_EXPORT closestPowerOfTen(double value);
  91. ///
  92. /// \ingroup Core
  93. /// Remove a directory recursively.
  94. /// \param dirName The directory to remove
  95. /// \return <code>true</code> on success, <code>false</code> otherwise.
  96. /// \sa QDir::rmdir
  97. bool CTK_CORE_EXPORT removeDirRecursively(const QString & dirName);
  98. ///
  99. /// \ingroup Core
  100. /// Convert Qt::HANDLE to string
  101. /// \sa Qt::HANDLE
  102. QString CTK_CORE_EXPORT qtHandleToString(Qt::HANDLE handle);
  103. ///
  104. /// \ingroup Core
  105. /// \brief Compute the milli seconds from one QDateTime to an other.
  106. ///
  107. /// This function can be used to correctly compute the amount of milli
  108. /// seconds from <code>t1</code> to <code>t2</code>. The QDateTime objects
  109. /// are converted to Qt::UTC to take daylight saving time into account. This is for
  110. /// back-wards compatibility with Qt 4.6. Since Qt 4.7 there exists
  111. /// a QDateTime::msecsTo() method which should be used instead, after
  112. /// bumping the minimum required Qt version for CTK.
  113. qint64 CTK_CORE_EXPORT msecsTo(const QDateTime& t1, const QDateTime& t2);
  114. }
  115. #endif