modal open dialogs

This commit is contained in:
Damien Elmes 2011-04-16 10:53:22 +09:00
parent 0eb56c27f9
commit 74e5122717
3 changed files with 46 additions and 43 deletions

View file

@ -637,10 +637,9 @@ class Editor(object):
" (*.jpg *.png *.gif *.tiff *.svg *.tif *.jpeg "+ " (*.jpg *.png *.gif *.tiff *.svg *.tif *.jpeg "+
"*.mp3 *.ogg *.wav *.avi *.ogv *.mpg *.mpeg *.mov *.mp4 " + "*.mp3 *.ogg *.wav *.avi *.ogv *.mpg *.mpeg *.mov *.mp4 " +
"*.mkv *.ogx *.ogv *.oga *.flv *.swf *.flac)") "*.mkv *.ogx *.ogv *.oga *.flv *.swf *.flac)")
file = getFile(self.widget, _("Add Media"), "media", key) def accept(file):
if not file: self.addMedia(file, canDelete=True)
return file = getFile(self.widget, _("Add Media"), accept, key, key="media")
self.addMedia(file, canDelete=True)
def addMedia(self, path, canDelete=False): def addMedia(self, path, canDelete=False):
html = self._addMedia(path, canDelete) html = self._addMedia(path, canDelete)

View file

@ -20,7 +20,7 @@ import anki.consts
import aqt, aqt.progress, aqt.webview import aqt, aqt.progress, aqt.webview
from aqt.utils import saveGeom, restoreGeom, showInfo, showWarning, \ from aqt.utils import saveGeom, restoreGeom, showInfo, showWarning, \
saveState, restoreState, getOnlyText, askUser, GetTextDialog, \ saveState, restoreState, getOnlyText, askUser, GetTextDialog, \
askUserDialog, applyStyles, getText, showText, showCritical askUserDialog, applyStyles, getText, showText, showCritical, getFile
config = aqt.config config = aqt.config
@ -307,23 +307,20 @@ Debug info:\n%s""") % traceback.format_exc(), help="DeckErrors")
def onOpen(self): def onOpen(self):
self.raiseMain() self.raiseMain()
key = _("Deck files (*.anki)") filter = _("Deck files (*.anki)")
defaultDir = self.getDefaultDir() if self.deck:
file = QFileDialog.getOpenFileName(self, _("Open deck"), dir = os.path.dirname(self.deck.path)
defaultDir, key)
file = unicode(file)
if not file:
return False
ret = self.loadDeck(file)
if not ret:
if ret is None:
showWarning(_("Unable to load file."))
self.deck = None
return False
else: else:
self.updateRecentFiles(file) dir = self.config['documentDir']
self.browserLastRefreshed = 0 def accept(file):
return True ret = self.loadDeck(file)
if not ret:
showWarning(_("Unable to load file."))
self.deck = None
else:
self.updateRecentFiles(file)
self.browserLastRefreshed = 0
getFile(self, _("Open deck"), accept, filter, dir)
def maybeLoadLastDeck(self, args): def maybeLoadLastDeck(self, args):
"Open the last deck if possible." "Open the last deck if possible."
@ -338,17 +335,6 @@ Debug info:\n%s""") % traceback.format_exc(), help="DeckErrors")
if r: if r:
return r return r
def getDefaultDir(self, save=False):
"Try and get default dir from most recently opened file."
defaultDir = ""
if self.config['recentDeckPaths']:
latest = self.config['recentDeckPaths'][0]
defaultDir = os.path.dirname(latest)
else:
defaultDir = unicode(os.path.expanduser("~/"),
sys.getfilesystemencoding())
return defaultDir
def updateRecentFiles(self, path): def updateRecentFiles(self, path):
"Add the current deck to the list of recent files." "Add the current deck to the list of recent files."
path = os.path.normpath(path) path = os.path.normpath(path)

View file

@ -13,11 +13,11 @@ def openLink(link):
def showWarning(text, parent=None, help=""): def showWarning(text, parent=None, help=""):
"Show a small warning with an OK button." "Show a small warning with an OK button."
return showInfo(text, parent, help, QMessageBox.warning) return showInfo(text, parent, help, "warning")
def showCritical(text, parent=None, help=""): def showCritical(text, parent=None, help=""):
"Show a small critical error with an OK button." "Show a small critical error with an OK button."
return showInfo(text, parent, help, QMessageBox.critical) return showInfo(text, parent, help, "critical")
def showInfo(text, parent=None, help="", type="info"): def showInfo(text, parent=None, help="", type="info"):
"Show a small info window with an OK button." "Show a small info window with an OK button."
@ -203,15 +203,33 @@ def getTag(parent, deck, question, tags="user", **kwargs):
te.setDeck(deck) te.setDeck(deck)
return getText(question, parent, edit=te, **kwargs) return getText(question, parent, edit=te, **kwargs)
def getFile(parent, title, dir, key): # File handling
"Ask the user for a file. Use DIR as config variable." ######################################################################
dirkey = dir+"Directory"
file = unicode(QFileDialog.getOpenFileName( def getFile(parent, title, cb, filter="*.*", dir=None, key=None):
parent, title, aqt.mw.config.get(dirkey, ""), key)) "Ask the user for a file."
if file: assert not dir or not key
dir = os.path.dirname(file) if not dir:
aqt.mw.config[dirkey] = dir dirkey = key+"Directory"
return file dir = aqt.mw.config.get(dirkey, "")
else:
dirkey = None
d = QFileDialog(parent)
d.setWindowModality(Qt.WindowModal)
d.setFileMode(QFileDialog.ExistingFile)
d.setDirectory(dir)
d.setWindowTitle(title)
d.setNameFilter(filter)
d.show()
def accept():
# work around an osx crash
aqt.mw.app.processEvents()
file = unicode(list(d.selectedFiles())[0])
if dirkey:
dir = os.path.dirname(file)
aqt.mw.config[dirkey] = dir
cb(file)
d.connect(d, SIGNAL("accepted()"), accept)
def getSaveFile(parent, title, dir, key, ext): def getSaveFile(parent, title, dir, key, ext):
"Ask the user for a file to save. Use DIR as config variable." "Ask the user for a file to save. Use DIR as config variable."