Krita Source Code Documentation
Loading...
Searching...
No Matches
kis_pan_action.cpp
Go to the documentation of this file.
1/* This file is part of the KDE project
2 * SPDX-FileCopyrightText: 2012 Arjen Hiemstra <ahiemstra@heimr.nl>
3 *
4 * SPDX-License-Identifier: GPL-2.0-or-later
5 */
6
7#include "kis_pan_action.h"
8
9#include <kis_debug.h>
10#include <QMouseEvent>
11#include <QApplication>
12#include <QGesture>
13
14#include <klocalizedstring.h>
15
16#include <KoCanvasController.h>
17
18#include <kis_canvas2.h>
19
20#include "kis_input_manager.h"
21
24
25
27{
28public:
29 Private() : panDistance(10) { }
30
31 const int panDistance;
32
34
35 std::unique_ptr<KisCanvasNavigationActionStrategy> actionStrategy;
36};
37
39 : KisAbstractInputAction("Pan Canvas")
40 , d(new Private)
41{
42 setName(i18n("Pan Canvas"));
43 setDescription(i18n("The <i>Pan Canvas</i> action pans the canvas."));
44
45 QHash<QString, int> shortcuts;
46 shortcuts.insert(i18n("Pan Mode"), PanModeShortcut);
47 shortcuts.insert(i18n("Pan Left"), PanLeftShortcut);
48 shortcuts.insert(i18n("Pan Right"), PanRightShortcut);
49 shortcuts.insert(i18n("Pan Up"), PanUpShortcut);
50 shortcuts.insert(i18n("Pan Down"), PanDownShortcut);
51 setShortcutIndexes(shortcuts);
52}
53
55{
56 delete d;
57}
58
60{
61 return 5;
62}
63
64void KisPanAction::activate(int shortcut)
65{
66 Q_UNUSED(shortcut);
67 QApplication::setOverrideCursor(Qt::OpenHandCursor);
68}
69
70void KisPanAction::deactivate(int shortcut)
71{
72 Q_UNUSED(shortcut);
73 QApplication::restoreOverrideCursor();
74}
75
76void KisPanAction::begin(int shortcut, QEvent *event)
77{
78 KisAbstractInputAction::begin(shortcut, event);
79
83 if (event
84 && (event->type() == QEvent::NativeGesture || event->type() == QEvent::TouchBegin
85 || event->type() == QEvent::TouchUpdate)
86 && shortcut == PanModeShortcut) {
87
89 using Flags = KisCanvasNavigationActionStrategyNativeGesture::Flags;
90
91 Flags flags;
92 flags.setFlag(Flag::PanEnabled);
93
94 if (event->type() == QEvent::NativeGesture) {
96 } else {
97 const QTouchEvent *tevent = static_cast<const QTouchEvent*>(event);
99 }
100
101 // native gestures don't have cursor tracking by the OS, so they shouldn't show any cursor
102 QApplication::restoreOverrideCursor();
103 return;
104 }
105
106 bool overrideCursor = true;
107
108 switch (shortcut) {
109 case PanModeShortcut: {
110 if (event->type() == QEvent::Wheel) {
111 // Some QT wheel events are actually be touch pad pan events. From the QT docs:
112 // "Wheel events are generated for both mouse wheels and trackpad scroll gestures."
113 QWheelEvent *wheelEvent = static_cast<QWheelEvent*>(event);
114 inputManager()->canvas()->canvasController()->pan(-wheelEvent->pixelDelta());
115
116 // native gestures don't have cursor tracking by the OS, so they shouldn't show any cursor
117 QApplication::restoreOverrideCursor();
118
119 return;
120 } else {
122 }
123
124 break;
125 }
126 case PanLeftShortcut:
127 inputManager()->canvas()->canvasController()->pan(QPoint(d->panDistance, 0));
128 break;
129 case PanRightShortcut:
130 inputManager()->canvas()->canvasController()->pan(QPoint(-d->panDistance, 0));
131 break;
132 case PanUpShortcut:
133 inputManager()->canvas()->canvasController()->pan(QPoint(0, d->panDistance));
134 break;
135 case PanDownShortcut:
136 inputManager()->canvas()->canvasController()->pan(QPoint(0, -d->panDistance));
137 break;
138 }
139
140 if (overrideCursor) {
141 QApplication::setOverrideCursor(Qt::ClosedHandCursor);
142 }
143}
144
145void KisPanAction::end(QEvent *event)
146{
147 d->actionStrategy.reset();
148 QApplication::restoreOverrideCursor();
150}
151
152void KisPanAction::inputEvent(QEvent *event)
153{
154 if(!event) {
155 return;
156 }
157
158 if (d->actionStrategy && d->actionStrategy->supportsEvent(event)) {
159 d->actionStrategy->inputEvent(event);
160 } else if (event->type() == QEvent::Wheel) {
161 QWheelEvent *wheelEvent = static_cast<QWheelEvent*>(event);
162 inputManager()->canvas()->canvasController()->pan(-wheelEvent->pixelDelta());
163 } else {
165 }
166}
167
168void KisPanAction::cursorMovedAbsolute(const QPointF &startPos, const QPointF &pos)
169{
171}
172
173bool KisPanAction::isShortcutRequired(int shortcut) const
174{
175 return shortcut == PanModeShortcut;
176}
177
179{
180 Q_UNUSED(shortcut);
182}
183
185{
186 Q_UNUSED(shortcut);
187 return true;
188}
KisInputActionGroup
@ ViewTransformActionGroup
Abstract base class for input actions.
virtual void inputEvent(QEvent *event)
static KisInputManager * inputManager
void setShortcutIndexes(const QHash< QString, int > &indexes)
QPointF eventPosF(const QEvent *event)
void setName(const QString &name)
virtual void end(QEvent *event)
void setDescription(const QString &description)
virtual void begin(int shortcut, QEvent *event)
KisCanvas2 * canvas() const
std::unique_ptr< KisCanvasNavigationActionStrategy > actionStrategy
void inputEvent(QEvent *event) override
void end(QEvent *event) override
void cursorMovedAbsolute(const QPointF &lastPos, const QPointF &pos) override
~KisPanAction() override
bool isShortcutRequired(int shortcut) const override
void activate(int shortcut) override
void begin(int shortcut, QEvent *event) override
bool supportsHiResInputEvents(int shortcut) const override
KisInputActionGroup inputActionGroup(int shortcut) const override
int priority() const override
Private *const d
@ PanRightShortcut
Pan right by a fixed amount.
@ PanModeShortcut
Toggle the pan mode.
@ PanUpShortcut
Pan up by a fixed amount.
@ PanDownShortcut
Pan down by a fixed amount.
@ PanLeftShortcut
Pan left by a fixed amount.
void deactivate(int shortcut) override
KoCanvasController * canvasController() const
virtual QPointF preferredCenter() const =0
Returns the currently set preferred center point in view coordinates (pixels)
virtual void setPreferredCenter(const QPointF &viewPoint)=0
virtual void pan(const QPoint &distance)=0
KisCanvas2 * canvas