Robot Framework Integrated Development Environment (RIDE)
clipboard.py
Go to the documentation of this file.
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 
16 import os
17 
18 import wx
19 
20 from ..context import IS_WINDOWS, IS_MAC
21 
22 
24 
25  def __init__(self, grid):
26  self._grid_grid = grid
27  self._clipboard_clipboard = _GridClipboard()
28 
29  def clipboard_content(self):
30  return self._clipboard_clipboard.get_contents()
31 
32 
36  def copy(self):
37  # print("DEBUG: Clipboard copy() got called \n")
38  if not self._edit_control_shown_edit_control_shown():
39  self._add_selected_data_to_clipboard_add_selected_data_to_clipboard()
40 
41 
45  def cut(self):
46  self._add_selected_data_to_clipboard_add_selected_data_to_clipboard()
47 
49  self._clipboard_clipboard.set_contents(self._grid_grid.get_selected_content())
50 
51 
54  def paste(self):
55  if not self._edit_control_shown_edit_control_shown():
56  self._paste_to_grid_paste_to_grid()
57 
59  clipboard = self._clipboard_clipboard.get_contents()
60  if isinstance(clipboard, list):
61  cells_as_text = ' '.join([' '.join(row) for row in clipboard])
62  self._get_edit_control_get_edit_control().WriteText(cells_as_text)
63 
64  def _paste_to_grid(self):
65  clipboard = self._clipboard_clipboard.get_contents()
66  if not clipboard:
67  return
68  cell = self._get_starting_cell_get_starting_cell()
69  if not isinstance(clipboard, list):
70  self._write_cell_write_cell(cell.row, cell.col, clipboard)
71  else:
72  row = cell.row
73  for datarow in clipboard:
74  col = cell.col
75  for value in datarow:
76  self._write_cell_write_cell(row, col, value)
77  col += 1
78  row += 1
79 
80  def _get_starting_cell(self):
81  return self._grid_grid.selection.topleft
82 
83  def _write_cell(self, row, col, value):
84  self._grid_grid.write_cell(row, col, value, update_history=False)
85 
86  def _get_edit_control(self):
87  return self._grid_grid.get_cell_edit_control()
88 
90  return self._grid_grid.IsCellEditControlShown()
91 
92 
94 
95  def copy(self):
96  if self._edit_control_shown_edit_control_shown():
97  self._get_edit_control_get_edit_control().Copy()
98  else:
99  _ClipboardHandler.copy(self)
100 
101  def cut(self):
102  if self._edit_control_shown_edit_control_shown():
103  self._get_edit_control_get_edit_control().Cut()
104  else:
105  _ClipboardHandler.cut(self)
106 
108  self._get_edit_control_get_edit_control().Paste()
109 
110  def paste(self):
111  if self._edit_control_shown_edit_control_shown():
112  self._paste_to_cell_editor_paste_to_cell_editor_paste_to_cell_editor()
113  else:
114  _ClipboardHandler.paste(self)
115 
116 
117 # for now mac clipboard handler is the same with windows
118 ClipboardHandler = _WindowsClipboardHandler if IS_WINDOWS or IS_MAC else _ClipboardHandler
119 
120 
121 
125 
126 
131  def set_contents(self, data):
132  data = self._format_data_format_data(data)
133  if not (data and wx.TheClipboard.Open()):
134  return
135  try:
136  tdo = wx.TextDataObject()
137  tdo.SetText(data)
138  wx.TheClipboard.SetData(tdo)
139  finally:
140  wx.TheClipboard.Close()
141 
142  def _format_data(self, data):
143  if isinstance(data, list):
144  return os.linesep.join('\t'.join(row) for row in data)
145  if isinstance(data, str):
146  return data
147  return None
148 
149 
153  def get_contents(self):
154  return self._split_string_from_tabs_and_newlines_split_string_from_tabs_and_newlines(self._get_contents_get_contents())
155 
156  def _get_contents(self):
157  if not wx.TheClipboard.Open():
158  return ''
159  try:
160  tdo = wx.TextDataObject()
161  wx.TheClipboard.GetData(tdo)
162  return tdo.GetText() or ''
163  finally:
164  wx.TheClipboard.Close()
165 
167  return [line.split('\t') for line in string.splitlines()]
def _write_cell(self, row, col, value)
Definition: clipboard.py:83
def cut(self)
Cuts the contents of the selected cell(s).
Definition: clipboard.py:45
def copy(self)
Copy the contents of the selected cell(s).
Definition: clipboard.py:36
def paste(self)
Paste the contents of the clipboard.
Definition: clipboard.py:54
Implements a "smart" clipboard.
Definition: clipboard.py:124
def _split_string_from_tabs_and_newlines(self, string)
Definition: clipboard.py:166
def set_contents(self, data)
Insert data to the system clipboard.
Definition: clipboard.py:131
def get_contents(self)
Gets contents of the clipboard.
Definition: clipboard.py:153
def paste(self)
Paste the contents of the clipboard.
Definition: clipboard.py:110
def copy(self)
Copy the contents of the selected cell(s).
Definition: clipboard.py:95
def cut(self)
Cuts the contents of the selected cell(s).
Definition: clipboard.py:101