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(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, "w") 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)
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(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, "w") as f: f.write(data) except (IOError, OSError) as err: QMessageBox.warning(self, app.caption(_("Error")), _("Can't write to destination:\n\n{url}\n\n{error}").format( url=filename, error=err.strerror))
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(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, "w") as f: f.write(data) except (IOError, OSError) as err: QMessageBox.warning( self, app.caption(_("Error")), _("Can't write to destination:\n\n{url}\n\n{error}").format( url=filename, error=err.strerror))
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)
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)
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)