-
Notifications
You must be signed in to change notification settings - Fork 36
/
Shared.py
141 lines (100 loc) · 4.72 KB
/
Shared.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
import FreeCAD
import FreeCADGui
from PySide import QtGui
def clearActiveDocument():
"""Clears the currently active 3D view so that we can re-render"""
# Grab our code editor so we can interact with it
mw = FreeCADGui.getMainWindow()
mdi = mw.findChild(QtGui.QMdiArea)
currentWin = mdi.currentSubWindow()
if currentWin == None:
return
winName = currentWin.windowTitle().split(" ")[0].split('.')[0]
# Translate dashes so that they can be safetly used since theyare common
if '-' in winName:
winName= winName.replace('-', "__")
try:
doc = FreeCAD.getDocument(winName)
# Make sure we have an active document to work with
if doc is not None:
for obj in doc.Objects:
doc.removeObject(obj.Name)
except:
pass
def getActiveCodePane():
"""Gets the currently active code pane, even if its 3D view is selected."""
# Grab our code editor so we can interact with it
mw = FreeCADGui.getMainWindow()
mdi = mw.findChild(QtGui.QMdiArea)
# If our current subwindow doesn't contain a script, we need to find the one that does
mdiWin = mdi.currentSubWindow()
if mdiWin == None: return None # We need to warn the caller that there is no code pane
windowTitle = mdiWin.windowTitle()
if mdiWin == 0 or ".py" not in mdiWin.windowTitle():
if '__' in mdiWin.windowTitle():
windowTitle = mdiWin.windowTitle().replace("__", '-')
subList = mdi.subWindowList()
for sub in subList:
if sub.windowTitle() == windowTitle.split(" ")[0] + ".py":
mdiWin = sub
winName = mdiWin.windowTitle().split('.')[0]
cqCodePane = mw.findChild(QtGui.QPlainTextEdit, "cqCodePane_" + winName)
return cqCodePane
def closeActiveCodeWindow():
mw = FreeCADGui.getMainWindow()
mdi = mw.findChild(QtGui.QMdiArea)
# We cannot trust the current subwindow to be a script window, it may be the associated 3D view
mdiWin = mdi.currentSubWindow()
# We have a 3D view selected so we need to find the corresponding script window
if mdiWin == 0 or ".py" not in mdiWin.windowTitle():
subList = mdi.subWindowList()
for sub in subList:
if sub.windowTitle() == mdiWin.windowTitle().split(" ")[0] + ".py":
sub.close()
return
mdiWin.close()
def setActiveWindowTitle(title):
"""Sets the title of the currently active MDI window, as long as it is a scripting window"""
mw = FreeCADGui.getMainWindow()
mdi = mw.findChild(QtGui.QMdiArea)
# We cannot trust the current subwindow to be a script window, it may be the associated 3D view
mdiWin = mdi.currentSubWindow()
if mdiWin == 0 or ".py" not in mdiWin.windowTitle():
subList = mdi.subWindowList()
for sub in subList:
if sub.windowTitle() == mdiWin.windowTitle() + ".py":
mdiWin = sub
# Change the window title if there is something there to change
if (mdiWin != 0):
mdiWin.setWindowTitle(title)
cqCodePane = mdiWin.findChild(QtGui.QPlainTextEdit)
cqCodePane.setObjectName("cqCodePane_" + title.split('.')[0])
def populateParameterEditor(parameters):
"""Puts the proper controls in the script variable editor pane based on the parameters found"""
mw = FreeCADGui.getMainWindow()
# If the widget is open, we need to close it
dockWidgets = mw.findChildren(QtGui.QDockWidget)
for widget in dockWidgets:
if widget.objectName() == "cqVarsEditor":
gridLayout = QtGui.QGridLayout()
line = 1
# Add controls for all the parameters so that they can be edited from the GUI
for pKey, pVal in list(parameters.items()):
label = QtGui.QLabel(pKey)
# We want to keep track of this parameter value field so that we can pull its value later when executing
value = QtGui.QLineEdit()
value.setText(str(pVal.default_value))
value.setObjectName("pcontrol_" + pKey)
# Add the parameter control sets, one set per line
gridLayout.addWidget(label, line, 0)
gridLayout.addWidget(value, line, 1)
line += 1
# Create a widget we can put the layout in and add a scrollbar
newWidget = QtGui.QWidget()
newWidget.setLayout(gridLayout)
# Add a scroll bar in case there are a lot of variables in the script
scrollArea = QtGui.QScrollArea()
scrollArea.setBackgroundRole(QtGui.QPalette.Light)
scrollArea.setStyleSheet("QLabel { color : black; }");
scrollArea.setWidget(newWidget)
widget.setWidget(scrollArea)