Esempio n. 1
0
class UnitLineEdit(QtWidgets.QWidget):
    edited = QtCore.Signal()

    def __init__(self, unitGroup: UnitGroup) -> None:
        super(UnitLineEdit, self).__init__()
        self.unit_idx : int = 0
        self._value : Optional[float] = 0

        self._layout = QtWidgets.QHBoxLayout()
        self._layout.setContentsMargins(0,0,0,0)

        self.__lineEdit = PLineEdit()

        # Skipping typing - slots don't seem to work
        self.__lineEdit.editingFinished.connect(self.text_changed)

        self.__unitDropDown = QtWidgets.QComboBox()
        self.__unitDropDown.currentIndexChanged.connect(self.indexChanged)

        self._placeholder_value : Optional[float] = None

        self.setLayout(self._layout)
        self._layout.addWidget(self.__lineEdit, 0)
        self._layout.addWidget(self.__unitDropDown, 1)

        self.setUnitGroup(unitGroup)

    def setPlaceholderText(self, text: str) -> None:
        self.__lineEdit.setPlaceholderText(text)

    def setPlaceholderValue(self, value: float) -> None:
        self._placeholder_value = value
        self.update_field_value()

    def setUnitGroup(self, ug: UnitGroup) -> None:
        self.__unitGroup = ug
        self.__unitDropDown.clear()
        for k in ug.names:
            self.__unitDropDown.addItem(k)

        self.__unitDropDown.setCurrentIndex(ug.default_index)

    def indexChanged(self, idx: int) -> None:
        self.unit_idx = idx
        self.update_field_value()

    def setUnitName(self, unitname: str) -> None:
        pass

    def getUnitName(self) -> None:
        pass

    def setEnabled(self, enabled: bool) -> None:
        self.__unitDropDown.setEnabled(enabled)
        self.__lineEdit.setEnabled(enabled)

    def setValue(self, value: Optional[float]) -> None:
        self._value = value
        self.update_field_value()

    def update_field_value(self) -> None:
        scale = self.__unitGroup.get_scale(self.unit_idx)
        if self._value is None:
            self.__lineEdit.setText("")
        else:
            self.__lineEdit.setText("%s" % (self._value / float(scale)))

        if self._placeholder_value is None:
            self.__lineEdit.setPlaceholderText("")
        else:
            self.__lineEdit.setPlaceholderText("%s" % (self._placeholder_value / float(scale)))

    def text_changed(self) -> None:
        v = self.__lineEdit.text()
        scale = self.__unitGroup.get_scale(self.unit_idx)

        newvalue : Optional[float]
        if v == "":
            newvalue = None
        else:
            newvalue = float(v) * scale

        if newvalue != self._value:
            self._value = newvalue

            self.edited.emit()

    def getValue(self) -> Optional[float]:
        if self._value is not None:
            return self._value
        return self._placeholder_value

    def getValueRaw(self) -> Optional[float]:
        return self._value

    @property
    def suppress_enter(self) -> bool:
        return self.__lineEdit.suppress_enter

    @suppress_enter.setter
    def suppress_enter(self, v: bool) -> None:
        self.__lineEdit.suppress_enter = v
