diff --git a/cockatrice/src/gameselector.cpp b/cockatrice/src/gameselector.cpp new file mode 100644 index 00000000..52ae2592 --- /dev/null +++ b/cockatrice/src/gameselector.cpp @@ -0,0 +1,78 @@ +#include +#include "gameselector.h" +#include "dlg_creategame.h" + +GameSelector::GameSelector(Client *_client, QWidget *parent) + : QWidget(parent), client(_client) +{ + gameListView = new QTreeView; + gameListModel = new GamesModel(this); + gameListView->setModel(gameListModel); + + createButton = new QPushButton(tr("&Create")); + joinButton = new QPushButton(tr("&Join")); + QHBoxLayout *buttonLayout = new QHBoxLayout; + buttonLayout->addStretch(); + buttonLayout->addWidget(createButton); + buttonLayout->addWidget(joinButton); + + QVBoxLayout *mainLayout = new QVBoxLayout; + mainLayout->addWidget(gameListView); + mainLayout->addLayout(buttonLayout); + + setLayout(mainLayout); + + setMinimumWidth(gameListView->columnWidth(0) * gameListModel->columnCount()); + setMinimumHeight(400); + + connect(createButton, SIGNAL(clicked()), this, SLOT(actCreate())); + connect(joinButton, SIGNAL(clicked()), this, SLOT(actJoin())); + + connect(client, SIGNAL(gameListEvent(ServerGame *)), gameListModel, SLOT(updateGameList(ServerGame *))); + client->listGames(); +} + +void GameSelector::actCreate() +{ + DlgCreateGame dlg(client, this); + if (dlg.exec()) + deleteLater(); +} + +void GameSelector::actRefresh() +{ + client->listGames(); +} + +void GameSelector::checkResponse(ServerResponse response) +{ + createButton->setEnabled(true); + joinButton->setEnabled(true); + + if (response == RespOk) + deleteLater(); + else { + QMessageBox::critical(this, tr("Error"), tr("XXX")); + return; + } +} + +void GameSelector::actJoin() +{ + QModelIndex ind = gameListView->currentIndex(); + if (!ind.isValid()) + return; + ServerGame *game = gameListModel->getGame(ind.row()); + QString password; + if (game->getHasPassword()) { + bool ok; + password = QInputDialog::getText(this, tr("Join game"), tr("Password:"), QLineEdit::Password, QString(), &ok); + if (!ok) + return; + } + + PendingCommand *joinCommand = client->joinGame(game->getGameId(), password); + connect(joinCommand, SIGNAL(finished(ServerResponse)), this, SLOT(checkResponse(ServerResponse))); + createButton->setEnabled(false); + joinButton->setEnabled(false); +} diff --git a/cockatrice/src/gameselector.h b/cockatrice/src/gameselector.h new file mode 100644 index 00000000..35dd463e --- /dev/null +++ b/cockatrice/src/gameselector.h @@ -0,0 +1,30 @@ +#ifndef GAMESELECTOR_H +#define GAMESELECTOR_H + +#include +#include "gamesmodel.h" +#include "servergame.h" +#include "client.h" + +class QPushButton; +class QTreeView; + +class GameSelector : public QWidget { + Q_OBJECT +public: + GameSelector(Client *_client, QWidget *parent = 0); +private slots: + void actCreate(); + void actRefresh(); + void actJoin(); + void checkResponse(ServerResponse response); +private: + Client *client; + + QTreeView *gameListView; + GamesModel *gameListModel; + QPushButton *createButton, *joinButton; +}; + +#endif + diff --git a/cockatrice/src/pilezone.cpp b/cockatrice/src/pilezone.cpp new file mode 100644 index 00000000..8af98a6a --- /dev/null +++ b/cockatrice/src/pilezone.cpp @@ -0,0 +1,84 @@ +#include +#include "pilezone.h" +#include "player.h" +#include "client.h" +#include "carddragitem.h" +#include "zoneviewzone.h" + +PileZone::PileZone(Player *_p, const QString &_name, QGraphicsItem *parent) + : CardZone(_p, _name, false, false, parent) +{ + cards = new CardList(true); + setCacheMode(DeviceCoordinateCache); // Do not move this line to the parent constructor! + setCursor(Qt::OpenHandCursor); +} + +QRectF PileZone::boundingRect() const +{ + return QRectF(0, 0, CARD_WIDTH, CARD_HEIGHT); +} + +void PileZone::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) +{ + if (!cards->isEmpty()) { + painter->save(); + cards->at(0)->paint(painter, option, widget); + painter->restore(); + } + + paintCardNumberEllipse(painter); + painter->drawRect(QRectF(0.5, 0.5, CARD_WIDTH - 1, CARD_HEIGHT - 1)); +} + +void PileZone::addCardImpl(CardItem *card, int x, int /*y*/) +{ + cards->insert(x, card); + card->setPos(0, 0); + card->setVisible(false); + card->resetState(); + card->setParentItem(this); +} + +void PileZone::handleDropEvent(int cardId, CardZone *startZone, const QPoint &/*dropPoint*/, bool /*faceDown*/) +{ + player->client->moveCard(cardId, startZone->getName(), getName(), 0, 0); +} + +void PileZone::reorganizeCards() +{ + qDebug(QString("PileZone: reorganize, x=%1, y=%2, w=%3, h=%4").arg(boundingRect().x()).arg(boundingRect().y()).arg(boundingRect().width()).arg(boundingRect().height()).toLatin1()); + update(boundingRect()); +} + +void PileZone::mousePressEvent(QGraphicsSceneMouseEvent *event) +{ + CardZone::mousePressEvent(event); + if (event->isAccepted()) + return; + + if (event->button() == Qt::LeftButton) { + setCursor(Qt::ClosedHandCursor); + event->accept(); + } else + event->ignore(); +} + +void PileZone::mouseMoveEvent(QGraphicsSceneMouseEvent *event) +{ + if ((event->screenPos() - event->buttonDownScreenPos(Qt::LeftButton)).manhattanLength() < QApplication::startDragDistance()) + return; + + if (cards->empty()) + return; + + bool faceDown = event->modifiers().testFlag(Qt::ShiftModifier); + CardItem *card = cards->at(0); + CardDragItem *drag = card->createDragItem(card->getId(), event->pos(), event->scenePos(), faceDown); + drag->grabMouse(); + setCursor(Qt::OpenHandCursor); +} + +void PileZone::mouseReleaseEvent(QGraphicsSceneMouseEvent */*event*/) +{ + setCursor(Qt::OpenHandCursor); +} diff --git a/cockatrice/src/pilezone.h b/cockatrice/src/pilezone.h new file mode 100644 index 00000000..14c1045c --- /dev/null +++ b/cockatrice/src/pilezone.h @@ -0,0 +1,21 @@ +#ifndef PILEZONE_H +#define PILEZONE_H + +#include "cardzone.h" + +class PileZone : public CardZone { +private: +public: + PileZone(Player *_p, const QString &_name, QGraphicsItem *parent = 0); + QRectF boundingRect() const; + void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); + void reorganizeCards(); + void handleDropEvent(int cardId, CardZone *startZone, const QPoint &dropPoint, bool faceDown); +protected: + void mousePressEvent(QGraphicsSceneMouseEvent *event); + void mouseMoveEvent(QGraphicsSceneMouseEvent *event); + void mouseReleaseEvent(QGraphicsSceneMouseEvent *event); + void addCardImpl(CardItem *card, int x, int y); +}; + +#endif