Krita Source Code Documentation
Loading...
Searching...
No Matches
KoToolProxy.cpp
Go to the documentation of this file.
1/* This file is part of the KDE project
2 * SPDX-FileCopyrightText: 2006-2007 Thomas Zander <zander@kde.org>
3 * SPDX-FileCopyrightText: 2006-2011 Boudewijn Rempt <boud@valdyas.org>
4 *
5 * SPDX-License-Identifier: LGPL-2.0-or-later
6 */
7#include "KoToolProxy.h"
8#include "KoToolProxy_p.h"
9
10#include <QMimeData>
11#include <QUrl>
12#include <QApplication>
13#include <QTouchEvent>
14#include <QClipboard>
15#include <QEvent>
16
17#include <kundo2command.h>
18#include <KoProperties.h>
19
20#include <FlakeDebug.h>
21#include <klocalizedstring.h>
22
23#include "KoToolBase.h"
24#include "KoPointerEvent.h"
25#include "KoInputDevice.h"
26#include "KoToolManager_p.h"
27#include "KoToolSelection.h"
28#include "KoCanvasBase.h"
29#include "KoCanvasController.h"
30#include "KoShapeManager.h"
31#include "KoSelection.h"
32#include "KoShapeLayer.h"
33#include "KoShapeRegistry.h"
34#include "KoShapeController.h"
35#include "KoViewConverter.h"
36#include "KoShapeFactoryBase.h"
37#include "kis_assert.h"
38#include "kactioncollection.h"
39#include "kis_global.h"
40#include "kis_algebra_2d.h"
41
42
44 : parent(p)
45{
46 scrollTimer.setInterval(100);
47}
48
49void KoToolProxyPrivate::timeout() // Auto scroll the canvas
50{
51 Q_ASSERT(controller);
52
53 const QPoint originalWidgetPoint = parent->documentToWidget(widgetScrollPointDoc).toPoint();
54
55 const QPointF margin(10.0, 10.0);
56
57 const QPointF mouseAreaTopLeftWidget = parent->documentToWidget(widgetScrollPointDoc) - margin;
58 const QPointF mouseAreaBottomRightWidget = mouseAreaTopLeftWidget + 2 * margin;
59
60 const QPointF mouseAreaTopLeftDoc = parent->widgetToDocument(mouseAreaTopLeftWidget);
61 const QPointF mouseAreaBottomRightDoc = parent->widgetToDocument(mouseAreaBottomRightWidget);
62 QRectF mouseAreaDoc(mouseAreaTopLeftDoc, mouseAreaBottomRightDoc);
63
64
65 const QPointF oldPreferredCenter = controller->preferredCenter();
66
67 controller->ensureVisibleDoc(mouseAreaDoc, true);
68
69 const QPointF newPreferredCenter = controller->preferredCenter();
70
71 // if scrolling has happened, then just return!
72 if (oldPreferredCenter == newPreferredCenter) {
73 return;
74 }
75
76 widgetScrollPointDoc = parent->widgetToDocument(originalWidgetPoint);
77
78 QMouseEvent event(QEvent::MouseMove, originalWidgetPoint, Qt::LeftButton, Qt::LeftButton, QFlags<Qt::KeyboardModifier>());
81}
82
84{
85 if (controller == 0) return;
86 if (!activeTool) return;
87 if (!activeTool->wantsAutoScroll()) return;
88 if (!event.isAccepted()) return;
89 if (!isToolPressed) return;
90 if (event.buttons() != Qt::LeftButton) return;
91
92
93 widgetScrollPointDoc = event.point;
94
95 if (! scrollTimer.isActive())
96 scrollTimer.start();
97}
98
100{
101 if (hasSelection == newSelection)
102 return;
103 hasSelection = newSelection;
105}
106
108{
109 if (!activeTool)
110 return false;
111
112 KoShapeManager * shapeManager = activeTool->canvas()->shapeManager();
113 KoShapeLayer * activeLayer = shapeManager->selection()->activeLayer();
114 if (activeLayer && !activeLayer->isShapeEditable())
115 return false;
116 return true;
117}
118
119KoToolProxy::KoToolProxy(KoCanvasBase *canvas, QObject *parent)
120 : QObject(parent),
121 d(new KoToolProxyPrivate(this))
122{
123 connect(&d->scrollTimer, SIGNAL(timeout()), this, SLOT(timeout()));
124}
125
127{
128 delete d;
129}
130
131void KoToolProxy::paint(QPainter &painter, const KoViewConverter &converter)
132{
133 if (d->activeTool) d->activeTool->paint(painter, converter);
134}
135
140
142{
143 return d->controller->canvas();
144}
145
147{
148 return d->multiClickCount;
149}
150
152{
153 QPointF globalPoint = ev->globalPos();
154
155 if (d->multiClickSource != eventType) {
156 d->multiClickCount = 0;
157 }
158
159 if (d->multiClickGlobalPoint != globalPoint) {
160 if (qAbs(globalPoint.x() - d->multiClickGlobalPoint.x()) > 5||
161 qAbs(globalPoint.y() - d->multiClickGlobalPoint.y()) > 5) {
162 d->multiClickCount = 0;
163 }
164 d->multiClickGlobalPoint = globalPoint;
165 }
166
167 if (d->multiClickCount && d->multiClickTimeStamp.elapsed() < QApplication::doubleClickInterval()) {
168 // One more multiclick;
170 } else {
171 d->multiClickTimeStamp.start();
172 d->multiClickCount = 1;
173 d->multiClickSource = QEvent::Type(eventType);
174 }
175
176 if (d->activeTool) {
177 switch (d->multiClickCount) {
178 case 0:
179 case 1:
181 break;
182 case 2:
184 break;
185 case 3:
186 default:
188 break;
189 }
190 } else {
191 d->multiClickCount = 0;
192 ev->ignore();
193 }
194
195}
196
197void KoToolProxy::tabletEvent(QTabletEvent *event, const QPointF &point)
198{
199 // We get these events exclusively from KisToolProxy - accept them
200 event->accept();
201
203 KoInputDevice::convertPointerType(event), event->uniqueId());
204 d->controller->toolManager()->priv()->switchInputDevice(id);
205
206 KoPointerEvent ev(event, point);
207
208 switch (event->type()) {
209 case QEvent::TabletPress:
210 countMultiClick(&ev, event->type());
211 break;
212 case QEvent::TabletRelease:
213 d->scrollTimer.stop();
214 if (d->activeTool)
216 break;
217 case QEvent::TabletMove:
218 if (d->activeTool)
220 d->checkAutoScroll(ev);
221 default:
222 ; // ignore the rest.
223 }
224
225 d->mouseLeaveWorkaround = true;
227}
228
230{
231 d->mouseLeaveWorkaround = false;
232 KoInputDevice id;
233 d->controller->toolManager()->priv()->switchInputDevice(id);
234 d->mouseDownPoint = ev->pos();
235
236
237 // this tries to make sure another mouse press event doesn't happen
238 // before a release event happens
239 if (d->isToolPressed) {
241 d->scrollTimer.stop();
242
243 if (d->activeTool) {
245 }
246
247 d->isToolPressed = false;
248
249 return;
250 }
251
252 countMultiClick(ev, QEvent::MouseButtonPress);
253
254 d->isToolPressed = true;
255}
256
257void KoToolProxy::mousePressEvent(QMouseEvent *event, const QPointF &point)
258{
259 KoPointerEvent ev(event, point);
260 mousePressEvent(&ev);
262}
263
264void KoToolProxy::mouseDoubleClickEvent(QMouseEvent *event, const QPointF &point)
265{
266 KoPointerEvent ev(event, point);
269}
270
272{
273 // let us handle it as any other mousepress (where we then detect multi clicks
274 mousePressEvent(event);
275}
276
277void KoToolProxy::mouseMoveEvent(QMouseEvent *event, const QPointF &point)
278{
279 KoPointerEvent ev(event, point);
280 mouseMoveEvent(&ev);
282}
283
285{
286 if (d->mouseLeaveWorkaround) {
287 d->mouseLeaveWorkaround = false;
288 return;
289 }
290 KoInputDevice id;
291 d->controller->toolManager()->priv()->switchInputDevice(id);
292 if (d->activeTool == 0) {
293 event->ignore();
294 return;
295 }
296
297 d->activeTool->mouseMoveEvent(event);
298
299 d->checkAutoScroll(*event);
300}
301
302void KoToolProxy::mouseReleaseEvent(QMouseEvent *event, const QPointF &point)
303{
304 KoPointerEvent ev(event, point);
307}
308
310{
311 d->mouseLeaveWorkaround = false;
312 KoInputDevice id;
313 d->controller->toolManager()->priv()->switchInputDevice(id);
314 d->scrollTimer.stop();
315
316 if (d->activeTool) {
318 } else {
319 event->ignore();
320 }
321
322 d->isToolPressed = false;
323}
324
325void KoToolProxy::keyPressEvent(QKeyEvent *event)
326{
327 if (d->activeTool)
328 d->activeTool->keyPressEvent(event);
329 else
330 event->ignore();
331}
332
333void KoToolProxy::keyReleaseEvent(QKeyEvent *event)
334{
335 if (d->activeTool)
337 else
338 event->ignore();
339
340 d->isToolPressed = false;
341}
342
349
350QVariant KoToolProxy::inputMethodQuery(Qt::InputMethodQuery query) const
351{
352 if (d->activeTool)
353 return d->activeTool->inputMethodQuery(query);
354 return QVariant();
355}
356
357void KoToolProxy::inputMethodEvent(QInputMethodEvent *event)
358{
360}
361
362void KoToolProxy::focusInEvent(QFocusEvent *event)
363{
364 if (d->activeTool) d->activeTool->focusInEvent(event);
365}
366
367void KoToolProxy::focusOutEvent(QFocusEvent *event)
368{
369 if (d->activeTool) d->activeTool->focusOutEvent(event);
370}
371
373{
374 return d->activeTool ? d->activeTool->popupActionsMenu() : 0;
375}
376
381
383{
384 if (d->activeTool) {
385 disconnect(d->activeTool, SIGNAL(selectionChanged(bool)), this, SLOT(selectionChanged(bool)));
386 d->toolPriorityShortcuts.clear();
387 }
388
389 d->activeTool = tool;
390
391 if (tool) {
394 if (collection) {
395 Q_FOREACH(QAction *action, collection->actions()) {
396
397 const QVariant prop = action->property("tool_action");
398
399 if (prop.isValid()) {
400 const QStringList tools = prop.toStringList();
401
402 if (tools.contains(d->activeTool->toolId())) {
403 const QList<QKeySequence> shortcuts = action->shortcuts();
404 std::copy(shortcuts.begin(), shortcuts.end(),
405 std::back_inserter(d->toolPriorityShortcuts));
406 }
407 }
408 }
409 }
410
411 connect(d->activeTool, SIGNAL(selectionChanged(bool)), this, SLOT(selectionChanged(bool)));
413 Q_EMIT toolChanged(tool->toolId());
414 }
415}
416
417void KoToolProxy::touchEvent(QTouchEvent* event, const QPointF& point)
418{
419 // only one "touchpoint" events should be here
420 KoPointerEvent ev(event, point);
421
422 if (!d->activeTool) return;
423
424 switch (event->touchPointStates())
425 {
426 case Qt::TouchPointPressed:
427 countMultiClick(&ev, QEvent::TouchBegin);
428 break;
429 case Qt::TouchPointMoved:
431 break;
432 case Qt::TouchPointReleased:
434 break;
435 default: // don't care
436 ;
437 }
438
440}
441
446
451
456
458{
459 return d->activeTool ? d->activeTool->hasSelection() : false;
460}
461
463{
465 d->activeTool->cut();
466}
467
469{
470 if (d->activeTool)
471 d->activeTool->copy();
472}
473
475{
476 bool success = false;
477
478 if (d->activeTool && d->isActiveLayerEditable()) {
479 success = d->activeTool->paste();
480 }
481
482 return success;
483}
484
486{
487 bool success = false;
488
489 if (d->activeTool && d->isActiveLayerEditable()) {
490 success = d->activeTool->selectAll();
491 }
492
493 return success;
494}
495
497{
498 if (d->activeTool)
500}
501
502void KoToolProxy::dragMoveEvent(QDragMoveEvent *event, const QPointF &point)
503{
504 if (d->activeTool)
505 d->activeTool->dragMoveEvent(event, point);
506}
507
508void KoToolProxy::dragLeaveEvent(QDragLeaveEvent *event)
509{
510 if (d->activeTool)
511 d->activeTool->dragLeaveEvent(event);
512}
513
514void KoToolProxy::dropEvent(QDropEvent *event, const QPointF &point)
515{
516 if (d->activeTool)
517 d->activeTool->dropEvent(event, point);
518}
519
525
526void KoToolProxy::processEvent(QEvent *e) const
527{
528 if(e->type()==QEvent::ShortcutOverride
529 && d->activeTool
531 && (static_cast<QKeyEvent*>(e)->modifiers()==Qt::NoModifier ||
532 static_cast<QKeyEvent*>(e)->modifiers()==Qt::ShiftModifier
533#ifdef Q_OS_WIN
534 // we should disallow AltGr shortcuts if a text box is in focus
535 || (static_cast<QKeyEvent*>(e)->modifiers()==(Qt::AltModifier | Qt::ControlModifier) &&
536 static_cast<QKeyEvent*>(e)->key() < Qt::Key_Escape)
537#endif
538 )) {
539 e->accept();
540 }
541}
542
549
556
563
565{
566 if (d->activeTool) {
568 }
569}
570
572{
573 return d;
574}
575
576//have to include this because of Q_PRIVATE_SLOT
577#include "moc_KoToolProxy.cpp"
const Params2D p
A container for a set of QAction objects.
QList< QAction * > actions() const
The PopupWidgetInterface abstract class defines the basic interface that will be used by all popup wi...
virtual KoShapeManager * shapeManager() const =0
virtual QPointF preferredCenter() const =0
Returns the currently set preferred center point in view coordinates (pixels)
virtual void ensureVisibleDoc(const QRectF &docRect, bool smooth)=0
Scrolls the content of the canvas so that the given rect is visible.
KisKActionCollection * actionCollection
QPointer< KoToolManager > toolManager
virtual KoCanvasBase * canvas() const
static InputDevice convertDeviceType(QTabletEvent *event)
static Pointer convertPointerType(QTabletEvent *event)
QPoint globalPos() const
Return the position screen coordinates.
Qt::MouseButtons buttons() const
return buttons pressed (see QMouseEvent::buttons());
bool isAccepted() const
return if the event has been accepted.
KoPointerEventWrapper deepCopyEvent() const
QPoint pos() const
return the position in widget coordinates
KoShapeLayer * activeLayer() const
KoSelection * selection
virtual bool isShapeEditable(bool recursive=true) const
checks recursively if the shape or one of its parents is not visible or locked
Definition KoShape.cpp:968
virtual void requestRedoDuringStroke()
KoCanvasBase * canvas() const
Returns the canvas the tool is working on.
virtual void deleteSelection()
virtual void requestUndoDuringStroke()
virtual void requestStrokeCancellation()
virtual KisPopupWidgetInterface * popupWidget()
Definition KoToolBase.h:340
virtual void mouseTripleClickEvent(KoPointerEvent *event)
Q_INVOKABLE QString toolId() const
virtual void keyReleaseEvent(QKeyEvent *event)
virtual void focusInEvent(QFocusEvent *event)
virtual void dropEvent(QDropEvent *event, const QPointF &point)
virtual void copy() const
virtual bool paste()
virtual void focusOutEvent(QFocusEvent *event)
virtual void mousePressEvent(KoPointerEvent *event)=0
virtual void deselect()
deselect the tool should clear the selection if it has one.
virtual void repaintDecorations()
virtual void mouseMoveEvent(KoPointerEvent *event)=0
virtual bool hasSelection()
virtual void mouseReleaseEvent(KoPointerEvent *event)=0
virtual void keyPressEvent(QKeyEvent *event)
bool isInTextMode() const
virtual bool selectAll()
selectAll select all data the tool can select.
virtual QMenu * popupActionsMenu()
Definition KoToolBase.h:334
virtual void cut()
virtual void mouseDoubleClickEvent(KoPointerEvent *event)
virtual void requestStrokeEnd()
virtual void inputMethodEvent(QInputMethodEvent *event)
virtual QVariant inputMethodQuery(Qt::InputMethodQuery query) const
virtual void paint(QPainter &painter, const KoViewConverter &converter)=0
virtual bool wantsAutoScroll() const
virtual void dragMoveEvent(QDragMoveEvent *event, const QPointF &point)
virtual void explicitUserStrokeEndRequest()
explicitUserStrokeEndRequest is called by the input manager when the user presses the Enter key or an...
virtual void dragLeaveEvent(QDragLeaveEvent *event)
void selectionChanged(bool newSelection)
KoToolBase * activeTool
void setCanvasController(KoCanvasController *controller)
the toolManager tells us which KoCanvasController this toolProxy is working for.
QEvent::Type multiClickSource
QElapsedTimer multiClickTimeStamp
void checkAutoScroll(const KoPointerEvent &event)
boost::optional< KoPointerEventWrapper > lastPointerEvent
KoCanvasController * controller
KoToolProxyPrivate(KoToolProxy *p)
QPointF widgetScrollPointDoc
QPointF multiClickGlobalPoint
KoToolProxy * parent
QVector< QKeySequence > toolPriorityShortcuts
void mouseMoveEvent(QMouseEvent *event, const QPointF &point)
Forwarded to the current KoToolBase.
KisPopupWidgetInterface * popupWidget()
Forwarded to the current KoToolBase.
void mousePressEvent(QMouseEvent *event, const QPointF &point)
Forwarded to the current KoToolBase.
void copy() const
Forwarded to the current KoToolBase.
void deselect()
Forwarded to the current KoToolBase.
void cut()
Forwarded to the current KoToolBase.
bool hasSelection() const
returns true if the current tool holds a selection
void dragLeaveEvent(QDragLeaveEvent *event)
Forwarded to the current KoToolBase.
KoToolProxyPrivate *const d
void requestUndoDuringStroke()
Forwarded to the current KoToolBase.
KoPointerEvent * lastDeliveredPointerEvent() const
void processEvent(QEvent *) const
virtual QPointF widgetToDocument(const QPointF &widgetPoint) const =0
void requestRedoDuringStroke()
Forwarded to the current KoToolBase.
KoCanvasBase * canvas() const
void paint(QPainter &painter, const KoViewConverter &converter)
Forwarded to the current KoToolBase.
void explicitUserStrokeEndRequest()
Forwarded to the current KoToolBase.
void tabletEvent(QTabletEvent *event, const QPointF &point)
Forwarded to the current KoToolBase.
virtual QPointF documentToWidget(const QPointF &documentPoint) const =0
QMenu * popupActionsMenu()
Forwarded to the current KoToolBase.
void touchEvent(QTouchEvent *event, const QPointF &point)
void countMultiClick(KoPointerEvent *ev, int eventType)
void focusInEvent(QFocusEvent *event)
Forwarded to the current KoToolBase.
void deleteSelection()
Forwarded to the current KoToolBase.
bool paste()
Forwarded to the current KoToolBase.
int multiClickCount() const
bool selectAll()
Forwarded to the current KoToolBase.
void mouseDoubleClickEvent(QMouseEvent *event, const QPointF &point)
Forwarded to the current KoToolBase.
void focusOutEvent(QFocusEvent *event)
Forwarded to the current KoToolBase.
KoToolProxy(KoCanvasBase *canvas, QObject *parent=0)
QVector< QKeySequence > toolPriorityShortcuts() const
KoToolProxyPrivate * priv()
void keyReleaseEvent(QKeyEvent *event)
Forwarded to the current KoToolBase.
void toolChanged(const QString &toolId)
void requestStrokeEnd()
Forwarded to the current KoToolBase.
void requestStrokeCancellation()
Forwarded to the current KoToolBase.
void inputMethodEvent(QInputMethodEvent *event)
Forwarded to the current KoToolBase.
virtual void setActiveTool(KoToolBase *tool)
Set the new active tool.
QVariant inputMethodQuery(Qt::InputMethodQuery query) const
Forwarded to the current KoToolBase.
~KoToolProxy() override
void keyPressEvent(QKeyEvent *event)
Forwarded to the current KoToolBase.
void dropEvent(QDropEvent *event, const QPointF &point)
Forwarded to the current KoToolBase.
void dragMoveEvent(QDragMoveEvent *event, const QPointF &point)
Forwarded to the current KoToolBase.
void mouseReleaseEvent(QMouseEvent *event, const QPointF &point)
Forwarded to the current KoToolBase.
void repaintDecorations()
Forwarded to the current KoToolBase.
void selectionChanged(bool hasSelection)
#define KIS_SAFE_ASSERT_RECOVER_NOOP(cond)
Definition kis_assert.h:130