Krita Source Code Documentation
Loading...
Searching...
No Matches
kis_tool_proxy.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2011 Dmitry Kazakov <dimula73@gmail.com>
3 *
4 * SPDX-License-Identifier: GPL-2.0-or-later
5 */
6
7#include "kis_tool_proxy.h"
8#include "kis_canvas2.h"
10
11#include <KoToolProxy_p.h>
12
13
15 : KoToolProxy(canvas, parent),
16 m_isActionActivated(false),
17 m_lastAction(KisTool::Primary)
18{
19}
20
22{
23 connect(image, SIGNAL(sigUndoDuringStrokeRequested()), SLOT(requestUndoDuringStroke()), Qt::UniqueConnection);
24 connect(image, SIGNAL(sigRedoDuringStrokeRequested()), SLOT(requestRedoDuringStroke()), Qt::UniqueConnection);
25 connect(image, SIGNAL(sigStrokeCancellationRequested()), SLOT(requestStrokeCancellation()), Qt::UniqueConnection);
26 connect(image, SIGNAL(sigStrokeEndRequested()), SLOT(requestStrokeEnd()), Qt::UniqueConnection);
27}
28
29QPointF KisToolProxy::tabletToDocument(const QPointF &globalPos)
30{
31 const QPointF pos = globalPos - QPointF(canvas()->canvasWidget()->mapToGlobal(QPoint(0, 0)));
32 return widgetToDocument(pos);
33}
34
35QPointF KisToolProxy::widgetToDocument(const QPointF &widgetPoint) const
36{
37 KisCanvas2 *kritaCanvas = dynamic_cast<KisCanvas2*>(canvas());
38 Q_ASSERT(kritaCanvas);
39
40 return kritaCanvas->coordinatesConverter()->widgetToDocument(widgetPoint);
41}
42
43QPointF KisToolProxy::documentToWidget(const QPointF &documentPoint) const
44{
45 KisCanvas2 *kritaCanvas = dynamic_cast<KisCanvas2*>(canvas());
46 Q_ASSERT(kritaCanvas);
47
48 return kritaCanvas->coordinatesConverter()->documentToWidget(documentPoint);
49}
50
51KoPointerEvent KisToolProxy::convertEventToPointerEvent(QEvent *event, const QPointF &docPoint, bool *result)
52{
53 switch (event->type()) {
54 case QEvent::TabletPress:
55 case QEvent::TabletRelease:
56 case QEvent::TabletMove:
57 {
58 *result = true;
59 QTabletEvent *tabletEvent = static_cast<QTabletEvent*>(event);
60 KoPointerEvent ev(tabletEvent, docPoint);
61 return ev;
62 }
63 case QEvent::MouseButtonPress:
64 case QEvent::MouseButtonDblClick:
65 case QEvent::MouseButtonRelease:
66 case QEvent::MouseMove:
67 {
68 *result = true;
69 QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event);
70 return KoPointerEvent(mouseEvent, docPoint);
71 }
72 case QEvent::TouchBegin:
73 case QEvent::TouchUpdate:
74 case QEvent::TouchEnd:
75 case QEvent::TouchCancel:
76 {
77 *result = true;
78 QTouchEvent *touchEvent = static_cast<QTouchEvent *> (event);
79 return KoPointerEvent(touchEvent, docPoint);
80 }
81 default:
82 ;
83 }
84
85 *result = false;
86 QMouseEvent fakeEvent(QEvent::MouseMove, QPoint(),
87 Qt::NoButton, Qt::NoButton,
88 Qt::NoModifier);
89
90 return KoPointerEvent(&fakeEvent, QPointF());
91}
92
94{
95 switch (event->type()) {
96 case QEvent::TabletMove: {
97 QTabletEvent *tabletEvent = static_cast<QTabletEvent*>(event);
98 QPointF docPoint = widgetToDocument(tabletEvent->posF());
99 this->tabletEvent(tabletEvent, docPoint);
100 return;
101 }
102
103 case QEvent::MouseMove: {
104 QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event);
105 QPointF docPoint = widgetToDocument(mouseEvent->localPos());
106 mouseMoveEvent(mouseEvent, docPoint);
107 return;
108 }
109
110 default: {
111 qWarning() << "forwardHoverEvent encountered unknown event type:"
112 << event->type();
113 return;
114 }
115 }
116}
117
118bool KisToolProxy::forwardEvent(ActionState state, KisTool::ToolAction action, QEvent *event, QEvent *originalEvent)
119{
120 bool retval = true;
121
122 QTabletEvent *tabletEvent = dynamic_cast<QTabletEvent*>(event);
123 QMouseEvent *mouseEvent = dynamic_cast<QMouseEvent*>(event);
124 QTouchEvent *touchEvent = dynamic_cast<QTouchEvent *> (event);
125
126 if (tabletEvent) {
127 QPointF docPoint = widgetToDocument(tabletEvent->posF());
128 tabletEvent->accept();
129 this->tabletEvent(tabletEvent, docPoint);
130 forwardToTool(state, action, tabletEvent, docPoint);
131 retval = tabletEvent->isAccepted();
132 }
133 else if (mouseEvent) {
134 QPointF docPoint = widgetToDocument(mouseEvent->localPos());
135 mouseEvent->accept();
136 if (mouseEvent->type() == QEvent::MouseButtonPress) {
137 mousePressEvent(mouseEvent, docPoint);
138 } else if (mouseEvent->type() == QEvent::MouseButtonDblClick) {
139 mouseDoubleClickEvent(mouseEvent, docPoint);
140 } else if (mouseEvent->type() == QEvent::MouseButtonRelease) {
141 mouseReleaseEvent(mouseEvent, docPoint);
142 } else if (mouseEvent->type() == QEvent::MouseMove) {
143 mouseMoveEvent(mouseEvent, docPoint);
144 }
145 forwardToTool(state, action, originalEvent, docPoint);
146 retval = mouseEvent->isAccepted();
147 }
148 else if (touchEvent) {
149 QPointF docPoint = widgetToDocument(touchEvent->touchPoints().at(0).pos());
150 touchEvent->accept();
151 this->touchEvent(touchEvent, docPoint);
152 forwardToTool(state, action, touchEvent, docPoint);
153 retval = touchEvent->isAccepted();
154 }
155 else if (event && event->type() == QEvent::KeyPress) {
156 QKeyEvent* kevent = static_cast<QKeyEvent*>(event);
157 keyPressEvent(kevent);
158 }
159 else if (event && event->type() == QEvent::KeyRelease) {
160 QKeyEvent* kevent = static_cast<QKeyEvent*>(event);
161 keyReleaseEvent(kevent);
162 }
163
164 return retval;
165}
166
167void KisToolProxy::forwardToTool(ActionState state, KisTool::ToolAction action, QEvent *event, const QPointF &docPoint)
168{
169 bool eventValid = false;
170 KoPointerEvent ev = convertEventToPointerEvent(event, docPoint, &eventValid);
171 KisTool *activeTool = dynamic_cast<KisTool*>(priv()->activeTool);
172
173 if (!eventValid || !activeTool) return;
174
175 switch (state) {
176 case BEGIN:
177 if (action == KisTool::Primary) {
178 if (event->type() == QEvent::MouseButtonDblClick) {
179 activeTool->beginPrimaryDoubleClickAction(&ev);
180 } else {
181 activeTool->beginPrimaryAction(&ev);
182 }
183 } else {
184 if (event->type() == QEvent::MouseButtonDblClick) {
186 } else {
188 }
189 }
190 Q_EMIT toolPrimaryActionActivated(true);
191 break;
192 case CONTINUE:
193 if (action == KisTool::Primary) {
194 activeTool->continuePrimaryAction(&ev);
195 } else {
197 }
198 break;
199 case END:
200 if (action == KisTool::Primary) {
201 activeTool->endPrimaryAction(&ev);
202 } else {
204 }
205 Q_EMIT toolPrimaryActionActivated(false);
206 break;
207 }
208}
209
211{
212 KisTool *activeTool = dynamic_cast<KisTool*>(const_cast<KisToolProxy*>(this)->priv()->activeTool);
213 return activeTool && activeTool->primaryActionSupportsHiResEvents();
214}
215
217{
218 KisTool *activeTool = dynamic_cast<KisTool*>(const_cast<KisToolProxy*>(this)->priv()->activeTool);
219 return activeTool && activeTool->alternateActionSupportsHiResEvents(action);
220}
221
223{
224 if (!tool) return;
225
230 } else {
232 }
233}
234
236{
237 KisTool *activeTool = dynamic_cast<KisTool*>(const_cast<KisToolProxy*>(this)->priv()->activeTool);
238
239 if (activeTool) {
240 if (action == KisTool::Primary) {
241 activeTool->activatePrimaryAction();
242 } else {
244 }
245 }
246
247 m_isActionActivated = true;
248 m_lastAction = action;
249}
250
252{
253 KisTool *activeTool = dynamic_cast<KisTool*>(const_cast<KisToolProxy*>(this)->priv()->activeTool);
254
255 if (activeTool) {
256 if (action == KisTool::Primary) {
257 activeTool->deactivatePrimaryAction();
258 } else {
260 }
261 }
262
263 m_isActionActivated = false;
264 m_lastAction = action;
265}
266
268{
269 KisTool *activeTool = dynamic_cast<KisTool*>(const_cast<KisToolProxy*>(this)->priv()->activeTool);
270 return activeTool && activeTool->supportsPaintingAssistants();
271}
connect(this, SIGNAL(optionsChanged()), this, SLOT(saveOptions()))
KisCoordinatesConverter * coordinatesConverter
_Private::Traits< T >::Result widgetToDocument(const T &obj) const
_Private::Traits< T >::Result documentToWidget(const T &obj) const
QPointF documentToWidget(const QPointF &documentPoint) const override
void deactivateToolAction(KisTool::ToolAction action)
void forwardToTool(ActionState state, KisTool::ToolAction action, QEvent *event, const QPointF &docPoint)
KisTool::ToolAction m_lastAction
bool primaryActionSupportsHiResEvents() const
QPointF widgetToDocument(const QPointF &widgetPoint) const override
void forwardHoverEvent(QEvent *event)
void initializeImage(KisImageSP image)
void activateToolAction(KisTool::ToolAction action)
QPointF tabletToDocument(const QPointF &globalPos)
bool supportsPaintingAssistants() const
bool alternateActionSupportsHiResEvents(KisTool::AlternateAction action) const
void toolPrimaryActionActivated(bool activated)
void setActiveTool(KoToolBase *tool) override
Set the new active tool.
bool forwardEvent(ActionState state, KisTool::ToolAction action, QEvent *event, QEvent *originalEvent)
KoPointerEvent convertEventToPointerEvent(QEvent *event, const QPointF &docPoint, bool *result)
KisToolProxy(KoCanvasBase *canvas, QObject *parent=0)
bool m_isActionActivated
KoToolBase * activeTool
void mouseMoveEvent(QMouseEvent *event, const QPointF &point)
Forwarded to the current KoToolBase.
void mousePressEvent(QMouseEvent *event, const QPointF &point)
Forwarded to the current KoToolBase.
void requestUndoDuringStroke()
Forwarded to the current KoToolBase.
void requestRedoDuringStroke()
Forwarded to the current KoToolBase.
KoCanvasBase * canvas() const
void tabletEvent(QTabletEvent *event, const QPointF &point)
Forwarded to the current KoToolBase.
void touchEvent(QTouchEvent *event, const QPointF &point)
void mouseDoubleClickEvent(QMouseEvent *event, const QPointF &point)
Forwarded to the current KoToolBase.
KoToolProxyPrivate * priv()
void keyReleaseEvent(QKeyEvent *event)
Forwarded to the current KoToolBase.
void requestStrokeEnd()
Forwarded to the current KoToolBase.
void requestStrokeCancellation()
Forwarded to the current KoToolBase.
virtual void setActiveTool(KoToolBase *tool)
Set the new active tool.
void keyPressEvent(QKeyEvent *event)
Forwarded to the current KoToolBase.
void mouseReleaseEvent(QMouseEvent *event, const QPointF &point)
Forwarded to the current KoToolBase.
virtual void activatePrimaryAction()
Definition kis_tool.cc:421
virtual void beginPrimaryAction(KoPointerEvent *event)
Definition kis_tool.cc:431
virtual void continueAlternateAction(KoPointerEvent *event, AlternateAction action)
Definition kis_tool.cc:477
@ Primary
Definition kis_tool.h:118
virtual void activateAlternateAction(AlternateAction action)
Definition kis_tool.cc:456
virtual void deactivateAlternateAction(AlternateAction action)
Definition kis_tool.cc:461
virtual void endAlternateAction(KoPointerEvent *event, AlternateAction action)
Definition kis_tool.cc:483
virtual void endPrimaryAction(KoPointerEvent *event)
Definition kis_tool.cc:446
virtual void continuePrimaryAction(KoPointerEvent *event)
Definition kis_tool.cc:441
virtual bool alternateActionSupportsHiResEvents(AlternateAction action) const
Definition kis_tool.cc:489
virtual void beginAlternateDoubleClickAction(KoPointerEvent *event, AlternateAction action)
Definition kis_tool.cc:472
virtual void beginAlternateAction(KoPointerEvent *event, AlternateAction action)
Definition kis_tool.cc:466
virtual void deactivatePrimaryAction()
Definition kis_tool.cc:426
AlternateAction
Definition kis_tool.h:134
virtual bool primaryActionSupportsHiResEvents() const
Definition kis_tool.cc:451
virtual void beginPrimaryDoubleClickAction(KoPointerEvent *event)
Definition kis_tool.cc:436
static AlternateAction actionToAlternateAction(ToolAction action)
Definition kis_tool.cc:416
virtual bool supportsPaintingAssistants() const
Definition kis_tool.cc:495