interactors.h 4.73 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
/***************************************************************************
 *   Copyright 2010 Stefan Majewsky <majewsky@gmx.net>
 *
 *   This program is free software; you can redistribute it and/or
 *   modify it under the terms of the GNU General Public
 *   License as published by the Free Software Foundation; either
 *   version 2 of the License, or (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, write to the Free Software
 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
***************************************************************************/

#ifndef PALAPELI_INTERACTORS_H
#define PALAPELI_INTERACTORS_H

#include "interactor.h"

#include <QGraphicsItem>

namespace Palapeli
{
28 29
	class Piece;

30 31 32
	//This interactor is assigned to LeftButton;NoModifier by default.
	//1. When you click on and drag a selected piece, all selected pieces are moved.
	//2. When you click on and drag an unselected piece, all other pieces are deselected, the clicked piece is selected and moved.
33 34 35 36 37
	class MovePieceInteractor : public Palapeli::Interactor
	{
		public:
			MovePieceInteractor(QGraphicsView* view);
		protected:
38 39 40
			virtual bool startInteraction(const Palapeli::MouseEvent& event);
			virtual void continueInteraction(const Palapeli::MouseEvent& event);
			virtual void stopInteraction(const Palapeli::MouseEvent& event);
41 42 43 44 45 46 47 48 49 50
		private:
			Palapeli::Piece* findPieceForItem(QGraphicsItem* mouseInteractingItem) const;
			void determineSelectedItems(QGraphicsItem* clickedItem, Palapeli::Piece* clickedPiece);

			QList<QGraphicsItem*> m_currentItems; //the item which accepts the events
			QList<Palapeli::Piece*> m_currentPieces; //the piece for this item
			QPointF m_baseScenePosition;
			QList<QPointF> m_basePositions;
	};

51 52 53 54 55 56 57
	//This interactor is assigned to LeftButton;ControlModifier by default.
	//When you click on a piece, its selection state will be toggled.
	class SelectPieceInteractor : public Palapeli::Interactor
	{
		public:
			SelectPieceInteractor(QGraphicsView* view);
		protected:
58
			virtual bool startInteraction(const Palapeli::MouseEvent& event);
59 60 61 62
	};

	//This interactor is assigned to RightButton;NoModifier by default.
	//Dragging will drag the viewport (only translations, no rotation or zooming).
63 64 65 66 67
	class MoveViewportInteractor : public Palapeli::Interactor
	{
		public:
			MoveViewportInteractor(QGraphicsView* view);
		protected:
68 69
			virtual bool startInteraction(const Palapeli::MouseEvent& event);
			virtual void continueInteraction(const Palapeli::MouseEvent& event);
70 71
		private:
			QPoint m_lastPos;
72 73
	};

74 75
	//This interactor is assigned to wheel:Vertical;NoModifier by default.
	//Turning the wheel will zoom the viewport.
76 77 78 79 80
	class ZoomViewportInteractor : public Palapeli::Interactor
	{
		public:
			ZoomViewportInteractor(QGraphicsView* view);
		protected:
81
			virtual void doInteraction(const Palapeli::WheelEvent& event);
82 83
	};

84 85 86 87 88 89 90 91 92 93 94 95
	//This interactor is assigned to nothing by default.
	//Turning the wheel will scroll the viewport either horizontally or vertically.
	class ScrollViewportInteractor : public Palapeli::Interactor
	{
		public:
			ScrollViewportInteractor(Qt::Orientation orientation, QGraphicsView* view);
		protected:
			virtual void doInteraction(const Palapeli::WheelEvent& event);
		private:
			Qt::Orientation m_orientation;
	};

96 97 98 99 100 101 102 103 104 105 106 107 108 109
	class RubberBandItem : public QGraphicsItem
	{
		public:
			RubberBandItem(QGraphicsItem* parent = 0);

			QRectF rect() const;
			void setRect(const QRectF& rect);

			virtual QRectF boundingRect() const;
			virtual void paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget = 0);
		private:
			QRectF m_rect;
	};

110 111
	//This interactor is assigned to LeftClick;NoModifier by default.
	//When you click on a free area of the view and drag the mouse, a rectangular rubberband will appear. All pieces inside the rubberband will be selected, all other pieces are deselected.
112 113 114 115 116 117
	class RubberBandInteractor : public Palapeli::Interactor
	{
		public:
			RubberBandInteractor(QGraphicsView* view);
			virtual ~RubberBandInteractor();
		protected:
118
			virtual void sceneChangeEvent(QGraphicsScene* oldScene, QGraphicsScene* newScene);
119 120 121
			virtual bool startInteraction(const Palapeli::MouseEvent& event);
			virtual void continueInteraction(const Palapeli::MouseEvent& event);
			virtual void stopInteraction(const Palapeli::MouseEvent& event);
122 123 124 125 126 127 128
		private:
			Palapeli::RubberBandItem* m_item;
			QPointF m_basePosition;
	};
}

#endif // PALAPELI_INTERACTORS_H