Skip to content

Commit

Permalink
Draft: Switching to Units system
Browse files Browse the repository at this point in the history
  • Loading branch information
yorikvanhavre committed Apr 16, 2014
1 parent 9766385 commit f5627b3
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 41 deletions.
92 changes: 60 additions & 32 deletions src/Mod/Draft/DraftGui.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# -*- coding: utf8 -*-

#***************************************************************************
#* *
Expand Down Expand Up @@ -175,6 +176,9 @@ def __init__(self):
self.isTaskOn = False
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.uiloader = FreeCADGui.UiLoader()

# set default to taskbar mode
if self.taskmode == None:
Expand Down Expand Up @@ -240,8 +244,7 @@ def _lineedit (self,name, layout, hide=True, width=None):
return lineedit

def _inputfield (self,name, layout, hide=True, width=None):
ui = FreeCADGui.UiLoader()
inputfield = ui.createWidget("Gui::InputField")
inputfield = self.uiloader.createWidget("Gui::InputField")
inputfield.setObjectName(name)
if hide: inputfield.hide()
if not width: width = 800
Expand Down Expand Up @@ -306,14 +309,14 @@ def setupToolBar(self,task=False):
self.layout.addLayout(yl)
self.layout.addLayout(zl)
self.labelx = self._label("labelx", xl)
self.xValue = self._lineedit("xValue", xl, width=60)
self.xValue.setText("0.00")
self.xValue = self._inputfield("xValue", xl) #width=60
self.xValue.setText(self.FORMAT % 0)
self.labely = self._label("labely", yl)
self.yValue = self._lineedit("yValue", yl, width=60)
self.yValue.setText("0.00")
self.yValue = self._inputfield("yValue", yl)
self.yValue.setText(self.FORMAT % 0)
self.labelz = self._label("labelz", zl)
self.zValue = self._lineedit("zValue", zl, width=60)
self.zValue.setText("0.00")
self.zValue = self._inputfield("zValue", zl)
self.zValue.setText(self.FORMAT % 0)
self.textValue = self._lineedit("textValue", self.layout)

# shapestring
Expand All @@ -333,14 +336,20 @@ def setupToolBar(self,task=False):
self.chooserButton.setText("...")

# options

self.numFaces = self._spinbox("numFaces", self.layout, 3)
self.offsetLabel = self._label("offsetlabel", self.layout)
self.offsetValue = self._lineedit("offsetValue", self.layout, width=60)
self.offsetValue.setText("0.00")
self.labelRadius = self._label("labelRadius", self.layout)
self.radiusValue = self._lineedit("radiusValue", self.layout, width=60)
self.radiusValue.setText("0.00")
fl = QtGui.QHBoxLayout()
self.layout.addLayout(fl)
self.numFacesLabel = self._label("numfaceslabel", fl)
self.numFaces = self._spinbox("numFaces", fl, 3)
ol = QtGui.QHBoxLayout()
self.layout.addLayout(ol)
self.offsetLabel = self._label("offsetlabel", ol)
self.offsetValue = self._inputfield("offsetValue", ol)
self.offsetValue.setText(self.FORMAT % 0)
rl = QtGui.QHBoxLayout()
self.layout.addLayout(rl)
self.labelRadius = self._label("labelRadius", rl)
self.radiusValue = self._inputfield("radiusValue", rl)
self.radiusValue.setText(self.FORMAT % 0)
self.isRelative = self._checkbox("isRelative",self.layout,checked=self.relativeMode)
self.hasFill = self._checkbox("hasFill",self.layout,checked=self.fillmode)
self.continueCmd = self._checkbox("continueCmd",self.layout,checked=self.continueMode)
Expand All @@ -365,6 +374,10 @@ def setupToolBar(self,task=False):
QtGui.QSizePolicy.Expanding)
self.layout.addItem(spacerItem)


QtCore.QObject.connect(self.xValue,QtCore.SIGNAL("valueChanged(double)"),self.changeXValue)
QtCore.QObject.connect(self.yValue,QtCore.SIGNAL("valueChanged(double)"),self.changeYValue)
QtCore.QObject.connect(self.zValue,QtCore.SIGNAL("valueChanged(double)"),self.changeZValue)
QtCore.QObject.connect(self.xValue,QtCore.SIGNAL("returnPressed()"),self.checkx)
QtCore.QObject.connect(self.yValue,QtCore.SIGNAL("returnPressed()"),self.checky)
QtCore.QObject.connect(self.xValue,QtCore.SIGNAL("textEdited(QString)"),self.checkSpecialChars)
Expand Down Expand Up @@ -467,6 +480,7 @@ def setupStyle(self):
style += "background-color: rgb(20,100,250) }"
self.baseWidget.setStyleSheet(style)


