Krita Source Code Documentation
Loading...
Searching...
No Matches
KoInteractionTool Class Referenceabstract

#include <KoInteractionTool.h>

+ Inheritance diagram for KoInteractionTool:

Public Member Functions

void keyPressEvent (QKeyEvent *event) override
 
void keyReleaseEvent (QKeyEvent *event) override
 
 KoInteractionTool (KoCanvasBase *canvas)
 
void mouseMoveEvent (KoPointerEvent *event) override
 
void mousePressEvent (KoPointerEvent *event) override
 
void mouseReleaseEvent (KoPointerEvent *event) override
 
void paint (QPainter &painter, const KoViewConverter &converter) override
 
 ~KoInteractionTool () override
 
- Public Member Functions inherited from KoToolBase
QAction * action (const QString &name) const
 
KoCanvasBasecanvas () const
 Returns the canvas the tool is working on.
 
virtual void copy () const
 
QCursor cursor () const
 return the last emitted cursor
 
virtual void customMoveEvent (KoPointerEvent *event)
 
virtual void customPressEvent (KoPointerEvent *event)
 
virtual void customReleaseEvent (KoPointerEvent *event)
 
virtual void cut ()
 
virtual QRectF decorationsRect () const
 
int decorationThickness () const
 decorationThickness The minimum thickness for tool decoration lines, this is derived from the screen magnification, thus the HiDPI settings. Note: to use this effectively, also set the pen to isCosmetic(true);
 
virtual void deleteSelection ()
 
virtual void deselect ()
 deselect the tool should clear the selection if it has one.
 
virtual void dragLeaveEvent (QDragLeaveEvent *event)
 
virtual void dragMoveEvent (QDragMoveEvent *event, const QPointF &point)
 
virtual void dropEvent (QDropEvent *event, const QPointF &point)
 
virtual void explicitUserStrokeEndRequest ()
 explicitUserStrokeEndRequest is called by the input manager when the user presses Enter key or any equivalent. This callback comes before requestStrokeEnd(), which comes from a different source.
 
KoToolFactoryBasefactory () const
 
virtual void focusInEvent (QFocusEvent *event)
 
virtual void focusOutEvent (QFocusEvent *event)
 
virtual bool hasSelection ()
 
virtual void inputMethodEvent (QInputMethodEvent *event)
 
virtual QVariant inputMethodQuery (Qt::InputMethodQuery query) const
 
bool isInTextMode () const
 
bool isOpacityPresetMode () const
 
 KoToolBase (KoCanvasBase *canvas)
 
bool maskSyntheticEvents () const
 
virtual void mouseDoubleClickEvent (KoPointerEvent *event)
 
virtual void mouseTripleClickEvent (KoPointerEvent *event)
 
QList< QPointer< QWidget > > optionWidgets ()
 
virtual bool paste ()
 
virtual QMenu * popupActionsMenu ()
 
virtual KisPopupWidgetInterfacepopupWidget ()
 
virtual bool selectAll ()
 selectAll select all data the tool can select.
 
virtual KoToolSelectionselection ()
 
Q_INVOKABLE QString toolId () const
 
virtual bool wantsAutoScroll () const
 
 ~KoToolBase () override
 

Protected Member Functions

void addInteractionFactory (KoInteractionStrategyFactory *factory)
 
void cancelCurrentStrategy ()
 Cancels the current strategy and deletes it.
 
virtual KoInteractionStrategycreateStrategy (KoPointerEvent *event)=0
 
KoInteractionStrategycreateStrategyBase (KoPointerEvent *event)
 
KoInteractionStrategycurrentStrategy ()
 
bool hasInteractionFactory (const QString &id)
 
 KoInteractionTool (KoInteractionToolPrivate &dd)
 
void removeInteractionFactory (const QString &id)
 
bool tryUseCustomCursor ()
 
- Protected Member Functions inherited from KoToolBase
virtual QWidget * createOptionWidget ()
 
virtual QList< QPointer< QWidget > > createOptionWidgets ()
 
int grabSensitivity () const
 Convenience function to get the current grab sensitivity.
 
qreal handleDocRadius () const
 
QRectF handleGrabRect (const QPointF &position) const
 
QRectF handlePaintRect (const QPointF &position) const
 
int handleRadius () const
 Convenience function to get the current handle radius.
 
bool isActivated () const
 
 KoToolBase (KoToolBasePrivate &dd)
 
KoPointerEventlastDeliveredPointerEvent () const
 
void setAbstractResource (KoAbstractCanvasResourceInterfaceSP abstractResource)
 
void setConverter (KoDerivedResourceConverterSP converter)
 
void setIsOpacityPresetMode (bool value)
 
void setMaskSyntheticEvents (bool value)
 
void setTextMode (bool value)
 
