The old delimiter is now kept, when cancel button is clicked

This commit is contained in:
Benjamin K 2021-03-04 19:39:43 +01:00
parent 3ba08d1189
commit a066cee326

View File

@ -10,6 +10,8 @@ import zipfile
from concurrent.futures import Future from concurrent.futures import Future
from typing import Any, Dict, Optional from typing import Any, Dict, Optional
from qt.aqt.utils import getText
import anki.importing as importing import anki.importing as importing
import aqt.deckchooser import aqt.deckchooser
import aqt.forms import aqt.forms
@ -82,6 +84,8 @@ class ChangeMap(QDialog):
class ImportDialog(QDialog): class ImportDialog(QDialog):
_DEFAULT_FILE_DELIMITER = "\t"
def __init__(self, mw: AnkiQt, importer: Any) -> None: def __init__(self, mw: AnkiQt, importer: Any) -> None:
QDialog.__init__(self, mw, Qt.Window) QDialog.__init__(self, mw, Qt.Window)
self.mw = mw self.mw = mw
@ -122,28 +126,39 @@ class ImportDialog(QDialog):
self.showMapping() self.showMapping()
def onDelimiter(self) -> None: def onDelimiter(self) -> None:
str = (
getOnlyText( # Open a modal dialog to enter an delimiter
tr(TR.IMPORTING_BY_DEFAULT_ANKI_WILL_DETECT_THE), # Todo/Idea Constrain the maximum width, so it doesnt take up that much screen space
self, delim, ok = getText(
help=HelpPage.IMPORTING, tr(TR.IMPORTING_BY_DEFAULT_ANKI_WILL_DETECT_THE),
) self,
or "\t" help=HelpPage.IMPORTING,
) )
str = str.replace("\\t", "\t")
if len(str) > 1:
showWarning(
tr(TR.IMPORTING_MULTICHARACTER_SEPARATORS_ARE_NOT_SUPPORTED_PLEASE)
)
return
self.hideMapping()
def updateDelim() -> None: # If the modal dialog has been confirmed, update the delimiter
self.importer.delimiter = str if ok:
self.importer.updateDelimiter() # Check if the entered value is valid and if not fallback to default
# at the moment every single character entry as well as '\t' is valid
self.showMapping(hook=updateDelim) delim = delim if len(delim) > 0 else self._DEFAULT_FILE_DELIMITER
self.updateDelimiterButtonText() delim = delim.replace("\\t", "\t") # un-escape it
if len(delim) > 1:
showWarning(
tr(TR.IMPORTING_MULTICHARACTER_SEPARATORS_ARE_NOT_SUPPORTED_PLEASE)
)
return
self.hideMapping()
def updateDelim() -> None:
self.importer.delimiter = delim
self.importer.updateDelimiter()
self.updateDelimiterButtonText()
self.showMapping(hook=updateDelim)
else:
# If the operation has been canceled, do not do anything
pass
def updateDelimiterButtonText(self) -> None: def updateDelimiterButtonText(self) -> None:
if not self.importer.needDelimiter: if not self.importer.needDelimiter: