Skip to content

Commit

Permalink
Moved to InputField widget in ship creation
Browse files Browse the repository at this point in the history
  • Loading branch information
sanguinariojoe authored and wwmayer committed Jul 13, 2014
1 parent d7fafdb commit 978030a
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 81 deletions.
1 change: 1 addition & 0 deletions src/Mod/Ship/CMakeLists.txt
Expand Up @@ -59,6 +59,7 @@ SET(ShipUtils_SRCS
shipUtils/__init__.py
shipUtils/Math.py
shipUtils/Paths.py
shipUtils/Units.py
)
SOURCE_GROUP("shiputils" FILES ${ShipUtils_SRCS})

Expand Down
3 changes: 2 additions & 1 deletion src/Mod/Ship/Makefile.am
Expand Up @@ -35,7 +35,8 @@ nobase_data_DATA = \
shipHydrostatics/Tools.py \
shipUtils/__init__.py \
shipUtils/Math.py \
shipUtils/Paths.py
shipUtils/Paths.py \
shipUtils/Units.py

CLEANFILES = $(BUILT_SOURCES)

Expand Down
95 changes: 60 additions & 35 deletions src/Mod/Ship/shipCreateShip/TaskPanel.py
Expand Up @@ -28,7 +28,7 @@
import Preview
import Instance
from shipUtils import Paths

import shipUtils.Units as USys

class TaskPanel:
def __init__(self):
Expand All @@ -44,12 +44,13 @@ def accept(self):
Instance.ViewProviderShip(obj.ViewObject)
mw = self.getMainWindow()
form = mw.findChild(QtGui.QWidget, "TaskPanel")
form.length = self.widget(QtGui.QDoubleSpinBox, "Length")
form.breadth = self.widget(QtGui.QDoubleSpinBox, "Breadth")
form.draft = self.widget(QtGui.QDoubleSpinBox, "Draft")
obj.Length = '{} m'.format(form.length.value())
obj.Breadth = '{} m'.format(form.breadth.value())
obj.Draft = '{} m'.format(form.draft.value())
form.length = self.widget(QtGui.QLineEdit, "Length")
form.breadth = self.widget(QtGui.QLineEdit, "Breadth")
form.draft = self.widget(QtGui.QLineEdit, "Draft")

obj.Length = form.length.text()
obj.Breadth = form.breadth.text()
obj.Draft = form.draft.text()
App.ActiveDocument.recompute()
return True

Expand Down Expand Up @@ -83,9 +84,9 @@ def setupUi(self):
"""Create and configurate the user interface"""
mw = self.getMainWindow()
form = mw.findChild(QtGui.QWidget, "TaskPanel")
form.length = self.widget(QtGui.QDoubleSpinBox, "Length")
form.breadth = self.widget(QtGui.QDoubleSpinBox, "Breadth")
form.draft = self.widget(QtGui.QDoubleSpinBox, "Draft")
form.length = self.widget(QtGui.QLineEdit, "Length")
form.breadth = self.widget(QtGui.QLineEdit, "Breadth")
form.draft = self.widget(QtGui.QLineEdit, "Draft")
form.mainLogo = self.widget(QtGui.QLabel, "MainLogo")
form.mainLogo.setPixmap(QtGui.QPixmap(":/icons/Ship_Logo.svg"))
self.form = form
Expand Down Expand Up @@ -167,7 +168,7 @@ def initValues(self):
return True
# Get the ship bounds. The ship instance can not have dimensions
# out of these values.
bounds = [0.0, 0.0, 0.0]
self.bounds = [0.0, 0.0, 0.0]
bbox = self.solids[0].BoundBox
minX = bbox.XMin
maxX = bbox.XMax
Expand All @@ -189,28 +190,30 @@ def initValues(self):
minZ = bbox.ZMin
if maxZ < bbox.ZMax:
maxZ = bbox.ZMax
bounds[0] = maxX - minX
bounds[1] = max(maxY - minY, abs(maxY), abs(minY))
bounds[2] = maxZ - minZ
self.bounds[0] = maxX - minX
self.bounds[1] = max(maxY - minY, abs(maxY), abs(minY))
self.bounds[2] = maxZ - minZ

