def _new_text_input(self, c: TextInputComponent) -> Tuple[QLabel, QLineEdit]: view = QLineEditObserver( ) if c.type == TextInputType.SINGLE_LINE else QPlainTextEditObserver() view.setSizePolicy(QSizePolicy.MinimumExpanding, QSizePolicy.Fixed) if c.id: view.setObjectName(c.id) if c.min_width >= 0: view.setMinimumWidth(int(c.min_width)) if c.min_height >= 0: view.setMinimumHeight(int(c.min_height)) if c.only_int: view.setValidator(QIntValidator()) if c.tooltip: view.setToolTip(c.tooltip) if c.placeholder: view.setPlaceholderText(c.placeholder) if c.value is not None: view.setText(str(c.value)) view.setCursorPosition(0) if c.read_only: view.setEnabled(False) def update_model(text: str): c.set_value(val=text, caller=view) view.textChanged.connect(update_model) c.observers.append(view) label = QWidget() label.setSizePolicy(QSizePolicy.Minimum, QSizePolicy.Minimum) label.setLayout(QHBoxLayout()) label_component = QLabel() label.layout().addWidget(label_component) if label: label_component.setText(c.get_label()) if c.tooltip: label.layout().addWidget(self.gen_tip_icon(c.tooltip)) return label, self._wrap(view, c)
def __init__(self, model: TextInputComponent): super(TextInputQt, self).__init__() self.model = model self.setLayout(QGridLayout()) self.setStyleSheet('QGridLayout {margin-left: 0} QLabel { font-weight: bold}') self.layout().addWidget(QLabel(model.get_label()), 0, 0) if self.model.max_width > 0: self.setMaximumWidth(self.model.max_width) self.text_input = QLineEditObserver() if model.type == TextInputType.SINGLE_LINE else QPlainTextEditObserver() if model.only_int: self.text_input.setValidator(QIntValidator()) if model.placeholder: self.text_input.setPlaceholderText(model.placeholder) if model.min_width >= 0: self.text_input.setMinimumWidth(model.min_width) if model.min_height >= 0: self.text_input.setMinimumHeight(model.min_height) if model.tooltip: self.text_input.setToolTip(model.tooltip) if model.value: self.text_input.setText(model.value) self.text_input.setCursorPosition(0) self.text_input.textChanged.connect(self._update_model) self.model.observers.append(self.text_input) self.layout().addWidget(self.text_input, 0, 1)