interactors.cpp 12.2 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 <KIcon>
27
#include <KLocalizedString>
28

29 30 31
//BEGIN Palapeli::MovePieceInteractor

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

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

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)
		{
58
			Palapeli::Piece* selectedPiece = Palapeli::Piece::fromSelectedItem(selectedItem);
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
			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;
	}
}

78
bool Palapeli::MovePieceInteractor::startInteraction(const Palapeli::MouseEvent& event)
79
{
80 81 82 83 84 85 86 87 88 89
	//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);
90 91 92 93 94 95 96
	m_baseScenePosition = event.scenePos;
	m_basePositions.clear();
	foreach(Palapeli::Piece* piece, m_currentPieces)
	{
		m_basePositions << piece->pos();
		piece->beginMove();
	}
97
	return true;
98 99
}

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

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

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

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

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

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

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

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

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

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

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

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

//END Palapeli::ZoomViewportInteractor
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
//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
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 267
//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)
268
	: 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)
269 270
	, m_item(new Palapeli::RubberBandItem)
{
271
	setMetadata(PieceInteraction, i18nc("Description (used like a name) for a mouse interaction method", "Select multiple pieces at once"), KIcon("select-rectangular"));
272 273
	if (scene())
		scene()->addItem(m_item);
274
	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?
275 276 277 278 279 280 281
}

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

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

291
bool Palapeli::RubberBandInteractor::startInteraction(const Palapeli::MouseEvent& event)
292
{
293
	//check items under mouse
294 295 296
	if (findSelectableItemAt(event.scenePos, scene()))
		return false;
	//start rubberbanding
297 298 299
	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()));
300 301
	scene()->setSelectionArea(QPainterPath()); //deselect everything
	return true;
302 303
}

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

311
void Palapeli::RubberBandInteractor::stopInteraction(const Palapeli::MouseEvent& event)
312 313
{
	Q_UNUSED(event)
314
	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.
315 316 317 318
	m_item->setRect(QRectF());
}

//END Palapeli::RubberBandInteractor
319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
//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