Get dialogParent dynamically

Took 8 minutes

Took 17 seconds
This commit is contained in:
Lukas Brübach 2026-05-27 00:00:23 +02:00
parent 359a8ca880
commit 0cd705753e
3 changed files with 32 additions and 27 deletions

View file

@ -9,8 +9,8 @@
#include <QInputDialog> #include <QInputDialog>
#include <libcockatrice/card/relation/card_relation.h> #include <libcockatrice/card/relation/card_relation.h>
PlayerDialogs::PlayerDialogs(PlayerGraphicsItem *_player, QWidget *_dialogParent, PlayerActions *_playerActions) PlayerDialogs::PlayerDialogs(PlayerGraphicsItem *_player, PlayerActions *_playerActions)
: QObject(_player), player(_player), dialogParent(_dialogParent), playerActions(_playerActions) : QObject(_player), player(_player), playerActions(_playerActions)
{ {
connect(playerActions, &PlayerActions::requestViewTopCardsDialog, this, connect(playerActions, &PlayerActions::requestViewTopCardsDialog, this,
&PlayerDialogs::onViewTopCardsDialogRequested); &PlayerDialogs::onViewTopCardsDialogRequested);
@ -62,7 +62,7 @@ PlayerDialogs::PlayerDialogs(PlayerGraphicsItem *_player, QWidget *_dialogParent
void PlayerDialogs::onViewTopCardsDialogRequested(int defaultNumberTopCards, int deckSize) void PlayerDialogs::onViewTopCardsDialogRequested(int defaultNumberTopCards, int deckSize)
{ {
bool ok; bool ok;
int number = QInputDialog::getInt(dialogParent, tr("View top cards of library"), int number = QInputDialog::getInt(dialogParent(), tr("View top cards of library"),
tr("Number of cards: (max. %1)").arg(deckSize), defaultNumberTopCards, 1, tr("Number of cards: (max. %1)").arg(deckSize), defaultNumberTopCards, 1,
deckSize, 1, &ok); deckSize, 1, &ok);
if (ok) { if (ok) {
@ -73,7 +73,7 @@ void PlayerDialogs::onViewTopCardsDialogRequested(int defaultNumberTopCards, int
void PlayerDialogs::onViewBottomCardsDialogRequested(int defaultNumberBottomCards, int deckSize) void PlayerDialogs::onViewBottomCardsDialogRequested(int defaultNumberBottomCards, int deckSize)
{ {
bool ok; bool ok;
int number = QInputDialog::getInt(dialogParent, tr("View bottom cards of library"), int number = QInputDialog::getInt(dialogParent(), tr("View bottom cards of library"),
tr("Number of cards: (max. %1)").arg(deckSize), defaultNumberBottomCards, 1, tr("Number of cards: (max. %1)").arg(deckSize), defaultNumberBottomCards, 1,
deckSize, 1, &ok); deckSize, 1, &ok);
if (ok) { if (ok) {
@ -84,7 +84,7 @@ void PlayerDialogs::onViewBottomCardsDialogRequested(int defaultNumberBottomCard
void PlayerDialogs::onShuffleTopDialogRequested(int defaultNumberTopCards, int maxCards) void PlayerDialogs::onShuffleTopDialogRequested(int defaultNumberTopCards, int maxCards)
{ {
bool ok; bool ok;
int number = QInputDialog::getInt(dialogParent, tr("Shuffle top cards of library"), int number = QInputDialog::getInt(dialogParent(), tr("Shuffle top cards of library"),
tr("Number of cards: (max. %1)").arg(maxCards), defaultNumberTopCards, 1, tr("Number of cards: (max. %1)").arg(maxCards), defaultNumberTopCards, 1,
maxCards, 1, &ok); maxCards, 1, &ok);
if (ok) { if (ok) {
@ -95,7 +95,7 @@ void PlayerDialogs::onShuffleTopDialogRequested(int defaultNumberTopCards, int m
void PlayerDialogs::onShuffleBottomDialogRequested(int defaultNumberBottomCards, int maxCards) void PlayerDialogs::onShuffleBottomDialogRequested(int defaultNumberBottomCards, int maxCards)
{ {
bool ok; bool ok;
int number = QInputDialog::getInt(dialogParent, tr("Shuffle bottom cards of library"), int number = QInputDialog::getInt(dialogParent(), tr("Shuffle bottom cards of library"),
tr("Number of cards: (max. %1)").arg(maxCards), defaultNumberBottomCards, 1, tr("Number of cards: (max. %1)").arg(maxCards), defaultNumberBottomCards, 1,
maxCards, 1, &ok); maxCards, 1, &ok);
if (ok) { if (ok) {
@ -106,7 +106,7 @@ void PlayerDialogs::onShuffleBottomDialogRequested(int defaultNumberBottomCards,
void PlayerDialogs::onMulliganDialogRequested(int startSize, int handSize, int deckSize) void PlayerDialogs::onMulliganDialogRequested(int startSize, int handSize, int deckSize)
{ {
bool ok; bool ok;
int number = QInputDialog::getInt(dialogParent, tr("Draw hand"), int number = QInputDialog::getInt(dialogParent(), tr("Draw hand"),
tr("Number of cards: (max. %1)").arg(deckSize) + '\n' + tr("Number of cards: (max. %1)").arg(deckSize) + '\n' +
tr("0 and lower are in comparison to current hand size"), tr("0 and lower are in comparison to current hand size"),
startSize, -handSize, deckSize, 1, &ok); startSize, -handSize, deckSize, 1, &ok);
@ -119,7 +119,7 @@ void PlayerDialogs::onMulliganDialogRequested(int startSize, int handSize, int d
void PlayerDialogs::onDrawCardsDialogRequested(int defaultNumberTopCards, int deckSize) void PlayerDialogs::onDrawCardsDialogRequested(int defaultNumberTopCards, int deckSize)
{ {
bool ok; bool ok;
int number = QInputDialog::getInt(dialogParent, tr("Draw cards"), tr("Number of cards: (max. %1)").arg(deckSize), int number = QInputDialog::getInt(dialogParent(), tr("Draw cards"), tr("Number of cards: (max. %1)").arg(deckSize),
defaultNumberTopCards, 1, deckSize, 1, &ok); defaultNumberTopCards, 1, deckSize, 1, &ok);
if (ok) { if (ok) {
@ -134,7 +134,7 @@ void PlayerDialogs::onMoveTopCardsToDialogRequested(int defaultNumberTopCards,
bool faceDown) bool faceDown)
{ {
bool ok; bool ok;
int number = QInputDialog::getInt(dialogParent, tr("Move top cards to %1").arg(zoneDisplayName), int number = QInputDialog::getInt(dialogParent(), tr("Move top cards to %1").arg(zoneDisplayName),
tr("Number of cards: (max. %1)").arg(maxCards), defaultNumberTopCards, 1, tr("Number of cards: (max. %1)").arg(maxCards), defaultNumberTopCards, 1,
maxCards, 1, &ok); maxCards, 1, &ok);
if (ok) { if (ok) {
@ -144,7 +144,7 @@ void PlayerDialogs::onMoveTopCardsToDialogRequested(int defaultNumberTopCards,
void PlayerDialogs::onMoveTopCardsUntilDialogRequested(MoveTopCardsUntilOptions options) void PlayerDialogs::onMoveTopCardsUntilDialogRequested(MoveTopCardsUntilOptions options)
{ {
DlgMoveTopCardsUntil dlg(dialogParent, options); DlgMoveTopCardsUntil dlg(dialogParent(), options);
if (!dlg.exec()) { if (!dlg.exec()) {
return; return;
} }
@ -158,7 +158,7 @@ void PlayerDialogs::onMoveBottomCardsToDialogRequested(int defaultNumberBottomCa
bool faceDown) bool faceDown)
{ {
bool ok; bool ok;
int number = QInputDialog::getInt(dialogParent, tr("Move bottom cards to %1").arg(zoneDisplayName), int number = QInputDialog::getInt(dialogParent(), tr("Move bottom cards to %1").arg(zoneDisplayName),
tr("Number of cards: (max. %1)").arg(maxCards), defaultNumberBottomCards, 1, tr("Number of cards: (max. %1)").arg(maxCards), defaultNumberBottomCards, 1,
maxCards, 1, &ok); maxCards, 1, &ok);
if (ok) { if (ok) {
@ -170,7 +170,7 @@ void PlayerDialogs::onDrawBottomCardsDialogRequested(int defaultNumberBottomCard
{ {
bool ok; bool ok;
int number = int number =
QInputDialog::getInt(dialogParent, tr("Draw bottom cards"), tr("Number of cards: (max. %1)").arg(maxCards), QInputDialog::getInt(dialogParent(), tr("Draw bottom cards"), tr("Number of cards: (max. %1)").arg(maxCards),
defaultNumberBottomCards, 1, maxCards, 1, &ok); defaultNumberBottomCards, 1, maxCards, 1, &ok);
if (ok) { if (ok) {
playerActions->actDrawBottomCards(number); playerActions->actDrawBottomCards(number);
@ -179,7 +179,7 @@ void PlayerDialogs::onDrawBottomCardsDialogRequested(int defaultNumberBottomCard
void PlayerDialogs::onRollDieDialogRequested() void PlayerDialogs::onRollDieDialogRequested()
{ {
DlgRollDice dlg(dialogParent); DlgRollDice dlg(dialogParent());
if (!dlg.exec()) { if (!dlg.exec()) {
return; return;
} }
@ -200,7 +200,7 @@ void PlayerDialogs::onCreateRelatedFromRelationDialogRequested(const CardItem *s
emit requestDialogSemaphore(true); emit requestDialogSemaphore(true);
variableCount = QInputDialog::getInt(dialogParent, tr("Create tokens"), tr("Number:"), variableCount = QInputDialog::getInt(dialogParent(), tr("Create tokens"), tr("Number:"),
cardRelation->getDefaultCount(), 1, MAX_TOKENS_PER_DIALOG, 1, &ok); cardRelation->getDefaultCount(), 1, MAX_TOKENS_PER_DIALOG, 1, &ok);
emit requestDialogSemaphore(false); emit requestDialogSemaphore(false);
@ -217,7 +217,7 @@ void PlayerDialogs::onCreateRelatedFromRelationDialogRequested(const CardItem *s
void PlayerDialogs::onCreateTokenDialogRequested(const QStringList &predefinedTokens) void PlayerDialogs::onCreateTokenDialogRequested(const QStringList &predefinedTokens)
{ {
DlgCreateToken dlg(predefinedTokens, dialogParent); DlgCreateToken dlg(predefinedTokens, dialogParent());
if (!dlg.exec()) { if (!dlg.exec()) {
return; return;
} }
@ -229,7 +229,7 @@ void PlayerDialogs::onMoveCardXCardsFromTopDialogRequested(int defaultNumberTopC
{ {
bool ok; bool ok;
int number = int number =
QInputDialog::getInt(dialogParent, tr("Place card X cards from top of library"), QInputDialog::getInt(dialogParent(), tr("Place card X cards from top of library"),
tr("Which position should this card be placed:") + "\n" + tr("(max. %1)").arg(deckSize), tr("Which position should this card be placed:") + "\n" + tr("(max. %1)").arg(deckSize),
defaultNumberTopCardsToPlaceBelow, 1, deckSize, 1, &ok); defaultNumberTopCardsToPlaceBelow, 1, deckSize, 1, &ok);
number -= 1; // indexes start at 0 number -= 1; // indexes start at 0
@ -243,7 +243,7 @@ void PlayerDialogs::onSetPTDialogRequested(const QString &oldPT)
{ {
bool ok; bool ok;
emit requestDialogSemaphore(true); emit requestDialogSemaphore(true);
QString pt = getTextWithMax(dialogParent, tr("Change power/toughness"), tr("Change stats to:"), QLineEdit::Normal, QString pt = getTextWithMax(dialogParent(), tr("Change power/toughness"), tr("Change stats to:"), QLineEdit::Normal,
oldPT, &ok); oldPT, &ok);
emit requestDialogSemaphore(false); emit requestDialogSemaphore(false);
@ -257,7 +257,7 @@ void PlayerDialogs::onSetPTDialogRequested(const QString &oldPT)
void PlayerDialogs::onSetAnnotationDialogRequested(const QString &oldAnnotation) void PlayerDialogs::onSetAnnotationDialogRequested(const QString &oldAnnotation)
{ {
emit requestDialogSemaphore(true); emit requestDialogSemaphore(true);
AnnotationDialog *dialog = new AnnotationDialog(dialogParent); AnnotationDialog *dialog = new AnnotationDialog(dialogParent());
dialog->setOptions(QInputDialog::UsePlainTextEditForTextInput); dialog->setOptions(QInputDialog::UsePlainTextEditForTextInput);
dialog->setWindowTitle(tr("Set annotation")); dialog->setWindowTitle(tr("Set annotation"));
dialog->setLabelText(tr("Please enter the new annotation:")); dialog->setLabelText(tr("Please enter the new annotation:"));
@ -278,7 +278,7 @@ void PlayerDialogs::onSetCardCounterDialogRequested(int counterId, const QString
auto &cardCounterSettings = SettingsCache::instance().cardCounters(); auto &cardCounterSettings = SettingsCache::instance().cardCounters();
QString counterName = cardCounterSettings.displayName(counterId); QString counterName = cardCounterSettings.displayName(counterId);
AbstractCounterDialog dialog(counterName, oldValueForDlg, dialogParent); AbstractCounterDialog dialog(counterName, oldValueForDlg, dialogParent());
int ok = dialog.exec(); int ok = dialog.exec();
emit requestDialogSemaphore(false); emit requestDialogSemaphore(false);

View file

@ -2,6 +2,7 @@
#define COCKATRICE_PLAYER_DIALOGS_H #define COCKATRICE_PLAYER_DIALOGS_H
#include "player_actions.h" #include "player_actions.h"
#include <QGraphicsView>
#include <QObject> #include <QObject>
class PlayerDialogs : public QObject class PlayerDialogs : public QObject
@ -10,7 +11,7 @@ class PlayerDialogs : public QObject
Q_OBJECT Q_OBJECT
public: public:
explicit PlayerDialogs(PlayerGraphicsItem *player, QWidget *dialogParent, PlayerActions *playerActions); explicit PlayerDialogs(PlayerGraphicsItem *player, PlayerActions *playerActions);
signals: signals:
void requestDialogSemaphore(bool active); void requestDialogSemaphore(bool active);
@ -44,8 +45,17 @@ public slots:
private: private:
PlayerGraphicsItem *player; PlayerGraphicsItem *player;
QWidget *dialogParent;
PlayerActions *playerActions; PlayerActions *playerActions;
QWidget *dialogParent() const
{
if (auto *s = player->scene()) {
if (auto *v = s->views().value(0)) {
return v->window();
}
}
return nullptr;
}
}; };
#endif // COCKATRICE_PLAYER_DIALOGS_H #endif // COCKATRICE_PLAYER_DIALOGS_H

View file

@ -45,12 +45,7 @@ PlayerGraphicsItem::PlayerGraphicsItem(PlayerLogic *_player) : player(_player)
} }
}); });
QWidget *dialogParent = nullptr; playerDialogs = new PlayerDialogs(this, player->getPlayerActions());
if (auto *view = scene() ? scene()->views().value(0) : nullptr) {
dialogParent = view->window();
}
playerDialogs = new PlayerDialogs(this, dialogParent, player->getPlayerActions());
playerArea = new PlayerArea(this); playerArea = new PlayerArea(this);