Added a specialized QTextBrowser subclass that adjusts its size to the contained text automatically (no scrollbars shown). eric7

Mon, 16 Sep 2024 13:20:17 +0200

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Mon, 16 Sep 2024 13:20:17 +0200
branch
eric7
changeset 10920
8a1d447323a2
parent 10919
4e4c8ee38c45
child 10921
495f084a737e

Added a specialized QTextBrowser subclass that adjusts its size to the contained text automatically (no scrollbars shown).

eric7.epj file | annotate | diff | comparison | revisions
src/eric7/APIs/Python3/eric7.api file | annotate | diff | comparison | revisions
src/eric7/APIs/Python3/eric7.bas file | annotate | diff | comparison | revisions
src/eric7/Documentation/Help/source.qch file | annotate | diff | comparison | revisions
src/eric7/Documentation/Help/source.qhp file | annotate | diff | comparison | revisions
src/eric7/Documentation/Source/eric7.EricWidgets.EricAutoResizeTextBrowser.html file | annotate | diff | comparison | revisions
src/eric7/Documentation/Source/index-eric7.EricWidgets.html file | annotate | diff | comparison | revisions
src/eric7/EricWidgets/EricAutoResizeTextBrowser.py file | annotate | diff | comparison | revisions
--- a/eric7.epj	Sun Sep 15 16:56:58 2024 +0200
+++ b/eric7.epj	Mon Sep 16 13:20:17 2024 +0200
@@ -1220,6 +1220,7 @@
       "src/eric7/EricWidgets/EricAnimatedLabel.py",
       "src/eric7/EricWidgets/EricAnimatedWidget.py",
       "src/eric7/EricWidgets/EricApplication.py",
+      "src/eric7/EricWidgets/EricAutoResizeTextBrowser.py",
       "src/eric7/EricWidgets/EricClickableLabel.py",
       "src/eric7/EricWidgets/EricComboSelectionDialog.py",
       "src/eric7/EricWidgets/EricCompleters.py",
--- a/src/eric7/APIs/Python3/eric7.api	Sun Sep 15 16:56:58 2024 +0200
+++ b/src/eric7/APIs/Python3/eric7.api	Mon Sep 16 13:20:17 2024 +0200
@@ -1407,6 +1407,9 @@
 eric7.EricWidgets.EricApplication.EricApplication.usesSmallScreen?4()
 eric7.EricWidgets.EricApplication.EricApplication?1(args)
 eric7.EricWidgets.EricApplication.ericApp?7
+eric7.EricWidgets.EricAutoResizeTextBrowser.EricAutoResizeTextBrowser.resizeEvent?4(evt)
+eric7.EricWidgets.EricAutoResizeTextBrowser.EricAutoResizeTextBrowser.updateGeometry?4()
+eric7.EricWidgets.EricAutoResizeTextBrowser.EricAutoResizeTextBrowser?1(parent=None)
 eric7.EricWidgets.EricClickableLabel.EricClickableLabel.clicked?7
 eric7.EricWidgets.EricClickableLabel.EricClickableLabel.middleClicked?7
 eric7.EricWidgets.EricClickableLabel.EricClickableLabel.mouseReleaseEvent?4(evt)
--- a/src/eric7/APIs/Python3/eric7.bas	Sun Sep 15 16:56:58 2024 +0200
+++ b/src/eric7/APIs/Python3/eric7.bas	Mon Sep 16 13:20:17 2024 +0200
@@ -243,6 +243,7 @@
 EricApplication QApplication
 EricArrowItem QAbstractGraphicsShapeItem
 EricArrowType enum.Enum
+EricAutoResizeTextBrowser QTextBrowser
 EricClearableLineEdit EricLineEdit
 EricClickableLabel QLabel
 EricClickableLed EricLed
Binary file src/eric7/Documentation/Help/source.qch has changed
--- a/src/eric7/Documentation/Help/source.qhp	Sun Sep 15 16:56:58 2024 +0200
+++ b/src/eric7/Documentation/Help/source.qhp	Mon Sep 16 13:20:17 2024 +0200
@@ -158,6 +158,7 @@
             <section title="eric7.EricWidgets.EricAnimatedLabel" ref="eric7.EricWidgets.EricAnimatedLabel.html" />
             <section title="eric7.EricWidgets.EricAnimatedWidget" ref="eric7.EricWidgets.EricAnimatedWidget.html" />
             <section title="eric7.EricWidgets.EricApplication" ref="eric7.EricWidgets.EricApplication.html" />
