[DeckEditor] Deck List History Manager. (#6340)

* [DeckEditor] Deck List History Manager.

Took 23 minutes

Took 17 minutes

* Add icons.

Took 2 minutes


Took 3 seconds

* Small fixes.

Took 12 minutes

* Style lint.

Took 48 seconds

* tr() things.

Took 5 minutes

* Add tooltips for buttons.

Took 3 minutes

* Add explanation label to history.

Took 3 minutes

* Refactor to .cpp, delegate undo/redo to manager, don't return memento

Took 8 minutes

* Clear history when setting deck.

Took 6 minutes

* Move to value based stacks.

Took 52 seconds

* Default constructor.

Took 31 seconds

Took 3 minutes

Took 4 minutes

Took 2 minutes

* Have it listen to deck editor additions.

Took 18 minutes

* Don't connect buttons *and* actions.

Took 2 minutes

---------

Co-authored-by: Lukas Brübach <Bruebach.Lukas@bdosecurity.de>
This commit is contained in:
BruebachL 2025-11-20 14:54:32 +01:00 committed by GitHub
parent c46f6d1178
commit 846f16ddaa
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
21 changed files with 612 additions and 41 deletions

View file

@ -28,6 +28,11 @@ DeckEditorDeckDockWidget::DeckEditorDeckDockWidget(AbstractTabDeckEditor *parent
void DeckEditorDeckDockWidget::createDeckDock()
{
historyManager = new DeckListHistoryManager();
connect(deckEditor, &AbstractTabDeckEditor::cardAboutToBeAdded, this,
&DeckEditorDeckDockWidget::onCardAboutToBeAdded);
deckModel = new DeckListModel(this);
deckModel->setObjectName("deckModel");
connect(deckModel, &DeckListModel::deckHashChanged, this, &DeckEditorDeckDockWidget::updateHash);
@ -37,6 +42,10 @@ void DeckEditorDeckDockWidget::createDeckDock()
proxy = new DeckListStyleProxy(this);
proxy->setSourceModel(deckModel);
historyManagerWidget = new DeckListHistoryManagerWidget(deckModel, proxy, historyManager, this);
connect(historyManagerWidget, &DeckListHistoryManagerWidget::requestDisplayWidgetSync, this,
&DeckEditorDeckDockWidget::syncDisplayWidgetsToModel);
deckView = new QTreeView();
deckView->setObjectName("deckView");
deckView->setModel(proxy);
@ -65,7 +74,15 @@ void DeckEditorDeckDockWidget::createDeckDock()
nameEdit->setMaxLength(MAX_NAME_LENGTH);
nameEdit->setObjectName("nameEdit");
nameLabel->setBuddy(nameEdit);
connect(nameEdit, &LineEditUnfocusable::textChanged, this, &DeckEditorDeckDockWidget::updateName);
nameDebounceTimer = new QTimer(this);
nameDebounceTimer->setSingleShot(true);
nameDebounceTimer->setInterval(300); // debounce duration in ms
connect(nameDebounceTimer, &QTimer::timeout, this, [this]() { updateName(nameEdit->text()); });
connect(nameEdit, &LineEditUnfocusable::textChanged, this, [this]() {
nameDebounceTimer->start(); // restart debounce timer
});
quickSettingsWidget = new SettingsButtonWidget(this);
@ -95,7 +112,16 @@ void DeckEditorDeckDockWidget::createDeckDock()
commentsEdit->setMinimumHeight(nameEdit->minimumSizeHint().height());
commentsEdit->setObjectName("commentsEdit");
commentsLabel->setBuddy(commentsEdit);
connect(commentsEdit, &QTextEdit::textChanged, this, &DeckEditorDeckDockWidget::updateComments);
commentsDebounceTimer = new QTimer(this);
commentsDebounceTimer->setSingleShot(true);
commentsDebounceTimer->setInterval(400); // longer debounce for multi-line
connect(commentsDebounceTimer, &QTimer::timeout, this, [this]() { updateComments(); });
connect(commentsEdit, &QTextEdit::textChanged, this, [this]() {
commentsDebounceTimer->start(); // restart debounce timer
});
bannerCardLabel = new QLabel();
bannerCardLabel->setObjectName("bannerCardLabel");
bannerCardLabel->setText(tr("Banner Card"));
@ -109,7 +135,7 @@ void DeckEditorDeckDockWidget::createDeckDock()
&DeckEditorDeckDockWidget::setBannerCard);
bannerCardComboBox->setHidden(!SettingsCache::instance().getDeckEditorBannerCardComboBoxVisible());
deckTagsDisplayWidget = new DeckPreviewDeckTagsDisplayWidget(this, deckLoader);
deckTagsDisplayWidget = new DeckPreviewDeckTagsDisplayWidget(this, deckModel->getDeckList());
deckTagsDisplayWidget->setHidden(!SettingsCache::instance().getDeckEditorTagsWidgetVisible());
activeGroupCriteriaLabel = new QLabel(this);
@ -187,7 +213,8 @@ void DeckEditorDeckDockWidget::createDeckDock()
lowerLayout->addWidget(tbDecrement, 0, 3);
lowerLayout->addWidget(tbRemoveCard, 0, 4);
lowerLayout->addWidget(tbSwapCard, 0, 5);
lowerLayout->addWidget(deckView, 1, 0, 1, 6);
lowerLayout->addWidget(historyManagerWidget, 0, 6);
lowerLayout->addWidget(deckView, 1, 0, 1, 7);
// Create widgets for both layouts to make splitter work correctly
auto *topWidget = new QWidget;
@ -249,6 +276,8 @@ void DeckEditorDeckDockWidget::updateCard(const QModelIndex /*&current*/, const
void DeckEditorDeckDockWidget::updateName(const QString &name)
{
historyManager->save(deckLoader->getDeckList()->createMemento(
QString(tr("Rename deck to \"%1\" from \"%2\"")).arg(name).arg(deckLoader->getDeckList()->getName())));
deckModel->getDeckList()->setName(name);
deckEditor->setModified(name.isEmpty());
emit nameChanged();
@ -257,6 +286,11 @@ void DeckEditorDeckDockWidget::updateName(const QString &name)
void DeckEditorDeckDockWidget::updateComments()
{
historyManager->save(
deckLoader->getDeckList()->createMemento(QString(tr("Updated comments (was %1 chars, now %2 chars)"))
.arg(deckLoader->getDeckList()->getComments().size())
.arg(commentsEdit->toPlainText().size())));
deckModel->getDeckList()->setComments(commentsEdit->toPlainText());
deckEditor->setModified(commentsEdit->toPlainText().isEmpty());
emit commentsChanged();
@ -329,6 +363,7 @@ void DeckEditorDeckDockWidget::updateBannerCardComboBox()
void DeckEditorDeckDockWidget::setBannerCard(int /* changedIndex */)
{
historyManager->save(deckLoader->getDeckList()->createMemento(tr("Banner card changed")));
auto [name, id] = bannerCardComboBox->currentData().value<QPair<QString, QString>>();
deckModel->getDeckList()->setBannerCard({name, id});
deckEditor->setModified(true);
@ -372,21 +407,40 @@ void DeckEditorDeckDockWidget::setDeck(DeckLoader *_deck)
connect(deckLoader, &DeckLoader::deckLoaded, deckModel, &DeckListModel::rebuildTree);
connect(deckLoader->getDeckList(), &DeckList::deckHashChanged, deckModel, &DeckListModel::deckHashChanged);
nameEdit->setText(deckModel->getDeckList()->getName());
commentsEdit->setText(deckModel->getDeckList()->getComments());
historyManager->clear();
historyManagerWidget->setDeckListModel(deckModel);
syncBannerCardComboBoxSelectionWithDeck();
updateBannerCardComboBox();
updateHash();
deckModel->sort(deckView->header()->sortIndicatorSection(), deckView->header()->sortIndicatorOrder());
deckView->expandAll();
deckView->expandAll();
deckTagsDisplayWidget->connectDeckList();
syncDisplayWidgetsToModel();
emit deckChanged();
}
void DeckEditorDeckDockWidget::syncDisplayWidgetsToModel()
{
nameEdit->blockSignals(true);
nameEdit->setText(deckModel->getDeckList()->getName());
nameEdit->blockSignals(false);
commentsEdit->blockSignals(true);
commentsEdit->setText(deckModel->getDeckList()->getComments());
commentsEdit->blockSignals(false);
bannerCardComboBox->blockSignals(true);
syncBannerCardComboBoxSelectionWithDeck();
updateBannerCardComboBox();
bannerCardComboBox->blockSignals(false);
updateHash();
sortDeckModelToDeckView();
expandAll();
deckTagsDisplayWidget->connectDeckList(deckModel->getDeckList());
}
void DeckEditorDeckDockWidget::sortDeckModelToDeckView()
{
deckModel->sort(deckView->header()->sortIndicatorSection(), deckView->header()->sortIndicatorOrder());
}
DeckLoader *DeckEditorDeckDockWidget::getDeckLoader()
{
return deckLoader;
@ -412,7 +466,7 @@ void DeckEditorDeckDockWidget::cleanDeck()
emit deckModified();
emit deckChanged();
updateBannerCardComboBox();
deckTagsDisplayWidget->connectDeckList();
deckTagsDisplayWidget->connectDeckList(deckModel->getDeckList());
}
void DeckEditorDeckDockWidget::recursiveExpand(const QModelIndex &index)
@ -422,6 +476,12 @@ void DeckEditorDeckDockWidget::recursiveExpand(const QModelIndex &index)
deckView->expand(index);
}
void DeckEditorDeckDockWidget::expandAll()
{
deckView->expandAll();
deckView->expandAll();
}
/**
* Gets the index of all the currently selected card nodes in the decklist table.
* The list is in reverse order of the visual selection, so that rows can be deleted while iterating over them.
@ -441,6 +501,14 @@ QModelIndexList DeckEditorDeckDockWidget::getSelectedCardNodes() const
return selectedRows;
}
void DeckEditorDeckDockWidget::onCardAboutToBeAdded(const ExactCard &addedCard, const QString &zoneName)
{
historyManager->save(deckLoader->getDeckList()->createMemento(
QString(tr("Added (%1): %2 (%3) %4"))
.arg(zoneName, addedCard.getName(), addedCard.getPrinting().getSet()->getCorrectedShortName(),
addedCard.getPrinting().getProperty("num"))));
}
void DeckEditorDeckDockWidget::actIncrement()
{
auto selectedRows = getSelectedCardNodes();
@ -559,6 +627,11 @@ void DeckEditorDeckDockWidget::actRemoveCard()
continue;
}
QModelIndex sourceIndex = proxy->mapToSource(index);
QString cardName = sourceIndex.sibling(sourceIndex.row(), 1).data().toString();
historyManager->save(
deckLoader->getDeckList()->createMemento(QString(tr("Removed \"%1\" (all copies)")).arg(cardName)));
deckModel->removeRow(sourceIndex.row(), sourceIndex.parent());
isModified = true;
}
@ -579,9 +652,21 @@ void DeckEditorDeckDockWidget::offsetCountAtIndex(const QModelIndex &idx, int of
QModelIndex sourceIndex = proxy->mapToSource(idx);
const QModelIndex numberIndex = sourceIndex.sibling(sourceIndex.row(), 0);
const QModelIndex nameIndex = sourceIndex.sibling(sourceIndex.row(), 1);
const QString cardName = deckModel->data(nameIndex, Qt::EditRole).toString();
const int count = deckModel->data(numberIndex, Qt::EditRole).toInt();
const int new_count = count + offset;
const auto reason =
QString(tr("%1 %2 × \"%3\" (%4)"))
.arg(offset > 0 ? tr("Added") : tr("Removed"))
.arg(qAbs(offset))
.arg(cardName)
.arg(deckModel->data(sourceIndex.sibling(sourceIndex.row(), 4), Qt::DisplayRole).toString());
historyManager->save(deckLoader->getDeckList()->createMemento(reason));
if (new_count <= 0) {
deckModel->removeRow(sourceIndex.row(), sourceIndex.parent());
} else {

View file

@ -12,7 +12,9 @@
#include "../../key_signals.h"
#include "../utility/custom_line_edit.h"
#include "../visual_deck_storage/deck_preview/deck_preview_deck_tags_display_widget.h"
#include "deck_list_history_manager_widget.h"
#include "deck_list_style_proxy.h"
#include "libcockatrice/deck_list/deck_list_history_manager.h"
#include <QComboBox>
#include <QDockWidget>
@ -53,6 +55,8 @@ public slots:
void cleanDeck();
void updateBannerCardComboBox();
void setDeck(DeckLoader *_deck);
void syncDisplayWidgetsToModel();
void sortDeckModelToDeckView();
DeckLoader *getDeckLoader();
DeckList *getDeckList();
void actIncrement();
@ -61,6 +65,7 @@ public slots:
void actDecrementSelection();
void actSwapCard();
void actRemoveCard();
void onCardAboutToBeAdded(const ExactCard &card, const QString &zoneName);
void offsetCountAtIndex(const QModelIndex &idx, int offset);
signals:
@ -73,14 +78,18 @@ signals:
private:
AbstractTabDeckEditor *deckEditor;
DeckListHistoryManager *historyManager;
DeckListHistoryManagerWidget *historyManagerWidget;
KeySignals deckViewKeySignals;
QLabel *nameLabel;
LineEditUnfocusable *nameEdit;
QTimer *nameDebounceTimer;
SettingsButtonWidget *quickSettingsWidget;
QCheckBox *showBannerCardCheckBox;
QCheckBox *showTagsWidgetCheckBox;
QLabel *commentsLabel;
QTextEdit *commentsEdit;
QTimer *commentsDebounceTimer;
QLabel *bannerCardLabel;
DeckPreviewDeckTagsDisplayWidget *deckTagsDisplayWidget;
QLabel *hashLabel1;
@ -104,6 +113,7 @@ private slots:
void updateShowBannerCardComboBox(bool visible);
void updateShowTagsWidget(bool visible);
void syncBannerCardComboBoxSelectionWithDeck();
void expandAll();
};
#endif // DECK_EDITOR_DECK_DOCK_WIDGET_H

View file

@ -0,0 +1,160 @@
#include "deck_list_history_manager_widget.h"
DeckListHistoryManagerWidget::DeckListHistoryManagerWidget(DeckListModel *_deckListModel,
DeckListStyleProxy *_styleProxy,
DeckListHistoryManager *manager,
QWidget *parent)
: QWidget(parent), deckListModel(_deckListModel), styleProxy(_styleProxy), historyManager(manager)
{
layout = new QHBoxLayout(this);
aUndo = new QAction(QString(), this);
aUndo->setIcon(QPixmap("theme:icons/arrow_undo"));
aUndo->setShortcut(QKeySequence::Undo);
aUndo->setShortcutContext(Qt::ApplicationShortcut);
connect(aUndo, &QAction::triggered, this, &DeckListHistoryManagerWidget::doUndo);
undoButton = new QToolButton(this);
undoButton->setDefaultAction(aUndo);
aRedo = new QAction(QString(), this);
aRedo->setIcon(QPixmap("theme:icons/arrow_redo"));
aRedo->setShortcut(QKeySequence::Redo);
aRedo->setShortcutContext(Qt::ApplicationShortcut);
connect(aRedo, &QAction::triggered, this, &DeckListHistoryManagerWidget::doRedo);
redoButton = new QToolButton(this);
redoButton->setDefaultAction(aRedo);
layout->addWidget(undoButton);
layout->addWidget(redoButton);
historyButton = new SettingsButtonWidget(this);
historyButton->setButtonIcon(QPixmap("theme:icons/arrow_history"));
historyLabel = new QLabel(this);
historyList = new QListWidget(this);
historyButton->addSettingsWidget(historyLabel);
historyButton->addSettingsWidget(historyList);
layout->addWidget(historyButton);
connect(historyList, &QListWidget::itemClicked, this, &DeckListHistoryManagerWidget::onListClicked);
connect(historyManager, &DeckListHistoryManager::undoRedoStateChanged, this,
&DeckListHistoryManagerWidget::refreshList);
refreshList();
retranslateUi();
}
void DeckListHistoryManagerWidget::retranslateUi()
{
undoButton->setToolTip(tr("Undo"));
redoButton->setToolTip(tr("Redo"));
historyButton->setToolTip(tr("Undo/Redo history"));
historyLabel->setText(tr("Click on an entry to revert to that point in the history."));
}
void DeckListHistoryManagerWidget::setDeckListModel(DeckListModel *_deckListModel)
{
deckListModel = _deckListModel;
}
void DeckListHistoryManagerWidget::refreshList()
{
historyList->clear();
// Fill redo section first (oldest redo at top, newest redo closest to divider)
const auto redoStack = historyManager->getRedoStack();
for (int i = 0; i < redoStack.size(); ++i) { // iterate forward
auto item = new QListWidgetItem(tr("[redo] ") + redoStack[i].getReason(), historyList);
item->setData(Qt::UserRole, QVariant("redo"));
item->setData(Qt::UserRole + 1, i); // index in redo stack
item->setForeground(Qt::gray);
historyList->addItem(item);
}
// Divider
if (!historyManager->getUndoStack().isEmpty() && !historyManager->getRedoStack().isEmpty()) {
auto divider = new QListWidgetItem("──────────", historyList);
divider->setFlags(Qt::NoItemFlags); // not selectable
historyList->addItem(divider);
}
// Fill undo section
const auto undoStack = historyManager->getUndoStack();
for (int i = undoStack.size() - 1; i >= 0; --i) {
auto item = new QListWidgetItem(tr("[undo] ") + undoStack[i].getReason(), historyList);
item->setData(Qt::UserRole, QVariant("undo"));
item->setData(Qt::UserRole + 1, i); // index in undo stack
historyList->addItem(item);
}
// Button enabled states
undoButton->setEnabled(historyManager->canUndo());
redoButton->setEnabled(historyManager->canRedo());
}
void DeckListHistoryManagerWidget::doUndo()
{
if (!historyManager->canUndo()) {
return;
}
historyManager->undo(deckListModel->getDeckList());
deckListModel->rebuildTree();
emit deckListModel->layoutChanged();
emit requestDisplayWidgetSync();
refreshList();
}
void DeckListHistoryManagerWidget::doRedo()
{
if (!historyManager->canRedo()) {
return;
}
historyManager->redo(deckListModel->getDeckList());
deckListModel->rebuildTree();
emit deckListModel->layoutChanged();
emit requestDisplayWidgetSync();
refreshList();
}
void DeckListHistoryManagerWidget::onListClicked(QListWidgetItem *item)
{
// Ignore non-selectable items (like divider)
if (!(item->flags() & Qt::ItemIsSelectable)) {
return;
}
const QString mode = item->data(Qt::UserRole).toString();
int index = item->data(Qt::UserRole + 1).toInt();
if (mode == "redo") {
const auto redoStack = historyManager->getRedoStack();
int steps = redoStack.size() - index;
for (int i = 0; i < steps; ++i) {
historyManager->redo(deckListModel->getDeckList());
}
} else if (mode == "undo") {
const auto undoStack = historyManager->getUndoStack();
int steps = undoStack.size() - 1 - index;
for (int i = 0; i < steps + 1; ++i) {
historyManager->undo(deckListModel->getDeckList());
}
}
deckListModel->rebuildTree();
emit deckListModel->layoutChanged();
emit requestDisplayWidgetSync();
refreshList();
}

View file

@ -0,0 +1,58 @@
#ifndef COCKATRICE_DECK_EDITOR_DECK_LIST_HISTORY_MANAGER_WIDGET_H
#define COCKATRICE_DECK_EDITOR_DECK_LIST_HISTORY_MANAGER_WIDGET_H
#ifndef COCKATRICE_DECK_UNDO_WIDGET_H
#define COCKATRICE_DECK_UNDO_WIDGET_H
#include "../quick_settings/settings_button_widget.h"
#include "deck_list_style_proxy.h"
#include <QAction>
#include <QHBoxLayout>
#include <QListWidget>
#include <QToolButton>
#include <QWidget>
#include <libcockatrice/deck_list/deck_list_history_manager.h>
#include <libcockatrice/models/deck_list/deck_list_model.h>
class DeckListHistoryManagerWidget : public QWidget
{
Q_OBJECT
signals:
void requestDisplayWidgetSync();
public slots:
void retranslateUi();
public:
explicit DeckListHistoryManagerWidget(DeckListModel *deckListModel,
DeckListStyleProxy *styleProxy,
DeckListHistoryManager *manager,
QWidget *parent = nullptr);
void setDeckListModel(DeckListModel *_deckListModel);
private slots:
void refreshList();
void onListClicked(QListWidgetItem *item);
void doUndo();
void doRedo();
private:
DeckListModel *deckListModel;
DeckListStyleProxy *styleProxy;
DeckListHistoryManager *historyManager;
QHBoxLayout *layout;
QAction *aUndo;
QToolButton *undoButton;
QAction *aRedo;
QToolButton *redoButton;
SettingsButtonWidget *historyButton;
QLabel *historyLabel;
QListWidget *historyList;
};
#endif // COCKATRICE_DECK_UNDO_WIDGET_H
#endif // COCKATRICE_DECK_EDITOR_DECK_LIST_HISTORY_MANAGER_WIDGET_H

View file

@ -7,9 +7,13 @@
QVariant DeckListStyleProxy::data(const QModelIndex &index, int role) const
{
QModelIndex src = mapToSource(index);
if (!src.isValid())
return {};
QVariant value = QIdentityProxyModel::data(index, role);
const bool isCard = QIdentityProxyModel::data(index, DeckRoles::IsCardRole).toBool();
bool isCard = src.data(DeckRoles::IsCardRole).toBool();
if (role == Qt::FontRole && !isCard) {
QFont f;
@ -24,7 +28,7 @@ QVariant DeckListStyleProxy::data(const QModelIndex &index, int role) const
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 depth = src.data(DeckRoles::DepthRole).toInt();
int color = 90 + 60 * depth;
return QBrush(QColor(color, 255, color));
}