Skip to content

Commit

Permalink
Addon Manager: Replace QtWebEngine with QTextBrowser
Browse files Browse the repository at this point in the history
Macro display is still a work-in-progress.
  • Loading branch information
chennes committed Jan 23, 2024
1 parent b8789ce commit 9577fb5
Showing 1 changed file with 45 additions and 8 deletions.
53 changes: 45 additions & 8 deletions src/Mod/AddonManager/addonmanager_readme_viewer.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
# * *
# ***************************************************************************

import tempfile

Check warning on line 24 in src/Mod/AddonManager/addonmanager_readme_viewer.py

View workflow job for this annotation

GitHub Actions / Lint / Lint

Unused import tempfile (unused-import)

import Addon
from PySide import QtCore, QtGui, QtWidgets

Expand All @@ -33,9 +35,9 @@

class ReadmeViewer(QtWidgets.QTextBrowser):

""" A QTextBrowser widget that, when given an Addon, downloads the README data as appropriate
and renders it with whatever technology is available (usually Qt's Markdown renderer for
workbenches and its HTML renderer for Macros). """
"""A QTextBrowser widget that, when given an Addon, downloads the README data as appropriate
and renders it with whatever technology is available (usually Qt's Markdown renderer for
workbenches and its HTML renderer for Macros)."""

def __init__(self, parent=None):
super().__init__(parent)
Expand All @@ -46,14 +48,17 @@ def __init__(self, parent=None):
self.repo: Addon.Addon = None
self.setOpenExternalLinks(True)
self.setOpenLinks(True)
self.image_map = {}

def set_addon(self, repo: Addon):

Check warning on line 53 in src/Mod/AddonManager/addonmanager_readme_viewer.py

View workflow job for this annotation

GitHub Actions / Lint / Lint

Missing function or method docstring (missing-function-docstring)
self.setPlainText(translate("AddonsInstaller", "Loading README data..."))
self.repo = repo
if self.repo.repo_type == Addon.Addon.Kind.MACRO:
self.url = self.repo.macro.wiki
if not self.url:
self.url = self.repo.macro.url
else:
self.url = utils.get_readme_url(repo)
self.repo = repo

self.request_index = NetworkManager.AM_NETWORK_MANAGER.submit_unmonitored_get(self.url)

Expand All @@ -63,9 +68,12 @@ def _download_completed(self, index: int, code: int, data: QtCore.QByteArray) ->
if code == 200: # HTTP success
self._process_package_download(data.data().decode("utf-8"))
else:
self.setPlainText(translate("AddonsInstaller",
"Failed to download data from {} -- received response code {}.").format(
self.url, code))
self.setPlainText(
translate(
"AddonsInstaller",
"Failed to download data from {} -- received response code {}.",
).format(self.url, code)
)

def _process_package_download(self, data: str):
if self.repo.repo_type == Addon.Addon.Kind.MACRO:
Expand All @@ -74,4 +82,33 @@ def _process_package_download(self, data: str):
if hasattr(self, "setMarkdown"):
self.setMarkdown(data)
else:
self.setPlainText(data)
self.setPlainText(data)

def loadResource(self, resource_type: int, name: QtCore.QUrl) -> object:

Check warning on line 87 in src/Mod/AddonManager/addonmanager_readme_viewer.py

View workflow job for this annotation

GitHub Actions / Lint / Lint

Missing function or method docstring (missing-function-docstring)
if resource_type == QtGui.QTextDocument.ImageResource:

Check warning on line 88 in src/Mod/AddonManager/addonmanager_readme_viewer.py

View workflow job for this annotation

GitHub Actions / Lint / Lint

Unnecessary "else" after "return", remove the "else" and de-indent the code inside it (no-else-return)
full_url = self._create_full_url(name.toString())
if full_url not in self.image_map:
self.image_map[full_url] = None
fci.Console.PrintMessage(f"Downloading image from {full_url}...\n")
data = NetworkManager.AM_NETWORK_MANAGER.blocking_get(full_url)
if data and data.data():
image = QtGui.QImage.fromData(data.data())
if image:
self.image_map[full_url] = self._ensure_appropriate_width(image)
return self.image_map[full_url]
else:
return super().loadResource(resource_type, name)

def _create_full_url(self, url: str) -> str:
if url.startswith("http"):
return url
if not self.url:
return url
lhs, slash, rhs = self.url.rpartition("/")

Check warning on line 107 in src/Mod/AddonManager/addonmanager_readme_viewer.py

View workflow job for this annotation

GitHub Actions / Lint / Lint

Unused variable 'rhs' (unused-variable)
return lhs + slash + url

def _ensure_appropriate_width(self, image: QtGui.QImage) -> QtGui.QImage:
ninety_seven_percent = self.width() * 0.97
if image.width() < ninety_seven_percent:
return image
return image.scaledToWidth(ninety_seven_percent)

0 comments on commit 9577fb5

Please sign in to comment.