Coverage for src/robotide/editor/clipboard.py: 80%

93 statements  

« prev     ^ index     » next       coverage.py v7.8.0, created at 2025-05-06 10:40 +0100

1# Copyright 2008-2015 Nokia Networks 

2# Copyright 2016- Robot Framework Foundation 

3# 

4# Licensed under the Apache License, Version 2.0 (the "License"); 

5# you may not use this file except in compliance with the License. 

6# You may obtain a copy of the License at 

7# 

8# http://www.apache.org/licenses/LICENSE-2.0 

9# 

10# Unless required by applicable law or agreed to in writing, software 

11# distributed under the License is distributed on an "AS IS" BASIS, 

12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 

13# See the License for the specific language governing permissions and 

14# limitations under the License. 

15 

16import os 1ef

17 

18import wx 1ef

19 

20from ..context import IS_WINDOWS, IS_MAC 1ef

21 

22 

23class _ClipboardHandler(object): 1ef

24 

25 def __init__(self, grid): 1ef

26 self._grid = grid 

27 self._clipboard = _GridClipboard() 

28 

29 def clipboard_content(self): 1ef

30 return self._clipboard.get_contents() 1ghiabcd

31 

32 def clear(self): 1ef

33 """Clears the content of clipboard. 

34 """ 

35 self._clipboard.set_contents('') 1ghijklmabcdno

36 

37 def copy(self): 1ef

38 """Copy the contents of the selected cell(s). This does a normal copy 

39 action if the user is editing a cell, otherwise it places the selected 

40 range of cells on the data. 

41 """ 

42 # print(f"DEBUG: Clipboard copy() got called \ncells={self._grid.cells}") 

43 if not self._edit_control_shown(): 43 ↛ exitline 43 didn't return from function 'copy' because the condition on line 43 was always true1ghiabcd

44 self._add_selected_data_to_clipboard() 1ghiabcd

45 

46 def cut(self): 1ef

47 """Cuts the contents of the selected cell(s). This does a normal cut 

48 action if the user is editing a cell, otherwise it places the selected 

49 range of cells on the clipboard. 

50 """ 

51 self._add_selected_data_to_clipboard() 1jklmno

52 

53 def _add_selected_data_to_clipboard(self): 1ef

54 content = self._grid.get_selected_content() 1ghijklmabcdno

55 # print(f"DEBUG: Clipboard _add_selected_data_to_clipboard content={content}\n") 

56 self._clipboard.set_contents(content) 1ghijklmabcdno

57 

58 def paste(self): 1ef

59 """Paste the contents of the clipboard. If a cell is being edited just 

60 do a normal paste. If a cell is not being edited, paste whole rows. 

61 """ 

62 if not self._edit_control_shown(): 62 ↛ exitline 62 didn't return from function 'paste' because the condition on line 62 was always true1abcd

63 self._paste_to_grid() 1abcd

64 

65 def _paste_to_cell_editor(self): 1ef

66 clipboard = self._clipboard.get_contents() 

67 if isinstance(clipboard, list): 

68 cells_as_text = ' '.join([' '.join(row) for row in clipboard]) 

69 self._get_edit_control().WriteText(cells_as_text) 

70 

71 def _paste_to_grid(self): 1ef

72 clipboard = self._clipboard.get_contents() 1abcd

73 if not clipboard: 73 ↛ 74line 73 didn't jump to line 74 because the condition on line 73 was never true1abcd

74 return 

75 cell = self._get_starting_cell() 1abcd

76 if not isinstance(clipboard, list): 76 ↛ 77line 76 didn't jump to line 77 because the condition on line 76 was never true1abcd

77 self._write_cell(cell.row, cell.col, clipboard) 

78 else: 

79 row = cell.row 1abcd

80 for datarow in clipboard: 1abcd

81 col = cell.col 1abcd

82 for value in datarow: 1abcd

83 self._write_cell(row, col, value) 1abcd

84 col += 1 1abcd

85 row += 1 1abcd

86 

87 def _get_starting_cell(self): 1ef

