예제 #1
0
 def saveDocumentAs(self, doc):
     """ Saves the document, always asking for a name.
     
     Returns True if saving succeeded.
     
     """
     filename = doc.url().toLocalFile()
     if filename:
         filetypes = app.filetypes(os.path.splitext(filename)[1])
     else:
         filename = app.basedir() # default directory to save to
         import documentinfo
         import ly.lex
         filetypes = app.filetypes(ly.lex.extensions[documentinfo.mode(doc)])
     caption = app.caption(_("dialog title", "Save File"))
     filename = QFileDialog.getSaveFileName(self, caption, filename, filetypes)
     if not filename:
         return False # cancelled
     if not util.iswritable(filename):
         QMessageBox.warning(self, app.caption(_("Error")),
             _("Can't write to destination:\n\n{url}").format(url=filename))
         return False
     url = QUrl.fromLocalFile(filename)
     doc.setUrl(url)
     recentfiles.add(url)
     return self.saveDocument(doc)
예제 #2
0
 def saveDocumentAs(self, doc):
     """ Saves the document, always asking for a name.
     
     Returns True if saving succeeded.
     
     """
     filename = doc.url().toLocalFile()
     if filename:
         filetypes = app.filetypes(os.path.splitext(filename)[1])
     else:
         filename = app.basedir()  # default directory to save to
         import documentinfo
         import ly.lex
         filetypes = app.filetypes(
             ly.lex.extensions[documentinfo.mode(doc)])
     caption = app.caption(_("dialog title", "Save File"))
     filename = QFileDialog.getSaveFileName(self, caption, filename,
                                            filetypes)
     if not filename:
         return False  # cancelled
     if not util.iswritable(filename):
         QMessageBox.warning(
             self, app.caption(_("Error")),
             _("Can't write to destination:\n\n{url}").format(url=filename))
         return False
     url = QUrl.fromLocalFile(filename)
     doc.setUrl(url)
     recentfiles.add(url)
     return self.saveDocument(doc)
예제 #3
0
 def openUrl(self, url, encoding=None):
     """Same as app.openUrl but with some error checking and recent files."""
     if not url.toLocalFile():
         # we only support local files
         QMessageBox.warning(self, app.caption(_("Warning")),
             _("Can't load non-local document:\n\n{url}").format(
                 url=url.toString()))
     else:
         recentfiles.add(url)
     return app.openUrl(url, encoding)
예제 #4
0
 def openUrl(self, url, encoding=None):
     """Same as app.openUrl but with some error checking and recent files."""
     if not url.toLocalFile():
         # we only support local files
         QMessageBox.warning(
             self, app.caption(_("Warning")),
             _("Can't load non-local document:\n\n{url}").format(
                 url=url.toString()))
     else:
         recentfiles.add(url)
     return app.openUrl(url, encoding)
예제 #5
0
    def saveDocument(self, doc, save_as=False):
        """ Saves the document, asking for a name if necessary.

        If save_as is True, a name is always asked.
        Returns True if saving succeeded.

        """
        if save_as or doc.url().isEmpty():
            filename = doc.url().toLocalFile()
            if filename:
                filetypes = app.filetypes(os.path.splitext(filename)[1])
            else:
                # find a suitable directory to save to
                for d in self.historyManager.documents()[1::]:
                    if d.url().toLocalFile():
                        directory = os.path.dirname(d.url().toLocalFile())
                        break
                else:
                    directory = app.basedir() # default directory to save to
                
                import documentinfo
                import ly.lex
                filename = os.path.join(directory, documentinfo.defaultfilename(doc))
                filetypes = app.filetypes(ly.lex.extensions[documentinfo.mode(doc)])
            caption = app.caption(_("dialog title", "Save File"))
            filename = QFileDialog.getSaveFileName(self, caption, filename, filetypes)[0]
            if not filename:
                return False # cancelled
            url = QUrl.fromLocalFile(filename)
        else:
            url = doc.url()

        if QSettings().value("strip_trailing_whitespace", False, bool):
            import reformat
            reformat.remove_trailing_whitespace(QTextCursor(doc))

        # we only support local files for now
        filename = url.toLocalFile()
        b = backup.backup(filename)
        try:
            doc.save(url)
        except IOError as e:
            msg = _("{message}\n\n{strerror} ({errno})").format(
                message = _("Could not write to: {url}").format(url=filename),
                strerror = e.strerror,
                errno = e.errno)
            QMessageBox.critical(self, app.caption(_("Error")), msg)
            return False
        else:
            if b:
                backup.removeBackup(filename)
            recentfiles.add(doc.url())
        return True