QHash< int, KoAbstractCanvasResourceInterfaceSPtoolAbstractResources ()
 
QHash< int, KoDerivedResourceConverterSPtoolConverters ()
 
void useCursor (const QCursor &cursor)
 

Private Member Functions

 KoInteractionTool (const KoInteractionTool &)
 
KoInteractionTooloperator= (const KoInteractionTool &)
 

Additional Inherited Members

- Public Slots inherited from KoToolBase
virtual void activate (const QSet< KoShape * > &shapes)
 
virtual void canvasResourceChanged (int key, const QVariant &res)
 
virtual void deactivate ()
 
virtual void documentResourceChanged (int key, const QVariant &res)
 
virtual void repaintDecorations ()
 
virtual void requestRedoDuringStroke ()
 
virtual void requestStrokeCancellation ()
 
virtual void requestStrokeEnd ()
 
virtual void requestUndoDuringStroke ()
 
void setStatusText (const QString &statusText)
 
void updateOptionsWidgetIcons ()
 
- Signals inherited from KoToolBase
void activateTool (const QString &id)
 
void cursorChanged (const QCursor &cursor)
 
void selectionChanged (bool hasSelection)
 
void statusTextChanged (const QString &statusText)
 
void textModeChanged (bool inTextMode)
 
- Protected Attributes inherited from KoToolBase
KoToolBasePrivated_ptr
 

Detailed Description

The interaction tool adds to the normal KoToolBase class the concept of strategies as a means to get one tool to have different actions the user can perform using the mouse. Each time the user presses the mouse until she releases the mouse a strategy object will be created, used and discarded. If the usage of a tool fits this pattern you need to inherit from this class instead of the plain KoToolBase and reimplement your createStrategy() method which returns a tool-specific strategy where all the real interaction code is placed. A tool can then become as simple as this;

class MyTool : public KoInteractionTool
{
public:
MyTool::MyTool(KoCanvasBase *canvas) : KoInteractionTool( canvas ) { }
KoInteractionStrategy *MyTool::createStrategy(KoPointerEvent *event) {
return new MyStrategy(this, m_canvas, event->point);
}
};
QPointF point
The point in document coordinates.

Whereas your strategy (MyStrategy in the example) will contain the interaction code.

Definition at line 43 of file KoInteractionTool.h.

Constructor & Destructor Documentation

◆ KoInteractionTool() [1/3]

KoInteractionTool::KoInteractionTool ( KoCanvasBase * canvas)
explicit

Constructor for basic interaction tool where user actions are translated and handled by interaction strategies of type KoInteractionStrategy.

Parameters
canvasthe canvas this tool will be working for.

Definition at line 20 of file KoInteractionTool.cpp.

22{
23}
KoCanvasBase * canvas() const
Returns the canvas the tool is working on.

◆ ~KoInteractionTool()

KoInteractionTool::~KoInteractionTool ( )
override

Definition at line 25 of file KoInteractionTool.cpp.

26{
27}

◆ KoInteractionTool() [2/3]

KoInteractionTool::KoInteractionTool ( KoInteractionToolPrivate & dd)
protected

Definition at line 204 of file KoInteractionTool.cpp.

205 : KoToolBase(dd)
206{
207}

◆ KoInteractionTool() [3/3]

KoInteractionTool::KoInteractionTool ( const KoInteractionTool & )
private

Member Function Documentation

◆ addInteractionFactory()

void KoInteractionTool::addInteractionFactory ( KoInteractionStrategyFactory * factory)
protected

Definition at line 149 of file KoInteractionTool.cpp.

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}
static bool compareLess(KoInteractionStrategyFactorySP f1, KoInteractionStrategyFactorySP f2)
KoToolFactoryBase * factory() const
#define KIS_SAFE_ASSERT_RECOVER_RETURN(cond)
Definition kis_assert.h:128
QSharedPointer< T > toQShared(T *ptr)

References KoInteractionStrategyFactory::compareLess(), KoToolBase::factory(), KoToolFactoryBase::id, KIS_SAFE_ASSERT_RECOVER_RETURN, and toQShared().

◆ cancelCurrentStrategy()

void KoInteractionTool::cancelCurrentStrategy ( )
protected

Cancels the current strategy and deletes it.

Definition at line 125 of file KoInteractionTool.cpp.

126{
128 if (d->currentStrategy) {
129 d->currentStrategy->cancelInteraction();
130 delete d->currentStrategy;
131 d->currentStrategy = 0;
132 }
133}

◆ createStrategy()

virtual KoInteractionStrategy * KoInteractionTool::createStrategy ( KoPointerEvent * event)
protectedpure virtual

Implemented in KoZoomTool, and DefaultTool.

◆ createStrategyBase()

KoInteractionStrategy * KoInteractionTool::createStrategyBase ( KoPointerEvent * event)
protected

