Sort items correctly

This commit is contained in:
Florian Bruhin 2014-01-27 22:18:12 +01:00
parent 7a549b38a4
commit 3e47c89bac
2 changed files with 29 additions and 2 deletions

View File

@ -99,6 +99,9 @@ class CompletionModel(QAbstractItemModel):
return len(pitem.children)
def sort(self, column, order=Qt.AscendingOrder):
raise NotImplementedError
def init_data(self):
for (cat, items) in self._data.items():
newcat = CompletionItem([cat], self.root)
@ -145,7 +148,7 @@ class CompletionItem():
class CompletionFilterModel(QSortFilterProxyModel):
_pattern = None
# FIXME sort in a way strings starting with pattern are first
pattern_changed = pyqtSignal(str)
@property
def pattern(self):
@ -154,7 +157,8 @@ class CompletionFilterModel(QSortFilterProxyModel):
@pattern.setter
def pattern(self, val):
self._pattern = val
self.invalidateFilter()
self.invalidate()
self.pattern_changed.emit(val)
def __init__(self, parent=None):
super().__init__(parent)
@ -169,3 +173,19 @@ class CompletionFilterModel(QSortFilterProxyModel):
if not self.pattern:
return True
return self.pattern in data
def lessThan(self, lindex, rindex):
left = self.sourceModel().data(lindex).value()
right = self.sourceModel().data(rindex).value()
leftstart = left.startswith(self.pattern)
rightstart = right.startswith(self.pattern)
if leftstart and rightstart:
return left < right
elif leftstart:
return True
elif rightstart:
return False
else:
return left < right

View File

@ -51,6 +51,7 @@ class CompletionView(QTreeView):
self.model = CompletionFilterModel()
self.setModel(self.model)
self.model.setSourceModel(self.completion_models['command'])
self.model.pattern_changed.connect(self.resort)
self.setItemDelegate(CompletionItemDelegate())
self.setStyleSheet(self._stylesheet.strip())
self.expandAll()
@ -71,6 +72,12 @@ class CompletionView(QTreeView):
self.model.pattern = ''
self.expandAll()
def resort(self, pattern):
try:
self.model.sourceModel().sort(0)
except NotImplementedError:
self.model.sort(0)
def resize_to_bar(self, geom):
bottomleft = geom.topLeft()
bottomright = geom.topRight()