Skip to content

Commit b42f433

Browse files
committed
other set of bug fixes and misc improvements
1 parent 2241520 commit b42f433

5 files changed

Lines changed: 99 additions & 9 deletions

File tree

‎mtgorg/connector.py‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,19 @@ def removeCardFromCollection(collectionName, cardId):
125125
)
126126

127127

128+
def replaceCardInCollection(collectionName: str, oldCardId: str, newCardId: str):
129+
collection = getCollection(collectionName)
130+
if collection is None:
131+
logging.error(f"Collection {collectionName=} not found")
132+
return
133+
cardList = collection["cardList"]
134+
for entry in cardList:
135+
if entry[1] == oldCardId:
136+
entry[1] = newCardId
137+
collection["cardList"] = cardList
138+
getDB().table(constants.COLLECTIONS_TABLE_NAME).update(collection, Query().name == collectionName)
139+
140+
128141
# ----------------------------------- DECKS ---------------------------------- #
129142
# TODO: handle commander/sideboard etc in card list
130143
# cardlist : (qty, id) -> (qty, id, zone)
@@ -220,6 +233,19 @@ def removeCardFromDeck(deckName, cardId):
220233
)
221234

222235

236+
def replaceCardInDeck(deckName: str, oldCardId: str, newCardId: str):
237+
deck = getDeck(deckName)
238+
if deck is None:
239+
logging.error(f"Deck {deckName=} not found")
240+
return
241+
cardList = deck["cardList"]
242+
for entry in cardList:
243+
if entry[1] == oldCardId:
244+
entry[1] = newCardId
245+
deck["cardList"] = cardList
246+
getDB().table(constants.DECKS_TABLE_NAME).update(deck, Query().name == deckName)
247+
248+
223249
# ----------------------------------- CACHE ---------------------------------- #
224250

225251

‎mtgorg/mtgorg_app.py‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ def setupUi(self):
140140

141141
# Left pane is card viewer
142142
self.cardViewer = CardViewer(self)
143+
self.cardViewer.replaceInDeckRequested.connect(self.on_replaceCardInDeck)
143144
self.splitter.addWidget(self.cardViewer)
144145

145146
# Middle Pane are card Browsers
@@ -169,11 +170,14 @@ def setupUi(self):
169170

170171
def on_decklistCardSelected(self, cardId: str):
171172
self.dbBrowser.dbResultsList.clearSelection()
172-
self.cardViewer.display(cardId)
173+
self.cardViewer.display_from_deck(cardId)
173174

174175
def on_dbBrowserCardSelected(self, cardId: str):
175176
self.decklist.cardsList.clearSelection()
176-
self.cardViewer.display(cardId)
177+
self.cardViewer.display_from_db(cardId)
178+
179+
def on_replaceCardInDeck(self, oldCardId: str, newCardId: str):
180+
self.decklist.cardsList.replaceCardInStack(oldCardId, newCardId)
177181

178182
def on_cardStackChange(self, cardStack: connector.Deck | connector.Collection):
179183
if cardStack is not None:

‎mtgorg/widgets/cardlistwidget.py‎

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,10 +120,13 @@ def getCardTableItem(self, cardData: dict, columns: list = []) -> list[QtWidgets
120120
text = utils.setSetsText([text])
121121
if column == "price":
122122
text = utils.getFromDict(cardData, ["prices", constants.CURRENCY[0]])
123+
_foil_key = constants.CURRENCY[0] + "_foil"
123124
if text is not None:
124125
text = str(text) + " " + constants.CURRENCY[1]
126+
elif utils.getFromDict(cardData, ["prices", _foil_key]) is not None:
127+
text = str(utils.getFromDict(cardData, ["prices", _foil_key])) + " " + constants.CURRENCY[1] + " (foil)"
125128
else:
126-
text = "?"
129+
text = "N/A"
127130
item.setData(QtCore.Qt.ItemDataRole.DisplayRole, text)
128131
item.setData(QtCore.Qt.ItemDataRole.UserRole, {"data": cardData, "column": column})
129132
dataList.append(item)
@@ -160,6 +163,8 @@ def setCardList(self, cardList: connector.Deck | connector.Collection):
160163
self.cardStack = []
161164
_loadingBar = qt.findAttrInParents(self, 'deckLoadingBar')
162165
if _loadingBar is not None:
166+
_loadingBar.setRange(0, len(cardList))
167+
_loadingBar.setValue(0)
163168
_loadingBar.setVisible(True)
164169

165170
self._loader_thread = QtCore.QThread()
@@ -180,6 +185,9 @@ def _on_card_loaded(self, qty: int, card: dict):
180185
self.insertRow(self.rowCount())
181186
card.update({"qty": qty})
182187
self._addOneLine(card)
188+
_loadingBar = qt.findAttrInParents(self, 'deckLoadingBar')
189+
if _loadingBar is not None:
190+
_loadingBar.setValue(_loadingBar.value() + 1)
183191

184192
def addCard(self, card: dict):
185193
self.insertRow(self.rowCount())
@@ -255,6 +263,19 @@ def keyPressEvent(self, event: QtGui.QKeyEvent) -> None:
255263
else:
256264
return super().keyPressEvent(event)
257265

266+
def replaceCardInStack(self, oldCardId: str, newCardId: str):
267+
stackType, stackName = qt.findAttrInParents(self, "deckSelector").getSelected()
268+
if stackType == "deck":
269+
connector.replaceCardInDeck(stackName, oldCardId, newCardId)
270+
deck = connector.getDeck(stackName)
271+
if deck is not None:
272+
self.setCardList(deck["cardList"])
273+
elif stackType == "collection":
274+
connector.replaceCardInCollection(stackName, oldCardId, newCardId)
275+
collection = connector.getCollection(stackName)
276+
if collection is not None:
277+
self.setCardList(collection["cardList"])
278+
258279
def updateCardListInfos(self):
259280
_loadingBar = qt.findAttrInParents(self, 'deckLoadingBar')
260281
if _loadingBar is not None:

‎mtgorg/widgets/cardslist.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ def initUi(self):
4949
self.cardsListButtonBoxLayout.addWidget(self.cardsListButton_color)
5050
self.cardsListLayout.addWidget(self.cardsListButtonBox)
5151
self.deckLoadingBar = QtWidgets.QProgressBar()
52-
self.deckLoadingBar.setRange(0, 0) # indeterminate
52+
self.deckLoadingBar.setRange(0, 1)
53+
self.deckLoadingBar.setValue(0)
5354
self.deckLoadingBar.setMaximumHeight(6)
5455
self.deckLoadingBar.setTextVisible(False)
5556
self.deckLoadingBar.setVisible(False)

‎mtgorg/widgets/cardviewer.py‎

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,11 @@ def run(self):
8787

8888
class 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

Comments
 (0)