peers - more callback and peers list refactoring

This commit is contained in:
ingvar1995 2018-05-20 17:22:44 +03:00
parent b8fa8df41a
commit c6b67452ed
6 changed files with 53 additions and 27 deletions

View File

@ -144,6 +144,9 @@ class ContactsManager(ToxSave):
def is_active_a_friend(self):
return type(self.get_curr_contact()) is Friend
def is_active_a_group(self):
return type(self.get_curr_contact()) is GroupChat
# -----------------------------------------------------------------------------------------------------------------
# Filtration
# -----------------------------------------------------------------------------------------------------------------

View File

@ -41,6 +41,10 @@ class GroupChat(contact.Contact, ToxSave):
is_current_user)
self._peers.append(peer)
def remove_peer(self, peer_id):
peer = self.get_peer_by_id(peer_id)
self._peers.remove(peer)
def get_peer_by_id(self, peer_id):
peers = list(filter(lambda p: p.id == peer_id, self._peers))

View File

@ -68,6 +68,8 @@ class GroupsService(tox_save.ToxSave):
# -----------------------------------------------------------------------------------------------------------------
def generate_peers_list(self):
if not self._contacts_manager.is_active_a_group():
return
group = self._contacts_manager.get_curr_contact()
PeersListGenerator().generate(group.peers, self, self._peers_list_widget, group.tox_id)

View File

@ -1,6 +1,4 @@
from PyQt5 import QtWidgets, QtCore
from ui.group_peers_list import PeerItem, PeerTypeItem
import utils.ui as util_ui
from wrapper.toxcore_enums_and_consts import *
from ui.widgets import *
@ -35,18 +33,16 @@ class PeerListBuilder:
return self
def build(self, parent):
parent.clear()
def build(self, list_widget):
list_widget.clear()
for i in range(self._index):
if i in self._peers:
peer = self._peers[i]
self._add_peer_item(peer, parent)
self._add_peer_item(peer, list_widget)
else:
title = self._titles[i]
self._add_peer_type_item(title, parent)
return parent
self._add_peer_type_item(title, list_widget)
def _add_peer_item(self, peer, parent):
item = PeerItem(peer, self._handler, parent.width(), parent)
@ -75,7 +71,7 @@ class PeerListBuilder:
class PeersListGenerator:
@staticmethod
def generate(peers_list, groups_service, parent, chat_id):
def generate(peers_list, groups_service, list_widget, chat_id):
admin_title = util_ui.tr('Administrator')
moderators_title = util_ui.tr('Moderators')
users_title = util_ui.tr('Users')
@ -87,15 +83,22 @@ class PeersListGenerator:
observers = list(filter(lambda p: p.role == TOX_GROUP_ROLE['OBSERVER'], peers_list))
builder = (PeerListBuilder()
.with_click_handler(lambda peer_id: groups_service.peer_selected(chat_id, peer_id))
.with_title(admin_title)
.with_peers(admins)
.with_title(moderators_title)
.with_peers(moderators)
.with_title(users_title)
.with_peers(users)
.with_title(observers_title)
.with_peers(observers)
)
.with_click_handler(lambda peer_id: groups_service.peer_selected(chat_id, peer_id)))
if len(admins):
(builder
.with_title(admin_title)
.with_peers(admins))
if len(moderators):
(builder
.with_title(moderators_title)
.with_peers(moderators))
if len(users):
(builder
.with_title(users_title)
.with_peers(users))
if len(observers):
(builder
.with_title(observers_title)
.with_peers(observers))
return builder.build(parent)
builder.build(list_widget)

View File

@ -401,28 +401,40 @@ def group_self_join(contacts_provider, groups_service):
return wrapped
def group_peer_join(contacts_provider):
def group_peer_join(contacts_provider, groups_service):
def wrapped(tox, group_number, peer_id, user_data):
group = contacts_provider.get_group_by_number(group_number)
group.add_peer(peer_id)
invoke_in_main_thread(groups_service.generate_peers_list)
return wrapped
def group_peer_name(contacts_provider):
def group_peer_exit(contacts_provider, groups_service):
def wrapped(tox, group_number, peer_id, message, length, user_data):
group = contacts_provider.get_group_by_number(group_number)
group.remove_peer(peer_id)
invoke_in_main_thread(groups_service.generate_peers_list)
return wrapped
def group_peer_name(contacts_provider, groups_service):
def wrapped(tox, group_number, peer_id, name, length, user_data):
group = contacts_provider.get_group_by_number(group_number)
peer = group.get_peer_by_id(peer_id)
peer.name = str(name[:length])
invoke_in_main_thread(groups_service.generate_peers_list)
return wrapped
def group_peer_status(contacts_provider):
def group_peer_status(contacts_provider, groups_service):
def wrapped(tox, group_number, peer_id, peer_status, user_data):
group = contacts_provider.get_group_by_number(group_number)
peer = group.get_peer_by_id(peer_id)
peer.status = peer_status
invoke_in_main_thread(groups_service.generate_peers_list)
return wrapped
@ -456,6 +468,8 @@ def init_callbacks(tox, profile, settings, plugin_loader, contacts_manager,
:param main_window: MainWindow instance
:param tray: tray (for notifications)
:param messenger: Messenger instance
:param groups_service: GroupsService instance
:param contacts_provider: ContactsProvider instance
"""
# self callbacks
tox.callback_self_connection_status(self_connection_status(tox, profile), 0)
@ -493,7 +507,8 @@ def init_callbacks(tox, profile, settings, plugin_loader, contacts_manager,
tox.callback_group_message(group_message(main_window, tray, tox, messenger, settings, profile), 0)
tox.callback_group_invite(group_invite(groups_service), 0)
tox.callback_group_self_join(group_self_join(contacts_provider, groups_service), 0)
tox.callback_group_peer_join(group_peer_join(contacts_provider), 0)
tox.callback_group_peer_name(group_peer_name(contacts_provider), 0)
tox.callback_group_peer_status(group_peer_status(contacts_provider), 0)
tox.callback_group_peer_join(group_peer_join(contacts_provider, groups_service), 0)
tox.callback_group_peer_exit(group_peer_exit(contacts_provider, groups_service), 0)
tox.callback_group_peer_name(group_peer_name(contacts_provider, groups_service), 0)
tox.callback_group_peer_status(group_peer_status(contacts_provider, groups_service), 0)
tox.callback_group_topic(group_topic(contacts_provider), 0)

View File

@ -435,7 +435,6 @@ class MainWindow(QtWidgets.QMainWindow):
self.account_name.setGeometry(QtCore.QRect(100, 15, self.width() - 560, 25))
self.account_status.setGeometry(QtCore.QRect(100, 35, self.width() - 560, 25))
self.messageEdit.setFocus()
self._contacts_manager.update()
def keyPressEvent(self, event):
key, modifiers = event.key(), event.modifiers()