Skip to content

Commit

Permalink
Using new class CardButton to simplify the PlayerCardDialog
Browse files Browse the repository at this point in the history
  • Loading branch information
Moligaloo committed Aug 2, 2013
1 parent 00d8952 commit 50cf4de
Show file tree
Hide file tree
Showing 8 changed files with 116 additions and 141 deletions.
6 changes: 4 additions & 2 deletions QSanguosha.pro
Expand Up @@ -152,7 +152,8 @@ SOURCES += \
src/dialog/generalmodel.cpp \
src/dialog/dialogutil.cpp \
src/dialog/damagemakerdialog.cpp \
src/client/clientplayerlistmodel.cpp
src/client/clientplayerlistmodel.cpp \
src/dialog/cardbutton.cpp

HEADERS += \
src/client/aux-skills.h \
Expand Down Expand Up @@ -278,7 +279,8 @@ HEADERS += \
src/dialog/generalmodel.h \
src/dialog/dialogutil.h \
src/dialog/damagemakerdialog.h \
src/client/clientplayerlistmodel.h
src/client/clientplayerlistmodel.h \
src/dialog/cardbutton.h

FORMS += \
src/dialog/cardoverview.ui \
Expand Down
55 changes: 20 additions & 35 deletions sanguosha.ts
Expand Up @@ -2990,42 +2990,42 @@
<context>
<name>PlayerCardDialog</name>
<message>
<location filename="src/dialog/playercarddialog.cpp" line="40"/>
<location filename="src/dialog/playercarddialog.cpp" line="41"/>
<source>Player info</source>
<translation>玩家信息</translation>
</message>
<message>
<location filename="src/dialog/playercarddialog.cpp" line="43"/>
<location filename="src/dialog/playercarddialog.cpp" line="44"/>
<source>Avatar</source>
<translation>头像</translation>
</message>
<message>
<location filename="src/dialog/playercarddialog.cpp" line="44"/>
<location filename="src/dialog/playercarddialog.cpp" line="45"/>
<source>Name</source>
<translation>名称</translation>
</message>
<message>
<location filename="src/dialog/playercarddialog.cpp" line="45"/>
<location filename="src/dialog/playercarddialog.cpp" line="46"/>
<source>Gender</source>
<translation>性别</translation>
</message>
<message>
<location filename="src/dialog/playercarddialog.cpp" line="46"/>
<location filename="src/dialog/playercarddialog.cpp" line="47"/>
<source>General</source>
<translation>武将</translation>
</message>
<message>
<location filename="src/dialog/playercarddialog.cpp" line="47"/>
<location filename="src/dialog/playercarddialog.cpp" line="48"/>
<source>Kingdom</source>
<translation>势力</translation>
</message>
<message>
<location filename="src/dialog/playercarddialog.cpp" line="48"/>
<location filename="src/dialog/playercarddialog.cpp" line="49"/>
<source>HP</source>
<translation>体力</translation>
</message>
<message>
<location filename="src/dialog/playercarddialog.cpp" line="51"/>
<location filename="src/dialog/playercarddialog.cpp" line="52"/>
<source>Marks</source>
<translation>标记</translation>
</message>
Expand All @@ -3035,47 +3035,32 @@
<translation>手牌区域</translation>
</message>
<message>
<location filename="src/dialog/playercarddialog.cpp" line="80"/>
<source>Handcard</source>
<translation>手牌</translation>
<location filename="src/dialog/playercarddialog.cpp" line="69"/>
<source>This guy does not have any hand cards</source>
<translation>这个家伙空城了</translation>
</message>
<message>
<location filename="src/dialog/playercarddialog.cpp" line="84"/>
<source>This guy has no any hand cards</source>
<translation>这个家伙空城了</translation>
<location filename="src/dialog/playercarddialog.cpp" line="81"/>
<source>Handcard (%1)</source>
<translation>手牌 (%1)</translation>
</message>
<message>
<location filename="src/dialog/playercarddialog.cpp" line="87"/>
<source>This guy has %1 hand card(s)</source>
<translation>这家伙有 %1 张牌</translation>
<location filename="src/dialog/playercarddialog.cpp" line="116"/>
<source>Judging area</source>
<translation>判定区</translation>
</message>
<message>
<location filename="src/dialog/playercarddialog.cpp" line="97"/>
<location filename="src/dialog/playercarddialog.cpp" line="112"/>
<source>Equip area</source>
<translation>装备区</translation>
</message>
<message>
<location filename="src/dialog/playercarddialog.cpp" line="122"/>
<source>(+1 horse)</source>
<translation>(+1 马)</translation>
</message>
<message>
<location filename="src/dialog/playercarddialog.cpp" line="132"/>
<source>(-1 horse)</source>
<translation>(-1 马)</translation>
</message>
<message>
<location filename="src/dialog/playercarddialog.cpp" line="141"/>
<location filename="src/dialog/playercarddialog.cpp" line="112"/>
<source>No equip</source>
<translation>没有装备</translation>
</message>
<message>
<location filename="src/dialog/playercarddialog.cpp" line="152"/>
<source>Judging Area</source>
<translation>判定区</translation>
</message>
<message>
<location filename="src/dialog/playercarddialog.cpp" line="165"/>
<location filename="src/dialog/playercarddialog.cpp" line="116"/>
<source>No judging cards</source>
<translation>没有判定牌</translation>
</message>
Expand Down
2 changes: 1 addition & 1 deletion src/client/client.cpp
Expand Up @@ -836,7 +836,7 @@ void Client::askForCardChosen(const QString &ask_str){
PlayerCardDialog *dialog = new PlayerCardDialog(player, flags);
dialog->setWindowTitle(Sanguosha->translate(reason));

connect(dialog, SIGNAL(card_id_chosen(int)), this, SLOT(chooseCard(int)));
connect(dialog, SIGNAL(idSelected(int)), this, SLOT(chooseCard(int)));
connect(dialog, SIGNAL(rejected()), this, SLOT(chooseCard()));

ask_dialog = dialog;
Expand Down
2 changes: 2 additions & 0 deletions src/core/card.h
Expand Up @@ -164,6 +164,8 @@ class Card : public QObject
mutable QStringList flags;
};

