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,27 +521,33 @@ 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():
return self matches.append(word)
else: for c in range(1, len(tokens)):
match = True word = words.next()
for j in range(len(tokens)): print tokens[c], word.string
if i+j >= len(self.tokens) or tokens[j].lower() != qstring_to_unicode(self.tokens[i+j].string).lower(): if tokens[c] not in qstring_to_unicode(word.string):
match = False return None
break matches.append(word)
self.tokens[i+j].highlight = True for w in matches:
if match: w.highlight = True
return self return self
except StopIteration:
return None return None