Refactor search implementation

This commit is contained in:
Kovid Goyal 2007-12-27 21:03:52 +00:00
parent 99ceb7a142
commit 7227fe1c4f

View File

@ -521,28 +521,34 @@ class Line(QGraphicsItem):
x += tok.width x += tok.width
painter.restore() painter.restore()
def words(self):
for w in self.tokens:
if isinstance(w, Word):
yield w
def search(self, phrase): def search(self, phrase):
tokens = phrase.lower().split() tokens = phrase.lower().split()
if len(tokens) < 1: return None if len(tokens) < 1: return None
for i in range(len(self.tokens)):
if not isinstance(self.tokens[i], Word): words = self.words()
continue matches = []
src = qstring_to_unicode(self.tokens[i].string).lower() try:
self.tokens[i].highlight = False while True:
if tokens[0] in src: word = words.next()
if len(tokens) == 1: word.highlight = False
self.tokens[i].highlight = True if tokens[0] in qstring_to_unicode(word.string).lower():
matches.append(word)
for c in range(1, len(tokens)):
word = words.next()
print tokens[c], word.string
if tokens[c] not in qstring_to_unicode(word.string):
return None
matches.append(word)
for w in matches:
w.highlight = True
return self return self
else: except StopIteration:
match = True return None
for j in range(len(tokens)):
if i+j >= len(self.tokens) or tokens[j].lower() != qstring_to_unicode(self.tokens[i+j].string).lower():
match = False
break
self.tokens[i+j].highlight = True
if match:
return self
return None
def getx(self, textwidth): def getx(self, textwidth):