[ColorIdentityWidget] Refactor (#6506)

* [ColorIdentityWidget] Refactor and add setter

* rename manaCost field

* nvm, just refactor for now

* use QtUtils

* move clearLayout into populate

* add back cardInfo constructor
This commit is contained in:
RickyRister 2026-01-14 02:41:03 -08:00 committed by GitHub
parent 289b139be9
commit 47720ff286
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 20 additions and 38 deletions

View file

@ -8,30 +8,10 @@
#include <QRegularExpression> #include <QRegularExpression>
#include <QResizeEvent> #include <QResizeEvent>
#include <QSize> #include <QSize>
#include <libcockatrice/utility/qt_utils.h>
ColorIdentityWidget::ColorIdentityWidget(QWidget *parent, CardInfoPtr _card) : QWidget(parent), card(_card) ColorIdentityWidget::ColorIdentityWidget(QWidget *parent, const QString &_colorIdentity)
{ : QWidget(parent), colorIdentity(_colorIdentity)
layout = new QHBoxLayout(this);
layout->setSpacing(5); // Small spacing between icons
layout->setContentsMargins(0, 0, 0, 0);
layout->setAlignment(Qt::AlignCenter); // Ensure icons are centered
setLayout(layout);
// Define the full WUBRG set (White, Blue, Black, Red, Green)
QString fullColorIdentity = "WUBRG";
if (card) {
manaCost = card->getColors(); // Get mana cost string
QStringList symbols = parseColorIdentity(manaCost); // Parse mana cost string
populateManaSymbolWidgets();
}
connect(&SettingsCache::instance(), &SettingsCache::visualDeckStorageDrawUnusedColorIdentitiesChanged, this,
&ColorIdentityWidget::toggleUnusedVisibility);
}
ColorIdentityWidget::ColorIdentityWidget(QWidget *parent, QString _manaCost)
: QWidget(parent), card(nullptr), manaCost(_manaCost)
{ {
layout = new QHBoxLayout(this); layout = new QHBoxLayout(this);
layout->setSpacing(5); // Small spacing between icons layout->setSpacing(5); // Small spacing between icons
@ -45,12 +25,21 @@ ColorIdentityWidget::ColorIdentityWidget(QWidget *parent, QString _manaCost)
&ColorIdentityWidget::toggleUnusedVisibility); &ColorIdentityWidget::toggleUnusedVisibility);
} }
ColorIdentityWidget::ColorIdentityWidget(QWidget *parent, const CardInfoPtr &card)
: ColorIdentityWidget(parent, card->getColors())
{
}
void ColorIdentityWidget::populateManaSymbolWidgets() void ColorIdentityWidget::populateManaSymbolWidgets()
{ {
// Define the full WUBRG set (White, Blue, Black, Red, Green) // Define the full WUBRG set (White, Blue, Black, Red, Green)
QString fullColorIdentity = "WUBRG"; QString fullColorIdentity = "WUBRG";
QStringList symbols = parseColorIdentity(manaCost); // Parse mana cost string QStringList symbols = parseColorIdentity(colorIdentity); // Parse mana cost string
// clear old layout
QtUtils::clearLayoutRec(layout);
// populate mana symbols
if (SettingsCache::instance().getVisualDeckStorageDrawUnusedColorIdentities()) { if (SettingsCache::instance().getVisualDeckStorageDrawUnusedColorIdentities()) {
for (const QString symbol : fullColorIdentity) { for (const QString symbol : fullColorIdentity) {
auto *manaSymbol = new ManaSymbolWidget(this, symbol, symbols.contains(symbol)); auto *manaSymbol = new ManaSymbolWidget(this, symbol, symbols.contains(symbol));
@ -66,13 +55,6 @@ void ColorIdentityWidget::populateManaSymbolWidgets()
void ColorIdentityWidget::toggleUnusedVisibility() void ColorIdentityWidget::toggleUnusedVisibility()
{ {
if (layout != nullptr) {
QLayoutItem *item;
while ((item = layout->takeAt(0)) != nullptr) {
item->widget()->deleteLater(); // Delete the widget
delete item; // Delete the layout item
}
}
populateManaSymbolWidgets(); populateManaSymbolWidgets();
} }
@ -97,12 +79,12 @@ void ColorIdentityWidget::resizeEvent(QResizeEvent *event)
} }
} }
QStringList ColorIdentityWidget::parseColorIdentity(const QString &cmc) QStringList ColorIdentityWidget::parseColorIdentity(const QString &manaString)
{ {
QStringList symbols; QStringList symbols;
// Handle split costs (e.g., "3U // 4UU") // Handle split costs (e.g., "3U // 4UU")
QStringList splitCosts = cmc.split(" // "); QStringList splitCosts = manaString.split(" // ");
for (const QString &part : splitCosts) { for (const QString &part : splitCosts) {
QRegularExpression regex(R"(\{([^}]+)\}|(\d+)|([WUBRGCSPX]))"); QRegularExpression regex(R"(\{([^}]+)\}|(\d+)|([WUBRGCSPX]))");
QRegularExpressionMatchIterator matches = regex.globalMatch(part); QRegularExpressionMatchIterator matches = regex.globalMatch(part);

View file

@ -15,19 +15,19 @@ class ColorIdentityWidget : public QWidget
{ {
Q_OBJECT Q_OBJECT
public: public:
explicit ColorIdentityWidget(QWidget *parent, CardInfoPtr card); explicit ColorIdentityWidget(QWidget *parent, const QString &_colorIdentity = "");
explicit ColorIdentityWidget(QWidget *parent, QString manaCost); explicit ColorIdentityWidget(QWidget *parent, const CardInfoPtr &card);
void populateManaSymbolWidgets(); void populateManaSymbolWidgets();
QStringList parseColorIdentity(const QString &manaString); static QStringList parseColorIdentity(const QString &manaString);
public slots: public slots:
void resizeEvent(QResizeEvent *event) override; void resizeEvent(QResizeEvent *event) override;
void toggleUnusedVisibility(); void toggleUnusedVisibility();
private: private:
CardInfoPtr card; QString colorIdentity;
QString manaCost;
QHBoxLayout *layout; QHBoxLayout *layout;
}; };