From cf95e5f328aceeaf18cbf44b96d309c2279bafd0 Mon Sep 17 00:00:00 2001 From: Max-Wilhelm Bruker Date: Fri, 15 Oct 2010 19:47:30 +0200 Subject: [PATCH] Added card hover + card info widget popup to MessageLogWidget --- cockatrice/src/abstractcarditem.cpp | 13 ++---- cockatrice/src/abstractcarditem.h | 2 + cockatrice/src/messagelogwidget.cpp | 62 ++++++++++++++++++++++++++++- cockatrice/src/messagelogwidget.h | 17 +++++++- cockatrice/src/tab_game.cpp | 29 +++++++++++++- cockatrice/src/tab_game.h | 3 ++ 6 files changed, 112 insertions(+), 14 deletions(-) diff --git a/cockatrice/src/abstractcarditem.cpp b/cockatrice/src/abstractcarditem.cpp index 908c7eaa..b9058718 100644 --- a/cockatrice/src/abstractcarditem.cpp +++ b/cockatrice/src/abstractcarditem.cpp @@ -183,21 +183,14 @@ void AbstractCardItem::mousePressEvent(QGraphicsSceneMouseEvent *event) } if (event->button() == Qt::LeftButton) setCursor(Qt::ClosedHandCursor); - else if (event->button() == Qt::MidButton) { - infoWidget = new CardInfoWidget(CardInfoWidget::ModePopUp, 0, Qt::Widget | Qt::FramelessWindowHint | Qt::X11BypassWindowManagerHint | Qt::WindowStaysOnTopHint); - infoWidget->setCard(this); - infoWidget->move(event->screenPos().x() - infoWidget->width() / 2, event->screenPos().y() - infoWidget->height() / 2); - infoWidget->show(); - } + else if (event->button() == Qt::MidButton) + emit showCardInfoPopup(event->screenPos(), name); event->accept(); } void AbstractCardItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) { - if (infoWidget) { - infoWidget->deleteLater(); - infoWidget = 0; - } + emit deleteCardInfoPopup(); } void AbstractCardItem::processHoverEvent() diff --git a/cockatrice/src/abstractcarditem.h b/cockatrice/src/abstractcarditem.h index d661f6c9..493f4dc6 100644 --- a/cockatrice/src/abstractcarditem.h +++ b/cockatrice/src/abstractcarditem.h @@ -27,6 +27,8 @@ private slots: void pixmapUpdated(); signals: void hovered(AbstractCardItem *card); + void showCardInfoPopup(QPoint pos, QString cardName); + void deleteCardInfoPopup(); public: enum { Type = typeCard }; int type() const { return Type; } diff --git a/cockatrice/src/messagelogwidget.cpp b/cockatrice/src/messagelogwidget.cpp index b77324e2..03e99d04 100644 --- a/cockatrice/src/messagelogwidget.cpp +++ b/cockatrice/src/messagelogwidget.cpp @@ -1,7 +1,10 @@ #include "messagelogwidget.h" #include "player.h" #include "cardzone.h" -#include +#include "cardinfowidget.h" +#include +#include +#include QString MessageLogWidget::sanitizeHtml(QString dirty) const { @@ -227,7 +230,7 @@ void MessageLogWidget::logUnattachCard(Player *player, QString cardName) void MessageLogWidget::logCreateToken(Player *player, QString cardName, QString pt) { - append(tr("%1 creates token: %2%3.").arg(sanitizeHtml(player->getName())).arg(QString("%1").arg(sanitizeHtml(cardName))).arg(pt.isEmpty() ? QString() : QString(" (%1)").arg(sanitizeHtml(pt)))); + append(tr("%1 creates token: %2%3.").arg(sanitizeHtml(player->getName())).arg(QString("%1").arg(sanitizeHtml(cardName))).arg(pt.isEmpty() ? QString() : QString(" (%1)").arg(sanitizeHtml(pt)))); } void MessageLogWidget::logCreateArrow(Player *player, Player *startPlayer, QString startCard, Player *targetPlayer, QString targetCard, bool playerTarget) @@ -375,3 +378,58 @@ MessageLogWidget::MessageLogWidget(QWidget *parent) f.setPixelSize(11); setFont(f); } + +void MessageLogWidget::enterEvent(QEvent *event) +{ + setMouseTracking(true); +} + +void MessageLogWidget::leaveEvent(QEvent *event) +{ + setMouseTracking(false); +} + +QString MessageLogWidget::getCardNameUnderMouse(const QPoint &pos) const +{ + QTextCursor cursor(cursorForPosition(pos)); + QTextBlock block(cursor.block()); + QTextBlock::iterator it; + for (it = block.begin(); !(it.atEnd()); ++it) { + QTextFragment frag = it.fragment(); + if (!frag.contains(cursor.position())) + continue; + + if (frag.charFormat().foreground().color() == Qt::blue) + return frag.text(); + + break; + } + return QString(); +} + +void MessageLogWidget::mouseMoveEvent(QMouseEvent *event) +{ + QString cardName = getCardNameUnderMouse(event->pos()); + if (!cardName.isEmpty()) + emit cardNameHovered(cardName); + + QTextEdit::mouseMoveEvent(event); +} + +void MessageLogWidget::mousePressEvent(QMouseEvent *event) +{ + if (event->button() == Qt::MidButton) { + QString cardName = getCardNameUnderMouse(event->pos()); + if (!cardName.isEmpty()) + emit showCardInfoPopup(event->globalPos(), cardName); + } + + QTextEdit::mousePressEvent(event); +} + +void MessageLogWidget::mouseReleaseEvent(QMouseEvent *event) +{ + emit deleteCardInfoPopup(); + + QTextEdit::mouseReleaseEvent(event); +} diff --git a/cockatrice/src/messagelogwidget.h b/cockatrice/src/messagelogwidget.h index b535a5dc..0640daa0 100644 --- a/cockatrice/src/messagelogwidget.h +++ b/cockatrice/src/messagelogwidget.h @@ -1,19 +1,28 @@ #ifndef MESSAGELOGWIDGET_H #define MESSAGELOGWIDGET_H -#include +#include #include #include "translation.h" #include "protocol_datastructures.h" class Player; class CardZone; +class QMouseEvent; +class QEvent; +class CardInfoWidget; class MessageLogWidget : public QTextEdit { Q_OBJECT private: + CardInfoWidget *infoWidget; QString sanitizeHtml(QString dirty) const; QString trZoneName(CardZone *zone, Player *player, bool hisOwn, GrammaticalCase gc) const; + QString getCardNameUnderMouse(const QPoint &pos) const; +signals: + void cardNameHovered(QString cardName); + void showCardInfoPopup(QPoint pos, QString cardName); + void deleteCardInfoPopup(); public slots: void logConnecting(QString hostname); void logConnected(); @@ -58,6 +67,12 @@ public slots: public: void connectToPlayer(Player *player); MessageLogWidget(QWidget *parent = 0); +protected: + void enterEvent(QEvent *event); + void leaveEvent(QEvent *event); + void mouseMoveEvent(QMouseEvent *event); + void mousePressEvent(QMouseEvent *event); + void mouseReleaseEvent(QMouseEvent *event); }; #endif diff --git a/cockatrice/src/tab_game.cpp b/cockatrice/src/tab_game.cpp index 889e68c9..8405719f 100644 --- a/cockatrice/src/tab_game.cpp +++ b/cockatrice/src/tab_game.cpp @@ -4,6 +4,8 @@ #include #include #include +#include +#include #include "tab_game.h" #include "cardinfowidget.h" #include "playerlistwidget.h" @@ -158,7 +160,7 @@ void DeckViewContainer::setDeck(DeckList *deck) } TabGame::TabGame(QList &_clients, int _gameId, const QString &_gameDescription, int _localPlayerId, bool _spectator, bool _spectatorsCanTalk, bool _spectatorsSeeEverything, bool _resuming) - : Tab(), clients(_clients), gameId(_gameId), gameDescription(_gameDescription), localPlayerId(_localPlayerId), spectator(_spectator), spectatorsCanTalk(_spectatorsCanTalk), spectatorsSeeEverything(_spectatorsSeeEverything), started(false), resuming(_resuming), currentPhase(-1) + : Tab(), clients(_clients), gameId(_gameId), gameDescription(_gameDescription), localPlayerId(_localPlayerId), spectator(_spectator), spectatorsCanTalk(_spectatorsCanTalk), spectatorsSeeEverything(_spectatorsSeeEverything), started(false), resuming(_resuming), currentPhase(-1), infoPopup(0) { scene = new GameScene(this); gameView = new GameView(scene); @@ -168,6 +170,9 @@ TabGame::TabGame(QList &_clients, int _gameId, const QString & playerListWidget = new PlayerListWidget; playerListWidget->setFocusPolicy(Qt::NoFocus); messageLog = new MessageLogWidget; + connect(messageLog, SIGNAL(cardNameHovered(QString)), cardInfo, SLOT(setCard(QString))); + connect(messageLog, SIGNAL(showCardInfoPopup(QPoint, QString)), this, SLOT(showCardInfoPopup(QPoint, QString))); + connect(messageLog, SIGNAL(deleteCardInfoPopup()), this, SLOT(deleteCardInfoPopup())); sayLabel = new QLabel; sayEdit = new QLineEdit; sayLabel->setBuddy(sayEdit); @@ -649,6 +654,8 @@ void TabGame::eventPing(Event_Ping *event, GameEventContext * /*context*/) void TabGame::newCardAdded(AbstractCardItem *card) { connect(card, SIGNAL(hovered(AbstractCardItem *)), cardInfo, SLOT(setCard(AbstractCardItem *))); + connect(card, SIGNAL(showCardInfoPopup(QPoint, QString)), this, SLOT(showCardInfoPopup(QPoint, QString))); + connect(card, SIGNAL(deleteCardInfoPopup()), this, SLOT(deleteCardInfoPopup())); } CardItem *TabGame::getCard(int playerId, const QString &zoneName, int cardId) const @@ -680,3 +687,23 @@ Player *TabGame::getActiveLocalPlayer() const return 0; } + +void TabGame::showCardInfoPopup(const QPoint &pos, const QString &cardName) +{ + infoPopup = new CardInfoWidget(CardInfoWidget::ModePopUp, 0, Qt::Widget | Qt::FramelessWindowHint | Qt::X11BypassWindowManagerHint | Qt::WindowStaysOnTopHint); + infoPopup->setCard(cardName); + QRect screenRect = qApp->desktop()->screenGeometry(this); + infoPopup->move( + qMax(screenRect.left(), qMin(pos.x() - infoPopup->width() / 2, screenRect.left() + screenRect.width() - infoPopup->width())), + qMax(screenRect.top(), qMin(pos.y() - infoPopup->height() / 2, screenRect.top() + screenRect.height() - infoPopup->height())) + ); + infoPopup->show(); +} + +void TabGame::deleteCardInfoPopup() +{ + if (infoPopup) { + infoPopup->deleteLater(); + infoPopup = 0; + } +} diff --git a/cockatrice/src/tab_game.h b/cockatrice/src/tab_game.h index 23f85818..802f847e 100644 --- a/cockatrice/src/tab_game.h +++ b/cockatrice/src/tab_game.h @@ -96,6 +96,7 @@ private: int currentPhase; int activePlayer; + CardInfoWidget *infoPopup; CardInfoWidget *cardInfo; PlayerListWidget *playerListWidget; MessageLogWidget *messageLog; @@ -134,6 +135,8 @@ signals: void gameClosing(TabGame *tab); private slots: void newCardAdded(AbstractCardItem *card); + void showCardInfoPopup(const QPoint &pos, const QString &cardName); + void deleteCardInfoPopup(); void actConcede(); void actLeaveGame();