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
|
# (C) Copyright 2004-2023 Enthought, Inc., Austin, TX
# All rights reserved.
#
# This software is provided without warranty under the terms of the BSD
# license included in LICENSE.txt and may be redistributed only under
# the conditions described in the aforementioned license. The license
# is also available online at http://www.enthought.com/licenses/BSD.txt
#
# Thanks for using Enthought open source!
""" Defines the various editors 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.
"""
import wx
import numpy
from pickle import load
from traits.api import Bool
from pyface.wx.drag_and_drop import (
PythonDropSource,
PythonDropTarget,
clipboard,
)
try:
from apptools.io import File
except ImportError:
File = None
try:
from apptools.naming.api import Binding
except ImportError:
Binding = None
from pyface.image_resource import ImageResource
from .editor import Editor
# 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, str)
# -------------------------------------------------------------------------
# '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)
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))
control.Bind(wx.EVT_LEFT_DOWN, self._left_down)
control.Bind(wx.EVT_LEFT_UP, self._left_up)
control.Bind(wx.EVT_MOTION, self._mouse_move)
control.Bind(wx.EVT_PAINT, self._on_paint)
def dispose(self):
"""Disposes of the contents of an editor."""
control = self.control
control.Unbind(wx.EVT_LEFT_DOWN)
control.Unbind(wx.EVT_LEFT_UP)
control.Unbind(wx.EVT_MOTION)
control.Unbind(wx.EVT_PAINT)
super().dispose()
def update_editor(self):
"""Updates the editor when the object trait changes externally to the
editor.
"""
return
# -- Private Methods ------------------------------------------------------
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 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)
if object is not None:
data = object
return data
def _unpickle(self, file_name):
"""Returns the unpickled version of a specified file (if possible)."""
with open(file_name, "rb") as fh:
try:
object = load(fh)
except Exception:
object = None
return object
# -- wxPython Event Handlers ----------------------------------------------
def _on_paint(self, event):
"""Called when the control needs repainting."""
image = self._image
control = self.control
if not control.IsEnabled():
image = self._disabled_image
wdx, wdy = control.GetClientSize()
wx.PaintDC(control).DrawBitmap(
image,
(wdx - image.GetWidth()) // 2,
(wdy - image.GetHeight()) // 2,
True,
)
def _left_down(self, event):
"""Handles the left mouse button being pressed."""
if self.control.IsEnabled() and self.drag_source:
self._x, self._y = event.GetX(), event.GetY()
self.control.CaptureMouse()
event.Skip()
def _left_up(self, event):
"""Handles the left mouse button being released."""
if self._x is not None:
self._x = None
self.control.ReleaseMouse()
event.Skip()
def _mouse_move(self, event):
"""Handles the mouse being moved."""
if self._x is not None:
if (
abs(self._x - event.GetX()) + abs(self._y - event.GetY())
) >= 3:
self.control.ReleaseMouse()
self._x = None
if self._is_file:
FileDropSource(self.control, self.value)
else:
PythonDropSource(self.control, self.value)
event.Skip()
# ----- Drag and drop event handlers: -------------------------------------
def wx_dropped_on(self, x, y, data, drag_result):
"""Handles a Python object being dropped on the tree."""
try:
self.value = self._get_drag_data(data)
return drag_result
except:
return wx.DragNone
def wx_drag_over(self, x, y, data, drag_result):
"""Handles a Python object being dragged over the tree."""
try:
self.object.base_trait(self.name).validate(
self.object, self.name, self._get_drag_data(data)
)
return drag_result
except:
return wx.DragNone
class CustomEditor(SimpleEditor):
"""Custom style of drag-and-drop editor, which is not a drag source."""
# -------------------------------------------------------------------------
# Trait definitions:
# -------------------------------------------------------------------------
#: Is the editor a drag source? This value overrides the default.
drag_source = False
class ReadonlyEditor(SimpleEditor):
"""Read-only style of drag-and-drop editor, which is not a drop target."""
# -------------------------------------------------------------------------
# Trait definitions:
# -------------------------------------------------------------------------
#: Is the editor a drop target? This value overrides the default.
drop_target = False
class FileDropSource(wx.DropSource):
"""Represents a draggable file."""
def __init__(self, source, files):
"""Initializes the object."""
self.handler = None
self.allow_move = True
# Put the data to be dragged on the clipboard:
clipboard.data = files
clipboard.source = source
clipboard.drop_source = self
data_object = wx.FileDataObject()
if isinstance(files, string_type):
files = [files]
for file in files:
data_object.AddFile(file)
# Create the drop source and begin the drag and drop operation:
super().__init__(source)
self.SetData(data_object)
self.result = self.DoDragDrop(True)
def on_dropped(self, drag_result):
"""Called when the data has been dropped."""
return
|