friend menu fixes - correct ordering, submenus fixes

This commit is contained in:
ingvar1995 2018-05-17 16:59:46 +03:00
parent 9294c3e779
commit bcefe9bc79

View file

@ -6,16 +6,19 @@ import utils.ui as util_ui
# Builder # Builder
# ----------------------------------------------------------------------------------------------------------------- # -----------------------------------------------------------------------------------------------------------------
def _create_menu(menu_name): def _create_menu(menu_name, parent):
return QtWidgets.QMenu(menu_name or '') menu_name = menu_name or ''
return QtWidgets.QMenu(menu_name) if parent is None else parent.addMenu(menu_name)
class ContactMenuBuilder: class ContactMenuBuilder:
def __init__(self): def __init__(self):
self._actions = [] self._actions = {}
self._submenus = [] self._submenus = {}
self._name = None self._name = None
self._index = 0
def with_name(self, name): def with_name(self, name):
self._name = name self._name = name
@ -23,40 +26,49 @@ class ContactMenuBuilder:
return self return self
def with_action(self, text, handler): def with_action(self, text, handler):
self._actions.append((text, handler)) self._add_action(text, handler)
return self return self
def with_actions(self, actions): def with_actions(self, actions):
self._actions.extend(actions) for action in actions:
self._add_action(*action)
return self return self
def with_submenu(self, submenu): def with_submenu(self, submenu_builder):
self._add_submenu(submenu) self._add_submenu(submenu_builder)
return self return self
def with_optional_submenu(self, submenu): def with_optional_submenu(self, submenu_builder):
if submenu is not None: if submenu_builder is not None:
self._add_submenu(submenu) self._add_submenu(submenu_builder)
return self return self
def build(self): # TODO: actions order def build(self, parent=None):
menu = _create_menu(self._name) menu = _create_menu(self._name, parent)
for text, handler in self._actions: for i in range(self._index):
action = menu.addAction(text) if i in self._actions:
action.triggered.connect(handler) text, handler = self._actions[i]
action = menu.addAction(text)
for submenu in self._submenus: action.triggered.connect(handler)
menu.addMenu(submenu) else:
submenu_builder = self._submenus[i]
submenu = submenu_builder.build(menu)
menu.addMenu(submenu)
return menu return menu
def _add_submenu(self, submenu): def _add_submenu(self, submenu):
self._submenus.append(submenu) self._submenus[self._index] = submenu
self._index += 1
def _add_action(self, text, handler):
self._actions[self._index] = (text, handler)
self._index += 1
# ----------------------------------------------------------------------------------------------------------------- # -----------------------------------------------------------------------------------------------------------------
# Generators # Generators
@ -75,9 +87,9 @@ class BaseContactMenuGenerator:
class FriendMenuGenerator(BaseContactMenuGenerator): class FriendMenuGenerator(BaseContactMenuGenerator):
def generate(self, plugin_loader, contacts_manager, main_screen, settings, number): def generate(self, plugin_loader, contacts_manager, main_screen, settings, number):
history_menu = self._generate_history_menu(main_screen, number) history_menu_builder = self._generate_history_menu_builder(main_screen, number)
copy_menu = self._generate_copy_menu(main_screen) copy_menu_builder = self._generate_copy_menu_builder(main_screen)
plugins_menu = self._generate_plugins_menu(plugin_loader, number) plugins_menu_builder = self._generate_plugins_menu_builder(plugin_loader, number)
allowed = self._contact.tox_id in settings['auto_accept_from_friends'] allowed = self._contact.tox_id in settings['auto_accept_from_friends']
auto = util_ui.tr('Disallow auto accept') if allowed else util_ui.tr('Allow auto accept') auto = util_ui.tr('Disallow auto accept') if allowed else util_ui.tr('Allow auto accept')
@ -85,14 +97,13 @@ class FriendMenuGenerator(BaseContactMenuGenerator):
builder = ContactMenuBuilder() builder = ContactMenuBuilder()
menu = (builder menu = (builder
.with_action(util_ui.tr('Set alias'), lambda: main_screen.set_alias(number)) .with_action(util_ui.tr('Set alias'), lambda: main_screen.set_alias(number))
.with_action(util_ui.tr('Chat history'), lambda: main_screen.clear_history(number)) .with_submenu(history_menu_builder)
.with_submenu(history_menu) .with_submenu(copy_menu_builder)
.with_submenu(copy_menu)
.with_action(auto, lambda: main_screen.auto_accept(number, not allowed)) .with_action(auto, lambda: main_screen.auto_accept(number, not allowed))
.with_action(util_ui.tr('Remove friend'), lambda: main_screen.remove_friend(number)) .with_action(util_ui.tr('Remove friend'), lambda: main_screen.remove_friend(number))
.with_action(util_ui.tr('Block friend'), lambda: main_screen.block_friend(number)) .with_action(util_ui.tr('Block friend'), lambda: main_screen.block_friend(number))
.with_action(util_ui.tr('Notes'), lambda: main_screen.show_note(self._contact)) .with_action(util_ui.tr('Notes'), lambda: main_screen.show_note(self._contact))
.with_optional_submenu(plugins_menu) .with_optional_submenu(plugins_menu_builder)
).build() ).build()
return menu return menu
@ -102,42 +113,42 @@ class FriendMenuGenerator(BaseContactMenuGenerator):
# ----------------------------------------------------------------------------------------------------------------- # -----------------------------------------------------------------------------------------------------------------
@staticmethod @staticmethod
def _generate_history_menu(main_screen, number): def _generate_history_menu_builder(main_screen, number):
history_menu_builder = ContactMenuBuilder() history_menu_builder = ContactMenuBuilder()
history_menu = (history_menu_builder (history_menu_builder
.with_name(util_ui.tr('Chat history')) .with_name(util_ui.tr('Chat history'))
.with_action(util_ui.tr('Clear history'), lambda: main_screen.clear_history(number)) .with_action(util_ui.tr('Clear history'), lambda: main_screen.clear_history(number))
.with_action(util_ui.tr('Export as text'), lambda: main_screen.export_history(number)) .with_action(util_ui.tr('Export as text'), lambda: main_screen.export_history(number))
.with_action(util_ui.tr('Export as HTML'), lambda: main_screen.export_history(number, False)) .with_action(util_ui.tr('Export as HTML'), lambda: main_screen.export_history(number, False))
).build() )
return history_menu return history_menu_builder
def _generate_copy_menu(self, main_screen): def _generate_copy_menu_builder(self, main_screen):
copy_menu_builder = ContactMenuBuilder() copy_menu_builder = ContactMenuBuilder()
copy_menu = (copy_menu_builder (copy_menu_builder
.with_name(util_ui.tr('Copy')) .with_name(util_ui.tr('Copy'))
.with_action(util_ui.tr('Name'), lambda: main_screen.copy_text(self._contact.name)) .with_action(util_ui.tr('Name'), lambda: main_screen.copy_text(self._contact.name))
.with_action(util_ui.tr('Status message'), lambda: main_screen.copy_text(self._contact.name)) .with_action(util_ui.tr('Status message'), lambda: main_screen.copy_text(self._contact.status_message))
.with_action(util_ui.tr('Public key'), lambda: main_screen.copy_text(self._contact.tox_id)) .with_action(util_ui.tr('Public key'), lambda: main_screen.copy_text(self._contact.tox_id))
).build() )
return copy_menu return copy_menu_builder
@staticmethod @staticmethod
def _generate_plugins_menu(plugin_loader, number): def _generate_plugins_menu_builder(plugin_loader, number):
if plugin_loader is None: if plugin_loader is None:
return None return None
plugins_actions = plugin_loader.get_menu(number) plugins_actions = plugin_loader.get_menu(number)
if not len(plugins_actions): if not len(plugins_actions):
return None return None
plugins_menu_builder = ContactMenuBuilder() plugins_menu_builder = ContactMenuBuilder()
plugins_menu = (plugins_menu_builder (plugins_menu_builder
.with_name(util_ui.tr('Plugins')) .with_name(util_ui.tr('Plugins'))
.with_actions(plugins_actions) .with_actions(plugins_actions)
).build() )
return plugins_menu return plugins_menu_builder
def _generate_groups_menu(self, contacts_manager): # TODO: fix def _generate_groups_menu(self, contacts_manager): # TODO: fix
chats = contacts_manager.get_group_chats() chats = contacts_manager.get_group_chats()