Skip to content

Commit

Permalink
Work Area: Fix Work Area - Search in results - Clear highlights
Browse files Browse the repository at this point in the history
  • Loading branch information
BLKSerene committed Jan 5, 2025
1 parent a6454df commit d6a5775
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 42 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
- Work Area: Fix Wordlist Generator - Filter results - Number of syllables
- Work Area: Fix Wordlist Generator - Syllabification
- Work Area: Fix Work Area - Filter results - File to filter
- Work Area: Fix Work Area - Search in results - Clear highlights

### ❌ Removals
- Measures: Remove effect size - Log-frequency biased MD / Mutual Dependency
Expand Down
49 changes: 24 additions & 25 deletions wordless/wl_results/wl_results_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,9 +196,6 @@ def find_next(self):
if self.items_found:
selected_rows = []

for table in self.tables:
table.disable_updates()

for table in self.tables:
if table.get_selected_rows():
selected_rows = [id(table), table.get_selected_rows()]
Expand Down Expand Up @@ -231,19 +228,13 @@ def find_next(self):
self.tables[0].scrollTo(table.model().index(self.items_found[0][1], 0))
self.tables[0].selectRow(self.items_found[0][1])

for table in self.tables:
table.enable_updates()

@wl_misc.log_time
def find_prev(self):
self.find_all()

if self.items_found:
selected_rows = []

for table in self.tables:
table.disable_updates()

for table in self.tables:
if table.get_selected_rows():
selected_rows = [id(table), table.get_selected_rows()]
Expand Down Expand Up @@ -276,9 +267,6 @@ def find_prev(self):
self.tables[-1].scrollTo(table.model().index(self.items_found[-1][1], 0))
self.tables[-1].selectRow(self.items_found[-1][1])

for table in self.tables:
table.enable_updates()

@wl_misc.log_time
def find_all(self):
# Search only when there are no search history or search settings have been changed
Expand Down Expand Up @@ -338,27 +326,29 @@ def clr_highlights(self):
for table in self.tables:
table.disable_updates()

for table, row, col in self.items_found:
if table.indexWidget(table.model().index(row, col)):
table.indexWidget(table.model().index(row, col)).setStyleSheet('border: 0')
# Skip if the found item no longer exist (eg. the table has been re-generated)
elif table.model().item(row, col):
table.model().item(row, col).setForeground(QBrush(QColor(table.default_foreground)))
table.model().item(row, col).setBackground(QBrush(QColor(table.default_background)))
# Clear highlights for every cell since indexes of items found would be changed after sorting
for table in self.tables:
for row in range(table.model().rowCount()):
for col in range(table.model().columnCount()):
if table.indexWidget(table.model().index(row, col)):
table.indexWidget(table.model().index(row, col)).setStyleSheet('border: 0')
else:
table.model().item(row, col).setForeground(QBrush(QColor(table.default_foreground)))
table.model().item(row, col).setBackground(QBrush(QColor(table.default_background)))

for table in self.tables:
table.enable_updates()
table.enable_updates(emit_signals = False)

self.clr_history()

self.main.statusBar().showMessage(self.tr('Highlights cleared.'))

self.button_clr_hightlights.setEnabled(False)

def clr_history(self):
self.last_search_settings.clear()
self.items_found.clear()

self.button_clr_hightlights.setEnabled(False)

class Wl_Worker_Results_Search(wl_threading.Wl_Worker):
def run(self):
for table in self.dialog.tables:
Expand Down Expand Up @@ -406,14 +396,23 @@ def run(self):

search_terms |= set(search_terms_file)

for search_term in search_terms:
len_search_term = len(search_term)
lens_search_terms = [len(search_term) for search_term in search_terms]

for (row, col), text in results.items():
matched = False

for (row, col), text in results.items():
for search_term, len_search_term in zip(search_terms, lens_search_terms):
for ngram in wl_nlp_utils.ngrams(text, len_search_term):
if ngram == tuple(search_term):
self.dialog.items_found.append([table, row, col])

matched = True

break

if matched:
break

self.dialog.items_found = sorted(
self.dialog.items_found,
key = lambda item: (id(item[0]), item[1], item[2])
Expand Down
27 changes: 13 additions & 14 deletions wordless/wl_results/wl_results_sort.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,9 @@ def update_gui(self, results):
results[i][2] = right_new

# Sort results
re_sorting_col_l = re.compile(self.tr(r'^L[1-9][0-9]*$'))
re_sorting_col_r = re.compile(self.tr(r'^R[1-9][0-9]*$'))

for sorting_col, sorting_order in reversed(self.settings['sorting_rules']):
reverse = 0 if sorting_order == self.tr('Ascending') else 1

Expand All @@ -148,13 +151,14 @@ def update_gui(self, results):
else:
span = int(sorting_col[1:])

if re.search(self.tr(r'^L[1-9][0-9]*$'), sorting_col):

if re_sorting_col_l.search(sorting_col):
results.sort(key = lambda item, span = span: item[0].tokens_raw[-span], reverse = reverse)
elif re.search(self.tr(r'^R[1-9][0-9]*$'), sorting_col):

elif re_sorting_col_r.search(sorting_col):
results.sort(key = lambda item, span = span: item[2].tokens_raw[span - 1], reverse = reverse)

# Clear highlights before sorting the results
self.table.dialog_results_search.clr_highlights()

self.table.disable_updates()

color_settings = self.main.settings_custom['tables']['concordancer']['sorting_settings']['highlight_colors']
Expand All @@ -167,6 +171,8 @@ def update_gui(self, results):
color_settings['lvl_6']
]

re_node_color = re.compile(r'(?<=color: )#[0-9A-Za-z]{6}(?=;)')

for row, (
left, node, right, sentiment,
no_token, no_token_pct,
Expand All @@ -180,18 +186,14 @@ def update_gui(self, results):
text_right = [token for token in right.tokens_raw if token]

# Re-apply node color
node_text = re.sub(
r'(?<=color: )#[0-9A-Za-z]{6}(?=;)',
color_settings['lvl_1'],
node.text()
)
node_text = re_node_color.sub(color_settings['lvl_1'], node.text())

# Start from level 2 (level 1 applies to nodes)
i_highlight_color_left = 1
i_highlight_color_right = 1

for sorting_col, _ in self.settings['sorting_rules']:
if re.search(self.tr(r'^L[0-9]+$'), sorting_col) and int(sorting_col[1:]) <= len(text_left):
if re_sorting_col_l.search(sorting_col) and int(sorting_col[1:]) <= len(text_left):
hightlight_color = highlight_colors[i_highlight_color_left % len(highlight_colors)]

text_left[-int(sorting_col[1:])] = f'''
Expand All @@ -201,7 +203,7 @@ def update_gui(self, results):
'''

i_highlight_color_left += 1
elif re.search(self.tr(r'^R[0-9]+$'), sorting_col) and int(sorting_col[1:]) - 1 < len(text_right):
elif re_sorting_col_r.search(sorting_col) and int(sorting_col[1:]) - 1 < len(text_right):
hightlight_color = highlight_colors[i_highlight_color_right % len(highlight_colors)]

text_right[int(sorting_col[1:]) - 1] = f'''
Expand Down Expand Up @@ -240,9 +242,6 @@ def update_gui(self, results):
self.table.set_item_num_val(row, 11, no_para_pct)
self.table.model().item(row, 12).setText(file)

# Clear highlights
self.table.dialog_results_search.clr_highlights()

self.table.enable_updates(emit_signals = False)

class Wl_Table_Results_Sort_Conordancer(wl_tables.Wl_Table_Add_Ins_Del_Clr):
Expand Down
6 changes: 3 additions & 3 deletions wordless/wl_widgets/wl_tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,10 +263,10 @@ def enable_updates(self, emit_signals = True):
self.selectionModel().blockSignals(False)
self.show()

self.horizontalHeader().sectionCountChanged.emit(self.num_cols_old, self.model().columnCount())
self.verticalHeader().sectionCountChanged.emit(self.num_rows_old, self.model().rowCount())

if emit_signals:
self.horizontalHeader().sectionCountChanged.emit(self.num_cols_old, self.model().columnCount())
self.verticalHeader().sectionCountChanged.emit(self.num_rows_old, self.model().rowCount())

self.model().itemChanged.emit(QStandardItem())
self.selectionModel().selectionChanged.emit(QItemSelection(), QItemSelection())

Expand Down

0 comments on commit d6a5775

Please sign in to comment.