improved rubber band drag

This commit is contained in:
Max-Wilhelm Bruker 2010-12-14 04:37:13 +01:00
parent 99387861cd
commit 99c0a41d18
15 changed files with 139 additions and 14 deletions

View file

@ -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 \

View file

@ -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();
}

View file

@ -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<ServerInfo_Card *> &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

View file

@ -2,17 +2,21 @@
#include "gamescene.h"
#include <QResizeEvent>
#include <QAction>
#include <QRubberBand>
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;
}
}

View file

@ -3,12 +3,20 @@
#include <QGraphicsView>
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:

View file

@ -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();

View file

@ -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;

View file

@ -0,0 +1,48 @@
#include <QGraphicsSceneMouseEvent>
#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<GameScene *>(scene())->resizeRubberBand(scenePos() + pos);
event->accept();
}
}
void SelectZone::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
if (event->button() == Qt::LeftButton) {
selectionOrigin = event->pos();
static_cast<GameScene *>(scene())->startRubberBand(event->scenePos());
event->accept();
}
}
void SelectZone::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
selectionOrigin = QPoint();
static_cast<GameScene *>(scene())->stopRubberBand();
event->accept();
}

View file

@ -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

View file

@ -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();

View file

@ -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;

View file

@ -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()));

View file

@ -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();

View file

@ -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);
}

View file

@ -1,14 +1,14 @@
#ifndef ZONEVIEWERZONE_H
#define ZONEVIEWERZONE_H
#include "cardzone.h"
#include "selectzone.h"
#include <QGraphicsLayoutItem>
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;