1
0
Fork 0
mirror of synced 2024-06-17 01:54:46 +12:00
Rare/rare/components/tabs/store/search_results.py

112 lines
3.7 KiB
Python
Raw Normal View History

from PyQt5 import QtGui
from PyQt5.QtCore import pyqtSignal, Qt
from PyQt5.QtGui import QFont
2021-12-24 22:09:50 +13:00
from PyQt5.QtWidgets import (
QWidget,
QVBoxLayout,
QHBoxLayout,
QLabel,
QScrollArea,
QGroupBox,
QPushButton,
QStackedWidget,
)
2022-06-22 11:38:04 +12:00
from rare.utils.extra_widgets import ImageLabel, WaitingSpinner
from rare.widgets.flow_layout import FlowLayout
2021-08-08 09:42:40 +12:00
class SearchResults(QStackedWidget):
show_info = pyqtSignal(dict)
2021-08-23 08:22:17 +12:00
def __init__(self, api_core):
super(SearchResults, self).__init__()
2021-08-08 09:42:40 +12:00
self.search_result_widget = QWidget()
2021-08-23 08:22:17 +12:00
self.api_core = api_core
2021-08-08 09:42:40 +12:00
self.addWidget(self.search_result_widget)
self.main_layout = QVBoxLayout()
2021-06-11 05:58:35 +12:00
self.back_button = QPushButton(self.tr("Back"))
self.main_layout.addWidget(self.back_button)
self.main_layout.addWidget(self.back_button)
self.result_area = QScrollArea()
self.widget = QWidget()
self.result_area.setWidgetResizable(True)
self.main_layout.addWidget(self.result_area)
self.result_area.setVerticalScrollBarPolicy(Qt.ScrollBarAsNeeded)
self.result_area.setWidget(self.widget)
self.layout = FlowLayout()
self.widget.setLayout(self.layout)
2021-08-08 09:42:40 +12:00
self.search_result_widget.setLayout(self.main_layout)
self.addWidget(WaitingSpinner())
self.setCurrentIndex(1)
def load_results(self, text: str):
self.setCurrentIndex(1)
if text != "":
2021-08-23 08:22:17 +12:00
self.api_core.search_game(text, self.show_results)
2021-08-08 09:42:40 +12:00
def show_results(self, results: dict):
self.widget.deleteLater()
self.widget = QWidget()
self.layout = FlowLayout()
2021-06-30 10:38:42 +12:00
if not results:
self.layout.addWidget(QLabel(self.tr("No results found")))
else:
for res in results:
w = _SearchResultItem(res)
w.show_info.connect(self.show_info.emit)
self.layout.addWidget(w)
self.widget.setLayout(self.layout)
self.result_area.setWidget(self.widget)
2021-08-08 09:42:40 +12:00
self.setCurrentIndex(0)
2021-06-10 09:12:49 +12:00
class _SearchResultItem(QGroupBox):
res: dict
show_info = pyqtSignal(dict)
def __init__(self, result: dict):
super(_SearchResultItem, self).__init__()
self.layout = QVBoxLayout()
self.image = ImageLabel()
for img in result["keyImages"]:
if img["type"] == "DieselStoreFrontTall":
width = 240
self.image.update_image(img["url"], result["title"], (width, 360))
break
else:
print("No image found")
self.layout.addWidget(self.image)
2021-06-10 09:12:49 +12:00
self.res = result
self.title = QLabel(self.res["title"])
title_font = QFont()
title_font.setPixelSize(15)
self.title.setFont(title_font)
self.title.setWordWrap(True)
self.layout.addWidget(self.title)
2021-12-24 22:09:50 +13:00
price = result["price"]["totalPrice"]["fmtPrice"]["originalPrice"]
discount_price = result["price"]["totalPrice"]["fmtPrice"]["discountPrice"]
price_layout = QHBoxLayout()
2021-06-15 08:30:57 +12:00
price_label = QLabel(price if price != "0" else self.tr("Free"))
price_layout.addWidget(price_label)
if price != discount_price:
font = QFont()
font.setStrikeOut(True)
price_label.setFont(font)
price_layout.addWidget(QLabel(discount_price))
# self.discount_price = QLabel(f"{self.tr('Discount price: ')}{discount_price}")
self.layout.addLayout(price_layout)
self.setLayout(self.layout)
self.setFixedWidth(260)
def mousePressEvent(self, a0: QtGui.QMouseEvent) -> None:
if a0.button() == 1:
self.show_info.emit(self.res)