comtools.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. #include "comtools.h"
  2. #include <QCoreApplication>
  3. #include <QDir>
  4. #include <QDirIterator>
  5. QHash<QString, QString> QSPTools::file_list;
  6. QString QSPTools::file_path;
  7. bool QSPTools::useCaseInsensitiveFilePath = true;
  8. QString QSPTools::GetHexColor(const QColor color)
  9. {
  10. return QString("%1%2%3").arg(color.red(), 2, 16, QLatin1Char( '0' )).arg(color.green(), 2, 16, QLatin1Char( '0' )).arg(color.blue(), 2, 16, QLatin1Char( '0' ));
  11. }
  12. QString QSPTools::HtmlizeWhitespaces(const QString& str)
  13. {
  14. QString::const_iterator i;
  15. QChar ch, quote;
  16. QString out;
  17. size_t j, linepos = 0;
  18. bool isLastSpace = true;
  19. for (i = str.begin(); i != str.end(); ++i)
  20. {
  21. ch = *i;
  22. if(ch == QChar('<'))
  23. {
  24. quote = 0;
  25. while (i != str.end())
  26. {
  27. ch = *i;
  28. if (quote.unicode())
  29. {
  30. if (ch == QChar('\\'))
  31. {
  32. if (++i == str.end()) break;
  33. ch = *i;
  34. if (ch == quote)
  35. {
  36. if(ch == QChar('"'))
  37. {
  38. out.append( QString("&quot;") );
  39. }
  40. else if(ch == QChar('\''))
  41. {
  42. out.append( QString("&apos;") );
  43. }
  44. ++i;
  45. continue;
  46. }
  47. out.append( QChar('\\') );
  48. }
  49. if(ch == QChar('&'))
  50. {
  51. out.append( QString("&amp;") );
  52. }
  53. else if(ch == QChar('\n'))
  54. {
  55. out.append( QString("%0A") );
  56. }
  57. else if(ch == QChar('<'))
  58. {
  59. out.append( QString("&lt;") );
  60. }
  61. else if (ch == QChar('>'))
  62. {
  63. out.append( QString("&gt;") );
  64. }
  65. else
  66. {
  67. if (ch == quote)
  68. quote = 0;
  69. out.append(ch);
  70. }
  71. }
  72. else
  73. {
  74. out.append(ch);
  75. if (ch == QChar('>'))
  76. break;
  77. else if (ch == QChar('"') || ch == QChar('\''))
  78. quote = ch;
  79. }
  80. ++i;
  81. }
  82. if (i == str.end()) return out;
  83. isLastSpace = true;
  84. }
  85. else if(ch == QChar(' '))
  86. {
  87. if (isLastSpace)
  88. out.append( QString("&ensp;") );
  89. else
  90. out.append( QChar(' ') );
  91. isLastSpace = !isLastSpace;
  92. ++linepos;
  93. }
  94. else if(ch == QChar('\r'))
  95. {
  96. }
  97. else if(ch == QChar('\n'))
  98. {
  99. out.append( QString("<br>") );
  100. isLastSpace = true;
  101. linepos = 0;
  102. }
  103. else if(ch == QChar('\t'))
  104. {
  105. for (j = 4 - linepos % 4; j > 0; --j)
  106. {
  107. if (isLastSpace)
  108. out.append( QString("&emsp;") );
  109. else
  110. out.append( QChar(' ') );
  111. isLastSpace = !isLastSpace;
  112. }
  113. linepos += 4 - linepos % 4;
  114. }
  115. else
  116. {
  117. out.append(ch);
  118. isLastSpace = false;
  119. ++linepos;
  120. }
  121. }
  122. return out;
  123. }
  124. QString QSPTools::ProceedAsPlain(const QString& str)
  125. {
  126. QString::const_iterator i;
  127. QChar ch;
  128. QString out;
  129. for (i = str.begin(); i != str.end(); ++i)
  130. {
  131. ch = *i;
  132. if( ch == QChar('<'))
  133. {
  134. out.append( QString("&lt;") );
  135. }
  136. else if(ch == QChar('>'))
  137. {
  138. out.append( QString("&gt;") );
  139. }
  140. else if(ch == QChar('&'))
  141. {
  142. out.append( QString("&amp;") );
  143. }
  144. else
  145. {
  146. out.append(ch);
  147. }
  148. }
  149. return out;
  150. }
  151. QString QSPTools::GetAppPath()
  152. {
  153. return QCoreApplication::applicationDirPath();
  154. }
  155. QString QSPTools::GetCaseInsensitiveFilePath(QString searchDir, QString originalPath)
  156. {
  157. QString new_name = originalPath.replace("\\", "/");
  158. if(new_name.startsWith("/"))
  159. new_name = new_name.remove(0, 1);
  160. #ifndef _WIN32
  161. if(useCaseInsensitiveFilePath)
  162. {
  163. QDir itDir(searchDir);
  164. if(file_path != searchDir && !searchDir.isEmpty())
  165. {
  166. file_list.clear();
  167. QDirIterator it(searchDir, QDir::Files, QDirIterator::Subdirectories);
  168. while (it.hasNext())
  169. {
  170. it.next();
  171. file_list.insert(itDir.relativeFilePath(it.filePath()).toLower(), itDir.relativeFilePath(it.filePath()));
  172. }
  173. file_path = searchDir;
  174. }
  175. if (file_list.contains(new_name.toLower()))
  176. return itDir.relativeFilePath(file_list.value(new_name.toLower()));
  177. }
  178. #endif
  179. return new_name;
  180. }
  181. QString QSPTools::GetCaseInsensitiveAbsoluteFilePath(QString searchDir, QString originalPath)
  182. {
  183. QString new_name = originalPath.replace("\\", "/");
  184. #ifndef _WIN32
  185. if(useCaseInsensitiveFilePath)
  186. {
  187. QDir itDir(searchDir);
  188. if(originalPath.startsWith(searchDir))
  189. new_name = new_name.remove(0, searchDir.length());
  190. if(file_path != searchDir && !searchDir.isEmpty())
  191. {
  192. file_list.clear();
  193. QDirIterator it(searchDir, QDir::Files, QDirIterator::Subdirectories);
  194. while (it.hasNext())
  195. {
  196. it.next();
  197. file_list.insert(itDir.relativeFilePath(it.filePath()).toLower(), itDir.relativeFilePath(it.filePath()));
  198. }
  199. file_path = searchDir;
  200. }
  201. if (file_list.contains(new_name.toLower()))
  202. return itDir.absoluteFilePath(file_list.value(new_name.toLower()));
  203. }
  204. #endif
  205. return new_name;
  206. }
  207. QString QSPTools::qspStrToQt(const QSP_CHAR *str)
  208. {
  209. //return QString::fromWCharArray(str.Str, (int)(str.End - str.Str));
  210. if(str == 0)
  211. return QString("");
  212. else
  213. return QString::fromUtf16(str);
  214. }
  215. QColor QSPTools::wxtoQColor(int wxColor)
  216. {
  217. QColor col;
  218. if(wxColor == 0)
  219. {
  220. col = Qt::black;
  221. return col;
  222. }
  223. col = QColor::fromRgba(wxColor);
  224. int red = col.red();
  225. col.setRed(col.blue());
  226. col.setBlue(red);
  227. return col;
  228. }