File: gtkedit.py

package info (click to toggle)
pythoncad 0.1.35-4
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 3,536 kB
  • ctags: 4,286
  • sloc: python: 62,752; sh: 743; makefile: 39
file content (376 lines) | stat: -rw-r--r-- 15,218 bytes parent folder | download
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
#
# Copyright (c) 2002, 2003, 2004, 2005, 2006 Art Haas
#
# This file is part of PythonCAD.
# 
# PythonCAD is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
# 
# PythonCAD is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with PythonCAD; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
#
# handle cut, copy, paste, selections, etc
#

import pygtk
pygtk.require('2.0')
import gtk

from PythonCAD.Generic.point import Point
from PythonCAD.Generic.segment import Segment
from PythonCAD.Generic.circle import Circle
from PythonCAD.Generic.arc import Arc
from PythonCAD.Generic.hcline import HCLine
from PythonCAD.Generic.vcline import VCLine
from PythonCAD.Generic.acline import ACLine
from PythonCAD.Generic.cline import CLine
from PythonCAD.Generic.ccircle import CCircle
from PythonCAD.Generic.dimension import LinearDimension
from PythonCAD.Generic.dimension import HorizontalDimension
from PythonCAD.Generic.dimension import VerticalDimension
from PythonCAD.Generic.dimension import RadialDimension
from PythonCAD.Generic.dimension import AngularDimension
from PythonCAD.Generic import text
import PythonCAD.Generic.globals

def select_region_end_cb(gtkimage, widget, event, tool):
    # print "called select_region_end_callback()"
    _image = gtkimage.getImage()
    _x2, _y2 = _image.getCurrentPoint()
    _y1 = tool.popObject()
    _x1 = tool.popObject()
    _xmin = min(_x1, _x2)
    _xmax = max(_x1, _x2)
    _ymin = min(_y1, _y2)
    _ymax = max(_y1, _y2)
    tool.delHandler("motion_notify")
    _active_layer = _image.getActiveLayer()
    _objs = _active_layer.objsInRegion(_xmin, _ymin, _xmax, _ymax)
    if len(_objs):
        _image.sendMessage('group_action_started')
        try:
            for _obj in _objs:
                _image.selectObject(_obj)
        finally:
            _image.sendMessage('group_action_ended')
    gtkimage.setPrompt(_('Click on the items you want to select.'))
    select_mode_init(tool)

def select_motion_notify(gtkimage, widget, event, tool):
    _tx, _ty = tool.getLocation()
    _px, _py = gtkimage.coordToPixTransform(_tx, _ty)
    _gc = gtkimage.getGC()
    _x = int(event.x)
    _y = int(event.y)
    _cp = tool.getCurrentPoint()
    if _cp is not None:
        _xc, _yc = _cp
        _xmin = min(_xc, _px)
        _ymin = min(_yc, _py)
        _rw = abs(_xc - _px)
        _rh = abs(_yc - _py)
        widget.window.draw_rectangle(_gc, False, _xmin, _ymin, _rw, _rh)
    tool.setCurrentPoint(_x, _y)
    _xmin = min(_x, _px)
    _ymin = min(_y, _py)
    _rw = abs(_x - _px)
    _rh = abs(_y - _py)
    widget.window.draw_rectangle(_gc, False, _xmin, _ymin, _rw, _rh)
    return True

def select_button_press_cb(gtkimage, widget, event, tool):
    _tol = gtkimage.getTolerance()
    _image = gtkimage.getImage()
    _x, _y = _image.getCurrentPoint()
    # print "x: %g; y: %g" % (_x, _y)
    _active_layer = _image.getActiveLayer()
    _pts = _active_layer.find('point', _x, _y, _tol)
    if len(_pts) > 0:
        _image.sendMessage('group_action_started')
        try:
            for _pt in _pts:
                _image.selectObject(_pt)
        finally:
            _image.sendMessage('group_action_ended')
    else:
        _objs = []
        for _tb in _active_layer.getLayerEntities('text'):
            # print "testing tb: " + `_tb`
            _tx, _ty = _tb.getLocation()
            # print "tx: %g; ty: %g" % (_tx, _ty)
            _bounds = _tb.getBounds()
            if _bounds is not None:
                # print "has bounds ..."
                _w, _h = _bounds
                _align = _tb.getAlignment()
                if _align == text.TextStyle.ALIGN_LEFT:
                    _txmin = _tx
                    _txmax = _tx + _w
                elif _align == text.TextStyle.ALIGN_CENTER:
                    _off = _w/2.0
                    _txmin = _tx - _off 
                    _txmax = _tx + _off
                elif _align == text.TextStyle.ALIGN_RIGHT:
                    _txmin = _tx - _w
                    _txmax = _tx
                else:
                    raise ValueError, "Unexpected alignment: %d" % _align
                _tymin = _ty - _h
                _tymax = _ty
                # print "txmin: %g" % _txmin
                # print "tymin: %g" % _tymin
                # print "txmax: %g" % _txmax
                # print "tymax: %g" % _tymax
                if _txmin < _x < _txmax and _tymin < _y < _tymax:
                    _objs.append(_tb)
        for _obj, _pt in _active_layer.mapPoint((_x, _y), _tol):
            _objs.append(_obj)
        if len(_objs):
            _image.sendMessage('group_action_started')
            try:
                for _obj in _objs:
                    _image.selectObject(_obj)
            finally:
                _image.sendMessage('group_action_ended')
        else:
            # print "no objects ..."
            gtkimage.setPrompt(_('Click on another point to select the region.'))
            gc = gtkimage.getGC()
            gc.set_line_attributes(1, gtk.gdk.LINE_SOLID,
                                   gtk.gdk.CAP_BUTT, gtk.gdk.JOIN_MITER)
            gc.set_function(gtk.gdk.INVERT)
            tool.setLocation(_x, _y)
            tool.pushObject(_x)
            tool.pushObject(_y)
            tool.setHandler("motion_notify", select_motion_notify)
            tool.setHandler("button_press", select_region_end_cb)

