Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add "Save as..." feature for dictionary conversion #1149

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions news.d/feature/1149.ui.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add "Save a Copy As..." feature to export dictionary to a different format.
6 changes: 6 additions & 0 deletions plover/gui_qt/dictionaries_table.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from PyQt5.QtWidgets import QTableWidget

class DictionariesTable(QTableWidget):
def contextMenuEvent(self, event):
row = self.rowAt(event.y())
self.parent().on_table_context_menu(row, event.globalPos())
58 changes: 58 additions & 0 deletions plover/gui_qt/dictionaries_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
QMenu,
QTableWidgetItem,
QWidget,
QAction,
)

from plover.config import DictionaryConfig
Expand Down Expand Up @@ -377,6 +378,63 @@ def _create_new_dictionary(self):
self._update_dictionaries(dictionaries, keep_selection=False,
loaded_dictionaries=self._loaded_dictionaries)

def on_table_context_menu(self, row, global_pos):
if row == -1:
# when the user right-clicks in the empty area of the table
return

selected_rows = self._get_selection()
assert list(selected_rows) == sorted(selected_rows)
if row not in selected_rows:
# in some cases (Ctrl+right click, for example), the current row might not be selected
return

menu = QMenu(self)

# the path of the dictionary on the row the mouse clicked on
dictionary_path = self._config_dictionaries[row].path

if len(selected_rows) == 1:
saveAsAction = QAction(_('Save a Copy As...'), self)
else:
saveAsAction = QAction(_('Merge and Save a Copy As...'), self)

saveAsAction.triggered.connect(lambda: self.on_save_as(
default_name=dictionary_path,
dictionaries=[
self._loaded_dictionaries[self._config_dictionaries[row].path]
for row in selected_rows]
))

def cleanup():
menu.deleteLater()
saveAsAction.deleteLater()
menu.aboutToHide.connect(cleanup)

menu.addAction(saveAsAction)
menu.popup(global_pos)

def on_save_as(self, default_name, dictionaries):
new_filename = QFileDialog.getSaveFileName(
self, _('Save a Copy As...'), default_name,
_dictionary_filters(include_readonly=False),
)[0]
if not new_filename:
return
new_filename = normalize_path(new_filename)
try:
d = create_dictionary(new_filename, threaded_save=False)
for dictionary in reversed(dictionaries):
d.update(dictionary)
d.save()
except:
log.error('creating dictionary %s failed', new_filename, exc_info=True)
return
# Note: pass in `loaded_dictionaries` to force update (use case:
# the user decided to overwrite an already loaded dictionary).
self._update_dictionaries(self._config_dictionaries,
loaded_dictionaries=self._loaded_dictionaries)

def on_add_translation(self):
selection = self._get_selection()
if selection:
Expand Down
9 changes: 8 additions & 1 deletion plover/gui_qt/dictionaries_widget.ui
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
<number>0</number>
</property>
<item>
<widget class="QTableWidget" name="table">
<widget class="DictionariesTable" name="table">
<property name="frameShape">
<enum>QFrame::Box</enum>
</property>
Expand Down Expand Up @@ -173,6 +173,13 @@
</action>
</widget>
<layoutdefault spacing="5" margin="8"/>
<customwidgets>
<customwidget>
<class>DictionariesTable</class>
<extends>QTableWidget</extends>
<header>plover.gui_qt.dictionaries_table</header>
</customwidget>
</customwidgets>
<resources>
<include location="resources/resources.qrc"/>
</resources>
Expand Down