ctkUtils.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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.commontk.org/LICENSE
  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. // Qt includes
  15. #include <QDebug>
  16. #include <QRegExp>
  17. #include <QString>
  18. #include <QStringList>
  19. #include "ctkUtils.h"
  20. // STD includes
  21. #include <algorithm>
  22. #ifdef _MSC_VER
  23. #pragma warning(disable: 4996)
  24. #endif
  25. //------------------------------------------------------------------------------
  26. void ctk::qListToSTLVector(const QStringList& list,
  27. std::vector<char*>& vector)
  28. {
  29. // Resize if required
  30. if (list.count() != static_cast<int>(vector.size()))
  31. {
  32. vector.resize(list.count());
  33. }
  34. for (int i = 0; i < list.count(); ++i)
  35. {
  36. // Allocate memory
  37. char* str = new char[list[i].size()+1];
  38. strcpy(str, list[i].toLatin1());
  39. vector[i] = str;
  40. }
  41. }
  42. //------------------------------------------------------------------------------
  43. namespace
  44. {
  45. /// Convert QString to std::string
  46. static std::string qStringToSTLString(const QString& qstring)
  47. {
  48. return qstring.toStdString();
  49. }
  50. }
  51. //------------------------------------------------------------------------------
  52. void ctk::qListToSTLVector(const QStringList& list,
  53. std::vector<std::string>& vector)
  54. {
  55. // To avoid unnessesary relocations, let's reserve the required amount of space
  56. vector.reserve(list.size());
  57. std::transform(list.begin(),list.end(),std::back_inserter(vector),&qStringToSTLString);
  58. }
  59. //------------------------------------------------------------------------------
  60. void ctk::stlVectorToQList(const std::vector<std::string>& vector,
  61. QStringList& list)
  62. {
  63. std::transform(vector.begin(),vector.end(),std::back_inserter(list),&QString::fromStdString);
  64. }
  65. //-----------------------------------------------------------------------------
  66. const char *ctkNameFilterRegExp =
  67. "^(.*)\\(([a-zA-Z0-9_.*? +;#\\-\\[\\]@\\{\\}/!<>\\$%&=^~:\\|]*)\\)$";
  68. const char *ctkValidWildCard =
  69. "^[\\w\\s\\.\\*\\_\\~\\$\\[\\]]+$";
  70. //-----------------------------------------------------------------------------
  71. QStringList ctk::nameFilterToExtensions(const QString& nameFilter)
  72. {
  73. QRegExp regexp(QString::fromLatin1(ctkNameFilterRegExp));
  74. int i = regexp.indexIn(nameFilter);
  75. if (i < 0)
  76. {
  77. QRegExp isWildCard(QString::fromLatin1(ctkValidWildCard));
  78. if (isWildCard.indexIn(nameFilter) >= 0)
  79. {
  80. return QStringList(nameFilter);
  81. }
  82. return QStringList();
  83. }
  84. QString f = regexp.cap(2);
  85. return f.split(QLatin1Char(' '), QString::SkipEmptyParts);
  86. }
  87. //-----------------------------------------------------------------------------
  88. QStringList ctk::nameFiltersToExtensions(const QStringList& nameFilters)
  89. {
  90. QStringList extensions;
  91. foreach(const QString& nameFilter, nameFilters)
  92. {
  93. extensions << nameFilterToExtensions(nameFilter);
  94. }
  95. return extensions;
  96. }
  97. //-----------------------------------------------------------------------------
  98. QString ctk::extensionToRegExp(const QString& extension)
  99. {
  100. // typically *.jpg
  101. QRegExp extensionExtractor("\\*\\.(\\w+)");
  102. int pos = extensionExtractor.indexIn(extension);
  103. if (pos < 0)
  104. {
  105. return QString();
  106. }
  107. return ".*\\." + extensionExtractor.cap(1) + "?$";
  108. }
  109. //-----------------------------------------------------------------------------
  110. QRegExp ctk::nameFiltersToRegExp(const QStringList& nameFilters)
  111. {
  112. QString pattern;
  113. foreach(const QString& nameFilter, nameFilters)
  114. {
  115. foreach(const QString& extension, nameFilterToExtensions(nameFilter))
  116. {
  117. QString regExpExtension = extensionToRegExp(extension);
  118. if (!regExpExtension.isEmpty())
  119. {
  120. if (pattern.isEmpty())
  121. {
  122. pattern = "(";
  123. }
  124. else
  125. {
  126. pattern += "|";
  127. }
  128. pattern +=regExpExtension;
  129. }
  130. }
  131. }
  132. if (pattern.isEmpty())
  133. {
  134. pattern = ".+";
  135. }
  136. else
  137. {
  138. pattern += ")";
  139. }
  140. return QRegExp(pattern);
  141. }
  142. //-----------------------------------------------------------------------------
  143. int ctk::significantDecimals(double value)
  144. {
  145. QString number = QString::number(value, 'f', 16);
  146. QString fractional = number.section('.', 1, 1);
  147. if (fractional.length() <= 2)
  148. {
  149. return fractional.length() - 1;
  150. }
  151. QChar previous;
  152. int previousRepeat=0;
  153. bool only0s = true;
  154. for (int i = 0; i < fractional.length(); ++i)
  155. {
  156. QChar digit = fractional.at(i);
  157. if (digit != '0')
  158. {
  159. only0s = false;
  160. }
  161. if (digit == previous && previousRepeat == 2 &&
  162. !only0s)
  163. {
  164. if (digit == '0' || digit == '9')
  165. {
  166. return i - previousRepeat;
  167. }
  168. return i;
  169. }
  170. if (i == fractional.length() - 1)
  171. {
  172. if (previousRepeat > 2)
  173. {
  174. return i - previousRepeat;
  175. }
  176. return i;
  177. }
  178. // get ready for next
  179. if (previous != digit)
  180. {
  181. previous = digit;
  182. previousRepeat = 1;
  183. }
  184. else
  185. {
  186. ++previousRepeat;
  187. }
  188. }
  189. return -1;
  190. }