Initial commit of mtgonline project
This commit is contained in:
@@ -0,0 +1,66 @@
|
||||
#include "card_set.h"
|
||||
|
||||
#include <QSet>
|
||||
#include <utility>
|
||||
|
||||
const char *CardSet::TOKENS_SETNAME = "TK";
|
||||
|
||||
CardSet::CardSet(ICardSetPriorityController *_priorityController,
|
||||
const QString &_shortName,
|
||||
const QString &_longName,
|
||||
const QString &_setType,
|
||||
const QDate &_releaseDate,
|
||||
const CardSet::Priority _priority)
|
||||
: priorityController(std::move(_priorityController)), shortName(_shortName), longName(_longName),
|
||||
releaseDate(_releaseDate), setType(_setType), priority(_priority)
|
||||
{
|
||||
loadSetOptions();
|
||||
}
|
||||
|
||||
CardSetPtr CardSet::newInstance(ICardSetPriorityController *_priorityController,
|
||||
const QString &_shortName,
|
||||
const QString &_longName,
|
||||
const QString &_setType,
|
||||
const QDate &_releaseDate,
|
||||
const Priority _priority)
|
||||
{
|
||||
CardSetPtr ptr(new CardSet(_priorityController, _shortName, _longName, _setType, _releaseDate, _priority));
|
||||
// ptr->setSmartPointer(ptr);
|
||||
return ptr;
|
||||
}
|
||||
|
||||
QString CardSet::getCorrectedShortName() const
|
||||
{
|
||||
// For Windows machines.
|
||||
QSet<QString> invalidFileNames;
|
||||
invalidFileNames << "CON" << "PRN" << "AUX" << "NUL" << "COM1" << "COM2" << "COM3" << "COM4" << "COM5" << "COM6"
|
||||
<< "COM7" << "COM8" << "COM9" << "LPT1" << "LPT2" << "LPT3" << "LPT4" << "LPT5" << "LPT6" << "LPT7"
|
||||
<< "LPT8" << "LPT9";
|
||||
|
||||
return invalidFileNames.contains(shortName) ? shortName + "_" : shortName;
|
||||
}
|
||||
|
||||
void CardSet::loadSetOptions()
|
||||
{
|
||||
sortKey = priorityController->getSortKey(shortName);
|
||||
enabled = priorityController->isEnabled(shortName);
|
||||
isknown = priorityController->isKnown(shortName);
|
||||
}
|
||||
|
||||
void CardSet::setSortKey(unsigned int _sortKey)
|
||||
{
|
||||
sortKey = _sortKey;
|
||||
priorityController->setSortKey(shortName, _sortKey);
|
||||
}
|
||||
|
||||
void CardSet::setEnabled(bool _enabled)
|
||||
{
|
||||
enabled = _enabled;
|
||||
priorityController->setEnabled(shortName, _enabled);
|
||||
}
|
||||
|
||||
void CardSet::setIsKnown(bool _isknown)
|
||||
{
|
||||
isknown = _isknown;
|
||||
priorityController->setIsKnown(shortName, _isknown);
|
||||
}
|
||||
@@ -0,0 +1,240 @@
|
||||
#ifndef COCKATRICE_CARD_SET_H
|
||||
#define COCKATRICE_CARD_SET_H
|
||||
|
||||
#include <QDate>
|
||||
#include <QList>
|
||||
#include <QSharedPointer>
|
||||
#include <QString>
|
||||
#include <libcockatrice/interfaces/interface_card_set_priority_controller.h>
|
||||
|
||||
class CardInfo;
|
||||
using CardInfoPtr = QSharedPointer<CardInfo>;
|
||||
|
||||
class CardSet;
|
||||
using CardSetPtr = QSharedPointer<CardSet>;
|
||||
|
||||
/**
|
||||
* @class CardSet
|
||||
* @ingroup CardSets
|
||||
*
|
||||
* @brief A collection of cards grouped under a common identifier.
|
||||
*
|
||||
* A set serves both as metadata (identifier, title, category, release date, and priority)
|
||||
* and as a container of all cards that belong to it. Each set can be enabled/disabled
|
||||
* and marked as known/unknown depending on context.
|
||||
*
|
||||
* The class inherits from `QList<CardInfoPtr>`, so it can be iterated over directly
|
||||
* to access its contents.
|
||||
*
|
||||
* Typical usage:
|
||||
* - Query metadata such as identifier, category, or release date.
|
||||
* - Enable or disable sets according to user preference.
|
||||
* - Store and retrieve CardInfo objects associated with the set.
|
||||
*/
|
||||
class CardSet : public QList<CardInfoPtr>
|
||||
{
|
||||
public:
|
||||
/**
|
||||
* @enum Priority
|
||||
* @brief Defines relative ordering and importance of sets.
|
||||
*/
|
||||
enum Priority
|
||||
{
|
||||
PriorityFallback = 0, ///< Used when no other priority is defined.
|
||||
PriorityPrimary = 10, ///< Primary, canonical set.
|
||||
PrioritySecondary = 20, ///< Secondary but relevant.
|
||||
PriorityReprint = 30, ///< Duplicate or reprint category.
|
||||
PriorityOther = 40, ///< Miscellaneous grouping.
|
||||
PriorityLowest = 100, ///< Lowest sorting priority.
|
||||
};
|
||||
|
||||
static const char *TOKENS_SETNAME; ///< Reserved identifier for token-like sets.
|
||||
|
||||
private:
|
||||
ICardSetPriorityController *priorityController; ///< Interface to the card set priority controller.
|
||||
QString shortName; ///< Short identifier for the set.
|
||||
QString longName; ///< Full name for the set.
|
||||
unsigned int sortKey; ///< Custom numeric sort key.
|
||||
QDate releaseDate; ///< Release date, may be empty if unknown.
|
||||
QString setType; ///< Type/category label for the set.
|
||||
Priority priority; ///< Priority level for sorting and relevance.
|
||||
bool enabled; ///< Whether the set is active/enabled.
|
||||
bool isknown; ///< Whether the set is considered known.
|
||||
|
||||
public:
|
||||
/**
|
||||
* @brief Constructs a CardSet.
|
||||
*
|
||||
* @param priorityController Interface to a card set priority controller.
|
||||
* @param _shortName Identifier string.
|
||||
* @param _longName Full descriptive name.
|
||||
* @param _setType Type/category string.
|
||||
* @param _releaseDate Release date (optional).
|
||||
* @param _priority Sorting/priority level.
|
||||
*/
|
||||
explicit CardSet(ICardSetPriorityController *priorityController,
|
||||
const QString &_shortName = QString(),
|
||||
const QString &_longName = QString(),
|
||||
const QString &_setType = QString(),
|
||||
const QDate &_releaseDate = QDate(),
|
||||
const Priority _priority = PriorityFallback);
|
||||
|
||||
/**
|
||||
* @brief Creates and returns a new shared CardSet instance.
|
||||
*
|
||||
* @param priorityController Interface to a card set priority controller.
|
||||
* @param _shortName Identifier string.
|
||||
* @param _longName Full descriptive name.
|
||||
* @param _setType Type/category string.
|
||||
* @param _releaseDate Release date (optional).
|
||||
* @param _priority Sorting/priority level.
|
||||
* @return A shared pointer to the new CardSet.
|
||||
*/
|
||||
static CardSetPtr newInstance(ICardSetPriorityController *priorityController,
|
||||
const QString &_shortName = QString(),
|
||||
const QString &_longName = QString(),
|
||||
const QString &_setType = QString(),
|
||||
const QDate &_releaseDate = QDate(),
|
||||
const Priority _priority = PriorityFallback);
|
||||
|
||||
/**
|
||||
* @brief Returns a safe, sanitized version of the short name.
|
||||
*
|
||||
* Intended for file paths or identifiers where only certain
|
||||
* characters are allowed.
|
||||
*
|
||||
* @return Sanitized short name.
|
||||
*/
|
||||
[[nodiscard]] QString getCorrectedShortName() const;
|
||||
|
||||
/** @return Short identifier of the set. */
|
||||
[[nodiscard]] QString getShortName() const
|
||||
{
|
||||
return shortName;
|
||||
}
|
||||
|
||||
/** @return Descriptive name of the set. */
|
||||
[[nodiscard]] QString getLongName() const
|
||||
{
|
||||
return longName;
|
||||
}
|
||||
|
||||
/** @return Type/category string of the set. */
|
||||
[[nodiscard]] QString getSetType() const
|
||||
{
|
||||
return setType;
|
||||
}
|
||||
|
||||
/** @return Release date of the set. */
|
||||
[[nodiscard]] QDate getReleaseDate() const
|
||||
{
|
||||
return releaseDate;
|
||||
}
|
||||
|
||||
/** @return Priority level of the set. */
|
||||
[[nodiscard]] Priority getPriority() const
|
||||
{
|
||||
return priority;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Sets the full name of the set.
|
||||
* @param _longName New full name.
|
||||
*/
|
||||
void setLongName(const QString &_longName)
|
||||
{
|
||||
longName = _longName;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Sets the category/type of the set.
|
||||
* @param _setType New category string.
|
||||
*/
|
||||
void setSetType(const QString &_setType)
|
||||
{
|
||||
setType = _setType;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Sets the release date of the set.
|
||||
* @param _releaseDate New release date.
|
||||
*/
|
||||
void setReleaseDate(const QDate &_releaseDate)
|
||||
{
|
||||
releaseDate = _releaseDate;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Updates the priority of the set.
|
||||
* @param _priority New priority value.
|
||||
*/
|
||||
|
||||
void setPriority(const Priority _priority)
|
||||
{
|
||||
priority = _priority;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Loads state values (enabled, known, sort key) from configuration.
|
||||
*
|
||||
* Reads external configuration and applies it to this set.
|
||||
*/
|
||||
void loadSetOptions();
|
||||
|
||||
void setSortKeyInMemory(unsigned int _sortKey)
|
||||
{
|
||||
sortKey = _sortKey;
|
||||
}
|
||||
void setEnabledInMemory(bool _enabled)
|
||||
{
|
||||
enabled = _enabled;
|
||||
}
|
||||
|
||||
/** @return The sort key assigned to this set. */
|
||||
[[nodiscard]] int getSortKey() const
|
||||
{
|
||||
return sortKey;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Assigns a new sort key to this set.
|
||||
* @param _sortKey The numeric key to use for sorting.
|
||||
*/
|
||||
void setSortKey(unsigned int _sortKey);
|
||||
|
||||
/** @return True if the set is enabled. */
|
||||
[[nodiscard]] bool getEnabled() const
|
||||
{
|
||||
return enabled;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Enables or disables the set.
|
||||
* @param _enabled True to enable, false to disable.
|
||||
*/
|
||||
void setEnabled(bool _enabled);
|
||||
|
||||
/** @return True if the set is considered known. */
|
||||
[[nodiscard]] bool getIsKnown() const
|
||||
{
|
||||
return isknown;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Marks the set as known or unknown.
|
||||
* @param _isknown True if known, false if unknown.
|
||||
*/
|
||||
void setIsKnown(bool _isknown);
|
||||
|
||||
/**
|
||||
* @brief Determines whether the set has incomplete metadata and should be ignored.
|
||||
*
|
||||
* @return True if the long name, type, and release date are all empty.
|
||||
*/
|
||||
[[nodiscard]] bool getIsKnownIgnored() const
|
||||
{
|
||||
return longName.length() + setType.length() + releaseDate.toString().length() == 0;
|
||||
}
|
||||
};
|
||||
|
||||
#endif // COCKATRICE_CARD_SET_H
|
||||
@@ -0,0 +1,66 @@
|
||||
/**
|
||||
* @file card_set_comparator.h
|
||||
* @ingroup CardSets
|
||||
*/
|
||||
//! \todo Document this file.
|
||||
|
||||
#ifndef SET_PRIORITY_COMPARATOR_H
|
||||
#define SET_PRIORITY_COMPARATOR_H
|
||||
|
||||
#include "../card_info.h"
|
||||
|
||||
class SetPriorityComparator
|
||||
{
|
||||
public:
|
||||
/*
|
||||
* Returns true if a has higher download priority than b
|
||||
* Enabled sets have priority over disabled sets
|
||||
* Both groups follow the user-defined order
|
||||
*/
|
||||
inline bool operator()(const CardSetPtr &a, const CardSetPtr &b) const
|
||||
{
|
||||
if (a->getEnabled()) {
|
||||
return !b->getEnabled() || a->getSortKey() < b->getSortKey();
|
||||
} else {
|
||||
return !b->getEnabled() && a->getSortKey() < b->getSortKey();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
class SetReleaseDateComparator
|
||||
{
|
||||
public:
|
||||
/*
|
||||
* Returns true if a has higher download priority than b
|
||||
* Enabled sets have priority over disabled sets
|
||||
* Both groups follow the user-defined order
|
||||
*/
|
||||
inline bool operator()(const CardSetPtr &a, const CardSetPtr &b) const
|
||||
{
|
||||
if (a->getEnabled()) {
|
||||
return !b->getEnabled() || a->getReleaseDate() < b->getReleaseDate();
|
||||
} else {
|
||||
return !b->getEnabled() && a->getReleaseDate() < b->getReleaseDate();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
class CardSetPriorityComparator
|
||||
{
|
||||
public:
|
||||
/*
|
||||
* Returns true if a has higher download priority than b
|
||||
* Enabled sets have priority over disabled sets
|
||||
* Both groups follow the user-defined order
|
||||
*/
|
||||
inline bool operator()(const PrintingInfo &a, const PrintingInfo &b) const
|
||||
{
|
||||
if (a.getSet()->getEnabled()) {
|
||||
return !b.getSet()->getEnabled() || a.getSet()->getSortKey() < b.getSet()->getSortKey();
|
||||
} else {
|
||||
return !b.getSet()->getEnabled() && a.getSet()->getSortKey() < b.getSet()->getSortKey();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
#endif // SET_PRIORITY_COMPARATOR_H
|
||||
@@ -0,0 +1,127 @@
|
||||
#include "card_set_list.h"
|
||||
|
||||
class CardSetList::KeyCompareFunctor
|
||||
{
|
||||
public:
|
||||
inline bool operator()(const CardSetPtr &a, const CardSetPtr &b) const
|
||||
{
|
||||
if (a.isNull() || b.isNull()) {
|
||||
// qCWarning(CardInfoLog) << "SetList::KeyCompareFunctor a or b is null";
|
||||
return false;
|
||||
}
|
||||
|
||||
return a->getSortKey() < b->getSortKey();
|
||||
}
|
||||
};
|
||||
|
||||
void CardSetList::sortByKey()
|
||||
{
|
||||
std::sort(begin(), end(), KeyCompareFunctor());
|
||||
}
|
||||
|
||||
int CardSetList::getEnabledSetsNum()
|
||||
{
|
||||
int num = 0;
|
||||
for (int i = 0; i < size(); ++i) {
|
||||
CardSetPtr set = at(i);
|
||||
if (set && set->getEnabled()) {
|
||||
++num;
|
||||
}
|
||||
}
|
||||
return num;
|
||||
}
|
||||
|
||||
int CardSetList::getUnknownSetsNum()
|
||||
{
|
||||
int num = 0;
|
||||
for (int i = 0; i < size(); ++i) {
|
||||
CardSetPtr set = at(i);
|
||||
if (set && !set->getIsKnown() && !set->getIsKnownIgnored()) {
|
||||
++num;
|
||||
}
|
||||
}
|
||||
return num;
|
||||
}
|
||||
|
||||
QStringList CardSetList::getUnknownSetsNames()
|
||||
{
|
||||
QStringList sets = QStringList();
|
||||
for (int i = 0; i < size(); ++i) {
|
||||
CardSetPtr set = at(i);
|
||||
if (set && !set->getIsKnown() && !set->getIsKnownIgnored()) {
|
||||
sets << set->getShortName();
|
||||
}
|
||||
}
|
||||
return sets;
|
||||
}
|
||||
|
||||
void CardSetList::enableAllUnknown()
|
||||
{
|
||||
for (int i = 0; i < size(); ++i) {
|
||||
CardSetPtr set = at(i);
|
||||
if (set && !set->getIsKnown() && !set->getIsKnownIgnored()) {
|
||||
set->setIsKnown(true);
|
||||
set->setEnabled(true);
|
||||
} else if (set && set->getIsKnownIgnored() && !set->getEnabled()) {
|
||||
set->setEnabled(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void CardSetList::enableAll()
|
||||
{
|
||||
for (int i = 0; i < size(); ++i) {
|
||||
CardSetPtr set = at(i);
|
||||
|
||||
if (set == nullptr) {
|
||||
// qCWarning(CardInfoLog) << "enabledAll has null";
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!set->getIsKnownIgnored()) {
|
||||
set->setIsKnown(true);
|
||||
}
|
||||
|
||||
set->setEnabled(true);
|
||||
}
|
||||
}
|
||||
|
||||
void CardSetList::markAllAsKnown()
|
||||
{
|
||||
for (int i = 0; i < size(); ++i) {
|
||||
CardSetPtr set = at(i);
|
||||
if (set && !set->getIsKnown() && !set->getIsKnownIgnored()) {
|
||||
set->setIsKnown(true);
|
||||
set->setEnabled(false);
|
||||
} else if (set && set->getIsKnownIgnored() && !set->getEnabled()) {
|
||||
set->setEnabled(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void CardSetList::guessSortKeys()
|
||||
{
|
||||
defaultSort();
|
||||
for (int i = 0; i < size(); ++i) {
|
||||
CardSetPtr set = at(i);
|
||||
if (set.isNull()) {
|
||||
// qCWarning(CardInfoLog) << "guessSortKeys set is null";
|
||||
continue;
|
||||
}
|
||||
set->setSortKey(i);
|
||||
}
|
||||
}
|
||||
|
||||
void CardSetList::defaultSort()
|
||||
{
|
||||
std::sort(begin(), end(), [](const CardSetPtr &a, const CardSetPtr &b) {
|
||||
// Sort by priority, then by release date, then by short name
|
||||
if (a->getPriority() != b->getPriority()) {
|
||||
return a->getPriority() < b->getPriority(); // lowest first
|
||||
} else if (a->getReleaseDate() != b->getReleaseDate()) {
|
||||
return a->getReleaseDate() > b->getReleaseDate(); // most recent first
|
||||
} else {
|
||||
return a->getShortName() < b->getShortName(); // alphabetically
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,102 @@
|
||||
#ifndef COCKATRICE_CARD_SET_LIST_H
|
||||
#define COCKATRICE_CARD_SET_LIST_H
|
||||
|
||||
#include "card_set.h"
|
||||
|
||||
#include <QList>
|
||||
|
||||
/**
|
||||
* @class CardSetList
|
||||
* @ingroup CardSets
|
||||
*
|
||||
* @brief A list-like container for CardSet objects with extended management methods.
|
||||
*
|
||||
* Extends `QList<CardSetPtr>` by adding convenience operations for sorting,
|
||||
* enabling/disabling sets, and tracking known/unknown status. Unlike a plain
|
||||
* list, this container provides domain-specific functionality for handling
|
||||
* groups of sets in bulk.
|
||||
*/
|
||||
class CardSetList : public QList<CardSetPtr>
|
||||
{
|
||||
private:
|
||||
/**
|
||||
* @class KeyCompareFunctor
|
||||
* @brief Internal comparison functor for sorting by sort key.
|
||||
*
|
||||
* Used internally by `sortByKey()` to order sets consistently
|
||||
* according to their assigned numeric sort keys.
|
||||
*/
|
||||
class KeyCompareFunctor;
|
||||
|
||||
public:
|
||||
/**
|
||||
* @brief Sorts the set list by each set’s assigned sort key.
|
||||
*
|
||||
* Uses KeyCompareFunctor internally. If two sets share the
|
||||
* same sort key, their relative order is unspecified.
|
||||
*/
|
||||
void sortByKey();
|
||||
|
||||
/**
|
||||
* @brief Reassigns sort keys based on the current order.
|
||||
*
|
||||
* Calls defaultSort() and then assigns sequential sort keys
|
||||
* to all sets according to their resulting positions, replacing
|
||||
* any existing sort keys to ensure consistent ordering.
|
||||
*/
|
||||
void guessSortKeys();
|
||||
|
||||
/**
|
||||
* @brief Enables all sets that are unknown or ignored.
|
||||
*
|
||||
* Sets that are not marked as known and not ignored are marked as known
|
||||
* and enabled. Ignored-known sets are also enabled, but remain ignored.
|
||||
*/
|
||||
void enableAllUnknown();
|
||||
|
||||
/**
|
||||
* @brief Enables all sets in the list.
|
||||
*
|
||||
* Equivalent to calling `setEnabled(true)` on each entry.
|
||||
*/
|
||||
void enableAll();
|
||||
|
||||
/**
|
||||
* @brief Marks all sets as known and adjusts their enabled state.
|
||||
*
|
||||
* Unknown, non-ignored sets become known and disabled.
|
||||
* Ignored-known sets are enabled if they were previously disabled.
|
||||
*/
|
||||
void markAllAsKnown();
|
||||
|
||||
/**
|
||||
* @brief Counts the number of sets that are currently enabled.
|
||||
*
|
||||
* @return Integer count of enabled sets.
|
||||
*/
|
||||
int getEnabledSetsNum();
|
||||
|
||||
/**
|
||||
* @brief Counts the number of sets that are currently unknown.
|
||||
*
|
||||
* @return Integer count of unknown sets.
|
||||
*/
|
||||
int getUnknownSetsNum();
|
||||
|
||||
/**
|
||||
* @brief Collects the short names of all sets marked as unknown.
|
||||
*
|
||||
* @return A list of unknown set names.
|
||||
*/
|
||||
QStringList getUnknownSetsNames();
|
||||
|
||||
/**
|
||||
* @brief Sorts the list by default rules.
|
||||
*
|
||||
* Orders sets first by priority (ascending), then by release date
|
||||
* (most recent first), and finally alphabetically by short name.
|
||||
*/
|
||||
void defaultSort();
|
||||
};
|
||||
|
||||
#endif // COCKATRICE_CARD_SET_LIST_H
|
||||
Reference in New Issue
Block a user