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 "+
"*.mp3 *.ogg *.wav *.avi *.ogv *.mpg *.mpeg *.mov *.mp4 " +
"*.mkv *.ogx *.ogv *.oga *.flv *.swf *.flac)")
file = getFile(self.widget, _("Add Media"), "media", key)
if not file:
return
self.addMedia(file, canDelete=True)
def accept(file):
self.addMedia(file, canDelete=True)
file = getFile(self.widget, _("Add Media"), accept, key, key="media")
def addMedia(self, path, canDelete=False):
html = self._addMedia(path, canDelete)

View file

@ -20,7 +20,7 @@ import anki.consts
import aqt, aqt.progress, aqt.webview
from aqt.utils import saveGeom, restoreGeom, showInfo, showWarning, \
saveState, restoreState, getOnlyText, askUser, GetTextDialog, \
askUserDialog, applyStyles, getText, showText, showCritical
askUserDialog, applyStyles, getText, showText, showCritical, getFile
config = aqt.config
@ -307,23 +307,20 @@ Debug info:\n%s""") % traceback.format_exc(), help="DeckErrors")
def onOpen(self):
self.raiseMain()
key = _("Deck files (*.anki)")
defaultDir = self.getDefaultDir()
file = QFileDialog.getOpenFileName(self, _("Open deck"),
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
filter = _("Deck files (*.anki)")
if self.deck:
dir = os.path.dirname(self.deck.path)
else:
self.updateRecentFiles(file)
self.browserLastRefreshed = 0
return True
dir = self.config['documentDir']
def accept(file):
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):
"Open the last deck if possible."
@ -338,17 +335,6 @@ Debug info:\n%s""") % traceback.format_exc(), help="DeckErrors")
if 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):
"Add the current deck to the list of recent files."
path = os.path.normpath(path)

View file

@ -13,11 +13,11 @@ def openLink(link):
def showWarning(text, parent=None, help=""):
"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=""):
"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"):
"Show a small info window with an OK button."
@ -203,15 +203,33 @@ def getTag(parent, deck, question, tags="user", **kwargs):
te.setDeck(deck)
return getText(question, parent, edit=te, **kwargs)
def getFile(parent, title, dir, key):
"Ask the user for a file. Use DIR as config variable."
dirkey = dir+"Directory"
file = unicode(QFileDialog.getOpenFileName(
parent, title, aqt.mw.config.get(dirkey, ""), key))
if file:
dir = os.path.dirname(file)
aqt.mw.config[dirkey] = dir
return file
# File handling
######################################################################
def getFile(parent, title, cb, filter="*.*", dir=None, key=None):
"Ask the user for a file."
assert not dir or not key
if not dir:
dirkey = key+"Directory"
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):
"Ask the user for a file to save. Use DIR as config variable."