def search(self, text="", show_msg=True): # impl. of interface IEditor find_text = self._view.search_replace.controls["search-edit"].text() if not show_msg and text.strip(): find_text = text # pragma: no cover flags = QTextDocument.FindFlags() if self._view.search_replace.controls["cs-box"].isChecked(): flags |= QTextDocument.FindCaseSensitively old_cursor = self._view.text.textCursor() self._view.text.moveCursor(QTextCursor.Start) color = QColor(Qt.yellow).lighter(130) extra_sels = [] while self._view.text.find(find_text, options=flags): extra = QTextEdit.ExtraSelection() extra.format.setBackground(color) extra.cursor = self._view.text.textCursor() extra_sels.append(extra) self._view.text.setExtraSelections(extra_sels) if len(extra_sels) != 0: txt = self.tr("Results found") txt = f'{txt}: {len(extra_sels)}' cursor = self._view.text.textCursor() cursor.clearSelection() self._view.text.setTextCursor(cursor) else: txt = self.tr("Nothing found :(") self._view.text.setTextCursor(old_cursor) if show_msg: self._view.show_info(txt)
def _replace_all(self): find_text = self._view.search_replace.controls["search-edit"].text() repl_text = self._view.search_replace.controls["replace-edit"].text() flags = QTextDocument.FindFlags() if self._view.search_replace.controls["cs-box"].isChecked(): flags |= QTextDocument.FindCaseSensitively old_cursor = self._view.text.textCursor() self._view.text.moveCursor(QTextCursor.Start) cnt = 0 while self._view.text.find(find_text, options=flags): cnt += 1 self._view.text.setTextCursor(old_cursor) if not cnt: return txt = (self.tr("Results found: ") + str(cnt) + self.tr(". Replace everything ?")) if not yes_no(txt, self): return cnt = 0 self._view.text.moveCursor(QTextCursor.Start) while self._view.text.find(find_text, options=flags): self._doc.replace(repl_text) cnt += 1 if cnt: txt = self.tr("Replacements done") self._view.show_info(f'{txt}: {cnt}')
def _search(self, forward=False): find_text = self._view.search_replace.controls["search-edit"].text() txt = self.tr("End of document.") flags = QTextDocument.FindFlags() if self._view.search_replace.controls["cs-box"].isChecked(): flags |= QTextDocument.FindCaseSensitively if forward: flags |= QTextDocument.FindBackward txt = self.tr("Begin of document.") if not self._view.text.find(find_text, options=flags): self._view.show_info(txt) return False return True