<commit_before>#  Copyright 2008-2009 Nokia Siemens Networks Oyj
#
#  Licensed under the Apache License, Version 2.0 (the "License");
#  you may not use this file except in compliance with the License.
#  You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
#  Unless required by applicable law or agreed to in writing, software
#  distributed under the License is distributed on an "AS IS" BASIS,
#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#  See the License for the specific language governing permissions and
#  limitations under the License.

import os
import wx
from wx import grid

from robotide.publish import RideGridCellChanged
from robotide.utils import PopupMenu

from clipboard import GRID_CLIPBOARD
from contentassist import ExpandingContentAssistTextCtrl
from popupwindow import RidePopupWindow


class ClipBoardHandler(object):
    def __init__(self, grid):
        self._grid = grid


class GridEditor(grid.Grid):

    def __init__(self, parent):
        grid.Grid.__init__(self, parent)
        self._bind_to_events()
        self._edit_history = []

    def _bind_to_events(self):
        self.Bind(grid.EVT_GRID_SELECT_CELL, self.OnSelectCell)
        self.Bind(grid.EVT_GRID_RANGE_SELECT, self.OnRangeSelect)
        self.Bind(grid.EVT_GRID_CELL_RIGHT_CLICK, self.OnCellRightClick)

    def set_dirty(self):
        # TODO: necessary?
        pass

    def _save_keywords(self):
        # TODO: necessary?
        pass

    def _remove_selected_rows(self):
        # TODO: necessary?
        pass

    def OnSelectCell(self, event):
        self._active_coords = _GridCoords((event.GetRow(), event.GetCol()))
        self.AutoSizeRows()
        event.Skip()

    def OnRangeSelect(self, event):
        if not event.Selecting():
            return
        whole_row_selection = self.GetSelectedRows()
        if whole_row_selection:
            topleft  = whole_row_selection[0], 0
            bottomright = whole_row_selection[-1], self.GetNumberCols()-1
        else:
            topleft = event.GetTopLeftCoords().GetRow(),\
                        event.GetTopLeftCoords().GetCol()
            bottomright = event.GetBottomRightCoords().GetRow(),\
                            event.GetBottomRightCoords().GetCol()
        self._active_coords = _GridCoords(topleft, bottomright)

    def OnCellRightClick(self, event):
        PopupMenu(self, ['Cut\tCtrl-X', 'Copy\tCtrl-C', 'Paste\tCtrl-V', '---',
                         'Delete\tDel'])

    def copy(self):
        """Copy the contents of the selected cell(s). This does a normal copy
        action if the user is editing a cell, otherwise it places the selected
        range of cells on the data.
        """
        if self.IsCellEditControlShown():
            # This is needed in Windows
            self._get_cell_edit_control().Copy()
        else:
            self._add_data_to_clipboard(self._get_selected_content())

    def cut(self):
        """Cuts the contents of the selected cell(s). This does a normal cut
        action if the user is editing a cell, otherwise it places the selected
        range of cells on the clipboard.
        """
        if self.IsCellEditControlShown():
            # This is needed in Windows
            self._get_cell_edit_control().Cut()
        else:
            self._add_data_to_clipboard(self._get_selected_content())
            self._clear_cells(self._get_selected_cells())

    def _clear_cells(self, cells):
        for row, col in cells:
            self.write_cell(row, col, '')

    def _get_selected_content(self):
        data = []
        for row in self._active_coords.get_selected_rows():
            data.append([self.GetCellValue(row, col) for
                         col in self._active_coords.get_selected_cols()])
        return data

    def _add_data_to_clipboard(self, data):
        if not data:
            return
        if self._is_single_cell_data(data) and os.name == 'nt':
            self._add_single_cell_data_to_clipboard(data)
        else:
            GRID_CLIPBOARD.set_contents(data)

    def _get_selected_cells(self):
        cells = []
        if self._active_coords is None:
            return cells
        for row in self._active_coords.get_selected_rows():
            for col in self._active_coords.get_selected_cols():
                cells.append((row, col))
        return cells

    def _is_single_cell_data(self, clipboard):
        return len(clipboard) == 1 and len(clipboard[0]) == 1

    def _add_single_cell_data_to_clipboard(self, data_table):
        #TODO: This should be moved to clipboard module
        do = wx.TextDataObject()
        do.SetText(data_table[0][0])
        wx.TheClipboard.Open()
        wx.TheClipboard.AddData(do)
        wx.TheClipboard.Close()

    def delete(self, event=None):
        if self.IsCellEditControlShown():
            # This is needed in Windows
            editor = self._get_cell_edit_control()
            start, end = editor.GetSelection()
            if start == end:
                end += 1
            editor.Remove(start, end)
            if event:
                event.Skip()
        else:
            self._clear_cells(self._get_selected_cells())
        self._save_keywords()
        self.set_dirty()

    def OnPaste(self, event=None):
        """Paste the contents of the clipboard. If a cell is being edited just
        do a normal paste. If a cell is not being edited, paste whole rows.
        """
        clipboard = GRID_CLIPBOARD.get_contents()
        if self.IsCellEditControlShown():
            # This is needed in Windows
            if isinstance(clipboard, list):
                cells_as_text = ' '.join([' '.join(row) for row in clipboard])
                self._get_cell_edit_control().WriteText(cells_as_text)
            # FIXME: there must be a better way to prevent double pasting on
            # linux, also this breaks the pasting via menu
            elif os.name == 'nt':
                self._get_cell_edit_control().Paste()
        else:
            if not clipboard:
                return
            cell = self._active_coords.topleft
            if not isinstance(clipboard, list):
                self.write_cell(cell.row, cell.col, clipboard)
            else:
                row = cell.row
                for datarow in clipboard:
                    col = cell.col
                    for value in datarow:
                        self.write_cell(row, col, value)
                        col += 1<commit_msg>the base class should not know or care about save keywords or dirty<commit_after>#  Copyright 2008-2009 Nokia Siemens Networks Oyj
