Krita Source Code Documentation
Loading...
Searching...
No Matches
scripter_hooks.py
Go to the documentation of this file.
1
# -*- coding: utf-8 -*-
2
#
3
# SPDX-License-Identifier: GPL-3.0-or-later
4
#
5
6
"""
7
This module will be a collection of functions to hook into the GUI of Scribus.
8
9
Currently it only provides functions to add items to a menubar.
10
Support for the toolbar, statusbar and dockarea have still to be implemented.
11
I have to think about how to provide this stuff to QtQml.
12
"""
13
14
try
:
15
from
PyQt6.QtWidgets
import
QApplication, QMenu
16
except
:
17
from
PyQt5.QtWidgets
import
QApplication, QMenu
18
19
import
mikro
20
21
22
class
MenuHooks
(object):
23
24
"""
25
This class lets extension-scripts hook into the main menu of Scribus.
26
"""
27
28
def
__init__
(self, window=None):
29
self.
window
= window
or
Scripter.dialogs.mainWindow.qt
30
self.
menubar
= self.
window
.menuBar()
31
self.
menus
= []
32
33
def
createMenu
(self, title):
34
m = QMenu(title)
35
self.
menus
.append(m)
36
self.
menubar
.addMenu(m)
37
return
m
38
39
def
iter_menus
(self):
40
for
action
in
self.
menubar
.actions():
41
menu = action.menu()
42
if
menu:
43
yield
menu
44
45
def
iter_inner_menus
(self, menu):
46
for
action
in
menu.actions():
47
menu = action.menu()
48
if
menu:
49
yield
menu
50
51
def
findMenu
(self, title):
52
"""
53
find a menu with a given title
54
55
@type title: string
56
@param title: English title of the menu
57
@rtype: QMenu
58
@return: None if no menu was found, else the menu with title
59
"""
60
# See also http://pyqt.sourceforge.net/Docs/PyQt5/i18n.html#differences-between-pyqt5-and-qt
61
title = QApplication.translate(mikro.classname(self.
window
), title)
62
for
menu
in
self.
iter_menus
():
63
if
menu.title() == title:
64
return
menu
65
for
innerMenu
in
self.
iter_inner_menus
(menu):
66
if
innerMenu.title() == title:
67
return
innerMenu
68
69
def
actionForMenu
(self, menu):
70
for
action
in
self.
menubar
.actions():
71
if
action.menu() == menu:
72
return
action
73
74
def
insertMenuBefore
(self, before_menu, new_menu):
75
"""
76
Insert a menu after another menu in the menubar
77
78
@type: before_menu QMenu instance or title string of menu
79
@param before_menu: menu which should be after the newly inserted menu
80
@rtype: QAction instance
81
@return: action for inserted menu
82
"""
83
if
isinstance(before_menu, basestring):
84
before_menu = self.
findMenu
(before_menu)
85
before_action = self.
actionForMenu
(before_menu)
86
# I have no clue why QMenuBar::insertMenu only allows
87
# to insert before another menu and not after a menu...
88
new_action = self.
menubar
.insertMenu(before_action, new_menu)
89
return
new_action
90
91
def
menuAfter
(self, menu):
92
# This method is needed for insertMenuAfter because
93
# QMenuBar.insertMenu can only insert before another menu
94
previous =
None
95
for
m
in
self.
iter_menus
():
96
if
previous
and
previous == menu:
97
return
m
98
previous = m
99
100
def
appendMenu
(self, menu):
101
"""
102
Probably not that useful
103
because it will add a menu after the help menu
104
"""
105
action = self.
menubar
.addMenu(menu)
106
return
action
107
108
def
insertMenuAfter
(self, after_menu, new_menu):
109
"""
110
Insert a menu before another menu in the menubar
111
"""
112
if
isinstance(after_menu, basestring):
113
after_menu = self.
findMenu
(after_menu)
114
after_after_menu = self.
menuAfter
(after_menu)
115
if
after_after_menu:
116
return
self.
insertMenuBefore
(after_after_menu, new_menu)
117
else
:
118
return
self.
appendMenu
(new_menu)
119
120
def
appendItem
(self, menu, item, *extra_args):
121
if
isinstance(menu, basestring):
122
title = menu
123
menu = self.
findMenu
(title)
124
if
not
menu:
125
raise
ValueError(
"Menu %r not found"
% title)
126
if
isinstance(item, QMenu):
127
action = menu.addMenu(item)
128
else
:
129
action = menu.addAction(item, *extra_args)
130
return
action
131
132
def
appendSeparator
(self, menu):
133
if
isinstance(menu, basestring):
134
menu = self.
findMenu
(menu)
135
menu.addSeparator()
scripter_hooks.MenuHooks
Definition
scripter_hooks.py:22
scripter_hooks.MenuHooks.appendSeparator
appendSeparator(self, menu)
Definition
scripter_hooks.py:132
scripter_hooks.MenuHooks.createMenu
createMenu(self, title)
Definition
scripter_hooks.py:33
scripter_hooks.MenuHooks.__init__
__init__(self, window=None)
Definition
scripter_hooks.py:28
scripter_hooks.MenuHooks.appendItem
appendItem(self, menu, item, *extra_args)
Definition
scripter_hooks.py:120
scripter_hooks.MenuHooks.iter_menus
iter_menus(self)
Definition
scripter_hooks.py:39
scripter_hooks.MenuHooks.insertMenuAfter
insertMenuAfter(self, after_menu, new_menu)
Definition
scripter_hooks.py:108
scripter_hooks.MenuHooks.menus
menus
Definition
scripter_hooks.py:31
scripter_hooks.MenuHooks.findMenu
findMenu(self, title)
Definition
scripter_hooks.py:51
scripter_hooks.MenuHooks.actionForMenu
actionForMenu(self, menu)
Definition
scripter_hooks.py:69
scripter_hooks.MenuHooks.menubar
menubar
Definition
scripter_hooks.py:30
scripter_hooks.MenuHooks.iter_inner_menus
iter_inner_menus(self, menu)
Definition
scripter_hooks.py:45
scripter_hooks.MenuHooks.menuAfter
menuAfter(self, menu)
Definition
scripter_hooks.py:91
scripter_hooks.MenuHooks.insertMenuBefore
insertMenuBefore(self, before_menu, new_menu)
Definition
scripter_hooks.py:74
scripter_hooks.MenuHooks.window
window
Definition
scripter_hooks.py:29
scripter_hooks.MenuHooks.appendMenu
appendMenu(self, menu)
Definition
scripter_hooks.py:100
QtWidgets
QtWidgets
plugins
extensions
pykrita
plugin
krita
attic
scripter_hooks.py
Generated at
2025-11-04 02:30:02+01:00
from
Krita
branch
master
, commit
c9dde2e79561a8aea4a7e8d9ac99c98a7bac9e52