Krita Source Code Documentation
Loading...
Searching...
No Matches
decorators.py
Go to the documentation of this file.
1# -*- coding: utf-8 -*-
2# SPDX-FileCopyrightText: 2006 Paul Giannaros <paul@giannaros.org>
3# SPDX-FileCopyrightText: 2013 Shaheed Haque <srhaque@theiet.org>
4# SPDX-FileCopyrightText: 2013 Alex Turbov <i.zaufi@gmail.com>
5# SPDX-FileCopyrightText: 2014-2016 Boudewijn Rempt <boud@valdyas.org>
6#
7# SPDX-License-Identifier: LGPL-2.0-only OR LGPL-3.0-only
8
9'''Decorators used in plugins'''
10
11import functools
12import inspect
13import sys
14import traceback
15
16try:
17 from PyQt6 import QtCore, QtGui, QtWidgets
18except:
19 from PyQt5 import QtCore, QtGui, QtWidgets
20
21import pykrita
22
23from .api import *
24
25#
26# initialization related stuff
27#
28
29
31 def _decorator(func):
32 setattr(pykrita, event, func)
33 del func
34 return _decorator
35
36
37def _callAll(plugin, functions, *args, **kwargs):
38 if plugin in functions:
39 for f in functions[plugin]:
40 try:
41 f(*args, **kwargs)
42 except:
43 traceback.print_exc()
44 sys.stderr.write('\n')
45 # TODO Return smth to a caller, so in case of
46 # failed initialization it may report smth to the
47 # C++ level and latter can show an error to the user...
48 continue
49
50
52 # automates the most common decorator pattern: calling a bunch
53 # of functions when an event has occurred
54 func.functions = dict()
55 func.fire = functools.partial(_callAll, functions=func.functions)
56 func.clear = func.functions.clear
57 return func
58
59
60def _registerCallback(plugin, event, func):
61 if plugin not in event.functions:
62 event.functions[plugin] = set()
63
64 event.functions[plugin].add(func)
65 return func
66
67
68@_simpleEventListener
69def init(func):
70 ''' The function will be called when particular plugin has loaded
71 and the configuration has been initiated
72 '''
73 plugin = sys._getframe(1).f_globals['__name__']
74 qDebug('@init: {}/{}'.format(plugin, func.__name__))
75 return _registerCallback(plugin, init, func)
76
77
78@_simpleEventListener
79def unload(func):
80 ''' The function will be called when particular plugin is being
81 unloaded from memory. Clean up any widgets that you have added
82 to the interface (toolviews etc).
83
84 ATTENTION Be really careful trying to access any window, view
85 or document from the @unload handler: in case of application
86 quit everything is dead already!
87 '''
88 plugin = sys._getframe(1).f_globals['__name__']
89 qDebug('@unload: {}/{}'.format(plugin, func.__name__))
90
91 def _module_cleaner():
92 qDebug('@unload/cleaner: {}/{}'.format(plugin, func.__name__))
93 if plugin in init.functions:
94 qDebug('@unload/init-cleaner: {}/{}'.format(plugin, func.__name__))
95 del init.functions[plugin]
96
97 func()
98
99 return _registerCallback(plugin, unload, _module_cleaner)
pykritaEventHandler(event)
Definition decorators.py:30
_simpleEventListener(func)
Definition decorators.py:51
_registerCallback(plugin, event, func)
Definition decorators.py:60
_callAll(plugin, functions, *args, **kwargs)
Definition decorators.py:37