1313# See the License for the specific language governing permissions and
1414# limitations under the License.
1515
16- import re
17- from os import linesep
18-
1916import wx
2017from wx import grid , Colour
2118
2219from .clipboard import ClipboardHandler
2320from ..context import IS_WINDOWS
21+ from ..utils import unescape_newlines_and_whitespaces
2422from ..widgets import PopupCreator , PopupMenuItems
2523
26- # FILTER_NEWLINES = True
27-
2824
2925class GridEditor (grid .Grid ):
3026 _col_add_threshold = 6
@@ -33,10 +29,6 @@ class GridEditor(grid.Grid):
3329 'Insert Rows\t Ctrl-I' , 'Delete Rows\t Ctrl-D' , '---' ,
3430 'Select All\t Ctrl-A' , '---' , 'Cut\t Ctrl-X' , 'Copy\t Ctrl-C' ,
3531 'Paste\t Ctrl-V' , 'Insert\t Ctrl-Shift-V' , '---' , 'Delete\t Del' ]
36- _regexps = (re .compile (r'(\\+)r\\n' ),
37- re .compile (r'(\\+)n' ),
38- re .compile (r'(\\+)r' ),
39- re .compile (r'(\\+) ' ))
4032
4133 def __init__ (self , parent , num_rows , num_cols , popup_creator = None ):
4234 grid .Grid .__init__ (self , parent )
@@ -86,35 +78,15 @@ def write_cell(self, row, col, value, update_history=True):
8678 self ._expand_if_necessary (row , col )
8779 if self .filter_newlines :
8880 # unescape \n to support multi lines display in grid cells
89- value = self . _unescape_newlines_and_whitespaces (value )
81+ value = unescape_newlines_and_whitespaces (value )
9082 self .SetCellValue (row , col , value )
9183
92- def _unescape_newlines_and_whitespaces (self , item ):
93- for regexp in self ._regexps :
94- if regexp .pattern .endswith (' ' ):
95- item = regexp .sub (self ._whitespace_replacer , item )
96- else :
97- item = regexp .sub (self ._newline_replacer , item )
98- return item
99-
100- def _whitespace_replacer (self , match ):
101- return self ._replacer (' ' , match )
102-
103- def _newline_replacer (self , match ):
104- return self ._replacer (linesep , match )
105-
106- def _replacer (self , char , match ):
107- slashes = len (match .group (1 ))
108- if slashes % 2 == 1 :
109- return '\\ ' * (slashes - 1 ) + char
110- return match .group ()
111-
11284 def _expand_if_necessary (self , row , col ):
11385 # Changed col and row fill because of blank spacing not changing color
11486 # print(f"DEBUG: GridEditor ENTER_expand_if_necessary row={row}, col={col}")
115- while self .NumberRows <= max (1 , row + 1 , 10 - row ): # DEBUG 25 makes slower rendering
87+ while self .NumberRows <= max (1 , row + 1 , 10 - row ): # DEBUG 25 makes slower rendering
11688 self .AppendRows (1 )
117- while self .NumberCols <= max (1 , col + 1 , 10 - col ): # DEBUG 40 makes slower rendering
89+ while self .NumberCols <= max (1 , col + 1 , 10 - col ): # DEBUG 40 makes slower rendering
11890 self .AppendCols (max (1 , self ._col_add_threshold )) # DEBUG: was infinite when value was 0
11991
12092 def has_focus (self ):
@@ -192,9 +164,10 @@ def _current_cell_value(self):
192164
193165 def _get_block_content (self , row_range , col_range ):
194166 return [[self .GetCellValue (row , col ) for col in col_range ]
195- for row in row_range ]
167+ for row in row_range ]
196168
197- def _strip_trailing_empty_cells (self , rowdata ):
169+ @staticmethod
170+ def _strip_trailing_empty_cells (rowdata ):
198171 while rowdata and not rowdata [- 1 ]:
199172 rowdata .pop ()
200173 return rowdata
@@ -234,23 +207,23 @@ def OnRangeSelect(self, event):
234207 self ._ensure_selected_row_is_visible (event .BottomRow )
235208
236209 def _ensure_selected_row_is_visible (self , bottom_row ):
237- if not self .IsVisible (bottom_row , 0 ) and bottom_row < self .NumberRows and \
210+ if not self .IsVisible (bottom_row , 0 ) and bottom_row < self .NumberRows and \
238211 self ._is_whole_row_selection ():
239212 self .MakeCellVisible (bottom_row , 0 )
240213
241214 def OnCellRightClick (self , event ):
242215 if hasattr (event , 'Row' ) and hasattr (event , 'Col' ):
243- if not (event .Row , event .Col ) in self .selection .cells ():
216+ if (event .Row , event .Col ) not in self .selection .cells ():
244217 self .select (event .Row , event .Col )
245218 self .selection .set_from_single_selection (event )
246219 self ._popup_creator .show (self , PopupMenuItems (self , self ._popup_items ),
247220 self .get_selected_content ())
248221
249- # TODO This code is overriden at fieldeditors
222+ # DEBUG: This code is overriden at fieldeditors
250223 def OnInsertCells (self , event ):
251224 self ._insert_or_delete_cells (self ._insert_cells , event )
252225
253- # TODO This code is overriden at fieldeditors
226+ # DEBUG: This code is overriden at fieldeditors
254227 def OnDeleteCells (self , event ):
255228 # print("DEBUG delete cells %s" % event)
256229 self ._insert_or_delete_cells (self ._delete_cells , event )
@@ -291,13 +264,13 @@ def _refresh_layout(self):
291264 self .GetParent ().Sizer .Layout ()
292265
293266
294- # TODO : refactor this internal state away if possible
267+ # DEBUG : refactor this internal state away if possible
295268class _GridSelection (object ):
296269 cell = property (lambda self : (self .topleft .row , self .topleft .col ))
297270
298- def __init__ (self , grid ):
271+ def __init__ (self , gridd ):
299272 self ._set ((0 , 0 ))
300- self ._grid = grid
273+ self ._grid = gridd
301274
302275 def _set (self , topleft , bottomright = None ):
303276 self .topleft = _Cell (topleft [0 ], topleft [1 ])
@@ -312,18 +285,19 @@ def _count_bottomright(self, topleft, bottomright):
312285 def set_from_single_selection (self , event ):
313286 self ._set ((event .Row , event .Col ))
314287
315- def set_from_range_selection (self , grid , event ):
316- self ._set (* self ._get_bounding_coordinates (grid , event ))
288+ def set_from_range_selection (self , gridd , event ):
289+ self ._set (* self ._get_bounding_coordinates (gridd , event ))
317290
318291 def clear (self ):
319292 selection = (self ._grid .GetGridCursorRow (), self ._grid .GetGridCursorCol ())
320293 self ._set (selection )
321294
322- def _get_bounding_coordinates (self , grid , event ):
323- whole_row_selection = sorted (grid .SelectedRows )
295+ @staticmethod
296+ def _get_bounding_coordinates (gridd , event ):
297+ whole_row_selection = sorted (gridd .SelectedRows )
324298 if whole_row_selection :
325299 return (whole_row_selection [0 ], 0 ), \
326- (whole_row_selection [- 1 ], grid .NumberCols - 1 )
300+ (whole_row_selection [- 1 ], gridd .NumberCols - 1 )
327301 return (event .TopLeftCoords .Row , event .TopLeftCoords .Col ), \
328302 (event .BottomRightCoords .Row , event .BottomRightCoords .Col )
329303
@@ -338,7 +312,7 @@ def cols(self):
338312 def cells (self ):
339313 """Return selected cells as a list of tuples (row, column)."""
340314 return [(row , col ) for col in self .cols ()
341- for row in self .rows ()]
315+ for row in self .rows ()]
342316
343317
344318class _Cell (object ):
0 commit comments