def getFile(parent, title, cb, filter="*.*", dir=None, key=None, multi=False): "Ask the user for a file." assert not dir or not key if not dir: dirkey = key+"Directory" dir = aqt.mw.pm.profile.get(dirkey, "") else: dirkey = None d = QFileDialog(parent) mode = QFileDialog.ExistingFiles if multi else QFileDialog.ExistingFile d.setFileMode(mode) if os.path.exists(dir): d.setDirectory(dir) d.setWindowTitle(title) d.setNameFilter(filter) ret = [] def accept(): files = list(d.selectedFiles()) if dirkey: dir = os.path.dirname(files[0]) aqt.mw.pm.profile[dirkey] = dir result = files if multi else files[0] if cb: cb(result) ret.append(result) d.accepted.connect(accept) if key: restoreState(d, key) d.exec_() if key: saveState(d, key) return ret and ret[0]
def pickcmdpath(self): # mod of aqt.utils getFile d = QFileDialog() mode = QFileDialog.ExistingFile d.setFileMode(mode) if isWin: d.setDirectory("C:\\") if isMac: d.setDirectory("/Applications") else: d.setDirectory(os.path.expanduser("~")) d.setWindowTitle("Anki Add-on: Select Executable") if d.exec(): self.dialog.le_cmd.setText(d.selectedFiles()[0])
def browse_for_wiki(self): "Use a file browser dialog to replace the path to a wiki." dlg = QFileDialog(self, caption="Browse for wiki", filter="HTML files (*.html);;All files (*)") if self.form.type_.currentText().lower() == 'folder': mode = QFileDialog.Directory else: mode = QFileDialog.ExistingFile dlg.setFileMode(mode) retval = dlg.exec() if retval != 0: filename = dlg.selectedFiles()[0] self.form.path_.setText(filename)
def select_adhoc_script(): dlg = QFileDialog() dlg.setAcceptMode(QFileDialog.AcceptOpen) dlg.setFileMode(QFileDialog.Directory) if dlg.exec(): Parser.parse(dlg.selectedFiles()[0], AdHocExecution())