2 # Copyright (c) 2002, 2003, 2004, 2005, 2006, 2007 Art Haas
4 # This file is part of PythonCAD.
6 # PythonCAD is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
11 # PythonCAD is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with PythonCAD; if not, write to the Free Software
18 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 # handle cut, copy, paste, selections, etc
28 from PythonCAD
.Generic
.point
import Point
29 from PythonCAD
.Generic
.segment
import Segment
30 from PythonCAD
.Generic
.circle
import Circle
31 from PythonCAD
.Generic
.arc
import Arc
32 from PythonCAD
.Generic
.hcline
import HCLine
33 from PythonCAD
.Generic
.vcline
import VCLine
34 from PythonCAD
.Generic
.acline
import ACLine
35 from PythonCAD
.Generic
.cline
import CLine
36 from PythonCAD
.Generic
.ccircle
import CCircle
37 from PythonCAD
.Generic
.dimension
import LinearDimension
38 from PythonCAD
.Generic
.dimension
import HorizontalDimension
39 from PythonCAD
.Generic
.dimension
import VerticalDimension
40 from PythonCAD
.Generic
.dimension
import RadialDimension
41 from PythonCAD
.Generic
.dimension
import AngularDimension
42 from PythonCAD
.Generic
import text
43 import PythonCAD
.Generic
.globals
45 def select_region_end_cb(gtkimage
, widget
, event
, tool
):
46 # print "called select_region_end_callback()"
47 _image
= gtkimage
.getImage()
48 _x2
, _y2
= _image
.getCurrentPoint()
49 _y1
= tool
.popObject()
50 _x1
= tool
.popObject()
55 tool
.delHandler("motion_notify")
56 _active_layer
= _image
.getActiveLayer()
57 _objs
= _active_layer
.objsInRegion(_xmin
, _ymin
, _xmax
, _ymax
)
59 _image
.sendMessage('group_action_started')
62 _image
.selectObject(_obj
)
64 _image
.sendMessage('group_action_ended')
65 gtkimage
.setPrompt(_('Click on the items you want to select.'))
66 select_mode_init(gtkimage
)
68 def select_motion_notify(gtkimage
, widget
, event
, tool
):
69 _tx
, _ty
= tool
.getLocation()
70 _px
, _py
= gtkimage
.coordToPixTransform(_tx
, _ty
)
71 _gc
= gtkimage
.getGC()
74 _cp
= tool
.getCurrentPoint()
81 widget
.window
.draw_rectangle(_gc
, False, _xmin
, _ymin
, _rw
, _rh
)
82 tool
.setCurrentPoint(_x
, _y
)
87 widget
.window
.draw_rectangle(_gc
, False, _xmin
, _ymin
, _rw
, _rh
)
90 def select_button_press_cb(gtkimage
, widget
, event
, tool
):
91 _tol
= gtkimage
.getTolerance()
92 _image
= gtkimage
.getImage()
93 _x
, _y
= _image
.getCurrentPoint()
94 # print "x: %g; y: %g" % (_x, _y)
95 _active_layer
= _image
.getActiveLayer()
96 _pts
= _active_layer
.find('point', _x
, _y
, _tol
)
98 _image
.sendMessage('group_action_started')
101 _image
.selectObject(_pt
)
103 _image
.sendMessage('group_action_ended')
106 for _tb
in _active_layer
.getLayerEntities('text'):
107 # print "testing tb: " + `_tb`
108 _tx
, _ty
= _tb
.getLocation()
109 # print "tx: %g; ty: %g" % (_tx, _ty)
110 _bounds
= _tb
.getBounds()
111 if _bounds
is not None:
112 # print "has bounds ..."
114 _align
= _tb
.getAlignment()
115 if _align
== text
.TextStyle
.ALIGN_LEFT
:
118 elif _align
== text
.TextStyle
.ALIGN_CENTER
:
122 elif _align
== text
.TextStyle
.ALIGN_RIGHT
:
126 raise ValueError, "Unexpected alignment: %d" % _align
129 # print "txmin: %g" % _txmin
130 # print "tymin: %g" % _tymin
131 # print "txmax: %g" % _txmax
132 # print "tymax: %g" % _tymax
133 if _txmin
< _x
< _txmax
and _tymin
< _y
< _tymax
:
135 for _obj
, _pt
in _active_layer
.mapPoint((_x
, _y
), _tol
):
138 _image
.sendMessage('group_action_started')
141 _image
.selectObject(_obj
)
143 _image
.sendMessage('group_action_ended')
145 # print "no objects ..."
146 gtkimage
.setPrompt(_('Click on another point to select the region.'))
147 gc
= gtkimage
.getGC()
148 gc
.set_line_attributes(1, gtk
.gdk
.LINE_SOLID
,
149 gtk
.gdk
.CAP_BUTT
, gtk
.gdk
.JOIN_MITER
)
150 gc
.set_function(gtk
.gdk
.INVERT
)
151 tool
.setLocation(_x
, _y
)
154 tool
.setHandler("motion_notify", select_motion_notify
)
155 tool
.setHandler("button_press", select_region_end_cb
)
157 def select_mode_init(gtkimage
, tool
=None):
158 gtkimage
.setPrompt(_('Click on the items you want to select.'))
159 _tool
= gtkimage
.getImage().getTool()
161 _tool
.setHandler("button_press", select_button_press_cb
)
163 def deselect_region_end_cb(gtkimage
, widget
, event
, tool
):
164 # print "called deselect_region_end_callback()"
165 _image
= gtkimage
.getImage()
166 _x2
, _y2
= _image
.getCurrentPoint()
167 _y1
= tool
.popObject()
168 _x1
= tool
.popObject()
169 _xmin
= min(_x1
, _x2
)
170 _xmax
= max(_x1
, _x2
)
171 _ymin
= min(_y1
, _y2
)
172 _ymax
= max(_y1
, _y2
)
173 tool
.delHandler("motion_notify")
174 _active_layer
= _image
.getActiveLayer()
175 _objs
= _active_layer
.objsInRegion(_xmin
, _ymin
, _xmax
, _ymax
)
178 for _obj
in _image
.getSelectedObjects(False):
179 if _obj
.getParent() is _active_layer
:
180 _sobjs
[id(_obj
)] = True
181 _image
.sendMessage('group_action_started')
184 if id(_obj
) in _sobjs
:
185 _image
.deselectObject(_obj
)
187 _image
.sendMessage('group_action_ended')
188 gtkimage
.setPrompt(_('Click on the items you want to deselect.'))
189 deselect_mode_init(gtkimage
)
191 def deselect_button_press_cb(gtkimage
, widget
, event
, tool
):
192 _tol
= gtkimage
.getTolerance()
193 _image
= gtkimage
.getImage()
194 _x
, _y
= _image
.getCurrentPoint()
195 # print "x: %g; y: %g" % (_x, _y)
196 _active_layer
= _image
.getActiveLayer()
198 for _obj
in _image
.getSelectedObjects(False):
199 if _obj
.getParent() is _active_layer
:
200 if isinstance(_obj
, Point
):
201 if abs(_obj
.x
- _x
) < _tol
and abs(_obj
.y
- _y
) < _tol
:
203 elif isinstance(_obj
, text
.TextBlock
):
204 _tx
, _ty
= _obj
.getLocation()
205 _bounds
= _obj
.getBounds()
206 if _bounds
is not None:
208 _align
= _obj
.getAlignment()
209 if _align
== text
.TextStyle
.ALIGN_LEFT
:
212 elif _align
== text
.TextStyle
.ALIGN_CENTER
:
216 elif _align
== text
.TextStyle
.ALIGN_RIGHT
:
220 raise ValueError, "Unexpected alignment: %d" % _align
223 if _txmin
< _x
< _txmax
and _tymin
< _y
< _tymax
:
225 elif _obj
.mapCoords(_x
, _y
, _tol
) is not None:
230 _image
.sendMessage('group_action_started')
233 _image
.deselectObject(_obj
)
235 _image
.sendMessage('group_action_ended')
237 gtkimage
.setPrompt(_('Click on another point to select the region.'))
238 gc
= gtkimage
.getGC()
239 gc
.set_line_attributes(1, gtk
.gdk
.LINE_SOLID
,
240 gtk
.gdk
.CAP_BUTT
, gtk
.gdk
.JOIN_MITER
)
241 gc
.set_function(gtk
.gdk
.INVERT
)
242 tool
.setLocation(_x
, _y
)
245 tool
.setHandler("motion_notify", select_motion_notify
)
246 tool
.setHandler("button_press", deselect_region_end_cb
)
248 def deselect_mode_init(gtkimage
, tool
=None):
249 gtkimage
.setPrompt(_('Click on the items you want to deselect.'))
250 _tool
= gtkimage
.getImage().getTool()
252 _tool
.setHandler("button_press", deselect_button_press_cb
)
254 def paste_button_press_cb(gtkimage
, widget
, event
, tool
):
255 # print "called paste_button_press_cb()"
256 _image
= gtkimage
.getImage()
257 _x
, _y
= _image
.getCurrentPoint()
258 # print "x: %g; y: %g" % (_x, _y)
259 _active_layer
= _image
.getActiveLayer()
260 _objs
= PythonCAD
.Generic
.globals.selectobj
.getObjects()
265 if isinstance(_obj
, Point
):
266 if not _objmap
.has_key('point'):
267 _objmap
['point'] = {}
269 _ept
= _active_layer
.findObject(_pt
)
271 _active_layer
.addObject(_pt
)
272 _objmap
['point'][_obj
] = _pt
274 _objmap
['point'][_obj
] = _ept
275 elif isinstance(_obj
, Segment
):
276 if not _objmap
.has_key('segment'):
277 _objmap
['segment'] = {}
280 _eseg
= _active_layer
.findObject(_cseg
)
282 _p1
, _p2
= _cseg
.getEndpoints()
283 _ep
= _active_layer
.findObject(_p1
)
285 _active_layer
.addObject(_p1
)
288 _ep
= _active_layer
.findObject(_p2
)
290 _active_layer
.addObject(_p2
)
293 _active_layer
.addObject(_cseg
)
295 _objmap
['segment'][_obj
] = _eseg
296 elif isinstance(_obj
, (Circle
, Arc
, CCircle
)):
299 _ec
= _active_layer
.findObject(_cc
)
301 _cp
= _cc
.getCenter()
302 _ep
= _active_layer
.findObject(_cp
)
304 _active_layer
.addObject(_cp
)
307 _active_layer
.addObject(_cc
)
308 elif isinstance(_obj
, (HCLine
, VCLine
, ACLine
)):
311 _ecl
= _active_layer
.findObject(_ccl
)
313 _lp
= _ccl
.getLocation()
314 _ep
= _active_layer
.findObject(_lp
)
316 _active_layer
.addObject(_lp
)
318 _ccl
.setLocation(_ep
)
319 _active_layer
.addObject(_ccl
)
320 elif isinstance(_obj
, CLine
):
323 _ecl
= _active_layer
.findObject(_ccl
)
325 _p1
, _p2
= _ccl
.getKeypoints()
326 _ep
= _active_layer
.findObject(_p1
)
328 _active_layer
.addObject(_p1
)
331 _ep
= _active_layer
.findObject(_p2
)
333 _active_layer
.addObject(_p2
)
336 _active_layer
.addObject(_ccl
)
337 elif isinstance(_obj
, LinearDimension
):
338 if _active_layer
.findObject(_obj
) is None:
339 _l1
, _l2
= _obj
.getDimLayers()
340 if _image
.hasLayer(_l1
) and _image
.hasLayer(_l2
):
341 _p1
, _p2
= _obj
.getDimPoints()
342 _ds
= _obj
.getDimStyle()
343 if isinstance(_obj
, HorizontalDimension
):
344 _dtype
= HorizontalDimension
345 elif isinstance(_obj
, VerticalDimension
):
346 _dtype
= VerticalDimension
348 _dtype
= LinearDimension
349 _dim
= _dtype(_l1
, _p1
, _l2
, _p2
, _x
, _y
, _ds
)
350 _active_layer
.addObject(_dim
)
351 elif isinstance(_obj
, RadialDimension
):
352 if _active_layer
.findObject(_obj
) is None:
353 _lyr
= _obj
.getDimLayer()
354 if _image
.hasLayer(_lyr
):
355 _dc
= _obj
.getDimCircle()
356 _ds
= _obj
.getDimStyle()
357 _dim
= RadialDimension(_lyr
, _dc
, _x
, _y
, _ds
)
358 _active_layer
.addObject(_dim
)
359 elif isinstance(_obj
, AngularDimension
):
360 if _active_layer
.findObject(_obj
) is None:
361 _cl
, _l1
, _l2
= _obj
.getDimLayers()
362 if (_image
.hasLayer(_cl
) and
363 _image
.hasLayer(_l1
) and
364 _image
.hasLayer(_l2
)):
365 _cp
, _p1
, _p2
= _obj
.getDimPoints()
366 _ds
= _obj
.getDimStyle()
367 _dim
= AngularDimension(_cl
, _cp
, _l1
, _p1
, _l2
, _p2
, _x
, _y
, _ds
)
368 _active_layer
.addObject(_dim
)
369 elif isinstance(_obj
, text
.TextBlock
):
371 _ntb
.setLocation(_x
, _y
)
372 _active_layer
.addObject(_ntb
)
374 print "Unexpected type for pasting: " + `
type(_obj
)`
378 def paste_mode_init(gtkimage
, tool
=None):
379 gtkimage
.setPrompt(_('Click where you want to paste the objects'))
380 _tool
= gtkimage
.getImage().getTool()
382 _tool
.setHandler("button_press", paste_button_press_cb
)