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
# -----------------------------------------------------------------------------------------------------------------
def _create_menu(menu_name):
return QtWidgets.QMenu(menu_name or '')
def _create_menu(menu_name, parent):
menu_name = menu_name or ''
return QtWidgets.QMenu(menu_name) if parent is None else parent.addMenu(menu_name)
class ContactMenuBuilder:
def __init__(self):
self._actions = []
self._submenus = []
self._actions = {}
self._submenus = {}
self._name = None
self._index = 0
def with_name(self, name):
self._name = name
@ -23,40 +26,49 @@ class ContactMenuBuilder:
return self
def with_action(self, text, handler):
self._actions.append((text, handler))
self._add_action(text, handler)
return self
def with_actions(self, actions):
self._actions.extend(actions)
for action in actions:
self._add_action(*action)
return self
def with_submenu(self, submenu):
self._add_submenu(submenu)
def with_submenu(self, submenu_builder):
self._add_submenu(submenu_builder)
return self
def with_optional_submenu(self, submenu):
if submenu is not None:
self._add_submenu(submenu)
def with_optional_submenu(self, submenu_builder):
if submenu_builder is not None:
self._add_submenu(submenu_builder)
return self
def build(self): # TODO: actions order
menu = _create_menu(self._name)
def build(self, parent=None):
menu = _create_menu(self._name, parent)
for text, handler in self._actions:
action = menu.addAction(text)
action.triggered.connect(handler)
for submenu in self._submenus:
menu.addMenu(submenu)
for i in range(self._index):
if i in self._actions:
text, handler = self._actions[i]
action = menu.addAction(text)
action.triggered.connect(handler)
else:
submenu_builder = self._submenus[i]
submenu = submenu_builder.build(menu)
menu.addMenu(submenu)
return menu
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
@ -75,9 +87,9 @@ class BaseContactMenuGenerator:
class FriendMenuGenerator(BaseContactMenuGenerator):
def generate(self, plugin_loader, contacts_manager, main_screen, settings, number):
history_menu = self._generate_history_menu(main_screen, number)
copy_menu = self._generate_copy_menu(main_screen)
plugins_menu = self._generate_plugins_menu(plugin_loader, number)
history_menu_builder = self._generate_history_menu_builder(main_screen, number)
copy_menu_builder = self._generate_copy_menu_builder(main_screen)
plugins_menu_builder = self._generate_plugins_menu_builder(plugin_loader, number)
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')
@ -85,14 +97,13 @@ class FriendMenuGenerator(BaseContactMenuGenerator):
builder = ContactMenuBuilder()
menu = (builder
.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)
.with_submenu(copy_menu)
.with_submenu(history_menu_builder)
.with_submenu(copy_menu_builder)
.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('Block friend'), lambda: main_screen.block_friend(number))
.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()
return menu
@ -102,42 +113,42 @@ class FriendMenuGenerator(BaseContactMenuGenerator):
# -----------------------------------------------------------------------------------------------------------------
@staticmethod
def _generate_history_menu(main_screen, number):
def _generate_history_menu_builder(main_screen, number):
history_menu_builder = ContactMenuBuilder()
history_menu = (history_menu_builder
.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('Export as text'), lambda: main_screen.export_history(number))
.with_action(util_ui.tr('Export as HTML'), lambda: main_screen.export_history(number, False))
).build()
(history_menu_builder
.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('Export as text'), lambda: main_screen.export_history(number))
.with_action(util_ui.tr('Export as HTML'), lambda: main_screen.export_history(number, False))
)
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 = (copy_menu_builder
.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('Status message'), lambda: main_screen.copy_text(self._contact.name))
.with_action(util_ui.tr('Public key'), lambda: main_screen.copy_text(self._contact.tox_id))
).build()
(copy_menu_builder
.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('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))
)
return copy_menu
return copy_menu_builder
@staticmethod
def _generate_plugins_menu(plugin_loader, number):
def _generate_plugins_menu_builder(plugin_loader, number):
if plugin_loader is None:
return None
plugins_actions = plugin_loader.get_menu(number)
if not len(plugins_actions):
return None
plugins_menu_builder = ContactMenuBuilder()
plugins_menu = (plugins_menu_builder
.with_name(util_ui.tr('Plugins'))
.with_actions(plugins_actions)
).build()
(plugins_menu_builder
.with_name(util_ui.tr('Plugins'))
.with_actions(plugins_actions)
)
return plugins_menu
return plugins_menu_builder
def _generate_groups_menu(self, contacts_manager): # TODO: fix
chats = contacts_manager.get_group_chats()