some zone view changes going on

This commit is contained in:
Max-Wilhelm Bruker 2009-09-10 18:28:19 +02:00
parent aed30708de
commit 224a4969bd
25 changed files with 754 additions and 365 deletions

View file

@ -30,3 +30,17 @@ CardItem *CardList::findCard(const int id, const bool remove, int *position)
}
return 0;
}
class CardList::compareFunctor {
public:
inline bool operator()(CardItem *a, CardItem *b) const
{
return a->getName() < b->getName();
}
};
void CardList::sort()
{
compareFunctor cf;
qSort(begin(), end(), cf);
}

View file

@ -6,12 +6,15 @@
class CardItem;
class CardList : public QList<CardItem *> {
private:
class compareFunctor;
protected:
bool contentsKnown;
public:
CardList(bool _contentsKnown);
CardItem *findCard(const int id, const bool remove, int *position = NULL);
bool getContentsKnown() const { return contentsKnown; }
void sort();
};
#endif

View file

@ -26,6 +26,42 @@ void CardZone::clearContents()
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)
@ -52,8 +88,12 @@ void CardZone::addCard(CardItem *card, bool reorganize, int x, int y)
addCardImpl(card, x, y);
if (reorganize)
if (reorganize) {
qDebug("------------ emitting");
dumpObjectInfo();
emit contentsChanged();
reorganizeCards();
}
}
CardItem *CardZone::getCard(int cardId, const QString &cardName)
@ -81,6 +121,7 @@ CardItem *CardZone::takeCard(int position, int cardId, const QString &cardName,
c->setId(cardId);
c->setName(cardName);
emit contentsChanged();
reorganizeCards();
return c;
}

View file

