17 from os
import linesep
20 from wx
import grid, Colour
22 from .clipboard
import ClipboardHandler
23 from ..context
import IS_WINDOWS
24 from ..widgets
import PopupCreator, PopupMenuItems
31 _col_add_threshold = 6
36 'Insert Cells\tCtrl-Shift-I',
'Delete Cells\tCtrl-Shift-D',
37 'Insert Rows\tCtrl-I',
'Delete Rows\tCtrl-D',
'---',
38 'Select All\tCtrl-A',
'---',
'Cut\tCtrl-X',
'Copy\tCtrl-C',
39 'Paste\tCtrl-V',
'Insert\tCtrl-Shift-V',
'---',
'Delete\tDel']
43 _regexps = (re.compile(
r'(\\+)r\\n'),
44 re.compile(
r'(\\+)n'),
45 re.compile(
r'(\\+)r'),
46 re.compile(
r'(\\+) '))
48 def __init__(self, parent, num_rows, num_cols, popup_creator=None):
49 grid.Grid.__init__(self, parent)
51 self.
settingssettings = parent.plugin.global_settings[
'Grid']
53 except AttributeError:
54 from ..preferences
import RideSettings
58 _settings = RideSettings()
60 self.
settingssettings = _settings[
'Grid']
72 self.CreateGrid(
int(num_rows),
int(num_cols))
73 self.SetDefaultCellBackgroundColour(Colour(self.
color_backgroundcolor_background))
82 self.Bind(grid.EVT_GRID_SELECT_CELL, self.
OnSelectCellOnSelectCell)
83 self.Bind(grid.EVT_GRID_RANGE_SELECT, self.
OnRangeSelectOnRangeSelect)
84 self.Bind(grid.EVT_GRID_CELL_RIGHT_CLICK, self.
OnCellRightClickOnCellRightClick)
92 def write_cell(self, row, col, value, update_history=True):
99 self.SetCellValue(row, col, value)
102 for regexp
in self.
_regexps_regexps:
103 if regexp.pattern.endswith(
' '):
110 return self.
_replacer_replacer(
' ', match)
113 return self.
_replacer_replacer(linesep, match)
116 slashes = len(match.group(1))
118 return '\\' * (slashes - 1) + char
124 while self.NumberRows <= max(1, row+1, 10-row):
126 while self.NumberCols <= max(1, col+1, 10-col):
130 return self.FindFocus() == self
137 range(self.NumberCols))
140 cell_under_cursor = property
143 x, y = self.ScreenToClient(wx.GetMousePosition())
144 x -= self.RowLabelSize
145 return self.XYToCell(*self.CalcUnscrolledPosition(x, y))
148 self.SelectBlock(row, column, row, column)
149 self.SetGridCursor(row, column)
150 self.MakeCellVisible(row, column)
162 for row, col
in self.
selectionselection.cells():
163 self.
write_cellwrite_cell(row, col,
'', update_history=
False)
174 _iscelleditcontrolshown = self.IsCellEditControlShown()
175 if _iscelleditcontrolshown:
183 start, end = editor.Selection
186 editor.Remove(start, end)
189 return self.SelectedRows
192 return self.GetCellEditor(*self.
selectionselection.cell).GetControl()
200 if len(cells) != 1
or len(cells[0]) != 1:
205 return self.GetCellValue(*self.
selectionselection.cell)
208 return [[self.GetCellValue(row, col)
for col
in col_range]
209 for row
in row_range]
212 while rowdata
and not rowdata[-1]:
217 prev_data = self.
_history_history.back()
220 self.
_write_data_write_data(prev_data, update_history=
False)
224 for row_index, row_data
in enumerate(data):
225 for col_index, cell_value
in enumerate(row_data):
226 self.
write_cellwrite_cell(row_index, col_index, cell_value, update_history)
231 self.SelectBlock(self.
selectionselection.topleft.row, self.
selectionselection.topleft.col,
235 self.
selectionselection.set_from_single_selection(event)
239 if not event.Selecting():
242 if event.ControlDown():
243 self.SetGridCursor(event.TopRow, event.LeftCol)
244 self.SelectBlock(event.TopRow, event.LeftCol,
245 event.BottomRow, event.RightCol, addToSelected=
False)
247 self.
selectionselection.set_from_range_selection(self, event)
251 if not self.IsVisible(bottom_row , 0)
and bottom_row < self.NumberRows
and \
253 self.MakeCellVisible(bottom_row, 0)
256 if hasattr(event,
'Row')
and hasattr(event,
'Col'):
257 if not (event.Row, event.Col)
in self.
selectionselection.cells():
258 self.
selectselect(event.Row, event.Col)
259 self.
selectionselection.set_from_single_selection(event)
275 for index
in self.
selectionselection.rows():
276 data = action(self.
_row_data_row_data(index))
283 left = right = cols[0]
284 data[left:right] = [
''] * len(cols)
290 left, right = cols[0], cols[-1]
292 data[left:right] = []
293 return data + [
''] * len(cols)
296 return [self.GetCellValue(row, col)
for col
in range(self.NumberCols)]
299 for col, value
in enumerate(data):
300 self.
write_cellwrite_cell(row, col, value, update_history=
False)
304 self.SetGridCursor(*self.
selectionselection.cell)
305 self.GetParent().Sizer.Layout()
313 self.
_set_set((0, 0))
316 def _set(self, topleft, bottomright=None):
322 return _Cell(topleft[0], topleft[1])
323 return _Cell(min(self.
_grid_grid.NumberRows - 1, bottomright[0]),
324 min(self.
_grid_grid.NumberCols - 1, bottomright[1]))
327 self.
_set_set((event.Row, event.Col))
333 selection = (self.
_grid_grid.GetGridCursorRow(), self.
_grid_grid.GetGridCursorCol())
334 self.
_set_set(selection)
337 whole_row_selection = sorted(grid.SelectedRows)
338 if whole_row_selection:
339 return (whole_row_selection[0], 0), \
340 (whole_row_selection[-1], grid.NumberCols - 1)
341 return (event.TopLeftCoords.Row, event.TopLeftCoords.Col), \
342 (event.BottomRightCoords.Row, event.BottomRightCoords.Col)
354 return [(row, col)
for col
in self.
colscols()
355 for row
in self.
rowsrows()]
365 for item
in self.
rowrow, self.
colcol:
376 if not self.
_back_back
or state != self.
_back_back[-1]:
377 self.
_back_back.append(state)
381 if not self.
_back_back:
390 self.
_back_back.append(state)
def _ensure_selected_row_is_visible(self, bottom_row)
def _clear_selected_cells(self)
def _bind_to_events(self)
def _get_all_content(self)
def _write_row(self, row, data)
def _update_history(self)
def __init__(self, parent, num_rows, num_cols, popup_creator=None)
def _refresh_layout(self)
color_secondary_foreground
def write_cell(self, row, col, value, update_history=True)
def _delete_from_cell_editor(self)
def register_context_menu_hook(self, callable)
def _is_whole_row_selection(self)
def _whitespace_replacer(self, match)
def select(self, row, column)
def get_single_selection_content(self)
def OnDeleteCells(self, event)
def _write_data(self, data, update_history=True)
def _current_cell_value(self)
def OnInsertCells(self, event)
def _expand_if_necessary(self, row, col)
def OnRangeSelect(self, event)
def _delete_cells(self, data)
def _insert_or_delete_cells(self, action, event)
def _newline_replacer(self, match)
def _replacer(self, char, match)
def OnSelectCell(self, event)
def OnCellRightClick(self, event)
def _insert_cells(self, data)
def get_selected_content(self)
def get_cell_edit_control(self)
color_secondary_background
def _get_block_content(self, row_range, col_range)
def unregister_context_menu_hook(self, callable)
def _unescape_newlines_and_whitespaces(self, item)
def _strip_trailing_empty_cells(self, rowdata)
def __init__(self, row, col)
def cols(self)
Returns a list containing indices of columns currently selected.
def rows(self)
Returns a list containing indices of rows currently selected.
def _get_bounding_coordinates(self, grid, event)
def set_from_range_selection(self, grid, event)
def _count_bottomright(self, topleft, bottomright)
def cells(self)
Return selected cells as a list of tuples (row, column).
def set_from_single_selection(self, event)
def _set(self, topleft, bottomright=None)