forked from SuzanneSoy/SearchBar
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathResultsToolbar.py
195 lines (180 loc) · 6.63 KB
/
ResultsToolbar.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import FreeCAD as App
from PySide import QtGui
import FreeCADGui
import Serialize_SearchBar
# Define the translation
translate = App.Qt.translate
def toolbarAction(nfo):
act = nfo["action"]
print(
"show toolbar "
+ act["toolbar"]
+ " from workbenches "
+ repr(act["workbenches"])
)
def subToolAction(nfo):
act = nfo["action"]
toolPath = act["toolbar"] + "." + act["tool"]
if "subTool" in act:
toolPath = toolPath + "." + act["subTool"]
def runTool():
mw = FreeCADGui.getMainWindow()
for the_toolbar in mw.findChildren(QtGui.QToolBar, act["toolbar"]):
for tbt in the_toolbar.findChildren(QtGui.QToolButton):
if tbt.text() == act["tool"]:
action = None
if "subTool" in act:
men = tbt.menu()
if men:
for mac in men.actions():
if mac.text() == act["subTool"]:
action = mac
break
else:
action = tbt.defaultAction()
if "showMenu" in act and act["showMenu"]:
print(
"Popup submenu of tool "
+ toolPath
+ " available in workbenches "
+ repr(act["workbenches"])
)
the_toolbar.show()
tbt.showMenu()
return True
elif action is not None:
print(
"Run action of tool "
+ toolPath
+ " available in workbenches "
+ repr(act["workbenches"])
)
action.trigger()
return True
return False
if runTool():
return
else:
for workbench in act["workbenches"]:
print("Activating workbench " + workbench + " to access tool " + toolPath)
try:
FreeCADGui.activateWorkbench(workbench)
except Exception:
print("SearchBar: Workbench not present! Was it disabled?")
return
if runTool():
return
print(
"Tool "
+ toolPath
+ " not found, was it offered by an extension that is no longer present?"
)
def toolbarToolTip(nfo, setParent):
workbenches = FreeCADGui.listWorkbenches()
in_workbenches = [
"<li>"
+ (
Serialize_SearchBar.iconToHTML(QtGui.QIcon(workbenches[wb].Icon))
if wb in workbenches
else "? "
)
+ wb
+ "</li>"
for wb in nfo["action"]["workbenches"]
]
return (
"<p>Show the "
+ nfo["text"]
+ " toolbar</p><p>This toolbar appears in the following workbenches: <ul>"
+ "".join(in_workbenches)
+ "</ul></p>"
)
def subToolToolTip(nfo, setParent):
return (
Serialize_SearchBar.iconToHTML(nfo["icon"], 32)
+ "<p>"
+ nfo["toolTip"]
+ "</p>"
)
def getAllToolbars():
all_tbs = dict()
for wbname, workbench in FreeCADGui.listWorkbenches().items():
try:
tbs = workbench.listToolbars()
except:
continue
# careful, tbs contains all the toolbars of the workbench, including shared toolbars
for tb in tbs:
if tb not in all_tbs:
all_tbs[tb] = set()
all_tbs[tb].add(wbname)
return all_tbs
def toolbarResultsProvider():
itemGroups = []
all_tbs = getAllToolbars()
mw = FreeCADGui.getMainWindow()
for toolbarName, toolbarIsInWorkbenches in all_tbs.items():
toolbarIsInWorkbenches = sorted(list(toolbarIsInWorkbenches))
for the_toolbar in mw.findChildren(QtGui.QToolBar, toolbarName):
group = []
for tbt in the_toolbar.findChildren(QtGui.QToolButton):
text = tbt.text()
act = tbt.defaultAction()
if text != "":
# TODO: there also is the tooltip
icon = tbt.icon()
men = tbt.menu()
subgroup = []
if men:
subgroup = []
for mac in men.actions():
if mac.text():
action = {
"handler": "subTool",
"workbenches": toolbarIsInWorkbenches,
"toolbar": toolbarName,
"tool": text,
"subTool": mac.text(),
}
subgroup.append(
{
"icon": mac.icon(),
"text": mac.text(),
"toolTip": mac.toolTip(),
"action": action,
"subitems": [],
}
)
# The default action of a menu changes dynamically, instead of triggering the last action, just show the menu.
action = {
"handler": "tool",
"workbenches": toolbarIsInWorkbenches,
"toolbar": toolbarName,
"tool": text,
"showMenu": bool(men),
}
group.append(
{
"icon": icon,
"text": text,
"toolTip": tbt.toolTip(),
"action": action,
"subitems": subgroup,
}
)
# TODO: move the 'workbenches' field to the itemgroup
action = {
"handler": "toolbar",
"workbenches": toolbarIsInWorkbenches,
"toolbar": toolbarName,
}
itemGroups.append(
{
"icon": QtGui.QIcon(":/icons/Group.svg"),
"text": toolbarName,
"toolTip": "",
"action": action,
"subitems": group,
}
)
return itemGroups