File: document.py

package info (click to toggle)
frescobaldi 3.3.0%2Bds1-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 24,212 kB
  • sloc: python: 39,014; javascript: 263; sh: 238; makefile: 80
file content (315 lines) | stat: -rw-r--r-- 10,991 bytes parent folder | download | duplicates (2)
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
# 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.

"""
A (Frescobaldi) document.

This contains the text the user can edit in Frescobaldi. In most cases it will
be a LilyPond source file, but other file types can be used as well.

There are two different document Classes: Document and EditorDocument.
Both provide a QTextDocument with additional metadata, but the EditorDocument
provides additional handling of signals that are hooked into the Frescobaldi
GUI environment. That means: use EditorDocument for documents open in the
editor, Document for "abstract" documents, for example to pass a generated
document to a job.lilypond.LilyPondJob without implicitly creating a tab.

"""


import os

from PyQt5.QtCore import QUrl
from PyQt5.QtGui import QTextCursor, QTextDocument
from PyQt5.QtWidgets import QPlainTextDocumentLayout

import app
import util
import variables
import signals


class AbstractDocument(QTextDocument):
    """Base class for a Frescobaldi document. Not intended to be instantiated.

    Objects of subclasses can be passed to the functions in documentinfo
    or lilypondinfo etc. for additional meta information.

    """

    @classmethod
    def load_data(cls, url, encoding=None):
        """Class method to load document contents from an url.

        This is intended to open a document without instantiating one
        if loading the contents fails.

        This method returns the text contents of the url as decoded text,
        thus a unicode string.

        The line separator is always '\\n'.

        """
        filename = url.toLocalFile()

        # currently, we do not support non-local files
        if not filename:
            raise IOError("not a local file")
        with open(filename, 'rb') as f:
            data = f.read()
        text = util.decode(data, encoding)
        return util.universal_newlines(text)

    @classmethod
    def new_from_url(cls, url, encoding=None):
        """Create and return a new document, loaded from url.

        This is intended to open a new Document without instantiating one
        if loading the contents fails.

        """
        # Do this first, raises IOError if not found, without creating the document.
        if not url.isEmpty():
            data = cls.load_data(url, encoding)
        # If this did not raise, proceed to create a new document.
        d = cls(url, encoding)
        if not url.isEmpty():
            d.setPlainText(data)
            d.setModified(False)
        return d

    def __init__(self, url=None, encoding=None):
        """Create a new Document with url and encoding.

        Does not load the contents, you should use load() for that, or
        use the new_from_url() constructor to instantiate a new Document
        with the contents loaded.

        """
        if url is None:
            url = QUrl()
        super(AbstractDocument, self).__init__()
        self.setDocumentLayout(QPlainTextDocumentLayout(self))
        self._encoding = encoding
        self._url = url # avoid urlChanged on init
        self.setUrl(url)

    def load(self, url=None, encoding=None, keepUndo=False):
        """Load the specified or current url (if None was specified).

        Currently only local files are supported. An IOError is raised
        when trying to load a nonlocal URL.

        If loading succeeds and an url was specified, the url is made the
        current url (by calling setUrl() internally).

        If keepUndo is True, the loading can be undone (with Ctrl-Z).

        """
        if url is None:
            url = QUrl()
        u = url if not url.isEmpty() else self.url()
        text = self.load_data(u, encoding or self._encoding)
        if keepUndo:
            c = QTextCursor(self)
            c.select(QTextCursor.Document)
            c.insertText(text)
        else:
            self.setPlainText(text)
        self.setModified(False)
        if not url.isEmpty():
            self.setUrl(url)

    def _save(self, url, filename):
        with open(filename, "wb") as f:
            f.write(self.encodedText())
            f.flush()
            os.fsync(f.fileno())
        self.setModified(False)
        if not url.isEmpty():
            self.setUrl(url)

    def save(self, url=None, encoding=None):
        """Saves the document to the specified or current url.

        Currently only local files are supported. An IOError is raised
        when trying to save a nonlocal URL.

        If saving succeeds and an url was specified, the url is made the
        current url (by calling setUrl() internally).

        This method is never called directly but only from the overriding
        subclass methods that make further specific use of the modified results.

        """
        if url is None:
            url = QUrl()
        u = url if not url.isEmpty() else self.url()
        filename = u.toLocalFile()
        # currently, we do not support non-local files
        if not filename:
            raise IOError("not a local file")
        # keep the url if specified when we didn't have one, even if saving
        # would fail
        if self.url().isEmpty() and not url.isEmpty():
            self.setUrl(url)
        return url, filename

    def url(self):
        return self._url

    def setUrl(self, url):
        """ Change the url for this document. """
        if url is None:
            url = QUrl()
        old, self._url = self._url, url
        # number for nameless documents
        if self._url.isEmpty():
            nums = [0]
            nums.extend(doc._num for doc in app.documents if doc is not self)
            self._num = max(nums) + 1
        else:
            self._num = 0
        return old

    def encoding(self):
        return variables.get(self, "coding") or self._encoding

    def setEncoding(self, encoding):
        self._encoding = encoding

    def encodedText(self):
        """Return the text of the document as a bytes string encoded in the
        correct encoding.

        The line separator is '\\n' on Unix/Linux/Mac OS X, '\\r\\n' on Windows.

        Useful to save to a file.

        """
        text = util.platform_newlines(self.toPlainText())
        return util.encode(text, self.encoding())

    def documentName(self):
        """Return a suitable name for this document.

        This is only to be used for display. If the url of the document is
        empty, something like "Untitled" or "Untitled (3)" is returned.

        """
        if self._url.isEmpty():
            if self._num == 1:
                return _("Untitled")
            else:
                return _("Untitled ({num})").format(num=self._num)
        else:
            return os.path.basename(self._url.path())


