class FloatOrEmptyValidator(QValidator): def __init__(self, parent): super().__init__(parent) self.dv = QDoubleValidator(parent) self.dv.setLocale(QLocale.c()) def validate(self, s, pos): if len(s) == 0: return (QValidator.Acceptable, s, pos) if "," in s: return (QValidator.Invalid, s, pos) else: return self.dv.validate(s, pos)
class FloatOrEmptyValidator(QValidator): def __init__(self, parent, allow_empty=False, bottom=float("-inf"), top=float("inf"), default_text=""): super().__init__(parent) self.dv = QDoubleValidator(parent) self.allow_empty = allow_empty self.default_text = default_text self.dv.setLocale(QLocale.c()) self.setBottom(bottom) self.setTop(top) def setDefault(self, s): self.default_text = s def setBottom(self, b): self.dv.setBottom(b) def setTop(self, t): self.dv.setTop(t) def fixup(self, p_str): # = only called at editingFinished so an Orange controlled value can still contain # invalid, because they are synchronized and every change # = called before returnPressedHandler try: f = float(p_str) if f > self.dv.top(): return str(self.dv.top()) if f < self.dv.bottom(): return str(self.dv.bottom()) except ValueError: return self.default_text def validate(self, s, pos): if self.allow_empty and len(s) == 0: return (QValidator.Acceptable, s, pos) if "," in s: return (QValidator.Invalid, s, pos) else: return self.dv.validate(s, pos)
def __init__(self): super().__init__() self.data = None # type: Optional[Orange.data.Table] self.learner = None # type: Optional[Learner] self.default_learner = SimpleTreeLearner(min_instances=10, max_depth=10) self.modified = False self.executor = qconcurrent.ThreadExecutor(self) self.__task = None main_layout = QVBoxLayout() main_layout.setContentsMargins(10, 10, 10, 10) self.controlArea.layout().addLayout(main_layout) box = gui.vBox(None, "Default Method") main_layout.addWidget(box) box_layout = QGridLayout() box_layout.setSpacing(8) box.layout().addLayout(box_layout) button_group = QButtonGroup() button_group.buttonClicked[int].connect(self.set_default_method) for i, (method, _) in enumerate(list(METHODS.items())[1:-1]): imputer = self.create_imputer(method) button = QRadioButton(imputer.name) button.setChecked(method == self.default_method_index) button_group.addButton(button, method) box_layout.addWidget(button, i % 3, i // 3) def set_default_time(datetime): datetime = datetime.toSecsSinceEpoch() if datetime != self.default_time: self.default_time = datetime if self.default_method_index == Method.Default: self._invalidate() hlayout = QHBoxLayout() box.layout().addLayout(hlayout) button = QRadioButton("Fixed values; numeric variables:") button_group.addButton(button, Method.Default) button.setChecked(Method.Default == self.default_method_index) hlayout.addWidget(button) locale = QLocale() locale.setNumberOptions(locale.NumberOption.RejectGroupSeparator) validator = QDoubleValidator() validator.setLocale(locale) self.numeric_value_widget = le = gui.lineEdit( None, self, "default_numeric", validator=validator, alignment=Qt.AlignRight, callback=self._invalidate, enabled=self.default_method_index == Method.Default) hlayout.addWidget(le) hlayout.addWidget(QLabel(", time:")) self.time_widget = gui.DateTimeEditWCalendarTime(self) self.time_widget.setEnabled( self.default_method_index == Method.Default) self.time_widget.setKeyboardTracking(False) self.time_widget.setContentsMargins(0, 0, 0, 0) self.time_widget.set_datetime( QDateTime.fromSecsSinceEpoch(self.default_time)) self.connect_control( "default_time", lambda value: self.time_widget.set_datetime( QDateTime.fromSecsSinceEpoch(value))) self.time_widget.dateTimeChanged.connect(set_default_time) hlayout.addWidget(self.time_widget) self.default_button_group = button_group box = QGroupBox(title=self.tr("Individual Attribute Settings"), flat=False) main_layout.addWidget(box) horizontal_layout = QHBoxLayout(box) main_layout.addWidget(box) self.varview = ListViewSearch( selectionMode=QListView.ExtendedSelection, uniformItemSizes=True) self.varview.setItemDelegate(DisplayFormatDelegate()) self.varmodel = itemmodels.VariableListModel() self.varview.setModel(self.varmodel) self.varview.selectionModel().selectionChanged.connect( self._on_var_selection_changed) self.selection = self.varview.selectionModel() horizontal_layout.addWidget(self.varview) vertical_layout = QVBoxLayout(margin=0) self.methods_container = QWidget(enabled=False) method_layout = QVBoxLayout(margin=0) self.methods_container.setLayout(method_layout) button_group = QButtonGroup() for method in Method: imputer = self.create_imputer(method) button = QRadioButton(text=imputer.name) button_group.addButton(button, method) method_layout.addWidget(button) self.value_combo = QComboBox( minimumContentsLength=8, sizeAdjustPolicy=QComboBox.AdjustToMinimumContentsLength, activated=self._on_value_selected) self.value_double = QDoubleSpinBox( editingFinished=self._on_value_selected, minimum=-1000., maximum=1000., singleStep=.1, decimals=3, ) self.value_stack = value_stack = QStackedWidget() value_stack.addWidget(self.value_combo) value_stack.addWidget(self.value_double) method_layout.addWidget(value_stack) button_group.buttonClicked[int].connect( self.set_method_for_current_selection) self.reset_button = QPushButton( "Restore All to Default", enabled=False, default=False, autoDefault=False, clicked=self.reset_variable_state, ) vertical_layout.addWidget(self.methods_container) vertical_layout.addStretch(2) vertical_layout.addWidget(self.reset_button) horizontal_layout.addLayout(vertical_layout) self.variable_button_group = button_group box = gui.auto_apply(self.controlArea, self, "autocommit") box.button.setFixedWidth(180) box.layout().insertStretch(0) self.info.set_input_summary(self.info.NoInput) self.info.set_output_summary(self.info.NoOutput)