-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCubeMenuCommon.py
186 lines (165 loc) · 5.17 KB
/
CubeMenuCommon.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
# Cube menu for FreeCAD.
# Copyright (C) 2015, 2016 (as part of TabBar) triplus @ FreeCAD
# Copyright (C) 2017, 2018, 2019 (as part of CommandPanel) triplus @ FreeCAD
# Copyright (C) 2020 triplus @ FreeCAD
#
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
"""Cube menu for FreeCAD - Common."""
import uuid
import FreeCADGui as Gui
import FreeCAD as App
from PySide import QtGui
mw = Gui.getMainWindow()
p = App.ParamGet("User parameter:BaseApp/CubeMenu")
def actionList():
"""Create a dictionary of unique actions. Exclude command names
containing . to prevent domain name system clash. Exclude
command names containing , to prevent possible join and split
related issues. Exclude actions with no text, as that can
result in ambiguity, when selecting the command."""
actions = {}
duplicates = []
for i in mw.findChildren(QtGui.QAction):
name = i.objectName()
if (name and
i.text() and
"." not in name and
"," not in name):
if name in actions:
if name not in duplicates:
duplicates.append(name)
else:
actions[name] = i
for d in duplicates:
del actions[d]
return actions
def wbIcon(i):
"""Create workbench icon."""
if str(i.find("XPM")) != "-1":
icon = []
for a in ((((i
.split('{', 1)[1])
.rsplit('}', 1)[0])
.strip())
.split("\n")):
icon.append((a
.split('"', 1)[1])
.rsplit('"', 1)[0])
icon = QtGui.QIcon(QtGui.QPixmap(icon))
else:
icon = QtGui.QIcon(QtGui.QPixmap(i))
if icon.isNull():
icon = QtGui.QIcon(":/icons/freecad")
return icon
def defaultGroup(base):
"""Create default group if no group exist."""
g = None
index = base.GetString("index")
if not index:
base.SetString("index", "1")
g = base.GetGroup("1")
g.SetString("uuid", str(uuid.uuid4()))
g.SetString("name", "Default")
cmd = ["Std_ViewFront",
"Std_ViewTop",
"Std_ViewRight",
"CPSeparator",
"CubeMenu"]
g.SetString("commands", ",".join(cmd))
return g
def splitIndex(base, string="index"):
"""Convenience function to create and return the index."""
index = base.GetString(string)
if index:
index = index.split(",")
else:
index = []
return index
def splitDomain(domain=None):
"""Split the domain name."""
if domain:
try:
d = domain.split(".")
except:
d = []
else:
d = []
# CPMenu
try:
prefix = d[0]
except IndexError:
prefix = None
# Source (User or System)
try:
source = d[1]
except IndexError:
source = None
# Workbench
try:
workbench = d[2]
except IndexError:
workbench = None
# UUID
try:
uid = d[3]
except IndexError:
uid = str(uuid.uuid4())
return [prefix, source, workbench, uid]
def findGroup(domain):
"""Find group matching the domain name."""
g = None
d = splitDomain(domain)
if all(d):
prefix, source, workbench, uid = d
base = p.GetGroup(source).GetGroup(workbench)
index = splitIndex(base)
for i in index:
if base.GetGroup(i).GetString("uuid") == uid:
g = base.GetGroup(i)
return g
def newGroup(domain):
"""Create a new group."""
g = None
d = splitDomain(domain)
if all(d):
prefix, source, workbench, uid = d
base = p.GetGroup(source).GetGroup(workbench)
index = splitIndex(base)
x = 1
while str(x) in index and x < 1000:
x += 1
index.append(str(x))
base.SetString("index", ",".join(index))
g = base.GetGroup(str(x))
g.SetString("uuid", uid)
return g
def deleteGroup(domain):
"""Delete group matching the domain name."""
d = splitDomain(domain)
if all(d):
temp = []
prefix, source, workbench, uid = d
base = p.GetGroup(source).GetGroup(workbench)
index = splitIndex(base)
for i in index:
if base.GetGroup(i).GetString("uuid") == uid:
base.RemGroup(i)
else:
temp.append(i)
base.SetString("index", ",".join(temp))
defaultGroup(base)
return True
return False