forked from capocchi/DEVSimPy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDropTarget.py
More file actions
229 lines (171 loc) · 5.46 KB
/
DropTarget.py
File metadata and controls
229 lines (171 loc) · 5.46 KB
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
# -*- coding: utf-8 -*-
"""
Name: Components.py
Brief descritpion: All classes for components
Author(s): L. Capocchi <capocchi@univ-corse.fr>
Version: 1.0
Last modified: 2012.12.16
GENERAL NOTES AND REMARKS:
> pyreverse -a1 -s1 -f ALL -o png DropTarget.py
GLOBAL VARIABLES AND FUNCTIONS:
"""
import wx
import os
import sys
import Components
import DetachedFrame
import Container
import Utilities
_ = wx.GetTranslation
class DropTarget(wx.PyDropTarget if wx.VERSION_STRING < '4.0' else wx.DropTarget):
""" DropTarget(canvas)
"""
SOURCE = None
def __init__(self, canvas = None):
""" Constructor
"""
if wx.VERSION_STRING < '4.0':
wx.PyDropTarget.__init__(self)
else:
wx.DropTarget.__init__(self)
##local copy
self.canvas = canvas
self.mainW = wx.GetApp().GetTopWindow()
self.__setDo()
def __setDo(self):
"""
"""
# file and text names
self.__fdo = wx.FileDataObject()
self.__tdo = wx.TextDataObject()
# allows several drop format
self._do = wx.DataObjectComposite()
self._do.Add(self.__tdo)
self._do.Add(self.__fdo)
self.SetDataObject(self._do)
#def OnEnter(self, x, y, d):
#sys.stdout.write("OnEnter: %d, %d, %d\n" % (x, y, d))
#return wx.DragCopy
def OnDragOver(self,x,y,d):
"""
"""
### list of ContainerBlock shape in canvas
L = {s for s in self.canvas.diagram.GetShapeList() if isinstance(s, Container.ContainerBlock)}
### for all ContainerBlock we make a rect and test if the point x,y is in this rect to instance the DetachedFrame
for shape in L:
### point (x,y) in rect
if self.HitTest(shape, x, y):
if hasattr(self, 'timer'):
if not self.timer.IsRunning():
self.timer.Start()
else:
self.timer = self.OnDetachedFrame(shape)
else:
if hasattr(self, 'timer'):
self.timer.Stop()
del self.timer
return wx.DragCopy
def OnDetachedFrame(self, shape):
"""
"""
### Detached Frame
frame = DetachedFrame.DetachedFrame(self.canvas, wx.NewIdRef(), shape.label, shape)
### to disabled transparency
frame.Unbind(wx.EVT_IDLE)
frame.Unbind(wx.EVT_MOVE)
frame.SetIcon(self.canvas.GetTopLevelParent().GetIcon())
frame.SetFocus()
timer = wx.CallLater(1200, frame.Show)
return timer
def HitTest(self, shape, x, y):
"""
"""
w = shape.x[1]-shape.x[0]
h = shape.y[1]-shape.y[0]
rect = wx.Rect(shape.x[0], shape.y[0], w, h)
### rect1 and rect2 intersect
### abs (x - shape.x[0]) < w and abs (y - shape.y[0]) < h
return rect.ContainsXY(x,y) if wx.VERSION_STRING < '4.0' else rect.Contains(x,y)
#def OnDragOver(self, x, y, d):
# sys.stdout.write("OnDragOver: %d, %d, %d\n" % (x, y, d))
# return wx.DragCopy
#def OnLeave(self):
#sys.stdout.write("OnLeave\n")
#def OnDrop(self, x, y):
#sys.stdout.write("OnDrop: %d %d\n" % (x, y))
#return True
def OnData(self, x, y, d):
"""
"""
if self.GetData():
df = self._do.GetReceivedFormat().GetType()
### list of blocks to create
block_list = []
### dropped object come from devsimpy (Library frame)
if df in [wx.DF_UNICODETEXT, wx.DF_TEXT]:
filename = self.__tdo.GetText()
# text is the filename
text = os.path.splitext(filename)[0]
### label is composed by the number of block in diagram
label = "%s_%s"%(os.path.basename(text),str(self.canvas.GetDiagram().GetBlockCount()))
m = self.GetBlock(filename, label, x, y)
### Append new block
block_list.append(m)
### list of ContainerBlock shape in canvas
L = {s for s in self.canvas.diagram.GetShapeList() if isinstance(s, Container.ContainerBlock)}
### for all ContainerBlock we make a rect and test if the point x,y is in this rect to instance the DetachedFrame
for shape in L:
### point (x,y) in rect
if self.HitTest(shape, x, y):
### Delete new block
del block_list[-1]
shape.AddShape(m)
### display pybusyinfo during 2s
msg = _("%s model Added!")%(str(m.label)).strip()
Utilities.PyBuzyInfo(msg, 2)
sys.stdout.write(_("Adding DEVSimPy model: \n").strip())
sys.stdout.write(repr(m))
### DetachedFrame avoided
if hasattr(self, 'timer'):
self.timer.Stop()
del self.timer
### dropped object come from system (like explorer)
elif df == wx.DF_FILENAME:
for filename in self.__fdo.GetFilenames():
# text is the filename
text, ext = os.path.splitext(filename)
# label is the file name
label = os.path.basename(text)
if not ext in (".amd", '.cmd', '.py', '.pyc'):
m = Components.DSPComponent.Load(filename, label, self.canvas)
else:
m = self.GetBlock(filename, label, x, y)
### Append new block
block_list.append(m)
### if bitmap is dropped
elif df == wx.DF_BITMAP:
pass
### add all block in the diagram and trace this operation
for block in block_list:
if block:
# Adding graphical model to diagram
self.canvas.AddShape(block)
sys.stdout.write(_("Adding DEVSimPy model: \n").strip())
sys.stdout.write(repr(block))
else:
sys.stdout.write(_("ERROR: DEVSimPy model not added.\n").strip())
return d
def GetBlock(self, filename, label, x, y):
"""
"""
### Block factory
bf = Components.BlockFactory()
### Get block
m = bf.GetBlock(filename, label)
### Move and append block
if m:
### convert coordinate depending on the canvas
x,y = self.canvas.GetXY(m, x, y)
# move model from mouse position
m.move(x, y)
return m