parent
b5781f0ed3
commit
1917911dd8
@ -34,7 +34,7 @@ class CompletionFilterModel(QSortFilterProxyModel):
|
|||||||
"""Subclass of QSortFilterProxyModel with custom sorting/filtering.
|
"""Subclass of QSortFilterProxyModel with custom sorting/filtering.
|
||||||
|
|
||||||
Attributes:
|
Attributes:
|
||||||
_pattern: The pattern to filter with.
|
pattern: The pattern to filter with.
|
||||||
srcmodel: The current source model.
|
srcmodel: The current source model.
|
||||||
Kept as attribute because calling `sourceModel` takes quite
|
Kept as attribute because calling `sourceModel` takes quite
|
||||||
a long time for some reason.
|
a long time for some reason.
|
||||||
@ -44,7 +44,7 @@ class CompletionFilterModel(QSortFilterProxyModel):
|
|||||||
super().__init__(parent)
|
super().__init__(parent)
|
||||||
super().setSourceModel(source)
|
super().setSourceModel(source)
|
||||||
self.srcmodel = source
|
self.srcmodel = source
|
||||||
self._pattern = ''
|
self.pattern = ''
|
||||||
|
|
||||||
def set_pattern(self, val):
|
def set_pattern(self, val):
|
||||||
"""Setter for pattern.
|
"""Setter for pattern.
|
||||||
@ -57,7 +57,7 @@ class CompletionFilterModel(QSortFilterProxyModel):
|
|||||||
Args:
|
Args:
|
||||||
val: The value to set.
|
val: The value to set.
|
||||||
"""
|
"""
|
||||||
self._pattern = val
|
self.pattern = val
|
||||||
self.invalidate()
|
self.invalidate()
|
||||||
sortcol = 0
|
sortcol = 0
|
||||||
try:
|
try:
|
||||||
@ -102,6 +102,8 @@ class CompletionFilterModel(QSortFilterProxyModel):
|
|||||||
|
|
||||||
def mark_all_items(self, text):
|
def mark_all_items(self, text):
|
||||||
"""Mark the given text in all visible items."""
|
"""Mark the given text in all visible items."""
|
||||||
|
if not text:
|
||||||
|
return
|
||||||
for i in range(self.rowCount()):
|
for i in range(self.rowCount()):
|
||||||
cat = self.index(i, 0)
|
cat = self.index(i, 0)
|
||||||
qtutils.ensure_valid(cat)
|
qtutils.ensure_valid(cat)
|
||||||
@ -129,7 +131,7 @@ class CompletionFilterModel(QSortFilterProxyModel):
|
|||||||
parent: The parent item QModelIndex.
|
parent: The parent item QModelIndex.
|
||||||
|
|
||||||
Return:
|
Return:
|
||||||
True if self._pattern is contained in item, or if it's a root item
|
True if self.pattern is contained in item, or if it's a root item
|
||||||
(category). False in all other cases
|
(category). False in all other cases
|
||||||
"""
|
"""
|
||||||
if parent == QModelIndex():
|
if parent == QModelIndex():
|
||||||
@ -138,14 +140,14 @@ class CompletionFilterModel(QSortFilterProxyModel):
|
|||||||
qtutils.ensure_valid(idx)
|
qtutils.ensure_valid(idx)
|
||||||
data = self.srcmodel.data(idx)
|
data = self.srcmodel.data(idx)
|
||||||
# TODO more sophisticated filtering
|
# TODO more sophisticated filtering
|
||||||
if not self._pattern:
|
if not self.pattern:
|
||||||
return True
|
return True
|
||||||
return self._pattern.casefold() in data.casefold()
|
return self.pattern.casefold() in data.casefold()
|
||||||
|
|
||||||
def lessThan(self, lindex, rindex):
|
def lessThan(self, lindex, rindex):
|
||||||
"""Custom sorting implementation.
|
"""Custom sorting implementation.
|
||||||
|
|
||||||
Prefers all items which start with self._pattern. Other than that, uses
|
Prefers all items which start with self.pattern. Other than that, uses
|
||||||
normal Python string sorting.
|
normal Python string sorting.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
@ -167,8 +169,8 @@ class CompletionFilterModel(QSortFilterProxyModel):
|
|||||||
left = self.srcmodel.data(lindex)
|
left = self.srcmodel.data(lindex)
|
||||||
right = self.srcmodel.data(rindex)
|
right = self.srcmodel.data(rindex)
|
||||||
|
|
||||||
leftstart = left.startswith(self._pattern)
|
leftstart = left.startswith(self.pattern)
|
||||||
rightstart = right.startswith(self._pattern)
|
rightstart = right.startswith(self.pattern)
|
||||||
|
|
||||||
if leftstart and rightstart:
|
if leftstart and rightstart:
|
||||||
return left < right
|
return left < right
|
||||||
|
@ -197,7 +197,10 @@ class CompletionItemDelegate(QStyledItemDelegate):
|
|||||||
"""))
|
"""))
|
||||||
self._doc.setDocumentMargin(2)
|
self._doc.setDocumentMargin(2)
|
||||||
|
|
||||||
if index.column() == 0:
|
if index.column() == 0 and index.model().pattern:
|
||||||
|
# We take a shortcut here by not checking anything if the models
|
||||||
|
# pattern is empty. This is required because the marks data is
|
||||||
|
# invalid in that case (for performance reasons).
|
||||||
marks = index.data(basecompletion.Role.marks)
|
marks = index.data(basecompletion.Role.marks)
|
||||||
if marks is None:
|
if marks is None:
|
||||||
return
|
return
|
||||||
|
Loading…
Reference in New Issue
Block a user