class IntOrEmptyValidator(QValidator): def __init__(self, parent): super().__init__(parent) self.dv = QIntValidator(parent) self.dv.setLocale(QLocale.c()) def validate(self, s, pos): if len(s) == 0: return (QValidator.Acceptable, s, pos) else: return self.dv.validate(s, pos)
def __init__(self): super().__init__() self.corpus = None # GUI box = gui.widgetBox(self.controlArea, "Info") self.info = gui.widgetLabel( box, 'Connect an input corpus to start querying') self.import_box = gui.vBox(self.controlArea, 'Dictionary') self.import_box.setVisible(False) gui.button(self.import_box, self, 'Use dictionary', toggleButton=True, value='dictionary_on', buttonType=QCheckBox) self.dictionarybox = ListEdit(self, 'dictionary_text', '', 60, self) self.dictionarybox.setTextColor(QColor(100, 100, 100)) self.dictionarybox.setReadOnly(True) self.import_box.layout().addWidget(self.dictionarybox) query_box = gui.widgetBox(self.controlArea, 'Query', addSpace=True) self.querytextbox = ListEdit(self, 'queries', '', 80, self) query_box.layout().addWidget(self.querytextbox) query_parameter_box = gui.hBox(self.controlArea, self) gui.radioButtonsInBox(query_parameter_box, self, 'query_mode', btnLabels=QUERY_MODES, box="Query mode", callback=self.toggle_mode) self.count_mode_parameters = gui.vBox(query_parameter_box, self) gui.checkBox(self.count_mode_parameters, self, 'include_unmatched', label="Include unmatched documents") self.filter_mode_parameters = gui.widgetBox(query_parameter_box, self) gui.lineEdit(self.filter_mode_parameters, self, "context_window", 'Output words in context window', validator=QIntValidator()) self.toggle_mode() info_box = gui.hBox(self.controlArea, 'Status') self.status = 'Waiting for input' gui.label(info_box, self, '%(status)s') self.search_button = gui.button(self.controlArea, self, 'Search', self.start_stop, focusPolicy=Qt.NoFocus)
def __init__(self, parent, allow_empty=False, bottom=-2147483647, top=2147483647, default_text=""): self.dv = QIntValidator(parent) self.valid_type = int super().__init__(parent, allow_empty=allow_empty, bottom=bottom, top=top, default_text=default_text)
def __init__(self): super().__init__() self.data = None # type: Optional[Table] self.clusterings = {} self.__executor = ThreadExecutor(parent=self) self.__task = None # type: Optional[Task] layout = QGridLayout() bg = gui.radioButtonsInBox( self.controlArea, self, "optimize_k", orientation=layout, box="Number of Clusters", callback=self.update_method, ) layout.addWidget( gui.appendRadioButton(bg, "Fixed:", addToLayout=False), 1, 1) sb = gui.hBox(None, margin=0) gui.spin(sb, self, "k", minv=2, maxv=30, controlWidth=60, alignment=Qt.AlignRight, callback=self.update_k) gui.rubber(sb) layout.addWidget(sb, 1, 2) layout.addWidget(gui.appendRadioButton(bg, "From", addToLayout=False), 2, 1) ftobox = gui.hBox(None) ftobox.layout().setContentsMargins(0, 0, 0, 0) layout.addWidget(ftobox, 2, 2) gui.spin(ftobox, self, "k_from", minv=2, maxv=29, controlWidth=60, alignment=Qt.AlignRight, callback=self.update_from) gui.widgetLabel(ftobox, "to") gui.spin(ftobox, self, "k_to", minv=3, maxv=30, controlWidth=60, alignment=Qt.AlignRight, callback=self.update_to) gui.rubber(ftobox) box = gui.vBox(self.controlArea, "Initialization") gui.comboBox(box, self, "smart_init", items=[m[0] for m in self.INIT_METHODS], callback=self.invalidate) layout = QGridLayout() gui.widgetBox(box, orientation=layout) layout.addWidget(gui.widgetLabel(None, "Re-runs: "), 0, 0, Qt.AlignLeft) sb = gui.hBox(None, margin=0) layout.addWidget(sb, 0, 1) gui.lineEdit(sb, self, "n_init", controlWidth=60, valueType=int, validator=QIntValidator(), callback=self.invalidate) layout.addWidget(gui.widgetLabel(None, "Maximum iterations: "), 1, 0, Qt.AlignLeft) sb = gui.hBox(None, margin=0) layout.addWidget(sb, 1, 1) gui.lineEdit(sb, self, "max_iterations", controlWidth=60, valueType=int, validator=QIntValidator(), callback=self.invalidate) self.apply_button = gui.auto_commit(self.buttonsArea, self, "auto_commit", "Apply", box=None, commit=self.commit) gui.rubber(self.controlArea) box = gui.vBox(self.mainArea, box="Silhouette Scores") self.mainArea.setVisible(self.optimize_k) self.table_model = ClusterTableModel(self) table = self.table_view = QTableView(self.mainArea) table.setModel(self.table_model) table.setSelectionMode(QTableView.SingleSelection) table.setSelectionBehavior(QTableView.SelectRows) table.setItemDelegate(gui.ColoredBarItemDelegate(self, color=Qt.cyan)) table.selectionModel().selectionChanged.connect(self.select_row) table.setMaximumWidth(200) table.horizontalHeader().setStretchLastSection(True) table.horizontalHeader().hide() table.setShowGrid(False) box.layout().addWidget(table)
def __init__(self, parent): super().__init__(parent) self.dv = QIntValidator(parent) self.dv.setLocale(QLocale.c())
class pos_int(int): # pylint: disable=invalid-name validator = QIntValidator()
def __init__(self): super().__init__() self.data = None self.km = None self.optimization_runs = [] box = gui.vBox(self.controlArea, "Number of Clusters") layout = QGridLayout() self.n_clusters = bg = gui.radioButtonsInBox(box, self, "optimize_k", [], orientation=layout, callback=self.update) layout.addWidget( gui.appendRadioButton(bg, "Fixed:", addToLayout=False), 1, 1) sb = gui.hBox(None, margin=0) self.fixedSpinBox = gui.spin(sb, self, "k", minv=2, maxv=30, controlWidth=60, alignment=Qt.AlignRight, callback=self.update_k) gui.rubber(sb) layout.addWidget(sb, 1, 2) layout.addWidget( gui.appendRadioButton(bg, "Optimized from", addToLayout=False), 2, 1) ftobox = gui.hBox(None) ftobox.layout().setContentsMargins(0, 0, 0, 0) layout.addWidget(ftobox) gui.spin(ftobox, self, "k_from", minv=2, maxv=29, controlWidth=60, alignment=Qt.AlignRight, callback=self.update_from) gui.widgetLabel(ftobox, "to") self.fixedSpinBox = gui.spin(ftobox, self, "k_to", minv=3, maxv=30, controlWidth=60, alignment=Qt.AlignRight, callback=self.update_to) gui.rubber(ftobox) layout.addWidget(gui.widgetLabel(None, "Scoring: "), 5, 1, Qt.AlignRight) layout.addWidget( gui.comboBox(None, self, "scoring", label="Scoring", items=list(zip(*self.SCORING_METHODS))[0], callback=self.update), 5, 2) box = gui.vBox(self.controlArea, "Initialization") gui.comboBox(box, self, "smart_init", items=self.INIT_METHODS, callback=self.update) layout = QGridLayout() box2 = gui.widgetBox(box, orientation=layout) box2.setSizePolicy(QSizePolicy.Maximum, QSizePolicy.Maximum) layout.addWidget(gui.widgetLabel(None, "Re-runs: "), 0, 0, Qt.AlignLeft) sb = gui.hBox(None, margin=0) layout.addWidget(sb, 0, 1) gui.lineEdit(sb, self, "n_init", controlWidth=60, valueType=int, validator=QIntValidator(), callback=self.update) layout.addWidget(gui.widgetLabel(None, "Maximal iterations: "), 1, 0, Qt.AlignLeft) sb = gui.hBox(None, margin=0) layout.addWidget(sb, 1, 1) gui.lineEdit(sb, self, "max_iterations", controlWidth=60, valueType=int, validator=QIntValidator(), callback=self.update) box = gui.vBox(self.controlArea, "Output") gui.comboBox(box, self, "place_cluster_ids", label="Append cluster ID as:", orientation=Qt.Horizontal, callback=self.send_data, items=self.OUTPUT_METHODS) gui.lineEdit(box, self, "output_name", label="Name:", orientation=Qt.Horizontal, callback=self.send_data) gui.separator(self.buttonsArea, 30) self.apply_button = gui.auto_commit(self.buttonsArea, self, "auto_run", "Apply", box=None, commit=self.commit) gui.rubber(self.controlArea) self.table_model = QStandardItemModel(self) self.table_model.setHorizontalHeaderLabels(["k", "Score"]) self.table_model.setColumnCount(2) self.table_box = gui.vBox(self.mainArea, "Optimization Report", addSpace=0) table = self.table_view = QTableView(self.table_box) table.setHorizontalScrollMode(QTableView.ScrollPerPixel) table.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff) table.setSelectionMode(QTableView.SingleSelection) table.setSelectionBehavior(QTableView.SelectRows) table.verticalHeader().hide() self.bar_delegate = gui.ColoredBarItemDelegate(self, color=Qt.cyan) table.setItemDelegateForColumn(1, self.bar_delegate) table.setModel(self.table_model) table.selectionModel().selectionChanged.connect( self.table_item_selected) table.setColumnWidth(0, 40) table.setColumnWidth(1, 120) table.horizontalHeader().setStretchLastSection(True) self.setSizePolicy(QSizePolicy.Preferred, QSizePolicy.Preferred) self.mainArea.setSizePolicy(QSizePolicy.Maximum, QSizePolicy.Preferred) self.table_box.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.MinimumExpanding) self.table_view.setSizePolicy(QSizePolicy.Preferred, QSizePolicy.MinimumExpanding) self.table_box.layout().addWidget(self.table_view) self.hide_show_opt_results()
def _create_buttons(self, box): """Create radio buttons""" def intspin(): s = QSpinBox(self) s.setMinimum(2) s.setMaximum(10) s.setFixedWidth(60) s.setAlignment(Qt.AlignRight) s.setContentsMargins(0, 0, 0, 0) return s, s.valueChanged def widthline(validator): s = QLineEdit(self) s.setFixedWidth(60) s.setAlignment(Qt.AlignRight) s.setValidator(validator) s.setContentsMargins(0, 0, 0, 0) return s, s.textChanged def manual_cut_editline(text="", enabled=True) -> QLineEdit: edit = QLineEdit( text=text, placeholderText="e.g. 0.0, 0.5, 1.0", toolTip='<p style="white-space:pre">' + 'Enter cut points as a comma-separate list of \n' 'strictly increasing numbers e.g. 0.0, 0.5, 1.0).</p>', enabled=enabled, ) edit.setValidator(IncreasingNumbersListValidator()) edit.setSizePolicy(QSizePolicy.MinimumExpanding, QSizePolicy.Fixed) @edit.textChanged.connect def update(): validator = edit.validator() if validator is not None and edit.text().strip(): state, _, _ = validator.validate(edit.text(), 0) else: state = QValidator.Acceptable palette = edit.palette() colors = { QValidator.Intermediate: (Qt.yellow, Qt.black), QValidator.Invalid: (Qt.red, Qt.black), }.get(state, None) if colors is None: palette = QPalette() else: palette.setColor(QPalette.Base, colors[0]) palette.setColor(QPalette.Text, colors[1]) cr = edit.cursorRect() p = edit.mapToGlobal(cr.bottomRight()) edit.setPalette(palette) if state != QValidator.Acceptable and edit.isVisible(): validator.show_tip(edit, p, edit.toolTip(), textFormat=Qt.RichText) else: validator.show_tip(edit, p, "") return edit, edit.textChanged children = [] def button(id_, *controls, stretch=True): layout = QHBoxLayout() desc = Options[id_] button = QRadioButton(desc.label) button.setToolTip(desc.tooltip) self.button_group.addButton(button, id_) layout.addWidget(button) if controls: if stretch: layout.addStretch(1) for c, signal in controls: layout.addWidget(c) if signal is not None: @signal.connect def arg_changed(): self.button_group.button(id_).setChecked(True) self.update_hints(id_) children.append(layout) button_box.layout().addLayout(layout) return (*controls, (None, ))[0][0] button_box = gui.vBox(box) button_box.layout().setSpacing(0) button_box.setSizePolicy(QSizePolicy(QSizePolicy.Fixed, QSizePolicy.Preferred)) self.button_group = QButtonGroup(self) self.button_group.idClicked.connect(self.update_hints) button(Methods.Keep) button(Methods.Remove) self.binning_spin = button(Methods.Binning, intspin()) validator = QDoubleValidator() validator.setBottom(0) self.width_line = button(Methods.FixedWidth, widthline(validator)) self.width_time_unit = u = QComboBox(self) u.setContentsMargins(0, 0, 0, 0) u.addItems([unit + "(s)" for unit in time_units]) validator = QIntValidator() validator.setBottom(1) self.width_time_line = button(Methods.FixedWidthTime, widthline(validator), (u, u.currentTextChanged)) self.freq_spin = button(Methods.EqualFreq, intspin()) self.width_spin = button(Methods.EqualWidth, intspin()) button(Methods.MDL) self.copy_to_custom = FixedSizeButton( text="CC", toolTip="Copy the current cut points to manual mode") self.copy_to_custom.clicked.connect(self._copy_to_manual) self.threshold_line = button(Methods.Custom, manual_cut_editline(), (self.copy_to_custom, None), stretch=False) button(Methods.Default) maxheight = max(w.sizeHint().height() for w in children) for w in children: w.itemAt(0).widget().setFixedHeight(maxheight) button_box.layout().addStretch(1)