<commit_before>#------------------------------------------------------------------------------
# 
#  Written by: <NAME>
#
#  Date: 06/25/2006
#
#  (c) Copyright 2006 by Enthought, Inc.
#
#------------------------------------------------------------------------------

""" Defines the various editors and editor factory for a drag-and-drop editor,
    for the wxPython user interface toolkit. A drag-and-drop editor represents 
    its value as a simple image which, depending upon the editor style, can be
    a drag source only, a drop target only, or both a drag source and a drop
    target.
"""

#-------------------------------------------------------------------------------
#  Imports:
#-------------------------------------------------------------------------------

import wx
import numpy

from cPickle \
    import load

from enthought.traits.api \
    import Instance, Bool
    
from enthought.traits.ui.ui_traits \
    import Image
    
from editor \
    import Editor
    
from editor_factory \
    import EditorFactory

from enthought.util.wx.drag_and_drop \
    import PythonDropSource, PythonDropTarget, clipboard
    
from enthought.io \
    import File
    
from enthought.naming.api \
    import Binding
           
from enthought.pyface.image_resource \
    import ImageResource
    
#-------------------------------------------------------------------------------
#  Constants:
#-------------------------------------------------------------------------------

# The image to use when the editor accepts files:
file_image = ImageResource( 'file' ).create_image()

# The image to use when the editor accepts objects:
object_image = ImageResource( 'object' ).create_image()

# The image to use when the editor is disabled:
inactive_image = ImageResource( 'inactive' ).create_image()

# String types:
string_type = ( str, unicode )

#-------------------------------------------------------------------------------
#  'ToolkitEditorFactory' class:
#-------------------------------------------------------------------------------

class ToolkitEditorFactory ( EditorFactory ):
    """ wxPython editor factory for drag-and-drop editors.
    """
    
    #---------------------------------------------------------------------------
    #  Trait definitions:
    #---------------------------------------------------------------------------
    
    # The image to use for the target:
    image = Image
    
    # The image to use when the target is disabled:
    disabled_image = Image
    
    #---------------------------------------------------------------------------
    #  'Editor' factory methods:
    #---------------------------------------------------------------------------
    
    def simple_editor ( self, ui, object, name, description, parent ):
        return SimpleEditor( parent,
                             factory     = self, 
                             ui          = ui, 
                             object      = object, 
                             name        = name, 
                             description = description ) 
    
    def custom_editor ( self, ui, object, name, description, parent ):
        return CustomEditor( parent,
                             factory     = self, 
                             ui          = ui, 
                             object      = object, 
                             name        = name, 
                             description = description ) 
    
    def text_editor ( self, ui, object, name, description, parent ):
        return SimpleEditor( parent,
                             factory     = self, 
                             ui          = ui, 
                             object      = object, 
                             name        = name, 
                             description = description ) 
    
    def readonly_editor ( self, ui, object, name, description, parent ):
        return ReadOnlyEditor( parent,
                             factory     = self, 
                             ui          = ui, 
                             object      = object, 
                             name        = name, 
                             description = description ) 
                                      
#-------------------------------------------------------------------------------
#  'SimpleEditor' class:
#-------------------------------------------------------------------------------
                               
class SimpleEditor ( Editor ):
    """ Simply style of editor for a drag-and-drop editor, which is both a drag
        source and a drop target.
    """
    
    #---------------------------------------------------------------------------
    #  Trait definitions:  
    #---------------------------------------------------------------------------

    # Is the editor a drop target?
    drop_target = Bool( True )
    
    # Is the editor a drag source?
    drag_source = Bool( True )
        
    #---------------------------------------------------------------------------
    #  Finishes initializing the editor by creating the underlying toolkit
    #  widget:
    #---------------------------------------------------------------------------
        
    def init ( self, parent ):
        """ Finishes initializing the editor by creating the underlying toolkit
            widget.
        """
        # Determine the drag/drop type:
        value         = self.value
        self._is_list = isinstance( value, list )
        self._is_file = (isinstance( value, string_type ) or
                         (self._is_list and (len( value ) > 0) and
                          isinstance( value[0], string_type )))
          
        # Get the right image to use:
        image = self.factory.image
        if image is not None:
            image = image.create_image()
            disabled_image = self.factory.disabled_image
            if disabled_image is not None:
                disabled_image = disabled_image.create_image()
        else:
            disabled_image = inactive_image
            image          = object_image
            if self._is_file:
                image = file_image
                
        self._image = image.ConvertToBitmap()
        if disabled_image is not None:
            self._disabled_image = disabled_image.ConvertToBitmap()
        else:
            data = numpy.reshape( numpy.fromstring( image.GetData(), 
                                                    numpy.uint8 ), 
                      ( -1, 3 ) ) * numpy.array( [ [ 0.297, 0.589, 0.114 ] ] )
            g = data[ :, 0 ] + data[ :, 1 ] + data[ :, 2 ]
            data[ :, 0 ] = data[ :, 1 ] = data[ :, 2 ] = g
            image.SetData( numpy.ravel( data.astype(numpy.uint8) ).tostring() )
            image.SetMaskColour( 0, 0, 0 )
            self._disabled_image = image.ConvertToBitmap()
        
        # Create the control and set up the event handlers:
        self.control = control = wx.Window( parent, -1, 
                         size = wx.Size( image.GetWidth(), image.GetHeight() ) )
        self.set_tooltip()
        
        if self.drop_target:
            control.SetDropTarget( PythonDropTarget( self ) )
            
        wx.EVT_LEFT_DOWN( control, self._left_down )
        wx.EVT_LEFT_UP(   control, self._left_up )
        wx.EVT_MOTION(    control, self._mouse_move )
        wx.EVT_PAINT(     control, self._on_paint )
        
    #---------------------------------------------------------------------------
    #  Updates the editor when the object trait changes external to the editor:
    #---------------------------------------------------------------------------
        
    def update_editor ( self ):
        """ Updates the editor when the object trait changes externally to the 
            editor.
        """
        return
        
