servatrice/cockatrice/src/cardzone.cpp
Max-Wilhelm Bruker 77f5ec29eb some gui code
2009-11-16 17:18:13 +01:00

150 lines
4.6 KiB
C++

#include <QtGui>
#include "cardzone.h"
#include "carditem.h"
#include "player.h"
#include "client.h"
#include "zoneviewzone.h"
CardZone::CardZone(Player *_p, const QString &_name, bool _hasCardAttr, bool _isShufflable, bool _contentsKnown, QGraphicsItem *parent, bool isView)
: AbstractGraphicsItem(parent), player(_p), name(_name), cards(_contentsKnown), view(NULL), menu(NULL), doubleClickAction(0), hasCardAttr(_hasCardAttr), isShufflable(_isShufflable)
{
if (!isView)
player->addZone(this);
}
CardZone::~CardZone()
{
qDebug(QString("CardZone destructor: %1").arg(name).toLatin1());
delete view;
clearContents();
}
void CardZone::clearContents()
{
for (int i = 0; i < cards.size(); i++)
delete cards.at(i);
cards.clear();
}
QString CardZone::getTranslatedName(bool hisOwn, GrammaticalCase gc) const
{
QString ownerName = player->getName();
if (name == "hand")
switch (gc) {
case CaseNominative: return hisOwn ? tr("his hand") : tr("%1's hand").arg(ownerName);
case CaseGenitive: return hisOwn ? tr("of his hand") : tr("of %1's hand").arg(ownerName);
case CaseAccusative: return hisOwn ? tr("his hand") : tr("%1's hand").arg(ownerName);
}
else if (name == "deck")
switch (gc) {
case CaseNominative: return hisOwn ? tr("his library") : tr("%1's library").arg(ownerName);
case CaseGenitive: return hisOwn ? tr("of his library") : tr("of %1's library").arg(ownerName);
case CaseAccusative: return hisOwn ? tr("his library") : tr("%1's library").arg(ownerName);
}
else if (name == "grave")
switch (gc) {
case CaseNominative: return hisOwn ? tr("his graveyard") : tr("%1's graveyard").arg(ownerName);
case CaseGenitive: return hisOwn ? tr("of his graveyard") : tr("of %1's graveyard").arg(ownerName);
case CaseAccusative: return hisOwn ? tr("his graveyard") : tr("%1's graveyard").arg(ownerName);
}
else if (name == "rfg")
switch (gc) {
case CaseNominative: return hisOwn ? tr("his exile") : tr("%1's exile").arg(ownerName);
case CaseGenitive: return hisOwn ? tr("of his exile") : tr("of %1's exile").arg(ownerName);
case CaseAccusative: return hisOwn ? tr("his exile") : tr("%1's exile").arg(ownerName);
}
else if (name == "sb")
switch (gc) {
case CaseNominative: return hisOwn ? tr("his sideboard") : tr("%1's sideboard").arg(ownerName);
case CaseGenitive: return hisOwn ? tr("of his sideboard") : tr("of %1's sideboard").arg(ownerName);
case CaseAccusative: return hisOwn ? tr("his sideboard") : tr("%1's sideboard").arg(ownerName);
}
return QString();
}
void CardZone::mouseDoubleClickEvent(QGraphicsSceneMouseEvent */*event*/)
{
if (doubleClickAction)
doubleClickAction->trigger();
}
void CardZone::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
if (event->button() == Qt::RightButton) {
if (menu) {
menu->exec(event->screenPos());
event->accept();
} else
event->ignore();
} else
event->ignore();
}
void CardZone::addCard(CardItem *card, bool reorganize, int x, int y)
{
if (view)
if ((x <= view->getCards().size()) || (view->getNumberCards() == -1))
view->addCard(new CardItem(card->getName(), card->getId()), reorganize, x, y);
addCardImpl(card, x, y);
if (reorganize) {
qDebug("------------ emitting");
dumpObjectInfo();
emit contentsChanged();
reorganizeCards();
}
}
CardItem *CardZone::getCard(int cardId, const QString &cardName)
{
CardItem *c = cards.findCard(cardId, false);
// If the card's id is -1, this zone is invisible,
// so we need to give the card an id and a name as it comes out.
// It can be assumed that in an invisible zone, all cards are equal.
if ((c->getId() == -1) || (c->getName().isEmpty())) {
c->setId(cardId);
c->setName(cardName);
}
return c;
}
CardItem *CardZone::takeCard(int position, int cardId, const QString &cardName, bool /*canResize*/)
{
Q_ASSERT(position < cards.size());
CardItem *c = cards.takeAt(position);
if (view)
view->removeCard(position);
c->setId(cardId);
c->setName(cardName);
emit contentsChanged();
reorganizeCards();
return c;
}
void CardZone::setCardAttr(int cardId, const QString &aname, const QString &avalue)
{
if (hasCardAttr)
player->client->setCardAttr(name, cardId, aname, avalue);
}
void CardZone::moveAllToZone()
{
QList<QVariant> data = static_cast<QAction *>(sender())->data().toList();
QString targetZone = data[0].toString();
int targetX = data[1].toInt();
// Cards need to be moved in reverse order so that the other
// cards' list index doesn't change
for (int i = cards.size() - 1; i >= 0; i--)
player->client->moveCard(cards.at(i)->getId(), getName(), targetZone, targetX);
}
QPointF CardZone::closestGridPoint(const QPointF &point)
{
return point;
}