gaupol/agents/menu.py¶
Source code for module gaupol.agents.menu from file gaupol/agents/menu.py.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 | # -*- coding: utf-8 -*-
# Copyright (C) 2005 Osmo Salomaa
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""Building and updating dynamic menus."""
import aeidon
_ = aeidon.i18n._
from gi.repository import Gtk
class MenuAgent(aeidon.Delegate):
"""
Building and updating dynamic menus.
:ivar _audio_tracks_id: A :class:`Gtk.UIManager` merge ID
:ivar _projects_id: A :class:`Gtk.UIManager` merge ID
:ivar _redo_menu_items: Redo menu tool button menu items
:ivar _undo_menu_items: Undo menu tool button menu items
"""
def __init__(self, master):
"""Initialize a :class:`MenuAgent` instance."""
aeidon.Delegate.__init__(self, master)
self._audio_tracks_id = None
self._projects_id = None
self._redo_menu_items = []
self._undo_menu_items = []
def _add_audio_track_action(self, index, language):
"""Add an action to the "audio tracks" action group."""
name = "activate_audio_track_{:d}".format(index)
label = language.replace("_", "__")
label = "{:d}. {}".format(index+1, label)
label = ("_{}".format(label) if index < 9 else label)
tooltip = _('Select "{}"').format(language)
action = Gtk.RadioAction(name=name,
label=label,
tooltip=tooltip,
stock_id=None,
value=index)
action_group = self.get_action_group("audio-tracks")
group = action_group.get_action("activate_audio_track_0")
if group is not None: action.join_group(group)
action_group.add_action(action)
action.connect("changed", self._on_audio_tracks_action_changed)
action.set_active(index == self.player.audio_track)
return action.get_name()
def _add_project_action(self, index, page):
"""Add an action to the "projects" action group."""
basename = page.get_main_basename()
name = "activate_project_{:d}".format(index)
label = page.tab_label.get_text().replace("_", "__")
label = "{:d}. {}".format(index+1, label)
label = ("_{}".format(label) if index < 9 else label)
tooltip = _('Activate "{}"').format(basename)
action = Gtk.RadioAction(name=name,
label=label,
tooltip=tooltip,
stock_id=None,
value=index)
action_group = self.get_action_group("projects")
group = action_group.get_action("activate_project_0")
if group is not None: action.join_group(group)
accel = ("<alt>{:d}".format(index+1) if index < 9 else None)
action_group.add_action_with_accel(action, accel)
action.connect("changed", self._on_projects_action_changed)
action.set_active(page is self.get_current_page())
return action.get_name()
def _on_audio_tracks_action_changed(self, item, active_item):
"""Select a new audio track."""
index = int(active_item.get_name().split("_")[-1])
self.player.audio_track = index
def _on_projects_action_changed(self, item, active_item):
"""Change the page in the notebook to the selected project."""
index = int(active_item.get_name().split("_")[-1])
self.notebook.set_current_page(index)
@aeidon.deco.export
def _on_redo_button_show_menu(self, *args):
"""Show a menu listing all redoable actions."""
menu = Gtk.Menu()
self._redo_menu_items = []
page = self.get_current_page()
for i, action in enumerate(page.project.redoables):
item = Gtk.MenuItem(label=action.description)
item.gaupol_index = i
item.gaupol_tooltip = _('Redo "{}"').format(action.description)
callback = self._on_redo_menu_item_activate
item.connect("activate", callback)
callback = self._on_redo_menu_item_enter_notify_event
item.connect("enter-notify-event", callback)
callback = self._on_redo_menu_item_leave_notify_event
item.connect("leave-notify-event", callback)
self._redo_menu_items.append(item)
menu.append(item)
menu.show_all()
self.get_tool_item("redo_action").set_menu(menu)
def _on_redo_menu_item_activate(self, menu_item):
"""Redo the selected action and all those above it."""
self.redo(menu_item.gaupol_index + 1)
def _on_redo_menu_item_enter_notify_event(self, menu_item, event):
"""Show tooltip and select all actions above `menu_item`."""
index = menu_item.gaupol_index
for item in self._redo_menu_items[:index]:
item.set_state(Gtk.StateType.PRELIGHT)
def _on_redo_menu_item_leave_notify_event(self, menu_item, event):
"""Hide tooltip and unselect all actions above `menu_item`."""
index = menu_item.gaupol_index
for item in self._redo_menu_items[:index]:
item.set_state(Gtk.StateType.NORMAL)
@aeidon.deco.export
def _on_show_audio_track_menu_activate(self, *args):
"""Show the audio track menu."""
action_group = self.get_action_group("audio-tracks")
for action in action_group.list_actions():
action_group.remove_action(action)
if self._audio_tracks_id is not None:
self.uim.remove_ui(self._audio_tracks_id)
if self.player is None: return
ui = '<ui><menubar name="menubar">'
ui += '<menu name="audio" action="show_audio_menu">'
ui += '<menu name="audio_track" action="show_audio_track_menu">'
ui += '<placeholder name="audio_tracks">'
for i, language in enumerate(self.player.get_audio_languages()):
language = language or _("Undefined")
name = self._add_audio_track_action(i, language)
ui += '<menuitem name="{:d}" action="{}"/>'.format(i, name)
ui += '</placeholder></menu></menu></menubar></ui>'
self._audio_tracks_id = self.uim.add_ui_from_string(ui)
self.uim.ensure_update()
@aeidon.deco.export
def _on_show_projects_menu_activate(self, *args):
"""Update all project actions in the projects menu."""
action_group = self.get_action_group("projects")
for action in action_group.list_actions():
action_group.remove_action(action)
if self._projects_id is not None:
self.uim.remove_ui(self._projects_id)
page = self.get_current_page()
if page is None: return
ui = '<ui><menubar name="menubar">'
ui += '<menu name="projects" action="show_projects_menu">'
ui += '<placeholder name="open">'
for i, page in enumerate(self.pages):
name = self._add_project_action(i, page)
ui += '<menuitem name="{:d}" action="{}"/>'.format(i, name)
ui += '</placeholder></menu></menubar></ui>'
self._projects_id = self.uim.add_ui_from_string(ui)
self.uim.ensure_update()
@aeidon.deco.export
def _on_tab_widget_button_press_event(self, button, event, page):
"""Display a pop-up menu with tab-related actions."""
if event.button != 3: return
if page is not self.get_current_page():
self.set_current_page(page)
menu = self.uim.get_widget("/ui/tab_popup")
menu.popup(parent_menu_shell=None,
parent_menu_item=None,
func=None,
data=None,
button=event.button,
activate_time=event.time)
@aeidon.deco.export
def _on_undo_button_show_menu(self, *args):
"""Show a menu listing all undoable actions."""
menu = Gtk.Menu()
self._undo_menu_items = []
page = self.get_current_page()
for i, action in enumerate(page.project.undoables):
item = Gtk.MenuItem(label=action.description)
item.gaupol_index = i
item.gaupol_tooltip = _('Undo "{}"').format(action.description)
callback = self._on_undo_menu_item_activate
item.connect("activate", callback)
callback = self._on_undo_menu_item_enter_notify_event
item.connect("enter-notify-event", callback)
callback = self._on_undo_menu_item_leave_notify_event
item.connect("leave-notify-event", callback)
self._undo_menu_items.append(item)
menu.append(item)
menu.show_all()
self.get_tool_item("undo_action").set_menu(menu)
def _on_undo_menu_item_activate(self, menu_item):
"""Undo the selected action and all those above it."""
self.undo(menu_item.gaupol_index + 1)
def _on_undo_menu_item_enter_notify_event(self, menu_item, event):
"""Show tooltip and select all actions above `menu_item`."""
index = menu_item.gaupol_index
for item in self._undo_menu_items[:index]:
item.set_state(Gtk.StateType.PRELIGHT)
def _on_undo_menu_item_leave_notify_event(self, menu_item, event):
"""Hide tooltip and unselect all actions above `menu_item`."""
index = menu_item.gaupol_index
for item in self._undo_menu_items[:index]:
item.set_state(Gtk.StateType.NORMAL)
|