def select_mode_init(tool):
    tool.initialize()
    tool.setHandler("button_press", select_button_press_cb)

def deselect_region_end_cb(gtkimage, widget, event, tool):
    # print "called deselect_region_end_callback()"
    _image = gtkimage.getImage()
    _x2, _y2 = _image.getCurrentPoint()
    _y1 = tool.popObject()
    _x1 = tool.popObject()
    _xmin = min(_x1, _x2)
    _xmax = max(_x1, _x2)
    _ymin = min(_y1, _y2)
    _ymax = max(_y1, _y2)
    tool.delHandler("motion_notify")
    _active_layer = _image.getActiveLayer()
    _objs = _active_layer.objsInRegion(_xmin, _ymin, _xmax, _ymax)
    if len(_objs):
        _sobjs = {}
        for _obj in _image.getSelectedObjects(False):
            if _obj.getParent() is _active_layer:
                _sobjs[id(_obj)] = True
        _image.sendMessage('group_action_started')
        try:
            for _obj in _objs:
                if id(_obj) in _sobjs:
                    _image.deselectObject(_obj)
        finally:
            _image.sendMessage('group_action_ended')
    gtkimage.setPrompt(_('Click on the items you want to deselect.'))
    deselect_mode_init(tool)

def deselect_button_press_cb(gtkimage, widget, event, tool):
    _tol = gtkimage.getTolerance()
    _image = gtkimage.getImage()
    _x, _y = _image.getCurrentPoint()
    # print "x: %g; y: %g" % (_x, _y)
    _active_layer = _image.getActiveLayer()
    _objs = []
    for _obj in _image.getSelectedObjects(False):
        if _obj.getParent() is _active_layer:
            if isinstance(_obj, Point):
                if abs(_obj.x - _x) < _tol and abs(_obj.y - _y) < _tol:
                    _objs.append(_obj)
            elif isinstance(_obj, text.TextBlock):
                _tx, _ty = _obj.getLocation()
                _bounds = _obj.getBounds()
                if _bounds is not None:
                    _w, _h = _bounds
                    _align = _obj.getAlignment()
                    if _align == text.TextStyle.ALIGN_LEFT:
                        _txmin = _tx
                        _txmax = _tx + _w
                    elif _align == text.TextStyle.ALIGN_CENTER:
                        _off = _w/2.0
                        _txmin = _tx - _off 
                        _txmax = _tx + _off
                    elif _align == text.TextStyle.ALIGN_RIGHT:
                        _txmin = _tx - _w
                        _txmax = _tx
                    else:
                        raise ValueError, "Unexpected alignment: %d" % _align
                    _tymin = _ty - _h
                    _tymax = _ty
                    if _txmin < _x < _txmax and _tymin < _y < _tymax:
                        _objs.append(_obj)
            elif _obj.mapCoords(_x, _y, _tol) is not None:
                _objs.append(_obj)
            else:
                pass
    if len(_objs):
        _image.sendMessage('group_action_started')
        try:
            for _obj in _objs:
                _image.deselectObject(_obj)
        finally:
            _image.sendMessage('group_action_ended')
    else:
        gtkimage.setPrompt(_('Click on another point to select the region.'))
        gc = gtkimage.getGC()
        gc.set_line_attributes(1, gtk.gdk.LINE_SOLID,
                               gtk.gdk.CAP_BUTT, gtk.gdk.JOIN_MITER)
        gc.set_function(gtk.gdk.INVERT)
        tool.setLocation(_x, _y)
        tool.pushObject(_x)
        tool.pushObject(_y)
        tool.setHandler("motion_notify", select_motion_notify)
        tool.setHandler("button_press", deselect_region_end_cb)

def deselect_mode_init(tool):
    tool.initialize()
    tool.setHandler("button_press", deselect_button_press_cb)