#---------------------------------------------------------------------------
# language tools
#---------------------------------------------------------------------------
Expand Down Expand Up @@ -504,6 +518,7 @@ def retranslateUi(self, widget=None):
self.closeButton.setToolTip(translate("draft", "Finishes and closes the current line (C)"))
self.wipeButton.setText(translate("draft", "&Wipe"))
self.wipeButton.setToolTip(translate("draft", "Wipes the existing segments of this line and starts again from the last point (W)"))
self.numFacesLabel.setText(translate("draft", "Sides"))
self.numFaces.setToolTip(translate("draft", "Number of sides"))
self.offsetLabel.setText(translate("draft", "Offset"))
self.xyButton.setText(translate("draft", "XY"))
Expand Down Expand Up @@ -557,7 +572,7 @@ def retranslateUi(self, widget=None):
# Interface modes
#---------------------------------------------------------------------------

def taskUi(self,title,extra=None,icon="Draft_Draft"):
def taskUi(self,title="Draft",extra=None,icon="Draft_Draft"):
if self.taskmode:
self.isTaskOn = True
todo.delay(FreeCADGui.Control.closeDialog,None)
Expand Down Expand Up @@ -694,6 +709,7 @@ def offUi(self):
self.setTitle(translate("draft", "None"))
self.labelx.setText(translate("draft", "X"))
self.hideXYZ()
self.numFacesLabel.hide()
self.numFaces.hide()
self.isRelative.hide()
self.hasFill.hide()
Expand Down Expand Up @@ -1019,10 +1035,13 @@ def validatePoint(self):
self.sourceCmd.offsetHandler(offset)
else:
try:
numx=float(self.xValue.text())
numy=float(self.yValue.text())
numz=float(self.zValue.text())
except ValueError:
#numx=float(self.xValue.text())
numx = self.x
#numy=float(self.yValue.text())
numy = self.y
#numz=float(self.zValue.text())
numz = self.z
except:
pass
else:
if self.pointcallback:
Expand Down Expand Up @@ -1271,22 +1290,21 @@ def displayPoint(self, point=None, last=None, plane=None, mask=None):
dp = plane.getLocalCoords(point)

# set widgets
ds = "%." + str(FreeCAD.ParamGet("User parameter:BaseApp/Preferences/Units").GetInt("Decimals",2)) + "f"
if self.mask in ['y','z']:
self.xValue.setText(ds % 0)
self.xValue.setText(self.FORMAT % 0)
else:
if dp:
self.xValue.setText(ds % dp.x)
self.xValue.setText(self.FORMAT % dp.x)
if self.mask in ['x','z']:
self.yValue.setText(ds % 0)
self.yValue.setText(self.FORMAT % 0)
else:
if dp:
self.yValue.setText(ds % dp.y)
self.yValue.setText(self.FORMAT % dp.y)
if self.mask in ['x','y']:
self.zValue.setText(ds % 0)
self.zValue.setText(self.FORMAT % 0)
else:
if dp:
self.zValue.setText(ds % dp.z)
self.zValue.setText(self.FORMAT % dp.z)

# set masks
if (mask == "x") or (self.mask == "x"):
Expand All @@ -1306,13 +1324,13 @@ def displayPoint(self, point=None, last=None, plane=None, mask=None):
self.yValue.setEnabled(False)
self.zValue.setEnabled(True)
self.zValue.setFocus()
self.zValue.selectAll()
self.zValue.selectAll()
else:
self.xValue.setEnabled(True)
self.yValue.setEnabled(True)
self.zValue.setEnabled(True)
self.xValue.setFocus()
self.xValue.selectAll()
self.xValue.selectAll()

def getDefaultColor(self,type,rgb=False):
"gets color from the preferences or toolbar"
Expand Down Expand Up @@ -1434,8 +1452,9 @@ def setSymmetricMode(self,bool):
self.addButton.setChecked(False)
self.delButton.setChecked(False)

def setRadiusValue(self,val):
self.radiusValue.setText("%.2f" % val)
def setRadiusValue(self,val,unit="mm"):
t = self.FORMAT.replace("mm",unit) % val
self.radiusValue.setText(t.decode("utf8".encode("latin1")))
self.radiusValue.setFocus()
self.radiusValue.selectAll()