#
#  Licensed under the Apache License, Version 2.0 (the "License");
#  you may not use this file except in compliance with the License.
#  You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
#  Unless required by applicable law or agreed to in writing, software
#  distributed under the License is distributed on an "AS IS" BASIS,
#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#  See the License for the specific language governing permissions and
#  limitations under the License.

import os
import wx
from wx import grid

from robotide.publish import RideGridCellChanged
from robotide.utils import PopupMenu

from clipboard import GRID_CLIPBOARD
from contentassist import ExpandingContentAssistTextCtrl
from popupwindow import RidePopupWindow


class ClipBoardHandler(object):
    def __init__(self, grid):
        self._grid = grid


class GridEditor(grid.Grid):

    def __init__(self, parent):
        grid.Grid.__init__(self, parent)
        self._bind_to_events()
        self._edit_history = []

    def _bind_to_events(self):
        self.Bind(grid.EVT_GRID_SELECT_CELL, self.OnSelectCell)
        self.Bind(grid.EVT_GRID_RANGE_SELECT, self.OnRangeSelect)
        self.Bind(grid.EVT_GRID_CELL_RIGHT_CLICK, self.OnCellRightClick)

    def OnSelectCell(self, event):
        self._active_coords = _GridCoords((event.GetRow(), event.GetCol()))
        self.AutoSizeRows()
        event.Skip()

    def OnRangeSelect(self, event):
        if not event.Selecting():
            return
        whole_row_selection = self.GetSelectedRows()
        if whole_row_selection:
            topleft  = whole_row_selection[0], 0
            bottomright = whole_row_selection[-1], self.GetNumberCols()-1
        else:
            topleft = event.GetTopLeftCoords().GetRow(),\
                        event.GetTopLeftCoords().GetCol()
            bottomright = event.GetBottomRightCoords().GetRow(),\
                            event.GetBottomRightCoords().GetCol()
        self._active_coords = _GridCoords(topleft, bottomright)

    def OnCellRightClick(self, event):
        PopupMenu(self, ['Cut\tCtrl-X', 'Copy\tCtrl-C', 'Paste\tCtrl-V', '---',
                         'Delete\tDel'])

    def copy(self):
        """Copy the contents of the selected cell(s). This does a normal copy
        action if the user is editing a cell, otherwise it places the selected
        range of cells on the data.
        """
        if self.IsCellEditControlShown():
            # This is needed in Windows
            self._get_cell_edit_control().Copy()
        else:
            self._add_data_to_clipboard(self._get_selected_content())

    def cut(self):
        """Cuts the contents of the selected cell(s). This does a normal cut
        action if the user is editing a cell, otherwise it places the selected
        range of cells on the clipboard.
        """
        if self.IsCellEditControlShown():
            # This is needed in Windows
            self._get_cell_edit_control().Cut()
        else:
            self._add_data_to_clipboard(self._get_selected_content())
            self._clear_cells(self._get_selected_cells())

    def _clear_cells(self, cells):
        for row, col in cells:
            self.write_cell(row, col, '')

    def _get_selected_content(self):
        data = []
        for row in self._active_coords.get_selected_rows():
            data.append([self.GetCellValue(row, col) for
                         col in self._active_coords.get_selected_cols()])
        return data

    def _add_data_to_clipboard(self, data):
        if not data:
            return
        if self._is_single_cell_data(data) and os.name == 'nt':
            self._add_single_cell_data_to_clipboard(data)
        else:
            GRID_CLIPBOARD.set_contents(data)

    def _get_selected_cells(self):
        cells = []
        if self._active_coords is None:
            return cells
        for row in self._active_coords.get_selected_rows():
            for col in self._active_coords.get_selected_cols():
                cells.append((row, col))
        return cells

    def _is_single_cell_data(self, clipboard):
        return len(clipboard) == 1 and len(clipboard[0]) == 1

    def _add_single_cell_data_to_clipboard(self, data_table):
        #TODO: This should be moved to clipboard module
        do = wx.TextDataObject()
        do.SetText(data_table[0][0])
        wx.TheClipboard.Open()
        wx.TheClipboard.AddData(do)
        wx.TheClipboard.Close()

    def delete(self, event=None):
        if self.IsCellEditControlShown():
            # This is needed in Windows
            editor = self._get_cell_edit_control()
            start, end = editor.GetSelection()
            if start == end:
                end += 1
            editor.Remove(start, end)
            if event:
                event.Skip()
        else:
            self._clear_cells(self._get_selected_cells())

    def OnPaste(self, event=None):
        """Paste the contents of the clipboard. If a cell is being edited just
        do a normal paste. If a cell is not being edited, paste whole rows.
        """
        clipboard = GRID_CLIPBOARD.get_contents()
        if self.IsCellEditControlShown():
            # This is needed in Windows
            if isinstance(clipboard, list):
                cells_as_text = ' '.join([' '.join(row) for row in clipboard])
                self._get_cell_edit_control().WriteText(cells_as_text)
            # FIXME: there must be a better way to prevent double pasting on
            # linux, also this breaks the pasting via menu
            elif os.name == 'nt':
                self._get_cell_edit_control().Paste()
        else:
            if not clipboard:
                return
            cell = self._active_coords.topleft
            if not isinstance(clipboard, list):
                self.write_cell(cell.row, cell.col, clipboard)
            else:
                row = cell.row
                for datarow in clipboard:
                    col = cell.col
                    for value in datarow:
                        self.write_cell(row, col, value)
                        col += 1
{'fraction_non_alphanumeric': 0.08527131782945736, 'fraction_numerical': 0.003565891472868217, 'mean_word_length': 2.1807199211045365, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7566113', 'n_tokens_mistral': 3717, 'n_tokens_neox': 3597, 'n_words': 1002}