From ea3be17220697326d3e508fc8c77d4e9bfbcf59f Mon Sep 17 00:00:00 2001 From: flow Date: Tue, 2 Aug 2022 15:17:54 -0300 Subject: [PATCH 1/7] feat: add widget for a text browser with image support Signed-off-by: flow --- launcher/CMakeLists.txt | 4 + .../ui/widgets/ProjectDescriptionPage.cpp | 17 +++ launcher/ui/widgets/ProjectDescriptionPage.h | 24 ++++ .../ui/widgets/VariableSizedImageObject.cpp | 118 ++++++++++++++++++ .../ui/widgets/VariableSizedImageObject.h | 55 ++++++++ 5 files changed, 218 insertions(+) create mode 100644 launcher/ui/widgets/ProjectDescriptionPage.cpp create mode 100644 launcher/ui/widgets/ProjectDescriptionPage.h create mode 100644 launcher/ui/widgets/VariableSizedImageObject.cpp create mode 100644 launcher/ui/widgets/VariableSizedImageObject.h diff --git a/launcher/CMakeLists.txt b/launcher/CMakeLists.txt index 0bdfcd44..044160a4 100644 --- a/launcher/CMakeLists.txt +++ b/launcher/CMakeLists.txt @@ -855,6 +855,10 @@ SET(LAUNCHER_SOURCES ui/widgets/PageContainer.cpp ui/widgets/PageContainer.h ui/widgets/PageContainer_p.h + ui/widgets/ProjectDescriptionPage.h + ui/widgets/ProjectDescriptionPage.cpp + ui/widgets/VariableSizedImageObject.h + ui/widgets/VariableSizedImageObject.cpp ui/widgets/ProjectItem.h ui/widgets/ProjectItem.cpp ui/widgets/VersionListView.cpp diff --git a/launcher/ui/widgets/ProjectDescriptionPage.cpp b/launcher/ui/widgets/ProjectDescriptionPage.cpp new file mode 100644 index 00000000..2e6f6d97 --- /dev/null +++ b/launcher/ui/widgets/ProjectDescriptionPage.cpp @@ -0,0 +1,17 @@ +#include "ProjectDescriptionPage.h" + +#include "VariableSizedImageObject.h" + +#include + +ProjectDescriptionPage::ProjectDescriptionPage(QWidget* parent) : QTextBrowser(parent), m_image_text_object(new VariableSizedImageObject) +{ + m_image_text_object->setParent(this); + document()->documentLayout()->registerHandler(QTextFormat::ImageObject, m_image_text_object.get()); +} + +void ProjectDescriptionPage::setMetaEntry(QString entry) +{ + if (m_image_text_object) + m_image_text_object->setMetaEntry(entry); +} diff --git a/launcher/ui/widgets/ProjectDescriptionPage.h b/launcher/ui/widgets/ProjectDescriptionPage.h new file mode 100644 index 00000000..8387d3fb --- /dev/null +++ b/launcher/ui/widgets/ProjectDescriptionPage.h @@ -0,0 +1,24 @@ +#pragma once + +#include + +#include "QObjectPtr.h" + +QT_BEGIN_NAMESPACE +class VariableSizedImageObject; +QT_END_NAMESPACE + +/** This subclasses QTextBrowser to provide additional capabilities + * to it, like allowing for images to be shown. + */ +class ProjectDescriptionPage final : public QTextBrowser { + Q_OBJECT + + public: + ProjectDescriptionPage(QWidget* parent = nullptr); + + void setMetaEntry(QString entry); + + private: + shared_qobject_ptr m_image_text_object; +}; diff --git a/launcher/ui/widgets/VariableSizedImageObject.cpp b/launcher/ui/widgets/VariableSizedImageObject.cpp new file mode 100644 index 00000000..0efdecab --- /dev/null +++ b/launcher/ui/widgets/VariableSizedImageObject.cpp @@ -0,0 +1,118 @@ +// SPDX-License-Identifier: GPL-3.0-only +/* + * PolyMC - Minecraft Launcher + * Copyright (c) 2022 flowln + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, version 3. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include "VariableSizedImageObject.h" + +#include +#include +#include +#include + +#include "Application.h" + +#include "net/NetJob.h" + +enum FormatProperties { ImageData = QTextFormat::UserProperty + 1 }; + +QSizeF VariableSizedImageObject::intrinsicSize(QTextDocument* doc, int posInDocument, const QTextFormat& format) +{ + Q_UNUSED(posInDocument); + + auto image = qvariant_cast(format.property(ImageData)); + auto size = image.size(); + + // Get the width of the text content to make the image similar sized. + // doc->textWidth() includes the margin, so we need to remove it. + auto doc_width = doc->textWidth() - 2 * doc->documentMargin(); + + if (size.width() > doc_width) + size *= doc_width / (double)size.width(); + + return { size }; +} +void VariableSizedImageObject::drawObject(QPainter* painter, + const QRectF& rect, + QTextDocument* doc, + int posInDocument, + const QTextFormat& format) +{ + if (!format.hasProperty(ImageData)) { + QUrl image_url{ qvariant_cast(format.property(QTextFormat::ImageName)) }; + if (m_fetching_images.contains(image_url)) + return; + + loadImage(doc, image_url, posInDocument); + return; + } + + auto image = qvariant_cast(format.property(ImageData)); + + painter->setRenderHint(QPainter::RenderHint::SmoothPixmapTransform); + painter->drawImage(rect, image); +} + +void VariableSizedImageObject::parseImage(QTextDocument* doc, QImage image, int posInDocument) +{ + QTextCursor cursor(doc); + cursor.setPosition(posInDocument); + cursor.setKeepPositionOnInsert(true); + + auto image_char_format = cursor.charFormat(); + + image_char_format.setObjectType(QTextFormat::ImageObject); + image_char_format.setProperty(ImageData, image); + + // Qt doesn't allow us to modify the properties of an existing object in the document. + // So we remove the old one and add the new one with the ImageData property set. + cursor.deleteChar(); + cursor.insertText(QString(QChar::ObjectReplacementCharacter), image_char_format); +} + +void VariableSizedImageObject::loadImage(QTextDocument* doc, const QUrl& source, int posInDocument) +{ + m_fetching_images.append(source); + + MetaEntryPtr entry = APPLICATION->metacache()->resolveEntry( + m_meta_entry, + QString("images/%1").arg(QString(QCryptographicHash::hash(source.toEncoded(), QCryptographicHash::Algorithm::Sha1).toHex()))); + + auto job = new NetJob(QString("Load Image: %1").arg(source.fileName()), APPLICATION->network()); + job->addNetAction(Net::Download::makeCached(source, entry)); + + auto full_entry_path = entry->getFullPath(); + auto source_url = source; + connect(job, &NetJob::succeeded, [this, doc, full_entry_path, source_url, posInDocument] { + qDebug() << "Loaded resource at" << full_entry_path; + + QImage image(full_entry_path); + doc->addResource(QTextDocument::ImageResource, source_url, image); + + parseImage(doc, image, posInDocument); + + // This size hack is needed to prevent the content from being laid out in an area smaller + // than the total width available (weird). + auto size = doc->pageSize(); + doc->adjustSize(); + doc->setPageSize(size); + + m_fetching_images.removeOne(source_url); + }); + connect(job, &NetJob::finished, job, &NetJob::deleteLater); + + job->start(); +} diff --git a/launcher/ui/widgets/VariableSizedImageObject.h b/launcher/ui/widgets/VariableSizedImageObject.h new file mode 100644 index 00000000..11563a37 --- /dev/null +++ b/launcher/ui/widgets/VariableSizedImageObject.h @@ -0,0 +1,55 @@ +// SPDX-License-Identifier: GPL-3.0-only +/* + * PolyMC - Minecraft Launcher + * Copyright (c) 2022 flowln + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, version 3. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#pragma once + +#include +#include +#include +#include + +/** Custom image text object to be used instead of the normal one in ProjectDescriptionPage. + * + * Why? Because we want to re-scale images dynamically based on the document's size, in order to + * not have images being weirdly cropped out in different resolutions. + */ +class VariableSizedImageObject : public QObject, public QTextObjectInterface { + Q_OBJECT + Q_INTERFACES(QTextObjectInterface) + + public: + QSizeF intrinsicSize(QTextDocument* doc, int posInDocument, const QTextFormat& format) override; + void drawObject(QPainter* painter, const QRectF& rect, QTextDocument* doc, int posInDocument, const QTextFormat& format) override; + + void setMetaEntry(QString meta_entry) { m_meta_entry = meta_entry; } + + protected: + /** Adds the image to the document, in the given position. + */ + void parseImage(QTextDocument* doc, QImage image, int posInDocument); + + /** Loads an image from an external source, and adds it to the document. + * + * This uses m_meta_entry to cache the image. + */ + void loadImage(QTextDocument* doc, const QUrl& source, int posInDocument); + + QString m_meta_entry; + + QList m_fetching_images; +}; From db158a5735e18442f76118f1f6a060e6c3680e33 Mon Sep 17 00:00:00 2001 From: flow Date: Sat, 10 Sep 2022 18:49:00 -0300 Subject: [PATCH 2/7] feat: add image support for mod pages Signed-off-by: flow --- launcher/ui/pages/modplatform/ModPage.ui | 9 ++++++++- launcher/ui/pages/modplatform/flame/FlameModPage.cpp | 2 ++ .../ui/pages/modplatform/modrinth/ModrinthModPage.cpp | 2 ++ 3 files changed, 12 insertions(+), 1 deletion(-) diff --git a/launcher/ui/pages/modplatform/ModPage.ui b/launcher/ui/pages/modplatform/ModPage.ui index afcd9bb7..943f02aa 100644 --- a/launcher/ui/pages/modplatform/ModPage.ui +++ b/launcher/ui/pages/modplatform/ModPage.ui @@ -14,7 +14,7 @@ - + true @@ -98,6 +98,13 @@ + + + ProjectDescriptionPage + QTextBrowser +
ui/widgets/ProjectDescriptionPage.h
+
+
searchEdit searchButton diff --git a/launcher/ui/pages/modplatform/flame/FlameModPage.cpp b/launcher/ui/pages/modplatform/flame/FlameModPage.cpp index 772fd2e0..b497d1fe 100644 --- a/launcher/ui/pages/modplatform/flame/FlameModPage.cpp +++ b/launcher/ui/pages/modplatform/flame/FlameModPage.cpp @@ -59,6 +59,8 @@ FlameModPage::FlameModPage(ModDownloadDialog* dialog, BaseInstance* instance) connect(ui->packView->selectionModel(), &QItemSelectionModel::currentChanged, this, &FlameModPage::onSelectionChanged); connect(ui->versionSelectionBox, &QComboBox::currentTextChanged, this, &FlameModPage::onVersionSelectionChanged); connect(ui->modSelectionButton, &QPushButton::clicked, this, &FlameModPage::onModSelected); + + ui->packDescription->setMetaEntry(metaEntryBase()); } auto FlameModPage::validateVersion(ModPlatform::IndexedVersion& ver, QString mineVer, ModAPI::ModLoaderTypes loaders) const -> bool diff --git a/launcher/ui/pages/modplatform/modrinth/ModrinthModPage.cpp b/launcher/ui/pages/modplatform/modrinth/ModrinthModPage.cpp index 5fa00b9b..62e417c8 100644 --- a/launcher/ui/pages/modplatform/modrinth/ModrinthModPage.cpp +++ b/launcher/ui/pages/modplatform/modrinth/ModrinthModPage.cpp @@ -59,6 +59,8 @@ ModrinthModPage::ModrinthModPage(ModDownloadDialog* dialog, BaseInstance* instan connect(ui->packView->selectionModel(), &QItemSelectionModel::currentChanged, this, &ModrinthModPage::onSelectionChanged); connect(ui->versionSelectionBox, &QComboBox::currentTextChanged, this, &ModrinthModPage::onVersionSelectionChanged); connect(ui->modSelectionButton, &QPushButton::clicked, this, &ModrinthModPage::onModSelected); + + ui->packDescription->setMetaEntry(metaEntryBase()); } auto ModrinthModPage::validateVersion(ModPlatform::IndexedVersion& ver, QString mineVer, ModAPI::ModLoaderTypes loaders) const -> bool From d99976f5d7e13f0e2432028b9dd8b38ded8bcc18 Mon Sep 17 00:00:00 2001 From: flow Date: Tue, 2 Aug 2022 15:14:25 -0300 Subject: [PATCH 3/7] fix: make mod and modpack caches separate for Modrinth This makes it similar to CF mods / modpacks. The mods cache is maintained with the same name because it most likely has more data it in, so this commit will affect existing caches as minimally as possible. Signed-off-by: flow --- launcher/Application.cpp | 1 + launcher/ui/pages/modplatform/modrinth/ModrinthModel.cpp | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/launcher/Application.cpp b/launcher/Application.cpp index e08ea7f4..64adc6e9 100644 --- a/launcher/Application.cpp +++ b/launcher/Application.cpp @@ -809,6 +809,7 @@ Application::Application(int &argc, char **argv) : QApplication(argc, argv) m_metacache->addBase("FlamePacks", QDir("cache/FlamePacks").absolutePath()); m_metacache->addBase("FlameMods", QDir("cache/FlameMods").absolutePath()); m_metacache->addBase("ModrinthPacks", QDir("cache/ModrinthPacks").absolutePath()); + m_metacache->addBase("ModrinthModpacks", QDir("cache/ModrinthModpacks").absolutePath()); m_metacache->addBase("root", QDir::currentPath()); m_metacache->addBase("translations", QDir("translations").absolutePath()); m_metacache->addBase("icons", QDir("cache/icons").absolutePath()); diff --git a/launcher/ui/pages/modplatform/modrinth/ModrinthModel.cpp b/launcher/ui/pages/modplatform/modrinth/ModrinthModel.cpp index fd7a3537..a0e9ab19 100644 --- a/launcher/ui/pages/modplatform/modrinth/ModrinthModel.cpp +++ b/launcher/ui/pages/modplatform/modrinth/ModrinthModel.cpp @@ -218,7 +218,7 @@ void ModpackListModel::getLogo(const QString& logo, const QString& logoUrl, Logo { if (m_logoMap.contains(logo)) { callback(APPLICATION->metacache() - ->resolveEntry("ModrinthPacks", QString("logos/%1").arg(logo.section(".", 0, 0))) + ->resolveEntry(m_parent->metaEntryBase(), QString("logos/%1").arg(logo.section(".", 0, 0))) ->getFullPath()); } else { requestLogo(logo, logoUrl); @@ -232,7 +232,7 @@ void ModpackListModel::requestLogo(QString logo, QString url) } MetaEntryPtr entry = - APPLICATION->metacache()->resolveEntry("ModrinthPacks", QString("logos/%1").arg(logo.section(".", 0, 0))); + APPLICATION->metacache()->resolveEntry(m_parent->metaEntryBase(), QString("logos/%1").arg(logo.section(".", 0, 0))); auto job = new NetJob(QString("%1 Icon Download %2").arg(m_parent->debugName()).arg(logo), APPLICATION->network()); job->addNetAction(Net::Download::makeCached(QUrl(url), entry)); From 60f19f305e4e4540f337a4dbcc96536c14e23e82 Mon Sep 17 00:00:00 2001 From: flow Date: Sat, 10 Sep 2022 18:49:31 -0300 Subject: [PATCH 4/7] feat: add image support for modrinth modpack pages Signed-off-by: flow --- launcher/ui/pages/modplatform/modrinth/ModrinthPage.cpp | 1 + launcher/ui/pages/modplatform/modrinth/ModrinthPage.ui | 9 ++++++++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/launcher/ui/pages/modplatform/modrinth/ModrinthPage.cpp b/launcher/ui/pages/modplatform/modrinth/ModrinthPage.cpp index cea6cdee..70f1388a 100644 --- a/launcher/ui/pages/modplatform/modrinth/ModrinthPage.cpp +++ b/launcher/ui/pages/modplatform/modrinth/ModrinthPage.cpp @@ -74,6 +74,7 @@ ModrinthPage::ModrinthPage(NewInstanceDialog* dialog, QWidget* parent) : QWidget connect(ui->versionSelectionBox, &QComboBox::currentTextChanged, this, &ModrinthPage::onVersionSelectionChanged); ui->packView->setItemDelegate(new ProjectItemDelegate(this)); + ui->packDescription->setMetaEntry(metaEntryBase()); } ModrinthPage::~ModrinthPage() diff --git a/launcher/ui/pages/modplatform/modrinth/ModrinthPage.ui b/launcher/ui/pages/modplatform/modrinth/ModrinthPage.ui index 6a34701d..6d8b2b67 100644 --- a/launcher/ui/pages/modplatform/modrinth/ModrinthPage.ui +++ b/launcher/ui/pages/modplatform/modrinth/ModrinthPage.ui @@ -66,7 +66,7 @@
- + true @@ -99,6 +99,13 @@ + + + ProjectDescriptionPage + QTextBrowser +
ui/widgets/ProjectDescriptionPage.h
+
+
searchEdit searchButton From d7992ab29d07c6d26377f6db1cfca6059aace471 Mon Sep 17 00:00:00 2001 From: flow Date: Sat, 10 Sep 2022 19:00:47 -0300 Subject: [PATCH 5/7] feat: add image support for FTB packs Signed-off-by: flow --- launcher/ui/pages/modplatform/ftb/FtbPage.cpp | 2 ++ launcher/ui/pages/modplatform/ftb/FtbPage.ui | 9 ++++++++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/launcher/ui/pages/modplatform/ftb/FtbPage.cpp b/launcher/ui/pages/modplatform/ftb/FtbPage.cpp index 8975d74e..34734eaf 100644 --- a/launcher/ui/pages/modplatform/ftb/FtbPage.cpp +++ b/launcher/ui/pages/modplatform/ftb/FtbPage.cpp @@ -73,6 +73,8 @@ FtbPage::FtbPage(NewInstanceDialog* dialog, QWidget *parent) connect(ui->sortByBox, &QComboBox::currentTextChanged, this, &FtbPage::onSortingSelectionChanged); connect(ui->packView->selectionModel(), &QItemSelectionModel::currentChanged, this, &FtbPage::onSelectionChanged); connect(ui->versionSelectionBox, &QComboBox::currentTextChanged, this, &FtbPage::onVersionSelectionChanged); + + ui->packDescription->setMetaEntry("FTBPacks"); } FtbPage::~FtbPage() diff --git a/launcher/ui/pages/modplatform/ftb/FtbPage.ui b/launcher/ui/pages/modplatform/ftb/FtbPage.ui index 850bf091..8de0f4e6 100644 --- a/launcher/ui/pages/modplatform/ftb/FtbPage.ui +++ b/launcher/ui/pages/modplatform/ftb/FtbPage.ui @@ -57,7 +57,7 @@ - + true @@ -70,6 +70,13 @@ + + + ProjectDescriptionPage + QTextBrowser +
ui/widgets/ProjectDescriptionPage.h
+
+
searchEdit versionSelectionBox From d194b02e28132df3ea3da961299e969614b8a185 Mon Sep 17 00:00:00 2001 From: flow Date: Tue, 11 Oct 2022 14:19:29 -0300 Subject: [PATCH 6/7] fix: prevent images overriding content when changing pages Signed-off-by: flow --- launcher/ui/pages/modplatform/ModPage.cpp | 1 + .../pages/modplatform/modrinth/ModrinthPage.cpp | 1 + launcher/ui/widgets/ProjectDescriptionPage.cpp | 6 ++++++ launcher/ui/widgets/ProjectDescriptionPage.h | 8 ++++++++ launcher/ui/widgets/VariableSizedImageObject.cpp | 13 +++++++++++-- launcher/ui/widgets/VariableSizedImageObject.h | 15 ++++++++++++--- 6 files changed, 39 insertions(+), 5 deletions(-) diff --git a/launcher/ui/pages/modplatform/ModPage.cpp b/launcher/ui/pages/modplatform/ModPage.cpp index 4fce0242..153bb049 100644 --- a/launcher/ui/pages/modplatform/ModPage.cpp +++ b/launcher/ui/pages/modplatform/ModPage.cpp @@ -350,4 +350,5 @@ void ModPage::updateUi() HoeDown h; ui->packDescription->setHtml(text + (current.extraData.body.isEmpty() ? current.description : h.process(current.extraData.body.toUtf8()))); + ui->packDescription->flush(); } diff --git a/launcher/ui/pages/modplatform/modrinth/ModrinthPage.cpp b/launcher/ui/pages/modplatform/modrinth/ModrinthPage.cpp index 70f1388a..4482774c 100644 --- a/launcher/ui/pages/modplatform/modrinth/ModrinthPage.cpp +++ b/launcher/ui/pages/modplatform/modrinth/ModrinthPage.cpp @@ -284,6 +284,7 @@ void ModrinthPage::updateUI() text += h.process(current.extra.body.toUtf8()); ui->packDescription->setHtml(text + current.description); + ui->packDescription->flush(); } void ModrinthPage::suggestCurrent() diff --git a/launcher/ui/widgets/ProjectDescriptionPage.cpp b/launcher/ui/widgets/ProjectDescriptionPage.cpp index 2e6f6d97..c7e79a17 100644 --- a/launcher/ui/widgets/ProjectDescriptionPage.cpp +++ b/launcher/ui/widgets/ProjectDescriptionPage.cpp @@ -15,3 +15,9 @@ void ProjectDescriptionPage::setMetaEntry(QString entry) if (m_image_text_object) m_image_text_object->setMetaEntry(entry); } + +void ProjectDescriptionPage::flush() +{ + if (m_image_text_object) + m_image_text_object->flush(); +} diff --git a/launcher/ui/widgets/ProjectDescriptionPage.h b/launcher/ui/widgets/ProjectDescriptionPage.h index 8387d3fb..3dd85302 100644 --- a/launcher/ui/widgets/ProjectDescriptionPage.h +++ b/launcher/ui/widgets/ProjectDescriptionPage.h @@ -19,6 +19,14 @@ class ProjectDescriptionPage final : public QTextBrowser { void setMetaEntry(QString entry); + public slots: + /** Flushes the current processing happening in the page. + * + * Should be called when changing the page's content entirely, to + * prevent old tasks from changing the new content. + */ + void flush(); + private: shared_qobject_ptr m_image_text_object; }; diff --git a/launcher/ui/widgets/VariableSizedImageObject.cpp b/launcher/ui/widgets/VariableSizedImageObject.cpp index 0efdecab..e57f7e95 100644 --- a/launcher/ui/widgets/VariableSizedImageObject.cpp +++ b/launcher/ui/widgets/VariableSizedImageObject.cpp @@ -66,6 +66,11 @@ void VariableSizedImageObject::drawObject(QPainter* painter, painter->drawImage(rect, image); } +void VariableSizedImageObject::flush() +{ + m_fetching_images.clear(); +} + void VariableSizedImageObject::parseImage(QTextDocument* doc, QImage image, int posInDocument) { QTextCursor cursor(doc); @@ -85,7 +90,7 @@ void VariableSizedImageObject::parseImage(QTextDocument* doc, QImage image, int void VariableSizedImageObject::loadImage(QTextDocument* doc, const QUrl& source, int posInDocument) { - m_fetching_images.append(source); + m_fetching_images.insert(source); MetaEntryPtr entry = APPLICATION->metacache()->resolveEntry( m_meta_entry, @@ -99,6 +104,10 @@ void VariableSizedImageObject::loadImage(QTextDocument* doc, const QUrl& source, connect(job, &NetJob::succeeded, [this, doc, full_entry_path, source_url, posInDocument] { qDebug() << "Loaded resource at" << full_entry_path; + // If we flushed, don't proceed. + if (!m_fetching_images.contains(source_url)) + return; + QImage image(full_entry_path); doc->addResource(QTextDocument::ImageResource, source_url, image); @@ -110,7 +119,7 @@ void VariableSizedImageObject::loadImage(QTextDocument* doc, const QUrl& source, doc->adjustSize(); doc->setPageSize(size); - m_fetching_images.removeOne(source_url); + m_fetching_images.remove(source_url); }); connect(job, &NetJob::finished, job, &NetJob::deleteLater); diff --git a/launcher/ui/widgets/VariableSizedImageObject.h b/launcher/ui/widgets/VariableSizedImageObject.h index 11563a37..137487ee 100644 --- a/launcher/ui/widgets/VariableSizedImageObject.h +++ b/launcher/ui/widgets/VariableSizedImageObject.h @@ -28,7 +28,7 @@ * Why? Because we want to re-scale images dynamically based on the document's size, in order to * not have images being weirdly cropped out in different resolutions. */ -class VariableSizedImageObject : public QObject, public QTextObjectInterface { +class VariableSizedImageObject final : public QObject, public QTextObjectInterface { Q_OBJECT Q_INTERFACES(QTextObjectInterface) @@ -38,7 +38,15 @@ class VariableSizedImageObject : public QObject, public QTextObjectInterface { void setMetaEntry(QString meta_entry) { m_meta_entry = meta_entry; } - protected: + public slots: + /** Stops all currently loading images from modifying the document. + * + * This does not stop the ongoing network tasks, it only prevents their result + * from impacting the document any further. + */ + void flush(); + + private: /** Adds the image to the document, in the given position. */ void parseImage(QTextDocument* doc, QImage image, int posInDocument); @@ -49,7 +57,8 @@ class VariableSizedImageObject : public QObject, public QTextObjectInterface { */ void loadImage(QTextDocument* doc, const QUrl& source, int posInDocument); + private: QString m_meta_entry; - QList m_fetching_images; + QSet m_fetching_images; }; From fda3f1352e203bc119f092e30b25356345342c18 Mon Sep 17 00:00:00 2001 From: flow Date: Tue, 11 Oct 2022 16:11:08 -0300 Subject: [PATCH 7/7] feat: add image support for the news reader :^) Signed-off-by: flow --- launcher/ui/dialogs/NewsDialog.cpp | 4 ++++ launcher/ui/dialogs/NewsDialog.ui | 9 ++++++++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/launcher/ui/dialogs/NewsDialog.cpp b/launcher/ui/dialogs/NewsDialog.cpp index d3b21627..e1b5dd74 100644 --- a/launcher/ui/dialogs/NewsDialog.cpp +++ b/launcher/ui/dialogs/NewsDialog.cpp @@ -20,7 +20,9 @@ NewsDialog::NewsDialog(QList entries, QWidget* parent) : QDialog(p auto article_entry = m_entries.constFind(first_item->text()).value(); ui->articleTitleLabel->setText(QString("%2").arg(article_entry->link, first_item->text())); + ui->currentArticleContentBrowser->setText(article_entry->content); + ui->currentArticleContentBrowser->flush(); } NewsDialog::~NewsDialog() @@ -33,7 +35,9 @@ void NewsDialog::selectedArticleChanged(const QString& new_title) auto const& article_entry = m_entries.constFind(new_title).value(); ui->articleTitleLabel->setText(QString("%2").arg(article_entry->link, new_title)); + ui->currentArticleContentBrowser->setText(article_entry->content); + ui->currentArticleContentBrowser->flush(); } void NewsDialog::toggleArticleList() diff --git a/launcher/ui/dialogs/NewsDialog.ui b/launcher/ui/dialogs/NewsDialog.ui index 2aaa08f1..08f35a0b 100644 --- a/launcher/ui/dialogs/NewsDialog.ui +++ b/launcher/ui/dialogs/NewsDialog.ui @@ -49,7 +49,7 @@ - + Qt::LinksAccessibleByKeyboard|Qt::LinksAccessibleByMouse|Qt::TextBrowserInteraction|Qt::TextSelectableByKeyboard|Qt::TextSelectableByMouse @@ -91,6 +91,13 @@ + + + ProjectDescriptionPage + QTextBrowser +
ui/widgets/ProjectDescriptionPage.h
+
+