From b8fa8df41a4091d6ecab0bf29ff6646a53c249a5 Mon Sep 17 00:00:00 2001 From: ingvar1995 Date: Sun, 20 May 2018 15:57:08 +0300 Subject: [PATCH] various fixes - peers list, resize event, tox instance recreation --- toxygen/app.py | 8 ++++-- toxygen/av/calls.py | 3 +++ toxygen/av/calls_manager.py | 3 +++ toxygen/contacts/contacts_manager.py | 15 ++++++++--- toxygen/contacts/friend_factory.py | 6 +++-- toxygen/contacts/group_chat.py | 5 ++-- toxygen/contacts/group_factory.py | 6 +++-- toxygen/contacts/profile.py | 4 +-- .../file_transfers/file_transfers_handler.py | 5 ++-- toxygen/groups/groups_service.py | 8 ++++++ toxygen/groups/peers_list.py | 8 +++--- toxygen/plugin_support/plugin_support.py | 10 +++---- toxygen/ui/group_peers_list.py | 4 +-- toxygen/ui/main_screen.py | 21 ++++++++++----- toxygen/ui/main_screen_widgets.py | 27 ++++++++++--------- toxygen/ui/menu.py | 4 +-- toxygen/ui/widgets_factory.py | 8 ++++-- toxygen/user_data/settings.py | 3 ++- 18 files changed, 97 insertions(+), 51 deletions(-) diff --git a/toxygen/app.py b/toxygen/app.py index ff86acb..2f18045 100644 --- a/toxygen/app.py +++ b/toxygen/app.py @@ -291,7 +291,11 @@ class App: self._tox = self._create_tox(data) self._start_threads() - # TODO: foreach in list of tox savers set_tox + tox_savers = [self._friend_factory, self._group_factory, self._plugin_loader, self._contacts_manager, + self._contacts_provider, self._messenger, self._file_transfer_handler, self._groups_service] + for tox_saver in tox_savers: + tox_saver.set_tox(self._tox) + self._calls_manager.set_toxav(self._tox.AV) return self._tox @@ -327,7 +331,7 @@ class App: self._groups_service = GroupsService(self._tox, self._contacts_manager, self._contacts_provider, self._ms) widgets_factory = WidgetsFactory(self._settings, profile, self._profile_manager, self._contacts_manager, self._file_transfer_handler, self._smiley_loader, self._plugin_loader, - self._toxes, self._version, self._groups_service) + self._toxes, self._version, self._groups_service, history) self._tray = tray.init_tray(profile, self._settings, self._ms, self._toxes) self._ms.set_dependencies(widgets_factory, self._tray, self._contacts_manager, self._messenger, profile, self._plugin_loader, self._file_transfer_handler, history, self._calls_manager, diff --git a/toxygen/av/calls.py b/toxygen/av/calls.py index 6f3eccf..2673f1a 100644 --- a/toxygen/av/calls.py +++ b/toxygen/av/calls.py @@ -36,6 +36,9 @@ class AV: self._video_width = 640 self._video_height = 480 + def set_toxav(self, toxav): + self._toxav = toxav + def stop(self): self._running = False self.stop_audio_thread() diff --git a/toxygen/av/calls_manager.py b/toxygen/av/calls_manager.py index 5e84573..eefd03a 100644 --- a/toxygen/av/calls_manager.py +++ b/toxygen/av/calls_manager.py @@ -18,6 +18,9 @@ class CallsManager: self._call_started_event = event.Event() # friend_number, audio, video, is_outgoing self._call_finished_event = event.Event() # friend_number, is_declined + def set_toxav(self, toxav): + self._call.set_toxav(toxav) + # ----------------------------------------------------------------------------------------------------------------- # Events # ----------------------------------------------------------------------------------------------------------------- diff --git a/toxygen/contacts/contacts_manager.py b/toxygen/contacts/contacts_manager.py index 94affd9..d0f0980 100644 --- a/toxygen/contacts/contacts_manager.py +++ b/toxygen/contacts/contacts_manager.py @@ -1,16 +1,17 @@ from contacts.friend import Friend from contacts.group_chat import GroupChat from messenger.messages import * +from common.tox_save import ToxSave -class ContactsManager: +class ContactsManager(ToxSave): """ Represents contacts list. """ def __init__(self, tox, settings, screen, profile_manager, contact_provider, history, tox_dns, messages_items_factory): - self._tox = tox + super().__init__(tox) self._settings = settings self._screen = screen self._profile_manager = profile_manager @@ -19,6 +20,7 @@ class ContactsManager: self._messages_items_factory = messages_items_factory self._messages = screen.messages self._contacts, self._active_contact = [], -1 + self._active_contact_changed = Event() self._sorting = settings['sorting'] self._filter_string = '' self._friend_item_height = 40 if settings['compact_mode'] else 70 @@ -115,13 +117,18 @@ class ContactsManager: # else: # self._screen.call_finished() self._set_current_contact_data(contact) - + self._active_contact_changed(contact) except Exception as ex: # no friend found. ignore util.log('Friend value: ' + str(value)) util.log('Error in set active: ' + str(ex)) raise - active_friend = property(get_active, set_active) + active_contact = property(get_active, set_active) + + def get_active_contact_changed(self): + return self._active_contact_changed + + active_contact_changed = property(get_active_contact_changed) def set_active_by_number_and_type(self, number, is_friend): # TODO: by id for i in range(len(self._contacts)): diff --git a/toxygen/contacts/friend_factory.py b/toxygen/contacts/friend_factory.py index a9b0477..8ebafd6 100644 --- a/toxygen/contacts/friend_factory.py +++ b/toxygen/contacts/friend_factory.py @@ -1,11 +1,13 @@ from contacts.friend import Friend +from common.tox_save import ToxSave -class FriendFactory: +class FriendFactory(ToxSave): def __init__(self, profile_manager, settings, tox, db, items_factory): + super().__init__(tox) self._profile_manager = profile_manager - self._settings, self._tox = settings, tox + self._settings = settings self._db = db self._items_factory = items_factory diff --git a/toxygen/contacts/group_chat.py b/toxygen/contacts/group_chat.py index 4d561cf..1f964c6 100644 --- a/toxygen/contacts/group_chat.py +++ b/toxygen/contacts/group_chat.py @@ -3,13 +3,14 @@ from contacts.contact_menu import GroupMenuGenerator import utils.util as util from groups.group_peer import GroupChatPeer from wrapper import toxcore_enums_and_consts as constants +from common.tox_save import ToxSave -class GroupChat(contact.Contact): +class GroupChat(contact.Contact, ToxSave): def __init__(self, tox, profile_manager, message_getter, number, name, status_message, widget, tox_id): super().__init__(profile_manager, message_getter, number, name, status_message, widget, tox_id) - self._tox = tox + ToxSave.__init__(self, tox) self.set_status(constants.TOX_USER_STATUS['NONE']) self._peers = [] self._add_self_to_gc() diff --git a/toxygen/contacts/group_factory.py b/toxygen/contacts/group_factory.py index 089fc09..8db3e9a 100644 --- a/toxygen/contacts/group_factory.py +++ b/toxygen/contacts/group_factory.py @@ -1,11 +1,13 @@ from contacts.group_chat import GroupChat +from common.tox_save import ToxSave -class GroupFactory: +class GroupFactory(ToxSave): def __init__(self, profile_manager, settings, tox, db, items_factory): + super().__init__(tox) self._profile_manager = profile_manager - self._settings, self._tox = settings, tox + self._settings = settings self._db = db self._items_factory = items_factory diff --git a/toxygen/contacts/profile.py b/toxygen/contacts/profile.py index 4e29147..469b8fe 100644 --- a/toxygen/contacts/profile.py +++ b/toxygen/contacts/profile.py @@ -67,7 +67,7 @@ class Profile(basecontact.BaseContact): # Reset # ----------------------------------------------------------------------------------------------------------------- - def _restart(self): + def restart(self): """ Recreate tox instance """ @@ -80,6 +80,6 @@ class Profile(basecontact.BaseContact): contacts = self._contacts_provider.get_all() if self.status is None or all(list(map(lambda x: x.status is None, contacts))) and len(contacts): self._waiting_for_reconnection = True - self._restart() + self.restart() self._timer = threading.Timer(50, self._reconnect) self._timer.start() diff --git a/toxygen/file_transfers/file_transfers_handler.py b/toxygen/file_transfers/file_transfers_handler.py index 1f5c9d0..64a2bfe 100644 --- a/toxygen/file_transfers/file_transfers_handler.py +++ b/toxygen/file_transfers/file_transfers_handler.py @@ -1,12 +1,13 @@ from messenger.messages import * from ui.contact_items import * import utils.util as util +from common.tox_save import ToxSave -class FileTransfersHandler: +class FileTransfersHandler(ToxSave): def __init__(self, tox, settings, contact_provider, file_transfers_message_service, profile): - self._tox = tox + super().__init__(tox) self._settings = settings self._contact_provider = contact_provider self._file_transfers_message_service = file_transfers_message_service diff --git a/toxygen/groups/groups_service.py b/toxygen/groups/groups_service.py index 01c1f44..c9960fc 100644 --- a/toxygen/groups/groups_service.py +++ b/toxygen/groups/groups_service.py @@ -11,6 +11,11 @@ class GroupsService(tox_save.ToxSave): self._contacts_provider = contacts_provider self._peers_list_widget = main_screen.peers_list + def set_tox(self, tox): + super().set_tox(tox) + for group in self._get_all_groups(): + group.set_tox(tox) + # ----------------------------------------------------------------------------------------------------------------- # Groups creation # ----------------------------------------------------------------------------------------------------------------- @@ -81,3 +86,6 @@ class GroupsService(tox_save.ToxSave): def _get_friend(self, friend_number): return self._contacts_provider.get_friend_by_number(friend_number) + + def _get_all_groups(self): + return self._contacts_provider.get_all_groups() diff --git a/toxygen/groups/peers_list.py b/toxygen/groups/peers_list.py index e1db064..21995b4 100644 --- a/toxygen/groups/peers_list.py +++ b/toxygen/groups/peers_list.py @@ -2,6 +2,8 @@ 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 * + # ----------------------------------------------------------------------------------------------------------------- # Builder @@ -47,16 +49,16 @@ class PeerListBuilder: return parent def _add_peer_item(self, peer, parent): - item = PeerItem(peer, self._handler, parent.width()) + item = PeerItem(peer, self._handler, parent.width(), parent) self._add_item(parent, item) def _add_peer_type_item(self, text, parent): - item = PeerTypeItem(text, parent.width()) + item = PeerTypeItem(text, parent.width(), parent) self._add_item(parent, item) @staticmethod def _add_item(parent, item): - elem = QtWidgets.QListWidgetItem() + elem = QtWidgets.QListWidgetItem(parent) elem.setSizeHint(QtCore.QSize(parent.width(), item.height())) parent.addItem(elem) parent.setItemWidget(elem, item) diff --git a/toxygen/plugin_support/plugin_support.py b/toxygen/plugin_support/plugin_support.py index 5871afa..e8c9a56 100644 --- a/toxygen/plugin_support/plugin_support.py +++ b/toxygen/plugin_support/plugin_support.py @@ -1,28 +1,26 @@ import utils.util as util -from contacts import profile import os import importlib import inspect import plugins.plugin_super_class as pl -from user_data import toxes import sys +from common.tox_save import ToxSave -class PluginLoader(): +class PluginLoader(ToxSave): def __init__(self, tox, toxes, profile, settings): - super().__init__() + super().__init__(tox) self._profile = profile self._settings = settings self._plugins = {} # dict. key - plugin unique short name, value - tuple (plugin instance, is active) - self._tox = tox self._toxes = toxes def set_tox(self, tox): """ New tox instance """ - self._tox = tox + super().set_tox(tox) for value in self._plugins.values(): value[0].set_tox(tox) diff --git a/toxygen/ui/group_peers_list.py b/toxygen/ui/group_peers_list.py index a648c04..9d2632d 100644 --- a/toxygen/ui/group_peers_list.py +++ b/toxygen/ui/group_peers_list.py @@ -8,7 +8,7 @@ class PeerItem(QtWidgets.QWidget): super().__init__(parent) self.resize(QtCore.QSize(width, 34)) self.nameLabel = DataLabel(self) - self.nameLabel.setGeometry(0, 0, width, 34) + self.nameLabel.setGeometry(5, 0, width - 5, 34) name = peer.name if peer.is_current_user: name += util_ui.tr(' (You)') @@ -29,5 +29,5 @@ class PeerTypeItem(QtWidgets.QWidget): super().__init__(parent) self.resize(QtCore.QSize(width, 34)) self.nameLabel = DataLabel(self) - self.nameLabel.setGeometry(0, 0, width, 34) + self.nameLabel.setGeometry(5, 0, width - 5, 34) self.nameLabel.setText(text) diff --git a/toxygen/ui/main_screen.py b/toxygen/ui/main_screen.py index 81c4f15..c32d0fa 100644 --- a/toxygen/ui/main_screen.py +++ b/toxygen/ui/main_screen.py @@ -33,6 +33,7 @@ class MainWindow(QtWidgets.QMainWindow): self._history_loader = history_loader self._calls_manager = calls_manager self._groups_service = groups_service + self._contacts_manager.active_contact_changed.add_callback(self._new_contact_selected) self.messageEdit.set_messenger(messenger) def show(self): @@ -284,6 +285,7 @@ class MainWindow(QtWidgets.QMainWindow): pixmap = QtGui.QPixmap(util.join_path(util.get_images_directory(), 'menu.png')) icon = QtGui.QIcon(pixmap) self.groupMenuButton.setIcon(icon) + self.groupMenuButton.setIconSize(QtCore.QSize(45, 60)) self.update_call_state('call') self.typing = QtWidgets.QLabel(Form) self.typing.setGeometry(QtCore.QRect(500, 25, 50, 30)) @@ -331,6 +333,8 @@ class MainWindow(QtWidgets.QMainWindow): self.peers_list.setSpacing(1) self.peers_list.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAsNeeded) self.peers_list.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff) + self.peers_list.verticalScrollBar().setContextMenuPolicy(QtCore.Qt.NoContextMenu) + self.peers_list.setSelectionMode(QtWidgets.QAbstractItemView.NoSelection) def initUI(self): self.setMinimumSize(920, 500) @@ -431,6 +435,7 @@ 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() @@ -564,7 +569,7 @@ class MainWindow(QtWidgets.QMainWindow): def send_smiley(self): self.menu.hide() - if self._contacts_manager.active_friend + 1: + if self._contacts_manager.get_curr_contact() is not None: self.smiley = self._widget_factory.create_smiley_window(self) self.smiley.setGeometry(QtCore.QRect(self.x() if self._settings['mirror_mode'] else 270 + self.x(), self.y() + self.height() - 200, @@ -687,11 +692,8 @@ class MainWindow(QtWidgets.QMainWindow): def friend_click(self, index): num = index.row() - self._contacts_manager.set_active(num) + self._contacts_manager.active_contact = num self.groupMenuButton.setVisible(not self._contacts_manager.is_active_a_friend()) - if self._should_show_group_peers_list: - self._toggle_gc_peers_list() - self.resizeEvent() def mouseReleaseEvent(self, event): pos = self.connection_status.pos() @@ -711,14 +713,21 @@ class MainWindow(QtWidgets.QMainWindow): return if self._contacts_manager.get_curr_friend() is None: return - self.search_field = SearchScreen(self.messages, self.messages.width(), self.messages.parent()) + self.search_field = self._widget_factory.create_search_screen(self.messages) x, y = self.messages.x(), self.messages.y() + self.messages.height() - 40 self.search_field.setGeometry(x, y, self.messages.width(), 40) self.messages.setGeometry(x, self.messages.y(), self.messages.width(), self.messages.height() - 40) + if self._should_show_group_peers_list: + self.peers_list.setFixedHeight(self.peers_list.height() - 40) self.search_field.show() def _toggle_gc_peers_list(self): self._should_show_group_peers_list = not self._should_show_group_peers_list + self.resizeEvent() if self._should_show_group_peers_list: self._groups_service.generate_peers_list() + + def _new_contact_selected(self, contact): + if self._should_show_group_peers_list: + self._toggle_gc_peers_list() self.resizeEvent() diff --git a/toxygen/ui/main_screen_widgets.py b/toxygen/ui/main_screen_widgets.py index dfd5ea7..479575e 100644 --- a/toxygen/ui/main_screen_widgets.py +++ b/toxygen/ui/main_screen_widgets.py @@ -374,8 +374,10 @@ class ClickableLabel(QtWidgets.QLabel): class SearchScreen(QtWidgets.QWidget): - def __init__(self, messages, width, *args): + def __init__(self, contacts_manager, history_loader, messages, width, *args): super().__init__(*args) + self._contacts_manager = contacts_manager + self._history_loader = history_loader self.setMaximumSize(width, 40) self.setMinimumSize(width, 40) self._messages = messages @@ -426,24 +428,24 @@ class SearchScreen(QtWidgets.QWidget): self.search_text.setFocus() def search(self): - Profile.get_instance().update() + self._contacts_manager.update() text = self.search_text.text() - friend = Profile.get_instance().get_curr_friend() - if text and friend and util.is_re_valid(text): - index = friend.search_string(text) + contact = self._contacts_manager.get_curr_contact() + if text and contact and util.is_re_valid(text): + index = contact.search_string(text) self.load_messages(index) def prev(self): - friend = Profile.get_instance().get_curr_friend() - if friend is not None: - index = friend.search_prev() + contact = self._contacts_manager.get_curr_contact() + if contact is not None: + index = contact.search_prev() self.load_messages(index) def next(self): - friend = Profile.get_instance().get_curr_friend() + contact = self._contacts_manager.get_curr_contact() text = self.search_text.text() - if friend is not None: - index = friend.search_next() + if contact is not None: + index = contact.search_next() if index is not None: count = self._messages.count() index += count @@ -456,10 +458,9 @@ class SearchScreen(QtWidgets.QWidget): def load_messages(self, index): text = self.search_text.text() if index is not None: - profile = Profile.get_instance() count = self._messages.count() while count + index < 0: - profile.load_history() + self._history_loader.load_history() count = self._messages.count() index += count item = self._messages.item(index) diff --git a/toxygen/ui/menu.py b/toxygen/ui/menu.py index e98405d..2f55bb5 100644 --- a/toxygen/ui/menu.py +++ b/toxygen/ui/menu.py @@ -281,7 +281,7 @@ class NetworkSettings(CenteredWidget): def __init__(self, settings, reset): super().__init__() self._settings = settings - self.reset = reset + self._reset = reset self.initUI() self.center() @@ -360,7 +360,7 @@ class NetworkSettings(CenteredWidget): self._settings['download_nodes_list'] = self.nodes.isChecked() self._settings.save() # recreate tox instance - self._profile.reset() + self._reset() self.close() except Exception as ex: log('Exception in restart: ' + str(ex)) diff --git a/toxygen/ui/widgets_factory.py b/toxygen/ui/widgets_factory.py index 66aca2c..4c4f1c6 100644 --- a/toxygen/ui/widgets_factory.py +++ b/toxygen/ui/widgets_factory.py @@ -6,7 +6,7 @@ from ui.groups_widgets import * class WidgetsFactory: def __init__(self, settings, profile, profile_manager, contacts_manager, file_transfer_handler, smiley_loader, - plugin_loader, toxes, version, groups_service): + plugin_loader, toxes, version, groups_service, history): self._settings = settings self._profile = profile self._profile_manager = profile_manager @@ -17,6 +17,7 @@ class WidgetsFactory: self._toxes = toxes self._version = version self._groups_service = groups_service + self._history = history def create_screenshot_window(self, *args): return ScreenShotWindow(self._file_transfer_handler, self._contacts_manager, *args) @@ -31,7 +32,7 @@ class WidgetsFactory: return ProfileSettings(self._profile, self._profile_manager, self._settings, self._toxes) def create_network_settings_window(self): - return NetworkSettings(self._settings, self._profile.reset) + return NetworkSettings(self._settings, self._profile.restart) def create_audio_settings_window(self): return AudioSettings(self._settings) @@ -71,3 +72,6 @@ class WidgetsFactory: def create_join_group_screen_window(self): return JoinGroupScreen(self._groups_service) + + def create_search_screen(self, messages): + return SearchScreen(self._contacts_manager, self._history, messages, messages.parent()) diff --git a/toxygen/user_data/settings.py b/toxygen/user_data/settings.py index 1d7329e..76a37c0 100644 --- a/toxygen/user_data/settings.py +++ b/toxygen/user_data/settings.py @@ -146,7 +146,8 @@ class Settings(dict): 'font': 'Times New Roman', 'update': 1, 'group_notifications': True, - 'download_nodes_list': False + 'download_nodes_list': False, + 'notify_all_gc': False } @staticmethod