Skip to content

Commit

Permalink
Store: Fix various wishlist issues.
Browse files Browse the repository at this point in the history
* Use horizontal scrollarea for free games. Based on the same idea as
WrapperSettings scrollarea. Both need some adjustments.

* Remove debugging dialogs. Need a better way anyways to debug.
  • Loading branch information
loathingKernel committed Feb 25, 2024
1 parent 7665579 commit fb0d5bb
Show file tree
Hide file tree
Showing 7 changed files with 215 additions and 186 deletions.
94 changes: 75 additions & 19 deletions rare/components/tabs/store/landing.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
import logging
from typing import List

from PyQt5.QtCore import Qt, pyqtSlot, pyqtSignal
from PyQt5.QtGui import QShowEvent, QHideEvent
from PyQt5.QtCore import Qt, pyqtSlot, pyqtSignal, QObject, QEvent
from PyQt5.QtGui import QShowEvent, QHideEvent, QResizeEvent
from PyQt5.QtWidgets import (
QHBoxLayout,
QWidget,
Expand Down Expand Up @@ -63,6 +63,38 @@ def show_details(self, game: CatalogOfferModel):
self.slideInWidget(self.details_widget)


class FreeGamesScroll(QScrollArea):
def __init__(self, parent=None):
super(FreeGamesScroll, self).__init__(parent=parent)
self.setObjectName(type(self).__name__)

def setWidget(self, w):
super().setWidget(w)
w.installEventFilter(self)

def eventFilter(self, a0: QObject, a1: QEvent) -> bool:
if a0 is self.widget() and a1.type() == QEvent.Resize:
self.__resize(a0)
return a0.event(a1)
return False

def __resize(self, e: QResizeEvent):
minh = self.horizontalScrollBar().minimum()
maxh = self.horizontalScrollBar().maximum()
# lk: when the scrollbar is not visible, min and max are 0
if maxh > minh:
height = (
e.size().height()
+ self.rect().height() // 2
- self.contentsRect().height() // 2
+ self.widget().layout().spacing()
+ self.horizontalScrollBar().sizeHint().height()
)
else:
height = e.size().height() + self.rect().height() - self.contentsRect().height()
self.setMinimumHeight(max(height, self.minimumHeight()))


class LandingWidget(QWidget, SideTabContents):
show_details = pyqtSignal(CatalogOfferModel)

Expand All @@ -76,21 +108,43 @@ def __init__(self, api: StoreAPI, parent=None):
self.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Fixed)

self.free_games_now = StoreGroup(self.tr("Free now"), layout=QHBoxLayout, parent=self)
self.free_games_now.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
self.free_games_now.main_layout.setAlignment(Qt.AlignLeft | Qt.AlignVCenter)
self.free_games_now.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed)

self.free_games_next = StoreGroup(self.tr("Free next week"), layout=QHBoxLayout, parent=self)
self.free_games_next.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
self.free_games_next.main_layout.setAlignment(Qt.AlignLeft | Qt.AlignVCenter)
self.free_games_next.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed)

self.discounts_group = StoreGroup(self.tr("Wishlist discounts"), layout=FlowLayout, parent=self)
self.discounts_group.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)

self.games_group = StoreGroup(self.tr("Free to play"), FlowLayout, self)
self.games_group.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
self.games_group.loading(False)
self.games_group.setVisible(True)

layout.addWidget(self.free_games_now, alignment=Qt.AlignTop)
layout.addWidget(self.free_games_next, alignment=Qt.AlignTop)
self.games_group.setVisible(False)

free_scroll = FreeGamesScroll(self)
free_container = QWidget(free_scroll)
free_scroll.setWidget(free_container)
free_container_layout = QHBoxLayout(free_container)

free_scroll.setWidgetResizable(True)
free_scroll.setFrameShape(QScrollArea.NoFrame)
free_scroll.setSizeAdjustPolicy(QScrollArea.AdjustToContents)
free_scroll.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff)

free_container_layout.setContentsMargins(0, 0, 0, 0)
free_container_layout.setAlignment(Qt.AlignLeft | Qt.AlignTop)
free_container_layout.setSizeConstraint(QHBoxLayout.SetFixedSize)
free_container_layout.addWidget(self.free_games_now)
free_container_layout.addWidget(self.free_games_next)

free_scroll.widget().setAutoFillBackground(False)
free_scroll.viewport().setAutoFillBackground(False)

# layout.addWidget(self.free_games_now, alignment=Qt.AlignTop)
# layout.addWidget(self.free_games_next, alignment=Qt.AlignTop)
layout.addWidget(free_scroll, alignment=Qt.AlignTop)
layout.addWidget(self.discounts_group, alignment=Qt.AlignTop)
layout.addWidget(self.games_group, alignment=Qt.AlignTop)
layout.addItem(QSpacerItem(0, 0, QSizePolicy.Fixed, QSizePolicy.Expanding))
Expand All @@ -113,12 +167,12 @@ def __update_wishlist_discounts(self, wishlist: List[WishlistItemModel]):
self.discounts_group.layout().removeWidget(w)
w.deleteLater()

