interactors.cpp 12.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/***************************************************************************
 *   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.
***************************************************************************/

#include "interactors.h"
20
#include "piece.h"
21
#include "scene.h"
22 23 24 25
#include "view.h"

#include <QStyle>
#include <QStyleOptionRubberBand>
26
#include <KLocalizedString>
27

28 29 30
//BEGIN Palapeli::MovePieceInteractor

Palapeli::MovePieceInteractor::MovePieceInteractor(QGraphicsView* view)
31
	: Palapeli::Interactor(20, Palapeli::MouseInteractor, view) //priority: very high because this is the most important interaction
32
{
33
	setMetadata(PieceInteraction, i18nc("Description (used like a name) for a mouse interaction method", "Move pieces by dragging"), QIcon());
34 35 36 37
}

static QGraphicsItem* findSelectableItemAt(const QPointF& scenePos, QGraphicsScene* scene)
{
38 39
	if (!scene)
		return 0;
40 41
	QList<QGraphicsItem*> itemsUnderMouse = scene->items(scenePos);
	foreach (QGraphicsItem* itemUnderMouse, itemsUnderMouse)
42
		if (itemUnderMouse->flags() & QGraphicsItem::ItemIsSelectable)
43 44
			return itemUnderMouse;
	return 0;
45 46 47 48 49 50 51 52 53 54 55 56
}

void Palapeli::MovePieceInteractor::determineSelectedItems(QGraphicsItem* clickedItem, Palapeli::Piece* clickedPiece)
{
	m_currentItems.clear();
	m_currentPieces.clear();
	const QList<QGraphicsItem*> selectedItems = clickedItem->scene()->selectedItems();
	if (clickedItem->isSelected())
	{
		//clicked item is already selected -> include all selected items/pieces in this move
		foreach (QGraphicsItem* selectedItem, selectedItems)
		{
57
			Palapeli::Piece* selectedPiece = Palapeli::Piece::fromSelectedItem(selectedItem);
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
			if (selectedPiece)
			{
				m_currentItems << selectedItem;
				m_currentPieces << selectedPiece;
			}
		}
		//NOTE: clickedItem is in the list selectedItems, so it need not be handled separately.
	}
	else
	{
		//clicked item is not selected -> deselect everything else and select only this item
		foreach (QGraphicsItem* selectedItem, selectedItems)
			selectedItem->setSelected(false);
		clickedItem->setSelected(true);
		m_currentItems << clickedItem;
		m_currentPieces << clickedPiece;
	}
}

77
bool Palapeli::MovePieceInteractor::startInteraction(const Palapeli::MouseEvent& event)
78
{
79 80 81 82 83 84 85 86 87 88
	//no item under mouse -> no interaction possible
	QGraphicsItem* selectableItemUnderMouse = findSelectableItemAt(event.scenePos, scene());
	if (!selectableItemUnderMouse)
		return false;
	//we will only move pieces -> find the piece which we are moving
	Palapeli::Piece* piece = Palapeli::Piece::fromSelectedItem(selectableItemUnderMouse);
	if (!piece)
		return false;
	//start moving this piece
	determineSelectedItems(selectableItemUnderMouse, piece);
89 90 91 92 93 94 95
	m_baseScenePosition = event.scenePos;
	m_basePositions.clear();
	foreach(Palapeli::Piece* piece, m_currentPieces)
	{
		m_basePositions << piece->pos();
		piece->beginMove();
	}
96
	return true;
97 98
}

99
void Palapeli::MovePieceInteractor::continueInteraction(const Palapeli::MouseEvent& event)
100 101 102 103 104 105 106 107
{
	for (int i = 0; i < m_currentPieces.count(); ++i)
	{
		m_currentPieces[i]->setPos(m_basePositions[i] + event.scenePos - m_baseScenePosition);
		m_currentPieces[i]->doMove();
	}
}

108
void Palapeli::MovePieceInteractor::stopInteraction(const Palapeli::MouseEvent& event)
109 110 111 112 113 114 115 116 117
{
	Q_UNUSED(event)
	foreach(Palapeli::Piece* piece, m_currentPieces)
		piece->endMove();
	m_currentItems.clear();
	m_currentPieces.clear();
}

//END Palapeli::MovePieceInteractor
118 119 120
//BEGIN Palapeli::SelectPieceInteractor

Palapeli::SelectPieceInteractor::SelectPieceInteractor(QGraphicsView* view)
121
	: Palapeli::Interactor(19, Palapeli::MouseInteractor, view) //priority: a bit less than MovePieceInteractor
122
{
123
	setMetadata(PieceInteraction, i18nc("Description (used like a name) for a mouse interaction method", "Select pieces by clicking"), QIcon());
124 125
}