@ -5,6 +5,7 @@
#include "cardlist.h"
#include "carditem.h"
#include "abstractgraphicsitem.h"
#include "translation.h"
class Player;
class ZoneViewZone;
@ -12,7 +13,8 @@ class QMenu;
class QAction;
class QPainter;
class CardZone : public AbstractGraphicsItem {
class CardZone : public QObject, public AbstractGraphicsItem {
Q_OBJECT
protected:
Player *player;
QString name;
@ -25,6 +27,8 @@ protected:
void mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event);
void mousePressEvent(QGraphicsSceneMouseEvent *event);
virtual void addCardImpl(CardItem *card, int x, int y) = 0;
signals:
void contentsChanged();
public:
enum { Type = typeZone };
int type() const { return Type; }
@ -37,6 +41,7 @@ public:
QMenu *getMenu() const { return menu; }
void setMenu(QMenu *_menu, QAction *_doubleClickAction = 0) { menu = _menu; doubleClickAction = _doubleClickAction; }
QString getName() const { return name; }
QString getTranslatedName(bool hisOwn, GrammaticalCase gc) const;
Player *getPlayer() const { return player; }
bool contentsKnown() const { return cards.getContentsKnown(); }
const CardList &getCards() const { return cards; }

View file

@ -234,15 +234,32 @@ AppearanceSettingsPage::AppearanceSettingsPage()
economicGridCheckBox = new QCheckBox;
economicGridCheckBox->setChecked(settings.value("economic", 1).toInt());
connect(economicGridCheckBox, SIGNAL(stateChanged(int)), this, SLOT(economicGridCheckBoxChanged(int)));
settings.endGroup();
QGridLayout *tableGrid = new QGridLayout;
tableGrid->addWidget(economicGridCheckBox, 0, 0, 1, 2);
tableGroupBox->setLayout(tableGrid);
zoneViewGroupBox = new QGroupBox;
settings.beginGroup("zoneview");
zoneViewSortingCheckBox = new QCheckBox;
zoneViewSortingCheckBox->setChecked(settings.value("sorting").toInt());
connect(zoneViewSortingCheckBox, SIGNAL(stateChanged(int)), this, SLOT(zoneViewSortingCheckBoxChanged(int)));
settings.endGroup();
QGridLayout *zoneViewGrid = new QGridLayout;
zoneViewGrid->addWidget(zoneViewSortingCheckBox, 0, 0, 1, 2);
zoneViewGroupBox->setLayout(zoneViewGrid);
QVBoxLayout *mainLayout = new QVBoxLayout;
mainLayout->addWidget(zoneBgGroupBox);
mainLayout->addWidget(tableGroupBox);
mainLayout->addWidget(zoneViewGroupBox);
setLayout(mainLayout);
@ -257,6 +274,9 @@ void AppearanceSettingsPage::retranslateUi()
tableGroupBox->setTitle(tr("Table grid layout"));
economicGridCheckBox->setText(tr("Economic layout"));
zoneViewGroupBox->setTitle(tr("Zone view layout"));
zoneViewSortingCheckBox->setText(tr("Sort alphabetically by default"));
}
void AppearanceSettingsPage::handBgButtonClicked()
@ -307,6 +327,15 @@ void AppearanceSettingsPage::economicGridCheckBoxChanged(int state)
emit economicGridChanged(state);
}
void AppearanceSettingsPage::zoneViewSortingCheckBoxChanged(int state)
{
QSettings settings;
settings.beginGroup("zoneview");
settings.setValue("sorting", state);
emit zoneViewSortingChanged(state);
}
MessagesSettingsPage::MessagesSettingsPage()
{
aAdd = new QAction(this);

View file

@ -55,16 +55,18 @@ private slots:
void tableBgButtonClicked();
void playerAreaBgButtonClicked();
void economicGridCheckBoxChanged(int state);
void zoneViewSortingCheckBoxChanged(int state);
signals:
void handBgChanged(const QString &path);
void tableBgChanged(const QString &path);
void playerAreaBgChanged(const QString &path);
void economicGridChanged(int state);
void zoneViewSortingChanged(int state);
private:
QLabel *handBgLabel, *tableBgLabel, *playerAreaBgLabel;
QLineEdit *handBgEdit, *tableBgEdit, *playerAreaBgEdit;
QCheckBox *economicGridCheckBox;
QGroupBox *zoneBgGroupBox, *tableGroupBox;
QCheckBox *economicGridCheckBox, *zoneViewSortingCheckBox;
QGroupBox *zoneBgGroupBox, *tableGroupBox, *zoneViewGroupBox;
public:
AppearanceSettingsPage();
void retranslateUi();

View file

@ -152,6 +152,7 @@ void Game::retranslateUi()
aMoveToTopLibrary->setText(tr("&top of library"));
aMoveToBottomLibrary->setText(tr("&bottom of library"));
aMoveToGraveyard->setText(tr("&graveyard"));
aMoveToGraveyard->setShortcut(tr("Ctrl+Del"));
aMoveToExile->setText(tr("&exile"));
moveMenu->setTitle(tr("&Move to"));
@ -319,7 +320,7 @@ void Game::gameEvent(const ServerEventData &msg)
CardZone *zone = zoneOwner->getZones()->findZone(data[1]);
if (!zone)
break;
emit logDumpZone(p, zone, zoneOwner, data[2].toInt());
emit logDumpZone(p, zone, data[2].toInt());
break;
}
case eventStopDumpZone: {
@ -330,7 +331,7 @@ void Game::gameEvent(const ServerEventData &msg)
CardZone *zone = zoneOwner->getZones()->findZone(data[1]);
if (!zone)
break;
emit logStopDumpZone(p, zone, zoneOwner);
emit logStopDumpZone(p, zone);
break;
}
case eventMoveCard: {

View file

@ -88,8 +88,8 @@ signals:
void logSetTapped(Player *player, QString cardName, bool tapped);
void logSetCounter(Player *player, QString counterName, int value, int oldValue);
void logSetDoesntUntap(Player *player, QString cardName, bool doesntUntap);
void logDumpZone(Player *player, CardZone *zone, Player *zoneOwner, int numberCards);
void logStopDumpZone(Player *player, CardZone *zone, Player *zoneOwner);
void logDumpZone(Player *player, CardZone *zone, int numberCards);
void logStopDumpZone(Player *player, CardZone *zone);
void logSetActivePlayer(Player *player);
void setActivePhase(int phase);
public:

View file

@ -53,8 +53,12 @@ void GameScene::rearrange()
if (localPlayer)
PlayerProcessor::processPlayer(localPlayer, sceneWidth, sceneHeight, base);
playersRect = QRectF(0, 0, sceneWidth, sceneHeight);
zvLayout->setPos(QPointF(sceneWidth, 0));
sceneWidth += zvLayout->size().width();
if (zvLayout->size().height() > sceneHeight)
sceneHeight = zvLayout->size().height();
setSceneRect(sceneRect().x(), sceneRect().y(), sceneWidth, sceneHeight);
qDebug(QString("rearrange(): w=%1 h=%2").arg(sceneWidth).arg(sceneHeight).toLatin1());

View file

@ -14,8 +14,10 @@ private:
QList<Player *> players;
ZoneViewLayout *zvLayout;
QRectF playersRect;
public:
GameScene(ZoneViewLayout *_zvLayout, QObject *parent = 0);
const QRectF &getPlayersRect() const { return playersRect; }
public slots:
void addPlayer(Player *player);
void removePlayer(Player *player);

View file

@ -69,6 +69,9 @@ void GamesModel::updateGameList(ServerGame *game)
void GamesModel::cleanList()
{
if (gameList.isEmpty())
return;
beginRemoveRows(QModelIndex(), 0, gameList.size() - 1);
QListIterator<ServerGame *> i(gameList);
while (i.hasNext())

View file

@ -12,29 +12,6 @@ QString MessageLogWidget::sanitizeHtml(QString dirty) const
.replace(">", "&gt;");
}
QString MessageLogWidget::trZoneName(CardZone *zone, Player *owner, bool hisOwn, GrammaticalCase gc) const
{
if (zone->getName() == "hand")
switch (gc) {
// case CaseNominative: return hisOwn ? tr("his hand") : tr("%1's hand").arg(owner->getName());
case CaseGenitive: return hisOwn ? tr("of his hand") : tr("of %1's hand").arg(owner->getName());
case CaseAccusative: return hisOwn ? tr("his hand") : tr("%1's hand").arg(owner->getName());
}
else if (zone->getName() == "deck")
switch (gc) {
// case CaseNominative: return hisOwn ? tr("his library") : tr("%1's library").arg(owner->getName());
case CaseGenitive: return hisOwn ? tr("of his library") : tr("of %1's library").arg(owner->getName());
case CaseAccusative: return hisOwn ? tr("his library") : tr("%1's library").arg(owner->getName());
}
else if (zone->getName() == "sb")
switch (gc) {
// case CaseNominative: return hisOwn ? tr("his sideboard") : tr("%1's sideboard").arg(owner->getName());
case CaseGenitive: return hisOwn ? tr("of his sideboard") : tr("of %1's sideboard").arg(owner->getName());
case CaseAccusative: return hisOwn ? tr("his sideboard") : tr("%1's sideboard").arg(owner->getName());
}
return QString();
}
void MessageLogWidget::logConnecting(QString hostname)
{
append(tr("Connecting to %1...").arg(sanitizeHtml(hostname)));
@ -226,17 +203,17 @@ void MessageLogWidget::logSetDoesntUntap(Player *player, QString cardName, bool
append(finalStr.arg(sanitizeHtml(player->getName())).arg(QString("<font color=\"blue\">%1</font>").arg(sanitizeHtml(cardName))));
}
void MessageLogWidget::logDumpZone(Player *player, CardZone *zone, Player *zoneOwner, int numberCards)
void MessageLogWidget::logDumpZone(Player *player, CardZone *zone, int numberCards)
{
if (numberCards != -1)
append(tr("%1 is looking at the top %2 cards %3.").arg(sanitizeHtml(player->getName())).arg(numberCards).arg(trZoneName(zone, zoneOwner, zoneOwner == player, CaseGenitive)));
append(tr("%1 is looking at the top %2 cards %3.").arg(sanitizeHtml(player->getName())).arg(numberCards).arg(zone->getTranslatedName(zone->getPlayer() == player, CaseGenitive)));
else
append(tr("%1 is looking at %2.").arg(sanitizeHtml(player->getName())).arg(trZoneName(zone, zoneOwner, zoneOwner == player, CaseAccusative)));
append(tr("%1 is looking at %2.").arg(sanitizeHtml(player->getName())).arg(zone->getTranslatedName(zone->getPlayer() == player, CaseAccusative)));
}
void MessageLogWidget::logStopDumpZone(Player *player, CardZone *zone, Player *zoneOwner)
void MessageLogWidget::logStopDumpZone(Player *player, CardZone *zone)
{
QString zoneName = trZoneName(zone, zoneOwner, zoneOwner == player, CaseAccusative);
QString zoneName = zone->getTranslatedName(zone->getPlayer() == player, CaseAccusative);
append(tr("%1 stops looking at %2.").arg(sanitizeHtml(player->getName())).arg(zoneName));
}
@ -283,8 +260,8 @@ void MessageLogWidget::connectToGame(Game *game)
connect(game, SIGNAL(logSetTapped(Player *, QString, bool)), this, SLOT(logSetTapped(Player *, QString, bool)));
connect(game, SIGNAL(logSetCounter(Player *, QString, int, int)), this, SLOT(logSetCounter(Player *, QString, int, int)));
connect(game, SIGNAL(logSetDoesntUntap(Player *, QString, bool)), this, SLOT(logSetDoesntUntap(Player *, QString, bool)));
connect(game, SIGNAL(logDumpZone(Player *, CardZone *, Player *, int)), this, SLOT(logDumpZone(Player *, CardZone *, Player *, int)));
connect(game, SIGNAL(logStopDumpZone(Player *, CardZone *, Player *)), this, SLOT(logStopDumpZone(Player *, CardZone *, Player *)));
connect(game, SIGNAL(logDumpZone(Player *, CardZone *, int)), this, SLOT(logDumpZone(Player *, CardZone *, int)));
connect(game, SIGNAL(logStopDumpZone(Player *, CardZone *)), this, SLOT(logStopDumpZone(Player *, CardZone *)));
connect(game, SIGNAL(logSetActivePlayer(Player *)), this, SLOT(logSetActivePlayer(Player *)));
connect(game, SIGNAL(setActivePhase(int)), this, SLOT(logSetActivePhase(int)));

View file

@ -4,6 +4,7 @@
#include <QPlainTextEdit>
#include <QAbstractSocket>
#include "client.h"
#include "translation.h"
class Game;
class Player;
@ -12,7 +13,6 @@ class CardZone;
class MessageLogWidget : public QTextEdit {
Q_OBJECT
private:
enum GrammaticalCase { /*CaseNominative, */CaseGenitive, CaseAccusative };
QString sanitizeHtml(QString dirty) const;
QString trZoneName(CardZone *zone, Player *player, bool hisOwn, GrammaticalCase gc) const;
public slots:
@ -37,8 +37,8 @@ private slots:
void logSetTapped(Player *player, QString cardName, bool tapped);
void logSetCounter(Player *player, QString counterName, int value, int oldValue);
void logSetDoesntUntap(Player *player, QString cardName, bool doesntUntap);
void logDumpZone(Player *player, CardZone *zone, Player *zoneOwner, int numberCards);
void logStopDumpZone(Player *player, CardZone *zone, Player *zoneOwner);
void logDumpZone(Player *player, CardZone *zone, int numberCards);
void logStopDumpZone(Player *player, CardZone *zone);
void logSetActivePlayer(Player *player);
void logSetActivePhase(int phase);
void msgAlert();

View file

@ -3,7 +3,7 @@
#include "cardzone.h"
class TableZone : public QObject, public CardZone {
class TableZone : public CardZone {
Q_OBJECT
signals:
void sizeChanged();

View file

@ -0,0 +1,6 @@
#ifndef TRANSLATION_H
#define TRANSLATION_H
enum GrammaticalCase { CaseNominative, CaseGenitive, CaseAccusative };
#endif

View file

@ -215,6 +215,7 @@ void MainWindow::retranslateUi()
gameSelector->retranslateUi();
if (game)
game->retranslateUi();
zoneLayout->retranslateUi();
}
void MainWindow::createActions()

View file

@ -20,14 +20,17 @@ void ZoneViewLayout::reorganize()
qreal x, y;
views.at(0)->getWindowFrameMargins(&x, &y, 0, 0);
qreal totalWidth = x;
qreal totalHeight = 0;
for (int i = 0; i < views.size(); i++) {
QRectF viewSize = views.at(i)->windowFrameRect();
qreal w = viewSize.right() - viewSize.left();
// qreal h = viewSize.bottom() - viewSize.top();
qreal h = viewSize.bottom() - viewSize.top();
views.at(i)->setPos(totalWidth, y);
totalWidth += w;
if (h > totalHeight)
totalHeight = h;
}
resize(totalWidth, scene()->sceneRect().height());
resize(totalWidth, totalHeight);
emit sizeChanged();
}
@ -45,6 +48,7 @@ void ZoneViewLayout::toggleZoneView(Player *player, const QString &zoneName, int
ZoneViewWidget *item = new ZoneViewWidget(db, player, player->getZones()->findZone(zoneName), numberCards, this);
views.append(item);
connect(item, SIGNAL(closePressed(ZoneViewWidget *)), this, SLOT(removeItem(ZoneViewWidget *)));
connect(item, SIGNAL(sizeChanged()), this, SLOT(reorganize()));
reorganize();
}
@ -69,6 +73,12 @@ void ZoneViewLayout::closeMostRecentZoneView()
void ZoneViewLayout::clear()
{
for (int i = views.size() - 1; i >= 0; i--)
for (int i = views.size() - 1; i >= 0; --i)
views.at(i)->close();
}
void ZoneViewLayout::retranslateUi()
{
for (int i = views.size() - 1; i >= 0; --i)
views.at(i)->retranslateUi();
}

View file

@ -17,12 +17,13 @@ private:
CardDatabase *db;
public:
ZoneViewLayout(CardDatabase *_db, QGraphicsItem *parent = 0);
void reorganize();
void retranslateUi();
public slots:
void toggleZoneView(Player *player, const QString &zoneName, int numberCards = 0);
void removeItem(ZoneViewWidget *item);
void removeItem(ZoneViewZone *item);
void closeMostRecentZoneView();
void reorganize();
void clear();
};

View file

@ -5,62 +5,82 @@
#include "zoneviewzone.h"
#include "player.h"
#include "client.h"
#include "gamescene.h"
ZoneViewWidget::ZoneViewWidget(CardDatabase *_db, Player *_player, CardZone *_origZone, int numberCards, QGraphicsItem *parent)
: QGraphicsWidget(parent, Qt::Tool | Qt::CustomizeWindowHint | Qt::WindowSystemMenuHint | Qt::WindowTitleHint/* | Qt::WindowCloseButtonHint*/), db(_db), player(_player)
{
setWindowTitle(QString("%1's %2").arg(player->getName()).arg(_origZone->getName()));
setAttribute(Qt::WA_DeleteOnClose);
QFont font;
font.setPixelSize(8);
setFont(font);
qreal y = 10;
QGraphicsLinearLayout *vbox = new QGraphicsLinearLayout(Qt::Vertical);
setLayout(vbox);
sortCheckBox = new QCheckBox;
QGraphicsProxyWidget *sortProxy = new QGraphicsProxyWidget;
sortProxy->setWidget(sortCheckBox);
vbox->addItem(sortProxy);
if (_origZone->getIsShufflable() && (numberCards == -1)) {
shuffleCheckBox = new QCheckBox("shuffle when closing");
shuffleCheckBox = new QCheckBox;
shuffleCheckBox->setChecked(true);
QGraphicsProxyWidget *shuffleProxy = new QGraphicsProxyWidget(this);
QGraphicsProxyWidget *shuffleProxy = new QGraphicsProxyWidget;
shuffleProxy->setWidget(shuffleCheckBox);
y += shuffleProxy->y() + shuffleProxy->size().height();
vbox->addItem(shuffleProxy);
} else
shuffleCheckBox = 0;
qreal left, top, right, bottom;
getWindowFrameMargins(&left, &top, &right, &bottom);
qreal h = scene()->sceneRect().height() - (top + bottom);
scrollBar = new QScrollBar(Qt::Vertical);
/* scrollBar = new QScrollBar(Qt::Vertical);
QGraphicsProxyWidget *scrollProxy = new QGraphicsProxyWidget(this);
scrollProxy->setWidget(scrollBar);
scrollProxy->setPos(138, y);
scrollProxy->resize(scrollProxy->size().width(), h - y);
qreal w = 138 + scrollProxy->size().width();
*/qreal w = 138;
resize(w, h);
setMinimumSize(w, h);
setMaximumSize(w, h);
zone = new ZoneViewZone(player, _origZone, numberCards, this);
zone->setPos(3, y);
zone->setHeight((int) (h - y));
if (!zone->initializeCards()) {
connect(player->client, SIGNAL(zoneDumpReceived(int, QList<ServerZoneCard *>)), this, SLOT(zoneDumpReceived(int, QList<ServerZoneCard *>)));
PendingCommand *dumpZoneCommand = player->client->dumpZone(player->getId(), _origZone->getName(), numberCards);
cmdId = dumpZoneCommand->getMsgId();
}
connect(sortCheckBox, SIGNAL(stateChanged(int)), zone, SLOT(setSortingEnabled(int)));
connect(zone, SIGNAL(contentsChanged()), this, SLOT(resizeToZoneContents()));
zone->dumpObjectInfo();
vbox->addItem(zone);
zone->initializeCards();
QSettings settings;
sortCheckBox->setChecked(settings.value("zoneview/sorting").toInt());
retranslateUi();
}
void ZoneViewWidget::zoneDumpReceived(int commandId, QList<ServerZoneCard *> cards)
void ZoneViewWidget::retranslateUi()
{
if (commandId != cmdId)
return;
setWindowTitle(zone->getTranslatedName(false, CaseNominative));
sortCheckBox->setText(tr("sort alphabetically"));
if (shuffleCheckBox)
shuffleCheckBox->setText(tr("shuffle when closing"));
}
for (int i = 0; i < cards.size(); i++) {
ServerZoneCard *temp = cards[i];
CardItem *card = new CardItem(db, temp->getName(), i, zone);
zone->addCard(card, false, i);
delete temp;
}
zone->reorganizeCards();
void ZoneViewWidget::resizeToZoneContents()
{
qDebug("+++++++ bla");
int cardCount = zone->getCards().size();
const QRectF &playersRect = static_cast<GameScene *>(scene())->getPlayersRect();
int h = 0;
if (cardCount * CARD_HEIGHT / 5 < playersRect.height() * 1.5)
h = cardCount * CARD_HEIGHT / 5;
else
h = playersRect.height() * 1.5;
qDebug(QString("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXx resizing to %1").arg(h).toLatin1());
resize(size().width(), h);
emit sizeChanged();
}
void ZoneViewWidget::closeEvent(QCloseEvent *event)

View file

@ -17,20 +17,21 @@ class ZoneViewWidget : public QGraphicsWidget {
Q_OBJECT
private:
ZoneViewZone *zone;
int cmdId;
QScrollBar *scrollBar;
QCheckBox *shuffleCheckBox;
QCheckBox *sortCheckBox, *shuffleCheckBox;
CardDatabase *db;
Player *player;
signals:
void closePressed(ZoneViewWidget *zv);
void sizeChanged();
private slots:
void zoneDumpReceived(int commandId, QList<ServerZoneCard *> cards);
void resizeToZoneContents();
public:
ZoneViewWidget(CardDatabase *_db, Player *_player, CardZone *_origZone, int numberCards = 0, QGraphicsItem *parent = 0);
ZoneViewZone *getZone() const { return zone; }
void retranslateUi();
protected:
void closeEvent(QCloseEvent *event);
};

View file

@ -4,7 +4,7 @@
#include "client.h"
ZoneViewZone::ZoneViewZone(Player *_p, CardZone *_origZone, int _numberCards, QGraphicsItem *parent)
: CardZone(_p, _origZone->getName(), false, false, true, parent, true), height(0), numberCards(_numberCards), origZone(_origZone)
: CardZone(_p, _origZone->getName(), false, false, true, parent, true), height(0), numberCards(_numberCards), origZone(_origZone), sortingEnabled(false)
{
origZone->setView(this);
}
@ -24,19 +24,40 @@ void ZoneViewZone::paint(QPainter */*painter*/, const QStyleOptionGraphicsItem *
{
}
bool ZoneViewZone::initializeCards()
void ZoneViewZone::initializeCards()
{
if (!origZone->contentsKnown())
return false;
const CardList &c = origZone->getCards();
int number = numberCards == -1 ? c.size() : (numberCards < c.size() ? numberCards : c.size());
for (int i = 0; i < number; i++) {
CardItem *card = c.at(i);
addCard(new CardItem(player->getDb(), card->getName(), card->getId(), this), false, i);
if (!origZone->contentsKnown()) {
connect(player->client, SIGNAL(zoneDumpReceived(int, QList<ServerZoneCard *>)), this, SLOT(zoneDumpReceived(int, QList<ServerZoneCard *>)));
PendingCommand *dumpZoneCommand = player->client->dumpZone(player->getId(), name, numberCards);
cmdId = dumpZoneCommand->getMsgId();
} else {
const CardList &c = origZone->getCards();
int number = numberCards == -1 ? c.size() : (numberCards < c.size() ? numberCards : c.size());
for (int i = 0; i < number; i++) {
CardItem *card = c.at(i);
addCard(new CardItem(player->getDb(), card->getName(), card->getId(), this), false, i);
}
emit contentsChanged();
reorganizeCards();
}
}
void ZoneViewZone::zoneDumpReceived(int commandId, QList<ServerZoneCard *> cards)
{
if (commandId != cmdId)
return;
for (int i = 0; i < cards.size(); i++) {
ServerZoneCard *temp = cards[i];
CardItem *card = new CardItem(player->getDb(), temp->getName(), i, this);
addCard(card, false, i);
delete temp;
}
emit contentsChanged();
reorganizeCards();
return true;
}
// Because of boundingRect(), this function must not be called before the zone was added to a scene.
@ -48,15 +69,23 @@ void ZoneViewZone::reorganizeCards()
return;
int cardCount = cards.size();
if (!origZone->contentsKnown())
for (int i = 0; i < cardCount; ++i)
cards[i]->setId(i);
qreal totalWidth = boundingRect().width();
qreal totalHeight = boundingRect().height();
qreal cardWidth = cards.at(0)->boundingRect().width();
qreal cardHeight = cards.at(0)->boundingRect().height();
qreal x1 = 0;
qreal x2 = (totalWidth - cardWidth);
CardList cardsToDisplay(cards);
if (sortingEnabled)
cardsToDisplay.sort();
for (int i = 0; i < cardCount; i++) {
CardItem *c = cards.at(i);
CardItem *c = cardsToDisplay.at(i);
qreal x = i % 2 ? x2 : x1;
// If the total height of the cards is smaller than the available height,
// the cards do not need to overlap and are displayed in the center of the area.
@ -64,12 +93,16 @@ void ZoneViewZone::reorganizeCards()
c->setPos(x, ((qreal) i) * (totalHeight - cardHeight) / (cardCount - 1));
else
c->setPos(x, ((qreal) i) * cardHeight + (totalHeight - cardCount * cardHeight) / 2);
if (!origZone->contentsKnown())
c->setId(i);
c->setZValue(i);
}
}
void ZoneViewZone::setSortingEnabled(int _sortingEnabled)
{
sortingEnabled = _sortingEnabled;
reorganizeCards();
}
void ZoneViewZone::addCardImpl(CardItem *card, int x, int /*y*/)
{
cards.insert(x, card);
@ -92,3 +125,20 @@ void ZoneViewZone::removeCard(int position)
delete card;
reorganizeCards();
}
void ZoneViewZone::setGeometry(const QRectF &rect)
{
setPos(rect.topLeft());
height = rect.height();
reorganizeCards();
}
QSizeF ZoneViewZone::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const
{
switch (which) {
case Qt::MinimumSize: return QSizeF(1.75 * CARD_WIDTH, 2 * CARD_HEIGHT);
case Qt::PreferredSize: return QSizeF(1.75 * CARD_WIDTH, constraint.height());
case Qt::MaximumSize: return QSizeF(1.75 * CARD_WIDTH, constraint.height());
default: return QSizeF();
}
}

View file

@ -3,29 +3,37 @@
#include "cardzone.h"
#include "serverzonecard.h"
#include <QGraphicsWidget>
#include <QGraphicsLayoutItem>
class ZoneViewWidget;
class ZoneViewZone : public CardZone {
class ZoneViewZone : public CardZone, public QGraphicsLayoutItem {
Q_OBJECT
private:
int height;
int numberCards;
void handleDropEvent(int cardId, CardZone *startZone, const QPoint &dropPoint, bool faceDown);
CardZone *origZone;
signals:
void removeZoneViewWidget(ZoneViewWidget *zv);
bool sortingEnabled;
int cmdId;
public:
ZoneViewZone(Player *_p, CardZone *_origZone, int _numberCards = -1, QGraphicsItem *parent = 0);
~ZoneViewZone();
QRectF boundingRect() const;
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
void reorganizeCards();
bool initializeCards();
void initializeCards();
void removeCard(int position);
void setHeight(int _height) { height = _height; }
void setGeometry(const QRectF &rect);
int getNumberCards() const { return numberCards; }
public slots:
void setSortingEnabled(int _sortingEnabled);
private slots:
void zoneDumpReceived(int commandId, QList<ServerZoneCard *> cards);
protected:
void addCardImpl(CardItem *card, int x, int y);
QSizeF sizeHint(Qt::SizeHint which, const QSizeF &constraint = QSizeF()) const;
};
#endif