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
|
# This file is part of the Frescobaldi project, http://www.frescobaldi.org/
#
# Copyright (c) 2008 - 2014 by Wilbert Berendsen
#
# 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 2
# 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, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
# See http://www.gnu.org/licenses/ for more information.
"""
Place snippets in menu.
If a snippet defines the variable 'menu' it is placed in the menu, sorted
on its internal action name and grouped by the value of the 'menu' variable.
If a snippet defines the variable 'template' it is placed in File->New from
template, sorted on its action name and grouped by the value of the 'template'
variable.
TODO:
- provide submenus
- caching (keep actions alive?)
"""
from PyQt5.QtCore import Qt, QUrl
from PyQt5.QtWidgets import QMenu
import app
import document
import qutil
import panelmanager
class SnippetMenuBase(QMenu):
def __init__(self, parent=None):
super(SnippetMenuBase, self).__init__(parent)
self.aboutToShow.connect(self.populate)
self.aboutToHide.connect(self.clearMenu, Qt.QueuedConnection)
self.triggered.connect(self.slotTriggered)
app.translateUI(self)
def mainwindow(self):
return self.parent().window()
def tool(self):
"""Returns the snippets tool."""
return panelmanager.manager(self.mainwindow()).snippettool
def populate(self):
"""Populates the menu with snippet actions."""
self.clearMenu() # on some systems aboutToHide does not fire...
from . import model, snippets, actions
last = self.insertBeforeAction()
shortcuts = self.tool().snippetActions
groups = {}
for name in sorted(model.model().names()):
variables = snippets.get(name).variables
group = self.snippetGroup(variables)
if group:
action = actions.action(name, self.mainwindow(), shortcuts)
self.visitAction(action, variables)
groups.setdefault(group, []).append(action)
for group in sorted(groups, key=lambda g: '' if g is True else g):
for action in groups[group]:
self.insertAction(last, action)
self.insertSeparator(last)
qutil.addAccelerators(self.actions())
def insertBeforeAction(self):
"""Should return an action to insert out stuff before, or None."""
return None
def snippetGroup(self, variables):
"""Should a group name if the snippet is to appear in the menu."""
def visitAction(self, action, variables):
"""May change the action depending on variables."""
def clearMenu(self):
"""Should delete the inserted actions."""
for a in self.actions():
self.removeAction(a)
a.deleteLater()
def slotTriggered(self, action):
"""Called when an action is triggered."""
name = action.objectName()
if name:
self.applySnippet(name)
def applySnippet(self, name):
"""Applies the named snippet."""
from . import insert
view = self.mainwindow().currentView()
view.setFocus()
insert.insert(name, view)
class SnippetMenu(SnippetMenuBase):
def __init__(self, parent=None):
super(SnippetMenu, self).__init__(parent)
self.addAction(self.tool().actionCollection.snippettool_activate)
def translateUI(self):
self.setTitle(_("menu title", "Sn&ippets"))
def insertBeforeAction(self):
return self.actions()[-1]
def snippetGroup(self, variables):
return variables.get('menu')
def visitAction(self, action, variables):
if 'yes' in variables.get('selection', ''):
action.setEnabled(self.mainwindow().hasSelection())
def clearMenu(self):
"""Deletes the actions on menu hide, excepts the "Snippets..." action."""
for a in self.actions()[:-1]:
self.removeAction(a)
a.deleteLater()
class TemplateMenu(SnippetMenuBase):
def __init__(self, parent=None):
super(TemplateMenu, self).__init__(parent)
self.addAction(self.tool().actionCollection.templates_manage)
def translateUI(self):
self.setTitle(_("New from &Template"))
def insertBeforeAction(self):
return self.actions()[-1]
def snippetGroup(self, variables):
return variables.get('template')
def applySnippet(self, name):
d = app.openUrl(QUrl())
self.mainwindow().setCurrentDocument(d)
super(TemplateMenu, self).applySnippet(name)
d.setUndoRedoEnabled(False)
d.setUndoRedoEnabled(True) # d.clearUndoRedoStacks() only in Qt >= 4.7
d.setModified(False)
from . import snippets
if 'template-run' in snippets.get(name).variables:
import engrave
engrave.engraver(self.mainwindow()).engrave('preview', d)
def clearMenu(self):
"""Deletes the actions on menu hide, except "Manage templates..."."""
for a in self.actions()[:-1]:
self.removeAction(a)
a.deleteLater()
|