126
bool Palapeli::SelectPieceInteractor::startInteraction(const Palapeli::MouseEvent& event)
127 128
{
	//no item under mouse -> no interaction possible
129
	QGraphicsItem* selectableItemUnderMouse = findSelectableItemAt(event.scenePos, scene());
130 131 132
	if (!selectableItemUnderMouse)
		return false;
	//we will only move pieces -> find the piece which we are moving
133 134 135
	Palapeli::Piece* currentPiece = Palapeli::Piece::fromSelectedItem(selectableItemUnderMouse);
	if (!currentPiece)
		return false;
136
	//toggle selection state for piece under mouse
137 138
	currentPiece->setSelected(!currentPiece->isSelected());
	return true;
139 140 141
}

//END Palapeli::SelectPieceInteractor
142 143 144
//BEGIN Palapeli::MoveViewportInteractor

Palapeli::MoveViewportInteractor::MoveViewportInteractor(QGraphicsView* view)
145
	: Palapeli::Interactor(1, Palapeli::MouseInteractor, view) //priority: very low because specific interaction points (e.g. pieces, scene boundaries) are much more important
146
{
147
	setMetadata(ViewportInteraction, i18nc("Description (used like a name) for a mouse interaction method", "Move viewport by dragging"), QIcon());
148 149
}

150
bool Palapeli::MoveViewportInteractor::startInteraction(const Palapeli::MouseEvent& event)
151 152
{
	m_lastPos = event.pos;
153
	return true;
154 155
}

156
void Palapeli::MoveViewportInteractor::continueInteraction(const Palapeli::MouseEvent& event)
157
{
158 159 160
	Palapeli::View* view = qobject_cast<Palapeli::View*>(this->view());
	if (view)
		view->moveViewportBy(event.pos - m_lastPos);
161
	m_lastPos = event.pos;
162 163 164 165 166 167
}

//END Palapeli::MoveViewportInteractor
//BEGIN Palapeli::ZoomViewportInteractor

Palapeli::ZoomViewportInteractor::ZoomViewportInteractor(QGraphicsView* view)
168
	: Palapeli::Interactor(0, Palapeli::WheelInteractor, view) //priority: unused for wheel interactors ATM
169
{
170
	setMetadata(ViewportInteraction, i18nc("Description (used like a name) for a mouse interaction method", "Zoom viewport"), QIcon());
171 172
}

173
void Palapeli::ZoomViewportInteractor::doInteraction(const Palapeli::WheelEvent& event)
174 175 176 177 178 179 180
{
	Palapeli::View* view = qobject_cast<Palapeli::View*>(this->view());
	if (view)
		view->zoomBy(event.delta);
}

//END Palapeli::ZoomViewportInteractor
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
//BEGIN Palapeli::ScrollViewportInteractor

Palapeli::ScrollViewportInteractor::ScrollViewportInteractor(Qt::Orientation orientation, QGraphicsView* view)
	: Palapeli::Interactor(0, Palapeli::WheelInteractor, view) //priority: unused for wheel interactors ATM
	, m_orientation(orientation)
{
	QString description;
	if (orientation == Qt::Horizontal)
		description = i18nc("Description (used like a name) for a mouse interaction method", "Scroll viewport horizontally");
	else
		description = i18nc("Description (used like a name) for a mouse interaction method", "Scroll viewport vertically");
	setMetadata(ViewportInteraction, description, QIcon());
}

void Palapeli::ScrollViewportInteractor::doInteraction(const Palapeli::WheelEvent& event)
{
	const QPoint widgetDelta = (m_orientation == Qt::Horizontal) ? QPoint(event.delta, 0) : QPoint(0, event.delta);
	Palapeli::View* view = qobject_cast<Palapeli::View*>(this->view());
	if (view)
		view->moveViewportBy(view->mapToScene(widgetDelta)- view->mapToScene(QPoint()));
}

//END Palapeli::ScrollViewportInteractor
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
//BEGIN Palapeli::RubberBandItem

Palapeli::RubberBandItem::RubberBandItem(QGraphicsItem* parent)
	: QGraphicsItem(parent)
{
}

QRectF Palapeli::RubberBandItem::rect() const
{
	return m_rect;
}

void Palapeli::RubberBandItem::setRect(const QRectF& rect)
{
	if (m_rect == rect || (m_rect.isEmpty() && rect.isEmpty()))
		return;
	prepareGeometryChange();
	m_rect = rect;
	update();
	//update list of selected items when rubberband is visible
	if (!rect.isEmpty())
	{
		QPainterPath p;
		p.addRect(sceneBoundingRect());
		scene()->setSelectionArea(p, Qt::ContainsItemBoundingRect);
	}
}

QRectF Palapeli::RubberBandItem::boundingRect() const
{
	return m_rect; //The QStyle does not paint outside this rect!
}

