Skip to content

Commit

Permalink
add web img display for mod desc
Browse files Browse the repository at this point in the history
  • Loading branch information
kaniol-lck committed Jul 4, 2024
1 parent 43c6a5b commit a8336c3
Show file tree
Hide file tree
Showing 9 changed files with 105 additions and 51 deletions.
5 changes: 4 additions & 1 deletion src/download/assetcache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,12 @@ bool AssetCache::exists() const
void AssetCache::download()
{
QNetworkRequest request(url_);
request.setAttribute(QNetworkRequest::RedirectPolicyAttribute,true); // 允许网址重定向
request.setAttribute(QNetworkRequest::RedirectPolicyAttribute, true);
static QNetworkAccessManager accessManager;
auto reply = accessManager.get(request);
connect(reply, &QNetworkReply::redirected, this, [=](QUrl url){
qDebug() << "redirected: " << url_ << " -> " << url;
});
connect(reply, &QNetworkReply::finished, this, [=]{
if(reply->error() != QNetworkReply::NoError) return;
QFile file(destFilePath_);
Expand Down
9 changes: 7 additions & 2 deletions src/ui/curseforge/curseforgemoddialog.ui
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
<number>0</number>
</property>
<item row="0" column="0">
<widget class="QTextBrowser" name="modDescription">
<widget class="WebTextBrowser" name="modDescription">
<property name="frameShape">
<enum>QFrame::NoFrame</enum>
</property>
Expand Down Expand Up @@ -252,9 +252,14 @@
<customwidget>
<class>TagsFlowWidget</class>
<extends>QWidget</extends>
<header>ui/tagsflowwidget.h</header>
<header location="global">ui/tagsflowwidget.h</header>
<container>1</container>
</customwidget>
<customwidget>
<class>WebTextBrowser</class>
<extends>QTextBrowser</extends>
<header location="global">ui/webtextbrowser.h</header>
</customwidget>
<customwidget>
<class>CurseforgeFileListWidget</class>
<extends>QWidget</extends>
Expand Down
38 changes: 2 additions & 36 deletions src/ui/curseforge/curseforgemodinfowidget.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "curseforgemoddialog.h"
#include "curseforgemodinfowidget.h"
#include "qbuffer.h"
#include "ui_curseforgemodinfowidget.h"

#include <QDesktopServices>
Expand All @@ -8,11 +9,8 @@

#include "curseforgemodbrowser.h"
#include "curseforge/curseforgemod.h"
#include "util/smoothscrollbar.h"
#include "util/flowlayout.h"
#include "util/funcutil.h"
#include "util/youdaotranslator.h"
#include "local/localmodpath.h"
#include "curseforge/curseforgemanager.h"

CurseforgeModInfoWidget::CurseforgeModInfoWidget(CurseforgeModBrowser *parent) :
Expand All @@ -25,9 +23,6 @@ CurseforgeModInfoWidget::CurseforgeModInfoWidget(CurseforgeModBrowser *parent) :
ui->modName->addAction(ui->actionOpen_Website_Link);
ui->modName->addAction(ui->actionCopy_Website_Link);
ui->scrollArea->setVisible(false);
//lag
// ui->scrollArea->setVerticalScrollBar(new SmoothScrollBar(this));
ui->modDescription->setVerticalScrollBar(new SmoothScrollBar(this));
}

CurseforgeModInfoWidget::~CurseforgeModInfoWidget()
Expand Down Expand Up @@ -59,36 +54,6 @@ void CurseforgeModInfoWidget::setMod(CurseforgeMod *mod)
ui->modDescription->setCursor(Qt::BusyCursor);
mod->acquireDescription();
}

// //update gallery
// if(mod->modInfo().images().isEmpty())
// ui->tabWidget->removeTab(1);
// for(const auto &image : mod->modInfo().images()){
// auto item = new QListWidgetItem();
// item->setText(image.title);
// item->setToolTip(image.description);
// item->setData(Qt::UserRole, image.url);
// item->setData(Qt::UserRole + 1, image.title);
// item->setData(Qt::UserRole + 2, image.description);
// item->setSizeHint(QSize(260, 260));
// ui->galleryListWidget->addItem(item);
// QNetworkRequest request(image.thumbnailUrl);
// static QNetworkAccessManager accessManager;
// static QNetworkDiskCache diskCache;
// diskCache.setCacheDirectory(QStandardPaths::writableLocation(QStandardPaths::CacheLocation));
// accessManager.setCache(&diskCache);
// request.setAttribute(QNetworkRequest::CacheLoadControlAttribute, QNetworkRequest::PreferCache);
// auto reply = accessManager.get(request);
// connect(reply, &QNetworkReply::finished, this, [=]{
// if(reply->error() != QNetworkReply::NoError) return;
// auto bytes = reply->readAll();
// QPixmap pixmap;
// pixmap.loadFromData(bytes);
// item->setIcon(QIcon(pixmap));
// item->setData(Qt::UserRole + 3, bytes);
// reply->deleteLater();
// });
// }
}

