qspwebbox.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. #include "qspwebbox.h"
  2. #include <QFileInfo>
  3. #include <QPalette>
  4. #include <QAbstractScrollArea>
  5. #include <QScrollBar>
  6. #include <QPainter>
  7. #include <QWebEngineSettings>
  8. //#include <QTimer>
  9. #include <QEventLoop>
  10. #include "comtools.h"
  11. //#include "qspwebengineurlrequestinterceptor.h"
  12. QspWebBox::QspWebBox(QWidget *parent) : QWebEngineView(parent)
  13. {
  14. m_isQuit = false;
  15. settings()->setDefaultTextEncoding("utf-8");
  16. setFocusPolicy(Qt::NoFocus);
  17. settings()->setAttribute(QWebEngineSettings::PlaybackRequiresUserGesture, false);
  18. settings()->setUnknownUrlSchemePolicy(QWebEngineSettings::AllowAllUnknownUrlSchemes);
  19. //setFrameStyle(QFrame::NoFrame);
  20. //setFrameShadow(QFrame::Plain);
  21. setContextMenuPolicy( Qt::NoContextMenu );
  22. setContentsMargins(0,0,0,0);
  23. m_isUseHtml = false;
  24. showPlainText = false;
  25. m_videoFix = true;
  26. m_font = font();
  27. //setOpenLinks(false);
  28. //QspWebEngineUrlRequestInterceptor *qwuri = new QspWebEngineUrlRequestInterceptor(this);
  29. //profile->setRequestInterceptor(qwuri);
  30. profile.installUrlSchemeHandler(QByteArray("qsp"), &qweush);
  31. profile.installUrlSchemeHandler(QByteArray("exec"), &qeweush);
  32. page()->triggerAction(QWebEnginePage::Stop);
  33. page()->deleteLater();
  34. QWebEnginePage * newpage = new QWebEnginePage(&profile, this);
  35. QWebChannel *channel = new QWebChannel(newpage);
  36. channel->registerObject(QStringLiteral("qsp"), &qspJS);
  37. newpage->setWebChannel(channel);
  38. newpage->settings()->setDefaultTextEncoding("utf-8");
  39. newpage->settings()->setAttribute(QWebEngineSettings::PlaybackRequiresUserGesture, false);
  40. newpage->settings()->setUnknownUrlSchemePolicy(QWebEngineSettings::AllowAllUnknownUrlSchemes);
  41. page()->deleteLater();
  42. setPage(newpage);
  43. connect(&qeweush, SIGNAL(qspLinkClicked(QUrl)), this, SLOT(OnQspLinkClicked(QUrl)));
  44. QEventLoop loop;
  45. connect(page(), SIGNAL(loadFinished(bool)), &loop, SLOT(quit()));
  46. page()->load(QUrl("qsp:/"));
  47. loop.exec();
  48. }
  49. QspWebBox::~QspWebBox()
  50. {
  51. }
  52. void QspWebBox::SetIsHtml(bool isHtml)
  53. {
  54. if (m_isUseHtml != isHtml)
  55. {
  56. m_isUseHtml = isHtml;
  57. RefreshUI();
  58. }
  59. }
  60. void QspWebBox::RefreshUI(bool isScroll)
  61. {
  62. if(m_isQuit)
  63. return;
  64. QString color(QSPTools::GetHexColor(GetForegroundColor()));
  65. QString str = m_text;
  66. QString text;
  67. // if(m_isUseHtml)
  68. // {
  69. // if(str.endsWith("\r"))
  70. // str.chop(1);
  71. // if(str.endsWith("\n"))
  72. // str.chop(1);
  73. // str = str.replace("\r", "").replace("\n", "<br>").replace("<video ", "<img ", Qt::CaseInsensitive);
  74. // text = str.replace("</center><br>", "</center>", Qt::CaseInsensitive).replace("</table><br>", "</table>", Qt::CaseInsensitive);
  75. // }
  76. // else
  77. // {
  78. // text = str;
  79. // }
  80. if(m_videoFix)
  81. {
  82. int copypos = 0;
  83. int startIndex = str.indexOf("<video", 0, Qt::CaseInsensitive);
  84. while (startIndex >= 0)
  85. {
  86. int endIndex = str.indexOf(">", startIndex, Qt::CaseInsensitive);
  87. if(endIndex < 0)
  88. break;
  89. endIndex = endIndex + 1;
  90. text.append(str.mid(copypos, startIndex + 6 - copypos));
  91. if(!str.mid(startIndex, endIndex - startIndex).contains("autoplay", Qt::CaseInsensitive))
  92. text.append(" autoplay");
  93. if(!str.mid(startIndex, endIndex - startIndex).contains("loop", Qt::CaseInsensitive))
  94. text.append(" loop");
  95. text.append(str.mid(startIndex + 6, endIndex - startIndex - 6));
  96. copypos = endIndex;
  97. int cloaseTegPos = str.indexOf("</video>", 0, Qt::CaseInsensitive);
  98. if(cloaseTegPos == -1)
  99. text.append("</video>");
  100. else
  101. {
  102. int nextV = str.indexOf("<video", endIndex, Qt::CaseInsensitive);
  103. if(nextV == -1)
  104. text.append("</video>");
  105. else
  106. {
  107. if(cloaseTegPos > nextV)
  108. text.append("</video>");
  109. }
  110. }
  111. startIndex = str.indexOf("<video", endIndex, Qt::CaseInsensitive);
  112. }
  113. text.append(str.mid(copypos));
  114. }
  115. else
  116. text = str;
  117. text = QSPTools::HtmlizeWhitespaces(m_isUseHtml ? text : QSPTools::ProceedAsPlain(text));
  118. if(showPlainText)
  119. qweush.SetPlainText(text);
  120. else
  121. qweush.SetHtml(text);
  122. QString url_str = QByteArray::fromPercentEncoding(url().toString().toUtf8());
  123. if(url_str.compare("qsp:" , Qt::CaseInsensitive) != 0 && url_str.compare("qsp:/" , Qt::CaseInsensitive) != 0)
  124. {
  125. page()->triggerAction(QWebEnginePage::Stop);
  126. page()->deleteLater();
  127. QWebEnginePage * newpage = new QWebEnginePage(&profile, this);
  128. QWebChannel *channel = new QWebChannel(newpage);
  129. channel->registerObject(QStringLiteral("qsp"), &qspJS);
  130. newpage->setWebChannel(channel);
  131. newpage->settings()->setDefaultTextEncoding("utf-8");
  132. newpage->settings()->setAttribute(QWebEngineSettings::PlaybackRequiresUserGesture, false);
  133. newpage->settings()->setUnknownUrlSchemePolicy(QWebEngineSettings::AllowAllUnknownUrlSchemes);
  134. setPage(newpage);
  135. QEventLoop loop;
  136. connect(page(), SIGNAL(loadFinished(bool)), &loop, SLOT(quit()));
  137. page()->load(QUrl("qsp:/"));
  138. loop.exec();
  139. }
  140. page()->triggerAction(QWebEnginePage::ReloadAndBypassCache);
  141. //QTimer wtimer;
  142. //wtimer.setSingleShot(true);
  143. //QEventLoop loop;
  144. //connect(this, SIGNAL(loadFinished(bool)), &loop, SLOT(quit()) );
  145. //connect(&wtimer, SIGNAL(timeout()), &loop, SLOT(quit()));
  146. //wtimer.start(400);
  147. //loop.exec();
  148. //if(!wtimer.isActive())
  149. //{
  150. // qDebug() << "timeout";
  151. //}
  152. //if (isScroll) verticalScrollBar()->setValue(verticalScrollBar()->maximum());
  153. }
  154. void QspWebBox::LoadBackImage(const QString& fileName)
  155. {
  156. qweush.SetBackgroundImage(fileName);
  157. QFileInfo file(m_path + fileName);
  158. QString path(file.absoluteFilePath());
  159. }
  160. void QspWebBox::SetText(const QString& text, bool isScroll)
  161. {
  162. if (m_text != text)
  163. {
  164. if (isScroll)
  165. {
  166. if (m_text.isEmpty() || !text.startsWith(m_text))
  167. isScroll = false;
  168. }
  169. m_text = text;
  170. RefreshUI(isScroll);
  171. }
  172. }
  173. void QspWebBox::SetTextFont(const QFont& new_font)
  174. {
  175. if (m_font != new_font)
  176. {
  177. m_font = new_font;
  178. qweush.SetTextFont(new_font);
  179. }
  180. }
  181. bool QspWebBox::SetLinkColor(const QColor &color)
  182. {
  183. if(m_linkColor != color)
  184. {
  185. m_linkColor = color;
  186. qweush.SetLinkColor(color);
  187. RefreshUI();
  188. return true;
  189. }
  190. return false;
  191. }
  192. void QspWebBox::SetGamePath(const QString &path)
  193. {
  194. m_path = path;
  195. qweush.SetGamePath(path);
  196. }
  197. //Returns the background color of the window.
  198. QColor QspWebBox::GetBackgroundColor()
  199. {
  200. return m_backColor;
  201. }
  202. //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.
  203. QColor QspWebBox::GetForegroundColor()
  204. {
  205. return m_fontColor;
  206. }
  207. //Returns true if the color was really changed, false if it was already set to this color and nothing was done.
  208. bool QspWebBox::SetBackgroundColor(const QColor &color)
  209. {
  210. if(m_backColor != color)
  211. {
  212. m_backColor = color;
  213. qweush.SetBackgroundColor(color);
  214. RefreshUI();
  215. return true;
  216. }
  217. return false;
  218. }
  219. bool QspWebBox::SetForegroundColor(const QColor &color)
  220. {
  221. if(m_fontColor != color)
  222. {
  223. m_fontColor = color;
  224. qweush.SetForegroundColor(color);
  225. RefreshUI();
  226. return true;
  227. }
  228. return false;
  229. }
  230. void QspWebBox::SetShowPlainText(bool isPlain)
  231. {
  232. showPlainText = isPlain;
  233. RefreshUI();
  234. }
  235. void QspWebBox::SetVideoFix(bool isFix)
  236. {
  237. m_videoFix = isFix;
  238. }
  239. void QspWebBox::SetHead(const QString &head)
  240. {
  241. if(m_head != head)
  242. {
  243. m_head = head;
  244. qweush.SetHead(head);
  245. RefreshUI();
  246. }
  247. }
  248. void QspWebBox::SetFontType(const int fontType)
  249. {
  250. if(m_fontType != fontType)
  251. {
  252. m_fontType = fontType;
  253. qweush.SetFontType(fontType);
  254. RefreshUI();
  255. }
  256. }
  257. void QspWebBox::SetSizeType(const int sizeType)
  258. {
  259. if(m_sizeType != sizeType)
  260. {
  261. m_sizeType = sizeType;
  262. qweush.SetSizeType(sizeType);
  263. RefreshUI();
  264. }
  265. }
  266. void QspWebBox::SetCustomCSS(bool customCSS)
  267. {
  268. qweush.SetCustomCSS(customCSS);
  269. }
  270. void QspWebBox::Quit()
  271. {
  272. m_isQuit = true;
  273. page()->triggerAction(QWebEnginePage::Stop);
  274. page()->deleteLater();
  275. QWebEnginePage * newpage = new QWebEnginePage(this);
  276. setPage(newpage);
  277. QEventLoop loop;
  278. connect(page(), SIGNAL(loadFinished(bool)), &loop, SLOT(quit()));
  279. page()->load(QUrl("about:blank"));
  280. loop.exec();
  281. }
  282. void QspWebBox::OnQspLinkClicked(QUrl url)
  283. {
  284. emit qspLinkClicked(url);
  285. }