mirror of
https://github.com/Cockatrice/Cockatrice.git
synced 2026-11-05 01:23:24 -08:00
Some checks are pending
CodeQL / Analyze (cpp) (push) Waiting to run
CodeQL / Analyze (actions) (push) Waiting to run
Build Desktop / Configure (push) Waiting to run
Build Desktop / Debian 13 (push) Blocked by required conditions
Build Desktop / Debian 12 (push) Blocked by required conditions
Build Desktop / Fedora 44 (push) Blocked by required conditions
Build Desktop / Fedora 43 (push) Blocked by required conditions
Build Desktop / Servatrice_Debian 12 (push) Blocked by required conditions
Build Desktop / Ubuntu 24.04 (push) Blocked by required conditions
Build Desktop / Ubuntu 26.04 (push) Blocked by required conditions
Build Desktop / Arch (push) Blocked by required conditions
Build Desktop / Ubuntu 26.04 cover (push) Blocked by required conditions
Build Desktop / macOS 13 Intel (push) Blocked by required conditions
Build Desktop / macOS 14 (push) Blocked by required conditions
Build Desktop / macOS 15 (push) Blocked by required conditions
Build Desktop / macOS 26 Debug (push) Blocked by required conditions
Build Desktop / Windows 10 (push) Blocked by required conditions
Build Desktop / Prune caches (push) Waiting to run
Build Docker / Servatrice (arm) (push) Waiting to run
Build Docker / Servatrice (x86) (push) Waiting to run
Build Docker / Publish multi-platform Servatrice image (push) Blocked by required conditions
* [Models] Use QCollator for natural, cached sorting QString::localeAwareCompare rebuilds its collation data for every comparison, and it compares numbers as text. Keep one QCollator per model, enable numeric mode, and use it for the card name, set and user name sorts. Add a test that sorts "Forest 1", "Forest 2", "Forest 10" through DeckListSortFilterProxyModel and checks the natural order. * [Models] Sort naturally under the C locale too Numeric mode only takes effect through a real locale: under the C locale, which headless CI processes run in, QCollator silently falls back to string order and sorts "Forest 10" before "Forest 2". That made collator_sort_test fail on every Linux leg while passing locally. Add makeNaturalSortCollator(), which enables numeric mode and pins English only when the process locale is the degenerate one, and build the models' collators through it. Users with a real locale keep their own collation. * [Models] Fix compile of natural sorting (QStringView mismatch) * nit: initialize collators in member initializer lists * nit: namespace makeNaturalSortCollator under NaturalSort --------- Co-authored-by: Lukas Brübach <Bruebach.Lukas@bdosecurity.de>
106 lines
3.1 KiB
C++
106 lines
3.1 KiB
C++
/**
|
|
* @file card_database_display_model.h
|
|
* @ingroup CardDatabaseModels
|
|
* @brief The CardDatabaseDisplayModel is a QSortFilterProxyModel that allows applying filters and sorting to a
|
|
* CardDatabaseModel.
|
|
*/
|
|
|
|
#ifndef COCKATRICE_CARD_DATABASE_DISPLAY_MODEL_H
|
|
#define COCKATRICE_CARD_DATABASE_DISPLAY_MODEL_H
|
|
|
|
#include "libcockatrice/card/card_info.h"
|
|
|
|
#include <QCollator>
|
|
#include <QModelIndex>
|
|
#include <QSet>
|
|
#include <QSortFilterProxyModel>
|
|
#include <QString>
|
|
#include <QStringView>
|
|
#include <QTimer>
|
|
#include <libcockatrice/card/card_localization.h>
|
|
#include <libcockatrice/filters/filter_string.h>
|
|
#include <qtmetamacros.h>
|
|
|
|
class FilterTree;
|
|
class QObject;
|
|
template <class Key, class T> class QMap;
|
|
|
|
class CardDatabaseDisplayModel : public QSortFilterProxyModel
|
|
{
|
|
Q_OBJECT
|
|
public:
|
|
enum FilterBool
|
|
{
|
|
ShowTrue,
|
|
ShowFalse,
|
|
ShowAll
|
|
};
|
|
|
|
private:
|
|
FilterBool isToken;
|
|
QString cardName, cardText;
|
|
QSet<QString> cardNameSet, cardTypes, cardColors;
|
|
FilterTree *filterTree;
|
|
FilterString *filterString;
|
|
int loadedRowCount;
|
|
QTimer dirtyTimer;
|
|
CardSearchLanguage searchLanguage;
|
|
QString searchText;
|
|
|
|
/** The translation table that will be used for sanitizeCardName. */
|
|
static QMap<wchar_t, wchar_t> characterTranslation;
|
|
|
|
/** Natural, locale-aware comparison shared by every sort in this model. */
|
|
QCollator collator;
|
|
|
|
public:
|
|
explicit CardDatabaseDisplayModel(QObject *parent = nullptr);
|
|
void setSourceModel(QAbstractItemModel *model) override;
|
|
void setFilterTree(FilterTree *_filterTree);
|
|
void setIsToken(FilterBool _isToken)
|
|
{
|
|
isToken = _isToken;
|
|
dirty();
|
|
}
|
|
|
|
void setCardName(const QString &_cardName)
|
|
{
|
|
if (filterString != nullptr) {
|
|
delete filterString;
|
|
filterString = nullptr;
|
|
}
|
|
cardName = sanitizeCardName(_cardName, characterTranslation);
|
|
dirty();
|
|
}
|
|
void setStringFilter(const QString &_src);
|
|
void setCardNameSet(const QSet<QString> &_cardNameSet)
|
|
{
|
|
cardNameSet = _cardNameSet;
|
|
dirty();
|
|
}
|
|
void setSearchLanguage(const CardSearchLanguage &searchLang);
|
|
|
|
void dirty()
|
|
{
|
|
dirtyTimer.start(20);
|
|
}
|
|
void clearFilterAll();
|
|
[[nodiscard]] int rowCount(const QModelIndex &parent = QModelIndex()) const override;
|
|
[[nodiscard]] bool canFetchMore(const QModelIndex &parent) const override;
|
|
void fetchMore(const QModelIndex &parent) override;
|
|
signals:
|
|
void modelDirty();
|
|
|
|
protected:
|
|
[[nodiscard]] bool lessThan(const QModelIndex &left, const QModelIndex &right) const override;
|
|
int lessThanNumerically(QStringView left, QStringView right) const;
|
|
[[nodiscard]] bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const override;
|
|
[[nodiscard]] bool rowMatchesCardName(CardInfoPtr info) const;
|
|
|
|
private slots:
|
|
void filterTreeChanged();
|
|
/** Will translate all undesirable characters in DIRTYNAME according to the TABLE. */
|
|
const QString sanitizeCardName(const QString &dirtyName, const QMap<wchar_t, wchar_t> &table);
|
|
};
|
|
|
|
#endif // COCKATRICE_CARD_DATABASE_DISPLAY_MODEL_H
|