[view feature] rendering all package images | replacing Cache thread lock by process lock

This commit is contained in:
Vinicius Moreira
2019-10-14 13:43:24 -03:00
parent 4702f473d4
commit d4e1f84e52
6 changed files with 80 additions and 18 deletions

View File

@@ -1,9 +1,10 @@
from typing import List from typing import List
from PyQt5.QtGui import QIcon, QPixmap 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.api.abstract.cache import MemoryCache
from bauh.view.qt import qt_utils
from bauh.view.qt.view_model import PackageView 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]): def __init__(self, pkg: PackageView, icon_cache: MemoryCache, i18n: dict, screenshots: List[QPixmap]):
super(ScreenshotsDialog, self).__init__() super(ScreenshotsDialog, self).__init__()
self.setWindowTitle(str(pkg)) self.setWindowTitle(str(pkg))
self.screenshots = screenshots
self.resize(1280, 720)
self.i18n = i18n
icon_data = icon_cache.get(pkg.model.icon_url) icon_data = icon_cache.get(pkg.model.icon_url)
@@ -20,11 +24,42 @@ class ScreenshotsDialog(QDialog):
else: else:
self.setWindowIcon(QIcon(pkg.model.get_type_icon_path())) 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: self.grid.addWidget(self.bt_back, 0, 0)
lb = QLabel() self.grid.addWidget(self.bt_next, 0, 2)
lb.setPixmap(screenshots[0])
self.layout().addWidget(lb) 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() self.adjustSize()
qt_utils.centralize(self)
def back(self):
self.idx -= 1
self._load_img()
def next(self):
self.idx += 1
self._load_img()

View File

@@ -1,11 +1,12 @@
import re import re
import time import time
from datetime import datetime, timedelta from datetime import datetime, timedelta
from threading import Thread
from typing import List, Type, Set from typing import List, Type, Set
import requests import requests
from PyQt5.QtCore import QThread, pyqtSignal from PyQt5.QtCore import QThread, pyqtSignal, Qt
from PyQt5.QtGui import QImage, QPixmap from PyQt5.QtGui import QPixmap
from bauh.api.abstract.cache import MemoryCache from bauh.api.abstract.cache import MemoryCache
from bauh.api.abstract.controller import SoftwareManager from bauh.api.abstract.controller import SoftwareManager
@@ -485,6 +486,8 @@ class CustomAction(AsyncAction):
class GetScreenshots(AsyncAction): class GetScreenshots(AsyncAction):
MAX_HEIGHT = 600
MAX_WIDTH = 800
def __init__(self, manager: SoftwareManager, http_client: HttpClient, pkg: PackageView = None): def __init__(self, manager: SoftwareManager, http_client: HttpClient, pkg: PackageView = None):
super(GetScreenshots, self).__init__() super(GetScreenshots, self).__init__()
@@ -492,22 +495,33 @@ class GetScreenshots(AsyncAction):
self.manager = manager self.manager = manager
self.http_client = http_client 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): def run(self):
if self.pkg: if self.pkg:
imgs = [] imgs = []
screenshots = self.manager.get_screenshots(self.pkg.model) screenshots = self.manager.get_screenshots(self.pkg.model)
if screenshots: if screenshots:
threads = []
for url in screenshots: 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: for t in threads:
pixmap = QPixmap() t.join()
pixmap.loadFromData(res.content)
imgs.append(pixmap)
break # TODO load all
self.notify_finished({'pkg': self.pkg, 'screenshots': imgs}) self.notify_finished({'pkg': self.pkg, 'screenshots': imgs})
self.pkg = None self.pkg = None

View File

@@ -127,3 +127,7 @@ categories=categories
summary=summary summary=summary
popup.screenshots.no_screenshot.body=it was not possible to retrieve {} screenshots popup.screenshots.no_screenshot.body=it was not possible to retrieve {} screenshots
terminalemulator=terminal terminalemulator=terminal
desktopsettings=desktop settings
texteditor=text editor
screenshots.bt_next.label=next
screenshots.bt_back.label=previous

View File

@@ -164,3 +164,7 @@ science=ciencias
news=noticias news=noticias
weather=tiempo weather=tiempo
finance=finanzas finance=finanzas
desktopsettings=configuraciones
texteditor=editor de texto
screenshots.bt_next.label=próxima
screenshots.bt_back.label=anterior

View File

@@ -166,3 +166,7 @@ science=ciências
news=notícias news=notícias
weather=tempo weather=tempo
finance=finanças finance=finanças
desktopsettings=configurações
texteditor=editor de texto
screenshots.bt_next.label=próxima
screenshots.bt_back.label=anterior

View File

@@ -1,6 +1,7 @@
import datetime import datetime
import time import time
from threading import Lock, Thread from multiprocessing import Lock
from threading import Thread
from bauh.api.abstract.cache import MemoryCache, MemoryCacheFactory from bauh.api.abstract.cache import MemoryCache, MemoryCacheFactory