typedef QList<const Card *> CardList;

class SkillCard: public Card{
Q_OBJECT

Expand Down
23 changes: 23 additions & 0 deletions src/dialog/cardbutton.cpp
@@ -0,0 +1,23 @@
#include "cardbutton.h"

#include "card.h"

CardButton::CardButton(const Card *card)
:card(card)
{
if(card){
setText(card->getFullName());
setIcon(card->getSuitIcon());
setToolTip(card->getDescription());
}

connect(this, SIGNAL(clicked()), this, SLOT(onClicked()));
}

void CardButton::onClicked()
{
if(card)
emit idSelected(card->getId());
else
emit idSelected(-1);
}
30 changes: 30 additions & 0 deletions src/dialog/cardbutton.h
@@ -0,0 +1,30 @@
#ifndef CARDBUTTON_H
#define CARDBUTTON_H

#ifdef Q_OS_WIN
#define CommandLinkButton QCommandLinkButton
#include <QCommandLinkButton>
#else
#define CommandLinkButton QPushButton
#include <QPushButton>
#endif

class Card;

class CardButton : public CommandLinkButton
{
Q_OBJECT
public:
explicit CardButton(const Card *card);

private:
const Card *card;

private slots:
void onClicked();

signals:
void idSelected(int id);
};

#endif // CARDBUTTON_H
132 changes: 35 additions & 97 deletions src/dialog/playercarddialog.cpp
@@ -1,6 +1,7 @@
#include "playercarddialog.h"
#include "standard.h"
#include "engine.h"
#include "cardbutton.h"

#include <QCommandLinkButton>
#include <QVBoxLayout>
Expand Down Expand Up @@ -60,120 +61,57 @@ QWidget *PlayerCardDialog::createAvatar(){
}

QWidget *PlayerCardDialog::createHandcardButton(){
if(!player->isKongcheng() && ((Self->hasSkill("dongcha") && player->hasFlag("dongchaee")) || Self == player)){
QGroupBox *area = new QGroupBox(tr("Handcard area"));
QVBoxLayout *layout = new QVBoxLayout;
QGroupBox *area = new QGroupBox(tr("Handcard area"));
QVBoxLayout *layout = new QVBoxLayout;
area->setLayout(layout);

if(player->isKongcheng()){
CommandLinkButton *button = new CommandLinkButton(tr("This guy does not have any hand cards"));
button->setEnabled(false);
layout->addWidget(button);
}else if((Self->hasSkill("dongcha") && player->hasFlag("dongchaee")) || Self == player){
QList<const Card *> cards = player->getCards();
foreach(const Card *card, cards){
QCommandLinkButton *button = new QCommandLinkButton(card->getFullName());
button->setIcon(card->getSuitIcon());

mapper.insert(button, card->getId());
connect(button, SIGNAL(clicked()), this, SLOT(emitId()));
CardButton *button = new CardButton(card);
layout->addWidget(button);
connect(button, SIGNAL(idSelected(int)), this, SIGNAL(idSelected(int)));
}

area->setLayout(layout);
return area;
}

QCommandLinkButton *button = new QCommandLinkButton(tr("Handcard"));
button->setObjectName("handcard_button");
int num = player->getHandcardNum();
if(num == 0){
button->setDescription(tr("This guy has no any hand cards"));
button->setEnabled(false);
}else{
button->setDescription(tr("This guy has %1 hand card(s)").arg(num));

mapper.insert(button, -1);
connect(button, SIGNAL(clicked()), this, SLOT(emitId()));
CardButton *button = new CardButton(NULL);
button->setText(tr("Handcard (%1)").arg(player->getHandcardNum()));
connect(button, SIGNAL(idSelected(int)), this, SIGNAL(idSelected(int)));
layout->addWidget(button);
}

return button;
return area;
}

QWidget *PlayerCardDialog::createEquipArea(){
QGroupBox *area = new QGroupBox(tr("Equip area"));
static QGroupBox *CreateButtonArea(PlayerCardDialog *dialog, const CardList &list, const QString &title, const QString &noCardText){
QGroupBox *area = new QGroupBox(title);
QVBoxLayout *layout = new QVBoxLayout;
area->setLayout(layout);

const Weapon *weapon = player->getWeapon();
if(weapon){
QCommandLinkButton *button = new QCommandLinkButton(weapon->getFullName());
button->setIcon(weapon->getSuitIcon());

mapper.insert(button, weapon->getId());
connect(button, SIGNAL(clicked()), this, SLOT(emitId()));
layout->addWidget(button);
}

const Armor *armor = player->getArmor();
if(armor){
QCommandLinkButton *button = new QCommandLinkButton(armor->getFullName());
button->setIcon(armor->getSuitIcon());

mapper.insert(button, armor->getId());
connect(button, SIGNAL(clicked()), this, SLOT(emitId()));
layout->addWidget(button);
}

const Horse *horse = player->getDefensiveHorse();
if(horse){
QCommandLinkButton *button = new QCommandLinkButton(horse->getFullName() + tr("(+1 horse)"));
button->setIcon(horse->getSuitIcon());

mapper.insert(button, horse->getId());
connect(button, SIGNAL(clicked()), this, SLOT(emitId()));
if(list.isEmpty()){
CommandLinkButton *button = new CommandLinkButton;
button->setText(noCardText);
button->setEnabled(false);
layout->addWidget(button);
}

horse = player->getOffensiveHorse();
if(horse){
QCommandLinkButton *button = new QCommandLinkButton(horse->getFullName() + tr("(-1 horse)"));
button->setIcon(horse->getSuitIcon());
}else{
foreach(const Card *card, list){
CardButton *button = new CardButton(card);
layout->addWidget(button);

mapper.insert(button, horse->getId());
connect(button, SIGNAL(clicked()), this, SLOT(emitId()));
layout->addWidget(button);
QObject::connect(button, SIGNAL(idSelected(int)), dialog, SIGNAL(idSelected(int)));
}
}

if(layout->count() == 0){
QCommandLinkButton *no_equip = new QCommandLinkButton(tr("No equip"));
no_equip->setEnabled(false);
no_equip->setObjectName("noequip_button");
return no_equip;
}else{
area->setLayout(layout);
return area;
}
return area;
}

QWidget *PlayerCardDialog::createJudgingArea(){
QGroupBox *area = new QGroupBox(tr("Judging Area"));
QVBoxLayout *layout = new QVBoxLayout;
QList<const Card *> cards = player->getJudgingArea();
foreach(const Card *card, cards){
QCommandLinkButton *button = new QCommandLinkButton(card->getFullName());
button->setIcon(card->getSuitIcon());
layout->addWidget(button);

mapper.insert(button, card->getId());
connect(button, SIGNAL(clicked()), this, SLOT(emitId()));
}

if(layout->count() == 0){
QCommandLinkButton *button = new QCommandLinkButton(tr("No judging cards"));
button->setEnabled(false);
button->setObjectName("nojuding_button");
return button;
}else{
area->setLayout(layout);
return area;
}
QWidget *PlayerCardDialog::createEquipArea(){
return CreateButtonArea(this, player->getEquips(), tr("Equip area"), tr("No equip"));
}

void PlayerCardDialog::emitId(){
int id = mapper.value(sender(), -2);
if(id != -2)
emit card_id_chosen(id);
QWidget *PlayerCardDialog::createJudgingArea(){
return CreateButtonArea(this, player->getJudgingArea(), tr("Judging area"), tr("No judging cards"));
}
7 changes: 1 addition & 6 deletions src/dialog/playercarddialog.h
Expand Up @@ -19,13 +19,8 @@ class PlayerCardDialog : public QDialog{
QWidget *createJudgingArea();

const ClientPlayer *player;
QMap<QObject *, int> mapper;

private slots:
void emitId();

signals:
void card_id_chosen(int card_id);
void idSelected(int card_id);
};

#endif // PLAYERCARDDIALOG_H

0 comments on commit 50cf4de

Please sign in to comment.