Esempio n. 1
0
 def saveCopyAs(self):
     import ly.lex
     doc = self.currentDocument()
     if not self.currentView().textCursor().hasSelection():
         import documentinfo
         mode = documentinfo.mode(doc)
         data = doc.encodedText()
         caption = app.caption(_("dialog title", "Save Copy"))
     else:
         import fileinfo
         text = self.currentView().textCursor().selection().toPlainText()
         mode = fileinfo.textmode(text)
         data = util.encode(util.platform_newlines(text))
         caption = app.caption(_("dialog title", "Save Selection"))
     filetypes = app.filetypes(ly.lex.extensions[mode])
     dirname = os.path.dirname(doc.url().toLocalFile()) or app.basedir()
     filename = QFileDialog.getSaveFileName(self, caption, dirname, filetypes)[0]
     if not filename:
         return # cancelled
     try:
         with open(filename, "wb") as f:
             f.write(data)
     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)
Esempio n. 2
0
 def saveCopyAs(self):
     import ly.lex
     doc = self.currentDocument()
     if not self.currentView().textCursor().hasSelection():
         import documentinfo
         mode = documentinfo.mode(doc)
         data = doc.encodedText()
         caption = app.caption(_("dialog title", "Save Copy"))
     else:
         import fileinfo
         text = self.currentView().textCursor().selection().toPlainText()
         mode = fileinfo.textmode(text)
         data = util.encode(util.platform_newlines(text))
         caption = app.caption(_("dialog title", "Save Selection"))
     filetypes = app.filetypes(ly.lex.extensions[mode])
     dirname = os.path.dirname(doc.url().toLocalFile()) or app.basedir()
     filename = QFileDialog.getSaveFileName(self, caption, dirname, filetypes)
     if not filename:
         return # cancelled
     try:
         with open(filename, "wb") as f:
             f.write(data)
     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)
Esempio n. 3
0
    def run(self):
        """Runs convert-ly (again)."""
        fromVersion = self.fromVersion.text()
        toVersion = self.toVersion.text()
        if not fromVersion or not toVersion:
            self.messages.setPlainText(
                _("Both 'from' and 'to' versions need to be set."))
            return
        info = self._info
        command = info.toolcommand(info.ly_tool('convert-ly'))
        command += ['-f', fromVersion, '-t', toVersion, '-']

        # if the user wants english messages, do it also here: LANGUAGE=C
        env = None
        if os.name == "nt":
            # Python 2.7 subprocess on Windows chokes on unicode in env
            env = util.bytes_environ()
        else:
            env = dict(os.environ)
        if sys.platform.startswith('darwin'):
            try:
                del env['PYTHONHOME']
            except KeyError:
                pass
            try:
                del env['PYTHONPATH']
            except KeyError:
                pass
        if QSettings().value("lilypond_settings/no_translation", False, bool):
            if os.name == "nt":
                # Python 2.7 subprocess on Windows chokes on unicode in env
                env[b'LANGUAGE'] = b'C'
            else:
                env['LANGUAGE'] = 'C'

        with qutil.busyCursor():
            try:
                proc = subprocess.Popen(command,
                                        env=env,
                                        stdin=subprocess.PIPE,
                                        stdout=subprocess.PIPE,
                                        stderr=subprocess.PIPE)
                out, err = proc.communicate(
                    util.platform_newlines(self._text).encode(self._encoding))
            except OSError as e:
                self.messages.setPlainText(
                    _("Could not start {convert_ly}:\n\n"
                      "{message}\n").format(convert_ly=command[0], message=e))
                return
            out = util.universal_newlines(out.decode('UTF-8'))
            err = util.universal_newlines(err.decode('UTF-8'))
            self.messages.setPlainText(err)
            self.setConvertedText(out)
            self.setDiffText(out)
            if not out or self._convertedtext == self._text:
                self.messages.append('\n' +
                                     _("The document has not been changed."))
Esempio n. 4
0
 def encodedText(self):
     """Returns the text of the document 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())
Esempio n. 5
0
 def encodedText(self):
     """Returns the text of the document 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())
