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"""
7This module will be a collection of functions to hook into the GUI of Scribus.
8
9Currently it only provides functions to add items to a menubar.
10Support for the toolbar, statusbar and dockarea have still to be implemented.
11I have to think about how to provide this stuff to QtQml.
12"""
13
14try:
15 from PyQt6.QtWidgets import QApplication, QMenu
16except:
17 from PyQt5.QtWidgets import QApplication, QMenu
18
19import mikro
20
21
22class 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()
__init__(self, window=None)
appendItem(self, menu, item, *extra_args)
insertMenuAfter(self, after_menu, new_menu)
insertMenuBefore(self, before_menu, new_menu)