Skip to content

Commit

Permalink
issue #1524 - Units in Draft Gui
Browse files Browse the repository at this point in the history
 - basic working version for points,radii entry
 - logic added to Trimex, Offset, Rotate
  • Loading branch information
WandererFan committed May 5, 2014
1 parent 5c01cf0 commit 213a09c
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 19 deletions.
81 changes: 70 additions & 11 deletions src/Mod/Draft/DraftGui.py
Expand Up @@ -100,6 +100,55 @@ def translate(context,text):
return QtGui.QApplication.translate(context, text, None,
QtGui.QApplication.UnicodeUTF8)

#---------------------------------------------------------------------------
# UNITS handling
#---------------------------------------------------------------------------
def getDefaultUnit(dim):
'''return default Unit of Measure for a Dimension based on user preference
Units Schema'''
# only Length and Angle so far
from FreeCAD import Units
if dim == 'Length':
qty = FreeCAD.Units.Quantity(1.0,FreeCAD.Units.Length)
UOM = qty.getUserPreferred()[2]
elif dim == 'Angle':
qty = FreeCAD.Units.Quantity(1.0,FreeCAD.Units.Angle)
UOM = qty.getUserPreferred()[2]
else:
UOM = "xx"
return UOM

def makeFormatSpec(decimals=4,dim='Length'):
''' return a % format spec with specified decimals for a specified
dimension based on on user preference Units Schema'''
if dim == 'Length':
fmtSpec = "%." + str(decimals) + "f "+ getDefaultUnit('Length')
elif dim == 'Angle':
fmtSpec = "%." + str(decimals) + "f "+ getDefaultUnit('Angle')
else:
fmtSpec = "%." + str(decimals) + "f " + "??"
return fmtSpec

def displayExternal(internValue,decimals=4,dim='Length'):
'''return an internal value (ie mm) Length converted for display according
to Units Schema in use.'''
from FreeCAD import Units
if dim == 'Length':
qty = FreeCAD.Units.Quantity(internValue,FreeCAD.Units.Length)
parts = (qty.getUserPreferred()[0]).split()
elif dim == 'Angle':
qty = FreeCAD.Units.Quantity(internValue,FreeCAD.Units.Angle)
pref=qty.getUserPreferred()
parts = (qty.getUserPreferred()[0]).split()
val = (qty.getUserPreferred()[0]).split()[0]
um = parts[1].decode('latin-1')
parts = (val,um)
else:
parts = (internValue,'??')
fmt = "{0:."+ str(decimals) + "f} "+ parts[1]
displayExt = fmt.format(float(parts[0]))
return displayExt

#---------------------------------------------------------------------------
# Customized widgets
#---------------------------------------------------------------------------
Expand Down Expand Up @@ -177,7 +226,8 @@ def __init__(self):
self.fillmode = Draft.getParam("fillmode",False)
self.mask = None
self.DECIMALS = FreeCAD.ParamGet("User parameter:BaseApp/Preferences/Units").GetInt("Decimals",2)
self.FORMAT = "%." + str(self.DECIMALS) + "f mm"
self.FORMAT = makeFormatSpec(self.DECIMALS,'Length')
self.AFORMAT = makeFormatSpec(self.DECIMALS,'Angle')
self.uiloader = FreeCADGui.UiLoader()

if self.taskmode:
Expand Down Expand Up @@ -705,6 +755,7 @@ def offsetUi(self):
self.isCopy.show()
self.occOffset.show()
self.labelRadius.setText(translate("draft","Distance"))
self.radiusValue.setText(self.FORMAT % 0)
self.radiusValue.setFocus()
self.radiusValue.selectAll()

Expand Down Expand Up @@ -761,6 +812,7 @@ def trimUi(self,title=translate("draft","Trim")):
self.taskUi(title)
self.radiusUi()
self.labelRadius.setText(translate("draft","Distance"))
self.radiusValue.setText(self.FORMAT % 0)
self.radiusValue.setFocus()
self.radiusValue.selectAll()

Expand Down Expand Up @@ -1305,20 +1357,20 @@ def displayPoint(self, point=None, last=None, plane=None, mask=None):

# set widgets
if self.mask in ['y','z']:
self.xValue.setText(self.FORMAT % 0)
self.xValue.setText(displayExternal(dp.x,self.DECIMALS,'Length'))
else:
if dp:
self.xValue.setText(self.FORMAT % dp.x)
self.xValue.setText(displayExternal(dp.x,self.DECIMALS,'Length'))
if self.mask in ['x','z']:
self.yValue.setText(self.FORMAT % 0)
self.yValue.setText(displayExternal(dp.y,self.DECIMALS,'Length'))
else:
if dp:
self.yValue.setText(self.FORMAT % dp.y)
self.yValue.setText(displayExternal(dp.y,self.DECIMALS,'Length'))
if self.mask in ['x','y']:
self.zValue.setText(self.FORMAT % 0)
self.zValue.setText(displayExternal(dp.z,self.DECIMALS,'Length'))
else:
if dp:
self.zValue.setText(self.FORMAT % dp.z)
self.zValue.setText(displayExternal(dp.z,self.DECIMALS,'Length'))