Esempio n. 6
0
 def run(self):
     """Runs convert-ly (again)."""
     fromVersion = self.fromVersion.text()
     toVersion = self.toVersion.text()
     if not fromVersion or not toVersion:
         self.messages.setPlainText(_(
             "Both 'from' and 'to' versions need to be set."))
         return
     info = self._info
     command = info.toolcommand(info.ly_tool('convert-ly'))
     command += ['-f', fromVersion, '-t', toVersion, '-']
     
     # if the user wants english messages, do it also here: LANGUAGE=C
     env = None
     if os.name == "nt":
         # Python 2.7 subprocess on Windows chokes on unicode in env
         env = util.bytes_environ()
     else:
         env = dict(os.environ)
     if sys.platform.startswith('darwin'):
         try:
             del env['PYTHONHOME']
         except KeyError:
             pass
         try:
             del env['PYTHONPATH']
         except KeyError:
             pass
     if QSettings().value("lilypond_settings/no_translation", False, bool):
         if os.name == "nt":
             # Python 2.7 subprocess on Windows chokes on unicode in env
             env[b'LANGUAGE'] = b'C'
         else:
             env['LANGUAGE'] = 'C'
     
     with qutil.busyCursor():
         try:
             proc = subprocess.Popen(command,
                 env = env,
                 stdin = subprocess.PIPE,
                 stdout = subprocess.PIPE,
                 stderr = subprocess.PIPE)
             out, err = proc.communicate(util.platform_newlines(self._text).encode(self._encoding))
         except OSError as e:
             self.messages.setPlainText(_(
                 "Could not start {convert_ly}:\n\n"
                 "{message}\n").format(convert_ly = command[0], message = e))
             return
         out = util.universal_newlines(out.decode('UTF-8'))
         err = util.universal_newlines(err.decode('UTF-8'))
         self.messages.setPlainText(err)
         self.setConvertedText(out)
         self.setDiffText(out)
         if not out or self._convertedtext == self._text:
             self.messages.append('\n' + _("The document has not been changed."))
Esempio n. 7
0
def move_to_include_file(cursor, parent_widget=None):
    """Opens a dialog to save the cursor's selection to a file.

    The cursor's selection is then replaced with an \\include statement.
    This function does its best to supply a good default filename and
    use it correctly in a relative \\include statement.

    Of course it only works well if the document already has a filename.

    """
    doc = cursor.document()
    text = cursor.selection().toPlainText()
    mode = fileinfo.textmode(text)
    caption = app.caption(_("dialog title", "Move to include file"))
    filetypes = app.filetypes(ly.lex.extensions[mode])
    name, ext = os.path.splitext(os.path.basename(doc.url().path()))
    if not ext or mode == "lilypond":
        ext = ".ily"
        version = documentinfo.docinfo(doc).version_string()
        if version:
            text = '\\version "{0}"\n\n{1}'.format(version, text)
    docname = name + "-include" + ext
    dirname = os.path.dirname(doc.url().toLocalFile()) or app.basedir()
    filename = os.path.join(dirname, docname)
    filename = QFileDialog.getSaveFileName(parent_widget, caption, filename,
                                           filetypes)[0]
    if not filename:
        return  # cancelled
    data = util.encode(util.platform_newlines(text))
    try:
        with open(filename, "wb") as f:
            f.write(data)
    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(parent_widget, app.caption(_("Error")), msg)
        return
    filename = os.path.relpath(filename, dirname)
    command = '\\include "{0}"\n'.format(filename)
    cursor.insertText(command)
Esempio n. 8
0
def move_to_include_file(cursor, parent_widget=None):
    """Opens a dialog to save the cursor's selection to a file.
    
    The cursor's selection is then replaced with an \\include statement.
    This function does its best to supply a good default filename and
    use it correctly in a relative \\include statement.
    
    Of course it only works well if the document already has a filename.
    
    """
    doc = cursor.document()
    text = cursor.selection().toPlainText()
    mode = fileinfo.textmode(text)
    caption = app.caption(_("dialog title", "Move to include file"))
    filetypes = app.filetypes(ly.lex.extensions[mode])
    name, ext = os.path.splitext(os.path.basename(doc.url().path()))
    if not ext or mode == "lilypond":
        ext = ".ily"
        version = documentinfo.docinfo(doc).version_string()
        if version:
            text = '\\version "{0}"\n\n{1}'.format(version, text)
    docname = name + "-include" + ext
    dirname = os.path.dirname(doc.url().toLocalFile()) or app.basedir()
    filename = os.path.join(dirname, docname)
    filename = QFileDialog.getSaveFileName(parent_widget, caption, filename, filetypes)[0]
    if not filename:
        return # cancelled
    data = util.encode(util.platform_newlines(text))
    try:
        with open(filename, "wb") as f:
            f.write(data)
    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
    filename = os.path.relpath(filename, dirname)
    command = '\\include "{0}"\n'.format(filename)
    cursor.insertText(command)