Style proxy.

Took 14 minutes

Took 6 minutes

Took 1 minute
This commit is contained in:
Lukas Brübach 2025-11-09 04:46:31 +01:00
parent 164c40234b
commit 8382bd4160
6 changed files with 133 additions and 73 deletions

View file

@ -150,6 +150,7 @@ set(cockatrice_SOURCES
src/interface/widgets/deck_editor/deck_editor_deck_dock_widget.cpp src/interface/widgets/deck_editor/deck_editor_deck_dock_widget.cpp
src/interface/widgets/deck_editor/deck_editor_filter_dock_widget.cpp src/interface/widgets/deck_editor/deck_editor_filter_dock_widget.cpp
src/interface/widgets/deck_editor/deck_editor_printing_selector_dock_widget.cpp src/interface/widgets/deck_editor/deck_editor_printing_selector_dock_widget.cpp
src/interface/widgets/deck_editor/deck_list_style_proxy.cpp
src/interface/widgets/general/background_sources.cpp src/interface/widgets/general/background_sources.cpp
src/interface/widgets/general/display/banner_widget.cpp src/interface/widgets/general/display/banner_widget.cpp
src/interface/widgets/general/display/bar_widget.cpp src/interface/widgets/general/display/bar_widget.cpp

View file

@ -2,6 +2,7 @@
#include "../../../client/settings/cache_settings.h" #include "../../../client/settings/cache_settings.h"
#include "../../deck_loader/deck_loader.h" #include "../../deck_loader/deck_loader.h"
#include "deck_list_style_proxy.h"
#include <QComboBox> #include <QComboBox>
#include <QDockWidget> #include <QDockWidget>
@ -30,9 +31,13 @@ void DeckEditorDeckDockWidget::createDeckDock()
deckModel = new DeckListModel(this); deckModel = new DeckListModel(this);
deckModel->setObjectName("deckModel"); deckModel->setObjectName("deckModel");
connect(deckModel, &DeckListModel::deckHashChanged, this, &DeckEditorDeckDockWidget::updateHash); connect(deckModel, &DeckListModel::deckHashChanged, this, &DeckEditorDeckDockWidget::updateHash);
DeckListStyleProxy *proxy = new DeckListStyleProxy(this);
proxy->setSourceModel(deckModel);
deckView = new QTreeView(); deckView = new QTreeView();
deckView->setObjectName("deckView"); deckView->setObjectName("deckView");
deckView->setModel(deckModel); deckView->setModel(proxy);
deckView->setUniformRowHeights(true); deckView->setUniformRowHeights(true);
deckView->setSortingEnabled(true); deckView->setSortingEnabled(true);
deckView->sortByColumn(1, Qt::AscendingOrder); deckView->sortByColumn(1, Qt::AscendingOrder);

View file

@ -0,0 +1,36 @@
#include "deck_list_style_proxy.h"
#include <QBrush>
#include <QColor>
#include <QFont>
#include <libcockatrice/models/deck_list/deck_list_model.h>
QVariant DeckListStyleProxy::data(const QModelIndex &index, int role) const
{
QVariant value = QIdentityProxyModel::data(index, role);
const bool isCard = QIdentityProxyModel::data(index, DeckRoles::IsCardRole).toBool();
if (role == Qt::FontRole && !isCard) {
QFont f;
f.setBold(true);
return f;
}
if (role == Qt::BackgroundRole) {
if (isCard) {
const bool legal = QIdentityProxyModel::data(index, DeckRoles::IsLegalRole).toBool();
int base = 255 - (index.row() % 2) * 30;
return legal ? QBrush(QColor(base, base, base)) : QBrush(QColor(255, base / 3, base / 3));
} else {
int depth = QIdentityProxyModel::data(index, DeckRoles::DepthRole).toInt();
int color = 90 + 60 * depth;
return QBrush(QColor(color, 255, color));
}
}
if (role == Qt::ForegroundRole)
return QBrush(QColor(0, 0, 0));
return value;
}

View file

@ -0,0 +1,15 @@
#ifndef COCKATRICE_DECK_LIST_STYLE_PROXY_H
#define COCKATRICE_DECK_LIST_STYLE_PROXY_H
#include <QIdentityProxyModel>
class DeckListStyleProxy : public QIdentityProxyModel
{
Q_OBJECT
public:
using QIdentityProxyModel::QIdentityProxyModel;
QVariant data(const QModelIndex &index, int role) const override;
};
#endif // COCKATRICE_DECK_LIST_STYLE_PROXY_H

View file

@ -88,92 +88,86 @@ int DeckListModel::columnCount(const QModelIndex & /*parent*/) const
QVariant DeckListModel::data(const QModelIndex &index, int role) const QVariant DeckListModel::data(const QModelIndex &index, int role) const
{ {
// debugIndexInfo("data", index); if (!index.isValid())
if (!index.isValid()) {
return {}; return {};
}
if (index.column() >= columnCount()) { if (index.column() >= columnCount())
return {}; return {};
}
auto *temp = static_cast<AbstractDecklistNode *>(index.internalPointer()); auto *node = static_cast<AbstractDecklistNode *>(index.internalPointer());
auto *card = dynamic_cast<DecklistModelCardNode *>(temp); auto *card = dynamic_cast<DecklistModelCardNode *>(node);
if (card == nullptr) {
const auto *node = dynamic_cast<InnerDecklistNode *>(temp); // Group node
if (!card) {
auto *group = static_cast<InnerDecklistNode *>(node);
switch (role) { switch (role) {
case Qt::FontRole: {
QFont f;
f.setBold(true);
return f;
}
case Qt::DisplayRole: case Qt::DisplayRole:
case Qt::EditRole: { case Qt::EditRole:
switch (index.column()) { switch (index.column()) {
case 0: case 0: return group->recursiveCount(true);
return node->recursiveCount(true); case 1: return (role == Qt::DisplayRole
case 1: { ? group->getVisibleName()
if (role == Qt::DisplayRole) : group->getName());
return node->getVisibleName(); case 2: return group->getCardSetShortName();
return node->getName(); case 3: return group->getCardCollectorNumber();
} case 4: return group->getCardProviderId();
case 2: { default: return {};
return node->getCardSetShortName();
}
case 3: {
return node->getCardCollectorNumber();
}
case 4: {
return node->getCardProviderId();
}
default:
return {};
} }
}
case Qt::UserRole + 1: case DeckRoles::IsCardRole:
return false; return false;
case Qt::BackgroundRole: {
int color = 90 + 60 * node->depth(); case DeckRoles::DepthRole:
return QBrush(QColor(color, 255, color)); return group->depth();
}
case Qt::ForegroundRole: { // legality does not apply to group nodes
return QBrush(QColor(0, 0, 0)); case DeckRoles::IsLegalRole:
}
default:
return {};
}
} else {
switch (role) {
case Qt::DisplayRole:
case Qt::EditRole: {
switch (index.column()) {
case 0:
return card->getNumber();
case 1:
return card->getName();
case 2:
return card->getCardSetShortName();
case 3:
return card->getCardCollectorNumber();
case 4:
return card->getCardProviderId();
default:
return {};
}
}
case Qt::UserRole + 1:
return true; return true;
case Qt::BackgroundRole: {
int color = 255 - (index.row() % 2) * 30;
return QBrush(QColor(color, color, color));
}
case Qt::ForegroundRole: {
return QBrush(QColor(0, 0, 0));
}
default: default:
return {}; return {};
} }
} }
// Card node
switch (role) {
case Qt::DisplayRole:
case Qt::EditRole:
switch (index.column()) {
case 0: return card->getNumber();
case 1: return card->getName();
case 2: return card->getCardSetShortName();
case 3: return card->getCardCollectorNumber();
case 4: return card->getCardProviderId();
default: return {};
}
case DeckRoles::IsCardRole:
return true;
case DeckRoles::DepthRole:
return card->depth();
default:
return {};
}
}
void DeckListModel::emitBackgroundUpdates(const QModelIndex &parent)
{
int rows = rowCount(parent);
if (rows == 0)
return;
QModelIndex topLeft = index(0, 0, parent);
QModelIndex bottomRight = index(rows - 1, columnCount() - 1, parent);
emit dataChanged(topLeft, bottomRight, {Qt::BackgroundRole});
for (int r = 0; r < rows; ++r) {
QModelIndex child = index(r, 0, parent);
emitBackgroundUpdates(child);
}
} }
QVariant DeckListModel::headerData(const int section, const Qt::Orientation orientation, const int role) const QVariant DeckListModel::headerData(const int section, const Qt::Orientation orientation, const int role) const

View file

@ -12,6 +12,14 @@ class CardDatabase;
class QPrinter; class QPrinter;
class QTextCursor; class QTextCursor;
namespace DeckRoles {
enum {
IsCardRole = Qt::UserRole + 1,
DepthRole,
IsLegalRole
};
}
/** /**
* @brief Specifies the criteria used to group cards in the DeckListModel. * @brief Specifies the criteria used to group cards in the DeckListModel.
*/ */
@ -163,6 +171,7 @@ public:
int rowCount(const QModelIndex &parent) const override; int rowCount(const QModelIndex &parent) const override;
int columnCount(const QModelIndex & /*parent*/ = QModelIndex()) const override; int columnCount(const QModelIndex & /*parent*/ = QModelIndex()) const override;
QVariant data(const QModelIndex &index, int role) const override; QVariant data(const QModelIndex &index, int role) const override;
void emitBackgroundUpdates(const QModelIndex &parent);
QVariant headerData(int section, Qt::Orientation orientation, int role) const override; QVariant headerData(int section, Qt::Orientation orientation, int role) const override;
QModelIndex index(int row, int column, const QModelIndex &parent) const override; QModelIndex index(int row, int column, const QModelIndex &parent) const override;
QModelIndex parent(const QModelIndex &index) const override; QModelIndex parent(const QModelIndex &index) const override;