+            <section title="eric7.EricWidgets.EricAutoResizeTextBrowser" ref="eric7.EricWidgets.EricAutoResizeTextBrowser.html" />
             <section title="eric7.EricWidgets.EricClickableLabel" ref="eric7.EricWidgets.EricClickableLabel.html" />
             <section title="eric7.EricWidgets.EricComboSelectionDialog" ref="eric7.EricWidgets.EricComboSelectionDialog.html" />
             <section title="eric7.EricWidgets.EricCompleters" ref="eric7.EricWidgets.EricCompleters.html" />
@@ -5444,6 +5445,11 @@
       <keyword name="EricArrowItem.setPoints" id="EricArrowItem.setPoints" ref="eric7.EricGraphics.EricArrowItem.html#EricArrowItem.setPoints" />
       <keyword name="EricArrowItem.setStartPoint" id="EricArrowItem.setStartPoint" ref="eric7.EricGraphics.EricArrowItem.html#EricArrowItem.setStartPoint" />
       <keyword name="EricArrowType" id="EricArrowType" ref="eric7.EricGraphics.EricArrowItem.html#EricArrowType" />
+      <keyword name="EricAutoResizeTextBrowser" id="EricAutoResizeTextBrowser" ref="eric7.EricWidgets.EricAutoResizeTextBrowser.html#EricAutoResizeTextBrowser" />
+      <keyword name="EricAutoResizeTextBrowser (Constructor)" id="EricAutoResizeTextBrowser (Constructor)" ref="eric7.EricWidgets.EricAutoResizeTextBrowser.html#EricAutoResizeTextBrowser.__init__" />
+      <keyword name="EricAutoResizeTextBrowser (Module)" id="EricAutoResizeTextBrowser (Module)" ref="eric7.EricWidgets.EricAutoResizeTextBrowser.html" />
+      <keyword name="EricAutoResizeTextBrowser.resizeEvent" id="EricAutoResizeTextBrowser.resizeEvent" ref="eric7.EricWidgets.EricAutoResizeTextBrowser.html#EricAutoResizeTextBrowser.resizeEvent" />
+      <keyword name="EricAutoResizeTextBrowser.updateGeometry" id="EricAutoResizeTextBrowser.updateGeometry" ref="eric7.EricWidgets.EricAutoResizeTextBrowser.html#EricAutoResizeTextBrowser.updateGeometry" />
       <keyword name="EricCache" id="EricCache" ref="eric7.EricUtilities.EricCache.html#EricCache" />
       <keyword name="EricCache (Constructor)" id="EricCache (Constructor)" ref="eric7.EricUtilities.EricCache.html#EricCache.__init__" />
       <keyword name="EricCache (Module)" id="EricCache (Module)" ref="eric7.EricUtilities.EricCache.html" />
@@ -21432,6 +21438,7 @@
       <file>eric7.EricWidgets.EricAnimatedLabel.html</file>
       <file>eric7.EricWidgets.EricAnimatedWidget.html</file>
       <file>eric7.EricWidgets.EricApplication.html</file>
+      <file>eric7.EricWidgets.EricAutoResizeTextBrowser.html</file>
       <file>eric7.EricWidgets.EricClickableLabel.html</file>
       <file>eric7.EricWidgets.EricComboSelectionDialog.html</file>
       <file>eric7.EricWidgets.EricCompleters.html</file>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/eric7/Documentation/Source/eric7.EricWidgets.EricAutoResizeTextBrowser.html	Mon Sep 16 13:20:17 2024 +0200
