mirror of
https://github.com/Cockatrice/Cockatrice.git
synced 2026-07-20 01:12:15 -07:00
Move the sorting options to their own widget.
This commit is contained in:
parent
2a7befaa9d
commit
060e8f0cfd
5 changed files with 203 additions and 169 deletions
|
|
@ -101,6 +101,7 @@ set(cockatrice_SOURCES
|
|||
src/client/ui/widgets/printing_selector/printing_selector_card_display_widget.cpp
|
||||
src/client/ui/widgets/printing_selector/printing_selector_card_overlay_widget.cpp
|
||||
src/client/ui/widgets/printing_selector/printing_selector_card_selection_widget.cpp
|
||||
src/client/ui/widgets/printing_selector/printing_selector_card_sorting_widget.cpp
|
||||
src/client/ui/widgets/printing_selector/set_name_and_collectors_number_display_widget.cpp
|
||||
src/client/network/release_channel.cpp
|
||||
src/server/remote/remote_client.cpp
|
||||
|
|
|
|||
|
|
@ -1,24 +1,16 @@
|
|||
#include "printing_selector.h"
|
||||
|
||||
#include "../../../../settings/cache_settings.h"
|
||||
#include "../../../../utility/card_set_comparator.h"
|
||||
#include "printing_selector_card_display_widget.h"
|
||||
#include "printing_selector_card_selection_widget.h"
|
||||
#include "printing_selector_card_sorting_widget.h"
|
||||
|
||||
#include <QComboBox>
|
||||
#include <QDebug>
|
||||
#include <QHBoxLayout>
|
||||
#include <QScrollBar>
|
||||
|
||||
const QString PrintingSelector::SORT_OPTIONS_ALPHABETICAL = tr("Alphabetical");
|
||||
const QString PrintingSelector::SORT_OPTIONS_PREFERENCE = tr("Preference");
|
||||
const QString PrintingSelector::SORT_OPTIONS_RELEASE_DATE = tr("Release Date");
|
||||
const QString PrintingSelector::SORT_OPTIONS_CONTAINED_IN_DECK = tr("Contained in Deck");
|
||||
const QString PrintingSelector::SORT_OPTIONS_POTENTIAL_CARDS = tr("Potential Cards in Deck");
|
||||
|
||||
const QStringList PrintingSelector::SORT_OPTIONS = {SORT_OPTIONS_ALPHABETICAL, SORT_OPTIONS_PREFERENCE,
|
||||
SORT_OPTIONS_RELEASE_DATE, SORT_OPTIONS_CONTAINED_IN_DECK,
|
||||
SORT_OPTIONS_POTENTIAL_CARDS};
|
||||
PrintingSelector::PrintingSelector(QWidget *parent,
|
||||
TabDeckEditor *deckEditor,
|
||||
DeckListModel *deckModel,
|
||||
|
|
@ -30,21 +22,8 @@ PrintingSelector::PrintingSelector(QWidget *parent,
|
|||
setLayout(layout);
|
||||
timer = new QTimer(this);
|
||||
|
||||
sortToolBar = new QHBoxLayout(this);
|
||||
|
||||
sortOptionsSelector = new QComboBox(this);
|
||||
sortOptionsSelector->addItems(SORT_OPTIONS);
|
||||
sortOptionsSelector->setCurrentIndex(2);
|
||||
connect(sortOptionsSelector, &QComboBox::currentTextChanged, this, &PrintingSelector::updateDisplay);
|
||||
sortToolBar->addWidget(sortOptionsSelector);
|
||||
|
||||
toggleSortOrder = new QPushButton(this);
|
||||
toggleSortOrder->setText(tr("Descending"));
|
||||
descendingSort = true;
|
||||
connect(toggleSortOrder, &QPushButton::clicked, this, &PrintingSelector::updateSortOrder);
|
||||
sortToolBar->addWidget(toggleSortOrder);
|
||||
|
||||
layout->addLayout(sortToolBar);
|
||||
sortToolBar = new PrintingSelectorCardSortingWidget(this);
|
||||
layout->addWidget(sortToolBar);
|
||||
|
||||
// Add the search bar
|
||||
searchBar = new QLineEdit(this);
|
||||
|
|
@ -74,17 +53,6 @@ PrintingSelector::PrintingSelector(QWidget *parent,
|
|||
});
|
||||
}
|
||||
|
||||
void PrintingSelector::updateSortOrder()
|
||||
{
|
||||
if (descendingSort) {
|
||||
toggleSortOrder->setText(tr("Ascending"));
|
||||
} else {
|
||||
toggleSortOrder->setText(tr("Descending"));
|
||||
}
|
||||
descendingSort = !descendingSort;
|
||||
updateDisplay();
|
||||
}
|
||||
|
||||
void PrintingSelector::updateDisplay()
|
||||
{
|
||||
timer->stop();
|
||||
|
|
@ -168,131 +136,19 @@ CardInfoPerSet PrintingSelector::getSetForUUID(const QString &uuid)
|
|||
return CardInfoPerSet();
|
||||
}
|
||||
|
||||
QList<CardInfoPerSet> PrintingSelector::prependPrintingsInDeck(const QList<CardInfoPerSet> &sets)
|
||||
{
|
||||
if (!selectedCard) {
|
||||
return {};
|
||||
}
|
||||
|
||||
CardInfoPerSetMap cardInfoPerSets = selectedCard->getSets();
|
||||
QList<QPair<CardInfoPerSet, int>> countList;
|
||||
|
||||
// Collect sets with their counts
|
||||
for (const auto &x : cardInfoPerSets) {
|
||||
for (const auto &cardInfoPerSet : x) {
|
||||
QModelIndex find_card =
|
||||
deckModel->findCard(selectedCard->getName(), DECK_ZONE_MAIN, cardInfoPerSet.getProperty("uuid"));
|
||||
if (find_card.isValid()) {
|
||||
int count =
|
||||
deckModel->data(find_card, Qt::DisplayRole).toInt(); // Ensure the count is treated as an integer
|
||||
if (count > 0) {
|
||||
countList.append(qMakePair(cardInfoPerSet, count));
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Sort sets by count in descending numerical order
|
||||
std::sort(countList.begin(), countList.end(),
|
||||
[](const QPair<CardInfoPerSet, int> &a, const QPair<CardInfoPerSet, int> &b) {
|
||||
return a.second > b.second; // Ensure numerical comparison
|
||||
});
|
||||
|
||||
// Create a copy of the original list to modify
|
||||
QList<CardInfoPerSet> result = sets;
|
||||
|
||||
// Prepend sorted sets and remove them from the original list
|
||||
for (const auto &pair : countList) {
|
||||
auto it = std::find_if(result.begin(), result.end(), [&pair](const CardInfoPerSet &item) {
|
||||
return item.getProperty("uuid") == pair.first.getProperty("uuid");
|
||||
});
|
||||
if (it != result.end()) {
|
||||
result.erase(it); // Remove the matching entry
|
||||
}
|
||||
result.prepend(pair.first); // Prepend the sorted item
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
QList<CardInfoPerSet> PrintingSelector::sortSets()
|
||||
{
|
||||
if (selectedCard.isNull()) {
|
||||
return {};
|
||||
}
|
||||
CardInfoPerSetMap cardInfoPerSets = selectedCard->getSets();
|
||||
|
||||
QList<CardSetPtr> sortedSets;
|
||||
|
||||
for (const auto &x : cardInfoPerSets) {
|
||||
for (const auto &set : x) {
|
||||
sortedSets << set.getPtr();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (sortedSets.empty()) {
|
||||
sortedSets << CardSet::newInstance("", "", "", QDate());
|
||||
}
|
||||
|
||||
if (sortOptionsSelector->currentText() == SORT_OPTIONS_PREFERENCE) {
|
||||
std::sort(sortedSets.begin(), sortedSets.end(), SetPriorityComparator());
|
||||
std::reverse(sortedSets.begin(), sortedSets.end());
|
||||
} else if (sortOptionsSelector->currentText() == SORT_OPTIONS_RELEASE_DATE) {
|
||||
std::sort(sortedSets.begin(), sortedSets.end(), SetReleaseDateComparator());
|
||||
}
|
||||
|
||||
QList<CardInfoPerSet> sortedCardInfoPerSets;
|
||||
// Reconstruct sorted list of CardInfoPerSet
|
||||
for (const auto &set : sortedSets) {
|
||||
for (auto it = cardInfoPerSets.begin(); it != cardInfoPerSets.end(); ++it) {
|
||||
for (const auto &x : it.value()) {
|
||||
if (x.getPtr() == set) {
|
||||
sortedCardInfoPerSets << it.value();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (descendingSort) {
|
||||
std::reverse(sortedCardInfoPerSets.begin(), sortedCardInfoPerSets.end());
|
||||
}
|
||||
|
||||
return sortedCardInfoPerSets;
|
||||
}
|
||||
|
||||
QList<CardInfoPerSet> PrintingSelector::filterSets(const QList<CardInfoPerSet> &sets) const
|
||||
{
|
||||
const QString searchText = searchBar->text().trimmed().toLower();
|
||||
|
||||
if (searchText.isEmpty()) {
|
||||
return sets;
|
||||
}
|
||||
|
||||
QList<CardInfoPerSet> filteredSets;
|
||||
|
||||
for (const auto &set : sets) {
|
||||
const QString longName = set.getPtr()->getLongName().toLower();
|
||||
const QString shortName = set.getPtr()->getShortName().toLower();
|
||||
|
||||
if (longName.contains(searchText) || shortName.contains(searchText)) {
|
||||
filteredSets << set;
|
||||
}
|
||||
}
|
||||
|
||||
return filteredSets;
|
||||
}
|
||||
|
||||
void PrintingSelector::getAllSetsForCurrentCard()
|
||||
{
|
||||
const QList<CardInfoPerSet> sortedSets = sortSets();
|
||||
const QList<CardInfoPerSet> filteredSets = filterSets(sortedSets);
|
||||
if (selectedCard.isNull()) {
|
||||
return;
|
||||
}
|
||||
|
||||
CardInfoPerSetMap cardInfoPerSets = selectedCard->getSets();
|
||||
const QList<CardInfoPerSet> sortedSets = sortToolBar->sortSets(cardInfoPerSets);
|
||||
const QList<CardInfoPerSet> filteredSets = sortToolBar->filterSets(sortedSets, searchBar->text().trimmed().toLower());
|
||||
QList<CardInfoPerSet> setsToUse;
|
||||
|
||||
if (SettingsCache::instance().getBumpSetsWithCardsInDeckToTop()) {
|
||||
setsToUse = prependPrintingsInDeck(filteredSets);
|
||||
setsToUse = sortToolBar->prependPrintingsInDeck(filteredSets, selectedCard, deckModel);
|
||||
} else {
|
||||
setsToUse = filteredSets;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -17,6 +17,7 @@
|
|||
#define BATCH_SIZE 10
|
||||
|
||||
class PrintingSelectorCardSelectionWidget;
|
||||
class PrintingSelectorCardSortingWidget;
|
||||
class TabDeckEditor;
|
||||
class PrintingSelector : public QWidget
|
||||
{
|
||||
|
|
@ -26,29 +27,16 @@ public:
|
|||
PrintingSelector(QWidget *parent, TabDeckEditor *deckEditor, DeckListModel *deckModel, QTreeView *deckView);
|
||||
void setCard(const CardInfoPtr &newCard, const QString &_currentZone);
|
||||
CardInfoPerSet getSetForUUID(const QString &uuid);
|
||||
QList<CardInfoPerSet> prependPrintingsInDeck(const QList<CardInfoPerSet> &sets);
|
||||
QList<CardInfoPerSet> sortSets();
|
||||
QList<CardInfoPerSet> filterSets(const QList<CardInfoPerSet> &sets) const;
|
||||
void getAllSetsForCurrentCard();
|
||||
|
||||
public slots:
|
||||
void updateDisplay();
|
||||
void selectPreviousCard();
|
||||
void selectNextCard();
|
||||
void updateSortOrder();
|
||||
|
||||
private:
|
||||
QVBoxLayout *layout;
|
||||
QHBoxLayout *sortToolBar;
|
||||
static const QString SORT_OPTIONS_ALPHABETICAL;
|
||||
static const QString SORT_OPTIONS_PREFERENCE;
|
||||
static const QString SORT_OPTIONS_RELEASE_DATE;
|
||||
static const QString SORT_OPTIONS_CONTAINED_IN_DECK;
|
||||
static const QString SORT_OPTIONS_POTENTIAL_CARDS;
|
||||
static const QStringList SORT_OPTIONS;
|
||||
QComboBox *sortOptionsSelector;
|
||||
bool descendingSort;
|
||||
QPushButton *toggleSortOrder;
|
||||
PrintingSelectorCardSortingWidget *sortToolBar;
|
||||
QLineEdit *searchBar;
|
||||
QTimer *searchDebounceTimer;
|
||||
FlowWidget *flowWidget;
|
||||
|
|
|
|||
|
|
@ -0,0 +1,154 @@
|
|||
#include "printing_selector_card_sorting_widget.h"
|
||||
|
||||
#include "../../../../utility/card_set_comparator.h"
|
||||
|
||||
const QString PrintingSelectorCardSortingWidget::SORT_OPTIONS_ALPHABETICAL = tr("Alphabetical");
|
||||
const QString PrintingSelectorCardSortingWidget::SORT_OPTIONS_PREFERENCE = tr("Preference");
|
||||
const QString PrintingSelectorCardSortingWidget::SORT_OPTIONS_RELEASE_DATE = tr("Release Date");
|
||||
const QString PrintingSelectorCardSortingWidget::SORT_OPTIONS_CONTAINED_IN_DECK = tr("Contained in Deck");
|
||||
const QString PrintingSelectorCardSortingWidget::SORT_OPTIONS_POTENTIAL_CARDS = tr("Potential Cards in Deck");
|
||||
|
||||
const QStringList PrintingSelectorCardSortingWidget::SORT_OPTIONS = {
|
||||
SORT_OPTIONS_ALPHABETICAL, SORT_OPTIONS_PREFERENCE, SORT_OPTIONS_RELEASE_DATE, SORT_OPTIONS_CONTAINED_IN_DECK,
|
||||
SORT_OPTIONS_POTENTIAL_CARDS};
|
||||
|
||||
PrintingSelectorCardSortingWidget::PrintingSelectorCardSortingWidget(PrintingSelector *parent) : parent(parent)
|
||||
{
|
||||
sortToolBar = new QHBoxLayout(this);
|
||||
|
||||
sortOptionsSelector = new QComboBox(this);
|
||||
sortOptionsSelector->addItems(SORT_OPTIONS);
|
||||
sortOptionsSelector->setCurrentIndex(2);
|
||||
connect(sortOptionsSelector, &QComboBox::currentTextChanged, parent, &PrintingSelector::updateDisplay);
|
||||
sortToolBar->addWidget(sortOptionsSelector);
|
||||
|
||||
toggleSortOrder = new QPushButton(this);
|
||||
toggleSortOrder->setText(tr("Descending"));
|
||||
descendingSort = true;
|
||||
connect(toggleSortOrder, &QPushButton::clicked, this, &PrintingSelectorCardSortingWidget::updateSortOrder);
|
||||
sortToolBar->addWidget(toggleSortOrder);
|
||||
}
|
||||
|
||||
void PrintingSelectorCardSortingWidget::updateSortOrder()
|
||||
{
|
||||
if (descendingSort) {
|
||||
toggleSortOrder->setText(tr("Ascending"));
|
||||
} else {
|
||||
toggleSortOrder->setText(tr("Descending"));
|
||||
}
|
||||
descendingSort = !descendingSort;
|
||||
parent->updateDisplay();
|
||||
}
|
||||
|
||||
QList<CardInfoPerSet> PrintingSelectorCardSortingWidget::sortSets(CardInfoPerSetMap cardInfoPerSets)
|
||||
{
|
||||
QList<CardSetPtr> sortedSets;
|
||||
|
||||
for (const auto &x : cardInfoPerSets) {
|
||||
for (const auto &set : x) {
|
||||
sortedSets << set.getPtr();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (sortedSets.empty()) {
|
||||
sortedSets << CardSet::newInstance("", "", "", QDate());
|
||||
}
|
||||
|
||||
if (sortOptionsSelector->currentText() == SORT_OPTIONS_PREFERENCE) {
|
||||
std::sort(sortedSets.begin(), sortedSets.end(), SetPriorityComparator());
|
||||
std::reverse(sortedSets.begin(), sortedSets.end());
|
||||
} else if (sortOptionsSelector->currentText() == SORT_OPTIONS_RELEASE_DATE) {
|
||||
std::sort(sortedSets.begin(), sortedSets.end(), SetReleaseDateComparator());
|
||||
}
|
||||
|
||||
QList<CardInfoPerSet> sortedCardInfoPerSets;
|
||||
// Reconstruct sorted list of CardInfoPerSet
|
||||
for (const auto &set : sortedSets) {
|
||||
for (auto it = cardInfoPerSets.begin(); it != cardInfoPerSets.end(); ++it) {
|
||||
for (const auto &x : it.value()) {
|
||||
if (x.getPtr() == set) {
|
||||
sortedCardInfoPerSets << it.value();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (descendingSort) {
|
||||
std::reverse(sortedCardInfoPerSets.begin(), sortedCardInfoPerSets.end());
|
||||
}
|
||||
|
||||
return sortedCardInfoPerSets;
|
||||
}
|
||||
|
||||
QList<CardInfoPerSet> PrintingSelectorCardSortingWidget::filterSets(const QList<CardInfoPerSet> &sets,
|
||||
const QString searchText) const
|
||||
{
|
||||
if (searchText.isEmpty()) {
|
||||
return sets;
|
||||
}
|
||||
|
||||
QList<CardInfoPerSet> filteredSets;
|
||||
|
||||
for (const auto &set : sets) {
|
||||
const QString longName = set.getPtr()->getLongName().toLower();
|
||||
const QString shortName = set.getPtr()->getShortName().toLower();
|
||||
|
||||
if (longName.contains(searchText) || shortName.contains(searchText)) {
|
||||
filteredSets << set;
|
||||
}
|
||||
}
|
||||
|
||||
return filteredSets;
|
||||
}
|
||||
|
||||
QList<CardInfoPerSet> PrintingSelectorCardSortingWidget::prependPrintingsInDeck(const QList<CardInfoPerSet> &sets,
|
||||
CardInfoPtr selectedCard,
|
||||
DeckListModel *deckModel)
|
||||
{
|
||||
if (!selectedCard) {
|
||||
return {};
|
||||
}
|
||||
|
||||
CardInfoPerSetMap cardInfoPerSets = selectedCard->getSets();
|
||||
QList<QPair<CardInfoPerSet, int>> countList;
|
||||
|
||||
// Collect sets with their counts
|
||||
for (const auto &x : cardInfoPerSets) {
|
||||
for (const auto &cardInfoPerSet : x) {
|
||||
QModelIndex find_card =
|
||||
deckModel->findCard(selectedCard->getName(), DECK_ZONE_MAIN, cardInfoPerSet.getProperty("uuid"));
|
||||
if (find_card.isValid()) {
|
||||
int count =
|
||||
deckModel->data(find_card, Qt::DisplayRole).toInt(); // Ensure the count is treated as an integer
|
||||
if (count > 0) {
|
||||
countList.append(qMakePair(cardInfoPerSet, count));
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Sort sets by count in descending numerical order
|
||||
std::sort(countList.begin(), countList.end(),
|
||||
[](const QPair<CardInfoPerSet, int> &a, const QPair<CardInfoPerSet, int> &b) {
|
||||
return a.second > b.second; // Ensure numerical comparison
|
||||
});
|
||||
|
||||
// Create a copy of the original list to modify
|
||||
QList<CardInfoPerSet> result = sets;
|
||||
|
||||
// Prepend sorted sets and remove them from the original list
|
||||
for (const auto &pair : countList) {
|
||||
auto it = std::find_if(result.begin(), result.end(), [&pair](const CardInfoPerSet &item) {
|
||||
return item.getProperty("uuid") == pair.first.getProperty("uuid");
|
||||
});
|
||||
if (it != result.end()) {
|
||||
result.erase(it); // Remove the matching entry
|
||||
}
|
||||
result.prepend(pair.first); // Prepend the sorted item
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
|
@ -0,0 +1,35 @@
|
|||
#ifndef PRINTING_SELECTOR_CARD_SORTING_WIDGET_H
|
||||
#define PRINTING_SELECTOR_CARD_SORTING_WIDGET_H
|
||||
|
||||
#include "printing_selector.h"
|
||||
|
||||
#include <QWidget>
|
||||
|
||||
class PrintingSelectorCardSortingWidget : public QWidget
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
explicit PrintingSelectorCardSortingWidget(PrintingSelector *parent);
|
||||
QList<CardInfoPerSet> sortSets(CardInfoPerSetMap cardInfoPerSets);
|
||||
QList<CardInfoPerSet> filterSets(const QList<CardInfoPerSet> &sets, QString searchText) const;
|
||||
QList<CardInfoPerSet>
|
||||
prependPrintingsInDeck(const QList<CardInfoPerSet> &sets, CardInfoPtr selectedCard, DeckListModel *deckModel);
|
||||
|
||||
public slots:
|
||||
void updateSortOrder();
|
||||
|
||||
private:
|
||||
PrintingSelector *parent;
|
||||
QHBoxLayout *sortToolBar;
|
||||
static const QString SORT_OPTIONS_ALPHABETICAL;
|
||||
static const QString SORT_OPTIONS_PREFERENCE;
|
||||
static const QString SORT_OPTIONS_RELEASE_DATE;
|
||||
static const QString SORT_OPTIONS_CONTAINED_IN_DECK;
|
||||
static const QString SORT_OPTIONS_POTENTIAL_CARDS;
|
||||
static const QStringList SORT_OPTIONS;
|
||||
QComboBox *sortOptionsSelector;
|
||||
bool descendingSort;
|
||||
QPushButton *toggleSortOrder;
|
||||
};
|
||||
|
||||
#endif // PRINTING_SELECTOR_CARD_SORTING_WIDGET_H
|
||||
Loading…
Add table
Add a link
Reference in a new issue