class Document(AbstractDocument):
    """A Frescobaldi document to be used anywhere except the main editor
    viewspace (also non-GUI jobs/operations)."""

    def save(self, url=None, encoding=None):
        url, filename = super().save(url, encoding)
        self._save(url, filename)


class EditorDocument(AbstractDocument):
    """A Frescobaldi document for use in the main editor view.
    Basically this is an AbstractDocument with signals added."""

    urlChanged = signals.Signal() # new url, old url
    closed = signals.Signal()
    loaded = signals.Signal()
    saving = signals.SignalContext()
    saved = signals.Signal()

    @classmethod
    def new_from_url(cls, url, encoding=None):
        d = super(EditorDocument, cls).new_from_url(url, encoding)
        if not url.isEmpty():
            d.loaded()
            app.documentLoaded(d)
        return d

    def __init__(self, url=None, encoding=None):
        super(EditorDocument, self).__init__(url, encoding)
        self.modificationChanged.connect(self.slotModificationChanged)
        app.documents.append(self)
        app.documentCreated(self)

    def slotModificationChanged(self):
        app.documentModificationChanged(self)

    def close(self):
        self.closed()
        app.documentClosed(self)
        app.documents.remove(self)

    def load(self, url=None, encoding=None, keepUndo=False):
        super(EditorDocument, self).load(url, encoding, keepUndo)
        self.loaded()
        app.documentLoaded(self)

    def save(self, url=None, encoding=None):
        url, filename = super().save(url, encoding)
        with self.saving(), app.documentSaving(self):
            self._save(url, filename)
        self.saved()
        app.documentSaved(self)

    def setUrl(self, url):
        old = super(EditorDocument, self).setUrl(url)
        if url != old:
            self.urlChanged(url, old)
            app.documentUrlChanged(self, url, old)

    def cursorAtPosition(self, line, column=None):
        """Return a new QTextCursor set to the line and column given (each starting at 1).

        This method avoids common pitfalls associated with arbitrarily setting the cursor
        position via setCursorPosition.
        - The cursor will be set at a vaid position in a valid block.
        - Reasonable defaults are used for under/over-limit input.
        - Character counting based on UTF-8 matches LilyPond and Python conventions.
        - The cursor will not be set in the middle of a surrogate pair or composed glyph.

        """
        if line < 1:
            line = column = 1
        elif not column or column < 1:
            column = 1
        cursor = QTextCursor(self)
        block = self.findBlockByNumber(line - 1)
        if block.isValid():
            line_text = block.text()
            if len(line_text) >= column:
                qchar_offset = len(line_text[:column - 1].encode('utf_16_le')) // 2
                cursor.setPosition(block.position() + qchar_offset)
                # Escape to in front of what might be the middle of a composed glyph.
                cursor.movePosition(QTextCursor.NextCharacter)
                cursor.movePosition(QTextCursor.PreviousCharacter)
            else:
                cursor.setPosition(block.position())
                cursor.movePosition(QTextCursor.EndOfBlock)
        else:
            cursor.movePosition(QTextCursor.End)
        return cursor