[view.qt] enhancement: displaying the screenshot download progress

This commit is contained in:
Vinicius Moreira
2023-12-13 09:52:46 -03:00
parent 3b7b22c9c7
commit da5cbdce6f
3 changed files with 53 additions and 28 deletions

View File

@@ -30,6 +30,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
- Snap supports both verified and unverified software - Snap supports both verified and unverified software
- minor reduction in the table loading time - minor reduction in the table loading time
- improvements to help with random widget centralisation issues - improvements to help with random widget centralisation issues
- displaying the download progress for screenshots
### Fixes ### Fixes
- AppImage - AppImage

View File

@@ -19,14 +19,15 @@ class HttpClient:
self.sleep = sleep self.sleep = sleep
self.logger = logger self.logger = logger
def get(self, url: str, params: dict = None, headers: dict = None, allow_redirects: bool = True, ignore_ssl: bool = False, single_call: bool = False, session: bool = True) -> Optional[requests.Response]: def get(self, url: str, params: dict = None, headers: dict = None, allow_redirects: bool = True, ignore_ssl: bool = False, single_call: bool = False,
session: bool = True, stream: bool = False) -> Optional[requests.Response]:
cur_attempts = 1 cur_attempts = 1
while cur_attempts <= self.max_attempts: while cur_attempts <= self.max_attempts:
cur_attempts += 1 cur_attempts += 1
try: try:
args = {'timeout': self.timeout, 'allow_redirects': allow_redirects} args = {'timeout': self.timeout, 'allow_redirects': allow_redirects, 'stream': stream}
if params: if params:
args['params'] = params args['params'] = params

View File

@@ -1,6 +1,7 @@
import logging import logging
from io import BytesIO
from threading import Thread from threading import Thread
from typing import List from typing import List, Dict
from PyQt5.QtCore import Qt from PyQt5.QtCore import Qt
from PyQt5.QtGui import QIcon, QPixmap, QCursor from PyQt5.QtGui import QIcon, QPixmap, QCursor
@@ -95,6 +96,7 @@ class ScreenshotsDialog(QDialog):
self.bt_next.setCursor(QCursor(Qt.PointingHandCursor)) self.bt_next.setCursor(QCursor(Qt.PointingHandCursor))
self.bt_next.clicked.connect(self.next) self.bt_next.clicked.connect(self.next)
self.container_buttons.layout().addWidget(self.bt_next) self.container_buttons.layout().addWidget(self.bt_next)
self.download_progress: Dict[int, float] = dict()
self.layout().addWidget(self.container_buttons) self.layout().addWidget(self.container_buttons)
@@ -107,13 +109,16 @@ class ScreenshotsDialog(QDialog):
t.start() t.start()
self.resize(self.max_img_width + 5, self.max_img_height + 5) self.resize(self.max_img_width + 5, self.max_img_height + 5)
self._load_img() self._load_img(self.img_idx)
qt_utils.centralize(self) qt_utils.centralize(self)
def _update_progress(self, val: int): def _update_progress(self, val: int):
self.progress_bar.setValue(val) self.progress_bar.setValue(val)
def _load_img(self): def _load_img(self, img_idx: int):
if img_idx != self.img_idx:
return
if len(self.loaded_imgs) > self.img_idx: if len(self.loaded_imgs) > self.img_idx:
img = self.loaded_imgs[self.img_idx] img = self.loaded_imgs[self.img_idx]
@@ -131,7 +136,10 @@ class ScreenshotsDialog(QDialog):
else: else:
self.img.setPixmap(QPixmap()) self.img.setPixmap(QPixmap())
self.img.setCursor(QCursor(Qt.WaitCursor)) self.img.setCursor(QCursor(Qt.WaitCursor))
self.img.setText('{} {}/{}...'.format(self.i18n['screenshots.image.loading'], self.img_idx + 1, len(self.screenshots)))
progress = self.download_progress.get(self.img_idx, 0)
self.img.setText(f"{self.i18n['screenshots.image.loading']} "
f"{self.img_idx + 1}/{len(self.screenshots)} ({progress:.2f}%)")
self.progress_bar.setVisible(True) self.progress_bar.setVisible(True)
self.thread_progress.start() self.thread_progress.start()
@@ -143,35 +151,50 @@ class ScreenshotsDialog(QDialog):
self.bt_next.setEnabled(self.img_idx != len(self.screenshots) - 1) self.bt_next.setEnabled(self.img_idx != len(self.screenshots) - 1)
def _download_img(self, idx: int, url: str): def _download_img(self, idx: int, url: str):
self.logger.info('Downloading image [{}] from {}'.format(idx, url)) self.logger.info(f"Downloading image [{idx}] from {url}")
res = self.http_client.get(url) res = self.http_client.get(url=url, stream=True)
if res: if not res:
if not res.content: self.logger.info(f"Could not retrieve image [{idx}] from '{url}'")
self.logger.warning('Image [{}] from {} has no content'.format(idx, url)) self.loaded_imgs.append(self.i18n["screenshots.download.no_response"])
self.loaded_imgs.append(self.i18n['screenshots.download.no_content']) self._load_img(idx)
self._load_img() return
else:
self.logger.info('Image [{}] successfully downloaded'.format(idx))
pixmap = QPixmap()
pixmap.loadFromData(res.content)
if pixmap.size().height() > self.max_img_height or pixmap.size().width() > self.max_img_width: try:
pixmap = pixmap.scaled(self.max_img_width, self.max_img_height, Qt.KeepAspectRatio, Qt.SmoothTransformation) content_length = int(res.headers.get("content-length", 0))
except Exception:
content_length = 0
self.logger.warning(f"Could not retrieve the content-length for file '{url}'")
self.loaded_imgs.append(pixmap) if content_length <= 0:
self.logger.warning(f"Image [{idx}] has no content ({url})")
if self.img_idx == idx: self.loaded_imgs.append(self.i18n['screenshots.download.no_content'])
self._load_img() self._load_img(idx)
else: else:
self.logger.info("Could not retrieve image [{}] from {}".format(idx, url)) byte_stream = BytesIO()
self.loaded_imgs.append(self.i18n['screenshots.download.no_response'])
self._load_img() total_downloaded = 0
for data in res.iter_content(chunk_size=1024):
byte_stream.write(data)
total_downloaded += len(data)
self.download_progress[idx] = (total_downloaded / content_length) * 100
self._load_img(idx)
self.logger.info(f"Image [{idx}] successfully downloaded ({url})")
pixmap = QPixmap()
pixmap.loadFromData(byte_stream.getvalue())
if pixmap.size().height() > self.max_img_height or pixmap.size().width() > self.max_img_width:
pixmap = pixmap.scaled(self.max_img_width, self.max_img_height, Qt.KeepAspectRatio,
Qt.SmoothTransformation)
self.loaded_imgs.append(pixmap)
self._load_img(idx)
def back(self): def back(self):
self.img_idx -= 1 self.img_idx -= 1
self._load_img() self._load_img(self.img_idx)
def next(self): def next(self):
self.img_idx += 1 self.img_idx += 1
self._load_img() self._load_img(self.img_idx)