예제 #6
0
    def saveDocument(self, doc, save_as=False):
        """ Saves the document, asking for a name if necessary.
        
        If save_as is True, a name is always asked.
        Returns True if saving succeeded.
        
        """
        if save_as or doc.url().isEmpty():
            filename = doc.url().toLocalFile()
            if filename:
                filetypes = app.filetypes(os.path.splitext(filename)[1])
            else:
                directory = app.basedir() # default directory to save to
                import documentinfo
                import ly.lex
                filename = os.path.join(directory, documentinfo.defaultfilename(doc))
                filetypes = app.filetypes(ly.lex.extensions[documentinfo.mode(doc)])
            caption = app.caption(_("dialog title", "Save File"))
            filename = QFileDialog.getSaveFileName(self, caption, filename, filetypes)
            if not filename:
                return False # cancelled
            url = QUrl.fromLocalFile(filename)
        else:
            url = doc.url()
        
        if QSettings().value("strip_trailing_whitespace", False, bool):
            import reformat
            reformat.remove_trailing_whitespace(QTextCursor(doc))

        # we only support local files for now
        filename = url.toLocalFile()
        b = backup.backup(filename)
        try:
            doc.save(url)
        except IOError as e:
            msg = _("{message}\n\n{strerror} ({errno})").format(
                message = _("Could not write to: {url}").format(url=filename),
                strerror = e.strerror,
                errno = e.errno)
            QMessageBox.critical(self, app.caption(_("Error")), msg)
            return False
        else:
            if b:
                backup.removeBackup(filename)
            recentfiles.add(doc.url())
        return True
예제 #7
0
    def openUrls(self, urls, encoding=None, ignore_errors=False):
        """Open a list of urls, using encoding if specified.

        Returns the list of documents that were successfully loaded.

        If encoding is None, the encoding is read from the document, defaulting
        to UTF-8.

        If ignore_errors is False (the default), an error message is given
        showing the url or urls that failed to load. If ignore_errors is True,
        load errors are silently ignored.

        If an url fails to load, a document is not created. To create an
        empty document with an url, use the document.Document constructor.
        Successfully loaded urls are added to the recent files.

        """
        docs = []
        failures = []
        for url in urls:
            try:
                doc = app.openUrl(url, encoding)
            except IOError as e:
                failures.append((url, e))
            else:
                docs.append(doc)
                recentfiles.add(url)
        if failures and not ignore_errors:
            if len(failures) == 1:
                url, e = failures[0]
                filename = url.toLocalFile()
                msg = _("{message}\n\n{strerror} ({errno})").format(
                    message = _("Could not read from: {url}").format(url=filename),
                    strerror = e.strerror,
                    errno = e.errno)
            else:
                msg = _("Could not read:") + "\n\n" + "\n".join(
                    "{url}: {strerror} ({errno})".format(
                        url = url.toLocalFile(),
                        strerror = e.strerror,
                        errno = e.errno) for url, e in failures)
            QMessageBox.critical(self, app.caption(_("Error")), msg)
        return docs
예제 #8
0
    def openUrls(self, urls, encoding=None, ignore_errors=False):
        """Open a list of urls, using encoding if specified.

        Returns the list of documents that were successfully loaded.

        If encoding is None, the encoding is read from the document, defaulting
        to UTF-8.

        If ignore_errors is False (the default), an error message is given
        showing the url or urls that failed to load. If ignore_errors is True,
        load errors are silently ignored.

        If an url fails to load, a document is not created. To create an
        empty document with an url, use the document.Document constructor.
        Successfully loaded urls are added to the recent files.

        """
        docs = []
        failures = []
        for url in urls:
            try:
                doc = app.openUrl(url, encoding)
            except IOError as e:
                failures.append((url, e))
            else:
                docs.append(doc)
                recentfiles.add(url)
        if failures and not ignore_errors:
            if len(failures) == 1:
                url, e = failures[0]
                filename = url.toLocalFile()
                msg = _("{message}\n\n{strerror} ({errno})").format(
                    message = _("Could not read from: {url}").format(url=filename),
                    strerror = e.strerror,
                    errno = e.errno)
            else:
                msg = _("Could not read:") + "\n\n" + "\n".join(
                    "{url}: {strerror} ({errno})".format(
                        url = url.toLocalFile(),
                        strerror = e.strerror,
                        errno = e.errno) for url, e in failures)
            QMessageBox.critical(self, app.caption(_("Error")), msg)
        return docs
예제 #9
0
 def openUrl(self, url, encoding=None):
     """Same as app.openUrl but adds url to recent files."""
     d = app.openUrl(url, encoding)
     recentfiles.add(url)
     return d
예제 #10
0
 def openUrl(self, url, encoding=None):
     """Same as app.openUrl but adds url to recent files."""
     d = app.openUrl(url, encoding)
     recentfiles.add(url)
     return d