diff --git a/cockatrice/cockatrice.pro b/cockatrice/cockatrice.pro index 61a98550..6bf1d4ff 100644 --- a/cockatrice/cockatrice.pro +++ b/cockatrice/cockatrice.pro @@ -16,6 +16,7 @@ HEADERS += src/counter.h \ src/remoteclient.h \ src/window_main.h \ src/cardzone.h \ + src/selectzone.h \ src/player.h \ src/playertarget.h \ src/cardlist.h \ @@ -96,6 +97,7 @@ SOURCES += src/counter.cpp \ src/player.cpp \ src/playertarget.cpp \ src/cardzone.cpp \ + src/selectzone.cpp \ src/cardlist.cpp \ src/abstractcarditem.cpp \ src/carditem.cpp \ diff --git a/cockatrice/src/gamescene.cpp b/cockatrice/src/gamescene.cpp index e92fb820..d0c94cbf 100644 --- a/cockatrice/src/gamescene.cpp +++ b/cockatrice/src/gamescene.cpp @@ -195,3 +195,18 @@ bool GameScene::event(QEvent *event) } return QGraphicsScene::event(event); } + +void GameScene::startRubberBand(const QPointF &selectionOrigin) +{ + emit sigStartRubberBand(selectionOrigin); +} + +void GameScene::resizeRubberBand(const QPointF &cursorPoint) +{ + emit sigResizeRubberBand(cursorPoint); +} + +void GameScene::stopRubberBand() +{ + emit sigStopRubberBand(); +} diff --git a/cockatrice/src/gamescene.h b/cockatrice/src/gamescene.h index da35320e..67031fc2 100644 --- a/cockatrice/src/gamescene.h +++ b/cockatrice/src/gamescene.h @@ -23,6 +23,10 @@ public: void retranslateUi(); const QRectF &getPlayersRect() const { return playersRect; } void processViewSizeChange(const QSize &newSize); + + void startRubberBand(const QPointF &selectionOrigin); + void resizeRubberBand(const QPointF &cursorPoint); + void stopRubberBand(); public slots: void toggleZoneView(Player *player, const QString &zoneName, int numberCards); void addRevealedZoneView(Player *player, CardZone *zone, const QList &cardList); @@ -35,6 +39,10 @@ private slots: void rearrange(); protected: bool event(QEvent *event); +signals: + void sigStartRubberBand(const QPointF &selectionOrigin); + void sigResizeRubberBand(const QPointF &cursorPoint); + void sigStopRubberBand(); }; #endif diff --git a/cockatrice/src/gameview.cpp b/cockatrice/src/gameview.cpp index fd716827..7b13ae11 100644 --- a/cockatrice/src/gameview.cpp +++ b/cockatrice/src/gameview.cpp @@ -2,17 +2,21 @@ #include "gamescene.h" #include #include +#include GameView::GameView(QGraphicsScene *scene, QWidget *parent) - : QGraphicsView(scene, parent) + : QGraphicsView(scene, parent), rubberBand(0) { setBackgroundBrush(QBrush(QColor(0, 0, 0))); setRenderHints(QPainter::TextAntialiasing | QPainter::Antialiasing/* | QPainter::SmoothPixmapTransform*/); - setDragMode(RubberBandDrag); setFocusPolicy(Qt::NoFocus); setViewportUpdateMode(BoundingRectViewportUpdate); connect(scene, SIGNAL(sceneRectChanged(const QRectF &)), this, SLOT(updateSceneRect(const QRectF &))); + + connect(scene, SIGNAL(sigStartRubberBand(const QPointF &)), this, SLOT(startRubberBand(const QPointF &))); + connect(scene, SIGNAL(sigResizeRubberBand(const QPointF &)), this, SLOT(resizeRubberBand(const QPointF &))); + connect(scene, SIGNAL(sigStopRubberBand()), this, SLOT(stopRubberBand())); aCloseMostRecentZoneView = new QAction(this); aCloseMostRecentZoneView->setShortcut(tr("Esc")); @@ -35,3 +39,25 @@ void GameView::updateSceneRect(const QRectF &rect) qDebug(QString("updateSceneRect = %1,%2").arg(rect.width()).arg(rect.height()).toLatin1()); fitInView(rect, Qt::KeepAspectRatio); } + +void GameView::startRubberBand(const QPointF &_selectionOrigin) +{ + selectionOrigin = _selectionOrigin; + rubberBand = new QRubberBand(QRubberBand::Rectangle, this); + rubberBand->setGeometry(QRect(mapFromScene(selectionOrigin), QSize(0, 0))); + rubberBand->show(); +} + +void GameView::resizeRubberBand(const QPointF &cursorPoint) +{ + if (rubberBand) + rubberBand->setGeometry(QRect(mapFromScene(selectionOrigin), mapFromScene(cursorPoint)).normalized()); +} + +void GameView::stopRubberBand() +{ + if (rubberBand) { + rubberBand->deleteLater(); + rubberBand = 0; + } +} diff --git a/cockatrice/src/gameview.h b/cockatrice/src/gameview.h index c45e675e..84acf45e 100644 --- a/cockatrice/src/gameview.h +++ b/cockatrice/src/gameview.h @@ -3,12 +3,20 @@ #include +class QRubberBand; + class GameView : public QGraphicsView { Q_OBJECT private: QAction *aCloseMostRecentZoneView; + QRubberBand *rubberBand; + QPointF selectionOrigin; protected: void resizeEvent(QResizeEvent *event); +private slots: + void startRubberBand(const QPointF &selectionOrigin); + void resizeRubberBand(const QPointF &cursorPoint); + void stopRubberBand(); public slots: void updateSceneRect(const QRectF &rect); public: diff --git a/cockatrice/src/handzone.cpp b/cockatrice/src/handzone.cpp index d871c54d..d3d26924 100644 --- a/cockatrice/src/handzone.cpp +++ b/cockatrice/src/handzone.cpp @@ -5,7 +5,7 @@ #include "protocol_items.h" HandZone::HandZone(Player *_p, bool _contentsKnown, int _zoneHeight, QGraphicsItem *parent) - : CardZone(_p, "hand", false, false, _contentsKnown, parent), zoneHeight(_zoneHeight) + : SelectZone(_p, "hand", false, false, _contentsKnown, parent), zoneHeight(_zoneHeight) { connect(settingsCache, SIGNAL(handBgPathChanged()), this, SLOT(updateBgPixmap())); updateBgPixmap(); diff --git a/cockatrice/src/handzone.h b/cockatrice/src/handzone.h index 1392fa16..4a68e59c 100644 --- a/cockatrice/src/handzone.h +++ b/cockatrice/src/handzone.h @@ -1,9 +1,9 @@ #ifndef HANDZONE_H #define HANDZONE_H -#include "cardzone.h" +#include "selectzone.h" -class HandZone : public CardZone { +class HandZone : public SelectZone { Q_OBJECT private: qreal width, zoneHeight; diff --git a/cockatrice/src/selectzone.cpp b/cockatrice/src/selectzone.cpp new file mode 100644 index 00000000..fcc6017b --- /dev/null +++ b/cockatrice/src/selectzone.cpp @@ -0,0 +1,48 @@ +#include +#include "selectzone.h" +#include "gamescene.h" + +SelectZone::SelectZone(Player *_player, const QString &_name, bool _hasCardAttr, bool _isShufflable, bool _contentsKnown, QGraphicsItem *parent, bool isView) + : CardZone(_player, _name, _hasCardAttr, _isShufflable, _contentsKnown, parent, isView) +{ +} + +void SelectZone::mouseMoveEvent(QGraphicsSceneMouseEvent *event) +{ + if (event->buttons().testFlag(Qt::LeftButton)) { + QPointF pos = event->pos(); + if (pos.x() < 0) + pos.setX(0); + QRectF br = boundingRect(); + if (pos.x() > br.width()) + pos.setX(br.width()); + if (pos.y() < 0) + pos.setY(0); + if (pos.y() > br.height()) + pos.setY(br.height()); + + QRectF selectionRect = QRectF(selectionOrigin, pos).normalized(); + for (int i = 0; i < cards.size(); ++i) + cards[i]->setSelected(selectionRect.intersects(cards[i]->mapRectToParent(cards[i]->boundingRect()))); + + static_cast(scene())->resizeRubberBand(scenePos() + pos); + event->accept(); + } +} + +void SelectZone::mousePressEvent(QGraphicsSceneMouseEvent *event) +{ + if (event->button() == Qt::LeftButton) { + selectionOrigin = event->pos(); + static_cast(scene())->startRubberBand(event->scenePos()); + event->accept(); + } +} + +void SelectZone::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) +{ + selectionOrigin = QPoint(); + static_cast(scene())->stopRubberBand(); + event->accept(); +} + diff --git a/cockatrice/src/selectzone.h b/cockatrice/src/selectzone.h new file mode 100644 index 00000000..23e82d1c --- /dev/null +++ b/cockatrice/src/selectzone.h @@ -0,0 +1,18 @@ +#ifndef SELECTZONE_H +#define SELECTZONE_H + +#include "cardzone.h" + +class SelectZone : public CardZone { + Q_OBJECT +private: + QPointF selectionOrigin; +protected: + void mouseMoveEvent(QGraphicsSceneMouseEvent *event); + void mousePressEvent(QGraphicsSceneMouseEvent *event); + void mouseReleaseEvent(QGraphicsSceneMouseEvent *event); +public: + SelectZone(Player *_player, const QString &_name, bool _hasCardAttr, bool _isShufflable, bool _contentsKnown, QGraphicsItem *parent = 0, bool isView = false); +}; + +#endif diff --git a/cockatrice/src/stackzone.cpp b/cockatrice/src/stackzone.cpp index b164db1f..b21bd77f 100644 --- a/cockatrice/src/stackzone.cpp +++ b/cockatrice/src/stackzone.cpp @@ -7,7 +7,7 @@ #include "protocol_items.h" StackZone::StackZone(Player *_p, int _zoneHeight, QGraphicsItem *parent) - : CardZone(_p, "stack", false, false, true, parent), zoneHeight(_zoneHeight) + : SelectZone(_p, "stack", false, false, true, parent), zoneHeight(_zoneHeight) { connect(settingsCache, SIGNAL(stackBgPathChanged()), this, SLOT(updateBgPixmap())); updateBgPixmap(); diff --git a/cockatrice/src/stackzone.h b/cockatrice/src/stackzone.h index 7717a114..40d67541 100644 --- a/cockatrice/src/stackzone.h +++ b/cockatrice/src/stackzone.h @@ -1,9 +1,9 @@ #ifndef STACKZONE_H #define STACKZONE_H -#include "cardzone.h" +#include "selectzone.h" -class StackZone : public CardZone { +class StackZone : public SelectZone { Q_OBJECT private: qreal zoneHeight; diff --git a/cockatrice/src/tablezone.cpp b/cockatrice/src/tablezone.cpp index 4d52cf93..93e97616 100644 --- a/cockatrice/src/tablezone.cpp +++ b/cockatrice/src/tablezone.cpp @@ -9,7 +9,7 @@ #include "arrowitem.h" TableZone::TableZone(Player *_p, QGraphicsItem *parent) - : CardZone(_p, "table", true, false, true, parent), active(false) + : SelectZone(_p, "table", true, false, true, parent), active(false) { connect(settingsCache, SIGNAL(tableBgPathChanged()), this, SLOT(updateBgPixmap())); connect(settingsCache, SIGNAL(economicalGridChanged()), this, SLOT(reorganizeCards())); diff --git a/cockatrice/src/tablezone.h b/cockatrice/src/tablezone.h index 9f9f0650..1a3f63e4 100644 --- a/cockatrice/src/tablezone.h +++ b/cockatrice/src/tablezone.h @@ -1,9 +1,9 @@ #ifndef TABLEZONE_H #define TABLEZONE_H -#include "cardzone.h" +#include "selectzone.h" -class TableZone : public CardZone { +class TableZone : public SelectZone { Q_OBJECT signals: void sizeChanged(); diff --git a/cockatrice/src/zoneviewzone.cpp b/cockatrice/src/zoneviewzone.cpp index e7fc9e13..18166308 100644 --- a/cockatrice/src/zoneviewzone.cpp +++ b/cockatrice/src/zoneviewzone.cpp @@ -5,7 +5,7 @@ #include "protocol_items.h" ZoneViewZone::ZoneViewZone(Player *_p, CardZone *_origZone, int _numberCards, QGraphicsItem *parent) - : CardZone(_p, _origZone->getName(), false, false, true, parent, true), bRect(QRectF()), minRows(0), numberCards(_numberCards), origZone(_origZone), sortByName(false), sortByType(false) + : SelectZone(_p, _origZone->getName(), false, false, true, parent, true), bRect(QRectF()), minRows(0), numberCards(_numberCards), origZone(_origZone), sortByName(false), sortByType(false) { origZone->setView(this); } diff --git a/cockatrice/src/zoneviewzone.h b/cockatrice/src/zoneviewzone.h index e331b7a2..52a9aac7 100644 --- a/cockatrice/src/zoneviewzone.h +++ b/cockatrice/src/zoneviewzone.h @@ -1,14 +1,14 @@ #ifndef ZONEVIEWERZONE_H #define ZONEVIEWERZONE_H -#include "cardzone.h" +#include "selectzone.h" #include class ZoneViewWidget; class ProtocolResponse; class ServerInfo_Card; -class ZoneViewZone : public CardZone, public QGraphicsLayoutItem { +class ZoneViewZone : public SelectZone, public QGraphicsLayoutItem { Q_OBJECT private: QRectF bRect, optimumRect;