2021-08-28 10:16:17 +12:00
|
|
|
from PyQt5.QtCore import Qt, QRect
|
|
|
|
from PyQt5.QtGui import QPaintEvent, QPainter, QPixmap, QPen, QFont, QColor
|
2021-11-22 10:44:45 +13:00
|
|
|
from PyQt5.QtWidgets import QVBoxLayout, QLabel, QHBoxLayout, QWidget
|
2021-08-28 10:16:17 +12:00
|
|
|
|
2021-12-02 08:14:41 +13:00
|
|
|
from rare import shared
|
|
|
|
from rare.utils.utils import get_pixmap, optimal_text_background, text_color_for_background, get_uninstalled_pixmap
|
2021-08-28 10:16:17 +12:00
|
|
|
|
|
|
|
|
2021-11-22 10:44:45 +13:00
|
|
|
class InstallingGameWidget(QWidget):
|
2021-09-03 09:32:25 +12:00
|
|
|
def __init__(self):
|
2021-08-28 10:16:17 +12:00
|
|
|
super(InstallingGameWidget, self).__init__()
|
|
|
|
self.setObjectName("game_widget_icon")
|
2021-11-22 10:44:45 +13:00
|
|
|
self.setProperty("noBorder", 1)
|
2021-08-28 10:16:17 +12:00
|
|
|
self.setLayout(QVBoxLayout())
|
|
|
|
|
2021-09-03 09:32:25 +12:00
|
|
|
self.pixmap = QPixmap()
|
2021-08-28 10:16:17 +12:00
|
|
|
w = 200
|
2021-09-03 09:32:25 +12:00
|
|
|
# self.pixmap = self.pixmap.scaled(w, int(w * 4 / 3), transformMode=Qt.SmoothTransformation)
|
|
|
|
self.image_widget = PaintWidget()
|
2021-11-22 10:44:45 +13:00
|
|
|
self.setContentsMargins(4, 4, 4, 4)
|
2021-08-28 10:16:17 +12:00
|
|
|
self.image_widget.setFixedSize(w, int(w * 4 / 3))
|
|
|
|
self.layout().addWidget(self.image_widget)
|
|
|
|
|
2021-09-03 09:32:25 +12:00
|
|
|
self.title_label = QLabel(f"<h4>Error</h4>")
|
2021-08-28 10:16:17 +12:00
|
|
|
self.title_label.setAutoFillBackground(False)
|
|
|
|
self.title_label.setWordWrap(True)
|
|
|
|
self.title_label.setFixedWidth(175)
|
|
|
|
minilayout = QHBoxLayout()
|
2021-09-03 09:32:25 +12:00
|
|
|
self.setObjectName("game_widget")
|
2021-08-28 10:16:17 +12:00
|
|
|
minilayout.addWidget(self.title_label)
|
|
|
|
self.layout().addLayout(minilayout)
|
|
|
|
|
2021-12-02 08:14:41 +13:00
|
|
|
def set_game(self, app_name):
|
|
|
|
game = shared.core.get_game(app_name)
|
2021-08-28 10:16:17 +12:00
|
|
|
self.title_label.setText(f"<h4>{game.app_title}</h4>")
|
2021-12-02 08:14:41 +13:00
|
|
|
|
|
|
|
self.image_widget.set_game(game.app_name)
|
2021-08-28 10:16:17 +12:00
|
|
|
|
2021-09-30 10:22:47 +13:00
|
|
|
def set_status(self, s: int):
|
2021-12-02 08:14:41 +13:00
|
|
|
self.image_widget.progress = s
|
2021-09-30 10:22:47 +13:00
|
|
|
self.image_widget.repaint()
|
|
|
|
|
2021-08-28 10:16:17 +12:00
|
|
|
|
|
|
|
class PaintWidget(QWidget):
|
2021-12-02 08:14:41 +13:00
|
|
|
color_image: QPixmap
|
|
|
|
bw_image: QPixmap
|
|
|
|
progress: int = 0
|
|
|
|
|
2021-09-03 09:32:25 +12:00
|
|
|
def __init__(self):
|
2021-08-28 10:16:17 +12:00
|
|
|
super(PaintWidget, self).__init__()
|
|
|
|
|
2021-12-02 08:14:41 +13:00
|
|
|
def set_game(self, app_name: str):
|
|
|
|
game = shared.core.get_game(app_name, False)
|
|
|
|
self.color_image = get_pixmap(game.app_name)
|
|
|
|
w = 200
|
|
|
|
self.color_image = self.color_image.scaled(w, int(w * 4 // 3), transformMode=Qt.SmoothTransformation)
|
|
|
|
self.setFixedSize(self.color_image.size())
|
|
|
|
self.bw_image = get_uninstalled_pixmap(app_name)
|
|
|
|
self.bw_image = self.bw_image.scaled(w, int(w * 4 // 3), transformMode=Qt.SmoothTransformation)
|
|
|
|
self.progress = 0
|
|
|
|
|
2021-08-28 10:16:17 +12:00
|
|
|
pixel_list = []
|
2021-12-02 08:14:41 +13:00
|
|
|
for x in range(self.color_image.width()):
|
|
|
|
for y in range(self.color_image.height()):
|
2021-08-28 10:16:17 +12:00
|
|
|
# convert pixmap to qimage, get pixel and remove alpha channel
|
2021-12-02 08:14:41 +13:00
|
|
|
pixel_list.append(self.color_image.toImage().pixelColor(x, y).getRgb()[:-1])
|
2021-08-28 10:16:17 +12:00
|
|
|
|
|
|
|
self.rgb_tuple = optimal_text_background(pixel_list)
|
|
|
|
|
|
|
|
def paintEvent(self, a0: QPaintEvent) -> None:
|
|
|
|
painter = QPainter()
|
|
|
|
painter.begin(self)
|
2021-12-02 08:14:41 +13:00
|
|
|
painter.drawPixmap(self.rect(), self.bw_image)
|
|
|
|
|
|
|
|
w = self.bw_image.width() * self.progress // 100
|
2021-08-28 10:16:17 +12:00
|
|
|
|
2021-12-02 08:14:41 +13:00
|
|
|
painter.drawPixmap(0, 0, w, self.color_image.height(),
|
|
|
|
self.color_image.copy(QRect(0, 0, w, self.color_image.height())))
|
2021-08-28 10:16:17 +12:00
|
|
|
|
|
|
|
# Draw Circle
|
|
|
|
pen = QPen(QColor(*self.rgb_tuple), 3)
|
|
|
|
painter.setPen(pen)
|
|
|
|
painter.setBrush(QColor(*self.rgb_tuple))
|
|
|
|
painter.drawEllipse(int(self.width() / 2) - 20, int(self.height() / 2) - 20, 40, 40)
|
|
|
|
|
|
|
|
# draw text
|
|
|
|
painter.setPen(QColor(*text_color_for_background(self.rgb_tuple)))
|
|
|
|
painter.setFont(QFont(None, 16))
|
2021-12-02 08:14:41 +13:00
|
|
|
painter.drawText(a0.rect(), Qt.AlignCenter, str(self.progress) + "%")
|
2021-08-28 10:16:17 +12:00
|
|
|
painter.end()
|