Source code for timelinelib.wxgui.frames.mainframe.menus.filemenu

# Copyright (C) 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018  Rickard Lindberg, Roger Lindberg
#
# This file is part of Timeline.
#
# Timeline 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.
#
# Timeline 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 Timeline.  If not, see <http://www.gnu.org/licenses/>.


import os

import wx
import timelinelib.wxgui.frames.mainframe.menus as mid
from timelinelib.wxgui.frames.mainframe.menus.menubase import MenuBase
from timelinelib.plugin.factory import EXPORTER
from timelinelib.plugin import factory
from timelinelib.wxgui.dialogs.filenew.view import open_file_new_dialog
from timelinelib.wxgui.dialogs.importevents.view import open_import_events_dialog

SHORTCUTS = (mid.ID_OPEN, mid.ID_NEW, mid.ID_SAVEAS, mid.ID_IMPORT, mid.ID_EXIT)
REQUIRING_TIMELINE = (mid.ID_IMPORT, mid.ID_SAVEAS)


[docs]class FileMenu(MenuBase): """The File menu (a wx.Menu)."""
[docs] def __init__(self, main_frame): self._mnu_open_recent_submenu = wx.Menu() event_handlers = { mid.ID_NEW: lambda evt: open_file_new_dialog(main_frame), mid.ID_OPEN: lambda evt: main_frame.controller.open_existing_timeline(), mid.ID_SAVEAS: lambda evt: main_frame.controller.save_as(), mid.ID_IMPORT: lambda evt: open_import_events_dialog(main_frame), mid.ID_EXIT: lambda evt: main_frame.Close(), } MenuBase.__init__(self, main_frame, event_handlers, SHORTCUTS, REQUIRING_TIMELINE) self._open_recent_map = {} self._create_menu() self._bind_event_handlers() self._register_shortcuts() self._register_menus_requiring_timeline()
[docs] def UpdateRecentSubmenu(self): """ Update the submenu that has a list of recent opened timeline files. Used when a new file is opened or created. """ self._clear_recent_menu_items() self._create_recent_menu_items()
def _create_menu(self): self.Append(mid.ID_NEW, _("New..."), _("Create a new timeline")) self.Append(mid.ID_OPEN, _("Open..."), _("Open an existing timeline")) self.Append(mid.ID_RECENT, _("Open &Recent"), self._mnu_open_recent_submenu) self.AppendSeparator() self.Append(mid.ID_SAVEAS, "", _("Save As...")) self.AppendSeparator() self.Append(mid.ID_IMPORT, _("Import events..."), _("Import events...")) self.AppendSeparator() self.Append(wx.ID_ANY, _("Export"), self._create_export_menues()) self.AppendSeparator() self.Append(mid.ID_EXIT, "", _("Exit the program")) def _clear_recent_menu_items(self): for item in self._mnu_open_recent_submenu.GetMenuItems(): self._mnu_open_recent_submenu.Delete(item) def _create_recent_menu_items(self): for path in self._main_frame.config.get_recently_opened(): self._map_path_to_recent_menu_item(path) def _map_path_to_recent_menu_item(self, path): name = "%s (%s)" % ( os.path.basename(path), os.path.dirname(os.path.abspath(path))) item = self._mnu_open_recent_submenu.Append(wx.ID_ANY, name) self._open_recent_map[item.GetId()] = path self.Bind(wx.EVT_MENU, self._on_open_recent_timeline, item) def _on_open_recent_timeline(self, evt): path = self._open_recent_map[evt.GetId()] self._main_frame.controller.open_timeline_if_exists(path) def _create_export_menues(self): submenu = wx.Menu() for plugin in factory.get_plugins(EXPORTER): self._create_submenu(plugin, submenu) return submenu def _create_submenu(self, plugin, submenu): wxid = plugin.wxid() submenu.Append(wxid, plugin.display_name(), plugin.display_name()) self._main_frame.Bind(wx.EVT_MENU, lambda evt: plugin.run(self._main_frame), id=wxid) self._main_frame.menu_controller.add_menu_requiring_timeline(submenu.FindItemById(wxid)) self._main_frame.shortcut_controller.add_shortcut_item(wxid, submenu.FindItemById(wxid))