|
| 1 | +"""Find and Replace Controller. |
| 2 | +
|
| 3 | +This controller mediates find/replace operations across multiple table |
| 4 | +controllers. It owns the coordination logic for searching, highlighting, |
| 5 | +and replacing across multiple tables. |
| 6 | +""" |
| 7 | + |
| 8 | + |
| 9 | +class FindReplaceController: |
| 10 | + """Coordinates find/replace operations across multiple table controllers. |
| 11 | +
|
| 12 | + This controller provides a clean interface for the FindReplaceBar view to |
| 13 | + search, highlight, focus, and replace text across multiple tables without |
| 14 | + knowing about individual table controllers. It works as a mediator |
| 15 | + encapsulating the coordination logic between multiple table controllers. |
| 16 | + """ |
| 17 | + |
| 18 | + def __init__(self, table_controllers: dict): |
| 19 | + """Initialize the find/replace controller. |
| 20 | +
|
| 21 | + Args: |
| 22 | + table_controllers: Dictionary mapping table names to their |
| 23 | + controllers. |
| 24 | + Example: {"Measurement Table": measurement_controller, ...} |
| 25 | + """ |
| 26 | + self.table_controllers = table_controllers |
| 27 | + |
| 28 | + def get_table_names(self) -> list[str]: |
| 29 | + """Get list of available table names. |
| 30 | +
|
| 31 | + Returns: |
| 32 | + List of table names that can be searched. |
| 33 | + """ |
| 34 | + return list(self.table_controllers.keys()) |
| 35 | + |
| 36 | + def find_text( |
| 37 | + self, |
| 38 | + search_text: str, |
| 39 | + case_sensitive: bool, |
| 40 | + regex: bool, |
| 41 | + whole_cell: bool, |
| 42 | + selected_table_names: list[str], |
| 43 | + ) -> list[tuple]: |
| 44 | + """Search for text across selected tables. |
| 45 | +
|
| 46 | + Args: |
| 47 | + search_text: The text to search for |
| 48 | + case_sensitive: Whether search is case-sensitive |
| 49 | + regex: Whether to use regex matching |
| 50 | + whole_cell: Whether to match whole cell only |
| 51 | + selected_table_names: List of table names to search in |
| 52 | +
|
| 53 | + Returns: |
| 54 | + List of tuples: (row, col, table_name, controller) |
| 55 | + Each tuple represents a match with its location and associated |
| 56 | + controller. |
| 57 | + """ |
| 58 | + matches = [] |
| 59 | + |
| 60 | + for table_name in selected_table_names: |
| 61 | + controller = self.table_controllers.get(table_name) |
| 62 | + if controller is None: |
| 63 | + continue |
| 64 | + |
| 65 | + # Get matches from this table |
| 66 | + table_matches = controller.find_text( |
| 67 | + search_text, case_sensitive, regex, whole_cell |
| 68 | + ) |
| 69 | + |
| 70 | + # Extend with table name and controller reference |
| 71 | + for row, col in table_matches: |
| 72 | + matches.append((row, col, table_name, controller)) |
| 73 | + |
| 74 | + return matches |
| 75 | + |
| 76 | + def focus_match( |
| 77 | + self, table_name: str, row: int, col: int, with_focus: bool = False |
| 78 | + ): |
| 79 | + """Focus on a specific match in a table. |
| 80 | +
|
| 81 | + Args: |
| 82 | + table_name: Name of the table containing the match |
| 83 | + row: Row index of the match |
| 84 | + col: Column index of the match |
| 85 | + with_focus: Whether to give the table widget focus |
| 86 | + """ |
| 87 | + controller = self.table_controllers.get(table_name) |
| 88 | + if controller: |
| 89 | + controller.focus_match((row, col), with_focus=with_focus) |
| 90 | + |
| 91 | + def unfocus_match(self, table_name: str): |
| 92 | + """Remove focus from current match in a table. |
| 93 | +
|
| 94 | + Args: |
| 95 | + table_name: Name of the table to unfocus |
| 96 | + """ |
| 97 | + controller = self.table_controllers.get(table_name) |
| 98 | + if controller: |
| 99 | + controller.focus_match(None) |
| 100 | + |
| 101 | + def replace_text( |
| 102 | + self, |
| 103 | + table_name: str, |
| 104 | + row: int, |
| 105 | + col: int, |
| 106 | + replace_text: str, |
| 107 | + search_text: str, |
| 108 | + case_sensitive: bool, |
| 109 | + regex: bool, |
| 110 | + ): |
| 111 | + """Replace text in a specific cell. |
| 112 | +
|
| 113 | + Args: |
| 114 | + table_name: Name of the table containing the cell |
| 115 | + row: Row index |
| 116 | + col: Column index |
| 117 | + replace_text: Text to replace with |
| 118 | + search_text: Original search text (for validation) |
| 119 | + case_sensitive: Whether the original search was case-sensitive |
| 120 | + regex: Whether the original search used regex |
| 121 | + """ |
| 122 | + controller = self.table_controllers.get(table_name) |
| 123 | + if controller: |
| 124 | + controller.replace_text( |
| 125 | + row=row, |
| 126 | + col=col, |
| 127 | + replace_text=replace_text, |
| 128 | + search_text=search_text, |
| 129 | + case_sensitive=case_sensitive, |
| 130 | + regex=regex, |
| 131 | + ) |
| 132 | + |
| 133 | + def replace_all( |
| 134 | + self, |
| 135 | + search_text: str, |
| 136 | + replace_text: str, |
| 137 | + case_sensitive: bool, |
| 138 | + regex: bool, |
| 139 | + matches: list[tuple], |
| 140 | + ): |
| 141 | + """Replace all matches across tables. |
| 142 | +
|
| 143 | + Args: |
| 144 | + search_text: Text to search for |
| 145 | + replace_text: Text to replace with |
| 146 | + case_sensitive: Whether search is case-sensitive |
| 147 | + regex: Whether to use regex |
| 148 | + matches: List of match tuples from find_text() |
| 149 | + """ |
| 150 | + # Group matches by controller |
| 151 | + controllers_to_update = {} |
| 152 | + for row, col, _, controller in matches: |
| 153 | + if controller not in controllers_to_update: |
| 154 | + controllers_to_update[controller] = [] |
| 155 | + controllers_to_update[controller].append((row, col)) |
| 156 | + |
| 157 | + # Call replace_all on each unique controller |
| 158 | + for controller, positions in controllers_to_update.items(): |
| 159 | + controller.replace_all( |
| 160 | + search_text, replace_text, case_sensitive, regex |
| 161 | + ) |
| 162 | + # Emit dataChanged for each affected cell |
| 163 | + for row, col in positions: |
| 164 | + controller.model.dataChanged.emit( |
| 165 | + controller.model.index(row, col), |
| 166 | + controller.model.index(row, col), |
| 167 | + ) |
| 168 | + |
| 169 | + def cleanse_all_highlights(self): |
| 170 | + """Clear highlights from all tables.""" |
| 171 | + for controller in self.table_controllers.values(): |
| 172 | + controller.cleanse_highlighted_cells() |
| 173 | + |
| 174 | + def highlight_matches(self, matches: list[tuple]): |
| 175 | + """Highlight matches in their respective tables. |
| 176 | +
|
| 177 | + Args: |
| 178 | + matches: List of match tuples from find_text() |
| 179 | + """ |
| 180 | + # Group matches by controller |
| 181 | + by_controller = {} |
| 182 | + for row, col, _, controller in matches: |
| 183 | + if controller not in by_controller: |
| 184 | + by_controller[controller] = [] |
| 185 | + by_controller[controller].append((row, col)) |
| 186 | + |
| 187 | + # Highlight in each table |
| 188 | + for controller, positions in by_controller.items(): |
| 189 | + controller.highlight_text(positions) |
0 commit comments