#-- Private Methods ------------------------------------------------------------

    #---------------------------------------------------------------------------
    #  Returns the processed version of a drag request's data:  
    #---------------------------------------------------------------------------

    def _get_drag_data ( self, data ):
        """ Returns the processed version of a drag request's data.
        """
        if isinstance( data, list ):
            
            if isinstance( data[0], Binding ):
                data = [ item.obj for item in data ]
                
            if isinstance( data[0], File ):
                data = [ item.absolute_path for item in data ]
                if not self._is_file:
                    result = []
                    for file in data:
                        item = self._unpickle( file )
                        if item is not None:
                            result.append( item )
                    data = result
                            
        else:
            if isinstance( data, Binding ):
                data = data.obj
                
            if isinstance( data, File ):
                data = data.absolute_path
                if not self._is_file:
                    object = self._unpickle( data )<commit_msg>making traits.ui.dnd_editor only optionally depend on enthought.naming and enthought.io so it works even if those are not present<commit_after>#------------------------------------------------------------------------------
# 
#  Written by: <NAME>
#
#  Date: 06/25/2006
#
#  (c) Copyright 2006 by Enthought, Inc.
#
#------------------------------------------------------------------------------

""" Defines the various editors and editor factory for a drag-and-drop editor,
    for the wxPython user interface toolkit. A drag-and-drop editor represents 
    its value as a simple image which, depending upon the editor style, can be
    a drag source only, a drop target only, or both a drag source and a drop
    target.
"""

#-------------------------------------------------------------------------------
#  Imports:
#-------------------------------------------------------------------------------

import wx
import numpy

from cPickle \
    import load

from enthought.traits.api \
    import Instance, Bool
    
from enthought.traits.ui.ui_traits \
    import Image
    
from editor \
    import Editor
    
from editor_factory \
    import EditorFactory

from enthought.util.wx.drag_and_drop \
    import PythonDropSource, PythonDropTarget, clipboard
    
try:
    from enthought.io import File
except ImportError:
    File = None
    
try:
    from enthought.naming.api import Binding
except ImportError:
    Binding = None
           
from enthought.pyface.image_resource \
    import ImageResource
    
#-------------------------------------------------------------------------------
#  Constants:
#-------------------------------------------------------------------------------

# The image to use when the editor accepts files:
file_image = ImageResource( 'file' ).create_image()

# The image to use when the editor accepts objects:
object_image = ImageResource( 'object' ).create_image()

# The image to use when the editor is disabled:
inactive_image = ImageResource( 'inactive' ).create_image()

# String types:
string_type = ( str, unicode )

#-------------------------------------------------------------------------------
#  'ToolkitEditorFactory' class:
#-------------------------------------------------------------------------------

class ToolkitEditorFactory ( EditorFactory ):
    """ wxPython editor factory for drag-and-drop editors.
    """
    
    #---------------------------------------------------------------------------
    #  Trait definitions:
    #---------------------------------------------------------------------------
    
    # The image to use for the target:
    image = Image
    
    # The image to use when the target is disabled:
    disabled_image = Image
    
    #---------------------------------------------------------------------------
    #  'Editor' factory methods:
    #---------------------------------------------------------------------------
    
    def simple_editor ( self, ui, object, name, description, parent ):
        return SimpleEditor( parent,
                             factory     = self, 
                             ui          = ui, 
                             object      = object, 
                             name        = name, 
                             description = description ) 
    
    def custom_editor ( self, ui, object, name, description, parent ):
        return CustomEditor( parent,
                             factory     = self, 
                             ui          = ui, 
                             object      = object, 
                             name        = name, 
                             description = description ) 
    
    def text_editor ( self, ui, object, name, description, parent ):
        return SimpleEditor( parent,
                             factory     = self, 
                             ui          = ui, 
                             object      = object, 
                             name        = name, 
                             description = description ) 
    
    def readonly_editor ( self, ui, object, name, description, parent ):
        return ReadOnlyEditor( parent,
                             factory     = self, 
                             ui          = ui, 
                             object      = object, 
                             name        = name, 
                             description = description ) 
                                      
