Deck Editor owns DeckHistoryManager (#6359)

Took 4 minutes
This commit is contained in:
BruebachL
2025-11-27 23:11:43 +01:00
committed by GitHub
parent bac6beeb50
commit 122926c6cd
4 changed files with 64 additions and 33 deletions

View File

@@ -52,11 +52,6 @@ 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);
@@ -66,7 +61,7 @@ void DeckEditorDeckDockWidget::createDeckDock()
proxy = new DeckListStyleProxy(this);
proxy->setSourceModel(deckModel);
historyManagerWidget = new DeckListHistoryManagerWidget(deckModel, proxy, historyManager, this);
historyManagerWidget = new DeckListHistoryManagerWidget(deckModel, proxy, deckEditor->getHistoryManager(), this);
connect(historyManagerWidget, &DeckListHistoryManagerWidget::requestDisplayWidgetSync, this,
&DeckEditorDeckDockWidget::syncDisplayWidgetsToModel);
@@ -300,8 +295,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())));
emit requestDeckHistorySave(
QString(tr("Rename deck to \"%1\" from \"%2\"")).arg(name).arg(deckLoader->getDeckList()->getName()));
deckModel->getDeckList()->setName(name);
deckEditor->setModified(name.isEmpty());
emit nameChanged();
@@ -310,10 +305,9 @@ 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())));
emit requestDeckHistorySave(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());
@@ -370,7 +364,7 @@ void DeckEditorDeckDockWidget::updateBannerCardComboBox()
// Handle results
if (restoreIndex != -1) {
bannerCardComboBox->setCurrentIndex(restoreIndex);
setBannerCard(restoreIndex);
syncDeckListBannerCardWithComboBox();
} else {
// Add a placeholder "-" and set it as the current selection
bannerCardComboBox->insertItem(0, "-");
@@ -383,13 +377,18 @@ 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});
emit requestDeckHistorySave(tr("Banner card changed"));
syncDeckListBannerCardWithComboBox();
deckEditor->setModified(true);
emit deckModified();
}
void DeckEditorDeckDockWidget::syncDeckListBannerCardWithComboBox()
{
auto [name, id] = bannerCardComboBox->currentData().value<QPair<QString, QString>>();
deckModel->getDeckList()->setBannerCard({name, id});
}
void DeckEditorDeckDockWidget::updateShowBannerCardComboBox(const bool visible)
{
bannerCardLabel->setHidden(!visible);
@@ -427,7 +426,7 @@ void DeckEditorDeckDockWidget::setDeck(DeckLoader *_deck)
connect(deckLoader, &DeckLoader::deckLoaded, deckModel, &DeckListModel::rebuildTree);
connect(deckLoader->getDeckList(), &DeckList::deckHashChanged, deckModel, &DeckListModel::deckHashChanged);
historyManager->clear();
emit requestDeckHistoryClear();
historyManagerWidget->setDeckListModel(deckModel);
syncDisplayWidgetsToModel();
@@ -521,14 +520,6 @@ 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();
@@ -649,8 +640,7 @@ void DeckEditorDeckDockWidget::actRemoveCard()
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)));
emit requestDeckHistorySave(QString(tr("Removed \"%1\" (all copies)")).arg(cardName));
deckModel->removeRow(sourceIndex.row(), sourceIndex.parent());
isModified = true;
@@ -685,7 +675,7 @@ void DeckEditorDeckDockWidget::offsetCountAtIndex(const QModelIndex &idx, int of
.arg(cardName)
.arg(deckModel->data(sourceIndex.sibling(sourceIndex.row(), 4), Qt::DisplayRole).toString());
historyManager->save(deckLoader->getDeckList()->createMemento(reason));
emit requestDeckHistorySave(reason);
if (new_count <= 0) {
deckModel->removeRow(sourceIndex.row(), sourceIndex.parent());

View File

@@ -14,7 +14,6 @@
#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>
@@ -70,7 +69,6 @@ public slots:
void actDecrementSelection();
void actSwapCard();
void actRemoveCard();
void onCardAboutToBeAdded(const ExactCard &card, const QString &zoneName);
void offsetCountAtIndex(const QModelIndex &idx, int offset);
signals:
@@ -79,11 +77,12 @@ signals:
void hashChanged();
void deckChanged();
void deckModified();
void requestDeckHistorySave(const QString &modificationReason);
void requestDeckHistoryClear();
void cardChanged(const ExactCard &_card);
private:
AbstractTabDeckEditor *deckEditor;
DeckListHistoryManager *historyManager;
DeckListHistoryManagerWidget *historyManagerWidget;
KeySignals deckViewKeySignals;
QLabel *nameLabel;
@@ -113,6 +112,7 @@ private slots:
void updateName(const QString &name);
void updateComments();
void setBannerCard(int);
void syncDeckListBannerCardWithComboBox();
void updateHash();
void refreshShortcuts();
void updateShowBannerCardComboBox(bool visible);

View File

@@ -58,6 +58,8 @@ AbstractTabDeckEditor::AbstractTabDeckEditor(TabSupervisor *_tabSupervisor) : Ta
{
setDockOptions(QMainWindow::AnimatedDocks | QMainWindow::AllowNestedDocks | QMainWindow::AllowTabbedDocks);
historyManager = new DeckListHistoryManager(this);
databaseDisplayDockWidget = new DeckEditorDatabaseDisplayWidget(this);
deckDockWidget = new DeckEditorDeckDockWidget(this);
cardInfoDockWidget = new DeckEditorCardInfoDockWidget(this);
@@ -70,6 +72,10 @@ AbstractTabDeckEditor::AbstractTabDeckEditor(TabSupervisor *_tabSupervisor) : Ta
// Connect deck signals to this tab
connect(deckDockWidget, &DeckEditorDeckDockWidget::deckChanged, this, &AbstractTabDeckEditor::onDeckChanged);
connect(deckDockWidget, &DeckEditorDeckDockWidget::deckModified, this, &AbstractTabDeckEditor::onDeckModified);
connect(deckDockWidget, &DeckEditorDeckDockWidget::requestDeckHistorySave, this,
&AbstractTabDeckEditor::onDeckHistorySaveRequested);
connect(deckDockWidget, &DeckEditorDeckDockWidget::requestDeckHistoryClear, this,
&AbstractTabDeckEditor::onDeckHistoryClearRequested);
connect(deckDockWidget, &DeckEditorDeckDockWidget::cardChanged, this, &AbstractTabDeckEditor::updateCard);
connect(this, &AbstractTabDeckEditor::decrementCard, deckDockWidget, &DeckEditorDeckDockWidget::actDecrementCard);
@@ -107,6 +113,7 @@ void AbstractTabDeckEditor::updateCard(const ExactCard &card)
/** @brief Placeholder: called when the deck changes. */
void AbstractTabDeckEditor::onDeckChanged()
{
historyManager->clear();
}
/**
@@ -118,6 +125,22 @@ void AbstractTabDeckEditor::onDeckModified()
deckMenu->setSaveStatus(!isBlankNewDeck());
}
/**
* @brief Marks the tab as modified and updates the save menu status.
*/
void AbstractTabDeckEditor::onDeckHistorySaveRequested(const QString &modificationReason)
{
historyManager->save(deckDockWidget->getDeckList()->createMemento(modificationReason));
}
/**
* @brief Marks the tab as modified and updates the save menu status.
*/
void AbstractTabDeckEditor::onDeckHistoryClearRequested()
{
historyManager->clear();
}
/**
* @brief Helper for adding a card to a deck zone.
* @param card Card to add.
@@ -131,7 +154,9 @@ void AbstractTabDeckEditor::addCardHelper(const ExactCard &card, QString zoneNam
if (card.getInfo().getIsToken())
zoneName = DECK_ZONE_TOKENS;
emit cardAboutToBeAdded(card, zoneName);
onDeckHistorySaveRequested(QString(tr("Added (%1): %2 (%3) %4"))
.arg(zoneName, card.getName(), card.getPrinting().getSet()->getCorrectedShortName(),
card.getPrinting().getProperty("num")));
QModelIndex newCardIndex = deckDockWidget->deckModel->addCard(card, zoneName);
deckDockWidget->deckView->clearSelection();

View File

@@ -17,6 +17,8 @@
#include "../interface/widgets/visual_deck_storage/deck_preview/deck_preview_deck_tags_display_widget.h"
#include "tab.h"
#include <libcockatrice/deck_list/deck_list_history_manager.h>
class CardDatabaseModel;
class CardDatabaseDisplayModel;
@@ -132,6 +134,13 @@ public:
return deckDockWidget;
}
DeckListHistoryManager *getHistoryManager() const
{
return historyManager;
}
DeckListHistoryManager *historyManager;
// UI Elements
DeckEditorMenu *deckMenu; ///< Menu for deck operations
DeckEditorDatabaseDisplayWidget *databaseDisplayDockWidget; ///< Database dock
@@ -147,6 +156,14 @@ public slots:
/** @brief Called when the deck is modified. */
virtual void onDeckModified();
/** @brief Called when a widget is about to modify the state of the DeckList.
* @param modificationReason The reason for the state modification
*/
virtual void onDeckHistorySaveRequested(const QString &modificationReason);
/** @brief Called when a widget would like to clear the history. */
virtual void onDeckHistoryClearRequested();
/** @brief Updates the card info panel.
* @param card The card to display.
*/
@@ -180,7 +197,6 @@ public slots:
virtual void dockTopLevelChanged(bool topLevel) = 0;
signals:
void cardAboutToBeAdded(const ExactCard &addedCard, const QString &zoneName);
/** @brief Emitted when a deck should be opened in a new editor tab. */
void openDeckEditor(DeckLoader *deckLoader);