1
0

qspwebbox_webkit.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. #include "qspwebbox_webkit.h"
  2. #include <QFileInfo>
  3. #include <QPalette>
  4. #include <QAbstractScrollArea>
  5. #include <QScrollBar>
  6. #include <QPainter>
  7. #include <QWebSettings>
  8. #include <QEventLoop>
  9. #include "comtools.h"
  10. QspWebBox::QspWebBox(QWidget *parent) : QWebView(parent)
  11. {
  12. settings()->setDefaultTextEncoding("utf-8");
  13. setFocusPolicy(Qt::NoFocus);
  14. //setFrameStyle(QFrame::NoFrame);
  15. //setFrameShadow(QFrame::Plain);
  16. setContextMenuPolicy( Qt::NoContextMenu );
  17. setContentsMargins(0,0,0,0);
  18. m_isUseHtml = false;
  19. showPlainText = false;
  20. m_videoFix = true;
  21. m_font = font();
  22. //setOpenLinks(false);
  23. page()->setLinkDelegationPolicy(QWebPage::DelegateAllLinks);
  24. QNetworkAccessManager *oldManager = page()->networkAccessManager();
  25. qspManager = new QspNetworkAccessManager(oldManager, this);
  26. page()->setNetworkAccessManager(qspManager);
  27. page()->setForwardUnsupportedContent(true);
  28. load(QUrl("qsp:/"));
  29. }
  30. QspWebBox::~QspWebBox()
  31. {
  32. }
  33. void QspWebBox::SetIsHtml(bool isHtml)
  34. {
  35. if (m_isUseHtml != isHtml)
  36. {
  37. m_isUseHtml = isHtml;
  38. RefreshUI();
  39. }
  40. }
  41. void QspWebBox::RefreshUI(bool isScroll)
  42. {
  43. QString color(QSPTools::GetHexColor(GetForegroundColor()));
  44. QString str = m_text;
  45. QString text;
  46. // if(m_isUseHtml)
  47. // {
  48. // if(str.endsWith("\r"))
  49. // str.chop(1);
  50. // if(str.endsWith("\n"))
  51. // str.chop(1);
  52. // str = str.replace("\r", "").replace("\n", "<br>").replace("<video ", "<img ", Qt::CaseInsensitive);
  53. // text = str.replace("</center><br>", "</center>", Qt::CaseInsensitive).replace("</table><br>", "</table>", Qt::CaseInsensitive);
  54. // }
  55. // else
  56. // {
  57. // text = str;
  58. // }
  59. if(m_videoFix)
  60. {
  61. int copypos = 0;
  62. int startIndex = str.indexOf("<video", 0, Qt::CaseInsensitive);
  63. while (startIndex >= 0)
  64. {
  65. int endIndex = str.indexOf(">", startIndex, Qt::CaseInsensitive);
  66. if(endIndex < 0)
  67. break;
  68. endIndex = endIndex + 1;
  69. text.append(str.mid(copypos, startIndex + 6 - copypos));
  70. if(!str.mid(startIndex, endIndex - startIndex).contains("autoplay", Qt::CaseInsensitive))
  71. text.append(" autoplay");
  72. if(!str.mid(startIndex, endIndex - startIndex).contains("loop", Qt::CaseInsensitive))
  73. text.append(" loop");
  74. text.append(str.mid(startIndex + 6, endIndex - startIndex - 6));
  75. copypos = endIndex;
  76. int cloaseTegPos = str.indexOf("</video>", 0, Qt::CaseInsensitive);
  77. if(cloaseTegPos == -1)
  78. text.append("</video>");
  79. else
  80. {
  81. int nextV = str.indexOf("<video", endIndex, Qt::CaseInsensitive);
  82. if(nextV == -1)
  83. text.append("</video>");
  84. else
  85. {
  86. if(cloaseTegPos > nextV)
  87. text.append("</video>");
  88. }
  89. }
  90. startIndex = str.indexOf("<video", endIndex, Qt::CaseInsensitive);
  91. }
  92. text.append(str.mid(copypos));
  93. }
  94. else
  95. text = str;
  96. text = QSPTools::HtmlizeWhitespaces(m_isUseHtml ? text : QSPTools::ProceedAsPlain(text));
  97. if(showPlainText)
  98. qspManager->SetPlainText(text);
  99. else
  100. qspManager->SetHtml(text);
  101. QString url_str = QByteArray::fromPercentEncoding(url().toString().toUtf8());
  102. if(url_str.compare("qsp:" , Qt::CaseInsensitive) != 0 && url_str.compare("qsp:/" , Qt::CaseInsensitive) != 0)
  103. {
  104. QEventLoop loop;
  105. connect(this, SIGNAL(loadFinished(bool)), &loop, SLOT(quit()));
  106. load(QUrl("qsp:/"));
  107. loop.exec();
  108. }
  109. triggerPageAction(QWebPage::ReloadAndBypassCache);
  110. }
  111. void QspWebBox::LoadBackImage(const QString& fileName)
  112. {
  113. qspManager->SetBackgroundImage(fileName);
  114. QFileInfo file(m_path + fileName);
  115. QString path(file.absoluteFilePath());
  116. }
  117. void QspWebBox::SetText(const QString& text, bool isScroll)
  118. {
  119. if (m_text != text)
  120. {
  121. if (isScroll)
  122. {
  123. if (m_text.isEmpty() || !text.startsWith(m_text))
  124. isScroll = false;
  125. }
  126. m_text = text;
  127. RefreshUI(isScroll);
  128. }
  129. }
  130. void QspWebBox::SetTextFont(const QFont& new_font)
  131. {
  132. if (m_font != new_font)
  133. {
  134. m_font = new_font;
  135. qspManager->SetTextFont(new_font);
  136. }
  137. }
  138. bool QspWebBox::SetLinkColor(const QColor &color)
  139. {
  140. if(m_linkColor != color)
  141. {
  142. m_linkColor = color;
  143. qspManager->SetLinkColor(color);
  144. RefreshUI();
  145. return true;
  146. }
  147. return false;
  148. }
  149. void QspWebBox::SetGamePath(const QString &path)
  150. {
  151. m_path = path;
  152. qspManager->SetGamePath(path);
  153. }
  154. //Returns the background color of the window.
  155. QColor QspWebBox::GetBackgroundColor()
  156. {
  157. return m_backColor;
  158. }
  159. //The meaning of foreground colour varies according to the window class; it may be the text colour or other colour, or it may not be used at all. Additionally, not all native controls support changing their foreground colour so this method may change their colour only partially or even not at all.
  160. QColor QspWebBox::GetForegroundColor()
  161. {
  162. return m_fontColor;
  163. }
  164. //Returns true if the color was really changed, false if it was already set to this color and nothing was done.
  165. bool QspWebBox::SetBackgroundColor(const QColor &color)
  166. {
  167. if(m_backColor != color)
  168. {
  169. m_backColor = color;
  170. qspManager->SetBackgroundColor(color);
  171. RefreshUI();
  172. return true;
  173. }
  174. return false;
  175. }
  176. bool QspWebBox::SetForegroundColor(const QColor &color)
  177. {
  178. if(m_fontColor != color)
  179. {
  180. m_fontColor = color;
  181. qspManager->SetForegroundColor(color);
  182. RefreshUI();
  183. return true;
  184. }
  185. return false;
  186. }
  187. void QspWebBox::SetShowPlainText(bool isPlain)
  188. {
  189. showPlainText = isPlain;
  190. RefreshUI();
  191. }
  192. void QspWebBox::SetVideoFix(bool isFix)
  193. {
  194. m_videoFix = isFix;
  195. }