Skip to content

Commit 2585d39

Browse files
committed
Added docstrings for text_engine module
1 parent 6b1a422 commit 2585d39

File tree

3 files changed

+192
-0
lines changed

3 files changed

+192
-0
lines changed

doc/reference/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,5 @@ Main `python-qwt` classes:
2929
series_store
3030
symbol
3131
text
32+
text_engine
3233
toqimage

doc/reference/text_engine.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.. automodule:: qwt.text_engine
2+
:members:

qwt/text_engine.py

Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,26 @@
55
# Copyright (c) 2015 Pierre Raybaut, for the Python translation/optimization
66
# (see LICENSE file for more details)
77

8+
"""
9+
QwtTextEngine
10+
-------------
11+
12+
.. autoclass:: QwtTextEngine
13+
:members:
14+
15+
QwtPlainTextEngine
16+
------------------
17+
18+
.. autoclass:: QwtPlainTextEngine
19+
:members:
20+
21+
QwtRichTextEngine
22+
-----------------
23+
24+
.. autoclass:: QwtRichTextEngine
25+
:members:
26+
"""
27+
828
from qwt.painter import QwtPainter
929

1030
from qwt.qt.QtGui import (QTextDocument, QTextOption, QColor, QFontMetricsF,
@@ -56,13 +76,92 @@ def __init__(self, text, flags, font):
5676

5777

5878
class QwtTextEngine(object):
79+
"""
80+
Abstract base class for rendering text strings
81+
82+
A text engine is responsible for rendering texts for a
83+
specific text format. They are used by `QwtText` to render a text.
84+
85+
`QwtPlainTextEngine` and `QwtRichTextEngine` are part of the
86+
`python-qwt` library.
87+
The implementation of `QwtMathMLTextEngine` uses code from the
88+
`Qt` solution package. Because of license implications it is built into
89+
a separate library.
90+
91+
.. seealso::
92+
93+
:py:meth:`qwt.text.QwtText.setTextEngine()`
94+
"""
5995
def __init__(self):
6096
pass
97+
98+
def heightForWidth(self, font, flags, text, width):
99+
"""
100+
Find the height for a given width
101+
102+
:param QFont font: Font of the text
103+
:param int flags: Bitwise OR of the flags used like in QPainter::drawText
104+
:param str text: Text to be rendered
105+
:param float width: Width
106+
:return: Calculated height
107+
"""
108+
pass
109+
110+
def textSize(self, font, flags, text):
111+
"""
112+
Returns the size, that is needed to render text
113+
114+
:param QFont font: Font of the text
115+
:param int flags: Bitwise OR of the flags like in for QPainter::drawText
116+
:param str text: Text to be rendered
117+
:return: Calculated size
118+
"""
119+
pass
120+
121+
def mightRender(self, text):
122+
"""
123+
Test if a string can be rendered by this text engine
124+
125+
:param str text: Text to be tested
126+
:return: True, if it can be rendered
127+
"""
128+
pass
129+
130+
def textMargins(self, font):
131+
"""
132+
Return margins around the texts
133+
134+
The textSize might include margins around the
135+
text, like QFontMetrics::descent(). In situations
136+
where texts need to be aligned in detail, knowing
137+
these margins might improve the layout calculations.
138+
139+
:param QFont font: Font of the text
140+
:return: tuple (left, right, top, bottom) representing margins
141+
"""
142+
pass
143+
144+
def draw(self, painter, rect, flags, text):
145+
"""
146+
Draw the text in a clipping rectangle
147+
148+
:param QPainter painter: Painter
149+
:param QRectF rect: Clipping rectangle
150+
:param int flags: Bitwise OR of the flags like in for QPainter::drawText()
151+
:param str text: Text to be rendered
152+
"""
153+
pass
61154

62155

63156
ASCENTCACHE = {}
64157

65158
class QwtPlainTextEngine(QwtTextEngine):
159+
"""
160+
A text engine for plain texts
161+
162+
`QwtPlainTextEngine` renders texts using the basic `Qt` classes
163+
`QPainter` and `QFontMetrics`.
164+
"""
66165
def __init__(self):
67166
self.qrectf_max = QRectF(0, 0, QWIDGETSIZE_MAX, QWIDGETSIZE_MAX)
68167
self._fm_cache = {}
@@ -85,12 +184,29 @@ def fontmetrics_f(self, font):
85184
return fm
86185

87186
def heightForWidth(self, font, flags, text, width):
187+
"""
188+
Find the height for a given width
189+
190+
:param QFont font: Font of the text
191+
:param int flags: Bitwise OR of the flags used like in QPainter::drawText
192+
:param str text: Text to be rendered
193+
:param float width: Width
194+
:return: Calculated height
195+
"""
88196
fm = self.fontmetrics_f(font)
89197
rect = fm.boundingRect(QRectF(0, 0, width, QWIDGETSIZE_MAX),
90198
flags, text)
91199
return rect.height()
92200

93201
def textSize(self, font, flags, text):
202+
"""
203+
Returns the size, that is needed to render text
204+
205+
:param QFont font: Font of the text
206+
:param int flags: Bitwise OR of the flags like in for QPainter::drawText
207+
:param str text: Text to be rendered
208+
:return: Calculated size
209+
"""
94210
fm = self.fontmetrics_f(font)
95211
rect = fm.boundingRect(self.qrectf_max, flags, text)
96212
return rect.size()
@@ -129,29 +245,77 @@ def findAscent(self, font):
129245
return fm.ascent()
130246

131247
def textMargins(self, font):
248+
"""
249+
Return margins around the texts
250+
251+
The textSize might include margins around the
252+
text, like QFontMetrics::descent(). In situations
253+
where texts need to be aligned in detail, knowing
254+
these margins might improve the layout calculations.
255+
256+
:param QFont font: Font of the text
257+
:return: tuple (left, right, top, bottom) representing margins
258+
"""
132259
left = right = top = 0
133260
fm = self.fontmetrics_f(font)
134261
top = fm.ascent() - self.effectiveAscent(font)
135262
bottom = fm.descent()
136263
return left, right, top, bottom
137264

138265
def draw(self, painter, rect, flags, text):
266+
"""
267+
Draw the text in a clipping rectangle
268+
269+
:param QPainter painter: Painter
270+
:param QRectF rect: Clipping rectangle
271+
:param int flags: Bitwise OR of the flags like in for QPainter::drawText()
272+
:param str text: Text to be rendered
273+
"""
139274
QwtPainter.drawText(painter, rect, flags, text)
140275

141276
def mightRender(self, text):
277+
"""
278+
Test if a string can be rendered by this text engine
279+
280+
:param str text: Text to be tested
281+
:return: True, if it can be rendered
282+
"""
142283
return True
143284

144285

145286
class QwtRichTextEngine(QwtTextEngine):
287+
"""
288+
A text engine for `Qt` rich texts
289+
290+
`QwtRichTextEngine` renders `Qt` rich texts using the classes
291+
of the Scribe framework of `Qt`.
292+
"""
146293
def __init__(self):
147294
pass
148295

149296
def heightForWidth(self, font, flags, text, width):
297+
"""
298+
Find the height for a given width
299+
300+
:param QFont font: Font of the text
301+
:param int flags: Bitwise OR of the flags used like in QPainter::drawText
302+
:param str text: Text to be rendered
303+
:param float width: Width
304+
:return: Calculated height
305+
"""
150306
doc = QwtRichTextDocument(text, flags, font)
151307
doc.setPageSize(QSizeF(width, QWIDGETSIZE_MAX))
152308
return doc.documentLayout().documentSize().height()
153309

154310
def textSize(self, font, flags, text):
311+
"""
312+
Returns the size, that is needed to render text
313+
314+
:param QFont font: Font of the text
315+
:param int flags: Bitwise OR of the flags like in for QPainter::drawText
316+
:param str text: Text to be rendered
317+
:return: Calculated size
318+
"""
155319
doc = QwtRichTextDocument(text, flags, font)
156320
option = doc.defaultTextOption()
157321
if option.wrapMode() != QTextOption.NoWrap:
@@ -161,14 +325,39 @@ def textSize(self, font, flags, text):
161325
return doc.size()
162326

163327
def draw(self, painter, rect, flags, text):
328+
"""
329+
Draw the text in a clipping rectangle
330+
331+
:param QPainter painter: Painter
332+
:param QRectF rect: Clipping rectangle
333+
:param int flags: Bitwise OR of the flags like in for QPainter::drawText()
334+
:param str text: Text to be rendered
335+
"""
164336
doc = QwtRichTextDocument(text, flags, painter.font())
165337
QwtPainter.drawSimpleRichText(painter, rect, flags, doc)
166338

167339
def taggedText(self, text, flags):
168340
return self.taggedRichText(text,flags)
169341

170342
def mightRender(self, text):
343+
"""
344+
Test if a string can be rendered by this text engine
345+
346+
:param str text: Text to be tested
347+
:return: True, if it can be rendered
348+
"""
171349
return Qt.mightBeRichText(text)
172350

173351
def textMargins(self, font):
352+
"""
353+
Return margins around the texts
354+
355+
The textSize might include margins around the
356+
text, like QFontMetrics::descent(). In situations
357+
where texts need to be aligned in detail, knowing
358+
these margins might improve the layout calculations.
359+
360+
:param QFont font: Font of the text
361+
:return: tuple (left, right, top, bottom) representing margins
362+
"""
174363
return 0, 0, 0, 0

0 commit comments

Comments
 (0)