Clean up code and documentation

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

View file

@ -3,26 +3,34 @@
#include <QList>
#include <QString>
#include <QStringList>
class CardItem;
/** @brief A single subtype (e.g., "Goblin", "Warrior") with its occurrence count. */
struct SubtypeEntry
{
QString name;
int count;
};
struct MainTypeGroup
{
QString mainType;
int cardCount;
QList<SubtypeEntry> subtypes;
QString name; ///< The subtype name
int count; ///< Number of selected cards with this subtype
bool operator==(const SubtypeEntry &other) const
{
return name == other.name && count == other.count;
}
};
/**
* @brief Extracts and tallies subtypes from selected cards.
*/
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
#endif

View file

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

View file

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