From d4e1f84e529394ee8ad18b6f99b3f92a74daec4b Mon Sep 17 00:00:00 2001 From: Vinicius Moreira Date: Mon, 14 Oct 2019 13:43:24 -0300 Subject: [PATCH] [view feature] rendering all package images | replacing Cache thread lock by process lock --- bauh/view/qt/screenshots.py | 47 ++++++++++++++++++++++++++++++----- bauh/view/qt/thread.py | 32 +++++++++++++++++------- bauh/view/resources/locale/en | 6 ++++- bauh/view/resources/locale/es | 6 ++++- bauh/view/resources/locale/pt | 4 +++ bauh/view/util/cache.py | 3 ++- 6 files changed, 80 insertions(+), 18 deletions(-) diff --git a/bauh/view/qt/screenshots.py b/bauh/view/qt/screenshots.py index ce803155..14408051 100644 --- a/bauh/view/qt/screenshots.py +++ b/bauh/view/qt/screenshots.py @@ -1,9 +1,10 @@ from typing import List from PyQt5.QtGui import QIcon, QPixmap -from PyQt5.QtWidgets import QDialog, QLabel, QVBoxLayout +from PyQt5.QtWidgets import QDialog, QLabel, QGridLayout, QPushButton from bauh.api.abstract.cache import MemoryCache +from bauh.view.qt import qt_utils from bauh.view.qt.view_model import PackageView @@ -12,6 +13,9 @@ class ScreenshotsDialog(QDialog): def __init__(self, pkg: PackageView, icon_cache: MemoryCache, i18n: dict, screenshots: List[QPixmap]): super(ScreenshotsDialog, self).__init__() self.setWindowTitle(str(pkg)) + self.screenshots = screenshots + self.resize(1280, 720) + self.i18n = i18n icon_data = icon_cache.get(pkg.model.icon_url) @@ -20,11 +24,42 @@ class ScreenshotsDialog(QDialog): else: self.setWindowIcon(QIcon(pkg.model.get_type_icon_path())) - self.setLayout(QVBoxLayout()) + self.grid = QGridLayout() + self.setLayout(self.grid) + self.bt_back = QPushButton(self.i18n['screenshots.bt_back.label'].capitalize()) + self.bt_back.clicked.connect(self.back) + self.bt_next = QPushButton(self.i18n['screenshots.bt_next.label'].capitalize()) + self.bt_next.clicked.connect(self.next) - if screenshots: - lb = QLabel() - lb.setPixmap(screenshots[0]) - self.layout().addWidget(lb) + self.grid.addWidget(self.bt_back, 0, 0) + self.grid.addWidget(self.bt_next, 0, 2) + + self.img = QLabel() + self.layout().addWidget(self.img, 1, 1) + + self.idx = 0 + self._load_img() + + def _load_img(self): + pixmap = self.screenshots[self.idx] + self.img.setPixmap(pixmap) + + if len(self.screenshots) == 1: + self.bt_back.setVisible(False) + self.bt_next.setVisible(False) + else: + self.bt_back.setVisible(self.idx != 0) + self.bt_next.setVisible(self.idx != len(self.screenshots) - 1) self.adjustSize() + qt_utils.centralize(self) + + def back(self): + self.idx -= 1 + self._load_img() + + def next(self): + self.idx += 1 + self._load_img() + + diff --git a/bauh/view/qt/thread.py b/bauh/view/qt/thread.py index 6ca69e91..5cf1526d 100644 --- a/bauh/view/qt/thread.py +++ b/bauh/view/qt/thread.py @@ -1,11 +1,12 @@ import re import time from datetime import datetime, timedelta +from threading import Thread from typing import List, Type, Set import requests -from PyQt5.QtCore import QThread, pyqtSignal -from PyQt5.QtGui import QImage, QPixmap +from PyQt5.QtCore import QThread, pyqtSignal, Qt +from PyQt5.QtGui import QPixmap from bauh.api.abstract.cache import MemoryCache from bauh.api.abstract.controller import SoftwareManager @@ -485,6 +486,8 @@ class CustomAction(AsyncAction): class GetScreenshots(AsyncAction): + MAX_HEIGHT = 600 + MAX_WIDTH = 800 def __init__(self, manager: SoftwareManager, http_client: HttpClient, pkg: PackageView = None): super(GetScreenshots, self).__init__() @@ -492,22 +495,33 @@ class GetScreenshots(AsyncAction): self.manager = manager self.http_client = http_client + def _get_img(self, url: str, imgs: list): + res = self.http_client.get(url) + + if res and res.status_code == 200 and res.content: + pixmap = QPixmap() + pixmap.loadFromData(res.content) + + if pixmap.size().height() > self.MAX_HEIGHT or pixmap.size().width() > self.MAX_WIDTH: + pixmap = pixmap.scaled(self.MAX_WIDTH, self.MAX_HEIGHT, Qt.KeepAspectRatio, Qt.SmoothTransformation) + + imgs.append(pixmap) + def run(self): if self.pkg: imgs = [] screenshots = self.manager.get_screenshots(self.pkg.model) if screenshots: + threads = [] for url in screenshots: - res = self.http_client.get(url) + t = Thread(target=self._get_img, args=(url, imgs)) + t.start() + threads.append(t) - if res and res.status_code == 200 and res.content: - pixmap = QPixmap() - pixmap.loadFromData(res.content) - imgs.append(pixmap) - break # TODO load all + for t in threads: + t.join() self.notify_finished({'pkg': self.pkg, 'screenshots': imgs}) self.pkg = None - diff --git a/bauh/view/resources/locale/en b/bauh/view/resources/locale/en index b83d0b5e..cf716b76 100644 --- a/bauh/view/resources/locale/en +++ b/bauh/view/resources/locale/en @@ -126,4 +126,8 @@ category=category categories=categories summary=summary popup.screenshots.no_screenshot.body=it was not possible to retrieve {} screenshots -terminalemulator=terminal \ No newline at end of file +terminalemulator=terminal +desktopsettings=desktop settings +texteditor=text editor +screenshots.bt_next.label=next +screenshots.bt_back.label=previous \ No newline at end of file diff --git a/bauh/view/resources/locale/es b/bauh/view/resources/locale/es index b6f5a9c0..a1dd92ff 100644 --- a/bauh/view/resources/locale/es +++ b/bauh/view/resources/locale/es @@ -163,4 +163,8 @@ fitness=fitness science=ciencias news=noticias weather=tiempo -finance=finanzas \ No newline at end of file +finance=finanzas +desktopsettings=configuraciones +texteditor=editor de texto +screenshots.bt_next.label=próxima +screenshots.bt_back.label=anterior \ No newline at end of file diff --git a/bauh/view/resources/locale/pt b/bauh/view/resources/locale/pt index 3cfcdcf7..6bc5a94b 100644 --- a/bauh/view/resources/locale/pt +++ b/bauh/view/resources/locale/pt @@ -166,3 +166,7 @@ science=ciências news=notícias weather=tempo finance=finanças +desktopsettings=configurações +texteditor=editor de texto +screenshots.bt_next.label=próxima +screenshots.bt_back.label=anterior diff --git a/bauh/view/util/cache.py b/bauh/view/util/cache.py index 7caffef9..7a1ba79d 100644 --- a/bauh/view/util/cache.py +++ b/bauh/view/util/cache.py @@ -1,6 +1,7 @@ import datetime import time -from threading import Lock, Thread +from multiprocessing import Lock +from threading import Thread from bauh.api.abstract.cache import MemoryCache, MemoryCacheFactory