OllamaInterface/AutoResizeTextBrowser.py

Mon, 07 Apr 2025 18:22:30 +0200

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Mon, 07 Apr 2025 18:22:30 +0200
changeset 69
eb9340034f26
parent 67
3c2bcbf7eeaf
permissions
-rw-r--r--

Created global tag <release-10.1.8>.

# -*- coding: utf-8 -*-

# Copyright (c) 2024 - 2025 Detlev Offenbach <detlev@die-offenbachs.de>
#

"""
Module implementing a QTextBrowser widget that resizes automatically.
"""

# backward compatibility for eric < 24.10

from PyQt6.QtCore import Qt
from PyQt6.QtWidgets import QFrame, QSizePolicy, QTextBrowser


class AutoResizeTextBrowser(QTextBrowser):
    """
    Class implementing a QTextBrowser widget that resizes automatically.
    """

    def __init__(self, parent=None):
        """
        Constructor

        @param parent reference to the parent widget (defaults to None)
        @type QWidget (optional)
        """
        super().__init__(parent=parent)

        self.setSizePolicy(QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Preferred)
        self.setHorizontalScrollBarPolicy(Qt.ScrollBarPolicy.ScrollBarAlwaysOff)
        self.setVerticalScrollBarPolicy(Qt.ScrollBarPolicy.ScrollBarAlwaysOff)
        self.setFrameShape(QFrame.Shape.NoFrame)

        self.textChanged.connect(self.updateGeometry)

    def resizeEvent(self, evt):
        """
        Protected method to handle resize events.

        @param evt reference to the resize event
        @type QResizeEvent
        """
        super().resizeEvent(evt)
        self.updateGeometry()

    def updateGeometry(self):
        """
        Public method to update the geometry depending on the current text.
        """
        # Set the text width of the document to match the width of the text browser.
        self.document().setTextWidth(
            self.width() - 2 * int(self.document().documentMargin())
        )

        # Get the document height and set it as the fixed height of the text browser.
        docHeight = self.document().size().height()
        self.setFixedHeight(int(docHeight))

        # Call the base class updateGeometry() method.
        super().updateGeometry()

eric ide

mercurial