From c0a34d3e140994bd78591f8f044c25a4b81d0a21 Mon Sep 17 00:00:00 2001 From: ingvar1995 Date: Wed, 22 Aug 2018 13:36:22 +0300 Subject: [PATCH] groups - nicks auto complete --- toxygen/contacts/contacts_manager.py | 16 ++++++++++++++++ toxygen/contacts/group_chat.py | 5 +++++ toxygen/ui/main_screen.py | 4 ++-- toxygen/ui/main_screen_widgets.py | 19 ++++++++++++++----- 4 files changed, 37 insertions(+), 7 deletions(-) diff --git a/toxygen/contacts/contacts_manager.py b/toxygen/contacts/contacts_manager.py index 48038ca..23dc61e 100644 --- a/toxygen/contacts/contacts_manager.py +++ b/toxygen/contacts/contacts_manager.py @@ -386,6 +386,22 @@ class ContactsManager(ToxSave): num = self._contacts.index(contact) self._delete_contact(num) + def get_gc_peer_name(self, name): + group = self.get_curr_contact() + + names = sorted(group.get_peers_names()) + if name in names: # return next nick + index = names.index(name) + index = (index + 1) % len(names) + + return names[index] + + suggested_names = list(filter(lambda x: x.startswith(name), names)) + if not len(suggested_names): + return '\t' + + return suggested_names[0] + # ----------------------------------------------------------------------------------------------------------------- # Friend requests # ----------------------------------------------------------------------------------------------------------------- diff --git a/toxygen/contacts/group_chat.py b/toxygen/contacts/group_chat.py index f173695..1ef6742 100644 --- a/toxygen/contacts/group_chat.py +++ b/toxygen/contacts/group_chat.py @@ -98,6 +98,11 @@ class GroupChat(contact.Contact, ToxSave): def remove_all_peers_except_self(self): self._peers = self._peers[:1] + def get_peers_names(self): + peers_names = map(lambda p: p.name, self._peers) + + return list(peers_names) + def get_peers(self): return self._peers[:] diff --git a/toxygen/ui/main_screen.py b/toxygen/ui/main_screen.py index afb3ef9..6217b47 100644 --- a/toxygen/ui/main_screen.py +++ b/toxygen/ui/main_screen.py @@ -1,5 +1,5 @@ from ui.contact_items import * -from ui.widgets import MultilineEdit, ComboBox +from ui.widgets import MultilineEdit from ui.main_screen_widgets import * import utils.util as util import utils.ui as util_ui @@ -37,7 +37,7 @@ class MainWindow(QtWidgets.QMainWindow): self._toxes = toxes self._messenger = messenger self._contacts_manager.active_contact_changed.add_callback(self._new_contact_selected) - self.messageEdit.set_messenger(messenger) + self.messageEdit.set_dependencies(messenger, contacts_manager) self.update_gc_invites_button_state() diff --git a/toxygen/ui/main_screen_widgets.py b/toxygen/ui/main_screen_widgets.py index 0945b0c..8b7bf52 100644 --- a/toxygen/ui/main_screen_widgets.py +++ b/toxygen/ui/main_screen_widgets.py @@ -1,6 +1,7 @@ from PyQt5 import QtCore, QtGui, QtWidgets from ui.widgets import RubberBandWindow, create_menu, QRightClickButton, CenteredWidget, LineEdit import urllib +import re import utils.util as util import utils.ui as util_ui from stickers.stickers import load_stickers @@ -11,14 +12,15 @@ class MessageArea(QtWidgets.QPlainTextEdit): def __init__(self, parent, form): super().__init__(parent) - self._messenger = None + self._messenger = self._contacts_manager = None self.parent = form self.setAcceptDrops(True) self._timer = QtCore.QTimer(self) self._timer.timeout.connect(lambda: self._messenger.send_typing(False)) - def set_messenger(self, messenger): + def set_dependencies(self, messenger, contacts_manager): self._messenger = messenger + self._contacts_manager = contacts_manager def keyPressEvent(self, event): if event.matches(QtGui.QKeySequence.Paste): @@ -39,10 +41,17 @@ class MessageArea(QtWidgets.QPlainTextEdit): self._messenger.send_message() elif event.key() == QtCore.Qt.Key_Up and not self.toPlainText(): self.appendPlainText(self._messenger.get_last_message()) - elif event.key() == QtCore.Qt.Key_Tab and not self._messenger.is_active_a_friend(): + elif event.key() == QtCore.Qt.Key_Tab and self._contacts_manager.is_active_a_group(): text = self.toPlainText() - pos = self.textCursor().position() - self.insertPlainText(self._messenger.get_gc_peer_name(text[:pos])) + text_cursor = self.textCursor() + pos = text_cursor.position() + current_word = re.split("\s+", text[:pos])[-1] + start_index = text.rindex(current_word, 0, pos) + peer_name = self._contacts_manager.get_gc_peer_name(current_word) + self.setPlainText(text[:start_index] + peer_name + text[pos:]) + new_pos = start_index + len(peer_name) + text_cursor.setPosition(new_pos, QtGui.QTextCursor.MoveAnchor) + self.setTextCursor(text_cursor) else: self._messenger.send_typing(True) if self._timer.isActive():