Skip to content

Commit 9734ad2

Browse files
committed
bold only current word, not all search suggested words
1 parent 3c3bc6d commit 9734ad2

1 file changed

Lines changed: 7 additions & 11 deletions

File tree

activity_browser/ui/widgets/text_edit.py

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,12 @@ def highlightBlock(self, text: str):
2929

3030

3131
class AutoCompleteDelegate(QStyledItemDelegate):
32-
def __init__(self, parent=None, get_bold_word_func=None):
32+
def __init__(self, parent=None):
3333
super().__init__(parent)
34-
self.get_bold_word_func = get_bold_word_func
34+
self.current_word_index = -1
3535

3636
def paint(self, painter, option, index):
3737
text = index.data(Qt.DisplayRole)
38-
bold_words = self.get_bold_word_func()
39-
bold_words = {word.lower() for word in bold_words}
4038

4139
painter.save()
4240

@@ -55,9 +53,9 @@ def paint(self, painter, option, index):
5553
font = option.font
5654
metrics = painter.fontMetrics()
5755

58-
for word in words:
56+
for i, word in enumerate(words):
5957
word_font = QFont(font)
60-
if word.lower() in bold_words:
58+
if i+1 == self.current_word_index:
6159
word_font.setBold(True)
6260
painter.setFont(word_font)
6361

@@ -97,16 +95,14 @@ class ABAutoCompleTextEdit(ABTextEdit):
9795
def __init__(self, parent=None, highlight_unknown=False):
9896
super().__init__(parent=parent)
9997
self.auto_complete_word = ""
100-
self.auto_complete_suggestions = []
10198

10299
# autocompleter settings
103100
self.model = QStringListModel()
104101
self.completer = QCompleter(self.model)
105102
self.completer.setWidget(self)
106103
self.popup = self.completer.popup()
107-
# set custom delegate to bold the current word
108-
delegate = AutoCompleteDelegate(self.popup, get_bold_word_func=lambda: self.auto_complete_suggestions)
109-
self.popup.setItemDelegate(delegate)
104+
self.delegate = AutoCompleteDelegate(self.popup) # set custom delegate to bold the current word
105+
self.popup.setItemDelegate(self.delegate)
110106
self.popup.setHorizontalScrollBarPolicy(Qt.ScrollBarAsNeeded)
111107
self.completer.setPopup(self.popup)
112108
self.completer.setCompletionMode(QCompleter.UnfilteredPopupCompletion) # allow all items in popup list
@@ -228,10 +224,10 @@ def _set_autocomplete_items(self):
228224
self.auto_complete_word = current_word
229225

230226
context = set((text[:start] + text[end:]).split(" "))
227+
self.delegate.current_word_index = len(text[:start].split(" ")) # current word index for bolding
231228
# get suggestions for the current word
232229
suggestions = AB_metadata.auto_complete(current_word, context=context, database=self.database_name)
233230
suggestions = suggestions[:6] # at most 6, though we should get ~3 usually
234-
self.auto_complete_suggestions = suggestions # set for bolding of autocomplete suggestions
235231
# replace the current word with each alternative
236232
items = []
237233
for alt in suggestions:

0 commit comments

Comments
 (0)