PageRenderTime 23ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/retroshare-gui/src/gui/common/RSTextBrowser.cpp

https://gitlab.com/cave/RetroShare
C++ | 136 lines | 102 code | 29 blank | 5 comment | 19 complexity | 0532905481404393ba4df816a90b68fb MD5 | raw file
Possible License(s): 0BSD, GPL-2.0
  1. #include <QDesktopServices>
  2. #include <QPainter>
  3. #include "RSTextBrowser.h"
  4. #include "RSImageBlockWidget.h"
  5. RSTextBrowser::RSTextBrowser(QWidget *parent) :
  6. QTextBrowser(parent)
  7. {
  8. setOpenExternalLinks(true);
  9. setOpenLinks(false);
  10. mShowImages = true;
  11. mImageBlockWidget = NULL;
  12. mLinkClickActive = true;
  13. connect(this, SIGNAL(anchorClicked(QUrl)), this, SLOT(linkClicked(QUrl)));
  14. }
  15. void RSTextBrowser::linkClicked(const QUrl &url)
  16. {
  17. if (!mLinkClickActive) {
  18. return;
  19. }
  20. // some links are opened directly in the QTextBrowser with open external links set to true,
  21. // so we handle links by our own
  22. #ifdef TO_DO
  23. // If we want extra file links to be anonymous, we need to insert the actual source here.
  24. if(url.host() == HOST_EXTRAFILE)
  25. {
  26. std::cerr << "Extra file link detected. Adding parent id " << _target_sslid << " to sourcelist" << std::endl;
  27. RetroShareLink link ;
  28. link.fromUrl(url) ;
  29. link.createExtraFile( link.name(),link.size(),link.hash(), _target_ssl_id) ;
  30. QDesktopServices::openUrl(link.toUrl());
  31. }
  32. else
  33. #endif
  34. QDesktopServices::openUrl(url);
  35. }
  36. void RSTextBrowser::setPlaceholderText(const QString &text)
  37. {
  38. mPlaceholderText = text;
  39. viewport()->repaint();
  40. }
  41. void RSTextBrowser::paintEvent(QPaintEvent *event)
  42. {
  43. QTextBrowser::paintEvent(event);
  44. if (mPlaceholderText.isEmpty() == false && document()->isEmpty()) {
  45. QWidget *vieportWidget = viewport();
  46. QPainter painter(vieportWidget);
  47. QPen pen = painter.pen();
  48. QColor color = pen.color();
  49. color.setAlpha(128);
  50. pen.setColor(color);
  51. painter.setPen(pen);
  52. painter.drawText(QRect(QPoint(), vieportWidget->size()), Qt::AlignHCenter | Qt::AlignVCenter | Qt::TextWordWrap, mPlaceholderText);
  53. }
  54. }
  55. QVariant RSTextBrowser::loadResource(int type, const QUrl &name)
  56. {
  57. if (mShowImages || type != QTextDocument::ImageResource || name.scheme().compare("data", Qt::CaseInsensitive) != 0) {
  58. return QTextBrowser::loadResource(type, name);
  59. }
  60. if (mImageBlockWidget) {
  61. mImageBlockWidget->show();
  62. }
  63. return QPixmap(":/trolltech/styles/commonstyle/images/file-16.png");
  64. }
  65. void RSTextBrowser::setImageBlockWidget(RSImageBlockWidget *widget)
  66. {
  67. if (mImageBlockWidget) {
  68. // disconnect
  69. disconnect(mImageBlockWidget, SIGNAL(destroyed()), this, SLOT(destroyImageBlockWidget()));
  70. disconnect(mImageBlockWidget, SIGNAL(showImages()), this, SLOT(showImages()));
  71. }
  72. mImageBlockWidget = widget;
  73. if (mImageBlockWidget) {
  74. // connect
  75. connect(mImageBlockWidget, SIGNAL(destroyed()), this, SLOT(destroyImageBlockWidget()));
  76. connect(mImageBlockWidget, SIGNAL(showImages()), this, SLOT(showImages()));
  77. }
  78. resetImagesStatus(false);
  79. }
  80. void RSTextBrowser::destroyImageBlockWidget()
  81. {
  82. mImageBlockWidget = NULL;
  83. }
  84. void RSTextBrowser::showImages()
  85. {
  86. if (mImageBlockWidget && sender() == mImageBlockWidget) {
  87. mImageBlockWidget->hide();
  88. }
  89. if (mShowImages) {
  90. return;
  91. }
  92. mShowImages = true;
  93. QString html = toHtml();
  94. clear();
  95. setHtml(html);
  96. }
  97. void RSTextBrowser::resetImagesStatus(bool load)
  98. {
  99. if (mImageBlockWidget) {
  100. mImageBlockWidget->hide();
  101. }
  102. mShowImages = load;
  103. }
  104. void RSTextBrowser::activateLinkClick(bool active)
  105. {
  106. mLinkClickActive = active;
  107. }