Krita Source Code Documentation
Loading...
Searching...
No Matches
KoInteractionTool.cpp
Go to the documentation of this file.
1/* This file is part of the KDE project
2
3 SPDX-FileCopyrightText: 2006-2007, 2010 Thomas Zander <zander@kde.org>
4
5 SPDX-License-Identifier: LGPL-2.0-or-later
6*/
7
8#include <QKeyEvent>
9
10#include "KoInteractionTool.h"
11#include "KoInteractionTool_p.h"
12#include "KoToolBase_p.h"
13#include "KoPointerEvent.h"
14#include "KoCanvasBase.h"
15
16#include "kis_global.h"
17#include "kis_assert.h"
18
19
24
28
29void KoInteractionTool::paint(QPainter &painter, const KoViewConverter &converter)
30{
32
33 if (d->currentStrategy) {
34 d->currentStrategy->paint(painter, converter);
35 } else {
36 Q_FOREACH (KoInteractionStrategyFactorySP factory, d->interactionFactories) {
37 // skip the rest of rendering if the factory asks for it
38 if (factory->paintOnHover(painter, converter)) break;
39 }
40 }
41}
42
44{
46 if (d->currentStrategy) { // possible if the user presses an extra mouse button
48 return;
49 }
50 d->currentStrategy = createStrategyBase(event);
51 if (d->currentStrategy == 0)
52 event->ignore();
53}
54
56{
58 d->lastPoint = event->point;
59
60 if (d->currentStrategy) {
61 d->currentStrategy->handleMouseMove(d->lastPoint, event->modifiers());
62 } else {
63 Q_FOREACH (KoInteractionStrategyFactorySP factory, d->interactionFactories) {
64 // skip the rest of rendering if the factory asks for it
65 if (factory->hoverEvent(event)) return;
66 }
67
68 event->ignore();
69 }
70}
71
73{
75 if (d->currentStrategy) {
76 d->currentStrategy->finishInteraction(event->modifiers());
77 KUndo2Command *command = d->currentStrategy->createCommand();
78 if (command)
79 d->canvas->addCommand(command);
80 delete d->currentStrategy;
81 d->currentStrategy = 0;
83 } else
84 event->ignore();
85}
86
88{
90 event->ignore();
91 if (d->currentStrategy &&
92 (event->key() == Qt::Key_Control ||
93 event->key() == Qt::Key_Alt || event->key() == Qt::Key_Shift ||
94 event->key() == Qt::Key_Meta)) {
95 d->currentStrategy->handleMouseMove(d->lastPoint, event->modifiers());
96 event->accept();
97 }
98}
99
101{
103
104 if (!d->currentStrategy) {
106 return;
107 }
108
109 if (event->key() == Qt::Key_Escape) {
111 event->accept();
112 } else if (event->key() == Qt::Key_Control ||
113 event->key() == Qt::Key_Alt || event->key() == Qt::Key_Shift ||
114 event->key() == Qt::Key_Meta) {
115 d->currentStrategy->handleMouseMove(d->lastPoint, event->modifiers());
116 }
117}
118
120{
122 return d->currentStrategy;
123}
124
126{
128 if (d->currentStrategy) {
129 d->currentStrategy->cancelInteraction();
130 delete d->currentStrategy;
131 d->currentStrategy = 0;
132 }
133}
134
136{
138
139 Q_FOREACH (KoInteractionStrategyFactorySP factory, d->interactionFactories) {
140 KoInteractionStrategy *strategy = factory->createStrategy(event);
141 if (strategy) {
142 return strategy;
143 }
144 }
145
146 return createStrategy(event);
147}
148
150{
152
153 Q_FOREACH (auto f, d->interactionFactories) {
155 }
156
157 d->interactionFactories.append(toQShared(factory));
158 std::sort(d->interactionFactories.begin(),
159 d->interactionFactories.end(),
161}
162
164{
167 d->interactionFactories.begin();
168
169 while (it != d->interactionFactories.end()) {
170 if ((*it)->id() == id) {
171 it = d->interactionFactories.erase(it);
172 } else {
173 ++it;
174 }
175 }
176}
177
179{
181
182 Q_FOREACH (auto f, d->interactionFactories) {
183 if (f->id() == id) {
184 return true;
185 }
186 }
187
188 return false;
189}
190
192{
194
195 Q_FOREACH (auto f, d->interactionFactories) {
196 if (f->tryUseCustomCursor()) {
197 return true;
198 }
199 }
200
201 return false;
202}
203
static bool compareLess(KoInteractionStrategyFactorySP f1, KoInteractionStrategyFactorySP f2)
void mouseReleaseEvent(KoPointerEvent *event) override
void mouseMoveEvent(KoPointerEvent *event) override
virtual KoInteractionStrategy * createStrategy(KoPointerEvent *event)=0
bool hasInteractionFactory(const QString &id)
void removeInteractionFactory(const QString &id)
void paint(QPainter &painter, const KoViewConverter &converter) override
void mousePressEvent(KoPointerEvent *event) override
void addInteractionFactory(KoInteractionStrategyFactory *factory)
KoInteractionStrategy * createStrategyBase(KoPointerEvent *event)
void keyReleaseEvent(QKeyEvent *event) override
void cancelCurrentStrategy()
Cancels the current strategy and deletes it.
void keyPressEvent(QKeyEvent *event) override
KoInteractionTool(KoCanvasBase *canvas)
KoInteractionStrategy * currentStrategy()
Qt::KeyboardModifiers modifiers() const
virtual void keyReleaseEvent(QKeyEvent *event)
virtual void repaintDecorations()
KoToolFactoryBase * factory() const
#define KIS_SAFE_ASSERT_RECOVER_RETURN(cond)
Definition kis_assert.h:128
QSharedPointer< T > toQShared(T *ptr)