ctkUtils.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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 <QRegExp>
  16. #include <QString>
  17. #include <QStringList>
  18. #include "ctkUtils.h"
  19. // STD includes
  20. #include <algorithm>
  21. #ifdef _MSC_VER
  22. #pragma warning(disable: 4996)
  23. #endif
  24. //------------------------------------------------------------------------------
  25. void ctk::qListToSTLVector(const QStringList& list,
  26. std::vector<char*>& vector)
  27. {
  28. // Resize if required
  29. if (list.count() != static_cast<int>(vector.size()))
  30. {
  31. vector.resize(list.count());
  32. }
  33. for (int i = 0; i < list.count(); ++i)
  34. {
  35. // Allocate memory
  36. char* str = new char[list[i].size()+1];
  37. strcpy(str, list[i].toLatin1());
  38. vector[i] = str;
  39. }
  40. }
  41. //------------------------------------------------------------------------------
  42. namespace
  43. {
  44. /// Convert QString to std::string
  45. static std::string qStringToSTLString(const QString& qstring)
  46. {
  47. return qstring.toStdString();
  48. }
  49. }
  50. //------------------------------------------------------------------------------
  51. void ctk::qListToSTLVector(const QStringList& list,
  52. std::vector<std::string>& vector)
  53. {
  54. // To avoid unnessesary relocations, let's reserve the required amount of space
  55. vector.reserve(list.size());
  56. std::transform(list.begin(),list.end(),std::back_inserter(vector),&qStringToSTLString);
  57. }
  58. //------------------------------------------------------------------------------
  59. void ctk::stlVectorToQList(const std::vector<std::string>& vector,
  60. QStringList& list)
  61. {
  62. std::transform(vector.begin(),vector.end(),std::back_inserter(list),&QString::fromStdString);
  63. }
  64. //-----------------------------------------------------------------------------
  65. const char *ctkNameFilterRegExp =
  66. "^(.*)\\(([a-zA-Z0-9_.*? +;#\\-\\[\\]@\\{\\}/!<>\\$%&=^~:\\|]*)\\)$";
  67. const char *ctkValidWildCard =
  68. "^[\\w\\s\\.\\*\\_\\~\\$\\[\\]]+$";
  69. //-----------------------------------------------------------------------------
  70. QStringList ctk::nameFilterToExtensions(const QString& nameFilter)
  71. {
  72. QRegExp regexp(QString::fromLatin1(ctkNameFilterRegExp));
  73. int i = regexp.indexIn(nameFilter);
  74. if (i < 0)
  75. {
  76. QRegExp isWildCard(QString::fromLatin1(ctkValidWildCard));
  77. if (isWildCard.indexIn(nameFilter) >= 0)
  78. {
  79. return QStringList(nameFilter);
  80. }
  81. return QStringList();
  82. }
  83. QString f = regexp.cap(2);
  84. return f.split(QLatin1Char(' '), QString::SkipEmptyParts);
  85. }
  86. //-----------------------------------------------------------------------------
  87. QStringList ctk::nameFiltersToExtensions(const QStringList& nameFilters)
  88. {
  89. QStringList extensions;
  90. foreach(const QString& nameFilter, nameFilters)
  91. {
  92. extensions << nameFilterToExtensions(nameFilter);
  93. }
  94. return extensions;
  95. }
  96. //-----------------------------------------------------------------------------
  97. QString ctk::extensionToRegExp(const QString& extension)
  98. {
  99. // typically *.jpg
  100. QRegExp extensionExtractor("\\*\\.(\\w+)");
  101. int pos = extensionExtractor.indexIn(extension);
  102. if (pos < 0)
  103. {
  104. return QString();
  105. }
  106. return ".*\\." + extensionExtractor.cap(1) + "?$";
  107. }
  108. //-----------------------------------------------------------------------------
  109. QRegExp ctk::nameFiltersToRegExp(const QStringList& nameFilters)
  110. {
  111. QString pattern;
  112. foreach(const QString& nameFilter, nameFilters)
  113. {
  114. foreach(const QString& extension, nameFilterToExtensions(nameFilter))
  115. {
  116. QString regExpExtension = extensionToRegExp(extension);
  117. if (!regExpExtension.isEmpty())
  118. {
  119. if (pattern.isEmpty())
  120. {
  121. pattern = "(";
  122. }
  123. else
  124. {
  125. pattern += "|";
  126. }
  127. pattern +=regExpExtension;
  128. }
  129. }
  130. }
  131. if (pattern.isEmpty())
  132. {
  133. pattern = ".+";
  134. }
  135. else
  136. {
  137. pattern += ")";
  138. }
  139. return QRegExp(pattern);
  140. }