ctkUtils.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. 16 is the max that can be returned, -1 for NaN numbers. When the
  64. /// number of decimals is not obvious, it defaults to defaultDecimals if it is
  65. /// different from -1, 16 otherwise.
  66. /// e.g. significantDecimals(120.01) returns 2
  67. /// significantDecimals(123456.1333333) returns 3
  68. /// significantDecimals(123456.26999999999999996) returns 2
  69. /// significantDecimals(123456.12345678901234567, 3) return 3
  70. /// See more cases in the test ctkUtilsSignificantDecimalsTest1
  71. int CTK_CORE_EXPORT significantDecimals(double value, int defaultDecimals = -1);
  72. ///
  73. /// \ingroup Core
  74. /// Return the order of magnitude of a number or numeric_limits<int>::min() if
  75. /// the order of magnitude can't be computed (e.g. 0, inf, Nan, denorm)...
  76. /// e.g.: orderOfMagnitude(1) returns 0
  77. /// orderOfMagnitude(10) returns 1
  78. /// orderOfMagnitude(99) returns 1
  79. /// orderOfMagnitude(101) returns 2
  80. /// orderOfMagnitude(0.1) returns -1
  81. /// orderOfMagnitude(0.15) returns -1
  82. /// orderOfMagnitude(0.) returns NaN
  83. /// See more cases in the test ctkUtilsOrderOfMagnitudeTest1
  84. int CTK_CORE_EXPORT orderOfMagnitude(double value);
  85. ///
  86. /// \ingroup Core
  87. /// Return the order of magnitude of a number.
  88. /// e.g.: closestPowerOfTen(11) returns 10
  89. /// closestPowerOfTen(-40) returns -10
  90. /// closestPowerOfTen(99) returns 100
  91. /// closestPowerOfTen(0.012) returns 0.010
  92. /// closestPowerOfTen(0.) returns 0
  93. /// See more cases in the test ctkUtilsClosestPowerOfTenTest1
  94. double CTK_CORE_EXPORT closestPowerOfTen(double value);
  95. ///
  96. /// \ingroup Core
  97. /// Remove a directory recursively.
  98. /// \param dirName The directory to remove
  99. /// \return <code>true</code> on success, <code>false</code> otherwise.
  100. /// \sa QDir::rmdir
  101. bool CTK_CORE_EXPORT removeDirRecursively(const QString & dirName);
  102. ///
  103. /// \ingroup Core
  104. /// Copy a directory recursively
  105. /// \param srcPath The directory to be copied
  106. /// \param dstPath The directory where the file should be copied
  107. /// \return <code>true</code> on success, <code>false</code> otherwise.
  108. /// \sa QFile::copy
  109. bool CTK_CORE_EXPORT copyDirRecursively(const QString &srcPath, const QString &dstPath);
  110. ///
  111. /// \ingroup Core
  112. /// Convert Qt::HANDLE to string
  113. /// \sa Qt::HANDLE
  114. QString CTK_CORE_EXPORT qtHandleToString(Qt::HANDLE handle);
  115. ///
  116. /// \ingroup Core
  117. /// \brief Compute the milli seconds from one QDateTime to an other.
  118. ///
  119. /// This function can be used to correctly compute the amount of milli
  120. /// seconds from <code>t1</code> to <code>t2</code>. The QDateTime objects
  121. /// are converted to Qt::UTC to take daylight saving time into account. This is for
  122. /// back-wards compatibility with Qt 4.6. Since Qt 4.7 there exists
  123. /// a QDateTime::msecsTo() method which should be used instead, after
  124. /// bumping the minimum required Qt version for CTK.
  125. qint64 CTK_CORE_EXPORT msecsTo(const QDateTime& t1, const QDateTime& t2);
  126. }
  127. #endif