Krita Source Code Documentation
Loading...
Searching...
No Matches
KoToolManager Class Reference

#include <KoToolManager.h>

+ Inheritance diagram for KoToolManager:

Public Slots

void switchBackRequested ()
 
void switchInputDeviceRequested (const KoInputDevice &id)
 
void switchToolRequested (const QString &id)
 
void themeChanged ()
 

Signals

void aboutToChangeTool (KoCanvasController *canvas)
 
void addedTool (KoToolAction *toolAction, KoCanvasController *canvas)
 
void changedStatusText (const QString &statusText)
 
void changedTool (KoCanvasController *canvas)
 
void createOpacityResource (bool isOpacityPresetMode, KoToolBase *tool)
 
void currentLayerChanged (const KoCanvasController *canvas, const KoShapeLayer *layer)
 
void inputDeviceChanged (const KoInputDevice &device)
 
void textModeChanged (bool text)
 
void toolCodesSelected (const QList< QString > &types)
 
void toolOptionWidgetsChanged (KoCanvasController *controller, const QList< QPointer< QWidget > > &widgets)
 

Public Member Functions

KoCanvasController * activeCanvasController () const
 
QString activeToolId () const
 Returns the toolId of the currently active tool.
 
void addController (KoCanvasController *controller)
 
void attachCanvas (KoCanvasController *controller)
 
Q_SLOT void attemptCanvasControllerRemoval (QObject *controller)
 
void connectActiveTool ()
 
CanvasData * createCanvasData (KoCanvasController *controller, const KoInputDevice &device)
 
KoToolBase * createTool (KoCanvasController *controller, KoToolAction *toolAction)
 
KoInputDevice currentInputDevice () const
 
void currentLayerChanged (const KoShapeLayer *layer)
 
void detachCanvas (KoCanvasController *controller)
 
void disconnectActiveTool ()
 
bool eventFilter (QObject *object, QEvent *event)
 
void initializeCurrentToolForCanvas ()
 
void initializeToolActions ()
 
 KoToolManager ()
 
void postSwitchTool ()
 
QString preferredToolForSelection (const QList< KoShape * > &shapes)
 
KoToolManager::Private * priv ()
 
 Private (KoToolManager *qq)
 
void removeCanvasController (KoCanvasController *controller)
 
void requestToolActivation (KoCanvasController *controller)
 Request tool activation for the given canvas controller.
 
void selectionChanged (const QList< KoShape * > &shapes)
 
void setAbstractResource (KoAbstractCanvasResourceInterfaceSP abstractResource, KoToolBase *tool)
 
void setConverter (KoDerivedResourceConverterSP converter, KoToolBase *tool)
 
void setCurrentCanvas (KoCanvasController *controller)
 Set the active canvas when the owning view manager changes its current view.
 
void setup ()
 
void switchBackRequested ()
 
void switchCanvasData (CanvasData *cd)
 
void switchInputDevice (const KoInputDevice &device)
 
void switchTool (const QString &id)
 
void switchToolTemporaryRequested (const QString &id)
 
QList< KoToolAction * > toolActionList () const
 
KoToolBase * toolById (KoCanvasBase *canvas, const QString &id) const
 
void updateCursor (const QCursor &cursor)
 
void updateToolForProxy ()
 
 ~KoToolManager () override
 
 ~Private ()
 

Public Attributes

CanvasData * canvasData
 
QHash< KoCanvasController *, QList< CanvasData * > > canvasses
 
KoInputDevice inputDevice
 
bool layerExplicitlyDisabled
 
QHash< KoCanvasBase *, KoToolProxy * > proxies
 
KoToolManager * q
 
QList< KoToolAction * > toolActionList
 

Private Member Functions

 KoToolManager (const KoToolManager &)
 
KoToolManager operator= (const KoToolManager &)
 
- Private Member Functions inherited from Private
 Private (KisCanvas2 *c)
 

Private Attributes

Private *const d
 
- Private Attributes inherited from Private
KisCanvas2 * canvas
 
