src/eric7/EricWidgets/EricAutoResizeTextBrowser.py

branch
eric7
changeset 10920
8a1d447323a2
child 11090
f5f5f5803935
equal deleted inserted replaced
10919:4e4c8ee38c45 10920:8a1d447323a2
1 # -*- coding: utf-8 -*-
2
3 # Copyright (c) 2024 Detlev Offenbach <detlev@die-offenbachs.de>
4 #
5
6 """
7 Module implementing a QTextBrowser widget that resizes automatically.
8 """
9
10 from PyQt6.QtCore import Qt
11 from PyQt6.QtWidgets import QFrame, QSizePolicy, QTextBrowser
12
13
14 class EricAutoResizeTextBrowser(QTextBrowser):
15 """
16 Class implementing a QTextBrowser widget that adjusts its size automatically to the
17 contained text.
18 """
19
20 def __init__(self, parent=None):
21 """
22 Constructor
23
24 @param parent reference to the parent widget (defaults to None)
25 @type QWidget (optional)
26 """
27 super().__init__(parent=parent)
28
29 self.setSizePolicy(QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Preferred)
30 self.setHorizontalScrollBarPolicy(Qt.ScrollBarPolicy.ScrollBarAlwaysOff)
31 self.setVerticalScrollBarPolicy(Qt.ScrollBarPolicy.ScrollBarAlwaysOff)
32 self.setFrameShape(QFrame.Shape.NoFrame)
33
34 self.textChanged.connect(self.updateGeometry)
35
36 def resizeEvent(self, evt):
37 """
38 Protected method to handle resize events.
39
40 @param evt reference to the resize event
41 @type QResizeEvent
42 """
43 super().resizeEvent(evt)
44 self.updateGeometry()
45
46 def updateGeometry(self):
47 """
48 Public method to update the geometry depending on the current text.
49 """
50 # Set the text width of the document to match the width of the text browser.
51 self.document().setTextWidth(
52 self.width() - 2 * int(self.document().documentMargin())
53 )
54
55 # Get the document height and set it as the fixed height of the text browser.
56 docHeight = self.document().size().height()
57 self.setFixedHeight(int(docHeight))
58
59 # Call the base class updateGeometry() method.
60 super().updateGeometry()

eric ide

mercurial