Skip to content

TextEditMixin

Base classes

Name Children Inherits
AbstractScrollAreaMixin
prettyqt.widgets.abstractscrollarea

Subclasses

Class Module Description
TextEdit prettyqt.widgets.textedit
TextBrowser prettyqt.widgets.textbrowser

⋔ Inheritance diagram

graph TD
  1473296253936["widgets.TextEditMixin"]
  1473293679456["widgets.AbstractScrollAreaMixin"]
  1473293662864["widgets.FrameMixin"]
  1473293688240["widgets.WidgetMixin"]
  1473299815024["core.ObjectMixin"]
  140713234304496["builtins.object"]
  1473245548480["gui.PaintDeviceMixin"]
  1473293679456 --> 1473296253936
  1473293662864 --> 1473293679456
  1473293688240 --> 1473293662864
  1473299815024 --> 1473293688240
  140713234304496 --> 1473299815024
  1473245548480 --> 1473293688240
  140713234304496 --> 1473245548480

🛈 DocStrings

Bases: AbstractScrollAreaMixin

Source code in prettyqt\widgets\textedit.py
class TextEditMixin(widgets.AbstractScrollAreaMixin):
    value_changed = core.Signal(str)

    def __init__(self, *args, **kwargs) -> None:
        super().__init__(*args, **kwargs)
        self.textChanged.connect(self.on_value_change)
        self.selecter = texteditselecter.TextEditSelecter(self)

    def __add__(self, other: str) -> TextEdit:
        self.append_text(other)
        return self

    def _get_map(self):
        maps = super()._get_map()
        maps |= {
            "autoFormatting": AUTO_FORMATTING,
            "lineWrapMode": LINE_WRAP_MODE,
            "wordWrapMode": gui.textoption.WORD_WRAP_MODE,
        }
        return maps

    def on_value_change(self) -> None:
        self.value_changed.emit(self.text())

    def set_text(self, text: str) -> None:
        self.setPlainText(text)

    def append_text(
        self,
        text: str,
        newline: bool = True,
        ensure_visible: Literal["always", "when_bottom", "never"] = "always",
    ):
        scrollbar = self.verticalScrollBar()
        at_bottom = scrollbar.value() >= (scrollbar.maximum() - 4)
        prev_val = scrollbar.value()
        if newline:
            self.append(text)
        else:
            self.selecter.move_cursor("end")
            self.insertHtml(text)
            self.selecter.move_cursor("end")
        match ensure_visible:
            case "always":
                self.ensureCursorVisible()
            case "when_bottom":
                if at_bottom:
                    self.ensureCursorVisible()
            case "never":
                scrollbar.setValue(prev_val)

    def text(self) -> str:
        return self.toPlainText()

    def set_read_only(self, value: bool = True) -> None:
        self.setReadOnly(value)

    def set_text_color(self, color: datatypes.ColorType) -> None:
        color = colors.get_color(color)
        self.setTextColor(color)

    def set_line_wrap_mode(self, mode: LineWrapModeStr | widgets.QTextEdit.LineWrapMode):
        """Set line wrap mode.

        Args:
            mode: line wrap mode to use
        """
        self.setLineWrapMode(LINE_WRAP_MODE.get_enum_value(mode))

    def get_line_wrap_mode(self) -> LineWrapModeStr:
        """Get the current wrap mode.

        Returns:
            Wrap mode
        """
        return LINE_WRAP_MODE.inverse[self.lineWrapMode()]

    def set_auto_formatting(
        self, mode: AutoFormattingStr | widgets.QTextEdit.AutoFormattingFlag
    ):
        """Set auto formatting mode.

        Args:
            mode: auto formatting mode to use
        """
        self.setAutoFormatting(AUTO_FORMATTING.get_enum_value(mode))

    def get_auto_formatting(self) -> AutoFormattingStr:
        """Get the current auto formatting mode.

        Returns:
            Auto formatting mode
        """
        return AUTO_FORMATTING.inverse[self.autoFormatting()]

    def set_word_wrap_mode(
        self, mode: gui.textoption.WordWrapModeStr | gui.QTextOption.WrapMode
    ):
        """Set word wrap mode.

        Args:
            mode: word wrap mode to use
        """
        self.setWordWrapMode(gui.textoption.WORD_WRAP_MODE.get_enum_value(mode))

    def get_word_wrap_mode(self) -> gui.textoption.WordWrapModeStr:
        """Get the current word wrap mode.

        Returns:
            Word wrap mode
        """
        return gui.textoption.WORD_WRAP_MODE.inverse[self.wordWrapMode()]

get_auto_formatting() -> AutoFormattingStr

Get the current auto formatting mode.

Source code in prettyqt\widgets\textedit.py
def get_auto_formatting(self) -> AutoFormattingStr:
    """Get the current auto formatting mode.

    Returns:
        Auto formatting mode
    """
    return AUTO_FORMATTING.inverse[self.autoFormatting()]

get_line_wrap_mode() -> LineWrapModeStr

Get the current wrap mode.

Source code in prettyqt\widgets\textedit.py
def get_line_wrap_mode(self) -> LineWrapModeStr:
    """Get the current wrap mode.

    Returns:
        Wrap mode
    """
    return LINE_WRAP_MODE.inverse[self.lineWrapMode()]

get_word_wrap_mode() -> gui.textoption.WordWrapModeStr

Get the current word wrap mode.

Source code in prettyqt\widgets\textedit.py
def get_word_wrap_mode(self) -> gui.textoption.WordWrapModeStr:
    """Get the current word wrap mode.

    Returns:
        Word wrap mode
    """
    return gui.textoption.WORD_WRAP_MODE.inverse[self.wordWrapMode()]

set_auto_formatting(mode: AutoFormattingStr | widgets.QTextEdit.AutoFormattingFlag)

Set auto formatting mode.

Parameters:

Name Type Description Default
mode AutoFormattingStr | AutoFormattingFlag

auto formatting mode to use

required
Source code in prettyqt\widgets\textedit.py
def set_auto_formatting(
    self, mode: AutoFormattingStr | widgets.QTextEdit.AutoFormattingFlag
):
    """Set auto formatting mode.

    Args:
        mode: auto formatting mode to use
    """
    self.setAutoFormatting(AUTO_FORMATTING.get_enum_value(mode))

set_line_wrap_mode(mode: LineWrapModeStr | widgets.QTextEdit.LineWrapMode)

Set line wrap mode.

Parameters:

Name Type Description Default
mode LineWrapModeStr | LineWrapMode

line wrap mode to use

required
Source code in prettyqt\widgets\textedit.py
def set_line_wrap_mode(self, mode: LineWrapModeStr | widgets.QTextEdit.LineWrapMode):
    """Set line wrap mode.

    Args:
        mode: line wrap mode to use
    """
    self.setLineWrapMode(LINE_WRAP_MODE.get_enum_value(mode))

set_word_wrap_mode(mode: gui.textoption.WordWrapModeStr | gui.QTextOption.WrapMode)

Set word wrap mode.

Parameters:

Name Type Description Default
mode WordWrapModeStr | WrapMode

word wrap mode to use

required
Source code in prettyqt\widgets\textedit.py
def set_word_wrap_mode(
    self, mode: gui.textoption.WordWrapModeStr | gui.QTextOption.WrapMode
):
    """Set word wrap mode.

    Args:
        mode: word wrap mode to use
    """
    self.setWordWrapMode(gui.textoption.WORD_WRAP_MODE.get_enum_value(mode))