comtools.cpp 6.6 KB

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