class FloatElement(MeasElement): """Base for elements that are floating point numbers.""" def createWidget(self, parent, client): if self.value is None: self.value = 10.0 self._widget = QLineEdit(parent) self._widget.setValidator(DoubleValidator(parent)) self._widget.setText('%g' % self.value) self._widget.textChanged.connect(self._updateValue) return self._widget def _updateValue(self, text): self.value = float(text.replace(',', '.')) self.changed.emit(self.value)
class TimeEditWidget(QWidget): units = ('s', 'm', 'h', 'd') returnPressed = pyqtSignal() def __init__(self, parent=None): QWidget.__init__(self, parent) self.setLayout(QHBoxLayout()) self.layout().setContentsMargins(0.1, 0.1, 0.1, 0.1) self.layout().setSpacing(1) self.val = QLineEdit() self.val.setValidator(QDoubleValidator(0, 1000000, 2)) self.layout().addWidget(self.val) self.unit = QComboBox() self.unit.insertItems(0, self.units) self.layout().addWidget(self.unit) self.setValue(1) self.val.returnPressed.connect(self.on_returnPressed) self.val.editingFinished.connect(self.on_returnPressed) self.unit.currentIndexChanged.connect(self.recalcValue) def value(self): val = float(self.val.text()) current_unit_index = self.unit.currentIndex() if current_unit_index == 1: # minutes val *= 60. elif current_unit_index == 2: # hours val *= 3600. elif current_unit_index == 3: # days val *= 86400. return int(val) def setValue(self, val): fmt = '%%.%df' % self.val.validator().decimals() self.val.setText(fmt % float(val)) self.unit.setCurrentIndex(0) self.currentUnit = 0 def recalcValue(self, idx): # adjust widget value to unit unit_index_current = self.currentUnit unit_index_next = idx if unit_index_next > unit_index_current: start = unit_index_current end = unit_index_next elif unit_index_next < unit_index_current: start = unit_index_next end = unit_index_current val = float(self.val.text()) factor = 1. for i in range(start, end): if self.units[i + 1] == 'd': factor *= 24. else: factor *= 60. if unit_index_next - unit_index_current > 0: factor = 1. / factor next_value = val * factor self.val.setText('%s' % next_value) self.currentUnit = idx def on_returnPressed(self): self.returnPressed.emit()