1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
|
#-------------------------------------------------------------------------------
#
# Adds a 'drop_file' feature to DockWindow which allows the object associated
# with a DockControl to expose a trait which can accept files dropped onto it.
# The trait can either accept strings (i.e. file names), FilePosition objects,
# or lists of the above. The trait which accepts files should have 'drop_file'
# metadata, which can either be True, a file suffix (e.g. '.py'), a list
# of file extensions, or a DropFile object. If file extensions are specified,
# then only files with corresponding extensions can be dropped on the view.
#
# Specifying a DropFile object as the 'drop_file' metadata also provides the
# option of making the trait draggable, as well as specifying a custom
# tooltip.
#
# Written by: David C. Morrill
#
# Date: 07/19/2006
#
# (c) Copyright 2006 by David C. Morrill
#
#-------------------------------------------------------------------------------
""" Copyright 2006 by David C. Morrill """
#-------------------------------------------------------------------------------
# Imports:
#-------------------------------------------------------------------------------
from os.path \
import exists, splitext
from enthought.traits.api \
import HasTraits, HasStrictTraits, Str, List, Enum, false
from enthought.pyface.api \
import FileDialog, OK
from enthought.pyface.dock.api \
import DockWindowFeature
from enthought.pyface.image_resource \
import ImageResource
from enthought.io.api \
import File
from enthought.developer.api \
import HasPayload, FilePosition
from feature_metadata \
import DropFile
#-------------------------------------------------------------------------------
# Constants:
#-------------------------------------------------------------------------------
a_file_position = FilePosition()
# Valid 'drop styles' and their corresponding types:
valid_types = (
( 'file_positions', [ a_file_position ] ),
( 'file_position', a_file_position ),
( 'file_names', [ '\\test' ] ),
( 'file_name', '\\test' )
)
# Feature images:
feature_images = (
ImageResource( 'drop_file_feature' ),
ImageResource( 'drag_file_feature' )
)
#-------------------------------------------------------------------------------
# Defines the 'drop_file' metadata filter:
#-------------------------------------------------------------------------------
def drop_file_filter ( value ):
return ((value is True) or
isinstance( value, ( str, list, tuple, DropFile ) ))
#-------------------------------------------------------------------------------
# 'DropFileFeature' class:
#-------------------------------------------------------------------------------
class DropFileFeature ( DockWindowFeature ):
#---------------------------------------------------------------------------
# Class variables:
#---------------------------------------------------------------------------
# The user interface name of the feature:
feature_name = 'Drag and Drop Files'
#---------------------------------------------------------------------------
# Trait definitions:
#---------------------------------------------------------------------------
# The trait on the object to assign dropped files to:
name = Str
# The list of valid extensions for dropped files:
extensions = List( Str )
# Is the trait also draggable?
draggable = false
# The type of value expected by the trait:
drop_style = Enum( 'file_name', 'file_names',
'file_position', 'file_positions' )
#-- DockWindowFeature Method Overrides -----------------------------------------
#---------------------------------------------------------------------------
# Handles the user left clicking on the feature image:
#---------------------------------------------------------------------------
def click ( self ):
""" Handles the user left clicking on the feature image.
"""
# Create the file dialog:
fd = FileDialog()
# Set up the default path based on the current value (if any):
default_path = getattr( self.dock_control.object, self.name, None )
if default_path is not None:
fd.default_path = default_path
# Set up the appropriate extension filters (if any):
if len( self.extensions ) > 0:
fd.wildcard = '\n'.join([ FileDialog.create_wildcard('', '*' + ext)
for ext in self.extensions ])
# Display the file dialog, and if successful, set the new file name:
if fd.open() == OK:
self.drop( fd.path )
#---------------------------------------------------------------------------
# Returns the object to be dragged when the user drags the feature image:
#---------------------------------------------------------------------------
def drag ( self ):
""" Returns the object to be dragged when the user drags the feature
image.
"""
if self.draggable:
result = getattr( self.dock_control.object, self.name, None )
if result:
return result
return None
#---------------------------------------------------------------------------
# Handles the user dropping a specified object on the feature image:
#---------------------------------------------------------------------------
def drop ( self, object ):
""" Handles the user dropping a specified object on the feature image.
"""
# Extract the list of FilePositions to be assigned:
if isinstance( object, FilePosition ):
file_positions = [ object ]
elif isinstance( object, HasPayload ):
file_positions = [ FilePosition( file_name = object.payload ) ]
elif isinstance( object, basestring ):
file_positions = [ FilePosition( file_name = object ) ]
else:
file_positions = [ FilePosition( file_name = file.absolute_path )
for file in object ]
# Assign them using the correct 'drop_style':
getattr( self, '_drop_' + self.drop_style )( file_positions )
#---------------------------------------------------------------------------
# Returns whether a specified object can be dropped on the feature image:
#---------------------------------------------------------------------------
def can_drop ( self, object ):
""" Returns whether a specified object can be dropped on the feature
image.
"""
if isinstance( object, FilePosition ):
file_names = [ object.file_name ]
elif (isinstance( object, HasPayload ) and
isinstance( object.payload, str )):
file_names = [ object.payload ]
elif isinstance( object, basestring ) and exists( object ):
file_names = [ object ]
elif isinstance( object, list ):
file_names = [ file.absolute_path for file in object
if isinstance( file, File ) ]
if len( file_names ) != len( object ):
return False
else:
return False
extensions = self.extensions
if len( extensions ) == 0:
return True
for file_name in file_names:
ext = splitext( file_name )[1]
if ext not in extensions:
return False
return True
#-- Private Methods ------------------------------------------------------------
#---------------------------------------------------------------------------
# Drops a single file name:
#---------------------------------------------------------------------------
def _drop_file_name ( self, file_positions ):
""" Drops a single file name.
"""
object = self.dock_control.object
name = self.name
for fp in file_positions:
setattr( object, name, fp.file_name )
#---------------------------------------------------------------------------
# Drops a single file position:
#---------------------------------------------------------------------------
def _drop_file_position ( self, file_positions ):
""" Drops a single file name.
"""
object = self.dock_control.object
name = self.name
for fp in file_positions:
setattr( object, name, fp )
#---------------------------------------------------------------------------
# Drops a list of file names:
#---------------------------------------------------------------------------
def _drop_file_names ( self, file_positions ):
""" Drops a list of file names.
"""
setattr( self.dock_control.object, self.name,
[ fp.file_name for fp in file_positions ] )
#---------------------------------------------------------------------------
# Drops a list of file positions:
#---------------------------------------------------------------------------
def _drop_file_positions ( self, file_positions ):
""" Drops a list of file names.
"""
setattr( self.dock_control.object, self.name, file_positions )
#---------------------------------------------------------------------------
# Set the correct images based on the current 'draggable' setting:
#---------------------------------------------------------------------------
def _set_image ( self ):
""" Set the correct images based on the current 'draggable' setting.
"""
self.image = feature_images[ self.draggable ]
#-- Event Handlers -------------------------------------------------------------
#---------------------------------------------------------------------------
# Handles the 'draggable' trait being changed:
#---------------------------------------------------------------------------
def _draggable_changed ( self, draggable ):
""" Handles the 'draggable' trait being changed.
"""
self._set_image()
#-- Overridable Class Methods --------------------------------------------------
#---------------------------------------------------------------------------
# Returns a feature object for use with the specified DockControl (or None
# if the feature does not apply to the DockControl object):
#---------------------------------------------------------------------------
def feature_for ( cls, dock_control ):
""" Returns a feature object for use with the specified DockControl (or
None if the feature does not apply to the DockControl object).
"""
object = dock_control.object
if isinstance( object, HasTraits ):
traits = object.traits( drop_file = drop_file_filter )
if len( traits ) == 1:
name, trait = traits.items()[0]
drop_file = trait.drop_file
draggable = False
tooltip = ''
# Determine the set of valid file extensions and generate a
# corresponding tooltip to describe them:
if isinstance( drop_file, DropFile ):
extensions = drop_file.extensions
draggable = drop_file.draggable
tooltip = drop_file.tooltip
elif isinstance( drop_file, basestring ):
extensions = [ drop_file ]
elif isinstance( drop_file, ( list, tuple ) ):
extensions = list( drop_file )
else:
extensions = []
if tooltip == '':
if draggable:
tooltip = 'Drag this file.\n'
exts = ', '.join( extensions )
if len( exts ) > 0:
exts += ' '
tooltip += 'Drop a %sfile here.' % exts
# Determine the type of value the trait accepts and save it as
# the feature's 'drop_style':
for drop_style, type in valid_types:
try:
trait.validate( object, name, type )
break
except:
pass
else:
# Doesn't accept any type we know how to make...give up!
return None
# Return the feature for this object:
result = cls( dock_control = dock_control,
name = name,
drop_style = drop_style,
extensions = extensions,
draggable = draggable,
tooltip = tooltip )
result._set_image()
return result
# Indicate this feature does not apply to this object:
return None
feature_for = classmethod( feature_for )
|