PageRenderTime 44ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/src/feeditemwidget.cpp

https://github.com/kitech/snsnotify
C++ | 220 lines | 152 code | 30 blank | 38 comment | 10 complexity | a03ede9096f3af4dee4b89c91bc7e1df MD5 | raw file
  1. #include <QtWebKit>
  2. #include "systemoption.h"
  3. #include "useroption.h"
  4. #include "threadfunctor.h"
  5. #include "feeditemwidget.h"
  6. FeedItemWidget::FeedItemWidget(FeedRecord *feed, QWidget *parent)
  7. : QWidget(parent),
  8. sizeAdjusted(false)
  9. {
  10. this->ui_win.setupUi(this);
  11. QString belongsTo = this->windowTitle().split(QChar('|')).at(1);
  12. this->feed = feed;
  13. Q_ASSERT(feed != NULL);
  14. Q_ASSERT(feed->account != NULL);
  15. //
  16. this->initFeedTemplate();
  17. this->wp = this->ui_win.webView->page();
  18. //this->wv->setContextMenuPolicy(Qt::CustomContextMenu);
  19. this->wp->setLinkDelegationPolicy(QWebPage::DelegateAllLinks);//info us all user clicked link
  20. this->wp->setForwardUnsupportedContent(true);
  21. this->wp->mainFrame()->setScrollBarPolicy(Qt::Vertical, Qt::ScrollBarAlwaysOff);
  22. this->wp->mainFrame()->setScrollBarPolicy(Qt::Horizontal, Qt::ScrollBarAlwaysOff);
  23. QObject::connect(this->ui_win.webView, SIGNAL(customContextMenuRequested(const QPoint &)),
  24. this, SLOT(wkContextMenuRequested(const QPoint &)));
  25. QObject::connect(this->ui_win.webView, SIGNAL(urlChanged(const QUrl &)),
  26. this, SLOT(wkChangeUrl(const QUrl &)));
  27. QObject::connect(this->wp, SIGNAL(linkClicked(const QUrl &)),
  28. this, SLOT(wkActiveLink(const QUrl &)));
  29. QObject::connect(this->wp, SIGNAL(linkHovered(const QString&, const QString &, const QString &)),
  30. this, SLOT(wkHoverLink(const QString&, const QString &, const QString &)));
  31. QObject::connect(this->wp, SIGNAL(geometryChangeRequested(const QRect&)),
  32. this, SLOT(wkChangeGeometry(const QRect &)));
  33. QObject::connect(this->wp, SIGNAL(repaintRequested(const QRect&)),
  34. this, SLOT(wkRequestRepaint(const QRect&)));
  35. QObject::connect(this->wp, SIGNAL(scrollRequested(int, int, const QRect&)),
  36. this, SLOT(wkRequestScroll(int, int, const QRect&)));
  37. QObject::connect(this->wp, SIGNAL(statusBarMessage(const QString&)),
  38. this, SLOT(wkStatusBarMessage(const QString&)));
  39. QObject::connect(this->wp, SIGNAL(loadFinished(bool)),
  40. this, SLOT(wkLoadFinished(bool)));
  41. QObject::connect(this->wp, SIGNAL(downloadRequested(const QNetworkRequest &)),
  42. this, SLOT(wkRequestDownload(const QNetworkRequest &)));
  43. QObject::connect(this->wp, SIGNAL(unsupportedContent(QNetworkReply *)),
  44. this, SLOT(wkDealUnsupportedContent(QNetworkReply*)));
  45. }
  46. FeedItemWidget::~FeedItemWidget()
  47. {
  48. }
  49. bool FeedItemWidget::initFeedTemplate()
  50. {
  51. //blog of liu@tom on hainei
  52. this->ui_win.webView->setHtml(this->feed->content);
  53. return true;
  54. }
  55. bool FeedItemWidget::equalsTo(FeedRecord *f)
  56. {
  57. if(this->feed->equalsTo(f)) return true;
  58. return false;
  59. }
  60. bool FeedItemWidget::similarToKeyword(QString keyword)
  61. {
  62. if(this->feed->content.indexOf(keyword) != -1) {
  63. return true;
  64. }
  65. return false;
  66. }
  67. void FeedItemWidget::activeLink(const QString &link)
  68. {
  69. QString decoded_link = QUrl::fromPercentEncoding(link.toAscii());
  70. q_debug()<<"vv"<<link<<decoded_link;
  71. QDesktopServices::openUrl(decoded_link);
  72. }
  73. void FeedItemWidget::hoverLink(const QString &link)
  74. {
  75. //q_debug()<<"vv"<<link;
  76. if(!link.isEmpty()) {
  77. if(link.startsWith("http"))
  78. this->setToolTip(link);
  79. else{
  80. //this->setToolTip(this->feed->account->sc->homeUri() + link);
  81. }
  82. }else{
  83. this->setToolTip(QString::null);
  84. }
  85. }
  86. //no use , use showEvent is ok
  87. void FeedItemWidget::show()
  88. {
  89. QWidget::show();
  90. }
  91. void FeedItemWidget::showEvent ( QShowEvent * event )
  92. {
  93. //q_debug()<<"?vvvvvvvvvvvvvvvvvvvvvvvvv";
  94. QWidget::showEvent(event);
  95. //q_debug()<<this->ui_win.webView->page()->mainFrame()->renderTreeDump();
  96. this->autoAdjustWebViewSize();
  97. }
  98. //自适应窗口高度
  99. void FeedItemWidget::autoAdjustWebViewSize()
  100. {
  101. if(this->sizeAdjusted) return;
  102. //QSize size = this->ui_win.webView->page()->viewportSize();
  103. //q_debug()<<size;
  104. //QSize(352, 167) , QSize(516, 167)
  105. /*
  106. "layer at (0,0) size 352x167
  107. RenderView at (0,0) size 352x167
  108. layer at (0,0) size 352x34
  109. RenderBlock {HTML} at (0,0) size 352x34
  110. RenderBody {BODY} at (8,8) size 336x18
  111. RenderText {#text} at (0,0) size 38x17
  112. text run at (0,0) width 38: "dsfsdf"
  113. RenderText {#text} at (0,0) size 0x0
  114. RenderText {#text} at (0,0) size 0x0
  115. "
  116. */
  117. QWebView *wv = this->ui_win.webView;
  118. QWebPage *wp = wv->page();
  119. //QWebPage *wp = new QWebPage();
  120. QWebFrame *wf = wp->mainFrame();
  121. QString tree;
  122. tree = wf->renderTreeDump();
  123. //q_debug()<<tree;
  124. int nl = tree.indexOf(QChar('\n'));
  125. tree = tree.left(nl);
  126. //q_debug()<<nl<<tree;
  127. QStringList tmp = tree.split(QChar('x'));
  128. //q_debug()<<tmp;
  129. wv->setFixedHeight(tmp.at(1).toInt());
  130. //this->sizeAdjusted = true;
  131. }
  132. void FeedItemWidget::wkActiveLink(const QUrl &url)
  133. {
  134. //QString decoded_link = QUrl::fromPercentEncoding(link.toAscii());
  135. //q_debug()<<"vv"<<link<<decoded_link;
  136. //QDesktopServices::openUrl(decoded_link);
  137. q_debug()<<url;
  138. QDesktopServices::openUrl(url);
  139. }
  140. void FeedItemWidget::wkHoverLink(const QString &link, const QString &title, const QString &content)
  141. {
  142. if(!link.isEmpty()) {
  143. if(link.startsWith("http"))
  144. this->setToolTip(link);
  145. else{
  146. //this->setToolTip(this->feed->account->sc->homeUri() + link);
  147. }
  148. }else{
  149. this->setToolTip(QString::null);
  150. }
  151. //q_debug()<<link<<title<<content;
  152. }
  153. void FeedItemWidget::wkContextMenuRequested(const QPoint &pos)
  154. {
  155. q_debug()<<pos;
  156. }
  157. void FeedItemWidget::wkChangeUrl(const QUrl &url)
  158. {
  159. q_debug()<<url;
  160. }
  161. void FeedItemWidget::wkChangeGeometry(const QRect &geom)
  162. {
  163. q_debug()<<geom;
  164. }
  165. void FeedItemWidget::wkRequestRepaint(const QRect &dirtyRect)
  166. {
  167. //q_debug()<<dirtyRect;
  168. }
  169. void FeedItemWidget::wkRequestScroll ( int dx, int dy, const QRect & rectToScroll )
  170. {
  171. q_debug()<<dx<<dy<<rectToScroll;
  172. }
  173. void FeedItemWidget::wkStatusBarMessage(const QString &text)
  174. {
  175. //q_debug()<<text;
  176. }
  177. void FeedItemWidget::wkLoadFinished(bool ok)
  178. {
  179. //q_debug()<<ok<<sender();
  180. this->autoAdjustWebViewSize();
  181. }
  182. void FeedItemWidget::wkRequestDownload(const QNetworkRequest & request)
  183. {
  184. //q_debug()<<request;
  185. q_debug()<<"what?"<<request.url();
  186. }
  187. void FeedItemWidget::wkDealUnsupportedContent(QNetworkReply *reply)
  188. {
  189. q_debug()<<reply;
  190. }