<commit_before>from chaco.grid_mapper import GridMapper
from traits.api import HasTraits

class ToolState(HasTraits):

    def __init__(self, prev, next):
        self.prev = prev
        self.next = next

    def apply(self, tool):
        raise NotImplementedError()

    def revert(self, tool):
        raise NotImplementedError()

class GroupedToolState(ToolState):

    def __init__(self, states):
        self.states = states

    def apply(self, tool):
        for state in self.states:
            state.apply(tool)

    def revert(self, tool):
        for state in self.states[::-1]:
            state.revert(tool)

class PanState(ToolState):


    def apply(self, tool):
        if isinstance(tool.component.index_mapper, GridMapper):
            index_mapper = tool.component.index_mapper._xmapper
            value_mapper = tool.component.index_mapper._ymapper
        else:
            index_mapper = tool.component.index_mapper
            value_mapper = tool.component.value_mapper

        high = index_mapper.range.high
        low = index_mapper.range.low
        range = high-low

        index_mapper.range.high = self.next[0] + range/2
        index_mapper.range.low = self.next[0] - range/2

        high = value_mapper.range.high
        low = value_mapper.range.low
        range = high-low

        value_mapper.range.high = self.next[1] + range/2
        value_mapper.range.low = self.next[1] - range/2

    def revert(self, tool):
        if isinstance(tool.component.index_mapper, GridMapper):
            index_mapper = tool.component.index_mapper._xmapper
            value_mapper = tool.component.index_mapper._ymapper
        else:
            index_mapper = tool.component.index_mapper
            value_mapper = tool.component.value_mapper

        high = index_mapper.range.high
        low = index_mapper.range.low
        range = high-low

        index_mapper.range.high = self.prev[0] + range/2
        index_mapper.range.low = self.prev[0] - range/2

        high = value_mapper.range.high
        low = value_mapper.range.low
        range = high-low

        value_mapper.range.high = self.prev[1] + range/2
        value_mapper.range.low = self.prev[1] - range/2

class ZoomState(ToolState):
    """ A zoom state which can be applied and reverted.

        This class exists so that subclasses can introduce new types
        of events which can be applied and reverted in the same manner.
        This greatly eases the code for managing history
    """
    def apply(self, zoom_tool):
        index_factor = self.next[0]/self.prev[0]
        value_factor = self.next[1]/self.prev[1]

        if isinstance(zoom_tool.component.index_mapper, GridMapper):
            index_mapper = zoom_tool.component.index_mapper._xmapper
            value_mapper = zoom_tool.component.index_mapper._ymapper
        else:
            index_mapper = zoom_tool.component.index_mapper
            value_mapper = zoom_tool.component.value_mapper<commit_msg>pan_state: don't modify mapper range if no change<commit_after>from chaco.grid_mapper import GridMapper
from traits.api import HasTraits

class ToolState(HasTraits):

    def __init__(self, prev, next):
        self.prev = prev
        self.next = next

    def apply(self, tool):
        raise NotImplementedError()

    def revert(self, tool):
        raise NotImplementedError()

class GroupedToolState(ToolState):

    def __init__(self, states):
        self.states = states

    def apply(self, tool):
        for state in self.states:
            state.apply(tool)

    def revert(self, tool):
        for state in self.states[::-1]:
            state.revert(tool)

class PanState(ToolState):


    def apply(self, tool):
        if isinstance(tool.component.index_mapper, GridMapper):
            index_mapper = tool.component.index_mapper._xmapper
            value_mapper = tool.component.index_mapper._ymapper
        else:
            index_mapper = tool.component.index_mapper
            value_mapper = tool.component.value_mapper
        if self.next[0] != self.prev[0]:
            high = index_mapper.range.high
            low = index_mapper.range.low
            range = high-low

            index_mapper.range.high = self.next[0] + range/2
            index_mapper.range.low = self.next[0] - range/2

        if self.next[1] != self.prev[1]:
            high = value_mapper.range.high
            low = value_mapper.range.low
            range = high-low

            value_mapper.range.high = self.next[1] + range/2
            value_mapper.range.low = self.next[1] - range/2

    def revert(self, tool):
        if isinstance(tool.component.index_mapper, GridMapper):
            index_mapper = tool.component.index_mapper._xmapper
            value_mapper = tool.component.index_mapper._ymapper
        else:
            index_mapper = tool.component.index_mapper
            value_mapper = tool.component.value_mapper

        if self.next[0] != self.prev[0]:
            high = index_mapper.range.high
            low = index_mapper.range.low
            range = high-low

            index_mapper.range.high = self.prev[0] + range/2
            index_mapper.range.low = self.prev[0] - range/2

        if self.next[1] != self.prev[1]:
            high = value_mapper.range.high
            low = value_mapper.range.low
            range = high-low

            value_mapper.range.high = self.prev[1] + range/2
            value_mapper.range.low = self.prev[1] - range/2

class ZoomState(ToolState):
    """ A zoom state which can be applied and reverted.

        This class exists so that subclasses can introduce new types
        of events which can be applied and reverted in the same manner.
        This greatly eases the code for managing history
    """
    def apply(self, zoom_tool):
        index_factor = self.next[0]/self.prev[0]
        value_factor = self.next[1]/self.prev[1]

        if isinstance(zoom_tool.component.index_mapper, GridMapper):
            index_mapper = zoom_tool.component.index_mapper._xmapper
            value_mapper = zoom_tool.component.index_mapper._ymapper
        else:
            index_mapper = zoom_tool.component.index_mapper
            value_mapper = zoom_tool.component.value_mapper
{'fraction_non_alphanumeric': 0.09004967152699887, 'fraction_numerical': 0.00801153661272232, 'mean_word_length': 2.2595300261096605, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '7189731', 'n_tokens_mistral': 1907, 'n_tokens_neox': 1856, 'n_words': 400}