From 1650efbb1fbc5bdb72448191a12d9d3495585061 Mon Sep 17 00:00:00 2001 From: BLKSerene Date: Sun, 5 Jan 2025 12:47:53 +0800 Subject: [PATCH] Work Area: Fix Work Area - Search in results - Clear highlights --- CHANGELOG.md | 1 + wordless/wl_results/wl_results_search.py | 49 ++++++++++++------------ wordless/wl_results/wl_results_sort.py | 6 +-- wordless/wl_widgets/wl_tables.py | 6 +-- 4 files changed, 31 insertions(+), 31 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d757f6ae4..14e5c8b17 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/wordless/wl_results/wl_results_search.py b/wordless/wl_results/wl_results_search.py index f6e484c16..2171dcd09 100644 --- a/wordless/wl_results/wl_results_search.py +++ b/wordless/wl_results/wl_results_search.py @@ -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()] @@ -231,9 +228,6 @@ 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() @@ -241,9 +235,6 @@ def find_prev(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()] @@ -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 @@ -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: @@ -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]) diff --git a/wordless/wl_results/wl_results_sort.py b/wordless/wl_results/wl_results_sort.py index e21f368ea..cb137a59a 100644 --- a/wordless/wl_results/wl_results_sort.py +++ b/wordless/wl_results/wl_results_sort.py @@ -155,6 +155,9 @@ def update_gui(self, results): 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'] @@ -240,9 +243,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): diff --git a/wordless/wl_widgets/wl_tables.py b/wordless/wl_widgets/wl_tables.py index a00e0afa0..8335b6513 100644 --- a/wordless/wl_widgets/wl_tables.py +++ b/wordless/wl_widgets/wl_tables.py @@ -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())