Skip to content

Commit

Permalink
Merge pull request #443 from benoit-pierre/dictionary_editor
Browse files Browse the repository at this point in the history
Dictionary management (editor, translation dialogue) improvements
  • Loading branch information
morinted committed Mar 22, 2016
2 parents bf50daa + fbb3cc5 commit fbbc1f7
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 60 deletions.
49 changes: 22 additions & 27 deletions plover/dictionary_editor_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,18 +54,22 @@ def __init__(self, engine, config):

self.pending_changes = False

for dict in reversed(self.engine.get_dictionary().dicts):
for dk, translation in dict.iteritems():
for dictionary in reversed(self.engine.get_dictionary().dicts):
for dk, translation in dictionary.iteritems():
joined = '/'.join(dk)
item = DictionaryItem(joined,
translation,
dict.get_path(),
dictionary,
item_id)
self.all_keys.append(item)
item_id += 1
self.filtered_keys = self.all_keys[:]
self.sorted_keys = self.filtered_keys[:]

def is_row_read_only(self, row):
item = self.sorted_keys[row]
return item.dictionary.save is None

def GetNumberOfRows(self):
return len(self.sorted_keys)

Expand All @@ -78,7 +82,7 @@ def GetValue(self, row, col):
elif col is COL_TRANSLATION:
result = shorten_unicode(item.translation)
elif col is COL_DICTIONARY:
result = item.dictionary
result = item.dictionary.get_path()
return result

def SetValue(self, row, col, value):
Expand Down Expand Up @@ -136,31 +140,26 @@ def DeleteSelected(self, row):
def SaveChanges(self):
self.pending_changes = False

# Set of dictionaries (paths) that needs saving.
needs_saving = set()

# Creates
for added_item in self.added_items:
dict = (self.engine
.get_dictionary()
.get_by_path(added_item.dictionary))
dict.__setitem__(self._splitStrokes(added_item.stroke),
added_item.translation)
for item in self.added_items:
item.dictionary[normalize_steno(item.stroke)] = item.translation
needs_saving.add(item.dictionary.get_path())

# Updates
for modified_item_id in self.modified_items:
modified_item = self.all_keys[modified_item_id]
dict = (self.engine
.get_dictionary()
.get_by_path(modified_item.dictionary))
dict.__setitem__(self._splitStrokes(modified_item.stroke),
modified_item.translation)
for item_id in self.modified_items:
item = self.all_keys[item_id]
item.dictionary[normalize_steno(item.stroke)] = item.translation
needs_saving.add(item.dictionary.get_path())

# Deletes
for deleted_item in self.deleted_items:
dict = (self.engine
.get_dictionary()
.get_by_path(deleted_item.dictionary))
dict.__delitem__(self._splitStrokes(deleted_item.stroke))
for item in self.deleted_items:
del item.dictionary[normalize_steno(item.stroke)]
needs_saving.add(item.dictionary.get_path())

self.engine.get_dictionary().save_all()
self.engine.get_dictionary().save(needs_saving)

def Sort(self, column):
if column is not COL_STROKE and column is not COL_TRANSLATION:
Expand Down Expand Up @@ -226,7 +225,3 @@ def _applySort(self):
reverse=reverse_sort)
else:
self.sorted_keys = self.filtered_keys[:]

def _splitStrokes(self, strokes_string):
result = normalize_steno(strokes_string.upper())
return result
4 changes: 2 additions & 2 deletions plover/gui/add_translation.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ def on_add_translation(self, event=None):
translation = self.translation_text.GetValue().strip()
if strokes and translation:
d.set(strokes, translation)
d.save()
d.save(path_list=(d.dicts[0].get_path(),))
self.Close()

def on_close(self, event=None):
Expand Down Expand Up @@ -233,7 +233,7 @@ def _apply_opacity(self, opacity):
self.SetTransparent(alpha_byte)

def _normalized_strokes(self):
strokes = self.strokes_text.GetValue().upper().replace('/', ' ').split()
strokes = self.strokes_text.GetValue().replace('/', ' ').split()
strokes = normalize_steno('/'.join(strokes))
return strokes

Expand Down
66 changes: 52 additions & 14 deletions plover/gui/dictionary_editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from plover.dictionary_editor_store import COL_TRANSLATION
from plover.dictionary_editor_store import COL_DICTIONARY
from plover.dictionary_editor_store import COL_SPACER
from wx.grid import EVT_GRID_LABEL_LEFT_CLICK
from wx.grid import EVT_GRID_LABEL_LEFT_CLICK, EVT_GRID_SELECT_CELL, EVT_GRID_RANGE_SELECT
from wx.grid import PyGridTableBase

TITLE = 'Plover: Dictionary Editor'
Expand Down Expand Up @@ -91,12 +91,6 @@ def __init__(self, parent, engine, config, on_exit):
self.grid.SetColSize(COL_TRANSLATION, 250)
self.grid.SetColSize(COL_DICTIONARY, 150)

read_only_right_aligned = wx.grid.GridCellAttr()
read_only_right_aligned.SetReadOnly(True)
read_only_right_aligned.SetAlignment(wx.ALIGN_RIGHT, wx.ALIGN_CENTRE)
self.grid.SetColAttr(COL_DICTIONARY, read_only_right_aligned)
self.grid.SetColAttr(COL_SPACER, read_only_right_aligned)

global_sizer.Add(self.grid, 1, wx.EXPAND)

buttons_sizer = wx.BoxSizer(wx.HORIZONTAL)
Expand Down Expand Up @@ -204,11 +198,18 @@ def __init__(self, *args, **kwargs):

