Refactor: move last token info into struct (#5808)

* add override

* refactor token info into struct

* correct default destroy value
This commit is contained in:
RickyRister 2025-04-09 08:26:14 -07:00 committed by GitHub
parent 0bd53d6dc7
commit 56bbd8a172
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 45 additions and 56 deletions

View file

@ -224,27 +224,11 @@ void DlgCreateToken::actReject()
reject();
}
QString DlgCreateToken::getName() const
TokenInfo DlgCreateToken::getTokenInfo() const
{
return nameEdit->text();
}
QString DlgCreateToken::getColor() const
{
return QString(colorEdit->itemData(colorEdit->currentIndex()).toChar());
}
QString DlgCreateToken::getPT() const
{
return ptEdit->text();
}
QString DlgCreateToken::getAnnotation() const
{
return annotationEdit->text();
}
bool DlgCreateToken::getDestroy() const
{
return destroyCheckBox->isChecked();
return {.name = nameEdit->text(),
.color = colorEdit->itemData(colorEdit->currentIndex()).toString(),
.pt = ptEdit->text(),
.annotation = annotationEdit->text(),
.destroy = destroyCheckBox->isChecked()};
}

View file

@ -17,19 +17,24 @@ class CardDatabaseModel;
class TokenDisplayModel;
class CardInfoPictureWidget;
struct TokenInfo
{
QString name;
QString color;
QString pt;
QString annotation;
bool destroy = true;
};
class DlgCreateToken : public QDialog
{
Q_OBJECT
public:
explicit DlgCreateToken(const QStringList &_predefinedTokens, QWidget *parent = nullptr);
QString getName() const;
QString getColor() const;
QString getPT() const;
QString getAnnotation() const;
bool getDestroy() const;
TokenInfo getTokenInfo() const;
protected:
void closeEvent(QCloseEvent *event);
void closeEvent(QCloseEvent *event) override;
private slots:
void tokenSelectionChanged(const QModelIndex &current, const QModelIndex &previous);
void updateSearch(const QString &search);

View file

@ -4,7 +4,6 @@
#include "../../client/tabs/tab_game.h"
#include "../../client/ui/theme_manager.h"
#include "../../deck/deck_loader.h"
#include "../../dialogs/dlg_create_token.h"
#include "../../dialogs/dlg_move_top_cards_until.h"
#include "../../dialogs/dlg_roll_dice.h"
#include "../../main.h"
@ -109,9 +108,9 @@ void PlayerArea::setPlayerZoneId(int _playerZoneId)
}
Player::Player(const ServerInfo_User &info, int _id, bool _local, bool _judge, TabGame *_parent)
: QObject(_parent), game(_parent), movingCardsUntil(false), shortcutsActive(false), lastTokenDestroy(true),
lastTokenTableRow(0), id(_id), active(false), local(_local), judge(_judge), mirrored(false), handVisible(false),
conceded(false), zoneId(0), dialogSemaphore(false), deck(nullptr)
: QObject(_parent), game(_parent), movingCardsUntil(false), shortcutsActive(false), lastTokenTableRow(0), id(_id),
active(false), local(_local), judge(_judge), mirrored(false), handVisible(false), conceded(false), zoneId(0),
dialogSemaphore(false), deck(nullptr)
{
userInfo = new ServerInfo_User;
userInfo->CopyFrom(info);
@ -1826,37 +1825,35 @@ void Player::actCreateToken()
return;
}
lastTokenName = dlg.getName();
lastTokenPT = dlg.getPT();
CardInfoPtr correctedCard = CardDatabaseManager::getInstance()->guessCard(lastTokenName);
lastTokenInfo = dlg.getTokenInfo();
CardInfoPtr correctedCard = CardDatabaseManager::getInstance()->guessCard(lastTokenInfo.name);
if (correctedCard) {
lastTokenName = correctedCard->getName();
lastTokenInfo.name = correctedCard->getName();
lastTokenTableRow = TableZone::clampValidTableRow(2 - correctedCard->getTableRow());
if (lastTokenPT.isEmpty()) {
lastTokenPT = correctedCard->getPowTough();
if (lastTokenInfo.pt.isEmpty()) {
lastTokenInfo.pt = correctedCard->getPowTough();
}
}
lastTokenColor = dlg.getColor();
lastTokenAnnotation = dlg.getAnnotation();
lastTokenDestroy = dlg.getDestroy();
aCreateAnotherToken->setEnabled(true);
aCreateAnotherToken->setText(tr("C&reate another %1 token").arg(lastTokenName));
aCreateAnotherToken->setText(tr("C&reate another %1 token").arg(lastTokenInfo.name));
actCreateAnotherToken();
}
void Player::actCreateAnotherToken()
{
if (lastTokenName.isEmpty()) {
if (lastTokenInfo.name.isEmpty()) {
return;
}
Command_CreateToken cmd;
cmd.set_zone("table");
cmd.set_card_name(lastTokenName.toStdString());
cmd.set_color(lastTokenColor.toStdString());
cmd.set_pt(lastTokenPT.toStdString());
cmd.set_annotation(lastTokenAnnotation.toStdString());
cmd.set_destroy_on_zone_change(lastTokenDestroy);
cmd.set_card_name(lastTokenInfo.name.toStdString());
cmd.set_color(lastTokenInfo.color.toStdString());
cmd.set_pt(lastTokenInfo.pt.toStdString());
cmd.set_annotation(lastTokenInfo.annotation.toStdString());
cmd.set_destroy_on_zone_change(lastTokenInfo.destroy);
cmd.set_x(-1);
cmd.set_y(lastTokenTableRow);
@ -4180,12 +4177,13 @@ void Player::setLastToken(CardInfoPtr cardInfo)
return;
}
lastTokenName = cardInfo->getName();
lastTokenColor = cardInfo->getColors().isEmpty() ? QString() : cardInfo->getColors().left(1).toLower();
lastTokenPT = cardInfo->getPowTough();
lastTokenAnnotation = SettingsCache::instance().getAnnotateTokens() ? cardInfo->getText() : "";
lastTokenInfo = {.name = cardInfo->getName(),
.color = cardInfo->getColors().isEmpty() ? QString() : cardInfo->getColors().left(1).toLower(),
.pt = cardInfo->getPowTough(),
.annotation = SettingsCache::instance().getAnnotateTokens() ? cardInfo->getText() : "",
.destroy = true};
lastTokenTableRow = TableZone::clampValidTableRow(2 - cardInfo->getTableRow());
lastTokenDestroy = true;
aCreateAnotherToken->setText(tr("C&reate another %1 token").arg(lastTokenName));
aCreateAnotherToken->setText(tr("C&reate another %1 token").arg(lastTokenInfo.name));
aCreateAnotherToken->setEnabled(true);
}

View file

@ -2,6 +2,7 @@
#define PLAYER_H
#include "../../client/tearoff_menu.h"
#include "../../dialogs/dlg_create_token.h"
#include "../board/abstract_graphics_item.h"
#include "../cards/card_database.h"
#include "../filters/filter_string.h"
@ -291,9 +292,10 @@ private:
int defaultNumberTopCardsToPlaceBelow = 1;
int defaultNumberBottomCards = 1;
int defaultNumberDieRoll = 20;
QString lastTokenName, lastTokenColor, lastTokenPT, lastTokenAnnotation;
bool lastTokenDestroy;
TokenInfo lastTokenInfo;
int lastTokenTableRow;
ServerInfo_User *userInfo;
int id;
bool active;