search in history with regex support

This commit is contained in:
ingvar1995 2017-02-12 19:27:38 +03:00
parent 8b56184510
commit de7f3359b8
4 changed files with 13 additions and 8 deletions

View File

@ -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

View File

@ -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('<font color="red">{}</font>'.format(text), tmp)
strings = re.findall(text, tmp, flags=re.IGNORECASE)
for s in strings:
tmp = tmp.replace(s, '<font color="red">{}</font>'.format(s))
self.message.setHtml(tmp)

View File

@ -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)

View File

@ -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