int displayedFrame
 
int intendedFrame
 

Detailed Description

This class manages the activation and deactivation of tools for each input device.

Managing the active tool and switching tool based on various variables.

There is only one tool box and a tool manager per one main vindow and view manager. This tool manager manages all the canvases of the main window via their corresponding KoCanvasController() objects.

MyGuiWidget::MyGuiWidget(KoToolManager *toolManager)
: m_toolManager(toolManager)
{
m_canvasController = new KoCanvasController(this);
m_canvasController->setCanvas(m_canvas);
m_toolManager->addController(m_canvasController);
}
MyGuiWidget::~MyGuiWidget() {
m_toolManager->removeCanvasController(m_canvasController);
}

For a new view that extends KoView all you need to do is implement KoView::createToolBox()

KoToolManager also keeps track of the current tool based on a complex set of conditions and heuristics:

  • there is one active tool per KoCanvasController (and there is one KoCanvasController per view, because this is a class with scrollbars and a zoomlevel and so on)
  • for every pointing device (determined by the unique id of tablet, or 0 for mice – you may have more than one mouse attached, but Qt cannot distinguish between them, there is an associated tool.
  • depending on things like tablet leave/enter proximity, incoming mouse or tablet events and a little timer (that gets stopped when we know what is what), the active pointing device is determined, and the active tool is set accordingly.

Nota bene: if you use KoToolManager and register your canvases with it you no longer have to manually implement methods to route mouse, tablet, key or wheel events to the active tool. In fact, it's no longer interesting to you which tool is active; you can safely route the paint event through KoToolProxy::paint().

(The reason the input events are handled completely by the toolmanager and the paint events not is that, generally speaking, it's okay if the tools get the input events first, but you want to paint your shapes or other canvas stuff first and only then paint the tool stuff.)

Definition at line 126 of file KoToolManager.h.

Constructor & Destructor Documentation

◆ KoToolManager() [1/2]

KoToolManager::KoToolManager ( )

Definition at line 185 of file KoToolManager.cpp.

186 : d(new Private(this))
187{
188}
Private *const d

◆ ~KoToolManager()

KoToolManager::~KoToolManager ( )
override

Definition at line 190 of file KoToolManager.cpp.

191{
192 // the canvasses should have been destroyed **before** the
193 // tool manager and, hence, they should have deregister
194 // themselves explicitly
195 KIS_SAFE_ASSERT_RECOVER_NOOP(d->canvasses.isEmpty());
196 delete d;
197}
#define KIS_SAFE_ASSERT_RECOVER_NOOP(cond)
Definition kis_assert.h:130

References d, and KIS_SAFE_ASSERT_RECOVER_NOOP.

◆ KoToolManager() [2/2]

KoToolManager::KoToolManager ( const KoToolManager & )
private

◆ ~Private()

KoToolManager::~Private ( )

Member Function Documentation

◆ aboutToChangeTool

void KoToolManager::aboutToChangeTool ( KoCanvasController * canvas)
signal

Emitted when a new tool is going to override the current tool

Parameters
canvasthe currently active canvas.

◆ activeCanvasController()

KoCanvasController * KoToolManager::activeCanvasController ( ) const
Returns
the active canvas controller

Definition at line 277 of file KoToolManager.cpp.

278{
279 if (! d->canvasData) return 0;
280 return d->canvasData->canvas;
281}
KisCanvas2 * canvas

References Private::canvas, and d.

◆ activeToolId()

QString KoToolManager::activeToolId ( ) const

Returns the toolId of the currently active tool.

Definition at line 334 of file KoToolManager.cpp.

335{
336 if (!d->canvasData) return QString();
337 return d->canvasData->activeToolId;
338}

References d.

◆ addController()

void KoToolManager::addController ( KoCanvasController * controller)

Register a new canvas controller

Parameters
controllerthe view controller that this toolmanager will manage the tools for

Definition at line 221 of file KoToolManager.cpp.

222{
223 Q_ASSERT(controller);
224 if (d->canvasses.contains(controller))
225 return;
226 d->setup();
227 d->attachCanvas(controller);
228 connect(controller->proxyObject, SIGNAL(destroyed(QObject*)), this, SLOT(attemptCanvasControllerRemoval(QObject*)));
229 connect(controller->proxyObject, SIGNAL(canvasRemoved(KoCanvasController*)), this, SLOT(detachCanvas(KoCanvasController*)));
230 connect(controller->proxyObject, SIGNAL(canvasSet(KoCanvasController*)), this, SLOT(attachCanvas(KoCanvasController*)));
231}
QPointer< KoCanvasControllerProxyObject > proxyObject
void attachCanvas(KoCanvasController *controller)
void detachCanvas(KoCanvasController *controller)
Q_SLOT void attemptCanvasControllerRemoval(QObject *controller)

References attachCanvas(), attemptCanvasControllerRemoval(), d, detachCanvas(), and KoCanvasController::proxyObject.

◆ addedTool

void KoToolManager::addedTool ( KoToolAction * toolAction,
KoCanvasController * canvas )
signal

emitted whenever a new tool is dynamically added for the given canvas

◆ attachCanvas()

void KoToolManager::attachCanvas ( KoCanvasController * controller)

◆ attemptCanvasControllerRemoval()

void KoToolManager::attemptCanvasControllerRemoval ( QObject * controller)

Attempt to remove a controller. This is automatically called when a controller's proxy object is deleted, and it ensures that the controller is, in fact, removed, even if the creator forgot to do so.

Parameters
controllerthe proxy object of the controller to be removed

Definition at line 241 of file KoToolManager.cpp.

242{
243 KoCanvasControllerProxyObject* controllerActual = qobject_cast<KoCanvasControllerProxyObject*>(controller);
244 if (controllerActual) {
245 removeCanvasController(controllerActual->canvasController());
246 }
247}
KoCanvasController * canvasController() const
void removeCanvasController(KoCanvasController *controller)

References KoCanvasControllerProxyObject::canvasController(), and removeCanvasController().

◆ changedStatusText

void KoToolManager::changedStatusText ( const QString & statusText)
signal

Emitted whenever the active tool changes the status text.

Parameters
statusTextthe new status text

◆ changedTool

void KoToolManager::changedTool ( KoCanvasController * canvas)
signal

Emitted when a new tool was selected or became active.

Parameters
canvasthe currently active canvas.

◆ connectActiveTool()

void KoToolManager::connectActiveTool ( )

◆ createCanvasData()

CanvasData * KoToolManager::createCanvasData ( KoCanvasController * controller,
const KoInputDevice & device )

◆ createOpacityResource

void KoToolManager::createOpacityResource ( bool isOpacityPresetMode,
KoToolBase * tool )
signal

Emitted to create and store the opacity resource in tool. The opacity is a derived or abstract resource depending on isOpacityPresetMode.

◆ createTool()

KoToolBase * KoToolManager::createTool ( KoCanvasController * controller,
KoToolAction * toolAction )

◆ currentInputDevice()

KoInputDevice KoToolManager::currentInputDevice ( ) const
Returns
the currently active pointing device

Definition at line 211 of file KoToolManager.cpp.

212{
213 return d->inputDevice;
214}

References d.

◆ currentLayerChanged [1/2]

void KoToolManager::currentLayerChanged ( const KoCanvasController * canvas,
const KoShapeLayer * layer )
signal

Emitted after the current layer changed either its properties or to a new layer.

Parameters
canvasthe currently active canvas.
layerthe layer that is selected.

◆ currentLayerChanged() [2/2]

void KoToolManager::currentLayerChanged ( const KoShapeLayer * layer)

◆ detachCanvas()

void KoToolManager::detachCanvas ( KoCanvasController * controller)

◆ disconnectActiveTool()

void KoToolManager::disconnectActiveTool ( )

◆ eventFilter()

bool KoToolManager::eventFilter ( QObject * object,
QEvent * event )

◆ initializeCurrentToolForCanvas()

void KoToolManager::initializeCurrentToolForCanvas ( )

Definition at line 313 of file KoToolManager.cpp.

314{
315 KIS_ASSERT_RECOVER_RETURN(d->canvasData);
316
317 // make a full reconnect cycle for the currently active tool
318 d->disconnectActiveTool();
319 d->connectActiveTool();
320 d->postSwitchTool();
321}
#define KIS_ASSERT_RECOVER_RETURN(cond)
Definition kis_assert.h:75

References d, and KIS_ASSERT_RECOVER_RETURN.

◆ initializeToolActions()

void KoToolManager::initializeToolActions ( )

Initialize actions for switching to tools. The actions will have the text / shortcut as stated by the toolFactory. If the application calls this in their KoView extending class they will have all the benefits from allowing this in the menus and to allow the use to configure the shortcuts used.

Definition at line 216 of file KoToolManager.cpp.

217{
218 d->setup();
219}

References d.

◆ inputDeviceChanged

void KoToolManager::inputDeviceChanged ( const KoInputDevice & device)
signal

Every time a new input device gets used by a tool, this event is emitted.

Parameters
devicethe new input device that the user picked up.

◆ operator=()

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

◆ postSwitchTool()

void KoToolManager::postSwitchTool ( )

◆ preferredToolForSelection()

QString KoToolManager::preferredToolForSelection ( const QList< KoShape * > & shapes)

For the list of shapes find out which tool is the highest priority tool that can handle it.

Returns
the toolId for the shapes.
Parameters
shapesa list of shapes, a selection for example, that is used to look for the tool.

Definition at line 283 of file KoToolManager.cpp.

284{
285 QSet<QString> shapeTypes;
286 Q_FOREACH (KoShape *shape, shapes) {
287 shapeTypes << shape->shapeId();
288 }
289 //KritaUtils::makeContainerUnique(types);
290
291 QString toolType = KoInteractionTool_ID;
292 int prio = INT_MAX;
293 Q_FOREACH (KoToolAction *helper, d->toolActionList) {
294 if (helper->priority() >= prio)
295 continue;
296
297 bool toolWillWork = false;
298 foreach (const QString &type, shapeTypes) {
299 if (helper->toolFactory()->activationShapeId().split(',').contains(type)) {
300 toolWillWork = true;
301 break;
302 }
303 }
304
305 if (toolWillWork) {
306 toolType = helper->id();
307 prio = helper->priority();
308 }
309 }
310 return toolType;
311}
#define KoInteractionTool_ID
QString shapeId() const
Definition KoShape.cpp:878
KoToolFactoryBase * toolFactory
QString id() const
The id of the tool.
int priority() const
Lower number (higher priority) means coming first in the section.
QString activationShapeId() const

References KoToolFactoryBase::activationShapeId(), d, KoToolAction::id(), KoInteractionTool_ID, KoToolAction::priority(), KoShape::shapeId(), and KoToolAction::toolFactory.

◆ priv()

KoToolManager::Private * KoToolManager::priv ( )

Definition at line 351 of file KoToolManager.cpp.

352{
353 return d;
354}

References d.

◆ Private()

KoToolManager::Private ( KoToolManager * qq)

◆ removeCanvasController()

void KoToolManager::removeCanvasController ( KoCanvasController * controller)

Remove a set of controllers When the controller is no longer used it should be removed so all tools can be deleted and stop eating memory.

Parameters
controllerthe controller that is removed

Definition at line 233 of file KoToolManager.cpp.

234{
235 Q_ASSERT(controller);
236 disconnect(controller->proxyObject, SIGNAL(canvasRemoved(KoCanvasController*)), this, SLOT(detachCanvas(KoCanvasController*)));
237 disconnect(controller->proxyObject, SIGNAL(canvasSet(KoCanvasController*)), this, SLOT(attachCanvas(KoCanvasController*)));
238 d->detachCanvas(controller);
239}

References attachCanvas(), d, detachCanvas(), and KoCanvasController::proxyObject.

◆ requestToolActivation()

void KoToolManager::requestToolActivation ( KoCanvasController * controller)

Request tool activation for the given canvas controller.

Definition at line 204 of file KoToolManager.cpp.

205{
206 if (d->canvasses.contains(controller)) {
207 d->switchTool(d->canvasses.value(controller).first()->activeToolId);
208 }
209}

References d.

◆ selectionChanged()

void KoToolManager::selectionChanged ( const QList< KoShape * > & shapes)

◆ setAbstractResource()

void KoToolManager::setAbstractResource ( KoAbstractCanvasResourceInterfaceSP abstractResource,
KoToolBase * tool )

Definition at line 345 of file KoToolManager.cpp.

346{
347 tool->setAbstractResource(abstractResource);
348}
void setAbstractResource(KoAbstractCanvasResourceInterfaceSP abstractResource)

References KoToolBase::setAbstractResource().

◆ setConverter()

void KoToolManager::setConverter ( KoDerivedResourceConverterSP converter,
KoToolBase * tool )

Definition at line 340 of file KoToolManager.cpp.

341{
342 tool->setConverter(converter);
343}
void setConverter(KoDerivedResourceConverterSP converter)

References KoToolBase::setConverter().

◆ setCurrentCanvas()

void KoToolManager::setCurrentCanvas ( KoCanvasController * controller)

Set the active canvas when the owning view manager changes its current view.

Definition at line 356 of file KoToolManager.cpp.

357{
358 if (d->canvasData && !controller) {
359 // removing the current and the only canvas
361 d->switchCanvasData(nullptr);
362 return;
363 }
364
365 if (!controller) return;
366
367 KIS_SAFE_ASSERT_RECOVER_RETURN(d->canvasses.contains(controller));
368
369 // switch to canvasdata matching inputdevice used last with this app instance
370 Q_FOREACH (CanvasData *data, d->canvasses.value(controller)) {
371 if (data->inputDevice == d->inputDevice) {
372 if (d->canvasData != data) {
373 d->switchCanvasData(data);
374 return;
375 }
376 }
377 }
378 // if no such inputDevice for this canvas, then simply fallback to first one
379 d->switchCanvasData(d->canvasses.value(controller).first());
380}
const KoInputDevice inputDevice
void toolOptionWidgetsChanged(KoCanvasController *controller, const QList< QPointer< QWidget > > &widgets)
#define KIS_SAFE_ASSERT_RECOVER_RETURN(cond)
Definition kis_assert.h:128

References Private::canvas, d, CanvasData::inputDevice, KIS_SAFE_ASSERT_RECOVER_RETURN, and toolOptionWidgetsChanged().

◆ setup()

void KoToolManager::setup ( )

◆ switchBackRequested [1/2]

void KoToolManager::switchBackRequested ( )
slot

Switches to the last tool used just before the current one, if any.

Definition at line 260 of file KoToolManager.cpp.

261{
262 if (!d->canvasData) return;
263 if (d->canvasData->mostRecentTools.isEmpty()) return;
264 d->switchTool(d->canvasData->mostRecentTools.first()->toolId());
265}

References d.

◆ switchBackRequested() [2/2]

void KoToolManager::switchBackRequested ( )

◆ switchCanvasData()

void KoToolManager::switchCanvasData ( CanvasData * cd)

◆ switchInputDevice()

void KoToolManager::switchInputDevice ( const KoInputDevice & device)

Request a switch from to the param input device. This will cause the tool for that device to be selected.

◆ switchInputDeviceRequested

void KoToolManager::switchInputDeviceRequested ( const KoInputDevice & id)
slot

Request change input device

Parameters
idthe id of the input device

Definition at line 254 of file KoToolManager.cpp.

255{
256 if (!d->canvasData) return;
257 d->switchInputDevice(id);
258}

References d.

◆ switchTool()

void KoToolManager::switchTool ( const QString & id)

◆ switchToolRequested

void KoToolManager::switchToolRequested ( const QString & id)
slot

Request switching tool

Parameters
idthe id of the tool

Definition at line 249 of file KoToolManager.cpp.

250{
251 d->switchTool(id);
252}

References d.

◆ switchToolTemporaryRequested()

void KoToolManager::switchToolTemporaryRequested ( const QString & id)

◆ textModeChanged

void KoToolManager::textModeChanged ( bool text)
signal

Emitted when the tool's text mode has changed.

Parameters
inTextModewhether it is now in text mode.

◆ themeChanged

void KoToolManager::themeChanged ( )
slot

Notify theme changes

Definition at line 323 of file KoToolManager.cpp.

324{
325 Q_FOREACH (const QList<CanvasData*> &canvasDataList, d->canvasses) {
326 Q_FOREACH (CanvasData *canvasData, canvasDataList) {
327 Q_FOREACH (KoToolBase *tool, canvasData->allTools) {
329 }
330 }
331 }
332}
QHash< QString, KoToolBase * > allTools
void updateOptionsWidgetIcons()
CanvasData * canvasData

References CanvasData::allTools, canvasData, d, and KoToolBase::updateOptionsWidgetIcons().

◆ toolActionList()

QList< KoToolAction * > KoToolManager::toolActionList ( ) const

Returns the list of toolActions for the current tools.

Returns
lists of toolActions for the current tools.

◆ toolById()

KoToolBase * KoToolManager::toolById ( KoCanvasBase * canvas,
const QString & id ) const

Returns the tool for the given tool id. The tool may be 0

Parameters
canvasthe canvas that is a child of a previously registered controller who's tool you want.
idthe tool identifier
See also
addController()

Definition at line 267 of file KoToolManager.cpp.

268{
269 Q_ASSERT(canvas);
270 Q_FOREACH (KoCanvasController *controller, d->canvasses.keys()) {
271 if (controller->canvas() == canvas)
272 return d->canvasData->allTools.value(id);
273 }
274 return 0;
275}
virtual KoCanvasBase * canvas() const

References KoCanvasController::canvas(), Private::canvas, and d.

◆ toolCodesSelected

void KoToolManager::toolCodesSelected ( const QList< QString > & types)
signal

Emitted after the selection changed to state which unique shape-types are now in the selection.

Parameters
typesa list of string that are the shape types of the selected objects.

◆ toolOptionWidgetsChanged

void KoToolManager::toolOptionWidgetsChanged ( KoCanvasController * controller,
const QList< QPointer< QWidget > > & widgets )
signal

Emit the new tool option widgets to be used with this canvas.

◆ updateCursor()

void KoToolManager::updateCursor ( const QCursor & cursor)

◆ updateToolForProxy()

void KoToolManager::updateToolForProxy ( )

Member Data Documentation

◆ canvasData

CanvasData* KoToolManager::canvasData

Definition at line 71 of file KoToolManager_p.h.

◆ canvasses

QHash<KoCanvasController*, QList<CanvasData*> > KoToolManager::canvasses

Definition at line 68 of file KoToolManager_p.h.

◆ d

Private* const KoToolManager::d
private

Definition at line 306 of file KoToolManager.h.

◆ inputDevice

KoInputDevice KoToolManager::inputDevice

Definition at line 73 of file KoToolManager_p.h.

◆ layerExplicitlyDisabled

bool KoToolManager::layerExplicitlyDisabled

Definition at line 75 of file KoToolManager_p.h.

◆ proxies

QHash<KoCanvasBase*, KoToolProxy*> KoToolManager::proxies

Definition at line 69 of file KoToolManager_p.h.

◆ q

KoToolManager* KoToolManager::q

Definition at line 64 of file KoToolManager_p.h.

◆ toolActionList

QList< KoToolAction * > KoToolManager::toolActionList

Definition at line 66 of file KoToolManager_p.h.


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