# set masks
if (mask == "x") or (self.mask == "x"):
Expand Down Expand Up @@ -1466,11 +1518,18 @@ def setSymmetricMode(self,bool):
self.addButton.setChecked(False)
self.delButton.setChecked(False)

def setRadiusValue(self,val,unit="mm"):
t = self.FORMAT.replace("mm",unit) % val
self.radiusValue.setText(t.decode("utf8".encode("latin1")))
def setRadiusValue(self,val,unit=None):
#print "DEBUG: setRadiusValue val: ", val, " unit: ", unit
if not isinstance(val, (int, long, float)): #??some code passes strings or ???
t = val
elif unit:
t= displayExternal(val,self.DECIMALS, unit)
else:
print "Error: setRadiusValue called for number without Dimension"
t = displayExternal(val,self.DECIMALS, None)
self.radiusValue.setText(t)
self.radiusValue.setFocus()
self.radiusValue.selectAll()


def show(self):
if not self.taskmode:
Expand Down
16 changes: 8 additions & 8 deletions src/Mod/Draft/DraftTools.py
Expand Up @@ -1062,7 +1062,7 @@ def action(self,arg):
if self.altdown:
self.altdown = False
self.rad = DraftVecUtils.dist(self.point,self.center)
self.ui.setRadiusValue(self.rad)
self.ui.setRadiusValue(self.rad, "Length")
self.arctrack.setRadius(self.rad)
self.linetrack.p1(self.center)
self.linetrack.p2(self.point)
Expand All @@ -1073,15 +1073,15 @@ def action(self,arg):
angle = DraftVecUtils.angle(plane.u, self.point.sub(self.center), plane.axis)
else: angle = 0
self.linetrack.p2(DraftVecUtils.scaleTo(self.point.sub(self.center),self.rad).add(self.center))
self.ui.setRadiusValue(math.degrees(angle),unit="°")
self.ui.setRadiusValue(math.degrees(angle),unit="Angle")
self.firstangle = angle
else: # choose second angle
currentrad = DraftVecUtils.dist(self.point,self.center)
if currentrad != 0:
angle = DraftVecUtils.angle(plane.u, self.point.sub(self.center), plane.axis)
else: angle = 0
self.linetrack.p2(DraftVecUtils.scaleTo(self.point.sub(self.center),self.rad).add(self.center))
self.ui.setRadiusValue(math.degrees(angle),unit="°")
self.ui.setRadiusValue(math.degrees(angle),unit="Angle")
self.updateAngle(angle)
self.arctrack.setApertureAngle(self.angle)

Expand Down Expand Up @@ -1351,7 +1351,7 @@ def action(self,arg):
if self.altdown:
self.altdown = False
self.rad = DraftVecUtils.dist(self.point,self.center)
self.ui.setRadiusValue(self.rad)
self.ui.setRadiusValue(self.rad,'Length')
self.arctrack.setRadius(self.rad)

elif arg["Type"] == "SoMouseButtonEvent":
Expand Down Expand Up @@ -2295,7 +2295,7 @@ def action(self,arg):
if (currentrad != 0):
angle = DraftVecUtils.angle(plane.u, self.point.sub(self.center), plane.axis)
else: angle = 0
self.ui.radiusValue.setText(formatUnit(math.degrees(angle)))
self.ui.setRadiusValue(math.degrees(angle),unit="Angle")
self.firstangle = angle
self.ui.radiusValue.setFocus()
self.ui.radiusValue.selectAll()
Expand All @@ -2312,7 +2312,7 @@ def action(self,arg):
if self.ghost:
self.ghost.rotate(plane.axis,sweep)
self.ghost.on()
self.ui.radiusValue.setText(formatUnit(math.degrees(sweep)))
self.ui.setRadiusValue(math.degrees(sweep), 'Angle')
self.ui.radiusValue.setFocus()
self.ui.radiusValue.selectAll()

Expand Down Expand Up @@ -2487,7 +2487,7 @@ def action(self,arg):
self.linetrack.on()
self.linetrack.p1(self.point)
self.linetrack.p2(self.point.add(dist[0]))
self.ui.radiusValue.setText(formatUnit(dist[0].Length))
self.ui.setRadiusValue(dist[0].Length,unit="Length")
else:
self.dvec = None
self.ghost.off()
Expand Down Expand Up @@ -2712,7 +2712,7 @@ def action(self,arg):
dist = self.extrude(self.shift)
else:
dist = self.redraw(self.point,self.snapped,self.shift,self.alt)
self.ui.radiusValue.setText(formatUnit(dist))
self.ui.setRadiusValue(dist,unit="Length")
self.ui.radiusValue.setFocus()
self.ui.radiusValue.selectAll()

Expand Down

0 comments on commit 213a09c

Please sign in to comment.