void Palapeli::RubberBandItem::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
{
	Q_UNUSED(painter)
	Q_UNUSED(option)
	if (m_rect.isEmpty())
		return;
	//find the view which we are painting on (we could be painting directly, or on a viewport contained in the view)
	QGraphicsView* view = qobject_cast<QGraphicsView*>(widget);
	if (!view)
		view = qobject_cast<QGraphicsView*>(widget->parent());
	Q_ASSERT(view);
	//render on view directly
	QPainter viewPainter(widget);
	QRect rect = view->mapFromScene(sceneBoundingRect()).boundingRect();
	QStyleOptionRubberBand opt;
	opt.initFrom(widget);
	opt.rect = rect;
	opt.shape = QRubberBand::Rectangle;
	//painter clipping for masked rubberbands
	QStyleHintReturnMask mask;
	if (widget->style()->styleHint(QStyle::SH_RubberBand_Mask, &opt, widget, &mask))
		painter->setClipRegion(mask.region, Qt::IntersectClip);
	//draw rubberband
	widget->style()->drawControl(QStyle::CE_RubberBand, &opt, &viewPainter, widget);
}

//END Palapeli::RubberBandItem
//BEGIN Palapeli::RubberBandInteractor

Palapeli::RubberBandInteractor::RubberBandInteractor(QGraphicsView* view)
267
	: Palapeli::Interactor(2, Palapeli::MouseInteractor, view) //priority: a bit more than MoveViewport, but still much less than interactions with specific interaction points (e.g. pieces, scene boundaries)
268 269
	, m_item(new Palapeli::RubberBandItem)
{
270
	setMetadata(PieceInteraction, i18nc("Description (used like a name) for a mouse interaction method", "Select multiple pieces at once"), QIcon());
271 272
	if (scene())
		scene()->addItem(m_item);
273
	m_item->hide(); //NOTE: This is not necessary for the painting, but we use m_item->isVisible() to determine whether we are rubberbanding at the moment. //FIXME: really?
274 275 276 277 278 279 280
}

Palapeli::RubberBandInteractor::~RubberBandInteractor()
{
	delete m_item;
}

281 282 283 284 285 286
void Palapeli::RubberBandInteractor::sceneChangeEvent(QGraphicsScene* oldScene, QGraphicsScene* newScene)
{
	if (oldScene)
		oldScene->removeItem(m_item);
	if (newScene)
		newScene->addItem(m_item);
287
	m_item->setVisible(false);
288 289
}

290
bool Palapeli::RubberBandInteractor::startInteraction(const Palapeli::MouseEvent& event)
291
{
292
	//check items under mouse
293 294 295
	if (findSelectableItemAt(event.scenePos, scene()))
		return false;
	//start rubberbanding
296 297 298
	m_basePosition = event.scenePos;
	m_item->show(); //NOTE: This is not necessary for the painting, but we use m_item->isVisible() to determine whether we are rubberbanding at the moment.
	m_item->setRect(QRectF(m_basePosition, QSizeF()));
299 300
	scene()->setSelectionArea(QPainterPath()); //deselect everything
	return true;
301 302
}

303
void Palapeli::RubberBandInteractor::continueInteraction(const Palapeli::MouseEvent& event)
304 305 306 307 308 309
{
	QSizeF size(event.scenePos.x() - m_basePosition.x(), event.scenePos.y() - m_basePosition.y());
	QRectF rect(m_basePosition, size);
	m_item->setRect(rect.normalized());
}

310
void Palapeli::RubberBandInteractor::stopInteraction(const Palapeli::MouseEvent& event)
311 312
{
	Q_UNUSED(event)
313
	m_item->hide(); //NOTE: This is not necessary for the painting, but we use m_item->isVisible() to determine whether we are rubberbanding at the moment.
314 315 316 317
	m_item->setRect(QRectF());
}

//END Palapeli::RubberBandInteractor
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
//BEGIN Palapeli::ToggleConstraintInteractor

Palapeli::ToggleConstraintInteractor::ToggleConstraintInteractor(QGraphicsView* view)
	: Palapeli::Interactor(30, Palapeli::MouseInteractor, view) //priority: higher than anything else (this should not depend on what the cursor is currently pointing)
{
	setMetadata(TableInteraction, i18nc("Description (used like a name) for a mouse interaction method", "Toggle lock state of the puzzle table area"), QIcon());
}

bool Palapeli::ToggleConstraintInteractor::startInteraction(const Palapeli::MouseEvent& event)
{
	Q_UNUSED(event)
	Palapeli::Scene* scene = qobject_cast<Palapeli::Scene*>(this->scene());
	if (scene)
		scene->setConstrained(!scene->isConstrained());
	return (bool) scene;
}

//END Palapeli::ToggleConstraintInteractor