Expand Down Expand Up @@ -1479,6 +1498,15 @@ def constrain(self,val):
if hasattr(FreeCADGui,"Snapper"):
FreeCADGui.Snapper.mask = val

def changeXValue(self,d):
self.x = d

def changeYValue(self,d):
self.y = d

def changeZValue(self,d):
self.z = d

#---------------------------------------------------------------------------
# TaskView operations
#---------------------------------------------------------------------------
Expand Down
6 changes: 3 additions & 3 deletions src/Mod/Draft/DraftSnap.py
Original file line number Diff line number Diff line change
Expand Up @@ -813,13 +813,13 @@ def setCursor(self,mode=None):
"setCursor(self,mode=None): sets or resets the cursor to the given mode or resets"
if self.selectMode:
mw = FreeCADGui.getMainWindow()
for w in mw.findChildren(QtGui.QWidget):
for w in mw.findChild(QtGui.QMdiArea).findChildren(QtGui.QWidget):
if w.metaObject().className() == "SoQtGLArea":
w.unsetCursor()
self.cursorMode = None
elif not mode:
mw = FreeCADGui.getMainWindow()
for w in mw.findChildren(QtGui.QWidget):
for w in mw.findChild(QtGui.QMdiArea).findChildren(QtGui.QWidget):
if w.metaObject().className() == "SoQtGLArea":
w.unsetCursor()
self.cursorMode = None
Expand All @@ -837,7 +837,7 @@ def setCursor(self,mode=None):
qp.end()
cur = QtGui.QCursor(newicon,8,8)
mw = FreeCADGui.getMainWindow()
for w in mw.findChildren(QtGui.QWidget):
for w in mw.findChild(QtGui.QMdiArea).findChildren(QtGui.QWidget):
if w.metaObject().className() == "SoQtGLArea":
w.setCursor(cur)
self.cursorMode = mode
Expand Down
21 changes: 15 additions & 6 deletions src/Mod/Draft/DraftTools.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# -*- coding: utf8 -*-

#***************************************************************************
#* *
#* Copyright (c) 2009, 2010 *
Expand Down Expand Up @@ -80,6 +82,12 @@ def msg(text=None,mode=None):
else:
FreeCAD.Console.PrintMessage(text)

def formatUnit(exp,unit="mm"):
'''returns a formatting string to set a number to the correct unit'''
d = FreeCAD.ParamGet("User parameter:BaseApp/Preferences/Units").GetInt("Decimals",2)
f = "%." + str(d) + "f " + unit
return f % exp

def selectObject(arg):
'''this is a scene even handler, to be called from the Draft tools
when they need to select an object'''
Expand Down Expand Up @@ -1065,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))
self.ui.setRadiusValue(math.degrees(angle),unit="°")
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))
self.ui.setRadiusValue(math.degrees(angle),unit="°")
self.updateAngle(angle)
self.arctrack.setApertureAngle(self.angle)

Expand Down Expand Up @@ -1275,6 +1283,7 @@ def Activated(self):
self.ui.pointUi(name)
self.ui.extUi()
self.ui.numFaces.show()
self.ui.numFacesLabel.show()
self.altdown = False
self.ui.sourceCmd = self
self.arctrack = arcTracker()
Expand Down Expand Up @@ -2286,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("%.2f" % math.degrees(angle))
self.ui.radiusValue.setText(formatUnit(math.degrees(angle)))
self.firstangle = angle
self.ui.radiusValue.setFocus()
self.ui.radiusValue.selectAll()
Expand All @@ -2303,7 +2312,7 @@ def action(self,arg):
if self.ghost:
self.ghost.rotate(plane.axis,sweep)
self.ghost.on()
self.ui.radiusValue.setText("%.2f" % math.degrees(sweep))
self.ui.radiusValue.setText(formatUnit(math.degrees(sweep)))
self.ui.radiusValue.setFocus()
self.ui.radiusValue.selectAll()

Expand Down Expand Up @@ -2478,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("%.2f" % dist[0].Length)
self.ui.radiusValue.setText(formatUnit(dist[0].Length))
else:
self.dvec = None
self.ghost.off()
Expand Down Expand Up @@ -2703,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("%.2f" % dist)
self.ui.radiusValue.setText(formatUnit(dist))
self.ui.radiusValue.setFocus()
self.ui.radiusValue.selectAll()

Expand Down

0 comments on commit f5627b3

Please sign in to comment.