Clean up code and documentation

This commit is contained in:
DawnFire42 2026-06-08 22:20:48 -04:00
parent 4850bcf809
commit 192570eda6
No known key found for this signature in database
GPG key ID: 24BB855EE2911B33
4 changed files with 54 additions and 76 deletions

View file

@ -8,8 +8,10 @@
namespace namespace
{ {
/** @brief Extracts subtypes from a single card face's type line. */
QStringList extractSubtypesFromFace(const QString &faceType) QStringList extractSubtypesFromFace(const QString &faceType)
{ {
// Card type format: "Creature — Goblin Warrior" or "Legendary Enchantment — Saga"
QStringList parts = faceType.split(QStringLiteral("")); QStringList parts = faceType.split(QStringLiteral(""));
if (parts.size() > 1) { if (parts.size() > 1) {
return parts[1].split(QStringLiteral(" "), Qt::SkipEmptyParts); return parts[1].split(QStringLiteral(" "), Qt::SkipEmptyParts);
@ -22,68 +24,41 @@ QStringList extractSubtypesFromFace(const QString &faceType)
namespace SelectionSubtypeTally namespace SelectionSubtypeTally
{ {
QList<MainTypeGroup> countSubtypes(const QList<CardItem *> &cards) QList<SubtypeEntry> countSubtypes(const QList<CardItem *> &cards)
{ {
QMap<QString, QMap<QString, int>> subtypesByMainType; QMap<QString, int> subtypeCounts;
QMap<QString, int> cardCountPerMainType;
for (CardItem *card : cards) { for (CardItem *card : cards) {
if (card->getFaceDown() || card->getCard().isEmpty()) { if (card->getFaceDown() || card->getCard().isEmpty()) {
continue; continue;
} }
QString mainType = card->getCardInfo().getMainCardType();
if (mainType.isEmpty()) {
mainType = QStringLiteral("Other");
}
QString cardType = card->getCardInfo().getCardType(); QString cardType = card->getCardInfo().getCardType();
// Handle double-faced cards: "Creature — Human // Creature — Werewolf"
QStringList cardFaces = cardType.split(QStringLiteral(" // ")); QStringList cardFaces = cardType.split(QStringLiteral(" // "));
bool contributedSubtypes = false;
for (const QString &face : cardFaces) { for (const QString &face : cardFaces) {
QStringList subtypes = extractSubtypesFromFace(face); QStringList subtypes = extractSubtypesFromFace(face);
for (const QString &subtype : subtypes) { for (const QString &subtype : subtypes) {
subtypesByMainType[mainType][subtype]++; subtypeCounts[subtype]++;
contributedSubtypes = true; }
} }
} }
if (contributedSubtypes) { QList<SubtypeEntry> entries;
cardCountPerMainType[mainType]++; for (auto it = subtypeCounts.constBegin(); it != subtypeCounts.constEnd(); ++it) {
} entries.append({it.key(), it.value()});
} }
QList<MainTypeGroup> groups; // Sort by count ascending, then alphabetically (lowest counts at bottom of display)
for (auto it = subtypesByMainType.constBegin(); it != subtypesByMainType.constEnd(); ++it) { std::sort(entries.begin(), entries.end(), [](const SubtypeEntry &a, const SubtypeEntry &b) {
MainTypeGroup group;
group.mainType = it.key();
group.cardCount = cardCountPerMainType.value(it.key(), 0);
for (auto subIt = it.value().constBegin(); subIt != it.value().constEnd(); ++subIt) {
group.subtypes.append({subIt.key(), subIt.value()});
}
// Sort subtypes: by count ascending, then alphabetically
std::sort(group.subtypes.begin(), group.subtypes.end(), [](const SubtypeEntry &a, const SubtypeEntry &b) {
if (a.count != b.count) { if (a.count != b.count) {
return a.count < b.count; return a.count < b.count;
} }
return a.name < b.name; return a.name < b.name;
}); });
groups.append(group); return entries;
}
// Sort groups: by card count ascending, then alphabetically by main type
std::sort(groups.begin(), groups.end(), [](const MainTypeGroup &a, const MainTypeGroup &b) {
if (a.cardCount != b.cardCount) {
return a.cardCount < b.cardCount;
}
return a.mainType < b.mainType;
});
return groups;
} }
} // namespace SelectionSubtypeTally } // namespace SelectionSubtypeTally

View file

@ -3,26 +3,34 @@
#include <QList> #include <QList>
#include <QString> #include <QString>
#include <QStringList>
class CardItem; class CardItem;
/** @brief A single subtype (e.g., "Goblin", "Warrior") with its occurrence count. */
struct SubtypeEntry struct SubtypeEntry
{ {
QString name; QString name; ///< The subtype name
int count; int count; ///< Number of selected cards with this subtype
};
struct MainTypeGroup bool operator==(const SubtypeEntry &other) const
{ {
QString mainType; return name == other.name && count == other.count;
int cardCount; }
QList<SubtypeEntry> subtypes;
}; };
/**
* @brief Extracts and tallies subtypes from selected cards.
*/
namespace SelectionSubtypeTally namespace SelectionSubtypeTally
{ {
QList<MainTypeGroup> countSubtypes(const QList<CardItem *> &cards); /**
* @brief Parses card type lines and counts each subtype occurrence.
*
* Skips face-down cards and cards without type info.
* @param cards The list of selected card items to analyze.
* @return Entries sorted by count ascending, then alphabetically.
*/
QList<SubtypeEntry> countSubtypes(const QList<CardItem *> &cards);
} // namespace SelectionSubtypeTally } // namespace SelectionSubtypeTally
#endif #endif

View file

@ -91,10 +91,8 @@ void GameView::resizeEvent(QResizeEvent *event)
{ {
QGraphicsView::resizeEvent(event); QGraphicsView::resizeEvent(event);
GameScene *s = dynamic_cast<GameScene *>(scene()); GameScene *s = static_cast<GameScene *>(scene());
if (s) {
s->processViewSizeChange(event->size()); s->processViewSizeChange(event->size());
}
updateSceneRect(scene()->sceneRect()); updateSceneRect(scene()->sceneRect());
updateTotalSelectionCount(event->size()); updateTotalSelectionCount(event->size());
@ -251,33 +249,28 @@ void GameView::updateTotalSelectionCount(const QSize &viewSize)
if (!SettingsCache::instance().getShowSubtypeSelectionTally() || count <= 1) { if (!SettingsCache::instance().getShowSubtypeSelectionTally() || count <= 1) {
subtypeCountContainer->hide(); subtypeCountContainer->hide();
cachedSubtypeEntries.clear();
return; return;
} }
GameScene *gameScene = dynamic_cast<GameScene *>(scene()); GameScene *gameScene = static_cast<GameScene *>(scene());
if (!gameScene) { QList<SubtypeEntry> entries = SelectionSubtypeTally::countSubtypes(gameScene->selectedCards());
subtypeCountContainer->hide();
return;
}
QList<MainTypeGroup> groups = SelectionSubtypeTally::countSubtypes(gameScene->selectedCards());
if (groups.isEmpty()) {
subtypeCountContainer->hide();
return;
}
QList<SubtypeEntry> entries;
for (const MainTypeGroup &group : groups) {
entries.append(group.subtypes);
}
if (entries.isEmpty()) { if (entries.isEmpty()) {
subtypeCountContainer->hide(); subtypeCountContainer->hide();
cachedSubtypeEntries.clear();
return; return;
} }
QSize containerSize = rebuildSubtypeLabels(entries); // Only rebuild labels if entries changed
QSize containerSize;
if (entries != cachedSubtypeEntries) {
cachedSubtypeEntries = entries;
containerSize = rebuildSubtypeLabels(entries);
subtypeCountContainer->resize(containerSize); subtypeCountContainer->resize(containerSize);
} else {
containerSize = subtypeCountContainer->size();
}
int x = availableWidth - containerSize.width() - kMarginInPixels; int x = availableWidth - containerSize.width() - kMarginInPixels;
int y; int y;

View file

@ -7,13 +7,14 @@
#ifndef GAMEVIEW_H #ifndef GAMEVIEW_H
#define GAMEVIEW_H #define GAMEVIEW_H
#include "selection_subtype_tally.h"
#include <QGraphicsView> #include <QGraphicsView>
class GameScene; class GameScene;
class QGridLayout; class QGridLayout;
class QLabel; class QLabel;
class QRubberBand; class QRubberBand;
struct SubtypeEntry;
class GameView : public QGraphicsView class GameView : public QGraphicsView
{ {
@ -23,9 +24,10 @@ private:
QRubberBand *rubberBand; QRubberBand *rubberBand;
QLabel *dragCountLabel; QLabel *dragCountLabel;
QLabel *totalCountLabel; QLabel *totalCountLabel;
QWidget *subtypeCountContainer; ///< Container widget for subtype tally display QWidget *subtypeCountContainer;
QGridLayout *subtypeCountLayout; ///< Grid layout for subtype name/count pairs QGridLayout *subtypeCountLayout;
QPointF selectionOrigin; QPointF selectionOrigin;
QList<SubtypeEntry> cachedSubtypeEntries; ///< Cached entries to avoid redundant rebuilds
QSize rebuildSubtypeLabels(const QList<SubtypeEntry> &entries); QSize rebuildSubtypeLabels(const QList<SubtypeEntry> &entries);
void clearSubtypeLabels(); void clearSubtypeLabels();