1
|
|
# ------------------------------------------------------------------------------
|
2
|
|
# Copyright (c) 2008, Riverbank Computing Limited
|
3
|
|
# All rights reserved.
|
4
|
|
#
|
5
|
|
# This software is provided without warranty under the terms of the BSD license.
|
6
|
|
# However, when used with the GPL version of PyQt the additional terms
|
7
|
|
# described in the PyQt GPL exception also apply
|
8
|
|
|
9
|
|
#
|
10
|
|
# Author: Riverbank Computing Limited
|
11
|
|
# ------------------------------------------------------------------------------
|
12
|
|
|
13
|
8
|
""" Defines a drop target editor for the PyQt user interface toolkit. A drop
|
14
|
|
target editor handles drag and drop operations as a drop target.
|
15
|
|
"""
|
16
|
|
|
17
|
|
|
18
|
8
|
from pyface.qt import QtGui, QtCore
|
19
|
|
|
20
|
|
# FIXME: ToolkitEditorFactory is a proxy class defined here just for backward
|
21
|
|
# compatibility. The class has been moved to the
|
22
|
|
# traitsui.editors.drop_editor file.
|
23
|
8
|
from traitsui.editors.drop_editor import ToolkitEditorFactory
|
24
|
|
|
25
|
8
|
from .editor import Editor as _BaseEditor
|
26
|
8
|
from .text_editor import SimpleEditor as Editor
|
27
|
8
|
from .constants import DropColor
|
28
|
8
|
from .clipboard import PyMimeData, clipboard
|
29
|
|
|
30
|
|
|
31
|
8
|
class SimpleEditor(Editor):
|
32
|
|
""" Simple style of drop editor, which displays a read-only text field that
|
33
|
|
contains the string representation of the object trait's value.
|
34
|
|
"""
|
35
|
|
|
36
|
|
#: Background color when it is OK to drop objects.
|
37
|
8
|
ok_color = DropColor
|
38
|
|
|
39
|
8
|
def init(self, parent):
|
40
|
|
""" Finishes initializing the editor by creating the underlying toolkit
|
41
|
|
widget.
|
42
|
|
"""
|
43
|
11
|
if self.factory.readonly:
|
44
|
8
|
self.control = QtGui.QLineEdit(self.str_value)
|
45
|
8
|
self.control.setReadOnly(True)
|
46
|
8
|
self.set_tooltip()
|
47
|
|
else:
|
48
|
8
|
super(SimpleEditor, self).init(parent)
|
49
|
|
|
50
|
8
|
pal = QtGui.QPalette(self.control.palette())
|
51
|
8
|
pal.setColor(QtGui.QPalette.Base, self.ok_color)
|
52
|
8
|
self.control.setPalette(pal)
|
53
|
|
|
54
|
|
# Install EventFilter on control to handle DND events.
|
55
|
8
|
drop_event_filter = _DropEventFilter(self.control)
|
56
|
8
|
self.control.installEventFilter(drop_event_filter)
|
57
|
|
|
58
|
8
|
self.control._qt4_editor = self
|
59
|
|
|
60
|
8
|
def dispose(self):
|
61
|
|
""" Disposes of the content of an editor.
|
62
|
|
"""
|
63
|
11
|
if self.factory.readonly:
|
64
|
|
# enthought/traitsui#884
|
65
|
8
|
_BaseEditor.dispose(self)
|
66
|
|
else:
|
67
|
8
|
super(SimpleEditor, self).dispose()
|
68
|
|
|
69
|
8
|
def string_value(self, value):
|
70
|
|
""" Returns the text representation of a specified object trait value.
|
71
|
|
"""
|
72
|
11
|
if value is None:
|
73
|
0
|
return ""
|
74
|
8
|
return str(value)
|
75
|
|
|
76
|
8
|
def error(self, excp):
|
77
|
|
""" Handles an error that occurs while setting the object's trait value.
|
78
|
|
"""
|
79
|
0
|
pass
|
80
|
|
|
81
|
|
|
82
|
8
|
class _DropEventFilter(QtCore.QObject):
|
83
|
8
|
def eventFilter(self, source, event):
|
84
|
8
|
typ = event.type()
|
85
|
11
|
if typ == QtCore.QEvent.Drop:
|
86
|
0
|
self.dropEvent(event)
|
87
|
11
|
elif typ == QtCore.QEvent.DragEnter:
|
88
|
0
|
self.dragEnterEvent(event)
|
89
|
8
|
return super(_DropEventFilter, self).eventFilter(source, event)
|
90
|
|
|
91
|
8
|
def dropEvent(self, e):
|
92
|
|
""" Handles a Python object being dropped on the tree.
|
93
|
|
"""
|
94
|
0
|
editor = self.parent()._qt4_editor
|
95
|
|
|
96
|
0
|
klass = editor.factory.klass
|
97
|
|
|
98
|
11
|
if editor.factory.binding:
|
99
|
0
|
value = getattr(clipboard, "node", None)
|
100
|
|
else:
|
101
|
0
|
value = e.mimeData().instance()
|
102
|
|
|
103
|
11
|
if (klass is None) or isinstance(value, klass):
|
104
|
0
|
editor._no_update = True
|
105
|
0
|
try:
|
106
|
11
|
if hasattr(value, "drop_editor_value"):
|
107
|
0
|
editor.value = value.drop_editor_value()
|
108
|
|
else:
|
109
|
0
|
editor.value = value
|
110
|
11
|
if hasattr(value, "drop_editor_update"):
|
111
|
0
|
value.drop_editor_update(self)
|
112
|
|
else:
|
113
|
0
|
self.setText(editor.str_value)
|
114
|
|
finally:
|
115
|
0
|
editor._no_update = False
|
116
|
|
|
117
|
0
|
e.acceptProposedAction()
|
118
|
|
|
119
|
8
|
def dragEnterEvent(self, e):
|
120
|
|
""" Handles a Python object being dragged over the tree.
|
121
|
|
"""
|
122
|
0
|
editor = self.parent()._qt4_editor
|
123
|
|
|
124
|
11
|
if editor.factory.binding:
|
125
|
0
|
data = getattr(clipboard, "node", None)
|
126
|
|
else:
|
127
|
0
|
md = e.mimeData()
|
128
|
|
|
129
|
11
|
if not isinstance(md, PyMimeData):
|
130
|
0
|
return
|
131
|
|
|
132
|
0
|
data = md.instance()
|
133
|
|
|
134
|
0
|
try:
|
135
|
0
|
editor.object.base_trait(editor.name).validate(
|
136
|
|
editor.object, editor.name, data
|
137
|
|
)
|
138
|
0
|
e.acceptProposedAction()
|
139
|
0
|
except:
|
140
|
0
|
pass
|
141
|
|
|
142
|
|
|
143
|
|
# Define the Text and ReadonlyEditor for use by the editor factory.
|
144
|
8
|
TextEditor = ReadonlyEditor = SimpleEditor
|