Skip to content

Commit c4d8897

Browse files
committed
QwtText: code cleaning / optimization
1 parent 54fbf0e commit c4d8897

File tree

1 file changed

+20
-36
lines changed

1 file changed

+20
-36
lines changed

qwt/text.py

Lines changed: 20 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -159,9 +159,12 @@ class QwtText(object):
159159
# enum LayoutAttribute
160160
MinimumLayout = 0x01
161161

162+
# Optimization: a single text engine for all QwtText objects
163+
# (this is not how it's implemented in Qwt6 C++ library)
164+
__map = {PlainText: QwtPlainTextEngine(), RichText: QwtRichTextEngine()}
165+
162166
def __init__(self, text=None, textFormat=None, other=None):
163167
self.__desktopwidget = None
164-
self._dict = QwtTextEngineDict()
165168
if text is None:
166169
text = ''
167170
if textFormat is None:
@@ -569,7 +572,7 @@ def draw(self, painter, rect):
569572
self.__data.renderFlags, self.__data.text)
570573
painter.restore()
571574

572-
def textEngine(self, *args):
575+
def textEngine(self, text=None, format_=None):
573576
"""
574577
Find the text engine for a text format
575578
@@ -585,7 +588,21 @@ def textEngine(self, *args):
585588
:param int format: Text format
586589
:return: Corresponding text engine
587590
"""
588-
return self._dict.textEngine(*args)
591+
if text is None:
592+
return self.__map.get(format_)
593+
elif format_ is not None:
594+
if format_ == QwtText.AutoText:
595+
for key, engine in list(self.__map.items()):
596+
if key != QwtText.PlainText:
597+
if engine and engine.mightRender(text):
598+
return engine
599+
engine = self.__map.get(format_)
600+
if engine is not None:
601+
return engine
602+
return self.__map[QwtText.PlainText]
603+
else:
604+
raise TypeError("%s().textEngine() takes 1 or 2 argument(s) (none"\
605+
" given)" % self.__class__.__name__)
589606

590607
def setTextEngine(self, format_, engine):
591608
"""
@@ -607,39 +624,6 @@ def setTextEngine(self, format_, engine):
607624
608625
Using `QwtText.AutoText` does nothing.
609626
"""
610-
self._dict.setTextEngine(format_, engine)
611-
612-
613-
class QwtTextEngineDict(object):
614-
# Optimization: a single text engine for all QwtText objects
615-
# (this is not how it's implemented in Qwt6 C++ library)
616-
__map = {QwtText.PlainText: QwtPlainTextEngine(),
617-
QwtText.RichText: QwtRichTextEngine()}
618-
619-
def textEngine(self, *args):
620-
if len(args) == 1:
621-
format_ = args[0]
622-
return self.__map.get(format_)
623-
elif len(args) == 2:
624-
text, format_ = args
625-
626-
if format_ == QwtText.AutoText:
627-
for key, engine in list(self.__map.items()):
628-
if key != QwtText.PlainText:
629-
if engine and engine.mightRender(text):
630-
return engine
631-
632-
engine = self.__map.get(format_)
633-
if engine is not None:
634-
return engine
635-
636-
engine = self.__map[QwtText.PlainText]
637-
return engine
638-
else:
639-
raise TypeError("%s().textEngine() takes 1 or 2 argument(s) (%s "\
640-
"given)" % (self.__class__.__name__, len(args)))
641-
642-
def setTextEngine(self, format_, engine):
643627
if format_ == QwtText.AutoText:
644628
return
645629
if format_ == QwtText.PlainText and engine is None:

0 commit comments

Comments
 (0)