mirror of
https://github.com/spalencsar/bearhub.git
synced 2026-07-07 07:04:16 +02:00
[view feature] rendering all package images | replacing Cache thread lock by process lock
This commit is contained in:
@@ -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()
|
||||
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -126,4 +126,8 @@ category=category
|
||||
categories=categories
|
||||
summary=summary
|
||||
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
|
||||
@@ -163,4 +163,8 @@ fitness=fitness
|
||||
science=ciencias
|
||||
news=noticias
|
||||
weather=tiempo
|
||||
finance=finanzas
|
||||
finance=finanzas
|
||||
desktopsettings=configuraciones
|
||||
texteditor=editor de texto
|
||||
screenshots.bt_next.label=próxima
|
||||
screenshots.bt_back.label=anterior
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user