From de7f3359b88cb801ff2ebd08ab6819410ac33dc7 Mon Sep 17 00:00:00 2001 From: ingvar1995 Date: Sun, 12 Feb 2017 19:27:38 +0300 Subject: [PATCH] search in history with regex support --- toxygen/contact.py | 8 ++++++-- toxygen/list_items.py | 5 +++-- toxygen/mainscreen_widgets.py | 6 +++--- toxygen/profile.py | 2 +- 4 files changed, 13 insertions(+), 8 deletions(-) diff --git a/toxygen/contact.py b/toxygen/contact.py index 41628c3..398638f 100644 --- a/toxygen/contact.py +++ b/toxygen/contact.py @@ -7,6 +7,7 @@ import basecontact import util from messages import * import file_transfers as ft +import re class Contact(basecontact.BaseContact): @@ -131,6 +132,7 @@ class Contact(basecontact.BaseContact): self._unsaved_messages -= 1 self._corr.remove(elem) self._message_getter.delete_one() + self._search_index = 0 def delete_old_messages(self): """ @@ -176,7 +178,8 @@ class Contact(basecontact.BaseContact): for i in range(self._search_index - 1, -l - 1, -1): if type(self._corr[i]) is not TextMessage: continue - if self._search_string.lower() in self._corr[i].get_data()[0].lower(): + message = self._corr[i].get_data()[0] + if re.search(self._search_string, message, re.IGNORECASE) is not None: self._search_index = i return i self._search_index = -l @@ -190,7 +193,8 @@ class Contact(basecontact.BaseContact): for i in range(self._search_index + 1, 0): if type(self._corr[i]) is not TextMessage: continue - if self._search_string.lower() in self._corr[i].get_data()[0].lower(): + message = self._corr[i].get_data()[0] + if re.search(self._search_string, message, re.IGNORECASE) is not None: self._search_index = i return i return None # not found diff --git a/toxygen/list_items.py b/toxygen/list_items.py index 9b84e13..90f7ed9 100644 --- a/toxygen/list_items.py +++ b/toxygen/list_items.py @@ -192,8 +192,9 @@ class MessageItem(QtGui.QWidget): def select_text(self, text=''): tmp = self.message.toHtml() - pattern = re.compile(re.escape(text), re.IGNORECASE) # TODO: save case - tmp = pattern.sub('{}'.format(text), tmp) + strings = re.findall(text, tmp, flags=re.IGNORECASE) + for s in strings: + tmp = tmp.replace(s, '{}'.format(s)) self.message.setHtml(tmp) diff --git a/toxygen/mainscreen_widgets.py b/toxygen/mainscreen_widgets.py index e277dc4..6fbfdad 100644 --- a/toxygen/mainscreen_widgets.py +++ b/toxygen/mainscreen_widgets.py @@ -449,14 +449,14 @@ class SearchScreen(QtGui.QWidget): self.close_button.setGeometry(width - 40, 0, 40, 40) self.close_button.clicked.connect(self.close) self.close_button.setText('×') - self.close_button.setAlignment(QtCore.Qt.AlignCenter) font = QtGui.QFont() font.setPointSize(32) font.setBold(True) self.close_button.setFont(font) def search(self): - text = self.search_text.text() # TODO: clean selection + Profile.get_instance().update() + text = self.search_text.text() friend = Profile.get_instance().get_curr_friend() if text and friend: index = friend.search_string(text) @@ -494,6 +494,6 @@ class SearchScreen(QtGui.QWidget): self._messages.itemWidget(item).select_text(text) def closeEvent(self, *args): - Profile.get_instance().update() # TODO: clean selection? + Profile.get_instance().update() self._messages.setGeometry(0, 0, self._messages.width(), self._messages.height() + 40) super().closeEvent(*args) diff --git a/toxygen/profile.py b/toxygen/profile.py index d8c6d3c..c4df1a7 100644 --- a/toxygen/profile.py +++ b/toxygen/profile.py @@ -1205,7 +1205,7 @@ class Profile(basecontact.BaseContact, Singleton): else: text = QtGui.QApplication.translate("incoming_call", "Outgoing audio call", None, QtGui.QApplication.UnicodeUTF8) - self.get_curr_friend().append_message(InfoMessage(text, time.time())) + self.get_curr_friend().append_message(InfoMessage(text, time.time())) self.create_message_item(text, time.time(), '', MESSAGE_TYPE['INFO_MESSAGE']) self._messages.scrollToBottom() elif num in self._call: # finish or cancel call if you call with active friend