def test_object(qapp): obj = core.Object() obj.set_id("test") with open("data.pkl", "wb") as jar: pickle.dump(obj, jar) with open("data.pkl", "rb") as jar: obj = pickle.load(jar) assert obj.get_id() == "test" w = widgets.Splitter("horizontal") w1 = widgets.PushButton() w1.set_id("w1") w2 = widgets.PlainTextEdit() w2.set_id("w2") w3 = widgets.MainWindow() w3.set_id("w3") w4 = widgets.TableView() w4.set_id("w4") w.add(w1, w2, w3, w4) assert w.find_children(widgets.PushButton, recursive=False) == [w1] assert w.find_children(core.Object, name="w2", recursive=False) == [w2] assert w.find_child(widgets.PlainTextEdit, recursive=True) == w2 assert w.find_child(core.Object, name="w2", recursive=False) == w2 assert w2.find_parent(widgets.Splitter) == w layout = widgets.BoxLayout("vertical") layout.add(w)
def test_sidebarwidget(qtbot): ex = custom_widgets.SidebarWidget(show_settings=True) page_1 = widgets.PlainTextEdit() page_2 = widgets.ColorDialog() page_3 = widgets.FileDialog() ex.add_tab(page_1, "Text", "mdi.timer") ex.add_tab(page_2, "Color", "mdi.format-color-fill", area="bottom") ex.add_tab(page_3, "Help", "mdi.help-circle-outline") ex.set_marker(page_3) ex.set_tab(page_2)
def test_plaintextedit(): widget = widgets.PlainTextEdit() widget.set_text("hallo") widget.set_disabled() widget.set_enabled() widget.set_font("Consolas") widget.append_text(" test") assert widget.text() == "hallo\n test" widget.highlight_current_line() widget.set_read_only() widget.get_result_widget() widget.scroll_to_bottom() widget.set_value("test") assert widget.get_value() == "test" with open("data.pkl", "wb") as jar: pickle.dump(widget, jar) with open("data.pkl", "rb") as jar: widget = pickle.load(jar)
def __init__(self, obj, name: str = ""): super().__init__() self.set_title("Object browser") self._instance_nr = self._add_instance() self.set_icon("mdi.language-python") self._attr_cols = DEFAULT_ATTR_COLS self._attr_details = DEFAULT_ATTR_DETAILS logger.debug("Reading model settings for window: %d", self._instance_nr) with core.Settings( settings_id=self._settings_group_name("model")) as settings: self._auto_refresh = settings.get("auto_refresh", False) self._refresh_rate = settings.get("refresh_rate", 2) show_callable_attrs = settings.get("show_callable_attrs", True) show_special_attrs = settings.get("show_special_attrs", True) self._tree_model = objectbrowsertreemodel.ObjectBrowserTreeModel( obj, name, attr_cols=self._attr_cols) self._proxy_tree_model = objectbrowsertreemodel.ObjectBrowserTreeProxyModel( show_callable_attrs=show_callable_attrs, show_special_attrs=show_special_attrs, ) self._proxy_tree_model.setSourceModel(self._tree_model) # self._proxy_tree_model.setSortRole(RegistryTableModel.SORT_ROLE) self._proxy_tree_model.setDynamicSortFilter(True) # self._proxy_tree_model.setSortCaseSensitivity(Qt.CaseInsensitive) # Views self._setup_actions() self.central_splitter = widgets.Splitter( parent=self, orientation=constants.VERTICAL) self.setCentralWidget(self.central_splitter) # Tree widget self.obj_tree = widgets.TreeView() self.obj_tree.setRootIsDecorated(True) self.obj_tree.setAlternatingRowColors(True) self.obj_tree.set_model(self._proxy_tree_model) self.obj_tree.set_selection_behaviour("rows") self.obj_tree.setUniformRowHeights(True) self.obj_tree.setAnimated(True) # Stretch last column? # It doesn't play nice when columns are hidden and then shown again. self.obj_tree.h_header.set_id("table_header") self.obj_tree.h_header.setSectionsMovable(True) self.obj_tree.h_header.setStretchLastSection(False) self.central_splitter.addWidget(self.obj_tree) # Bottom pane bottom_pane_widget = widgets.Widget() bottom_pane_widget.set_layout("horizontal", spacing=0, margin=5) self.central_splitter.addWidget(bottom_pane_widget) group_box = widgets.GroupBox("Details") bottom_pane_widget.box.addWidget(group_box) group_box.set_layout("horizontal", margin=2) # Radio buttons radio_widget = widgets.Widget() radio_widget.set_layout("vertical", margin=0) self.button_group = widgets.ButtonGroup(self) for button_id, attr_detail in enumerate(self._attr_details): radio_button = widgets.RadioButton(attr_detail.name) radio_widget.box.addWidget(radio_button) self.button_group.addButton(radio_button, button_id) self.button_group.buttonClicked.connect(self._change_details_field) self.button_group.button(0).setChecked(True) radio_widget.box.addStretch(1) group_box.box.addWidget(radio_widget) # Editor widget font = gui.Font("Courier") font.setFixedPitch(True) # font.setPointSize(14) self.editor = widgets.PlainTextEdit() self.editor.setReadOnly(True) self.editor.setFont(font) group_box.box.addWidget(self.editor) # Splitter parameters self.central_splitter.setCollapsible(0, False) self.central_splitter.setCollapsible(1, True) self.central_splitter.setSizes([400, 200]) self.central_splitter.setStretchFactor(0, 10) self.central_splitter.setStretchFactor(1, 0) selection_model = self.obj_tree.selectionModel() selection_model.currentChanged.connect(self._update_details) menubar = self.menuBar() file_menu = menubar.add_menu("&File") file_menu.addAction("C&lose", self.close, "Ctrl+W") file_menu.addAction("E&xit", lambda: widgets.app().closeAllWindows(), "Ctrl+Q") view_menu = menubar.add_menu("&View") view_menu.addAction("&Refresh", self._tree_model.refresh_tree, "Ctrl+R") view_menu.addAction(self.toggle_auto_refresh_action) view_menu.addSeparator() self.show_cols_submenu = widgets.Menu("Table columns") view_menu.add_menu(self.show_cols_submenu) actions = self.obj_tree.h_header.get_header_actions() self.show_cols_submenu.add_actions(actions) view_menu.addSeparator() view_menu.addAction(self.toggle_callable_action) view_menu.addAction(self.toggle_special_attribute_action) assert self._refresh_rate > 0 self._refresh_timer = core.Timer(self) self._refresh_timer.setInterval(self._refresh_rate * 1000) self._refresh_timer.timeout.connect(self._tree_model.refresh_tree) # Update views with model self.toggle_special_attribute_action.setChecked(show_special_attrs) self.toggle_callable_action.setChecked(show_callable_attrs) self.toggle_auto_refresh_action.setChecked(self._auto_refresh) # Select first row so that a hidden root node will not be selected. first_row_index = self._proxy_tree_model.first_item_index() self.obj_tree.setCurrentIndex(first_row_index) if self._tree_model.inspected_node_is_visible: self.obj_tree.expand(first_row_index)
return start = match.capturedStart() add = match.capturedLength() # As long as there's a delimiter match on this line... while start >= 0: # Look for the ending delimiter match = delimiter.match(text, start + add) end = match.capturedStart() # Ending delimiter on this line? if end >= add: length = end + match.capturedLength() self.setCurrentBlockState(0) # No; multi-line string else: self.setCurrentBlockState(1) length = len(text) self.setFormat(start, length - start + add, style) # Look for the next match start = delimiter.match(text, start + length).capturedStart() if __name__ == "__main__": from prettyqt import widgets app = widgets.app() editor = widgets.PlainTextEdit() highlighter = MarkdownHighlighter(editor.document()) editor.show() app.main_loop()
def __init__( self, title: str = "Regex Editor", regex: str = "", teststring: str = "", parent: QtWidgets.QWidget | None = None, ): super().__init__(parent) self.resize(1200, 800) self.set_title(title) self.set_icon("mdi.regex") self.set_layout("horizontal") self.left_layout = widgets.BoxLayout("vertical") self.right_layout = widgets.BoxLayout("vertical") self.prog: Pattern | None = None self.matches: list[Match] = list() self.groupbox = widgets.GroupBox(title="Regular expression") self.grid = widgets.GridLayout(self.groupbox) self.layout_toprow = widgets.BoxLayout("horizontal") self.regexinput = custom_widgets.RegexInput() self.regexinput.set_min_size(400, 0) self.layout_toprow.add(self.regexinput) self.grid.add(self.layout_toprow, 1, 0) self.left_layout.add(self.groupbox) self.groupbox_teststring = widgets.GroupBox(title="Test strings") self.groupbox_teststring.set_layout("grid") self.textedit_teststring = widgets.PlainTextEdit(teststring) self.textedit_teststring.set_min_size(400, 0) self.groupbox_teststring.box.add(self.textedit_teststring, 0, 0) self.label_num_matches = widgets.Label("No match") self.label_num_matches.set_alignment("center") self.left_layout.add(self.groupbox_teststring) self.groupbox_sub = widgets.GroupBox(title="Substitution", checkable=True) self.layout_sub = widgets.GridLayout(self.groupbox_sub) self.lineedit_sub = widgets.LineEdit() self.lineedit_sub.textChanged.connect(self.update_sub_textedit) self.textedit_sub = widgets.PlainTextEdit() self.textedit_sub.set_min_size(400, 0) self.textedit_sub.set_read_only() self.layout_sub.add(self.lineedit_sub, 0, 0) self.layout_sub.add(self.textedit_sub, 1, 0) self.left_layout.add(self.groupbox_sub) self.cb_quickref = widgets.CheckBox("Show Regular Expression Quick Reference") self.left_layout.add(self.cb_quickref) self.table_matches = widgets.TableView() self.table_matches.setup_list_style() self.box.add(self.left_layout) self.box.add(self.right_layout) self.right_layout.add(self.label_num_matches) self.right_layout.add(self.table_matches) model = custom_models.RegexMatchesModel() self.table_matches.set_model(model) self.table_matches.setColumnWidth(0, 60) self.table_matches.setColumnWidth(1, 60) self.groupbox_sub.toggled.connect(self.lineedit_sub.setVisible) self.groupbox_sub.toggled.connect(self.textedit_sub.setVisible) doc = self.textedit_teststring.document() self._highlighter = RegexMatchHighlighter(doc) self._highlighter.rehighlight() self.cb_quickref.stateChanged.connect(self.quick_ref_requested) self.regexinput.value_changed.connect(self._update_view) self.textedit_teststring.textChanged.connect(self._update_view) self.regexinput.pattern = regex self._update_view()
self, start_pos: int, end_pos: MoveOperationStr | int, to_replace: str, ): self.set_position(start_pos) if isinstance(end_pos, int): self.set_position(end_pos, mode="keep") else: self.move_position(end_pos, mode="keep") self.insertText(to_replace) self.select_text(start_pos, start_pos + len(to_replace)) @contextlib.contextmanager def edit_block(self): """Context manager for edit blocks. Can be used for undo actions.""" self.beginEditBlock() yield self.endEditBlock() if __name__ == "__main__": from prettyqt import widgets app = widgets.app() widget = widgets.PlainTextEdit() widget.show() app.main_loop() cursor = widget.get_text_cursor() print(str(cursor))
# callback=callback, # checkable=checkable) act = widgets.Action( text=title, icon=icon, shortcut=shortcut, checkable=checkable, callback=callback, ) if area == "top": self.sidebar.insertAction(self.spacer_action, act) if area == "bottom": self.sidebar.addAction(act) button = self.sidebar.widgetForAction(act) button.setFixedWidth(self.BUTTON_WIDTH) return act if __name__ == "__main__": app = widgets.app() ex = SidebarWidget(show_settings=True) page_1 = widgets.PlainTextEdit() page_2 = widgets.ColorDialog() page_3 = widgets.FileDialog() ex.add_tab(page_1, "Text", "mdi.timer") ex.add_tab(page_2, "Color", "mdi.format-color-fill", area="bottom") ex.add_tab(page_3, "Help", "mdi.help-circle-outline") ex.set_marker(page_3) ex.show() app.main_loop()
def test_plaintextedit(): widget = widgets.PlainTextEdit() widget.set_text("hallo") widget.show()