for item in wishlist:
if item.offer.price.totalPrice.discount > 0:
w = StoreItemWidget(self.api.cached_manager, item.offer)
w.show_details.connect(self.show_details)
self.discounts_group.layout().addWidget(w)
self.discounts_group.setVisible(bool(wishlist))
for item in filter(lambda x: bool(x.offer.price.totalPrice.discount), wishlist):
w = StoreItemWidget(self.api.cached_manager, item.offer)
w.show_details.connect(self.show_details)
self.discounts_group.layout().addWidget(w)
have_discounts = any(map(lambda x: bool(x.offer.price.totalPrice.discount), wishlist))
self.discounts_group.setVisible(have_discounts)
self.discounts_group.loading(False)

def __update_free_games(self, free_games: List[CatalogOfferModel]):
Expand Down Expand Up @@ -171,13 +225,15 @@ def __update_free_games(self, free_games: List[CatalogOfferModel]):
self.free_games_next.loading(False)

def show_games(self, data):
if not data:
return

for w in self.games_group.findChildren(StoreItemWidget, options=Qt.FindDirectChildrenOnly):
self.games_group.layout().removeWidget(w)
w.deleteLater()

if data:
for game in data:
w = StoreItemWidget(self.api.cached_manager, game)
w.show_details.connect(self.show_details)
self.games_group.layout().addWidget(w)
for game in data:
w = StoreItemWidget(self.api.cached_manager, game)
w.show_details.connect(self.show_details)
self.games_group.layout().addWidget(w)
self.games_group.loading(False)
11 changes: 0 additions & 11 deletions rare/components/tabs/store/store_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
from PyQt5.QtCore import pyqtSignal, QObject
from PyQt5.QtWidgets import QApplication

from rare.components.tabs.store.api.debug import DebugDialog
from rare.components.tabs.store.constants import (
wishlist_query,
search_query,
Expand Down Expand Up @@ -154,13 +153,9 @@ def browse_games(self, browse_model: SearchStoreQuery, handle_func):
"query": search_query,
"variables": browse_model.to_dict()
}
# debug = DebugDialog(payload["variables"], None)
# debug.exec()
self.manager.post(graphql_url, lambda data: self.__handle_browse_games(data, handle_func), payload)

def __handle_browse_games(self, data, handle_func):
# debug = DebugDialog(data, None)
# debug.exec()
self.browse_active = False
if data is None:
data = {}
Expand Down Expand Up @@ -204,8 +199,6 @@ def get_game_config_cms(self, slug: str, is_bundle: bool, handle_func):

@staticmethod
def __handle_get_game(data, handle_func):
# debug = DebugDialog(data, None)
# debug.exec()
try:
product = DieselProduct.from_dict(data)
handle_func(product)
Expand All @@ -229,8 +222,6 @@ def add_to_wishlist(self, namespace, offer_id, handle_func: callable):
self.authed_manager.post(graphql_url, lambda data: self._handle_add_to_wishlist(data, handle_func), payload)

def _handle_add_to_wishlist(self, data, handle_func):
# debug = DebugDialog(data, None)
# debug.exec()
try:
response = ResponseModel.from_dict(data)
data = response.data.wishlist.addToWishlist
Expand All @@ -255,8 +246,6 @@ def remove_from_wishlist(self, namespace, offer_id, handle_func: callable):
payload)

def _handle_remove_from_wishlist(self, data, handle_func):
# debug = DebugDialog(data, None)
# debug.exec()
try:
response = ResponseModel.from_dict(data)
data = response.data.wishlist.removeFromWishlist
Expand Down
3 changes: 0 additions & 3 deletions rare/components/tabs/store/widgets/details.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
QSizePolicy,
)

from rare.components.tabs.store.api.debug import DebugDialog
from rare.components.tabs.store.api.models.diesel import DieselProduct, DieselProductDetail, DieselSystemDetail
from rare.components.tabs.store.api.models.response import CatalogOfferModel
from rare.components.tabs.store.store_api import StoreAPI
Expand Down Expand Up @@ -72,8 +71,6 @@ def handle_wishlist_update(self, wishlist: List[CatalogOfferModel]):
self.in_wishlist = False

def update_game(self, offer: CatalogOfferModel):
debug = DebugDialog(offer.__dict__, None)
debug.exec()
self.ui.title.setText(offer.title)
self.title_str = offer.title
self.id_str = offer.id
Expand Down
4 changes: 0 additions & 4 deletions rare/components/tabs/store/widgets/items.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
from PyQt5.QtGui import QMouseEvent
from PyQt5.QtWidgets import QPushButton

from rare.components.tabs.store.api.debug import DebugDialog
from rare.components.tabs.store.api.models.response import CatalogOfferModel
from rare.models.image import ImageSize
from rare.utils.misc import qta_icon
Expand All @@ -27,9 +26,6 @@ def mousePressEvent(self, a0: QMouseEvent) -> None:
self.show_details.emit(self.catalog_game)
if a0.button() == Qt.RightButton:
a0.accept()
print(self.catalog_game.__dict__)
dialog = DebugDialog(self.catalog_game.__dict__, self)
dialog.show()


class StoreItemWidget(ItemWidget):
Expand Down
Loading

0 comments on commit fb0d5bb

Please sign in to comment.