void CurseforgeModInfoWidget::updateBasicInfo()
Expand Down Expand Up @@ -122,6 +87,7 @@ void CurseforgeModInfoWidget::updateDescription()
{
ui->modDescription->setFont(qApp->font());
auto desc = mod_->modInfo().description();

ui->modDescription->setHtml(desc);
ui->modDescription->setCursor(Qt::ArrowCursor);
}
Expand Down
11 changes: 7 additions & 4 deletions src/ui/curseforge/curseforgemodinfowidget.ui
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@
<property name="font">
<font>
<pointsize>15</pointsize>
<weight>75</weight>
<bold>true</bold>
</font>
</property>
Expand Down Expand Up @@ -135,7 +134,7 @@
</widget>
</item>
<item>
<widget class="QTextBrowser" name="modDescription">
<widget class="WebTextBrowser" name="modDescription">
<property name="frameShape">
<enum>QFrame::NoFrame</enum>
</property>
Expand Down Expand Up @@ -168,8 +167,7 @@
</action>
<action name="actionCopy_Website_Link">
<property name="icon">
<iconset theme="edit-copy">
<normaloff>.</normaloff>.</iconset>
<iconset theme="edit-copy"/>
</property>
<property name="text">
<string>Copy Website Link</string>
Expand All @@ -183,6 +181,11 @@
<header location="global">ui/tagsflowwidget.h</header>
<container>1</container>
</customwidget>
<customwidget>
<class>WebTextBrowser</class>
<extends>QTextBrowser</extends>
<header location="global">ui/webtextbrowser.h</header>
</customwidget>
</customwidgets>
<resources>
<include location="../../images/image.qrc"/>
Expand Down
10 changes: 7 additions & 3 deletions src/ui/modrinth/modrinthmoddialog.ui
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@
<property name="font">
<font>
<pointsize>15</pointsize>
<weight>75</weight>
<bold>true</bold>
</font>
</property>
Expand Down Expand Up @@ -179,7 +178,7 @@
<number>0</number>
</property>
<item row="0" column="0">
<widget class="QTextBrowser" name="modDescription">
<widget class="WebTextBrowser" name="modDescription">
<property name="frameShape">
<enum>QFrame::NoFrame</enum>
</property>
Expand All @@ -203,9 +202,14 @@
<customwidget>
<class>TagsFlowWidget</class>
<extends>QWidget</extends>
<header>ui/tagsflowwidget.h</header>
<header location="global">ui/tagsflowwidget.h</header>
<container>1</container>
</customwidget>
<customwidget>
<class>WebTextBrowser</class>
<extends>QTextBrowser</extends>
<header location="global">ui/webtextbrowser.h</header>
</customwidget>
<customwidget>
<class>ModrinthFileListWidget</class>
<extends>QWidget</extends>
Expand Down
1 change: 0 additions & 1 deletion src/ui/modrinth/modrinthmodinfowidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ ModrinthModInfoWidget::ModrinthModInfoWidget(ModrinthModBrowser *parent) :
ui->modName->addAction(ui->actionOpen_Website_Link);
ui->modName->addAction(ui->actionCopy_Website_Link);
ui->scrollArea->setVisible(false);
ui->modDescription->setVerticalScrollBar(new SmoothScrollBar(this));
}

ModrinthModInfoWidget::~ModrinthModInfoWidget()
Expand Down
11 changes: 7 additions & 4 deletions src/ui/modrinth/modrinthmodinfowidget.ui
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@
<property name="font">
<font>
<pointsize>15</pointsize>
<weight>75</weight>
<bold>true</bold>
</font>
</property>
Expand Down Expand Up @@ -129,7 +128,7 @@
</widget>
</item>
<item>
<widget class="QTextBrowser" name="modDescription">
<widget class="WebTextBrowser" name="modDescription">
<property name="frameShape">
<enum>QFrame::NoFrame</enum>
</property>
Expand All @@ -148,8 +147,7 @@
</layout>
<action name="actionCopy_Website_Link">
<property name="icon">
<iconset theme="edit-copy">
<normaloff>.</normaloff>.</iconset>
<iconset theme="edit-copy"/>
</property>
<property name="text">
<string>Copy Website Link</string>
Expand Down Expand Up @@ -177,6 +175,11 @@
<header location="global">ui/tagsflowwidget.h</header>
<container>1</container>
</customwidget>
<customwidget>
<class>WebTextBrowser</class>
<extends>QTextBrowser</extends>
<header location="global">ui/webtextbrowser.h</header>
</customwidget>
</customwidgets>
<resources>
<include location="../../images/image.qrc"/>
Expand Down
53 changes: 53 additions & 0 deletions src/ui/webtextbrowser.cpp
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();
});
}
}
18 changes: 18 additions & 0 deletions src/ui/webtextbrowser.h
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

0 comments on commit a8336c3

Please sign in to comment.