Reimplement this factory method to create your strategy to be used for mouse interaction.

Returns
a new strategy, or 0 when there is nothing to do.

Definition at line 135 of file KoInteractionTool.cpp.

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}
virtual KoInteractionStrategy * createStrategy(KoPointerEvent *event)=0

References createStrategy(), and KoToolBase::factory().

◆ currentStrategy()

KoInteractionStrategy * KoInteractionTool::currentStrategy ( )
protected

the strategy that is 'in progress'

Definition at line 119 of file KoInteractionTool.cpp.

120{
122 return d->currentStrategy;
123}

◆ hasInteractionFactory()

bool KoInteractionTool::hasInteractionFactory ( const QString & id)
protected

Definition at line 178 of file KoInteractionTool.cpp.

179{
181
182 Q_FOREACH (auto f, d->interactionFactories) {
183 if (f->id() == id) {
184 return true;
185 }
186 }
187
188 return false;
189}

◆ keyPressEvent()

void KoInteractionTool::keyPressEvent ( QKeyEvent * event)
overridevirtual

Called when a key is pressed. Implementors should call event->ignore() if they do not actually use the event. Default implementation ignores this event.

Parameters
eventstate and reason of this key press

Reimplemented from KoToolBase.

Reimplemented in KoZoomTool.

Definition at line 87 of file KoInteractionTool.cpp.

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}

◆ keyReleaseEvent()

void KoInteractionTool::keyReleaseEvent ( QKeyEvent * event)
overridevirtual

Called when a key is released Implementors should call event->ignore() if they do not actually use the event. Default implementation ignores this event.

Parameters
eventstate and reason of this key release

Reimplemented from KoToolBase.

Reimplemented in KoZoomTool.

Definition at line 100 of file KoInteractionTool.cpp.

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}
void cancelCurrentStrategy()
Cancels the current strategy and deletes it.
virtual void keyReleaseEvent(QKeyEvent *event)

References cancelCurrentStrategy(), and KoToolBase::keyReleaseEvent().

◆ mouseMoveEvent()

void KoInteractionTool::mouseMoveEvent ( KoPointerEvent * event)
overridevirtual

Called when the mouse or stylus moved over the canvas. Implementors should call event->ignore() if they do not actually use the event.

Parameters
eventstate and reason of this mouse or stylus move

Implements KoToolBase.

Reimplemented in KoZoomTool.

Definition at line 55 of file KoInteractionTool.cpp.

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}
Qt::KeyboardModifiers modifiers() const

References KoToolBase::factory(), and KoPointerEvent::modifiers().

◆ mousePressEvent()

void KoInteractionTool::mousePressEvent ( KoPointerEvent * event)
overridevirtual

Called when (one of) the mouse or stylus buttons is pressed. Implementors should call event->ignore() if they do not actually use the event.

Parameters
eventstate and reason of this mouse or stylus press

Implements KoToolBase.

Definition at line 43 of file KoInteractionTool.cpp.

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}
KoInteractionStrategy * createStrategyBase(KoPointerEvent *event)

References cancelCurrentStrategy(), and createStrategyBase().

◆ mouseReleaseEvent()

void KoInteractionTool::mouseReleaseEvent ( KoPointerEvent * event)
overridevirtual

Called when (one of) the mouse or stylus buttons is released. Implementors should call event->ignore() if they do not actually use the event.

Parameters
eventstate and reason of this mouse or stylus release

Implements KoToolBase.

Reimplemented in KoZoomTool.

Definition at line 72 of file KoInteractionTool.cpp.

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}
virtual void repaintDecorations()

References KoPointerEvent::modifiers(), and KoToolBase::repaintDecorations().

◆ operator=()

KoInteractionTool & KoInteractionTool::operator= ( const KoInteractionTool & )
private

◆ paint()

void KoInteractionTool::paint ( QPainter & painter,
const KoViewConverter & converter )
overridevirtual

Called by the canvas to paint any decorations that the tool deems needed. The painter has the top left of the canvas as its origin.

Parameters
painterused for painting the shape
converterto convert between internal and view coordinates.

Implements KoToolBase.

Definition at line 29 of file KoInteractionTool.cpp.

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}

References KoToolBase::factory().

◆ removeInteractionFactory()

void KoInteractionTool::removeInteractionFactory ( const QString & id)
protected

Definition at line 163 of file KoInteractionTool.cpp.

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}

◆ tryUseCustomCursor()

bool KoInteractionTool::tryUseCustomCursor ( )
protected

Definition at line 191 of file KoInteractionTool.cpp.

192{
194
195 Q_FOREACH (auto f, d->interactionFactories) {
196 if (f->tryUseCustomCursor()) {
197 return true;
198 }
199 }
200
201 return false;
202}

The documentation for this class was generated from the following files: