Krita Source Code Documentation
Loading...
Searching...
No Matches
kis_tool_invocation_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
8
9#include <kis_debug.h>
10
11#include <klocalizedstring.h>
12
13#include <KoToolManager.h>
14
15#include <kis_tool_proxy.h>
16#include <kis_canvas2.h>
18
19#include "kis_tool.h"
20#include "kis_input_manager.h"
21#include "kis_image.h"
22
33
35 : KisAbstractInputAction("Tool Invocation")
36 , d(new Private)
37{
38 setName(i18n("Tool Invocation"));
39 setDescription(i18n("The <i>Tool Invocation</i> action invokes the current tool, for example, using the brush tool, it will start painting."));
40
41 QHash<QString, int> indexes;
42 indexes.insert(i18n("Activate"), ActivateShortcut);
43 indexes.insert(i18n("Confirm"), ConfirmShortcut);
44 indexes.insert(i18n("Cancel"), CancelShortcut);
45 indexes.insert(i18n("Activate Line Tool"), LineToolShortcut);
46 indexes.insert(i18n("Activate Ellipse Tool"), EllipseToolShortcut);
47 indexes.insert(i18n("Activate Rectangle Tool"), RectToolShortcut);
48 indexes.insert(i18n("Activate Move Tool"), MoveToolShortcut);
49 indexes.insert(i18n("Activate Fill Tool"), FillToolShortcut);
50 indexes.insert(i18n("Activate Gradient Tool"), GradientToolShortcut);
51 indexes.insert(i18n("Activate Measure Tool"), MeasureToolShortcut);
52 indexes.insert(i18n("Activate Elliptical Selection Tool"), EllipseSelToolShortcut);
53 indexes.insert(i18n("Activate Rectangular Selection Tool"), RectSelToolShortcut);
54 indexes.insert(i18n("Activate Contiguous Selection Tool"), ContigSelToolShortcut);
55 indexes.insert(i18n("Activate Freehand Selection Tool"), FreehandSelToolShortcut);
56 indexes.insert(i18n("Activate with Other Color"), ActivateWithOtherColorShortcut);
57
59}
60
65
67{
68 Q_UNUSED(shortcut);
69 if (!inputManager()) return;
70
71 QString temporaryToolId;
72 switch(shortcut) {
74 temporaryToolId = "KritaShape/KisToolLine";
75 break;
77 temporaryToolId = "KritaShape/KisToolEllipse";
78 break;
80 temporaryToolId = "KritaShape/KisToolRectangle";
81 break;
83 temporaryToolId = "KritaTransform/KisToolMove";
84 break;
86 temporaryToolId = "KritaFill/KisToolFill";
87 break;
89 temporaryToolId = "KritaFill/KisToolGradient";
90 break;
92 temporaryToolId = "KritaShape/KisToolMeasure";
93 break;
95 temporaryToolId = "KisToolSelectElliptical";
96 break;
98 temporaryToolId = "KisToolSelectRectangular";
99 break;
101 temporaryToolId = "KisToolSelectContiguous";
102 break;
104 temporaryToolId = "KisToolSelectOutline";
105 break;
106 }
107
108 if (!temporaryToolId.isEmpty()) {
110 }
111
113
115
116 if (d->isUsingOtherColor) {
117 inputManager()->canvas()->resourceManager()->setUsingOtherColor(true);
118 }
119
120 d->activatedToolProxy->activateToolAction(KisTool::Primary);
121}
122
124{
125 Q_UNUSED(shortcut);
126 if (!inputManager()) return;
127
128 // Revert UsingOtherColor
129 if (d->isUsingOtherColor) {
130 inputManager()->canvas()->resourceManager()->setUsingOtherColor(false);
131 }
132
137 if (d->activatedToolProxy) {
138 d->activatedToolProxy->deactivateToolAction(KisTool::Primary);
139 d->activatedToolProxy.clear();
140 }
141
142 if (shortcut != ActivateShortcut && shortcut != ConfirmShortcut && shortcut != CancelShortcut && shortcut != ActivateWithOtherColorShortcut && !d->deactivating) {
143 // Switching tool will force deactivation and will re-enter
144 d->deactivating = true;
146 d->deactivating = false;
147 }
148}
149
151{
152 return 0;
153}
154
156{
157 return true;
158}
159
160void KisToolInvocationAction::begin(int shortcut, QEvent *event)
161{
162 if (shortcut != ConfirmShortcut && shortcut != CancelShortcut ) {
164 d->active =
165 d->runningToolProxy->forwardEvent(
167 }
168 else if (shortcut == ConfirmShortcut) {
169 QKeyEvent pressEvent(QEvent::KeyPress, Qt::Key_Return, QFlags<Qt::KeyboardModifier>());
170 inputManager()->toolProxy()->keyPressEvent(&pressEvent);
171 QKeyEvent releaseEvent(QEvent::KeyRelease, Qt::Key_Return, QFlags<Qt::KeyboardModifier>());
172 inputManager()->toolProxy()->keyReleaseEvent(&releaseEvent);
173
181
191 inputManager()->toolProxy()->explicitUserStrokeEndRequest();
192
193 } else if (shortcut == CancelShortcut) {
200 }
201}
202
204{
205 if (d->active) {
206 // It might happen that the action is still running, while the
207 // canvas has been removed, which kills the toolProxy.
209 if (d->runningToolProxy) {
211 forwardEvent(KisToolProxy::END, KisTool::Primary, event, event);
212 d->runningToolProxy.clear();
213 }
214 d->active = false;
215 }
216
218}
219
221{
222 if (!d->active) return;
223 if (!d->runningToolProxy) return;
224
226 forwardEvent(KisToolProxy::CONTINUE, KisTool::Primary, event, event);
227}
228
230{
231 bool savedState = d->active;
232 KisToolProxy *savedToolProxy = d->runningToolProxy;
233 if (!d->runningToolProxy) {
235 }
236 d->active = true;
237 inputEvent(event);
238 d->active = savedState;
239 d->runningToolProxy = savedToolProxy;
240}
241
243{
244 Q_UNUSED(shortcut);
245 return inputManager()->toolProxy()->primaryActionSupportsHiResEvents();
246}
247
249{
250 // These three shortcuts are pretty important for basic user interaction,
251 // so they should always exist.
252 return (shortcut == ActivateShortcut || shortcut == ConfirmShortcut || shortcut == CancelShortcut);
253}
254
256{
257 Q_UNUSED(shortcut);
259}
KisInputActionGroup
@ ToolInvoactionActionGroup
Abstract base class for input actions.
static KisInputManager * inputManager
void setShortcutIndexes(const QHash< QString, int > &indexes)
void setName(const QString &name)
virtual void end(QEvent *event)
void setDescription(const QString &description)
KisImageWSP image() const
void requestStrokeCancellation()
void requestStrokeEnd()
KisCanvas2 * canvas() const
QPointer< KisToolProxy > toolProxy() const
void begin(int shortcut, QEvent *event) override
bool supportsHiResInputEvents(int shortcut) const override
void inputEvent(QEvent *event) override
void deactivate(int shortcut) override
bool isShortcutRequired(int shortcut) const override
void activate(int shortcut) override
KisInputActionGroup inputActionGroup(int shortcut) const override
void end(QEvent *event) override
QPointer< KoCanvasResourceProvider > resourceManager
void switchToolRequested(const QString &id)
void switchBackRequested()
static KoToolManager * instance()
Return the toolmanager singleton.
#define KIS_SAFE_ASSERT_RECOVER_NOOP(cond)
Definition kis_assert.h:130
@ Primary
Definition kis_tool.h:118