File indexing completed on 2024-12-22 04:12:39

0001 /* This file is part of the KDE project
0002  * SPDX-FileCopyrightText: 2012 Arjen Hiemstra <ahiemstra@heimr.nl>
0003  *
0004  *  SPDX-License-Identifier: GPL-2.0-or-later
0005  */
0006 
0007 #include "kis_abstract_input_action.h"
0008 
0009 #include <QPointF>
0010 #include <QMouseEvent>
0011 #include <klocalizedstring.h>
0012 #include <kis_debug.h>
0013 
0014 class Q_DECL_HIDDEN KisAbstractInputAction::Private
0015 {
0016 public:
0017     QString id;
0018     QString name;
0019     QString description;
0020     QHash<QString, int> indexes;
0021 
0022     QPointF lastCursorPosition;
0023     QPointF startCursorPosition;
0024 
0025     static KisInputManager *inputManager;
0026 };
0027 
0028 KisInputManager *KisAbstractInputAction::Private::inputManager = 0;
0029 
0030 KisAbstractInputAction::KisAbstractInputAction(const QString &id)
0031     : d(new Private)
0032 {
0033     d->id = id;
0034     d->indexes.insert(i18n("Activate"), 0);
0035 }
0036 
0037 KisAbstractInputAction::~KisAbstractInputAction()
0038 {
0039     delete d;
0040 }
0041 
0042 void KisAbstractInputAction::activate(int shortcut)
0043 {
0044     Q_UNUSED(shortcut);
0045 }
0046 
0047 void KisAbstractInputAction::deactivate(int shortcut)
0048 {
0049     Q_UNUSED(shortcut);
0050 }
0051 
0052 void KisAbstractInputAction::begin(int shortcut, QEvent *event)
0053 {
0054     Q_UNUSED(shortcut);
0055 
0056     if (event) {
0057         d->lastCursorPosition = eventPosF(event);
0058         d->startCursorPosition = d->lastCursorPosition;
0059     }
0060 }
0061 
0062 void KisAbstractInputAction::inputEvent(QEvent *event)
0063 {
0064     if (event) {
0065         QPointF newPosition = eventPosF(event);
0066         cursorMoved(d->lastCursorPosition, newPosition);
0067         cursorMovedAbsolute(d->startCursorPosition, newPosition);
0068         d->lastCursorPosition = newPosition;
0069     }
0070 }
0071 
0072 void KisAbstractInputAction::end(QEvent *event)
0073 {
0074     Q_UNUSED(event);
0075 }
0076 
0077 void KisAbstractInputAction::cursorMoved(const QPointF &lastPos, const QPointF &pos)
0078 {
0079     Q_UNUSED(lastPos);
0080     Q_UNUSED(pos);
0081 }
0082 
0083 void KisAbstractInputAction::cursorMovedAbsolute(const QPointF &startPos, const QPointF &pos)
0084 {
0085     Q_UNUSED(startPos);
0086     Q_UNUSED(pos);
0087 }
0088 
0089 bool KisAbstractInputAction::supportsHiResInputEvents(int shortcut) const
0090 {
0091     Q_UNUSED(shortcut);
0092     return false;
0093 }
0094 
0095 KisInputActionGroup KisAbstractInputAction::inputActionGroup(int shortcut) const
0096 {
0097     Q_UNUSED(shortcut);
0098     return ModifyingActionGroup;
0099 }
0100 
0101 KisInputManager* KisAbstractInputAction::inputManager() const
0102 {
0103     return Private::inputManager;
0104 }
0105 
0106 QString KisAbstractInputAction::name() const
0107 {
0108     return d->name;
0109 }
0110 
0111 QString KisAbstractInputAction::description() const
0112 {
0113     return d->description;
0114 }
0115 
0116 int KisAbstractInputAction::priority() const
0117 {
0118     return 0;
0119 }
0120 
0121 bool KisAbstractInputAction::canIgnoreModifiers() const
0122 {
0123     return false;
0124 }
0125 
0126 QHash< QString, int > KisAbstractInputAction::shortcutIndexes() const
0127 {
0128     return d->indexes;
0129 }
0130 
0131 QString KisAbstractInputAction::id() const
0132 {
0133     return d->id;
0134 }
0135 
0136 void KisAbstractInputAction::setName(const QString &name)
0137 {
0138     d->name = name;
0139 }
0140 
0141 void KisAbstractInputAction::setDescription(const QString &description)
0142 {
0143     d->description = description;
0144 }
0145 
0146 void KisAbstractInputAction::setShortcutIndexes(const QHash< QString, int > &indexes)
0147 {
0148     d->indexes = indexes;
0149 }
0150 
0151 void KisAbstractInputAction::setInputManager(KisInputManager *manager)
0152 {
0153     Private::inputManager = manager;
0154 }
0155 
0156 bool KisAbstractInputAction::isShortcutRequired(int shortcut) const
0157 {
0158     Q_UNUSED(shortcut);
0159     return false;
0160 }
0161 
0162 QPoint KisAbstractInputAction::eventPos(const QEvent *event)
0163 {
0164     if(!event) {
0165         return QPoint();
0166     }
0167 
0168     switch (event->type()) {
0169     case QEvent::MouseMove:
0170     case QEvent::MouseButtonPress:
0171     case QEvent::MouseButtonDblClick:
0172     case QEvent::MouseButtonRelease:
0173         return static_cast<const QMouseEvent*>(event)->pos();
0174 
0175     case QEvent::TabletMove:
0176     case QEvent::TabletPress:
0177     case QEvent::TabletRelease:
0178         return static_cast<const QTabletEvent*>(event)->pos();
0179 
0180     case QEvent::TouchBegin:
0181     case QEvent::TouchUpdate:
0182     case QEvent::TouchEnd:
0183         return static_cast<const QTouchEvent *>(event)->touchPoints().at(0).pos().toPoint();
0184 
0185     case QEvent::Wheel:
0186         return static_cast<const QWheelEvent*>(event)->pos();
0187 
0188     case QEvent::NativeGesture:
0189         return static_cast<const QNativeGestureEvent*>(event)->pos();
0190 
0191     default:
0192         warnInput << "KisAbstractInputAction" << d->name << "tried to process event data from an unhandled event type" << event->type();
0193         return QPoint();
0194     }
0195 }
0196 
0197 QPointF KisAbstractInputAction::eventPosF(const QEvent *event) {
0198 
0199     switch (event->type()) {
0200     case QEvent::MouseMove:
0201     case QEvent::MouseButtonPress:
0202     case QEvent::MouseButtonDblClick:
0203     case QEvent::MouseButtonRelease:
0204         return static_cast<const QMouseEvent*>(event)->localPos();
0205 
0206     case QEvent::TabletMove:
0207     case QEvent::TabletPress:
0208     case QEvent::TabletRelease:
0209         return static_cast<const QTabletEvent*>(event)->posF();
0210 
0211     case QEvent::TouchBegin:
0212     case QEvent::TouchUpdate:
0213     case QEvent::TouchEnd:
0214         return static_cast<const QTouchEvent *>(event)->touchPoints().at(0).pos();
0215 
0216     case QEvent::Wheel:
0217         return static_cast<const QWheelEvent*>(event)->posF();
0218 
0219     case QEvent::NativeGesture:
0220         return QPointF(static_cast<const QNativeGestureEvent*>(event)->pos());
0221 
0222     default:
0223         warnInput << "KisAbstractInputAction" << d->name << "tried to process event data from an unhandled event type" << event->type();
0224         return QPointF();
0225     }
0226 }
0227 
0228 bool KisAbstractInputAction::isAvailable() const
0229 {
0230     return true;
0231 }