@@ -0,0 +1,112 @@
+<!DOCTYPE html>
+<html><head>
+<title>eric7.EricWidgets.EricAutoResizeTextBrowser</title>
+<meta charset="UTF-8">
+<link rel="stylesheet" href="styles.css">
+</head>
+<body>
+<a NAME="top" ID="top"></a>
+<h1>eric7.EricWidgets.EricAutoResizeTextBrowser</h1>
+<p>
+Module implementing a QTextBrowser widget that resizes automatically.
+</p>
+
+<h3>Global Attributes</h3>
+<table>
+<tr><td>None</td></tr>
+</table>
+
+<h3>Classes</h3>
+<table>
+<tr>
+<td><a href="#EricAutoResizeTextBrowser">EricAutoResizeTextBrowser</a></td>
+<td>Class implementing a QTextBrowser widget that adjusts its size automatically to the contained text.</td>
+</tr>
+</table>
+
+<h3>Functions</h3>
+<table>
+<tr><td>None</td></tr>
+</table>
+
+<hr />
+<hr />
+<a NAME="EricAutoResizeTextBrowser" ID="EricAutoResizeTextBrowser"></a>
+<h2>EricAutoResizeTextBrowser</h2>
+<p>
+    Class implementing a QTextBrowser widget that adjusts its size automatically to the
+    contained text.
+</p>
+
+<h3>Derived from</h3>
+QTextBrowser
+<h3>Class Attributes</h3>
+<table>
+<tr><td>None</td></tr>
+</table>
+
+<h3>Class Methods</h3>
+<table>
+<tr><td>None</td></tr>
+</table>
+
+<h3>Methods</h3>
+<table>
+<tr>
+<td><a href="#EricAutoResizeTextBrowser.__init__">EricAutoResizeTextBrowser</a></td>
+<td>Constructor</td>
+</tr>
+<tr>
+<td><a href="#EricAutoResizeTextBrowser.resizeEvent">resizeEvent</a></td>
+<td>Protected method to handle resize events.</td>
+</tr>
+<tr>
+<td><a href="#EricAutoResizeTextBrowser.updateGeometry">updateGeometry</a></td>
+<td>Public method to update the geometry depending on the current text.</td>
+</tr>
+</table>
+
+<h3>Static Methods</h3>
+<table>
+<tr><td>None</td></tr>
+</table>
+
+
+<a NAME="EricAutoResizeTextBrowser.__init__" ID="EricAutoResizeTextBrowser.__init__"></a>
+<h4>EricAutoResizeTextBrowser (Constructor)</h4>
+<b>EricAutoResizeTextBrowser</b>(<i>parent=None</i>)
+<p>
+        Constructor
+</p>
+
+<dl>
+
+<dt><i>parent</i> (QWidget (optional))</dt>
+<dd>
+reference to the parent widget (defaults to None)
+</dd>
+</dl>
+<a NAME="EricAutoResizeTextBrowser.resizeEvent" ID="EricAutoResizeTextBrowser.resizeEvent"></a>
+<h4>EricAutoResizeTextBrowser.resizeEvent</h4>
+<b>resizeEvent</b>(<i>evt</i>)
+<p>
+        Protected method to handle resize events.
+</p>
+
+<dl>
+
+<dt><i>evt</i> (QResizeEvent)</dt>
+<dd>
+reference to the resize event
+</dd>
+</dl>
+<a NAME="EricAutoResizeTextBrowser.updateGeometry" ID="EricAutoResizeTextBrowser.updateGeometry"></a>
+<h4>EricAutoResizeTextBrowser.updateGeometry</h4>
+<b>updateGeometry</b>(<i></i>)
+<p>
+        Public method to update the geometry depending on the current text.
+</p>
+
+<div align="right"><a href="#top">Up</a></div>
+<hr />
+</body></html>
--- a/src/eric7/Documentation/Source/index-eric7.EricWidgets.html	Sun Sep 15 16:56:58 2024 +0200
+++ b/src/eric7/Documentation/Source/index-eric7.EricWidgets.html	Mon Sep 16 13:20:17 2024 +0200
@@ -31,6 +31,10 @@
 <td>Class implementing a specialized application class.</td>
 </tr>
 <tr>
+<td><a href="eric7.EricWidgets.EricAutoResizeTextBrowser.html">EricAutoResizeTextBrowser</a></td>
+<td>Module implementing a QTextBrowser widget that resizes automatically.</td>
+</tr>
+<tr>
 <td><a href="eric7.EricWidgets.EricClickableLabel.html">EricClickableLabel</a></td>
 <td>Module implementing a clickable label.</td>
 </tr>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/eric7/EricWidgets/EricAutoResizeTextBrowser.py	Mon Sep 16 13:20:17 2024 +0200
@@ -0,0 +1,60 @@
+# -*- coding: utf-8 -*-
+
+# Copyright (c) 2024 Detlev Offenbach <detlev@die-offenbachs.de>
+#
+
+"""
+Module implementing a QTextBrowser widget that resizes automatically.
+"""
+
+from PyQt6.QtCore import Qt
+from PyQt6.QtWidgets import QFrame, QSizePolicy, QTextBrowser
+
+
+class EricAutoResizeTextBrowser(QTextBrowser):
+    """
+    Class implementing a QTextBrowser widget that adjusts its size automatically to the
+    contained text.
+    """
+
+    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