From a868d37438aba04290562964b4060e3035ba6aa0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lukas=20Br=C3=BCbach?= Date: Thu, 27 Feb 2025 09:52:45 +0100 Subject: [PATCH] DeckDock can handle its own menu. --- .../client/tabs/tab_generic_deck_editor.cpp | 32 ++++++------------- .../src/client/tabs/tab_generic_deck_editor.h | 2 -- .../deck_editor_deck_dock_widget.cpp | 31 +++++++++++++++--- .../deck_editor_deck_dock_widget.h | 4 ++- 4 files changed, 39 insertions(+), 30 deletions(-) diff --git a/cockatrice/src/client/tabs/tab_generic_deck_editor.cpp b/cockatrice/src/client/tabs/tab_generic_deck_editor.cpp index de8c338fb..2ef0a2d41 100644 --- a/cockatrice/src/client/tabs/tab_generic_deck_editor.cpp +++ b/cockatrice/src/client/tabs/tab_generic_deck_editor.cpp @@ -48,18 +48,6 @@ void TabGenericDeckEditor::updateCard(CardInfoPtr _card) printingSelectorDockWidget->printingSelector->setCard(_card, DECK_ZONE_MAIN); } -void TabGenericDeckEditor::decklistCustomMenu(QPoint point) -{ - QMenu menu; - const CardInfoPtr info = cardInfoDockWidget->cardInfo->getInfo(); - - QAction *selectPrinting = menu.addAction(tr("Select Printing")); - - connect(selectPrinting, &QAction::triggered, this, &TabGenericDeckEditor::showPrintingSelector); - - menu.exec(deckDockWidget->deckView->mapToGlobal(point)); -} - bool TabGenericDeckEditor::confirmClose() { if (modified) { @@ -225,7 +213,7 @@ void TabGenericDeckEditor::openDeckFromFile(const QString &fileName, DeckOpenLoc bool TabGenericDeckEditor::actSaveDeck() { - DeckLoader *const deck = deckDockWidget->deckModel->getDeckList(); + DeckLoader *const deck = getDeckList(); if (deck->getLastRemoteDeckId() != -1) { QString deckString = deck->writeToString_Native(); if (deckString.length() > MAX_FILE_LENGTH) { @@ -262,14 +250,14 @@ bool TabGenericDeckEditor::actSaveDeckAs() dialog.setAcceptMode(QFileDialog::AcceptSave); dialog.setDefaultSuffix("cod"); dialog.setNameFilters(DeckLoader::fileNameFilters); - dialog.selectFile(deckDockWidget->deckModel->getDeckList()->getName().trimmed() + ".cod"); + dialog.selectFile(getDeckList()->getName().trimmed() + ".cod"); if (!dialog.exec()) return false; QString fileName = dialog.selectedFiles().at(0); DeckLoader::FileFormat fmt = DeckLoader::getFormatFromName(fileName); - if (!deckDockWidget->deckModel->getDeckList()->saveToFile(fileName, fmt)) { + if (!getDeckList()->saveToFile(fileName, fmt)) { QMessageBox::critical( this, tr("Error"), tr("The deck could not be saved.\nPlease check that the directory is writable and try again.")); @@ -316,7 +304,7 @@ void TabGenericDeckEditor::actSaveDeckToClipboard() { QString buffer; QTextStream stream(&buffer); - deckDockWidget->deckModel->getDeckList()->saveToStream_Plain(stream); + getDeckList()->saveToStream_Plain(stream); QApplication::clipboard()->setText(buffer, QClipboard::Clipboard); QApplication::clipboard()->setText(buffer, QClipboard::Selection); } @@ -325,7 +313,7 @@ void TabGenericDeckEditor::actSaveDeckToClipboardNoSetNameAndNumber() { QString buffer; QTextStream stream(&buffer); - deckDockWidget->deckModel->getDeckList()->saveToStream_Plain(stream, true, false); + getDeckList()->saveToStream_Plain(stream, true, false); QApplication::clipboard()->setText(buffer, QClipboard::Clipboard); QApplication::clipboard()->setText(buffer, QClipboard::Selection); } @@ -334,7 +322,7 @@ void TabGenericDeckEditor::actSaveDeckToClipboardRaw() { QString buffer; QTextStream stream(&buffer); - deckDockWidget->deckModel->getDeckList()->saveToStream_Plain(stream, false); + getDeckList()->saveToStream_Plain(stream, false); QApplication::clipboard()->setText(buffer, QClipboard::Clipboard); QApplication::clipboard()->setText(buffer, QClipboard::Selection); } @@ -343,7 +331,7 @@ void TabGenericDeckEditor::actSaveDeckToClipboardRawNoSetNameAndNumber() { QString buffer; QTextStream stream(&buffer); - deckDockWidget->deckModel->getDeckList()->saveToStream_Plain(stream, false, false); + getDeckList()->saveToStream_Plain(stream, false, false); QApplication::clipboard()->setText(buffer, QClipboard::Clipboard); QApplication::clipboard()->setText(buffer, QClipboard::Selection); } @@ -359,7 +347,7 @@ void TabGenericDeckEditor::actPrintDeck() void TabGenericDeckEditor::actExportDeckDecklist() { // Get the decklist class for the deck. - DeckLoader *const deck = deckDockWidget->deckModel->getDeckList(); + DeckLoader *const deck = getDeckList(); // create a string to load the decklist url into. QString decklistUrlString; // check if deck is not null @@ -389,14 +377,14 @@ void TabGenericDeckEditor::actAnalyzeDeckDeckstats() { auto *interface = new DeckStatsInterface(*databaseDisplayDockWidget->databaseModel->getDatabase(), this); // it deletes itself when done - interface->analyzeDeck(deckDockWidget->deckModel->getDeckList()); + interface->analyzeDeck(getDeckList()); } void TabGenericDeckEditor::actAnalyzeDeckTappedout() { auto *interface = new TappedOutInterface(*databaseDisplayDockWidget->databaseModel->getDatabase(), this); // it deletes itself when done - interface->analyzeDeck(deckDockWidget->deckModel->getDeckList()); + interface->analyzeDeck(getDeckList()); } void TabGenericDeckEditor::addCardHelper(const CardInfoPtr info, QString zoneName) diff --git a/cockatrice/src/client/tabs/tab_generic_deck_editor.h b/cockatrice/src/client/tabs/tab_generic_deck_editor.h index 692635735..6010ed1fe 100644 --- a/cockatrice/src/client/tabs/tab_generic_deck_editor.h +++ b/cockatrice/src/client/tabs/tab_generic_deck_editor.h @@ -83,8 +83,6 @@ signals: void deckEditorClosing(TabGenericDeckEditor *tab); protected slots: - void decklistCustomMenu(QPoint point); - // Deck Operations virtual void actNewDeck(); void cleanDeckAndResetModified(); diff --git a/cockatrice/src/client/ui/widgets/deck_editor/deck_editor_deck_dock_widget.cpp b/cockatrice/src/client/ui/widgets/deck_editor/deck_editor_deck_dock_widget.cpp index c7a5ab908..c55e3b7b1 100644 --- a/cockatrice/src/client/ui/widgets/deck_editor/deck_editor_deck_dock_widget.cpp +++ b/cockatrice/src/client/ui/widgets/deck_editor/deck_editor_deck_dock_widget.cpp @@ -42,7 +42,7 @@ void DeckEditorDeckDockWidget::createDeckDock() connect(deckView->selectionModel(), &QItemSelectionModel::currentRowChanged, this, &DeckEditorDeckDockWidget::updateCard); connect(deckView, SIGNAL(doubleClicked(const QModelIndex &)), this, SLOT(actSwapCard())); - connect(deckView, SIGNAL(customContextMenuRequested(QPoint)), deckEditor, SLOT(decklistCustomMenu(QPoint))); + connect(deckView, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(decklistCustomMenu(QPoint))); connect(&deckViewKeySignals, SIGNAL(onShiftS()), this, SLOT(actSwapCard())); connect(&deckViewKeySignals, SIGNAL(onEnter()), this, SLOT(actIncrement())); connect(&deckViewKeySignals, SIGNAL(onCtrlAltEqual()), this, SLOT(actIncrement())); @@ -164,16 +164,17 @@ void DeckEditorDeckDockWidget::createDeckDock() retranslateUi(); } -void DeckEditorDeckDockWidget::updateCard(const QModelIndex ¤t, const QModelIndex & /*previous*/) +CardInfoPtr DeckEditorDeckDockWidget::getCurrentCard() { + QModelIndex current = deckView->selectionModel()->currentIndex(); if (!current.isValid()) - return; + return {}; const QString cardName = current.sibling(current.row(), 1).data().toString(); const QString cardProviderID = current.sibling(current.row(), 4).data().toString(); const QModelIndex gparent = current.parent().parent(); if (!gparent.isValid()) { - return; + return {}; } const QString zoneName = gparent.sibling(gparent.row(), 1).data(Qt::EditRole).toString(); @@ -183,9 +184,17 @@ void DeckEditorDeckDockWidget::updateCard(const QModelIndex ¤t, const QMod QString providerId = current.sibling(current.row(), 4).data().toString(); if (CardInfoPtr selectedCard = CardDatabaseManager::getInstance()->getCardByNameAndProviderId(cardName, providerId)) { - emit cardChanged(selectedCard); + return selectedCard; } } + return {}; +} + +void DeckEditorDeckDockWidget::updateCard(const QModelIndex /*¤t*/, const QModelIndex & /*previous*/) +{ + if (CardInfoPtr card = getCurrentCard()) { + emit cardChanged(card); + } } void DeckEditorDeckDockWidget::updateName(const QString &name) @@ -464,6 +473,18 @@ void DeckEditorDeckDockWidget::offsetCountAtIndex(const QModelIndex &idx, int of // setModified(true); } +void DeckEditorDeckDockWidget::decklistCustomMenu(QPoint point) +{ + QMenu menu; + const CardInfoPtr info = getCurrentCard(); + + QAction *selectPrinting = menu.addAction(tr("Select Printing")); + + connect(selectPrinting, &QAction::triggered, deckEditor, &TabGenericDeckEditor::showPrintingSelector); + + menu.exec(deckView->mapToGlobal(point)); +} + void DeckEditorDeckDockWidget::refreshShortcuts() { ShortcutsSettings &shortcuts = SettingsCache::instance().shortcuts(); diff --git a/cockatrice/src/client/ui/widgets/deck_editor/deck_editor_deck_dock_widget.h b/cockatrice/src/client/ui/widgets/deck_editor/deck_editor_deck_dock_widget.h index b6a88b4ee..9f73c26c1 100644 --- a/cockatrice/src/client/ui/widgets/deck_editor/deck_editor_deck_dock_widget.h +++ b/cockatrice/src/client/ui/widgets/deck_editor/deck_editor_deck_dock_widget.h @@ -24,6 +24,7 @@ public: QTreeView *deckView; QComboBox *bannerCardComboBox; void createDeckDock(); + CardInfoPtr getCurrentCard(); void retranslateUi(); QString getDeckName() { @@ -67,7 +68,8 @@ private: QModelIndexList getSelectedCardNodes() const; private slots: - void updateCard(const QModelIndex ¤t, const QModelIndex &previous); + void decklistCustomMenu(QPoint point); + void updateCard(QModelIndex, const QModelIndex ¤t); void updateName(const QString &name); void updateComments(); void setBannerCard(int);