def paste_button_press_cb(gtkimage, widget, event, tool):
    # print "called paste_button_press_cb()"
    _image = gtkimage.getImage()
    _x, _y = _image.getCurrentPoint()
    # print "x: %g; y: %g" % (_x, _y)
    _active_layer = _image.getActiveLayer()
    _objs = PythonCAD.Generic.globals.selectobj.getObjects()
    _objmap = {}
    _image.startAction()
    try:
        for _obj in _objs:
            if isinstance(_obj, Point):
                if not _objmap.has_key('point'):
                    _objmap['point'] = {}
                _pt = Point(_x, _y)
                _ept = _active_layer.findObject(_pt)
                if _ept is None:
                    _active_layer.addObject(_pt)
                    _objmap['point'][_obj] = _pt
                else:
                    _objmap['point'][_obj] = _ept
            elif isinstance(_obj, Segment):
                if not _objmap.has_key('segment'):
                    _objmap['segment'] = {}
                _cseg = _obj.clone()
                _cseg.move(_x, _y)
                _eseg = _active_layer.findObject(_cseg)
                if _eseg is None:
                    _p1, _p2 = _cseg.getEndpoints()
                    _ep = _active_layer.findObject(_p1)
                    if _ep is None:
                        _active_layer.addObject(_p1)
                    else:
                        _cseg.setP1(_ep)
                    _ep = _active_layer.findObject(_p2)
                    if _ep is None:
                        _active_layer.addObject(_p2)
                    else:
                        _cseg.setP2(_ep)
                    _active_layer.addObject(_cseg)
                else:
                    _objmap['segment'][_obj] = _eseg
            elif isinstance(_obj, (Circle, Arc, CCircle)):
                _cc = _obj.clone()
                _cc.move(_x, _y)
                _ec = _active_layer.findObject(_cc)
                if _ec is None:
                    _cp = _cc.getCenter()
                    _ep = _active_layer.findObject(_cp)
                    if _ep is None:
                        _active_layer.addObject(_cp)
                    else:
                        _cc.setLocation(_ep)
                    _active_layer.addObject(_cc)
            elif isinstance(_obj, (HCLine, VCLine, ACLine)):
                _ccl = _obj.clone()
                _ccl.move(_x, _y)
                _ecl = _active_layer.findObject(_ccl)
                if _ecl is None:
                    _lp = _ccl.getLocation()
                    _ep = _active_layer.findObject(_lp)
                    if _ep is None:
                        _active_layer.addObject(_lp)
                    else:
                        _ccl.setLocation(_ep)
                    _active_layer.addObject(_ccl)
            elif isinstance(_obj, CLine):
                _ccl = _obj.clone()
                _ccl.move(_x, _y)
                _ecl = _active_layer.findObject(_ccl)
                if _ecl is None:
                    _p1, _p2 = _ccl.getKeypoints()
                    _ep = _active_layer.findObject(_p1)
                    if _ep is None:
                        _active_layer.addObject(_p1)
                    else:
                        _ccl.setP1(_ep)
                    _ep = _active_layer.findObject(_p2)
                    if _ep is None:
                        _active_layer.addObject(_p2)
                    else:
                        _ccl.setP2(_ep)
                    _active_layer.addObject(_ccl)
            elif isinstance(_obj, LinearDimension):
                if _active_layer.findObject(_obj) is None:
                    _l1, _l2 = _obj.getDimLayers()
                    if _image.hasLayer(_l1) and _image.hasLayer(_l2):
                        _p1, _p2 = _obj.getDimPoints()
                        _ds = _obj.getDimStyle()
                        if isinstance(_obj, HorizontalDimension):
                            _dtype = HorizontalDimension
                        elif isinstance(_obj, VerticalDimension):
                            _dtype = VerticalDimension
                        else:
                            _dtype = LinearDimension
                        _dim = _dtype(_l1, _p1, _l2, _p2, _x, _y, _ds)
                        _active_layer.addObject(_dim)
            elif isinstance(_obj, RadialDimension):
                if _active_layer.findObject(_obj) is None:
                    _lyr = _obj.getDimLayer()
                    if _image.hasLayer(_lyr):
                        _dc = _obj.getDimCircle()
                        _ds = _obj.getDimStyle()
                        _dim = RadialDimension(_lyr, _dc, _x, _y, _ds)
                        _active_layer.addObject(_dim)
            elif isinstance(_obj, AngularDimension):
                if _active_layer.findObject(_obj) is None:
                    _cl, _l1, _l2 = _obj.getDimLayers()
                    if (_image.hasLayer(_cl) and
                        _image.hasLayer(_l1) and
                        _image.hasLayer(_l2)):
                        _cp, _p1, _p2 = _obj.getDimPoints()
                        _ds = _obj.getDimStyle()
                        _dim = AngularDimension(_cl, _cp, _l1, _p1, _l2, _p2, _x, _y, _ds)
                        _active_layer.addObject(_dim)
            elif isinstance(_obj, text.TextBlock):
                _ntb = _obj.clone()
                _ntb.setLocation(_x, _y)
                _active_layer.addObject(_ntb)
            else:
                print "Unexpected type for pasting: " + `type(_obj)`
    finally:
        _image.endAction()
    
def paste_mode_init(tool):
    tool.initialize()
    tool.setHandler("button_press", paste_button_press_cb)