Skip to content
This repository has been archived by the owner on Mar 31, 2021. It is now read-only.

Commit

Permalink
Confirmation popup before closing the occlusion editor
Browse files Browse the repository at this point in the history
Fixes glutanimate#72.
Note that this confirmation popup is shown in some cases where I don't
think it's necessary. If the user adds occlusions, clicks on one of the
buttons to create the cards and then closes the editor dialog they are
prompted for confirmation.
The only way I can think of to prevent this is to reset the svg-edit
undo stack when the user clicks on the button, but I'm not sure if
that's acceptable.
  • Loading branch information
5hir0kur0 committed Jan 4, 2020
1 parent ad092ef commit 65ef508
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 4 deletions.
9 changes: 7 additions & 2 deletions src/image_occlusion_enhanced/add.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

from aqt.qt import *

from aqt import mw
from aqt import mw, dialogs
from aqt.utils import tooltip, showWarning

from .ngen import *
Expand Down Expand Up @@ -189,7 +189,7 @@ def callImgOccEdit(self, width, height):
flds = self.mflds
deck = mw.col.decks.nameOrNone(opref["did"])

dialog = ImgOccEdit(self, self.ed.parentWindow)
dialog = dialogs.open('ImgOccEdit', self, self.ed.parentWindow)
dialog.setupFields(flds)
dialog.switchToMode(self.mode)
self.imgoccedit = dialog
Expand Down Expand Up @@ -247,6 +247,11 @@ def onSvgEditLoaded():
ioInfo("obsolete_aa", parent=dialog)
dialog.showSvgEdit(True)
dialog.fitImageCanvas()
# The only thing undo does in edit mode is removing all
# occlusions. If the undo stack is not empty at the beginning
# there will always be a confirmation popup before the user
# can close the dialog.
dialog.svg_edit.eval("svgCanvas.undoMgr.resetUndoStack()")

dialog.svg_edit.runOnLoaded(onSvgEditLoaded)
dialog.visible = True
Expand Down
32 changes: 30 additions & 2 deletions src/image_occlusion_enhanced/editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

from aqt.qt import *

from aqt import mw, webview, deckchooser, tagedit, sip
from aqt import mw, webview, deckchooser, tagedit, sip, dialogs
from aqt.utils import saveGeom, restoreGeom
from anki.hooks import addHook, remHook

Expand Down Expand Up @@ -89,14 +89,42 @@ def __init__(self, imgoccadd, parent):
restoreGeom(self, "imgoccedit")
addHook("unloadProfile", self.onProfileUnload)

def closeWithCallback(self, callback):
def ifCanClose():
self._close()
callback()

self.askIfClose(ifCanClose)

def askIfClose(self, callback):
from aqt.utils import askUser
from anki.lang import _

def js_callback(size):
canClose = not size or size == 0 \
or (size > 0 and
askUser(_("Close and lose current input?"),
defaultno=True))
if canClose:
callback()

self.svg_edit.evalWithCallback('svgCanvas.undoMgr.getUndoStackSize()',
js_callback)

def closeEvent(self, event):
event.ignore()
self.askIfClose(lambda: self._close())

def _close(self):
if mw.pm.profile is not None:
self.deckChooser.cleanup()
saveGeom(self, "imgoccedit")
self.visible = False
self.svg_edit = None
del(self.svg_edit_anim) # might not be gc'd
if hasattr(self, "svg_edit_anim"):
del(self.svg_edit_anim) # might not be gc'd
remHook("unloadProfile", self.onProfileUnload)
dialogs.markClosed("ImgOccEdit")
QDialog.reject(self)

def onProfileUnload(self):
Expand Down
5 changes: 5 additions & 0 deletions src/image_occlusion_enhanced/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from aqt.qt import *

from aqt import mw
from aqt import dialogs
from aqt.editor import Editor, EditorWebView
from aqt.addcards import AddCards
from aqt.editcurrent import EditCurrent
Expand All @@ -35,6 +36,7 @@
from .add import ImgOccAdd
from .options import ImgOccOpts
from .dialogs import ioHelp, ioCritical
from .editor import ImgOccEdit

logging.basicConfig(stream=sys.stdout, level=logging.ERROR)

Expand Down Expand Up @@ -269,3 +271,6 @@ def onShowAnswer(self, _old):
Reviewer._keyHandler = wrap(Reviewer._keyHandler, newKeyHandler, "before")
else:
addHook("reviewStateShortcuts", onReviewerStateShortcuts)

# Set up dialog
dialogs._dialogs['ImgOccEdit'] = [ImgOccEdit, None]

0 comments on commit 65ef508

Please sign in to comment.