Esempio n. 1
0
def test_constructor_blank(qtbot):
    widget = SympyEntryWidget()
    show(locals())
    testLogger.debug((widget.getError(), widget.lineEdit.getError(),
                      widget.lineEdit._error, widget.lineEdit._expr))
    assert widget.getError() is None
    assert getCurrentColor(
        widget.lineEdit,
        'Background').names[0] == widget.defaultColors['blank'][0]
Esempio n. 2
0
def test_constructor_text(qtbot):
    widget = SympyEntryWidget(text='text*mm')
    show(locals())
    testLogger.debug((widget.getError(), widget.lineEdit.getError(),
                      widget.lineEdit._error, widget.lineEdit._expr))
    assert widget.getError() is False
    assert list(widget.getSymbols().keys()) == ['text']
    assert getCurrentColor(
        widget.lineEdit,
        'Background').names[0] == widget.defaultColors['default'][0]

    widget = SympyEntryWidget(text='text')
    show(locals())
    testLogger.debug((widget.getError(), widget.lineEdit.getError(),
                      widget.lineEdit._error, widget.lineEdit._expr))
    assert widget.getError()
    assert getCurrentColor(
        widget.lineEdit,
        'Background').names[0] == widget.defaultColors['error'][0]
Esempio n. 3
0
def test_text_changes_value(qtbot):
    widget = SympyEntryWidget(text='1*mm + 1*inch', options='length')
    show(locals())
    assert widget.getError() is False
    assert (widget.convertTo(units.inch) -
            (132 / 127) * units.inch) == 0.0 * units.inch

    qtbot.keyPress(widget.lineEdit, QtCore.Qt.Key_Home)
    qtbot.keyClicks(widget.lineEdit, '1')
    assert (widget.convertTo(units.inch) -
            (182 / 127) * units.inch) == 0.0 * units.inch
    assert quantity_simplify(widget.getValue() - (182 / 127 * units.inch)) == 0

    qtbot.keyPress(widget.lineEdit, 'b')
    assert widget.getError()
    assert getCurrentColor(
        widget.lineEdit, 'Window').names[0] == widget.defaultColors['error'][0]
Esempio n. 4
0
def test_invalid_conversion(qtbot):
    widget = SympyEntryWidget(text='(1*mm)*b',
                              options={
                                  'mm': units.mm,
                                  'kg': units.kg,
                                  'm2': units.m * units.m
                              })
    show(locals())
    assert set(widget.getSymbols().keys()) == {'b'}

    testLogger.debug((widget.getExpr(), widget.getUnits(), widget.getValue()))
    assert widget.convertTo('kg') == parseUnits('b*mm')

    widget.setSelected('m2')
    testLogger.debug((widget.getExpr(), widget.getUnits(), widget.getValue()))
    assert bool(widget.getError()) is True

    widget.setUnits('mm')
    assert widget.convertTo('mm') == parseUnits('b*mm')
Esempio n. 5
0
def test_conversion_with_symbols(qtbot):
    widget = SympyEntryWidget(text='1*b*mm',
                              options={
                                  'mm': units.mm,
                                  'm': units.m,
                                  'kg': units.kg
                              })
    show(locals())
    assert set(widget.getSymbols().keys()) == {'b'}

    expr = widget.getValue()
    conv = units.convert_to(expr, units.meter)
    assert conv == units.meter * Symbol('b') / 1000
    assert widget.convertTo('meter') == conv

    widget.setSelected('kg')
    assert widget.getError()
    assert widget.getValue() is None

    widget.setUnits('m')
    expr = widget.getValue()
    conv = units.convert_to(expr, units.gram)
    assert (conv - units.meter * Symbol('b') / 1000) == 0.0 * units.meter
    assert (widget.convertTo('meter') - conv) == 0.0 * units.meter
    assert (conv - widget.convertTo(units.gram)) == 0.0 * units.meter
Esempio n. 6
0
def test_option_changes_value(qtbot):
    widget = SympyEntryWidget(text='3*mm',
                              windowTitle='EntryWidget',
                              objectName='EntryWidget',
                              options={
                                  'mm': units.mm,
                                  'm': units.meter,
                                  'kg': units.kg,
                                  'n/a': units.One
                              })
    show(locals())
    assert widget.getValue() == 3.0 * units.mm
    widget.setUnits('kg')
    assert widget.getError()
    widget.setUnits('m')
    assert widget.getValue() == .003 * units.m
    widget.setUnits('n/a')
    assert widget.getError()
    widget.setText('3')
    assert widget.getValue() == 3

    widget = SympyEntryWidget(text='3*inch',
                              windowTitle='EntryWidget',
                              objectName='EntryWidget',
                              options={
                                  'mm': units.mm,
                                  'kg': units.kg,
                                  'n/a': units.One
                              })
    show(locals())
    assert abs(
        quantity_simplify(widget.getValue() -
                          (3 * 25.4) * units.mm).evalf()) <= (1e-12) * units.mm
    widget.setUnits('kg')
    assert widget.getError()
    assert widget.getValue() is None
    assert widget.convertTo(units.inch) is None
    widget.setUnits('n/a')
    assert widget.getError()
    assert widget.getValue() is None
    assert widget.convertTo(units.inch) is None

    widget.setText('3*inch / (1*foot)')
    assert widget.getError() is False
    assert widget.getValue() == .25
    assert (widget.convertTo(units.inch) - .25 * units.inch) == 0.0
Esempio n. 7
0
    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)
Esempio n. 8
0
from sympyentrywidget import SympyEntryWidget, ExprEdit, UnitEdit, DimensionEdit, SymbolEdit
from PyQt5.QtWidgets import QWidget, QVBoxLayout

# Qt stuff
# from PyQt5.Qt import QApplication  # optional, can be started from widget.mkQApp()
# from PyQt5.QtWidgets import QApplication
# app = QApplication(sys.argv)

import sys

# log to console
import logging
logging.basicConfig(stream=sys.stdout, level=logging.DEBUG)

# start Qt
app = SympyEntryWidget.mkQApp()


# <editor-fold desc="Support Funcs">
def show_symbols(widget):
    print(type(widget).__name__, '-> symbols:', widget.getSymbols())


def printer(title):
    return lambda *t: print(title, *t)


# </editor-fold>

w = QWidget()
layout = QVBoxLayout()