1
# This file is part of the Frescobaldi project, http://www.frescobaldi.org/
3
# Copyright (c) 2008 - 2011 by Wilbert Berendsen
5
# This program is free software; you can redistribute it and/or
6
# modify it under the terms of the GNU General Public License
7
# as published by the Free Software Foundation; either version 2
8
# of the License, or (at your option) any later version.
10
# This program is distributed in the hope that it will be useful,
11
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
# GNU General Public License for more details.
15
# You should have received a copy of the GNU General Public License
16
# along with this program; if not, write to the Free Software
17
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18
# See http://www.gnu.org/licenses/ for more information.
21
The model containing the snippets data.
25
from __future__ import unicode_literals
29
from PyQt4.QtCore import QAbstractItemModel, QModelIndex, Qt
30
from PyQt4.QtGui import QKeySequence
33
import actioncollection
35
from . import snippets
39
"""Returns the global model containing snippets."""
40
m = SnippetModel(app.qApp)
46
class SnippetModel(QAbstractItemModel):
47
"""Presents the snippets as a Qt Model."""
48
def __init__(self, parent = None):
49
super(SnippetModel, self).__init__(parent)
52
app.settingsChanged.connect(self.slotSettingsChanged)
53
app.languageChanged.connect(self.slotLanguageChanged)
55
# methods needed to be a well-behaved model
56
def headerData(self, section, orientation, role=Qt.DisplayRole):
57
if role == Qt.DisplayRole and orientation == Qt.Horizontal:
61
return _("Description")
65
def index(self, row, column, parent=None):
66
return self.createIndex(row, column)
68
def parent(self, index):
71
def columnCount(self, parent=QModelIndex()):
72
return 3 if not parent.isValid() else 0
74
def rowCount(self, parent=QModelIndex()):
75
return len(self._names) if not parent.isValid() else 0
77
def data(self, index, role=Qt.DisplayRole):
78
name = self.name(index)
79
if role == Qt.DisplayRole:
80
if index.column() == 0:
81
return snippets.get(name).variables.get('name')
82
elif index.column() == 1:
83
return snippets.title(name)
86
elif role == Qt.DecorationRole and index.column() == 1:
87
return snippets.icon(name)
90
def slotSettingsChanged(self):
91
"""Called when settings change, e.g. when keyboard shortcuts are altered."""
94
def slotLanguageChanged(self):
95
"""Called when the user changes the language."""
96
self.headerDataChanged.emit(Qt.Horizontal, 0, 2)
99
self.beginResetModel()
100
self._names = sorted(snippets.names(), key=snippets.title)
103
# interface for getting/altering snippets
105
"""Returns the internal list of snippet names in title order. Do not alter!"""
108
def name(self, index):
109
"""The internal snippet id for the given QModelIndex."""
110
return self._names[index.row()]
112
def removeRows(self, row, count, parent=QModelIndex()):
114
self.beginRemoveRows(parent, row, end)
116
for name in self._names[row:end]:
117
snippets.delete(name)
118
del self._names[row:end]
123
def saveSnippet(self, name, text, title):
126
If name is None or does not exist in names(), a new snippet is created.
127
Returns the QModelIndex the snippet was stored at.
132
# first, get the old titles list
133
titles = list(snippets.title(n) for n in self._names)
137
name = snippets.name(self._names)
140
oldrow = self._names.index(name)
143
snippets.save(name, text, title)
144
# sort the new snippet in
145
# if oldrow is not None, it is the row to be removed.
146
title = snippets.title(name)
147
i = bisect.bisect_right(titles, title)
150
# just insert new snippet
151
self.beginInsertRows(QModelIndex(), i, i )
152
self._names.insert(i, name)
154
return self.createIndex(i, 0)
155
elif i in (oldrow, oldrow+1):
157
self._names[oldrow] = name
158
self.dataChanged.emit(self.createIndex(oldrow, 0), self.createIndex(oldrow, 2))
159
return self.createIndex(oldrow, 0)
161
# move the old row to the new place
162
if self.beginMoveRows(QModelIndex(), oldrow, oldrow, QModelIndex(), i):
163
del self._names[oldrow]
166
self._names.insert(i, name)
168
self.dataChanged.emit(self.createIndex(i, 0), self.createIndex(i, 2))
169
return self.createIndex(i, 0)
170
raise RuntimeError("wrong row move offset")
174
"""Returns a shortcut text for the named snippets, if any, else None."""
177
text = s[0].toString(QKeySequence.NativeText)
184
"""Returns a (maybe empty) list of QKeySequences for the named snippet."""
186
return ac and ac.shortcuts(name) or []
190
"""Returns an instance of the 'snippets' ShortcutCollection, if existing."""
192
# HACK alert :-) access an instance of the ShortcutCollection named 'snippets'
193
ref = actioncollection.ShortcutCollection.others['snippets'][0]
194
except (KeyError, IndexError):