self._changedRow = None

# We need to keep track of the selection ourselves...
self.Bind(EVT_GRID_SELECT_CELL, self._on_select_cell)
self.Bind(EVT_GRID_RANGE_SELECT, self._on_select_range)
self.selection = set()

def CreateGrid(self, store, rows, cols):
""" Create the grid """

wx.grid.Grid.CreateGrid(self, rows, cols)
wx.grid.Grid.DisableDragRowSize(self)
# TODO: enable this when wx is fixed...
# self.SetSelectionMode(wx.grid.Grid.wxGridSelectRows)

self.store = store

Expand All @@ -225,14 +226,28 @@ def RefreshView(self):
self._table.ResetView(self)

def InsertNew(self):
selected_row = self.GetGridCursorRow()
self.store.InsertNew(selected_row)
self._table.ResetView(self)
for row in self.selection:
if not self.store.is_row_read_only(row):
self.store.InsertNew(row)
self._table.ResetView(self)
self.SetFocus()
self.ClearSelection()
self.SelectRow(row)
self.SetGridCursor(row, 0)
self.MakeCellVisible(row, 0)
break
else:
self.ClearSelection()

def DeleteSelected(self):
selected_row = self.GetGridCursorRow()
self.store.DeleteSelected(selected_row)
self._table.ResetView(self)
delete_selection = [row for row in self.selection
if not self.store.is_row_read_only(row)]
if delete_selection:
# Delete in reverse order, so row numbers are stable.
for row in sorted(delete_selection, reverse=True):
self.store.DeleteSelected(row)
self._table.ResetView(self)
self.ClearSelection()

def _onLabelClick(self, evt):
""" Handle Grid label click"""
Expand Down Expand Up @@ -261,6 +276,17 @@ def _updateGridLabel(self, column, mode):
(directionLabel if column == i else ""))
self._table.SetColLabelValue(i, label)

def _on_select_cell(self, evt):
row = evt.GetRow()
self.selection = set((row,))

def _on_select_range(self, evt):
select_range = set(range(evt.GetTopRow(), evt.GetBottomRow() + 1))
if evt.Selecting():
self.selection |= select_range
else:
self.selection -= select_range


class DictionaryEditorGridTable(PyGridTableBase):
"""
Expand Down Expand Up @@ -302,6 +328,18 @@ def GetValue(self, row, col):
def SetValue(self, row, col, value):
self.store.SetValue(row, col, value)

def GetAttr(self, row, col, params):
if col in (COL_DICTIONARY, COL_SPACER):
attr = wx.grid.GridCellAttr()
attr.SetReadOnly(True)
attr.SetAlignment(wx.ALIGN_RIGHT, wx.ALIGN_CENTRE)
return attr
if self.store.is_row_read_only(row):
attr = wx.grid.GridCellAttr()
attr.SetReadOnly(True)
return attr
return None

def ResetView(self, grid):

grid.BeginBatch()
Expand Down Expand Up @@ -358,4 +396,4 @@ def clear_instance():
clear_instance)
Show.dialog_instance.Show()
Show.dialog_instance.Raise()
util.SetTopApp()
util.SetTopApp(Show.dialog_instance)
38 changes: 21 additions & 17 deletions plover/steno_dictionary.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,26 +51,24 @@ def __getitem__(self, key):

def __setitem__(self, key, value):
self._longest_key = max(self._longest_key, len(key))
self._dict.__setitem__(key, value)
self._dict[key] = value
self.reverse[value].append(key)
# Case-insensitive reverse dict
self.casereverse[value.lower()].add(value)

def __delitem__(self, key):
value = self._dict[key]
value = self._dict.pop(key)
self.reverse[value].remove(key)
self._dict.__delitem__(key)
if len(key) == self.longest_key:
if self._dict:
self._longest_key = max(len(x) for x in self._dict.iterkeys())
else:
self._longest_key = 0

def __contains__(self, key):
contained = self._dict.__contains__(key)
if not contained:
value = self._dict.get(key)
if value is None:
return False
value = self._dict[key]
for f in self.filters:
if f(key, value):
return False
Expand Down Expand Up @@ -138,7 +136,7 @@ def set_dicts(self, dicts):

def lookup(self, key):
for d in self.dicts:
value = d.get(key, None)
value = d.get(key)
if value:
for f in self.filters:
if f(key, value):
Expand All @@ -147,33 +145,39 @@ def lookup(self, key):

def raw_lookup(self, key):
for d in self.dicts:
value = d.get(key, None)
value = d.get(key)
if value:
return value

def reverse_lookup(self, value):
for d in self.dicts:
key = d.reverse.get(value, None)
key = d.reverse.get(value)
if key:
return key

def casereverse_lookup(self, value):
for d in self.dicts:
key = d.casereverse.get(value, None)
key = d.casereverse.get(value)
if key:
return key

def set(self, key, value):
if self.dicts:
self.dicts[0][key] = value

def save(self):
if self.dicts:
self.dicts[0].save()

def save_all(self):
for dict in self.dicts:
dict.save()
def save(self, path_list=None):
'''Save the dictionaries in <path_list>.
If <path_list> is None, all writable dictionaries are saved'''
if path_list is None:
dict_list = [dictionary
for dictionary in self.dicts
if dictionary.save is not None]
else:
dict_list = [self.get_by_path(path)
for path in path_list]
for dictionary in dict_list:
dictionary.save()

def get_by_path(self, path):
for d in self.dicts:
Expand Down

0 comments on commit fbbc1f7

Please sign in to comment.