On Thursday, August 28, 2014 4:54:10 AM UTC-5, Edward K. Ream wrote:
Two parts to this reply:
===== Part 1: defining w.getTextLength
> w.getTextLength() isn't trivial...Caching the value returned by
w.getAllText() seems too dangerous.
Actually, defining w.getTextLength using a textChanged event handler in
LeoQTextBrowser is straightforward::
def onTextChanged(self):
'''The textChanged event handler. Cached the text in self.leo_s.'''
self.leo_s = s = g.u(self.toPlainText())
def getTextLength(self):
'''Return the length of all the text.'''
if 1: # safe.
s = g.u(self.toPlainText())
if s != self.leo_s:
g.trace('can not happen: text mismatch.')
self.leo_s = s
return len(self.leo_s)
else: # fast
return len(self.leo_s)
We can use the "safe" version of getTextLength until we are *sure* the can
not happen message never happens ;-)
Otoh, we must be careful: onTextChanged will be called in the middle of the
node-switching logic.
===== Part 2: doing without w.getTextLength
Imo, the simplest thing that could possibly work is to avoid the extra call
to w.getAllText in the node switching logic in selectHelper.
Here are the relevant parts of selectHelper. (w is a LeoQTextEditWidget).
1. setBodyTextAfterSelect *already* calls w.getAllText() to get the
"before" text. This is unavoidable.
The "after" text will be p.b, where p is the *new* position.
2. restoreCursorAndScroll calls w.setInsertPoint, which just calls
w.setSelectionRangeHelper.
3. w.setSelectionRangeHelper is the problem It used to call the horribly
slow lengthHelper. Now it calls w.getAllText()
But w.getAllText must be the new p.b! So the selectHelper logic could
simply pass an optional s argument to w.setInsertPoint and
w.setSelectionRangeHelper. If present, setSelectionRangeHelper will use s
rather than calling w.getAllText.
Imo, this is the safe, if not exactly elegant way.
Edward
P.S. I plan to review all calls to w.getAllText, so see if any other calls
are used just to compute a length.
EKR
--
You received this message because you are subscribed to the Google Groups
"leo-editor" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/leo-editor.
For more options, visit https://groups.google.com/d/optout.