#-------------------------------------------------------------------------------
#  'SimpleEditor' class:
#-------------------------------------------------------------------------------
                               
class SimpleEditor ( Editor ):
    """ Simply style of editor for a drag-and-drop editor, which is both a drag
        source and a drop target.
    """
    
    #---------------------------------------------------------------------------
    #  Trait definitions:  
    #---------------------------------------------------------------------------

    # Is the editor a drop target?
    drop_target = Bool( True )
    
    # Is the editor a drag source?
    drag_source = Bool( True )
        
    #---------------------------------------------------------------------------
    #  Finishes initializing the editor by creating the underlying toolkit
    #  widget:
    #---------------------------------------------------------------------------
        
    def init ( self, parent ):
        """ Finishes initializing the editor by creating the underlying toolkit
            widget.
        """
        # Determine the drag/drop type:
        value         = self.value
        self._is_list = isinstance( value, list )
        self._is_file = (isinstance( value, string_type ) or
                         (self._is_list and (len( value ) > 0) and
                          isinstance( value[0], string_type )))
          
        # Get the right image to use:
        image = self.factory.image
        if image is not None:
            image = image.create_image()
            disabled_image = self.factory.disabled_image
            if disabled_image is not None:
                disabled_image = disabled_image.create_image()
        else:
            disabled_image = inactive_image
            image          = object_image
            if self._is_file:
                image = file_image
                
        self._image = image.ConvertToBitmap()
        if disabled_image is not None:
            self._disabled_image = disabled_image.ConvertToBitmap()
        else:
            data = numpy.reshape( numpy.fromstring( image.GetData(), 
                                                    numpy.uint8 ), 
                      ( -1, 3 ) ) * numpy.array( [ [ 0.297, 0.589, 0.114 ] ] )
            g = data[ :, 0 ] + data[ :, 1 ] + data[ :, 2 ]
            data[ :, 0 ] = data[ :, 1 ] = data[ :, 2 ] = g
            image.SetData( numpy.ravel( data.astype(numpy.uint8) ).tostring() )
            image.SetMaskColour( 0, 0, 0 )
            self._disabled_image = image.ConvertToBitmap()
        
        # Create the control and set up the event handlers:
        self.control = control = wx.Window( parent, -1, 
                         size = wx.Size( image.GetWidth(), image.GetHeight() ) )
        self.set_tooltip()
        
        if self.drop_target:
            control.SetDropTarget( PythonDropTarget( self ) )
            
        wx.EVT_LEFT_DOWN( control, self._left_down )
        wx.EVT_LEFT_UP(   control, self._left_up )
        wx.EVT_MOTION(    control, self._mouse_move )
        wx.EVT_PAINT(     control, self._on_paint )
        
    #---------------------------------------------------------------------------
    #  Updates the editor when the object trait changes external to the editor:
    #---------------------------------------------------------------------------
        
    def update_editor ( self ):
        """ Updates the editor when the object trait changes externally to the 
            editor.
        """
        return
        
#-- Private Methods ------------------------------------------------------------

    #---------------------------------------------------------------------------
    #  Returns the processed version of a drag request's data:  
    #---------------------------------------------------------------------------

    def _get_drag_data ( self, data ):
        """ Returns the processed version of a drag request's data.
        """
        if isinstance( data, list ):
            
            if Binding is not None and if isinstance( data[0], Binding ):
                data = [ item.obj for item in data ]
                
            if File is not None and isinstance( data[0], File ):
                data = [ item.absolute_path for item in data ]
                if not self._is_file:
                    result = []
                    for file in data:
                        item = self._unpickle( file )
                        if item is not None:
                            result.append( item )
                    data = result
                            
        else:
            if Binding is not None and isinstance( data, Binding ):
                data = data.obj
                
            if File is not None and isinstance( data, File ):
                data = data.absolute_path
                if not self._is_file:
                    object = self._unpickle( data )
{'fraction_non_alphanumeric': 0.24940771053198363, 'fraction_numerical': 0.004522937755761361, 'mean_word_length': 0.8830984487478455, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 24, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1064245', 'n_tokens_mistral': 4067, 'n_tokens_neox': 3860, 'n_words': 1234}