-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
43c6a5b
commit a8336c3
Showing
9 changed files
with
105 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
#include "webtextbrowser.h" | ||
|
||
#include <QRegularExpression> | ||
#include <QNetworkAccessManager> | ||
#include <QNetworkReply> | ||
#include <QBuffer> | ||
|
||
#include "util/smoothscrollbar.h" | ||
|
||
WebTextBrowser::WebTextBrowser(QWidget *parent) : | ||
QTextBrowser(parent) | ||
{ | ||
setVerticalScrollBar(new SmoothScrollBar(this)); | ||
} | ||
|
||
void WebTextBrowser::setHtml(const QString &html) | ||
{ | ||
for(auto &&reply : qAsConst(replies_)){ | ||
if(!reply->isFinished()){ | ||
reply->abort(); | ||
reply->deleteLater(); | ||
} | ||
} | ||
replies_.clear(); | ||
|
||
QTextBrowser::setHtml(html); | ||
static QRegularExpression reg(R"_(<img .*?src="(.*?)".*?>)_"); | ||
for(auto i = reg.globalMatch(html); i.hasNext();){ | ||
auto match = i.next(); | ||
QString imageStr = match.captured(1); | ||
QUrl imageUrl(imageStr); | ||
|
||
QNetworkRequest request(imageUrl); | ||
request.setAttribute(QNetworkRequest::RedirectPolicyAttribute, true); | ||
|
||
static QNetworkAccessManager accessManager; | ||
auto reply = accessManager.get(request); | ||
replies_ << reply; | ||
connect(reply, &QNetworkReply::finished, this, [=]{ | ||
if(reply->error() != QNetworkReply::NoError) return; | ||
QPixmap pixmap; | ||
pixmap.loadFromData(reply->readAll()); | ||
|
||
QByteArray byteArray; | ||
QBuffer buffer(&byteArray); | ||
pixmap.toImage().save(&buffer, "JPEG"); | ||
QString base64Img = "data:image/png;base64," + QString::fromLatin1(byteArray.toBase64().data()); | ||
QTextBrowser::setHtml(QTextBrowser::toHtml().replace(imageUrl.toString(), base64Img)); | ||
replies_.removeOne(reply); | ||
reply->deleteLater(); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#ifndef WEBTEXTBROWSER_H | ||
#define WEBTEXTBROWSER_H | ||
|
||
#include <QTextBrowser> | ||
|
||
class QNetworkReply; | ||
class WebTextBrowser : public QTextBrowser | ||
{ | ||
Q_OBJECT | ||
public: | ||
explicit WebTextBrowser(QWidget *parent = nullptr); | ||
|
||
void setHtml(const QString &html); | ||
private: | ||
QList<QNetworkReply*> replies_; | ||
}; | ||
|
||
#endif // WEBTEXTBROWSER_H |