1
0
Fork 0
mirror of synced 2024-06-24 17:20:23 +12:00
Rare/rare/components/tabs/shop/search_results.py

117 lines
4.4 KiB
Python
Raw Normal View History

from PyQt5 import QtGui
from PyQt5.QtCore import pyqtSignal, Qt
from PyQt5.QtGui import QFont
2021-08-08 09:42:40 +12:00
from PyQt5.QtWidgets import QWidget, QVBoxLayout, QHBoxLayout, QLabel, QScrollArea, QGroupBox, QPushButton, \
QStackedWidget
2021-08-08 09:42:40 +12:00
from rare.components.tabs.shop.constants import search_query
from rare.utils.extra_widgets import ImageLabel, FlowLayout, WaitingSpinner
from rare.utils.utils import QtRequestManager, get_lang
2021-08-08 09:42:40 +12:00
class SearchResults(QStackedWidget):
show_info = pyqtSignal(dict)
def __init__(self):
super(SearchResults, self).__init__()
2021-08-08 09:42:40 +12:00
self.search_result_widget = QWidget()
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_manager = QtRequestManager("json")
self.search_manager.data_ready.connect(self.show_results)
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 != "":
locale = get_lang()
payload = {
"query": search_query,
"variables": {"category": "games/edition/base|bundles/games|editors|software/edition/base",
"count": 20,
"country": locale.upper(), "keywords": text, "locale": locale, "sortDir": "DESC",
"allowCountries": locale.upper(),
"start": 0, "tag": "", "withMapping": False, "withPrice": True}
}
self.search_manager.post("https://www.epicgames.com/graphql", payload)
def show_results(self, results: dict):
results = results["data"]["Catalog"]["searchStore"]["elements"]
QVBoxLayout().addWidget(self.widget)
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)
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)