triggermapper.cpp 8.77 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
/***************************************************************************
 *   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 "triggermapper.h"
#include "constraintinteractor.h"
#include "interactors.h"

#include <QKeyEvent>
#include <QMouseEvent>
#include <QWheelEvent>
26 27 28
#include <KConfig>
#include <KConfigGroup>
#include <KGlobal>
29 30 31 32 33 34 35 36 37 38 39 40 41 42

Palapeli::TriggerMapper* Palapeli::TriggerMapper::instance()
{
	static Palapeli::TriggerMapper instance;
	return &instance;
}

QMap<QByteArray, Palapeli::Interactor*> Palapeli::TriggerMapper::createInteractors(QGraphicsView* view)
{
	QMap<QByteArray, Palapeli::Interactor*> result;
	result["MovePiece"] = new Palapeli::MovePieceInteractor(view);
	result["SelectPiece"] = new Palapeli::SelectPieceInteractor(view);
	result["MoveViewport"] = new Palapeli::MoveViewportInteractor(view);
	result["ZoomViewport"] = new Palapeli::ZoomViewportInteractor(view);
43 44
	result["ScrollViewportHoriz"] = new Palapeli::ScrollViewportInteractor(Qt::Horizontal, view);
	result["ScrollViewportVert"] = new Palapeli::ScrollViewportInteractor(Qt::Vertical, view);
45 46
	result["RubberBand"] = new Palapeli::RubberBandInteractor(view);
	result["Constraints"] = new Palapeli::ConstraintInteractor(view);
47
	result["ToggleConstraints"] = new Palapeli::ToggleConstraintInteractor(view);
48 49 50
	return result;
}

51 52 53 54 55 56 57 58 59 60 61 62
QMap<QByteArray, Palapeli::Trigger> Palapeli::TriggerMapper::defaultAssociations()
{
	QMap<QByteArray, Palapeli::Trigger> result;
	result.insert("MovePiece", Palapeli::Trigger("LeftButton;NoModifier"));
	result.insert("SelectPiece", Palapeli::Trigger("LeftButton;ControlModifier"));
	result.insert("MoveViewport", Palapeli::Trigger("RightButton;NoModifier"));
	result.insert("ZoomViewport", Palapeli::Trigger("wheel:Vertical;NoModifier"));
	result.insert("RubberBand", Palapeli::Trigger("LeftButton;NoModifier"));
	result.insert("Constraints", Palapeli::Trigger("LeftButton;NoModifier"));
	return result;
}

63 64 65 66 67 68 69 70 71 72 73
Palapeli::TriggerMapper::TriggerMapper()
{
	//initialize quasi-static data
	m_keyModifierMap[Qt::Key_Shift] = Qt::ShiftModifier;
	m_keyModifierMap[Qt::Key_Control] = Qt::ControlModifier;
	m_keyModifierMap[Qt::Key_Alt] = Qt::AltModifier;
	m_keyModifierMap[Qt::Key_Meta] = Qt::MetaModifier;
	//intialize dynamic data
	readSettings();
}

74 75 76 77 78
QMap<QByteArray, Palapeli::Trigger> Palapeli::TriggerMapper::associations() const
{
	return m_associations;
}

79 80
void Palapeli::TriggerMapper::readSettings()
{
81 82 83 84 85 86 87 88 89 90 91 92
	m_associations.clear();
	//read config
	KConfigGroup group(KGlobal::config(), "Mouse Interaction");
	const QStringList configKeys = group.keyList();
	foreach (const QString& configKey, configKeys)
	{
		const QByteArray interactorKey = configKey.toLatin1();
		const QList<QByteArray> triggers = group.readEntry(configKey, QList<QByteArray>());
		foreach (const Palapeli::Trigger& trigger, triggers) //implicit casts FTW
			if (trigger.isValid())
				m_associations.insertMulti(interactorKey, trigger);
	}
93
	//fallback to default settings if necessary
94
	if (m_associations.isEmpty())
95 96 97
		m_associations = Palapeli::TriggerMapper::defaultAssociations();
	//announce update to InteractorManagers
	emit associationsChanged();
98 99
}

100
void Palapeli::TriggerMapper::setAssociations(const QMap<QByteArray, Palapeli::Trigger>& associations)
101
{
102 103 104
	m_associations = associations;
	//announce update to InteractorManagers
	emit associationsChanged();
105 106 107 108 109 110 111 112 113 114 115 116 117
	//assemble trigger serializations
	QMap<QByteArray, QList<QByteArray> > triggerSerializations;
	{
		QMap<QByteArray, Palapeli::Trigger>::const_iterator it1 = m_associations.begin(), it2 = m_associations.end();
		for (; it1 != it2; ++it1)
			triggerSerializations[it1.key()] << it1.value().serialized();
	}
	//write config (in a way that supports multiple triggers for one interactor)
	KConfigGroup group(KGlobal::config(), "Mouse Interaction");
	QMap<QByteArray, QList<QByteArray> >::const_iterator it1 = triggerSerializations.begin(), it2 = triggerSerializations.end();
	for (; it1 != it2; ++it1)
		group.writeEntry(it1.key().data(), it1.value());
	KGlobal::config()->sync();
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 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
}

Palapeli::EventProcessingFlags Palapeli::TriggerMapper::testTrigger(const QByteArray& interactor, QWheelEvent* event) const
{
	Palapeli::EventProcessingFlags result;
	QMap<QByteArray, Palapeli::Trigger>::const_iterator it1 = m_associations.begin(), it2 = m_associations.end();
	for (; it1 != it2; ++it1)
		if (it1.key() == interactor)
			result |= testTrigger(it1.value(), event);
	return result;
}

Palapeli::EventContext Palapeli::TriggerMapper::testTrigger(const QByteArray& interactor, QMouseEvent* event) const
{
	Palapeli::EventContext result;
	QMap<QByteArray, Palapeli::Trigger>::const_iterator it1 = m_associations.begin(), it2 = m_associations.end();
	for (; it1 != it2; ++it1)
		if (it1.key() == interactor)
		{
			const Palapeli::EventProcessingFlags flags = testTrigger(it1.value(), event);
			result.flags |= flags;
			if (flags & Palapeli::EventMatches)
				result.triggeringButtons |= it1.value().button();
		}
	return result;
}

Palapeli::EventContext Palapeli::TriggerMapper::testTrigger(const QByteArray& interactor, QKeyEvent* event, Qt::MouseButtons buttons) const
{
	Palapeli::EventContext result;
	QMap<QByteArray, Palapeli::Trigger>::const_iterator it1 = m_associations.begin(), it2 = m_associations.end();
	for (; it1 != it2; ++it1)
		if (it1.key() == interactor)
		{
			result.flags |= testTrigger(it1.value(), event, buttons);
			result.triggeringButtons |= it1.value().button();
		}
	return result;
}

Palapeli::EventProcessingFlags Palapeli::TriggerMapper::testTrigger(const Palapeli::Trigger& trigger, QWheelEvent* event) const
{
	if (trigger.isValid())
	{
		const bool testModifiers = trigger.modifiers() == event->modifiers();
		const bool checkDirection = trigger.wheelDirection() != 0;
		const bool testDirection = trigger.wheelDirection() == event->orientation();
		if (testModifiers && checkDirection && testDirection)
			return Palapeli::EventMatches;
	}
	//if execution comes to this point, trigger does not match
	return 0;
}

Palapeli::EventProcessingFlags Palapeli::TriggerMapper::testTrigger(const Palapeli::Trigger& trigger, QMouseEvent* event) const
{
	if (trigger.isValid())
	{
		const bool testModifiers = trigger.modifiers() == event->modifiers();
		const bool checkDirection = trigger.wheelDirection() == 0;
		if (testModifiers && checkDirection)
		{
			if (trigger.button() == Qt::NoButton)
				//trigger matches
				return Palapeli::EventMatches;
			const bool checkButtons = (event->button() | event->buttons()) & trigger.button();
			if (checkButtons)
			{
				//trigger matches - construct result
				Palapeli::EventProcessingFlags result = Palapeli::EventMatches;
				if (event->button() == trigger.button())
				{
					if (event->type() == QEvent::MouseButtonPress)
						result |= Palapeli::EventStartsInteraction;
					if (event->type() == QEvent::MouseButtonRelease)
						result |= Palapeli::EventConcludesInteraction;
				}
				return result;
			}
		}
	}
	//if execution comes to this point, trigger does not match
	return 0;
}

Palapeli::EventProcessingFlags Palapeli::TriggerMapper::testTrigger(const Palapeli::Trigger& trigger, QKeyEvent* event, Qt::MouseButtons buttons) const
{
	if (trigger.isValid())
	{
		//read modifiers
		const Qt::KeyboardModifier keyModifier = m_keyModifierMap.value((Qt::Key) event->key(), Qt::NoModifier);
		const Qt::KeyboardModifiers modifiers = keyModifier | event->modifiers();
		//checking
		const bool testModifiers = trigger.modifiers() == modifiers;
		const bool checkDirection = trigger.wheelDirection() == 0;
		const bool checkButton = (trigger.button() & buttons) || trigger.button() == Qt::NoButton;
		if (testModifiers && checkDirection && checkButton)
		{
			//trigger matches - construct result
			Palapeli::EventProcessingFlags result = Palapeli::EventMatches;
			if (keyModifier != Qt::NoModifier)
			{
				if (event->type() == QEvent::KeyPress)
					result |= Palapeli::EventStartsInteraction;
				if (event->type() == QEvent::KeyRelease)
					result |= Palapeli::EventConcludesInteraction;
			}
			return result;
		}
	}
	//if execution comes to this point, trigger does not match
	return 0;
}

#include "triggermapper.moc"