Esempio n. 2
0
class UnitLineEdit(QtGui.QWidget):
    edited = QtCore.Signal()

    def __init__(self, unitGroup, field_type=int):
        super(UnitLineEdit, self).__init__()
        self.unit_idx = 0
        self._value = 0
        self._field_type = float

        self.layout = QtGui.QHBoxLayout()
        self.layout.setContentsMargins(0, 0, 0, 0)

        self.__lineEdit = PLineEdit()
        self.__lineEdit.editingFinished.connect(self.text_changed)

        self.__unitDropDown = QtGui.QComboBox()
        self.__unitDropDown.currentIndexChanged.connect(self.indexChanged)

        self.setLayout(self.layout)
        self.layout.addWidget(self.__lineEdit, 0)
        self.layout.addWidget(self.__unitDropDown, 1)

        self.setUnitGroup(unitGroup)

    def setPlaceholderText(self, text):
        self.__lineEdit.setPlaceholderText(text)

    def resize(self, w, h):
        super(UnitLineEdit, self).resize(w, h)

    def setUnitGroup(self, ug):
        self.__unitGroup = ug
        self.__unitDropDown.clear()
        for k in ug.names:
            self.__unitDropDown.addItem(k)

        self.__unitDropDown.setCurrentIndex(ug.default_index)

    def indexChanged(self, idx):
        self.unit_idx = idx
        self.update_field_value()

    def setUnitName(self, unitname):
        pass

    def getUnitName(self):
        pass

    def setEnabled(self, enabled):
        self.__unitDropDown.setEnabled(enabled)
        self.__lineEdit.setEnabled(enabled)

    def setValue(self, value):
        self._value = value
        self.update_field_value()

    def update_field_value(self):
        if self._value is None:
            self.__lineEdit.setText("")
        else:
            scale = self.__unitGroup.get_scale(self.unit_idx)
            self.__lineEdit.setText("%s" % (self._value / float(scale)))

    def text_changed(self):
        v = self.__lineEdit.text()
        scale = self.__unitGroup.get_scale(self.unit_idx)

        if v == "":
            newvalue = None
        else:
            newvalue = float(v) * scale

        if newvalue != self._value:
            self._value = newvalue
            self.edited.emit()

    def getValue(self):
        return self._value

    @property
    def suppress_enter(self):
        return self.__lineEdit.suppress_enter

    @suppress_enter.setter
    def suppress_enter(self, v):
        self.__lineEdit.suppress_enter = v
Esempio n. 3
0
class UnitLineEdit(QtGui.QWidget):
    edited = QtCore.Signal()

    def __init__(self, unitGroup, field_type=int):
        super(UnitLineEdit, self).__init__()
        self.unit_idx = 0
        self._value = 0
        self._field_type = float

        self.layout = QtGui.QHBoxLayout()
        self.layout.setContentsMargins(0, 0, 0, 0)

        self.__lineEdit = PLineEdit()
        self.__lineEdit.editingFinished.connect(self.text_changed)

        self.__unitDropDown = QtGui.QComboBox()
        self.__unitDropDown.currentIndexChanged.connect(self.indexChanged)

        self._placeholder_value = None

        self.setLayout(self.layout)
        self.layout.addWidget(self.__lineEdit, 0)
        self.layout.addWidget(self.__unitDropDown, 1)

        self.setUnitGroup(unitGroup)

    def setPlaceholderText(self, text):
        self.__lineEdit.setPlaceholderText(text)

    def setPlaceholderValue(self, value):
        self._placeholder_value = value
        self.update_field_value()

    def resize(self, w, h):
        super(UnitLineEdit, self).resize(w, h)

    def setUnitGroup(self, ug):
        self.__unitGroup = ug
        self.__unitDropDown.clear()
        for k in ug.names:
            self.__unitDropDown.addItem(k)

        self.__unitDropDown.setCurrentIndex(ug.default_index)

    def indexChanged(self, idx):
        self.unit_idx = idx
        self.update_field_value()

    def setUnitName(self, unitname):
        pass

    def getUnitName(self):
        pass

    def setEnabled(self, enabled):
        self.__unitDropDown.setEnabled(enabled)
        self.__lineEdit.setEnabled(enabled)

    def setValue(self, value):
        self._value = value
        self.update_field_value()

    def update_field_value(self):
        scale = self.__unitGroup.get_scale(self.unit_idx)
        if self._value is None:
            self.__lineEdit.setText("")
        else:
            self.__lineEdit.setText("%s" % (self._value / float(scale)))

        if self._placeholder_value is None:
            self.__lineEdit.setPlaceholderText("")
        else:
            self.__lineEdit.setPlaceholderText(
                "%s" % (self._placeholder_value / float(scale)))

    def text_changed(self):
        v = self.__lineEdit.text()
        scale = self.__unitGroup.get_scale(self.unit_idx)

        if v == "":
            newvalue = None
        else:
            newvalue = float(v) * scale

        if newvalue != self._value:
            self._value = newvalue
            self.edited.emit()

    def getValue(self):
        return self._value

    @property
    def suppress_enter(self):
        return self.__lineEdit.suppress_enter

    @suppress_enter.setter
    def suppress_enter(self, v):
        self.__lineEdit.suppress_enter = v