@@ -87,8 +87,11 @@ def run(self):
8787
8888class CardViewer (QtWidgets .QWidget ):
8989 # To Build
90+ replaceInDeckRequested : QtCore .Signal = QtCore .Signal (str , str ) # (old_card_id, new_card_id)
91+
9092 def __init__ (self , parent = None , ** kwargs ):
9193 super (CardViewer , self ).__init__ (parent = parent , ** kwargs )
94+ self ._deck_card_id : str | None = None
9295 self .setupUi ()
9396 if constants .IMG_DOWNLOAD_METHOD == "qt" :
9497 self .imgDownloader = ImageDownloader ()
@@ -124,7 +127,11 @@ def setupUi(self):
124127 self .cardFaceChooser = QtWidgets .QPushButton ("See other face \u21B7 " )
125128 self .cardFaceChooser .setVisible (False )
126129 self .cardFaceChooser .clicked .connect (self .on_cardflip )
127- self .mainLayout .addWidget (self .cardFaceChooser , line .postinc (), 0 )
130+ self .mainLayout .addWidget (self .cardFaceChooser , line .val (), 0 )
131+ self .applyToDeckPB = QtWidgets .QPushButton ("Apply set to deck \u2713 " )
132+ self .applyToDeckPB .setVisible (False )
133+ self .applyToDeckPB .clicked .connect (self .on_applyToDeck )
134+ self .mainLayout .addWidget (self .applyToDeckPB , line .postinc (), 1 )
128135
129136 # Card Img
130137 self .cardImgGraphicsView = qt .ResizingGraphicsView ()
@@ -153,6 +160,17 @@ def setupUi(self):
153160 def on_reloadCardData (self ):
154161 self .display (self .card ["id" ], forceRefresh = True )
155162
163+ def display_from_deck (self , cardId : str ):
164+ """Display a card that originates from the current deck/collection."""
165+ self ._deck_card_id = cardId
166+ self .display (cardId )
167+
168+ def display_from_db (self , cardId : str ):
169+ """Display a card from the DB browser (no deck context)."""
170+ self ._deck_card_id = None
171+ self .applyToDeckPB .setVisible (False )
172+ self .display (cardId )
173+
156174 def on_add (self ):
157175 # TODO check when already present to raise qty instead of adding other line
158176 qt .findAttrInParents (self , "decklist" ).cardsList .addCard (self .card )
@@ -165,7 +183,7 @@ def on_randomCardPBClicked(self):
165183 self ._random_worker = RandomCardFetchWorker ()
166184 self ._random_worker .moveToThread (self ._random_thread )
167185 self ._random_thread .started .connect (self ._random_worker .run )
168- self ._random_worker .ready .connect (self .display )
186+ self ._random_worker .ready .connect (self .display_from_db )
169187 self ._random_worker .failed .connect (self ._on_display_failed )
170188 self ._random_worker .ready .connect (self ._random_thread .quit )
171189 self ._random_worker .failed .connect (self ._random_thread .quit )
@@ -324,14 +342,34 @@ def _on_display_data_ready(self, data: dict):
324342 except KeyError :
325343 self .scryfallUriLabel .setText (f"<a href=\" { self .card ['scryfall_uri' ]} \" >Scryfall Link</a>" )
326344
327- self .avgPriceLabel .setText (
328- str (utils .getFromDict (self .card , ["prices" , constants .CURRENCY [0 ]])) + " " + constants .CURRENCY [1 ]
329- )
345+ _price = utils .getFromDict (self .card , ["prices" , constants .CURRENCY [0 ]])
346+ _foil_key = constants .CURRENCY [0 ] + "_foil"
347+ _foil_price = utils .getFromDict (self .card , ["prices" , _foil_key ])
348+ if _price is not None :
349+ _price_text = f"{ _price } { constants .CURRENCY [1 ]} "
350+ elif _foil_price is not None :
351+ _price_text = f"{ _foil_price } { constants .CURRENCY [1 ]} (foil)"
352+ else :
353+ _price_text = "N/A"
354+ self .avgPriceLabel .setText (_price_text )
355+
356+ # Show "Apply to deck" button only when the displayed card differs from the deck's card
357+ if self ._deck_card_id is not None and self .card ["id" ] != self ._deck_card_id :
358+ self .applyToDeckPB .setVisible (True )
359+ else :
360+ self .applyToDeckPB .setVisible (False )
330361
331362 def _on_display_failed (self , error : str ):
332363 logging .warning (f"Failed to display card: { error } " )
333364 self .nameLabel .setText (f"Failed to load card" )
334365
366+ def on_applyToDeck (self ):
367+ if self ._deck_card_id is not None and self .card ["id" ] != self ._deck_card_id :
368+ old_id = self ._deck_card_id
369+ self ._deck_card_id = self .card ["id" ]
370+ self .applyToDeckPB .setVisible (False )
371+ self .replaceInDeckRequested .emit (old_id , self .card ["id" ])
372+
335373 def downloadCardImg (self , imageUri , cardId ):
336374 url = QtCore .QUrl .fromUserInput (imageUri )
337375 self .imgDownloader .start_download (url , cardId )
0 commit comments