Refactor CompletionView

This commit is contained in:
Florian Bruhin 2014-01-29 09:14:37 +01:00
parent a8f9137fc8
commit 46660b11ef
2 changed files with 38 additions and 36 deletions

View File

@ -111,6 +111,28 @@ class CompletionModel(QAbstractItemModel):
newitem = CompletionItem(item, newcat) newitem = CompletionItem(item, newcat)
newcat.children.append(newitem) newcat.children.append(newitem)
def mark_all_items(self, needle):
for i in range(self.rowCount()):
cat = self.index(i, 0)
for k in range(self.rowCount(cat)):
idx = self.index(k, 0, cat)
old = self.data(idx).value()
marks = self._get_marks(needle, old)
self.setData(idx, marks, Qt.UserRole)
def _get_marks(self, needle, haystack):
pos1 = pos2 = 0
marks = []
if not needle:
return marks
while True:
pos1 = haystack.find(needle, pos2)
if pos1 == -1:
break
pos2 = pos1 + len(needle)
marks.append((pos1, pos2))
return marks
class CompletionItem(): class CompletionItem():
parent = None parent = None
@ -192,3 +214,11 @@ class CompletionFilterModel(QSortFilterProxyModel):
return False return False
else: else:
return left < right return left < right
def first_item(self):
cat = self.index(0, 0)
return self.index(0, 0, cat)
def last_item(self):
cat = self.index(self.rowCount() - 1, 0)
return self.index(self.rowCount(cat) - 1, 0, cat)

View File

@ -152,40 +152,10 @@ class CompletionView(QTreeView):
self.setmodel('command') self.setmodel('command')
text = text.lstrip(':') text = text.lstrip(':')
self.model.pattern = text self.model.pattern = text
self.mark_all_items(text) self.model.sourceModel().mark_all_items(text)
if self.enabled: if self.enabled:
self.show() self.show()
def first_item(self):
cat = self.model.index(0, 0)
return self.model.index(0, 0, cat)
def last_item(self):
cat = self.model.index(self.model.rowCount() - 1, 0)
return self.model.index(self.model.rowCount(cat) - 1, 0, cat)
def mark_all_items(self, needle):
for i in range(self.model.rowCount()):
cat = self.model.index(i, 0)
for k in range(self.model.rowCount(cat)):
idx = self.model.index(k, 0, cat)
old = self.model.data(idx)
marks = self.get_marks(needle, old)
self.model.setData(idx, marks, Qt.UserRole)
def get_marks(self, needle, haystack):
pos1 = pos2 = 0
marks = []
if not needle:
return marks
while True:
pos1 = haystack.find(needle, pos2)
if pos1 == -1:
break
pos2 = pos1 + len(needle)
marks.append((pos1, pos2))
return marks
def tab_handler(self, shift): def tab_handler(self, shift):
if not self.completing: if not self.completing:
return return
@ -201,13 +171,15 @@ class CompletionView(QTreeView):
idx = self.selectionModel().currentIndex() idx = self.selectionModel().currentIndex()
if not idx.isValid(): if not idx.isValid():
# No item selected yet # No item selected yet
return self.first_item() return self.model.first_item()
while True: while True:
idx = self.indexAbove(idx) if shift else self.indexBelow(idx) idx = self.indexAbove(idx) if shift else self.indexBelow(idx)
if not idx.isValid(): # wrap around if we arrived at beginning/end
# wrap around if we arrived at beginning/end if not idx.isValid() and shift:
return self.last_item() if shift else self.first_item() return self.model.last_item()
if idx.parent().isValid(): elif not idx.isValid() and not shift:
return self.model.first_item()
elif idx.parent().isValid():
# Item is a real item, not a category header -> success # Item is a real item, not a category header -> success
return idx return idx