input_format = USys.getLengthFormat()

mw = self.getMainWindow()
form = mw.findChild(QtGui.QWidget, "TaskPanel")
form.length = self.widget(QtGui.QDoubleSpinBox, "Length")
form.breadth = self.widget(QtGui.QDoubleSpinBox, "Breadth")
form.draft = self.widget(QtGui.QDoubleSpinBox, "Draft")
form.length = self.widget(QtGui.QLineEdit, "Length")
form.breadth = self.widget(QtGui.QLineEdit, "Breadth")
form.draft = self.widget(QtGui.QLineEdit, "Draft")

form.length.setMaximum(bounds[0] / Units.Metre.Value)
form.length.setMinimum(0.001)
form.length.setValue(bounds[0] / Units.Metre.Value)
self.L = bounds[0] / Units.Metre.Value
form.breadth.setMaximum(bounds[1] / Units.Metre.Value)
form.breadth.setMinimum(0.001)
form.breadth.setValue(bounds[1] / Units.Metre.Value)
self.B = bounds[1] / Units.Metre.Value
form.draft.setMaximum(bounds[2] / Units.Metre.Value)
form.draft.setMinimum(0.001)
form.draft.setValue(0.5 * bounds[2] / Units.Metre.Value)
self.T = 0.5 * bounds[2] / Units.Metre.Value
qty = Units.Quantity(self.bounds[0], Units.Length)
form.length.setText(input_format.format(
qty.getValueAs(USys.getLengthUnits()).Value))
self.L = self.bounds[0] / Units.Metre.Value
qty = Units.Quantity(self.bounds[1], Units.Length)
form.breadth.setText(input_format.format(
qty.getValueAs(USys.getLengthUnits()).Value))
self.B = self.bounds[1] / Units.Metre.Value
qty = Units.Quantity(self.bounds[2], Units.Length)
form.draft.setText(input_format.format(
0.5 * qty.getValueAs(USys.getLengthUnits()).Value))
self.T = 0.5 * self.bounds[2] / Units.Metre.Value
return False

def retranslateUi(self):
Expand Down Expand Up @@ -239,6 +242,16 @@ def retranslateUi(self):
None,
QtGui.QApplication.UnicodeUTF8))

def clampVal(self, widget, val_min, val_max, val):
if val >= val_min and val <= val_max:
return val
input_format = USys.getLengthFormat()
val = min(val_max, max(val_min, val))
qty = Units.Quantity('{} m'.format(val))
widget.setText(input_format.format(
qty.getValueAs(USys.getLengthUnits()).Value))
return val

def onData(self, value):
"""Updates the 3D preview on data changes.
Expand All @@ -248,13 +261,25 @@ def onData(self, value):
"""
mw = self.getMainWindow()
form = mw.findChild(QtGui.QWidget, "TaskPanel")
form.length = self.widget(QtGui.QDoubleSpinBox, "Length")
form.breadth = self.widget(QtGui.QDoubleSpinBox, "Breadth")
form.draft = self.widget(QtGui.QDoubleSpinBox, "Draft")
form.length = self.widget(QtGui.QLineEdit, "Length")
form.breadth = self.widget(QtGui.QLineEdit, "Breadth")
form.draft = self.widget(QtGui.QLineEdit, "Draft")

self.L = form.length.value()
self.B = form.breadth.value()
self.T = form.draft.value()
qty = Units.Quantity(form.length.text())
val_min = 0.001
val_max = self.bounds[0] / Units.Metre.Value
val = qty.getValueAs('m').Value
self.L = self.clampVal(form.length, val_min, val_max, val)
qty = Units.Quantity(form.breadth.text())
val_min = 0.001
val_max = self.bounds[1] / Units.Metre.Value
val = qty.getValueAs('m').Value
self.B = self.clampVal(form.breadth, val_min, val_max, val)
qty = Units.Quantity(form.draft.text())
val_min = 0.001
val_max = self.bounds[2] / Units.Metre.Value
val = qty.getValueAs('m').Value
self.T = self.clampVal(form.draft, val_min, val_max, val)
self.preview.update(self.L, self.B, self.T)

def getSolids(self, obj):
Expand Down
58 changes: 13 additions & 45 deletions src/Mod/Ship/shipCreateShip/TaskPanel.ui
Expand Up @@ -102,26 +102,13 @@
</widget>
</item>
<item>
<widget class="QDoubleSpinBox" name="Length">
<property name="decimals">
<number>3</number>
</property>
<property name="singleStep">
<double>0.010000000000000</double>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="LengthUnits">
<widget class="Gui::InputField" name="Length">
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Preferred">
<sizepolicy hsizetype="Preferred" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>m</string>
</property>
</widget>
</item>
</layout>
Expand All @@ -148,26 +135,13 @@
</widget>
</item>
<item>
<widget class="QDoubleSpinBox" name="Breadth">
<property name="decimals">
<number>3</number>
</property>
<property name="singleStep">
<double>0.010000000000000</double>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="BreadthUnits">
<widget class="Gui::InputField" name="Breadth">
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Preferred">
<sizepolicy hsizetype="Preferred" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>m</string>
</property>
</widget>
</item>
</layout>
Expand All @@ -194,26 +168,13 @@
</widget>
</item>
<item>
<widget class="QDoubleSpinBox" name="Draft">
<property name="decimals">
<number>3</number>
</property>
<property name="singleStep">
<double>0.010000000000000</double>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="DraftUnits">
<widget class="Gui::InputField" name="Draft">
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Preferred">
<sizepolicy hsizetype="Preferred" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>m</string>
</property>
</widget>
</item>
</layout>
Expand All @@ -227,6 +188,13 @@
</item>
</layout>
</widget>
<customwidgets>
<customwidget>
<class>Gui::InputField</class>
<extends>QLineEdit</extends>
<header location="global">Gui/Inputfield.h</header>
</customwidget>
</customwidgets>
<resources/>
<connections/>
</ui>
42 changes: 42 additions & 0 deletions src/Mod/Ship/shipUtils/Units.py
@@ -0,0 +1,42 @@
#***************************************************************************
#* *
#* Copyright (c) 2011, 2012 *
#* Jose Luis Cercos Pita <jlcercos@gmail.com> *
#* *
#* This program is free software; you can redistribute it and/or modify *
#* it under the terms of the GNU Lesser General Public License (LGPL) *
#* as published by the Free Software Foundation; either version 2 of *
#* the License, or (at your option) any later version. *
#* for detail see the LICENCE text file. *
#* *
#* This program is distributed in the hope that it will be useful, *
#* but WITHOUT ANY WARRANTY; without even the implied warranty of *
#* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
#* GNU Library General Public License for more details. *
#* *
#* You should have received a copy of the GNU Library General Public *
#* License along with this program; if not, write to the Free Software *
#* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 *
#* USA *
#* *
#***************************************************************************

import FreeCAD
import Units


# Systems of length units
LENGTH_UNITS = ('mm', 'm', 'in', 'in')


def getLengthUnits():
param = FreeCAD.ParamGet("User parameter:BaseApp/Preferences/Units")
units_id = param.GetInt('UserSchema', 0)
return LENGTH_UNITS[units_id]


def getLengthFormat():
param = FreeCAD.ParamGet("User parameter:BaseApp/Preferences/Units")
decimals = param.GetInt("Decimals", 2)
units_id = param.GetInt('UserSchema', 0)
return '{0:.' + str(decimals) + 'f} ' + LENGTH_UNITS[units_id]
1 change: 1 addition & 0 deletions src/WindowsInstaller/ModShip.wxi
Expand Up @@ -92,6 +92,7 @@
<File Id="shipUtils01" Name="__init__.py" />
<File Id="shipUtils02" Name="Math.py" />
<File Id="shipUtils03" Name="Paths.py" />
<File Id="shipUtils04" Name="Units.py" />
</Component>
</Directory>
</Directory>
Expand Down

0 comments on commit 978030a

Please sign in to comment.