def test_constructor_text(qtbot): widget = UnitEdit(text='text') show(locals()) assert bool(widget.getError()) is False assert getCurrentColor( widget, 'Background').names[0] == widget.defaultColors['default'][0] assert set(widget.getSymbols().keys()) == {'text'}
def test_constructor(qtbot): widget = UnitEdit() show(locals()) assert widget.getError() is None assert getCurrentColor( widget, 'Background').names[0] == widget.defaultColors['blank'][0] assert dict() == widget.getSymbols()
def test_unit_consistency(qtbot): widget = UnitEdit() show(locals()) for e in units_work_check: testLogger.debug(e) if e[1] is False: with pytest.raises(UnitMisMatchError): unitsAreConsistent(parse_expr(e[0], local_dict=unitSubs)) widget.setText(e[0]) assert bool(widget.getError()) is not e[1] if e[1] is False: continue
def test_conversion_with_symbols(qtbot): widget = UnitEdit(text='1*b*mm') show(locals()) syms = widget.getSymbols() assert set(syms.keys()) == {'b'} expr = widget.getExpr() conv = units.convert_to(expr, units.meter) assert conv == units.meter * Symbol('b') / 1000 assert widget.convertTo('meter') == conv assert widget.convertTo(units.meter) == conv
def setupUi(self, Form): Form.setObjectName("Form") Form.resize(693, 190) self.gridLayout = QtWidgets.QGridLayout(Form) self.gridLayout.setObjectName("gridLayout") self.sympyExprEdit = ExprEdit(Form) self.sympyExprEdit.setMinimumSize(QtCore.QSize(120, 0)) self.sympyExprEdit.setMaximumSize(QtCore.QSize(120, 16777215)) self.sympyExprEdit.setObjectName("sympyExprEdit") self.gridLayout.addWidget(self.sympyExprEdit, 2, 1, 1, 1) self.label_17 = QtWidgets.QLabel(Form) self.label_17.setObjectName("label_17") self.gridLayout.addWidget(self.label_17, 5, 9, 1, 1) self.label_12 = QtWidgets.QLabel(Form) self.label_12.setWordWrap(True) self.label_12.setObjectName("label_12") self.gridLayout.addWidget(self.label_12, 2, 9, 1, 1) self.line = QtWidgets.QFrame(Form) self.line.setFrameShape(QtWidgets.QFrame.VLine) self.line.setFrameShadow(QtWidgets.QFrame.Sunken) self.line.setObjectName("line") self.gridLayout.addWidget(self.line, 0, 4, 7, 1) self.sympyEntryWidget = SympyEntryWidget(Form) self.sympyEntryWidget.setMinimumSize(QtCore.QSize(120, 0)) self.sympyEntryWidget.setMaximumSize(QtCore.QSize(120, 16777215)) self.sympyEntryWidget.setObjectName("sympyEntryWidget") self.gridLayout.addWidget(self.sympyEntryWidget, 6, 1, 1, 1) self.sympySymbolEdit = SymbolEdit(Form) self.sympySymbolEdit.setMinimumSize(QtCore.QSize(120, 0)) self.sympySymbolEdit.setMaximumSize(QtCore.QSize(120, 16777215)) self.sympySymbolEdit.setObjectName("sympySymbolEdit") self.gridLayout.addWidget(self.sympySymbolEdit, 4, 1, 1, 1) self.label = QtWidgets.QLabel(Form) self.label.setMaximumSize(QtCore.QSize(120, 16777215)) self.label.setObjectName("label") self.gridLayout.addWidget(self.label, 2, 5, 1, 1) self.label_3 = QtWidgets.QLabel(Form) self.label_3.setMaximumSize(QtCore.QSize(120, 16777215)) self.label_3.setObjectName("label_3") self.gridLayout.addWidget(self.label_3, 4, 5, 1, 1) self.label_4 = QtWidgets.QLabel(Form) self.label_4.setMaximumSize(QtCore.QSize(120, 16777215)) self.label_4.setObjectName("label_4") self.gridLayout.addWidget(self.label_4, 6, 5, 1, 1) self.label_2 = QtWidgets.QLabel(Form) self.label_2.setMaximumSize(QtCore.QSize(120, 16777215)) self.label_2.setObjectName("label_2") self.gridLayout.addWidget(self.label_2, 3, 5, 1, 1) self.sympyUnitEdit = UnitEdit(Form) self.sympyUnitEdit.setMinimumSize(QtCore.QSize(120, 0)) self.sympyUnitEdit.setMaximumSize(QtCore.QSize(120, 16777215)) self.sympyUnitEdit.setObjectName("sympyUnitEdit") self.gridLayout.addWidget(self.sympyUnitEdit, 3, 1, 1, 1) self.label_5 = QtWidgets.QLabel(Form) self.label_5.setMinimumSize(QtCore.QSize(150, 0)) self.label_5.setObjectName("label_5") self.gridLayout.addWidget(self.label_5, 2, 0, 1, 1) self.label_7 = QtWidgets.QLabel(Form) self.label_7.setMinimumSize(QtCore.QSize(150, 0)) self.label_7.setObjectName("label_7") self.gridLayout.addWidget(self.label_7, 4, 0, 1, 1) self.label_9 = QtWidgets.QLabel(Form) self.label_9.setAlignment(QtCore.Qt.AlignCenter) self.label_9.setObjectName("label_9") self.gridLayout.addWidget(self.label_9, 0, 5, 1, 1) self.label_10 = QtWidgets.QLabel(Form) self.label_10.setAlignment(QtCore.Qt.AlignCenter) self.label_10.setObjectName("label_10") self.gridLayout.addWidget(self.label_10, 0, 7, 1, 1) self.label_6 = QtWidgets.QLabel(Form) self.label_6.setMinimumSize(QtCore.QSize(150, 0)) self.label_6.setObjectName("label_6") self.gridLayout.addWidget(self.label_6, 3, 0, 1, 1) self.label_8 = QtWidgets.QLabel(Form) self.label_8.setMinimumSize(QtCore.QSize(150, 0)) self.label_8.setObjectName("label_8") self.gridLayout.addWidget(self.label_8, 6, 0, 1, 1) self.line_3 = QtWidgets.QFrame(Form) self.line_3.setFrameShape(QtWidgets.QFrame.VLine) self.line_3.setFrameShadow(QtWidgets.QFrame.Sunken) self.line_3.setObjectName("line_3") self.gridLayout.addWidget(self.line_3, 0, 6, 7, 1) self.label_16 = QtWidgets.QLabel(Form) self.label_16.setMinimumSize(QtCore.QSize(150, 0)) self.label_16.setObjectName("label_16") self.gridLayout.addWidget(self.label_16, 5, 0, 1, 1) self.sympydimensionedit = DimensionEdit(Form) self.sympydimensionedit.setMinimumSize(QtCore.QSize(120, 0)) self.sympydimensionedit.setMaximumSize(QtCore.QSize(120, 16777215)) self.sympydimensionedit.setObjectName("sympydimensionedit") self.gridLayout.addWidget(self.sympydimensionedit, 5, 1, 1, 1) self.label_18 = QtWidgets.QLabel(Form) self.label_18.setMaximumSize(QtCore.QSize(120, 16777215)) self.label_18.setObjectName("label_18") self.gridLayout.addWidget(self.label_18, 5, 5, 1, 1) self.label_20 = QtWidgets.QLabel(Form) self.label_20.setMinimumSize(QtCore.QSize(120, 0)) self.label_20.setMaximumSize(QtCore.QSize(120, 16777215)) self.label_20.setObjectName("label_20") self.gridLayout.addWidget(self.label_20, 2, 7, 1, 1) self.label_19 = QtWidgets.QLabel(Form) self.label_19.setMinimumSize(QtCore.QSize(120, 0)) self.label_19.setMaximumSize(QtCore.QSize(120, 16777215)) self.label_19.setObjectName("label_19") self.gridLayout.addWidget(self.label_19, 3, 7, 1, 1) self.label_21 = QtWidgets.QLabel(Form) self.label_21.setMinimumSize(QtCore.QSize(120, 0)) self.label_21.setMaximumSize(QtCore.QSize(120, 16777215)) self.label_21.setObjectName("label_21") self.gridLayout.addWidget(self.label_21, 4, 7, 1, 1) self.label_22 = QtWidgets.QLabel(Form) self.label_22.setMinimumSize(QtCore.QSize(120, 0)) self.label_22.setMaximumSize(QtCore.QSize(120, 16777215)) self.label_22.setObjectName("label_22") self.gridLayout.addWidget(self.label_22, 5, 7, 1, 1) self.label_23 = QtWidgets.QLabel(Form) self.label_23.setMinimumSize(QtCore.QSize(120, 0)) self.label_23.setMaximumSize(QtCore.QSize(120, 16777215)) self.label_23.setObjectName("label_23") self.gridLayout.addWidget(self.label_23, 6, 7, 1, 1) self.label_11 = QtWidgets.QLabel(Form) self.label_11.setAlignment(QtCore.Qt.AlignCenter) self.label_11.setObjectName("label_11") self.gridLayout.addWidget(self.label_11, 0, 9, 1, 1) self.label_13 = QtWidgets.QLabel(Form) self.label_13.setWordWrap(True) self.label_13.setObjectName("label_13") self.gridLayout.addWidget(self.label_13, 3, 9, 1, 1) self.label_14 = QtWidgets.QLabel(Form) self.label_14.setWordWrap(True) self.label_14.setObjectName("label_14") self.gridLayout.addWidget(self.label_14, 4, 9, 1, 1) self.label_15 = QtWidgets.QLabel(Form) self.label_15.setWordWrap(True) self.label_15.setObjectName("label_15") self.gridLayout.addWidget(self.label_15, 6, 9, 1, 1) self.line_2 = QtWidgets.QFrame(Form) self.line_2.setFrameShape(QtWidgets.QFrame.VLine) self.line_2.setFrameShadow(QtWidgets.QFrame.Sunken) self.line_2.setObjectName("line_2") self.gridLayout.addWidget(self.line_2, 0, 8, 7, 1) self.gridLayout.setColumnStretch(1, 1) self.gridLayout.setColumnStretch(9, 2) self.retranslateUi(Form) self.sympyExprEdit.exprChanged['QString'].connect(self.label.setText) self.sympyUnitEdit.exprChanged['QString'].connect(self.label_2.setText) self.sympySymbolEdit.exprChanged['QString'].connect( self.label_3.setText) self.sympyExprEdit.errorChanged['QString'].connect( self.label_12.setText) self.sympyUnitEdit.errorChanged['QString'].connect( self.label_13.setText) self.sympySymbolEdit.errorChanged['QString'].connect( self.label_14.setText) self.sympyEntryWidget.exprChanged['QString'].connect( self.label_4.setText) self.sympyEntryWidget.errorChanged['QString'].connect( self.label_15.setText) self.sympydimensionedit.errorChanged['QString'].connect( self.label_17.setText) self.sympydimensionedit.exprChanged['QString'].connect( self.label_18.setText) self.sympyExprEdit.valueChanged['QString'].connect( self.label_20.setText) self.sympyUnitEdit.valueChanged['QString'].connect( self.label_19.setText) self.sympydimensionedit.valueChanged['QString'].connect( self.label_22.setText) self.sympyEntryWidget.valueChanged['QString'].connect( self.label_23.setText) QtCore.QMetaObject.connectSlotsByName(Form) Form.setTabOrder(self.sympyExprEdit, self.sympyUnitEdit) Form.setTabOrder(self.sympyUnitEdit, self.sympySymbolEdit) Form.setTabOrder(self.sympySymbolEdit, self.sympydimensionedit)
sym.exprChanged[object].connect( printer('SymbolEdit.exprChanged[object] -> Symbol is:')) sym.setToolTip("Enter a symbol name") layout.addWidget(sym) expr = ExprEdit(text='1*a + 2*b - 1*a + cos(pi)') expr.hasError[str].connect(printer('ExprEdit.hasError[str]')) expr.exprChanged[str].connect( printer('ExprEdit.exprChanged[str] -> Expression is:')) expr.editingFinished.connect(lambda: show_symbols(expr)) expr.editingFinished.connect( lambda: print('ExprEdit.editingFinished -> getValue():', expr.getValue())) expr.setToolTip("Do some math") layout.addWidget(expr) unit = UnitEdit(text='2*mm + 1*inch') unit.hasError[str].connect(printer('UnitEdit.hasError[str]')) unit.exprChanged[object].connect(printer('UnitEdit.exprChanged[object]')) unit.exprChanged.connect( lambda: print('UnitEdit.exprChanged -> getExpr()', unit.getExpr())) unit.exprChanged.connect( lambda: print('UnitEdit.exprChanged -> getValue()', unit.getValue())) unit.valueChanged[object].connect(printer('UnitEdit.valueChanged[object]')) unit.setToolTip("Do math with units") layout.addWidget(unit) dim = DimensionEdit(text='2*mm + 1*inch', dimension='length') dim.hasError[str].connect(printer('DimensionEdit.hasError[str]')) dim.exprChanged[object].connect(printer('DimensionEdit.exprChanged[object]')) dim.valueChanged[object].connect(printer('DimensionEdit.valueChanged[object]')) dim.setToolTip("Dimension (length) is enforced")
def test_invalid_conversion(qtbot): widget = UnitEdit(text='(1*mm)*b') show(locals()) syms = widget.getSymbols() assert set(syms.keys()) == {'b'} assert (widget.convertTo(units.inch) - parseUnits('b*5*inch/127')).simplify() == 0 assert (widget.convertTo('kg') - parseUnits('b*mm')).simplify() == 0 assert (widget.convertTo(units.m * units.m) - parseUnits('b*m/1000')).simplify() == 0 assert (widget.convertTo(units.m) - parseUnits('b*m/1000')).simplify() == 0 widget = UnitEdit(text='2*mm') show(locals()) assert widget.getSymbols() == dict() widget.convertTo(units.inch) assert (widget.convertTo('kg') - parseUnits('2*mm')).simplify() == 0 assert (widget.convertTo(units.m * units.m) - parseUnits('1*m/500')).simplify() == 0 assert (widget.convertTo(units.m) - parseUnits('1*m/500')).simplify() == 0
def test_constructor_units(qtbot): widget = UnitEdit(text='2.0*mm') show(locals()) assert widget.getError() is False assert getCurrentColor( widget, 'Background').names[0] == widget.defaultColors['default'][0] assert widget.getSymbols() == dict() assert widget.getExpr().atoms(units.Unit) == {units.mm} assert widget.getDimension() == units.Dimension('length') assert widget.getMagnitude() == 2 assert widget.getUnits() == units.millimeter assert widget.getValue() == widget.getUnits() * widget.getMagnitude()