Skip to content

Commit 3bb260b

Browse files
committed
gui_qt/dictionaries: cache the file dialogs directory
Default the initial directory to the configuration folder, and the remember and re-use on subsequent invocations.
1 parent 8610b6e commit 3bb260b

File tree

1 file changed

+33
-27
lines changed

1 file changed

+33
-27
lines changed

plover/gui_qt/dictionaries_widget.py

Lines changed: 33 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -58,28 +58,6 @@ def _new_dictionary(filename):
5858
except Exception as e:
5959
raise Exception('creating dictionary %s failed. %s' % (filename, e)) from e
6060

61-
def _get_dictionary_save_name(parent_widget, title, default_name=None,
62-
default_extensions=(), initial_filename=None):
63-
if default_name is not None:
64-
# Default to a writable dictionary format.
65-
writable_extensions = set(_dictionary_formats(include_readonly=False))
66-
default_name += '.' + next((e for e in default_extensions
67-
if e in writable_extensions),
68-
'json')
69-
default_name = os.path.join(CONFIG_DIR, default_name)
70-
else:
71-
default_name = CONFIG_DIR
72-
new_filename = QFileDialog.getSaveFileName(
73-
parent=parent_widget, caption=title, directory=default_name,
74-
filter=_dictionary_filters(include_readonly=False),
75-
)[0]
76-
if not new_filename:
77-
return None
78-
new_filename = normalize_path(new_filename)
79-
if new_filename == initial_filename:
80-
return None
81-
return new_filename
82-
8361

8462
class DictionariesWidget(QWidget, Ui_DictionariesWidget):
8563

@@ -95,6 +73,8 @@ def __init__(self, *args, **kwargs):
9573
self._config_dictionaries = {}
9674
self._loaded_dictionaries = {}
9775
self._reverse_order = False
76+
# The save/open/new dialogs will open on that directory.
77+
self._file_dialogs_directory = CONFIG_DIR
9878
for action in (
9979
self.action_Undo,
10080
self.action_EditDictionaries,
@@ -381,6 +361,29 @@ def on_edit_dictionaries(self):
381361
assert selection
382362
self._edit([self._config_dictionaries[row] for row in selection])
383363

364+
def _get_dictionary_save_name(self, title, default_name=None,
365+
default_extensions=(), initial_filename=None):
366+
if default_name is not None:
367+
# Default to a writable dictionary format.
368+
writable_extensions = set(_dictionary_formats(include_readonly=False))
369+
default_name += '.' + next((e for e in default_extensions
370+
if e in writable_extensions),
371+
'json')
372+
default_name = os.path.join(self._file_dialogs_directory, default_name)
373+
else:
374+
default_name = self._file_dialogs_directory
375+
new_filename = QFileDialog.getSaveFileName(
376+
parent=self, caption=title, directory=default_name,
377+
filter=_dictionary_filters(include_readonly=False),
378+
)[0]
379+
if not new_filename:
380+
return None
381+
new_filename = normalize_path(new_filename)
382+
self._file_dialogs_directory = os.path.dirname(new_filename)
383+
if new_filename == initial_filename:
384+
return None
385+
return new_filename
386+
384387
def _copy_dictionaries(self, dictionaries_list):
385388
need_reload = False
386389
title_template = _('Save a copy of {name} as...')
@@ -389,8 +392,8 @@ def _copy_dictionaries(self, dictionaries_list):
389392
title = title_template.format(name=dictionary.short_path)
390393
name, ext = os.path.splitext(os.path.basename(dictionary.path))
391394
default_name = default_name_template.format(name=name)
392-
new_filename = _get_dictionary_save_name(self, title, default_name, [ext[1:]],
393-
initial_filename=dictionary.path)
395+
new_filename = self._get_dictionary_save_name(title, default_name, [ext[1:]],
396+
initial_filename=dictionary.path)
394397
if new_filename is None:
395398
continue
396399
with _new_dictionary(new_filename) as d:
@@ -405,7 +408,7 @@ def _merge_dictionaries(self, dictionaries_list):
405408
default_name = ' + '.join(names)
406409
default_exts = list(dict.fromkeys(e[1:] for e in exts))
407410
title = _('Merge {names} as...').format(names=default_name)
408-
new_filename = _get_dictionary_save_name(self, title, default_name, default_exts)
411+
new_filename = self._get_dictionary_save_name(title, default_name, default_exts)
409412
if new_filename is None:
410413
return False
411414
with _new_dictionary(new_filename) as d:
@@ -447,11 +450,14 @@ def on_add_dictionaries(self):
447450

448451
def _add_existing_dictionaries(self):
449452
new_filenames = QFileDialog.getOpenFileNames(
450-
self, _('Add dictionaries'), None, _dictionary_filters(),
453+
parent=self, caption=_('Add dictionaries'),
454+
directory=self._file_dialogs_directory,
455+
filter=_dictionary_filters(),
451456
)[0]
452457
dictionaries = self._config_dictionaries[:]
453458
for filename in new_filenames:
454459
filename = normalize_path(filename)
460+
self._file_dialogs_directory = os.path.dirname(filename)
455461
for d in dictionaries:
456462
if d.path == filename:
457463
break
@@ -460,7 +466,7 @@ def _add_existing_dictionaries(self):
460466
self._update_dictionaries(dictionaries, keep_selection=False)
461467

462468
def _create_new_dictionary(self):
463-
new_filename = _get_dictionary_save_name(self, _('New dictionary'))
469+
new_filename = self._get_dictionary_save_name(_('New dictionary'))
464470
if new_filename is None:
465471
return
466472
with _new_dictionary(new_filename) as d:

0 commit comments

Comments
 (0)