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 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408
|
# 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.
"""
The snippets widget.
"""
from PyQt5.QtCore import QEvent, QItemSelectionModel, QModelIndex, Qt
from PyQt5.QtGui import QKeySequence
from PyQt5.QtWidgets import (
QAction, QApplication, QCompleter, QFileDialog, QHBoxLayout, QMenu,
QMessageBox, QPushButton, QSplitter, QTextBrowser, QToolButton,
QTreeView, QVBoxLayout, QWidget)
import app
import userguide
import icons
import widgets.lineedit
import textformats
import actioncollectionmanager
from . import actions
from . import model
from . import snippets
from . import edit
from . import insert
from . import highlight
class Widget(QWidget):
def __init__(self, panel):
super(Widget, self).__init__(panel)
layout = QVBoxLayout()
self.setLayout(layout)
layout.setSpacing(0)
self.searchEntry = SearchLineEdit()
self.treeView = QTreeView(contextMenuPolicy=Qt.CustomContextMenu)
self.textView = QTextBrowser()
applyButton = QToolButton(autoRaise=True)
editButton = QToolButton(autoRaise=True)
addButton = QToolButton(autoRaise=True)
self.menuButton = QPushButton(flat=True)
menu = QMenu(self.menuButton)
self.menuButton.setMenu(menu)
splitter = QSplitter(Qt.Vertical)
top = QHBoxLayout()
layout.addLayout(top)
splitter.addWidget(self.treeView)
splitter.addWidget(self.textView)
layout.addWidget(splitter)
splitter.setSizes([200, 100])
splitter.setCollapsible(0, False)
top.addWidget(self.searchEntry)
top.addWidget(applyButton)
top.addSpacing(10)
top.addWidget(addButton)
top.addWidget(editButton)
top.addWidget(self.menuButton)
# action generator for actions added to search entry
def act(slot, icon=None):
a = QAction(self, triggered=slot)
self.addAction(a)
a.setShortcutContext(Qt.WidgetWithChildrenShortcut)
icon and a.setIcon(icons.get(icon))
return a
# hide if ESC pressed in lineedit
a = act(self.slotEscapePressed)
a.setShortcut(QKeySequence(Qt.Key_Escape))
# import action
a = self.importAction = act(self.slotImport, 'document-open')
menu.addAction(a)
# export action
a = self.exportAction = act(self.slotExport, 'document-save-as')
menu.addAction(a)
# apply button
a = self.applyAction = act(self.slotApply, 'edit-paste')
applyButton.setDefaultAction(a)
menu.addSeparator()
menu.addAction(a)
# add button
a = self.addAction_ = act(self.slotAdd, 'list-add')
a.setShortcut(QKeySequence(Qt.Key_Insert))
addButton.setDefaultAction(a)
menu.addSeparator()
menu.addAction(a)
# edit button
a = self.editAction = act(self.slotEdit, 'document-edit')
a.setShortcut(QKeySequence(Qt.Key_F2))
editButton.setDefaultAction(a)
menu.addAction(a)
# set shortcut action
a = self.shortcutAction = act(self.slotShortcut, 'preferences-desktop-keyboard-shortcuts')
menu.addAction(a)
# delete action
a = self.deleteAction = act(self.slotDelete, 'list-remove')
a.setShortcut(QKeySequence(Qt.CTRL + Qt.Key_Delete))
menu.addAction(a)
# restore action
a = self.restoreAction = act(self.slotRestore)
menu.addSeparator()
menu.addAction(a)
# help button
a = self.helpAction = act(self.slotHelp, 'help-contents')
menu.addSeparator()
menu.addAction(a)
self.treeView.setSelectionBehavior(QTreeView.SelectRows)
self.treeView.setSelectionMode(QTreeView.ExtendedSelection)
self.treeView.setRootIsDecorated(False)
self.treeView.setAllColumnsShowFocus(True)
self.treeView.setModel(model.model())
self.treeView.setCurrentIndex(QModelIndex())
# signals
self.searchEntry.returnPressed.connect(self.slotReturnPressed)
self.searchEntry.textChanged.connect(self.updateFilter)
self.treeView.doubleClicked.connect(self.slotDoubleClicked)
self.treeView.customContextMenuRequested.connect(self.showContextMenu)
self.treeView.selectionModel().currentChanged.connect(self.updateText)
self.treeView.model().dataChanged.connect(self.updateFilter)
# highlight text
self.highlighter = highlight.Highlighter(self.textView.document())
# complete on snippet variables
self.searchEntry.setCompleter(QCompleter([
':icon', ':indent', ':menu', ':name', ':python', ':selection',
':set', ':symbol', ':template', ':template-run'], self.searchEntry))
self.readSettings()
app.settingsChanged.connect(self.readSettings)
app.translateUI(self)
self.updateColumnSizes()
self.setAcceptDrops(True)
def dropEvent(self, ev):
if not ev.source() and ev.mimeData().hasUrls():
filename = ev.mimeData().urls()[0].toLocalFile()
if filename:
ev.accept()
from . import import_export
import_export.load(filename, self)
def dragEnterEvent(self, ev):
if not ev.source() and ev.mimeData().hasUrls():
ev.accept()
def translateUI(self):
try:
self.searchEntry.setPlaceholderText(_("Search..."))
except AttributeError:
pass # not in Qt 4.6
shortcut = lambda a: a.shortcut().toString(QKeySequence.NativeText)
self.menuButton.setText(_("&Menu"))
self.addAction_.setText(_("&Add..."))
self.addAction_.setToolTip(
_("Add a new snippet. ({key})").format(key=shortcut(self.addAction_)))
self.editAction.setText(_("&Edit..."))
self.editAction.setToolTip(
_("Edit the current snippet. ({key})").format(key=shortcut(self.editAction)))
self.shortcutAction.setText(_("Configure Keyboard &Shortcut..."))
self.deleteAction.setText(_("&Remove"))
self.deleteAction.setToolTip(_("Remove the selected snippets."))
self.applyAction.setText(_("A&pply"))
self.applyAction.setToolTip(_("Apply the current snippet."))
self.importAction.setText(_("&Import..."))
self.importAction.setToolTip(_("Import snippets from a file."))
self.exportAction.setText(_("E&xport..."))
self.exportAction.setToolTip(_("Export snippets to a file."))
self.restoreAction.setText(_("Restore &Built-in Snippets..."))
self.restoreAction.setToolTip(
_("Restore deleted or changed built-in snippets."))
self.helpAction.setText(_("&Help"))
self.searchEntry.setToolTip(_(
"Enter text to search in the snippets list.\n"
"See \"What's This\" for more information."))
self.searchEntry.setWhatsThis(''.join(map("<p>{0}</p>\n".format, (
_("Enter text to search in the snippets list, and "
"press Enter to apply the currently selected snippet."),
_("If the search text fully matches the value of the '{name}' variable "
"of a snippet, that snippet is selected.").format(name="name"),
_("If the search text starts with a colon ':', the rest of the "
"search text filters snippets that define the given variable. "
"After a space a value can also be entered, snippets will then "
"match if the value of the given variable contains the text after "
"the space."),
_("E.g. entering {menu} will show all snippets that are displayed "
"in the insert menu.").format(menu="<code>:menu</code>"),
))))
def sizeHint(self):
return self.parent().mainwindow().size() / 4
def readSettings(self):
data = textformats.formatData('editor')
self.textView.setFont(data.font)
self.textView.setPalette(data.palette())
def showContextMenu(self, pos):
"""Called when the user right-clicks the tree view."""
self.menuButton.menu().popup(self.treeView.viewport().mapToGlobal(pos))
def slotReturnPressed(self):
"""Called when the user presses Return in the search entry. Applies current snippet."""
name = self.currentSnippet()
if name:
view = self.parent().mainwindow().currentView()
insert.insert(name, view)
self.parent().hide() # make configurable?
view.setFocus()
def slotEscapePressed(self):
"""Called when the user presses ESC in the search entry. Hides the panel."""
self.parent().hide()
self.parent().mainwindow().currentView().setFocus()
def slotDoubleClicked(self, index):
name = self.treeView.model().name(index)
view = self.parent().mainwindow().currentView()
insert.insert(name, view)
def slotAdd(self):
"""Called when the user wants to add a new snippet."""
edit.Edit(self, None)
def slotEdit(self):
"""Called when the user wants to edit a snippet."""
name = self.currentSnippet()
if name:
edit.Edit(self, name)
def slotShortcut(self):
"""Called when the user selects the Configure Shortcut action."""
from widgets import shortcuteditdialog
name = self.currentSnippet()
if name:
collection = self.parent().snippetActions
action = actions.action(name, None, collection)
default = collection.defaults().get(name)
mgr = actioncollectionmanager.manager(self.parent().mainwindow())
cb = mgr.findShortcutConflict
dlg = shortcuteditdialog.ShortcutEditDialog(self, cb, (collection, name))
if dlg.editAction(action, default):
mgr.removeShortcuts(action.shortcuts())
collection.setShortcuts(name, action.shortcuts())
self.treeView.update()
def slotDelete(self):
"""Called when the user wants to delete the selected rows."""
rows = sorted(set(i.row() for i in self.treeView.selectedIndexes()), reverse=True)
if rows:
for row in rows:
name = self.treeView.model().names()[row]
self.parent().snippetActions.setShortcuts(name, [])
self.treeView.model().removeRow(row)
self.updateFilter()
def slotApply(self):
"""Called when the user clicks the apply button. Applies current snippet."""
name = self.currentSnippet()
if name:
view = self.parent().mainwindow().currentView()
insert.insert(name, view)
def slotImport(self):
"""Called when the user activates the import action."""
filetypes = "{0} (*.xml);;{1} (*)".format(_("XML Files"), _("All Files"))
caption = app.caption(_("dialog title", "Import Snippets"))
filename = None
filename = QFileDialog.getOpenFileName(self, caption, filename, filetypes)[0]
if filename:
from . import import_export
import_export.load(filename, self)
def slotExport(self):
"""Called when the user activates the export action."""
allrows = [row for row in range(model.model().rowCount())
if not self.treeView.isRowHidden(row, QModelIndex())]
selectedrows = [i.row() for i in self.treeView.selectedIndexes()
if i.column() == 0 and i.row() in allrows]
names = self.treeView.model().names()
names = [names[row] for row in selectedrows or allrows]
filetypes = "{0} (*.xml);;{1} (*)".format(_("XML Files"), _("All Files"))
n = len(names)
caption = app.caption(_("dialog title",
"Export {num} Snippet", "Export {num} Snippets", n).format(num=n))
filename = QFileDialog.getSaveFileName(self, caption, None, filetypes)[0]
if filename:
from . import import_export
try:
import_export.save(names, filename)
except (IOError, OSError) as e:
QMessageBox.critical(self, _("Error"), _(
"Can't write to destination:\n\n{url}\n\n{error}").format(
url=filename, error=e.strerror))
def slotRestore(self):
"""Called when the user activates the Restore action."""
from . import restore
dlg = restore.RestoreDialog(self)
dlg.setWindowModality(Qt.WindowModal)
dlg.populate()
dlg.show()
dlg.finished.connect(dlg.deleteLater)
def slotHelp(self):
"""Called when the user clicks the small help button."""
userguide.show("snippets")
def currentSnippet(self):
"""Returns the name of the current snippet if it is visible."""
row = self.treeView.currentIndex().row()
if row != -1 and not self.treeView.isRowHidden(row, QModelIndex()):
return self.treeView.model().names()[row]
def updateFilter(self):
"""Called when the text in the entry changes, updates search results."""
text = self.searchEntry.text()
ltext = text.lower()
filterVars = text.startswith(':')
if filterVars:
try:
fvar, fval = text[1:].split(None, 1)
fhide = lambda v: v.get(fvar) in (True, None) or fval not in v.get(fvar)
except ValueError:
fvar = text[1:].strip()
fhide = lambda v: not v.get(fvar)
for row in range(self.treeView.model().rowCount()):
name = self.treeView.model().names()[row]
nameid = snippets.get(name).variables.get('name', '')
if filterVars:
hide = fhide(snippets.get(name).variables)
elif nameid == text:
i = self.treeView.model().createIndex(row, 0)
self.treeView.selectionModel().setCurrentIndex(i, QItemSelectionModel.SelectCurrent | QItemSelectionModel.Rows)
hide = False
elif nameid.lower().startswith(ltext):
hide = False
elif ltext in snippets.title(name).lower():
hide = False
else:
hide = True
self.treeView.setRowHidden(row, QModelIndex(), hide)
self.updateText()
def updateText(self):
"""Called when the current snippet changes."""
name = self.currentSnippet()
self.textView.clear()
if name:
s = snippets.get(name)
self.highlighter.setPython('python' in s.variables)
self.textView.setPlainText(s.text)
def updateColumnSizes(self):
self.treeView.resizeColumnToContents(0)
self.treeView.resizeColumnToContents(1)
class SearchLineEdit(widgets.lineedit.LineEdit):
def __init__(self, *args):
super(SearchLineEdit, self).__init__(*args)
def event(self, ev):
if ev.type() == QEvent.KeyPress and any(ev.matches(key) for key in (
QKeySequence.MoveToNextLine, QKeySequence.SelectNextLine,
QKeySequence.MoveToPreviousLine, QKeySequence.SelectPreviousLine,
QKeySequence.MoveToNextPage, QKeySequence.SelectNextPage,
QKeySequence.MoveToPreviousPage, QKeySequence.SelectPreviousPage)):
QApplication.sendEvent(self.parent().treeView, ev)
return True
return super(SearchLineEdit, self).event(ev)
|