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)
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)
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
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 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 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 importMusicXML(self): """ Opens a MusicXML file. Converts it to ly by using musicxml2ly """ filetypes = app.filetypes('*.xml') caption = app.caption(_("dialog title", "Import a MusicXML file")) directory = os.path.dirname(self.mainwindow().currentDocument().url().toLocalFile()) or app.basedir() importfile = QFileDialog.getOpenFileName(self.mainwindow(), caption, directory, filetypes) if not importfile: return # the dialog was cancelled by user try: dlg = self._importDialog except AttributeError: from . import musicxml dlg = self._importDialog = musicxml.Dialog(self.mainwindow()) dlg.addAction(self.mainwindow().actionCollection.help_whatsthis) dlg.setWindowModality(Qt.WindowModal) dlg.setDocument(importfile) if dlg.exec_(): with qutil.busyCursor(): stdout, stderr = dlg.run_command() if stdout: #success lyfile = os.path.splitext(importfile)[0] + ".ly" self.createDocument(lyfile, stdout.decode('utf-8')) else: #failure to convert QMessageBox.critical(None, _("Error"), _("The file couldn't be converted. Error message:\n") + stderr)
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
def openDocument(self): """ Displays an open dialog to open one or more documents. """ ext = os.path.splitext(self.currentDocument().url().path())[1] filetypes = app.filetypes(ext) caption = app.caption(_("dialog title", "Open File")) directory = os.path.dirname(self.currentDocument().url().toLocalFile()) or app.basedir() files = QFileDialog.getOpenFileNames(self, caption, directory, filetypes) urls = [QUrl.fromLocalFile(filename) for filename in files] self.openDocuments(urls)
def file_open(): filetypes = app.filetypes('.ly') caption = app.caption(_("dialog title", "Open File")) directory = app.basedir() files = QFileDialog.getOpenFileNames(None, caption, directory, filetypes) if files: w = mainwindow() docs = [w.openUrl(QUrl.fromLocalFile(f)) for f in files] if docs: w.setCurrentDocument(docs[-1])
def file_open(): filetypes = app.filetypes(".ly") caption = app.caption(_("dialog title", "Open File")) directory = app.basedir() files = QFileDialog.getOpenFileNames(None, caption, directory, filetypes) if files: w = mainwindow() docs = [w.openUrl(QUrl.fromLocalFile(f)) for f in files] if docs: w.setCurrentDocument(docs[-1])
def file_open(): filetypes = app.filetypes('.ly') caption = app.caption(_("dialog title", "Open File")) directory = app.basedir() files = QFileDialog.getOpenFileNames(None, caption, directory, filetypes)[0] if files: w = mainwindow() w.openUrls([QUrl.fromLocalFile(f) for f in files]) if not app.documents: w.cleanStart()
def openDocument(self): """ Displays an open dialog to open one or more documents. """ ext = os.path.splitext(self.currentDocument().url().path())[1] filetypes = app.filetypes(ext) caption = app.caption(_("dialog title", "Open File")) directory = os.path.dirname( self.currentDocument().url().toLocalFile()) or app.basedir() files = QFileDialog.getOpenFileNames(self, caption, directory, filetypes) urls = [QUrl.fromLocalFile(filename) for filename in files] self.openDocuments(urls)
def openDocument(self): """ Displays an open dialog to open one or more documents. """ if app.documents: ext = os.path.splitext(self.currentDocument().url().path())[1] directory = os.path.dirname(self.currentDocument().url().toLocalFile()) or app.basedir() else: ext = ".ly" directory = app.basedir() filetypes = app.filetypes(ext) caption = app.caption(_("dialog title", "Open File")) files = QFileDialog.getOpenFileNames(self, caption, directory, filetypes)[0] urls = [QUrl.fromLocalFile(filename) for filename in files] docs = self.openUrls(urls) if docs: self.setCurrentDocument(docs[-1])
def insertFromFile(self): ext = os.path.splitext(self.currentDocument().url().path())[1] filetypes = app.filetypes(ext) caption = app.caption(_("dialog title", "Insert From File")) directory = os.path.dirname(self.currentDocument().url().toLocalFile()) or app.basedir() filename = QFileDialog.getOpenFileName(self, caption, directory, filetypes) if filename: try: data = open(filename).read() except (IOError, OSError) as err: QMessageBox.warning(self, app.caption(_("Error")), _("Can't read from source:\n\n{url}\n\n{error}").format( url=filename, error=err.strerror)) else: text = util.decode(data) self.currentView().textCursor().insertText(text)
def openDocument(self): """ Displays an open dialog to open one or more documents. """ d = self.currentDocument() if d: ext = os.path.splitext(d.url().path())[1] directory = os.path.dirname(d.url().toLocalFile()) or app.basedir() else: ext = ".ly" directory = app.basedir() filetypes = app.filetypes(ext) caption = app.caption(_("dialog title", "Open File")) files = QFileDialog.getOpenFileNames(self, caption, directory, filetypes)[0] urls = [QUrl.fromLocalFile(filename) for filename in files] docs = self.openUrls(urls) if docs: self.setCurrentDocument(docs[-1])
def insertFromFile(self): ext = os.path.splitext(self.currentDocument().url().path())[1] filetypes = app.filetypes(ext) caption = app.caption(_("dialog title", "Insert From File")) directory = os.path.dirname(self.currentDocument().url().toLocalFile()) or app.basedir() filename = QFileDialog.getOpenFileName(self, caption, directory, filetypes) if filename: try: with open(filename) as f: data = f.read() except IOError as e: msg = _("{message}\n\n{strerror} ({errno})").format( message = _("Could not read from: {url}").format(url=filename), strerror = e.strerror, errno = e.errno) QMessageBox.critical(self, app.caption(_("Error")), msg) else: text = util.decode(data) self.currentView().textCursor().insertText(text)
def insertFromFile(self): ext = os.path.splitext(self.currentDocument().url().path())[1] filetypes = app.filetypes(ext) caption = app.caption(_("dialog title", "Insert From File")) directory = os.path.dirname(self.currentDocument().url().toLocalFile()) or app.basedir() filename = QFileDialog.getOpenFileName(self, caption, directory, filetypes)[0] if filename: try: with open(filename, 'rb') as f: data = f.read() except IOError as e: msg = _("{message}\n\n{strerror} ({errno})").format( message = _("Could not read from: {url}").format(url=filename), strerror = e.strerror, errno = e.errno) QMessageBox.critical(self, app.caption(_("Error")), msg) else: text = util.universal_newlines(util.decode(data)) self.currentView().textCursor().insertText(text)
def insertFromFile(self): ext = os.path.splitext(self.currentDocument().url().path())[1] filetypes = app.filetypes(ext) caption = app.caption(_("dialog title", "Insert From File")) directory = os.path.dirname( self.currentDocument().url().toLocalFile()) or app.basedir() filename = QFileDialog.getOpenFileName(self, caption, directory, filetypes) if filename: try: data = open(filename).read() except (IOError, OSError) as err: QMessageBox.warning( self, app.caption(_("Error")), _("Can't read from source:\n\n{url}\n\n{error}").format( url=filename, error=err.strerror)) else: text = util.decode(data) self.currentView().textCursor().insertText(text)
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)