1 /* This file is part of the KDE project
2  * Copyright (C) 2012 Arjen Hiemstra <ahiemstra@heimr.nl>
3  *
4  *  This program is free software; you can redistribute it and/or modify
5  *  it under the terms of the GNU General Public License as published by
6  *  the Free Software Foundation; either version 2 of the License, or
7  *  (at your option) any later version.
8  *
9  *  This program is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU General Public License for more details.
13  *
14  *  You should have received a copy of the GNU General Public License
15  *  along with this program; if not, write to the Free Software
16  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  */
18 
19 #include "kis_abstract_input_action.h"
20 
21 #include <QPointF>
22 #include <QMouseEvent>
23 #include <klocalizedstring.h>
24 #include <kis_debug.h>
25 
26 class Q_DECL_HIDDEN KisAbstractInputAction::Private
27 {
28 public:
29     QString id;
30     QString name;
31     QString description;
32     QHash<QString, int> indexes;
33 
34     QPointF lastCursorPosition;
35     QPointF startCursorPosition;
36 
37     static KisInputManager *inputManager;
38 };
39 
40 KisInputManager *KisAbstractInputAction::Private::inputManager = 0;
41 
KisAbstractInputAction(const QString & id)42 KisAbstractInputAction::KisAbstractInputAction(const QString &id)
43     : d(new Private)
44 {
45     d->id = id;
46     d->indexes.insert(i18n("Activate"), 0);
47 }
48 
~KisAbstractInputAction()49 KisAbstractInputAction::~KisAbstractInputAction()
50 {
51     delete d;
52 }
53 
activate(int shortcut)54 void KisAbstractInputAction::activate(int shortcut)
55 {
56     Q_UNUSED(shortcut);
57 }
58 
deactivate(int shortcut)59 void KisAbstractInputAction::deactivate(int shortcut)
60 {
61     Q_UNUSED(shortcut);
62 }
63 
begin(int shortcut,QEvent * event)64 void KisAbstractInputAction::begin(int shortcut, QEvent *event)
65 {
66     Q_UNUSED(shortcut);
67 
68     if (event) {
69         d->lastCursorPosition = eventPosF(event);
70         d->startCursorPosition = d->lastCursorPosition;
71     }
72 }
73 
inputEvent(QEvent * event)74 void KisAbstractInputAction::inputEvent(QEvent *event)
75 {
76     if (event) {
77         QPointF newPosition = eventPosF(event);
78         cursorMoved(d->lastCursorPosition, newPosition);
79         cursorMovedAbsolute(d->startCursorPosition, newPosition);
80         d->lastCursorPosition = newPosition;
81     }
82 }
83 
end(QEvent * event)84 void KisAbstractInputAction::end(QEvent *event)
85 {
86     Q_UNUSED(event);
87 }
88 
cursorMoved(const QPointF & lastPos,const QPointF & pos)89 void KisAbstractInputAction::cursorMoved(const QPointF &lastPos, const QPointF &pos)
90 {
91     Q_UNUSED(lastPos);
92     Q_UNUSED(pos);
93 }
94 
cursorMovedAbsolute(const QPointF & startPos,const QPointF & pos)95 void KisAbstractInputAction::cursorMovedAbsolute(const QPointF &startPos, const QPointF &pos)
96 {
97     Q_UNUSED(startPos);
98     Q_UNUSED(pos);
99 }
100 
supportsHiResInputEvents(int shortcut) const101 bool KisAbstractInputAction::supportsHiResInputEvents(int shortcut) const
102 {
103     Q_UNUSED(shortcut);
104     return false;
105 }
106 
inputActionGroup(int shortcut) const107 KisInputActionGroup KisAbstractInputAction::inputActionGroup(int shortcut) const
108 {
109     Q_UNUSED(shortcut);
110     return ModifyingActionGroup;
111 }
112 
inputManager() const113 KisInputManager* KisAbstractInputAction::inputManager() const
114 {
115     return Private::inputManager;
116 }
117 
name() const118 QString KisAbstractInputAction::name() const
119 {
120     return d->name;
121 }
122 
description() const123 QString KisAbstractInputAction::description() const
124 {
125     return d->description;
126 }
127 
priority() const128 int KisAbstractInputAction::priority() const
129 {
130     return 0;
131 }
132 
canIgnoreModifiers() const133 bool KisAbstractInputAction::canIgnoreModifiers() const
134 {
135     return false;
136 }
137 
shortcutIndexes() const138 QHash< QString, int > KisAbstractInputAction::shortcutIndexes() const
139 {
140     return d->indexes;
141 }
142 
id() const143 QString KisAbstractInputAction::id() const
144 {
145     return d->id;
146 }
147 
setName(const QString & name)148 void KisAbstractInputAction::setName(const QString &name)
149 {
150     d->name = name;
151 }
152 
setDescription(const QString & description)153 void KisAbstractInputAction::setDescription(const QString &description)
154 {
155     d->description = description;
156 }
157 
setShortcutIndexes(const QHash<QString,int> & indexes)158 void KisAbstractInputAction::setShortcutIndexes(const QHash< QString, int > &indexes)
159 {
160     d->indexes = indexes;
161 }
162 
setInputManager(KisInputManager * manager)163 void KisAbstractInputAction::setInputManager(KisInputManager *manager)
164 {
165     Private::inputManager = manager;
166 }
167 
isShortcutRequired(int shortcut) const168 bool KisAbstractInputAction::isShortcutRequired(int shortcut) const
169 {
170     Q_UNUSED(shortcut);
171     return false;
172 }
173 
eventPos(const QEvent * event)174 QPoint KisAbstractInputAction::eventPos(const QEvent *event)
175 {
176     if(!event) {
177         return QPoint();
178     }
179 
180     switch (event->type()) {
181     case QEvent::MouseMove:
182     case QEvent::MouseButtonPress:
183     case QEvent::MouseButtonDblClick:
184     case QEvent::MouseButtonRelease:
185         return static_cast<const QMouseEvent*>(event)->pos();
186 
187     case QEvent::TabletMove:
188     case QEvent::TabletPress:
189     case QEvent::TabletRelease:
190         return static_cast<const QTabletEvent*>(event)->pos();
191 
192     case QEvent::Wheel:
193         return static_cast<const QWheelEvent*>(event)->pos();
194 
195     case QEvent::NativeGesture:
196         return static_cast<const QNativeGestureEvent*>(event)->pos();
197 
198     default:
199         warnInput << "KisAbstractInputAction" << d->name << "tried to process event data from an unhandled event type" << event->type();
200         return QPoint();
201     }
202 }
203 
eventPosF(const QEvent * event)204 QPointF KisAbstractInputAction::eventPosF(const QEvent *event) {
205 
206     switch (event->type()) {
207     case QEvent::MouseMove:
208     case QEvent::MouseButtonPress:
209     case QEvent::MouseButtonDblClick:
210     case QEvent::MouseButtonRelease:
211         return static_cast<const QMouseEvent*>(event)->localPos();
212 
213     case QEvent::TabletMove:
214     case QEvent::TabletPress:
215     case QEvent::TabletRelease:
216         return static_cast<const QTabletEvent*>(event)->posF();
217 
218     case QEvent::Wheel:
219         return static_cast<const QWheelEvent*>(event)->posF();
220 
221     case QEvent::NativeGesture:
222         return QPointF(static_cast<const QNativeGestureEvent*>(event)->pos());
223 
224     default:
225         warnInput << "KisAbstractInputAction" << d->name << "tried to process event data from an unhandled event type" << event->type();
226         return QPointF();
227     }
228 }
229 
isAvailable() const230 bool KisAbstractInputAction::isAvailable() const
231 {
232     return true;
233 }
234