[Refactor] Clean up some PrintingSelector widgets (#6451)

* remove currentZone from PrintingSelector

* don't store constructor args in fields if they're just passed

* simplify some methods

* refactor

* clean up initializeFormats

* more refactoring in CardAmountWidget
This commit is contained in:
RickyRister 2025-12-29 03:03:44 -08:00 committed by GitHub
parent ca3f6bba02
commit 96c82a0377
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
15 changed files with 92 additions and 110 deletions

View file

@ -23,8 +23,7 @@ AllZonesCardAmountWidget::AllZonesCardAmountWidget(QWidget *parent,
QTreeView *deckView,
QSlider *cardSizeSlider,
const ExactCard &rootCard)
: QWidget(parent), deckEditor(deckEditor), deckModel(deckModel), deckView(deckView), cardSizeSlider(cardSizeSlider),
rootCard(rootCard)
: QWidget(parent), cardSizeSlider(cardSizeSlider)
{
layout = new QVBoxLayout(this);
layout->setAlignment(Qt::AlignHCenter);

View file

@ -36,11 +36,7 @@ public slots:
private:
QVBoxLayout *layout;
AbstractTabDeckEditor *deckEditor;
DeckListModel *deckModel;
QTreeView *deckView;
QSlider *cardSizeSlider;
ExactCard rootCard;
QLabel *zoneLabelMainboard;
CardAmountWidget *buttonBoxMainboard;
QLabel *zoneLabelSideboard;

View file

@ -137,6 +137,31 @@ void CardAmountWidget::updateCardCount()
layout->activate();
}
static QModelIndex addAndReplacePrintings(DeckListModel *model,
const QModelIndex &existing,
const ExactCard &rootCard,
const QString &zone,
int extraCopies,
bool replaceProviderless)
{
auto newCardIndex = model->addCard(rootCard, zone);
if (!newCardIndex.isValid()) {
return {};
}
// Check if a card without a providerId already exists in the deckModel and replace it, if so.
if (existing.isValid() && existing != newCardIndex && replaceProviderless) {
for (int i = 0; i < extraCopies; i++) {
model->addCard(rootCard, zone);
}
model->removeRow(existing.row(), existing.parent());
}
// Set Index and Focus as if the user had just clicked the new card and modify the deckEditor saveState
return model->findCard(rootCard.getName(), zone, rootCard.getPrinting().getUuid(),
rootCard.getPrinting().getProperty("num"));
}
/**
* @brief Adds a printing of the card to the specified zone (Mainboard or Sideboard).
*
@ -144,26 +169,24 @@ void CardAmountWidget::updateCardCount()
*/
void CardAmountWidget::addPrinting(const QString &zone)
{
int addedCount = 1;
// Check if we will need to add extra copies due to replacing copies without providerIds
QModelIndex existing = deckModel->findCard(rootCard.getName(), zone);
int extraCopies = 0;
bool replacingProviderless = false;
if (existing.isValid()) {
QString providerId =
QString foundProviderId =
existing.siblingAtColumn(DeckListModelColumns::CARD_PROVIDER_ID).data(Qt::DisplayRole).toString();
if (providerId.isEmpty()) {
if (foundProviderId.isEmpty()) {
int amount = existing.data(Qt::DisplayRole).toInt();
extraCopies = amount - 1; // One less because we *always* add one
replacingProviderless = true;
}
}
addedCount += extraCopies;
QString reason = QString("Added %1 copies of '%2 (%3) %4' to %5 [ProviderID: %6]%7")
.arg(addedCount)
.arg(1 + extraCopies)
.arg(rootCard.getName())
.arg(rootCard.getPrinting().getSet()->getShortName())
.arg(rootCard.getPrinting().getProperty("num"))
@ -174,26 +197,13 @@ void CardAmountWidget::addPrinting(const QString &zone)
emit deckModified(reason);
// Add the card and expand the list UI
auto newCardIndex = deckModel->addCard(rootCard, zone);
auto newCardIndex = addAndReplacePrintings(deckModel, existing, rootCard, zone, extraCopies, replacingProviderless);
// Check if a card without a providerId already exists in the deckModel and replace it, if so.
QString foundProviderId =
existing.siblingAtColumn(DeckListModelColumns::CARD_PROVIDER_ID).data(Qt::DisplayRole).toString();
if (existing.isValid() && existing != newCardIndex && foundProviderId == "") {
auto amount = existing.data(Qt::DisplayRole);
for (int i = 0; i < amount.toInt() - 1; i++) {
deckModel->addCard(rootCard, zone);
}
deckModel->removeRow(existing.row(), existing.parent());
if (newCardIndex.isValid()) {
deckView->setCurrentIndex(newCardIndex);
deckView->setFocus(Qt::FocusReason::MouseFocusReason);
deckEditor->setModified(true);
}
// Set Index and Focus as if the user had just clicked the new card and modify the deckEditor saveState
newCardIndex = deckModel->findCard(rootCard.getName(), zone, rootCard.getPrinting().getUuid(),
rootCard.getPrinting().getProperty("num"));
deckView->setCurrentIndex(newCardIndex);
deckView->setFocus(Qt::FocusReason::MouseFocusReason);
deckEditor->setModified(true);
}
/**
@ -259,22 +269,14 @@ void CardAmountWidget::decrementCardHelper(const QString &zone)
*/
int CardAmountWidget::countCardsInZone(const QString &deckZone)
{
if (rootCard.getPrinting().getUuid().isEmpty()) {
return 0; // Cards without uuids/providerIds CANNOT match another card, they are undefined for us.
}
QString uuid = rootCard.getPrinting().getUuid();
if (!deckModel) {
return -1;
if (uuid.isEmpty()) {
return 0; // Cards without uuids/providerIds CANNOT match another card, they are undefined for us.
}
QList<ExactCard> cards = deckModel->getCardsForZone(deckZone);
int count = 0;
for (auto currentCard : cards) {
if (currentCard.getPrinting().getUuid() == rootCard.getPrinting().getProperty("uuid")) {
count++;
}
}
return count;
return std::count_if(cards.cbegin(), cards.cend(),
[&uuid](const ExactCard &card) { return card.getPrinting().getUuid() == uuid; });
}

View file

@ -115,9 +115,8 @@ void PrintingSelector::updateDisplay()
* @brief Sets the current card for the selector and updates the display.
*
* @param newCard The new card to set.
* @param _currentZone The current zone the card is in.
*/
void PrintingSelector::setCard(const CardInfoPtr &newCard, const QString &_currentZone)
void PrintingSelector::setCard(const CardInfoPtr &newCard)
{
if (newCard.isNull()) {
return;
@ -129,7 +128,6 @@ void PrintingSelector::setCard(const CardInfoPtr &newCard, const QString &_curre
}
selectedCard = newCard;
currentZone = _currentZone;
if (isVisible()) {
updateDisplay();
}
@ -166,8 +164,8 @@ void PrintingSelector::getAllSetsForCurrentCard()
connect(widgetLoadingBufferTimer, &QTimer::timeout, this, [=, this]() mutable {
for (int i = 0; i < BATCH_SIZE && currentIndex < printingsToUse.size(); ++i, ++currentIndex) {
auto card = ExactCard(selectedCard, printingsToUse[currentIndex]);
auto *cardDisplayWidget = new PrintingSelectorCardDisplayWidget(
this, deckEditor, deckModel, deckView, cardSizeWidget->getSlider(), card, currentZone);
auto *cardDisplayWidget = new PrintingSelectorCardDisplayWidget(this, deckEditor, deckModel, deckView,
cardSizeWidget->getSlider(), card);
flowWidget->addWidget(cardDisplayWidget);
cardDisplayWidget->clampSetNameToPicture();
connect(cardDisplayWidget, &PrintingSelectorCardDisplayWidget::cardPreferenceChanged, this,

View file

@ -33,7 +33,7 @@ class PrintingSelector : public QWidget
public:
PrintingSelector(QWidget *parent, AbstractTabDeckEditor *deckEditor);
void setCard(const CardInfoPtr &newCard, const QString &_currentZone);
void setCard(const CardInfoPtr &newCard);
void getAllSetsForCurrentCard();
[[nodiscard]] DeckListModel *getDeckModel() const
{
@ -78,7 +78,6 @@ private:
DeckListModel *deckModel;
QTreeView *deckView;
CardInfoPtr selectedCard;
QString currentZone;
QTimer *widgetLoadingBufferTimer;
int currentIndex = 0;
};

View file

@ -17,22 +17,19 @@
* display.
*
* @param parent The parent widget for this display.
* @param _deckEditor The TabDeckEditor instance for deck management.
* @param _deckModel The DeckListModel instance providing deck data.
* @param _deckView The QTreeView instance displaying the deck.
* @param _cardSizeSlider The slider controlling the size of the displayed card.
* @param _rootCard The root card object, representing the card to be displayed.
* @param _currentZone The current zone in which the card is located.
* @param deckEditor The TabDeckEditor instance for deck management.
* @param deckModel The DeckListModel instance providing deck data.
* @param deckView The QTreeView instance displaying the deck.
* @param cardSizeSlider The slider controlling the size of the displayed card.
* @param rootCard The root card object, representing the card to be displayed.
*/
PrintingSelectorCardDisplayWidget::PrintingSelectorCardDisplayWidget(QWidget *parent,
AbstractTabDeckEditor *_deckEditor,
DeckListModel *_deckModel,
QTreeView *_deckView,
QSlider *_cardSizeSlider,
const ExactCard &_rootCard,
QString &_currentZone)
: QWidget(parent), deckEditor(_deckEditor), deckModel(_deckModel), deckView(_deckView),
cardSizeSlider(_cardSizeSlider), rootCard(_rootCard), currentZone(_currentZone)
AbstractTabDeckEditor *deckEditor,
DeckListModel *deckModel,
QTreeView *deckView,
QSlider *cardSizeSlider,
const ExactCard &rootCard)
: QWidget(parent)
{
layout = new QVBoxLayout(this);
setLayout(layout);

View file

@ -20,12 +20,11 @@ class PrintingSelectorCardDisplayWidget : public QWidget
public:
PrintingSelectorCardDisplayWidget(QWidget *parent,
AbstractTabDeckEditor *_deckEditor,
DeckListModel *_deckModel,
QTreeView *_deckView,
QSlider *_cardSizeSlider,
const ExactCard &_rootCard,
QString &_currentZone);
AbstractTabDeckEditor *deckEditor,
DeckListModel *deckModel,
QTreeView *deckView,
QSlider *cardSizeSlider,
const ExactCard &rootCard);
public slots:
void clampSetNameToPicture();
@ -36,12 +35,6 @@ signals:
private:
QVBoxLayout *layout;
SetNameAndCollectorsNumberDisplayWidget *setNameAndCollectorsNumberDisplayWidget;
AbstractTabDeckEditor *deckEditor;
DeckListModel *deckModel;
QTreeView *deckView;
QSlider *cardSizeSlider;
ExactCard rootCard;
QString currentZone;
PrintingSelectorCardOverlayWidget *overlayWidget;
};

View file

@ -22,19 +22,18 @@
*
* @param parent The parent widget for this overlay.
* @param _deckEditor The TabDeckEditor instance for deck management.
* @param _deckModel The DeckListModel instance providing deck data.
* @param _deckView The QTreeView instance displaying the deck.
* @param _cardSizeSlider The slider controlling the size of the card.
* @param deckModel The DeckListModel instance providing deck data.
* @param deckView The QTreeView instance displaying the deck.
* @param cardSizeSlider The slider controlling the size of the card.
* @param _rootCard The root card object that contains information about the card.
*/
PrintingSelectorCardOverlayWidget::PrintingSelectorCardOverlayWidget(QWidget *parent,
AbstractTabDeckEditor *_deckEditor,
DeckListModel *_deckModel,
QTreeView *_deckView,
QSlider *_cardSizeSlider,
DeckListModel *deckModel,
QTreeView *deckView,
QSlider *cardSizeSlider,
const ExactCard &_rootCard)
: QWidget(parent), deckEditor(_deckEditor), deckModel(_deckModel), deckView(_deckView),
cardSizeSlider(_cardSizeSlider), rootCard(_rootCard)
: QWidget(parent), deckEditor(_deckEditor), rootCard(_rootCard)
{
// Set up the main layout
auto *mainLayout = new QVBoxLayout(this);

View file

@ -48,9 +48,6 @@ private:
AllZonesCardAmountWidget *allZonesCardAmountWidget;
QLabel *pinBadge = nullptr;
AbstractTabDeckEditor *deckEditor;
DeckListModel *deckModel;
QTreeView *deckView;
QSlider *cardSizeSlider;
ExactCard rootCard;
};

View file

@ -13,7 +13,7 @@ SetNameAndCollectorsNumberDisplayWidget::SetNameAndCollectorsNumberDisplayWidget
const QString &_setName,
const QString &_collectorsNumber,
QSlider *_cardSizeSlider)
: QWidget(parent)
: QWidget(parent), cardSizeSlider(_cardSizeSlider)
{
// Set up the layout for the widget
layout = new QVBoxLayout(this);
@ -35,7 +35,6 @@ SetNameAndCollectorsNumberDisplayWidget::SetNameAndCollectorsNumberDisplayWidget
collectorsNumber->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
// Store the card size slider and connect its signal to the font size adjustment slot
cardSizeSlider = _cardSizeSlider;
connect(cardSizeSlider, &QSlider::valueChanged, this, &SetNameAndCollectorsNumberDisplayWidget::adjustFontSize);
// Add labels to the layout