def openFile(self, path=None): if not path: fileFormat = self.tr("UFO Fonts {}") if platformSpecific.treatPackageAsFile(): ext = "(*.ufo)" else: ext = "(metainfo.plist)" path, _ = QFileDialog.getOpenFileName( self.activeWindow(), self.tr("Open File"), '', fileFormat.format(ext) ) if not path: return if ".plist" in path: path = os.path.dirname(path) path = os.path.normpath(path) for widget in self.topLevelWidgets(): if isinstance(widget, FontWindow): font = widget.font_() if font is not None and font.path == path: widget.raise_() return try: font = TFont(path) window = FontWindow(font) except Exception as e: msg = self.tr( "There was an issue when opening the font at {}.".format( path)) errorReports.showCriticalException(e, msg) return window.show() self.setCurrentFile(font.path)
def _openFile(self, path=None, openFile=True, importFile=False): if not path: fileFormats = [] supportedFiles = "" if openFile: packageAsFile = platformSpecific.treatPackageAsFile() if packageAsFile: ufoFormat = "*.ufo" tfExtFormat = "*.tfExt" else: ufoFormat = "metainfo.plist" tfExtFormat = "info.plist" fileFormats.extend( [ self.tr("UFO Fonts {}").format("(%s)" % ufoFormat), self.tr("TruFont Extension {}").format("(%s)" % tfExtFormat), ] ) supportedFiles += ufoFormat + " " + tfExtFormat + " " if importFile: # TODO: systematize this fileFormats.extend( [ self.tr("OpenType Font file {}").format("(*.otf *.ttf)"), self.tr("Type1 Font file {}").format("(*.pfa *.pfb)"), self.tr("ttx Font file {}").format("(*.ttx)"), self.tr("WOFF Font file {}").format("(*.woff)"), ] ) supportedFiles += "*.otf *.pfa *.pfb *.ttf *.ttx *.woff" fileFormats.extend( [ self.tr("All supported files {}").format("(%s)" % supportedFiles.rstrip()), self.tr("All files {}").format("(*.*)"), ] ) title = self.tr("Open File") if openFile else self.tr("Import File") path, _ = QFileDialog.getOpenFileName( self.activeWindow(), title, None, ";;".join(fileFormats), fileFormats[-2] ) if not path: return # sanitize path = os.path.normpath(path) if ".plist" in path: path = os.path.dirname(path) ext = os.path.splitext(path)[1] if ext == ".ufo": self._loadUFO(path) elif ext == ".tfExt": self._loadExt(path) else: self._loadBinary(path)
def _openFile(self, path=None, openFile=True, importFile=False): if not path: # formats fileFormats = [] supportedFiles = "" if openFile: packageAsFile = platformSpecific.treatPackageAsFile() if packageAsFile: ufoFormat = "*.ufo" tfExtFormat = "*.tfExt" else: ufoFormat = "metainfo.plist" tfExtFormat = "info.plist" fileFormats.extend([ self.tr("UFO Fonts {}").format("(%s)" % ufoFormat), self.tr("TruFont Extension {}").format("(%s)" % tfExtFormat), ]) supportedFiles += f"{ufoFormat} {tfExtFormat} " if importFile: # TODO: systematize this fileFormats.extend([ self.tr("OpenType Font file {}").format("(*.otf *.ttf)"), self.tr("Type1 Font file {}").format("(*.pfa *.pfb)"), self.tr("ttx Font file {}").format("(*.ttx)"), self.tr("WOFF Font file {}").format("(*.woff *.woff2)"), ]) supportedFiles += "*.otf *.pfa *.pfb *.ttf *.ttx *.woff" fileFormats.extend([ self.tr("All supported files {}").format( "(%s)" % supportedFiles.rstrip()), self.tr("All files {}").format("(*.*)"), ]) # dialog importKey = importFile and not openFile state = (settings.openFileDialogState() if not importKey else settings.importFileDialogState()) directory = (None if state else QStandardPaths.standardLocations( QStandardPaths.DocumentsLocation)[0]) title = self.tr("Open File") if openFile else self.tr( "Import File") dialog = QFileDialog(self.activeWindow(), title, directory, ";;".join(fileFormats)) if state: dialog.restoreState(state) dialog.setAcceptMode(QFileDialog.AcceptOpen) dialog.setFileMode(QFileDialog.ExistingFile) dialog.setNameFilter(fileFormats[-2]) ret = dialog.exec_() # save current directory # TODO: should open w/o file chooser also update current dir? state = dialog.saveState() if importKey: settings.setImportFileDialogState(directory) else: settings.setOpenFileDialogState(directory) # cancelled? if not ret: return path = dialog.selectedFiles()[0] # sanitize path = os.path.normpath(path) if ".plist" in os.path.basename(path): path = os.path.dirname(path) ext = os.path.splitext(path)[1] if ext == ".ufo": self._loadUFO(path) elif ext == ".tfExt": self._loadExt(path) else: self._loadBinary(path)
def _openFile(self, path=None, openFile=True, importFile=False): if not path: # formats fileFormats = [] supportedFiles = "" if openFile: packageAsFile = platformSpecific.treatPackageAsFile() if packageAsFile: ufoFormat = "*.ufo" tfExtFormat = "*.tfExt" else: ufoFormat = "metainfo.plist" tfExtFormat = "info.plist" fileFormats.extend([ self.tr("UFO Fonts {}").format("(%s)" % ufoFormat), self.tr("TruFont Extension {}").format( "(%s)" % tfExtFormat) ]) supportedFiles += "{} {} ".format(ufoFormat, tfExtFormat) if importFile: # TODO: systematize this fileFormats.extend([ self.tr("OpenType Font file {}").format("(*.otf *.ttf)"), self.tr("Type1 Font file {}").format("(*.pfa *.pfb)"), self.tr("ttx Font file {}").format("(*.ttx)"), self.tr("WOFF Font file {}").format("(*.woff)"), ]) supportedFiles += "*.otf *.pfa *.pfb *.ttf *.ttx *.woff" fileFormats.extend([ self.tr("All supported files {}").format( "(%s)" % supportedFiles.rstrip()), self.tr("All files {}").format("(*.*)"), ]) # dialog importKey = importFile and not openFile state = settings.openFileDialogState( ) if not importKey else settings.importFileDialogState() directory = None if state else QStandardPaths.standardLocations( QStandardPaths.DocumentsLocation)[0] title = self.tr( "Open File") if openFile else self.tr("Import File") dialog = QFileDialog( self.activeWindow(), title, directory, ";;".join(fileFormats)) if state: dialog.restoreState(state) dialog.setAcceptMode(QFileDialog.AcceptOpen) dialog.setFileMode(QFileDialog.ExistingFile) dialog.setNameFilter(fileFormats[-2]) ret = dialog.exec_() # save current directory # TODO: should open w/o file chooser also update current dir? state = dialog.saveState() if importKey: settings.setImportFileDialogState(directory) else: settings.setOpenFileDialogState(directory) # cancelled? if not ret: return path = dialog.selectedFiles()[0] # sanitize path = os.path.normpath(path) if ".plist" in path: path = os.path.dirname(path) ext = os.path.splitext(path)[1] if ext == ".ufo": self._loadUFO(path) elif ext == ".tfExt": self._loadExt(path) else: self._loadBinary(path)