Skip to content

Commit

Permalink
Added support for enumerations to the tool bit setup and using the pr…
Browse files Browse the repository at this point in the history
…operty bag editors instead of re-implementing.
  • Loading branch information
mlampert committed Jan 27, 2021
1 parent ee27fc8 commit ac1e863
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 70 deletions.
36 changes: 26 additions & 10 deletions src/Mod/Path/PathScripts/PathToolBit.py
Expand Up @@ -192,7 +192,17 @@ def _updateBitShape(self, obj, properties=None):
if obj.BitBody is not None:
for attributes in [o for o in obj.BitBody.Group if hasattr(o, 'Proxy') and hasattr(o.Proxy, 'getCustomProperties')]:
for prop in attributes.Proxy.getCustomProperties():
setattr(attributes, prop, obj.getPropertyByName(prop))
# the property might not exist in our local object (new attribute in shape)
# for such attributes we just keep the default
if hasattr(obj, prop):
setattr(attributes, prop, obj.getPropertyByName(prop))
else:
# if the template shape has a new attribute defined we should add that
# to the local object
self._setupProperty(obj, prop, attributes)
propNames = obj.BitPropertyNames
propNames.append(prop)
obj.BitPropertyNames = propNames
self._copyBitShape(obj)

def _copyBitShape(self, obj):
Expand Down Expand Up @@ -251,6 +261,20 @@ def loadBitBody(self, obj, force=False):
def unloadBitBody(self, obj):
self._removeBitBody(obj)

def _setupProperty(self, obj, prop, orig):
# extract property parameters and values so it can be copied
val = orig.getPropertyByName(prop)
typ = orig.getTypeIdOfProperty(prop)
grp = orig.getGroupOfProperty(prop)
dsc = orig.getDocumentationOfProperty(prop)

obj.addProperty(typ, prop, grp, dsc)
if 'App::PropertyEnumeration' == typ:
setattr(obj, prop, orig.getEnumerationsOfProperty(prop))

obj.setEditorMode(prop, 1)
PathUtil.setProperty(obj, prop, val)

def _setupBitShape(self, obj, path=None):
PathLog.track(obj.Label)

Expand All @@ -275,15 +299,7 @@ def _setupBitShape(self, obj, path=None):
for attributes in [o for o in bitBody.Group if PathPropertyBag.IsPropertyBag(o)]:
PathLog.debug("Process properties from {}".format(attributes.Label))
for prop in attributes.Proxy.getCustomProperties():
# extract property parameters and values so it can be copied
src = attributes.getPropertyByName(prop)
typ = PathPropertyBag.getPropertyType(src)
grp = attributes.getGroupOfProperty(prop)
dsc = attributes.getDocumentationOfProperty(prop)

obj.addProperty(typ, prop, grp, dsc)
obj.setEditorMode(prop, 1)
PathUtil.setProperty(obj, prop, src)
self._setupProperty(obj, prop, attributes)
propNames.append(prop)
if not propNames:
PathLog.error(translate('PathToolBit', 'Did not find a PropertyBag in {} - not a ToolBit shape?').format(docName))
Expand Down
62 changes: 2 additions & 60 deletions src/Mod/Path/PathScripts/PathToolBitEdit.py
Expand Up @@ -25,6 +25,7 @@
import PathScripts.PathGui as PathGui
import PathScripts.PathLog as PathLog
import PathScripts.PathPreferences as PathPreferences
import PathScripts.PathPropertyEditor as PathPropertyEditor
import PathScripts.PathToolBit as PathToolBit
import PathScripts.PathUtil as PathUtil
import os
Expand All @@ -40,65 +41,6 @@
def translate(context, text, disambig=None):
return QtCore.QCoreApplication.translate(context, text, disambig)

class _PropertyEditorBase(object):
'''Base class of all typed property editors'''

def __init__(self, obj, prop):
self.obj = obj
self.prop = prop
def getValue(self):
return getattr(self.obj, self.prop)
def setValue(self, val):
setattr(self.obj, self.prop, val)

class _PropertyEditorInteger(_PropertyEditorBase):
def widget(self, parent):
return QtGui.QSpinBox(parent)
def setEditorData(self, widget):
widget.setValue(self.getValue())
def setModelData(self, widget):
self.setValue(widget.value())

class _PropertyEditorFloat(_PropertyEditorInteger):
def widget(self, parent):
return QtGui.QDoubleSpinBox(parent)

class _PropertyEditorBool(_PropertyEditorBase):
def widget(self, parent):
return QtGui.QComboBox(parent)
def setEditorData(self, widget):
widget.clear()
widget.addItems([str(False), str(True)])
widget.setCurrentIndex(1 if self.getValue() else 0)
def setModelData(self, widget):
self.setValue(widget.currentIndex() == 1)

class _PropertyEditorString(_PropertyEditorBase):
def widget(self, parent):
return QtGui.QLineEdit(parent)
def setEditorData(self, widget):
widget.setText(self.getValue())
def setModelData(self, widget):
self.setValue(widget.text())

class _PropertyEditorQuantity(_PropertyEditorBase):
def widget(self, parent):
qsb = FreeCADGui.UiLoader().createWidget('Gui::QuantitySpinBox', parent)
self.editor = PathGui.QuantitySpinBox(qsb, self.obj, self.prop)
return qsb
def setEditorData(self, widget):
self.editor.updateSpinBox()
def setModelData(self, widget):
self.editor.updateProperty()

_PropertyEditorFactory = {
bool : _PropertyEditorBool,
int : _PropertyEditorInteger,
float : _PropertyEditorFloat,
str : _PropertyEditorString,
FreeCAD.Units.Quantity : _PropertyEditorQuantity,
}

class _Delegate(QtGui.QStyledItemDelegate):
'''Handles the creation of an appropriate editing widget for a given property.'''
ObjectRole = QtCore.Qt.UserRole + 1
Expand All @@ -110,7 +52,7 @@ def createEditor(self, parent, option, index):
if editor is None:
obj = index.data(self.ObjectRole)
prp = index.data(self.PropertyRole)
editor = _PropertyEditorFactory[type(getattr(obj, prp))](obj, prp)
editor = PathPropertyEditor.Editor(obj, prp)
index.model().setData(index, editor, self.EditorRole)
return editor.widget(parent)

Expand Down
Binary file modified src/Mod/Path/Tools/Shape/endmill.fcstd
Binary file not shown.

0 comments on commit ac1e863

Please sign in to comment.