2016-01-12 21:43:04 -05:00
|
|
|
#include <iostream>
|
|
|
|
|
2013-09-23 15:53:26 -04:00
|
|
|
#include <QDesktopServices>
|
|
|
|
#include <QPainter>
|
|
|
|
|
|
|
|
#include "RSTextBrowser.h"
|
|
|
|
#include "RSImageBlockWidget.h"
|
|
|
|
|
|
|
|
RSTextBrowser::RSTextBrowser(QWidget *parent) :
|
|
|
|
QTextBrowser(parent)
|
|
|
|
{
|
|
|
|
setOpenExternalLinks(true);
|
|
|
|
setOpenLinks(false);
|
|
|
|
|
|
|
|
mShowImages = true;
|
|
|
|
mImageBlockWidget = NULL;
|
2015-03-16 10:40:30 -04:00
|
|
|
mLinkClickActive = true;
|
2013-09-23 15:53:26 -04:00
|
|
|
|
2015-12-06 15:11:47 -05:00
|
|
|
highliter = new RsSyntaxHighlighter(this);
|
|
|
|
|
2013-09-23 15:53:26 -04:00
|
|
|
connect(this, SIGNAL(anchorClicked(QUrl)), this, SLOT(linkClicked(QUrl)));
|
|
|
|
}
|
|
|
|
|
|
|
|
void RSTextBrowser::linkClicked(const QUrl &url)
|
|
|
|
{
|
2015-03-16 10:40:30 -04:00
|
|
|
if (!mLinkClickActive) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-09-23 15:53:26 -04:00
|
|
|
// some links are opened directly in the QTextBrowser with open external links set to true,
|
|
|
|
// so we handle links by our own
|
|
|
|
|
|
|
|
#ifdef TO_DO
|
|
|
|
// If we want extra file links to be anonymous, we need to insert the actual source here.
|
|
|
|
if(url.host() == HOST_EXTRAFILE)
|
|
|
|
{
|
|
|
|
std::cerr << "Extra file link detected. Adding parent id " << _target_sslid << " to sourcelist" << std::endl;
|
|
|
|
|
|
|
|
RetroShareLink link ;
|
|
|
|
link.fromUrl(url) ;
|
|
|
|
|
|
|
|
link.createExtraFile( link.name(),link.size(),link.hash(), _target_ssl_id) ;
|
|
|
|
|
|
|
|
QDesktopServices::openUrl(link.toUrl());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
QDesktopServices::openUrl(url);
|
|
|
|
}
|
|
|
|
|
|
|
|
void RSTextBrowser::setPlaceholderText(const QString &text)
|
|
|
|
{
|
|
|
|
mPlaceholderText = text;
|
|
|
|
viewport()->repaint();
|
|
|
|
}
|
|
|
|
|
|
|
|
void RSTextBrowser::paintEvent(QPaintEvent *event)
|
|
|
|
{
|
|
|
|
QTextBrowser::paintEvent(event);
|
|
|
|
|
|
|
|
if (mPlaceholderText.isEmpty() == false && document()->isEmpty()) {
|
|
|
|
QWidget *vieportWidget = viewport();
|
|
|
|
QPainter painter(vieportWidget);
|
|
|
|
|
|
|
|
QPen pen = painter.pen();
|
|
|
|
QColor color = pen.color();
|
|
|
|
color.setAlpha(128);
|
|
|
|
pen.setColor(color);
|
|
|
|
painter.setPen(pen);
|
|
|
|
|
|
|
|
painter.drawText(QRect(QPoint(), vieportWidget->size()), Qt::AlignHCenter | Qt::AlignVCenter | Qt::TextWordWrap, mPlaceholderText);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariant RSTextBrowser::loadResource(int type, const QUrl &name)
|
|
|
|
{
|
2016-01-13 00:08:27 -05:00
|
|
|
// case 1: always trust the image if it comes from an internal resource
|
2016-01-12 21:43:04 -05:00
|
|
|
|
2016-01-13 00:08:27 -05:00
|
|
|
if(name.scheme().compare("qrc",Qt::CaseInsensitive)==0 && type == QTextDocument::ImageResource)
|
|
|
|
return QTextBrowser::loadResource(type, name);
|
|
|
|
|
2016-01-13 00:39:22 -05:00
|
|
|
// case 2: only display if the user allows it. Data resources can be bad (svg bombs) but we filter them out globally at the network layer.
|
|
|
|
// It would be good to add here a home-made resource loader that only loads images and not svg crap, just in case.
|
2016-01-13 00:08:27 -05:00
|
|
|
|
|
|
|
if(name.scheme().compare("data",Qt::CaseInsensitive)==0 && mShowImages)
|
|
|
|
return QTextBrowser::loadResource(type, name);
|
|
|
|
|
|
|
|
// case 3: otherwise, do not display
|
|
|
|
|
2016-01-13 00:39:22 -05:00
|
|
|
std::cerr << "TEXTBROWSER: refusing load ressource request: type=" << type << " scheme=" << name.scheme().toStdString() << ", url=" << name.toString().toStdString() << std::endl;
|
2016-01-13 00:08:27 -05:00
|
|
|
|
|
|
|
if (mImageBlockWidget)
|
|
|
|
mImageBlockWidget->show();
|
2013-09-23 15:53:26 -04:00
|
|
|
|
2016-08-07 13:41:44 -04:00
|
|
|
//https://git.merproject.org/lbt/qtbase/commit/6d13e9f29597e0d557857e3f80173faba5368424
|
|
|
|
#if QT_VERSION >= QT_VERSION_CHECK (5, 0, 0)
|
|
|
|
return QPixmap(":/qt-project.org/styles/commonstyle/images/file-16.png");
|
|
|
|
#else
|
2016-01-13 00:08:27 -05:00
|
|
|
return QPixmap(":/trolltech/styles/commonstyle/images/file-16.png");
|
2016-08-07 13:41:44 -04:00
|
|
|
#endif
|
2013-09-23 15:53:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void RSTextBrowser::setImageBlockWidget(RSImageBlockWidget *widget)
|
|
|
|
{
|
|
|
|
if (mImageBlockWidget) {
|
|
|
|
// disconnect
|
|
|
|
disconnect(mImageBlockWidget, SIGNAL(destroyed()), this, SLOT(destroyImageBlockWidget()));
|
|
|
|
disconnect(mImageBlockWidget, SIGNAL(showImages()), this, SLOT(showImages()));
|
|
|
|
}
|
|
|
|
|
|
|
|
mImageBlockWidget = widget;
|
|
|
|
|
|
|
|
if (mImageBlockWidget) {
|
|
|
|
// connect
|
|
|
|
connect(mImageBlockWidget, SIGNAL(destroyed()), this, SLOT(destroyImageBlockWidget()));
|
|
|
|
connect(mImageBlockWidget, SIGNAL(showImages()), this, SLOT(showImages()));
|
|
|
|
}
|
|
|
|
|
|
|
|
resetImagesStatus(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void RSTextBrowser::destroyImageBlockWidget()
|
|
|
|
{
|
|
|
|
mImageBlockWidget = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void RSTextBrowser::showImages()
|
|
|
|
{
|
|
|
|
if (mImageBlockWidget && sender() == mImageBlockWidget) {
|
|
|
|
mImageBlockWidget->hide();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mShowImages) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mShowImages = true;
|
|
|
|
|
|
|
|
QString html = toHtml();
|
|
|
|
clear();
|
|
|
|
setHtml(html);
|
|
|
|
}
|
|
|
|
|
|
|
|
void RSTextBrowser::resetImagesStatus(bool load)
|
|
|
|
{
|
|
|
|
if (mImageBlockWidget) {
|
|
|
|
mImageBlockWidget->hide();
|
|
|
|
}
|
|
|
|
mShowImages = load;
|
|
|
|
}
|
2015-03-16 10:40:30 -04:00
|
|
|
|
|
|
|
void RSTextBrowser::activateLinkClick(bool active)
|
|
|
|
{
|
|
|
|
mLinkClickActive = active;
|
|
|
|
}
|