88 return self._grid.selection.topleft 1abcd

89 

90 def _write_cell(self, row, col, value): 1ef

91 self._grid.write_cell(row, col, value, update_history=False) 1abcd

92 

93 def _get_edit_control(self): 1ef

94 return self._grid.get_cell_edit_control() 

95 

96 def _edit_control_shown(self): 1ef

97 return self._grid.IsCellEditControlShown() 1ghijklmabcdno

98 

99 

100class _WindowsClipboardHandler(_ClipboardHandler): 1ef

101 

102 def copy(self): 1ef

103 if self._edit_control_shown(): 103 ↛ 104line 103 didn't jump to line 104 because the condition on line 103 was never true1ghiabcd

104 self._get_edit_control().Copy() 

105 else: 

106 _ClipboardHandler.copy(self) 1ghiabcd

107 

108 def cut(self): 1ef

109 if self._edit_control_shown(): 109 ↛ 110line 109 didn't jump to line 110 because the condition on line 109 was never true1jklmno

110 self._get_edit_control().Cut() 

111 else: 

112 _ClipboardHandler.cut(self) 1jklmno

113 

114 def _paste_to_cell_editor(self): 1ef

115 self._get_edit_control().Paste() 

116 

117 def paste(self): 1ef

118 if self._edit_control_shown(): 118 ↛ 119line 118 didn't jump to line 119 because the condition on line 118 was never true1abcd

119 self._paste_to_cell_editor() 

120 else: 

121 _ClipboardHandler.paste(self) 1abcd

122 

123 

124# for now mac clipboard handler is the same with windows 

125ClipboardHandler = _WindowsClipboardHandler if IS_WINDOWS or IS_MAC else _ClipboardHandler 1ef

126 

127 

128class _GridClipboard(object): 1ef

129 """Implements a "smart" clipboard.""" 

130 

131 def set_contents(self, data): 1ef

132 """Insert `data` to the system clipboard 

133 

134 `data` may be either a string or list of lists representing rows of 

135 grid data. Other data is ignored 

136 """ 

137 data = self._format_data(data) 1qrpghijklmabcdno

138 if not (data and wx.TheClipboard.Open()): 1qrpghijklmabcdno

139 return 1ghijklmabcdno

140 try: 1qrpghijklmabcdno

141 tdo = wx.TextDataObject() 1qrpghijklmabcdno

142 tdo.SetText(data) 1qrpghijklmabcdno

143 wx.TheClipboard.SetData(tdo) 1qrpghijklmabcdno

144 finally: 

145 wx.TheClipboard.Close() 1qrpghijklmabcdno

146 

147 def _format_data(self, data): 1ef

148 if isinstance(data, list): 1qrpghijklmabcdno

149 return os.linesep.join('\t'.join(row) for row in data) 1qrghijklmabcdno

150 if isinstance(data, str): 150 ↛ 152line 150 didn't jump to line 152 because the condition on line 150 was always true1pghijklmabcdno

151 return data 1pghijklmabcdno

152 return None 

153 

154 def get_contents(self): 1ef

155 """Gets contents of the clipboard. 

156 

157 Returns either a string or a list of rows to be pasted into clipboard. 

158 """ 

159 return self._split_string_from_tabs_and_newlines(self._get_contents()) 1ghijklabcd

160 

161 def _get_contents(self): 1ef

162 if not wx.TheClipboard.Open(): 162 ↛ 163line 162 didn't jump to line 163 because the condition on line 162 was never true1qrpghijklabcd

163 return '' 

164 try: 1qrpghijklabcd

165 tdo = wx.TextDataObject() 1qrpghijklabcd

166 wx.TheClipboard.GetData(tdo) 1qrpghijklabcd

167 return tdo.GetText() or '' 1qrpghijklabcd

168 finally: 

169 wx.TheClipboard.Close() 1qrpghijklabcd

170 

171 def _split_string_from_tabs_and_newlines(self, string): 1ef

172 return [line.split('\t') for line in string.splitlines()] 1ghijklabcd