Editing Works

This commit is contained in:
sliptonic
2020-11-02 10:39:30 -06:00
parent 0a2183fe4f
commit 7c4c25ceed
3 changed files with 134 additions and 152 deletions

View File

@@ -36,12 +36,14 @@ __author__ = "sliptonic (Brad Collette)"
__url__ = "https://www.freecadweb.org"
__doc__ = "Task panel editor for a ToolBit"
# Qt translation handling
def translate(context, text, disambig=None):
return QtCore.QCoreApplication.translate(context, text, disambig)
#PathLog.setLevel(PathLog.Level.DEBUG, PathLog.thisModule())
#PathLog.trackModule(PathLog.thisModule())
# PathLog.setLevel(PathLog.Level.DEBUG, PathLog.thisModule())
# PathLog.trackModule(PathLog.thisModule())
class ViewProvider(object):
'''ViewProvider for a ToolBit.
@@ -58,7 +60,7 @@ class ViewProvider(object):
def attach(self, vobj):
PathLog.track(vobj.Object)
self.vobj = vobj
self.obj = vobj.Object
self.obj = vobj.Object
def getIcon(self):
png = self.obj.Proxy.getBitThumbnail(self.obj)
@@ -113,6 +115,7 @@ class ViewProvider(object):
def doubleClicked(self, vobj):
self.setEdit(vobj)
class TaskPanel:
'''TaskPanel for the SetupSheet - if it is being edited directly.'''
@@ -123,14 +126,16 @@ class TaskPanel:
self.editor = PathToolBitEdit.ToolBitEditor(self.obj)
self.form = self.editor.form
self.deleteOnReject = deleteOnReject
FreeCAD.ActiveDocument.openTransaction(translate('PathToolBit', 'Edit ToolBit'))
FreeCAD.ActiveDocument.openTransaction(translate('PathToolBit',
'Edit ToolBit'))
def reject(self):
FreeCAD.ActiveDocument.abortTransaction()
self.editor.reject()
FreeCADGui.Control.closeDialog()
if self.deleteOnReject:
FreeCAD.ActiveDocument.openTransaction(translate('PathToolBit', 'Uncreate ToolBit'))
FreeCAD.ActiveDocument.openTransaction(translate('PathToolBit',
'Uncreate ToolBit'))
self.editor.reject()
FreeCAD.ActiveDocument.removeObject(self.obj.Name)
FreeCAD.ActiveDocument.commitTransaction()
@@ -155,132 +160,47 @@ class TaskPanel:
self.editor.setupUI()
class ToolBitSelector(object):
ToolRole = QtCore.Qt.UserRole + 1
def __init__(self):
self.buttons = None
self.editor = None
self.dialog = None
self.form = FreeCADGui.PySideUic.loadUi(':/panels/ToolBitSelector.ui')
self.setupUI()
def updateTools(self, selected=None):
PathLog.track()
selItem = None
self.form.tools.setUpdatesEnabled(False)
if selected is None and self.form.tools.currentItem():
selected = self.form.tools.currentItem().text()
self.form.tools.clear()
for tool in sorted(self.loadedTools(), key=lambda t: t.Label):
icon = None
if tool.ViewObject and tool.ViewObject.Proxy:
icon = tool.ViewObject.Proxy.getIcon()
if icon and isinstance(icon, QtGui.QIcon):
item = QtGui.QListWidgetItem(icon, tool.Label)
else:
item = QtGui.QListWidgetItem(tool.Label)
item.setData(self.ToolRole, tool)
if selected == tool.Label:
selItem = item
self.form.tools.addItem(item)
if selItem:
self.form.tools.setCurrentItem(selItem)
self.updateSelection()
self.form.tools.setUpdatesEnabled(True)
def getTool(self):
PathLog.track()
self.updateTools()
res = self.form.exec_()
if 1 == res and self.form.tools.currentItem():
return self.form.tools.currentItem().data(self.ToolRole)
return None
def loadedTools(self):
PathLog.track()
if FreeCAD.ActiveDocument:
return [o for o in FreeCAD.ActiveDocument.Objects if hasattr(o, 'Proxy') and isinstance(o.Proxy, PathToolBit.ToolBit)]
return []
def loadTool(self):
PathLog.track()
tool = LoadTool(self.form)
if tool:
self.updateTools(tool.Label)
def createTool(self):
PathLog.track()
tool = PathToolBit.Factory.Create()
def accept():
self.editor.accept()
self.dialog.done(1)
self.updateTools(tool.Label)
def reject():
FreeCAD.ActiveDocument.openTransaction(translate('PathToolBit', 'Uncreate ToolBit'))
self.editor.reject()
self.dialog.done(0)
FreeCAD.ActiveDocument.removeObject(tool.Name)
FreeCAD.ActiveDocument.commitTransaction()
self.dialog = QtGui.QDialog(self.form)
layout = QtGui.QVBoxLayout(self.dialog)
self.editor = PathToolBitEdit.ToolBitEditor(tool, self.dialog)
self.editor.setupUI()
self.buttons = QtGui.QDialogButtonBox(
QtGui.QDialogButtonBox.Ok | QtGui.QDialogButtonBox.Cancel,
QtCore.Qt.Horizontal, self.dialog)
layout.addWidget(self.buttons)
self.buttons.accepted.connect(accept)
self.buttons.rejected.connect(reject)
print(self.dialog.exec_())
def updateSelection(self):
PathLog.track()
if self.form.tools.selectedItems():
self.form.buttonBox.button(QtGui.QDialogButtonBox.Ok).setEnabled(True)
else:
self.form.buttonBox.button(QtGui.QDialogButtonBox.Ok).setEnabled(False)
def setupUI(self):
PathLog.track()
self.form.toolCreate.clicked.connect(self.createTool)
self.form.toolLoad.clicked.connect(self.loadTool)
self.form.tools.itemSelectionChanged.connect(self.updateSelection)
self.form.tools.doubleClicked.connect(self.form.accept)
class ToolBitGuiFactory(PathToolBit.ToolBitFactory):
def Create(self, name='ToolBit', shapeFile=None):
'''Create(name = 'ToolBit') ... creates a new tool bit.
It is assumed the tool will be edited immediately so the internal bit body is still attached.'''
FreeCAD.ActiveDocument.openTransaction(translate('PathToolBit', 'Create ToolBit'))
'''
Create(name = 'ToolBit') ... creates a new tool bit.
It is assumed the tool will be edited immediately so the internal
bit body is still attached.
'''
FreeCAD.ActiveDocument.openTransaction(translate('PathToolBit',
'Create ToolBit'))
tool = PathToolBit.ToolBitFactory.Create(self, name, shapeFile)
PathIconViewProvider.Attach(tool.ViewObject, name)
FreeCAD.ActiveDocument.commitTransaction()
return tool
def GetToolFile(parent = None):
def GetToolFile(parent=None):
if parent is None:
parent = QtGui.QApplication.activeWindow()
foo = QtGui.QFileDialog.getOpenFileName(parent, 'Tool', PathPreferences.lastPathToolBit(), '*.fctb')
foo = QtGui.QFileDialog.getOpenFileName(parent, 'Tool',
PathPreferences.lastPathToolBit(),
'*.fctb')
if foo and foo[0]:
PathPreferences.setLastPathToolBit(os.path.dirname(foo[0]))
return foo[0]
return None
def GetToolFiles(parent = None):
def GetToolFiles(parent=None):
if parent is None:
parent = QtGui.QApplication.activeWindow()
foo = QtGui.QFileDialog.getOpenFileNames(parent, 'Tool', PathPreferences.lastPathToolBit(), '*.fctb')
foo = QtGui.QFileDialog.getOpenFileNames(parent, 'Tool',
PathPreferences.lastPathToolBit(),
'*.fctb')
if foo and foo[0]:
PathPreferences.setLastPathToolBit(os.path.dirname(foo[0][0]))
return foo[0]
return []
def GetToolShapeFile(parent = None):
def GetToolShapeFile(parent=None):
if parent is None:
parent = QtGui.QApplication.activeWindow()
@@ -290,7 +210,8 @@ def GetToolShapeFile(parent = None):
elif not os.path.isdir(location):
location = PathPreferences.filePath()
fname = QtGui.QFileDialog.getOpenFileName(parent, 'Select Tool Shape', location, '*.fcstd')
fname = QtGui.QFileDialog.getOpenFileName(parent, 'Select Tool Shape',
location, '*.fcstd')
if fname and fname[0]:
if fname != location:
PathPreferences.setLastPathToolShape(location)
@@ -298,15 +219,22 @@ def GetToolShapeFile(parent = None):
else:
return None
def LoadTool(parent = None):
'''LoadTool(parent=None) ... Open a file dialog to load a tool from a file.'''
def LoadTool(parent=None):
'''
LoadTool(parent=None) ... Open a file dialog to load a tool from a file.
'''
foo = GetToolFile(parent)
return PathToolBit.Factory.CreateFrom(foo) if foo else foo
def LoadTools(parent = None):
'''LoadTool(parent=None) ... Open a file dialog to load a tool from a file.'''
def LoadTools(parent=None):
'''
LoadTool(parent=None) ... Open a file dialog to load a tool from a file.
'''
return [PathToolBit.Factory.CreateFrom(foo) for foo in GetToolFiles(parent)]
# Set the factory so all tools are created with UI
PathToolBit.Factory = ToolBitGuiFactory()