fix: prevent images overriding content when changing pages

Signed-off-by: flow <flowlnlnln@gmail.com>
This commit is contained in:
flow 2022-10-11 14:19:29 -03:00
parent d7992ab29d
commit d194b02e28
No known key found for this signature in database
GPG Key ID: 8D0F221F0A59F469
6 changed files with 39 additions and 5 deletions

View File

@ -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();
}

View File

@ -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()

View File

@ -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();
}

View File

@ -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<VariableSizedImageObject> m_image_text_object;
};

View File

@ -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);

View File

@ -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<QUrl> m_fetching_images;
QSet<QUrl> m_fetching_images;
};