/
InitGui.py
253 lines (206 loc) · 7.89 KB
/
InitGui.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# Persistent toolbars for FreeCAD
# Copyright (C) 2016, 2017 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
"""Persistent toolbars for FreeCAD."""
def persistentToolbars():
"""Persistent toolbars for FreeCAD."""
import operator
import FreeCAD as App
import FreeCADGui as Gui
from PySide import QtCore
from PySide import QtGui
import PersistentToolbarsPath
conectedToolbars = []
mw = Gui.getMainWindow()
t = PersistentToolbarsPath.timer()
def pythonToolbars():
"""Manage Python based toolbars in Arch and Draft workbench."""
active = Gui.activeWorkbench().__class__.__name__
if active == "DraftWorkbench" or active == "ArchWorkbench":
if hasattr(Gui, "draftToolBar"):
try:
Gui.draftToolBar.Activated()
except:
m = "Persistent toolbars: draftToolBar toolbar not managed.\n"
App.Console.PrintMessage(m)
if hasattr(Gui, "Snapper"):
try:
Gui.Snapper.show()
except:
m = "Persistent toolbars: Snapper toolbar not managed.\n"
App.Console.PrintMessage(m)
else:
pass
def isConnected(i):
"""Connect toolbar to onSave function."""
if i not in conectedToolbars:
conectedToolbars.append(i)
i.topLevelChanged.connect(onSave)
else:
pass
def onRestore(active):
"""Restore current workbench toolbars position."""
toolbars = {}
tb = mw.findChildren(QtGui.QToolBar)
p = App.ParamGet("User parameter:BaseApp/PersistentToolbars")
for i in tb:
isConnected(i)
if i.objectName() and not i.isFloating():
toolbars[i.objectName()] = i
else:
pass
if p.GetGroup(active).GetBool("Saved"):
group = p.GetGroup(active)
topRestore = group.GetString("Top").split(",")
leftRestore = group.GetString("Left").split(",")
rightRestore = group.GetString("Right").split(",")
bottomRestore = group.GetString("Bottom").split(",")
# Reduce flickering.
for i in toolbars:
if (i not in topRestore and
i not in leftRestore and
i not in rightRestore and
i not in bottomRestore):
area = mw.toolBarArea(toolbars[i])
if area == QtCore.Qt.ToolBarArea.LeftToolBarArea:
leftRestore.append(i)
elif area == QtCore.Qt.ToolBarArea.RightToolBarArea:
rightRestore.append(i)
elif area == QtCore.Qt.ToolBarArea.BottomToolBarArea:
bottomRestore.append(i)
else:
topRestore.append(i)
else:
pass
for i in topRestore:
if i == "Break":
mw.addToolBarBreak(QtCore.Qt.TopToolBarArea)
elif i in toolbars:
mw.addToolBar(QtCore.Qt.TopToolBarArea, toolbars[i])
else:
pass
for i in leftRestore:
if i == "Break":
mw.addToolBarBreak(QtCore.Qt.LeftToolBarArea)
elif i in toolbars:
mw.addToolBar(QtCore.Qt.LeftToolBarArea, toolbars[i])
else:
pass
for i in rightRestore:
if i == "Break":
mw.addToolBarBreak(QtCore.Qt.RightToolBarArea)
elif i in toolbars:
mw.addToolBar(QtCore.Qt.RightToolBarArea, toolbars[i])
else:
pass
for i in bottomRestore:
if i == "Break":
mw.addToolBarBreak(QtCore.Qt.BottomToolBarArea)
elif i in toolbars:
mw.addToolBar(QtCore.Qt.BottomToolBarArea, toolbars[i])
else:
pass
else:
pass
def onSave():
"""Save current workbench toolbars position."""
tb = mw.findChildren(QtGui.QToolBar)
active = Gui.activeWorkbench().__class__.__name__
p = App.ParamGet("User parameter:BaseApp/PersistentToolbars")
group = p.GetGroup(active)
top = []
left = []
right = []
bottom = []
for i in tb:
if i.objectName() and i.isVisible() and not i.isFloating():
area = mw.toolBarArea(i)
x = i.geometry().x()
y = i.geometry().y()
b = mw.toolBarBreak(i)
n = i.objectName()
if area == QtCore.Qt.ToolBarArea.TopToolBarArea:
top.append([x, y, b, n])
elif area == QtCore.Qt.ToolBarArea.LeftToolBarArea:
left.append([x, y, b, n])
elif area == QtCore.Qt.ToolBarArea.RightToolBarArea:
right.append([-x, y, b, n])
elif area == QtCore.Qt.ToolBarArea.BottomToolBarArea:
bottom.append([x, -y, b, n])
else:
pass
else:
pass
top = sorted(top, key=operator.itemgetter(1, 0))
left = sorted(left, key=operator.itemgetter(0, 1))
right = sorted(right, key=operator.itemgetter(0, 1))
bottom = sorted(bottom, key=operator.itemgetter(1, 0))
topSave = []
leftSave = []
rightSave = []
bottomSave = []
for i in top:
if i[2] is True:
topSave.append("Break")
topSave.append(i[3])
else:
topSave.append(i[3])
for i in left:
if i[2] is True:
leftSave.append("Break")
leftSave.append(i[3])
else:
leftSave.append(i[3])
for i in right:
if i[2] is True:
rightSave.append("Break")
rightSave.append(i[3])
else:
rightSave.append(i[3])
for i in bottom:
if i[2] is True:
bottomSave.append("Break")
bottomSave.append(i[3])
else:
bottomSave.append(i[3])
group.SetBool("Saved", 1)
group.SetString("Top", ",".join(topSave))
group.SetString("Left", ",".join(leftSave))
group.SetString("Right", ",".join(rightSave))
group.SetString("Bottom", ",".join(bottomSave))
def onWorkbenchActivated():
"""When workbench gets activated restore toolbar position."""
active = Gui.activeWorkbench().__class__.__name__
if active:
pythonToolbars()
onRestore(active)
else:
pass
def onStart():
"""Start persistent toolbars."""
onWorkbenchActivated()
mw.workbenchActivated.connect(onWorkbenchActivated)
t.setSingleShot(True)
t.timeout.connect(onStart)
t.start()
try:
if FreeCAD.Version()[1] <= "16":
persistentToolbars()
else:
pass
except:
pass