File: ImageDocument.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 (520 lines) | stat: -rw-r--r-- 16,726 bytes parent folder | download | duplicates (4)
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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
#
# Copyright (c) 2002-2004 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
#
# Cocoa wrapper for Image class - handles display of
# drawing in a window, saving & loading of files
#
# P.L.V. nov 23 05
# import adjustment
from __future__ import division

import math
import sys
import types

import PythonCAD.Generic.image
import PythonCAD.Generic.imageio
import PythonCAD.Generic.tools
import PythonCAD.Generic.globals
import PythonCAD.Generic.layer
import PythonCAD.Generic.message

import PythonCAD.Interface.Cocoa.Globals
import PythonCAD.Interface.Cocoa.CADView
from PythonCAD.Interface.Cocoa import ImageWindowController

from PyObjCTools import NibClassBuilder
from Foundation import NSLog, NSObject, NSString, NSNotificationCenter, NSNotificationQueue, NSNotification, NSPostWhenIdle, NSPostASAP, NSNotificationNoCoalescing
from AppKit import NSOutlineViewDataSource, NSWindowController, NSChangeDone

#
# Define True & False if this is Python < 2.2.1
#
try:
    True, False
except NameError:
    True, False = 1, 0

NibClassBuilder.extractClasses("ImageDocument")

class CocoaMessenger(PythonCAD.Generic.message.Messenger):
    """ Class converts PythonCAD messages into NSNotificationCenter
    messages
    
    """
    def added_object_event(self, obj, *args):
        """ Notification of new point, line, etc. added to drawing.
        object is Image object, userinfo dict stores new object
        under key "item"
        """
        _nq = NSNotificationQueue.defaultQueue()
        if len(args) != 2:
            raise ValueError, "Invalid number of new items in added_object_event"
        _info = {"layer" : args[0], "item": args[1]}
        _obj = Globals.wrap(obj)
        _note = NSNotification.notificationWithName_object_userInfo_("added_object", _obj, _info)
        _nq.enqueueNotification_postingStyle_coalesceMask_forModes_(_note, NSPostASAP, NSNotificationNoCoalescing, None)

    def deleted_object_event(self, obj, *args):
        """ Notification of point, line, etc. removed from a layer.
        object is Image object, userinfo dict stores object
        under key "item", layer it was removed from under "layer"
        """
        _nq = NSNotificationQueue.defaultQueue()
        if len(args) != 2:
            raise ValueError, "Invalid number of items in deleted_object_event"
        _info = {"layer" : args[0], "item": args[1]}
        _obj = Globals.wrap(obj)
        _note = NSNotification.notificationWithName_object_userInfo_("deleted_object", _obj, _info)
        _nq.enqueueNotification_postingStyle_coalesceMask_forModes_(_note, NSPostASAP, NSNotificationNoCoalescing, None)

    def added_child_event(self, obj, *args):
        """ Notification of new layer added to drawing.
        object is Image object, userinfo dict stores new layer
        under key "layer", parent layer under key "parent"
        """
        _nq = NSNotificationQueue.defaultQueue()
        if len(args) != 2:
            raise ValueError, "Invalid number of new items in added_layer_event"
        _info = {"layer" : args[0], "parent" : args[0].getParent()}
        _obj = Globals.wrap(obj)
        _note = NSNotification.notificationWithName_object_userInfo_(Globals.LayerAddedNotification, _obj, _info)
        _nq.enqueueNotification_postingStyle_coalesceMask_forModes_(_note, NSPostASAP, NSNotificationNoCoalescing, None)
  
    def removed_child_event(self, obj, *args):
        """ Notification of deletion of a layer.  object is Image object,
        userinfo dict stores layer under key "layer", parent layer under
        key "parent"
        """
        _nq = NSNotificationQueue.defaultQueue()
        if len(args) != 2:
            raise ValueError, "Invalid number of items in deleted_layer_event"
        _info = {"layer" : args[0], "parent" : args[0].getParent()}
        _obj = Globals.wrap(obj)
        _note = NSNotification.notificationWithName_object_userInfo_(Globals.LayerDeletedNotification, _obj, _info)
        _nq.enqueueNotification_postingStyle_coalesceMask_forModes_(_note, NSPostASAP, NSNotificationNoCoalescing, None)


# class defined in ImageDocument.nib
class ImageDocument(NibClassBuilder.AutoBaseClass):
    """Cocoa wrapper around an Image.
    
The ImageDocument is an NSDocument which has as a member variable
an instance of the Image class.  It is the data source for the 
Outline view of the window.  It has the following additional methods:

getDA(): Get CADView holding the drawing area
getOutlineView: Get NSOutlineView holding layer information
getEntry(): Get NSTextField used for command entry
getSplitView(): Get NSSplitView containing the CADView & Layer Outline View
{get/set}Option(): Get/set option in generic Image object
{get/set}Image(): Get generic Image object
{get/set}Prompt(): Get/Set the prompt.
{get/set}Tool(): Get/Set the tool used for working in the GTKImage.
{get/set}UnitsPerPixel(): Get/Set this display parameter.
{get/set}Point(): Get/Set the current coordinates of the tool.
reset(): Sets the image to an initial drawing state

    """
#
# Setup variables
#
    __image = None
#
# Beginning of Cocoa methods
#
    def init(self):
        """ NSDocument override for cocoa initializer of ImageDocument. 

init()
        """
        
        self = super(ImageDocument, self).init()
        if (self):
            #
            # Set up messaging
            #
            self.setMessenger(CocoaMessenger()) 
            #
            # Initialize image object
            #
            self.setImage(PythonCAD.Generic.image.Image())             
            #
            # tool setup & location
            #
            self.__tool = None
            self.__oldtool = PythonCAD.Generic.tools.Tool()
            self.__point_x = 0.0
            self.__point_y = 0.0
            
            #
            # viewable region 
            #
            self.__disp_width = None
            self.__disp_height = None
            self.__xmin = None
            self.__ymin = None
            self.__xmax = None
            self.__ymax = None
                                
        return self
        
    def windowNibName(self):
        """ NSDocument override to load ImageDocument nib

windowNibName()
        """
        return "ImageDocument"
        
    def makeWindowControllers(self):
        """ NSDocument override to manage custom NSWindowController
        
makeWindowControllers()
        """
        _nib = self.windowNibName()
        _wc = ImageWindowController.alloc().initWithWindowNibName_owner_(_nib,self)
        self.addWindowController_(_wc)
        
        
    def windowControllerDidLoadNib_(self, windowController):
        """ NSDocument override to do post-window appearance initialization
        
windowControllerDidLoadNib(windowController)
        """
        #
        # layer view setup
        #
        self.promptField.setStringValue_("Enter Command:")
        self.locationField.setStringValue_(self.getPoint())
        self.getOutlineView().reloadData()
        _da = self.getDA()
        _img = self.getImage()    
        if len(_img.getTopLayer().getChildren()) > 0:
            _da.fitImage()
        _da.postLoadSetup()
        
    
    
    def close(self):
        """ NSDocument override called when window is closing

close()
        """
        PythonCAD.Generic.image.Image.close(self.getImage())
        super(ImageDocument, self).close()
                
        
    def setFileName_(self, name):
        """ NSDocument override to keep filename set correctly in Image

setFileName(name)
        """
        self.getImage().setFilename(name)
        super(ImageDocument, self).setFileName_(name)
        
    def prepareSavePanel_(self, savePanel):
        """ NSDocument override to specify required extensions, etc

prepareSavePanel(savePanel)
        """
        savePanel.setRequiredFileType_("xml.gz")
        return True
        
            
    def writeToFile_ofType_(self, path, tp):
        """ NSDocument override to write file to disk
        
writeToFile_ofType_(path, filetype)
        """
        try:
            PythonCAD.Generic.imageio.save_image(self.getImage(),path)
        except (IOError, OSError), e:
            sys.stderr.write("Error writing file: %s\n" % str(e))
            return False
        except StandardError, e:
            sys.stderr.write("Error: %s\n" % str(e))
            return False
        return True

    def readFromFile_ofType_(self, path, tp):
        """ NSDocument override to read file from disk
        
readFromFile_ofType_(path, filetype)
        """
        _image = PythonCAD.Generic.image.Image()
        try:
            _imagefile = PythonCAD.Generic.fileio.CompFile(path, "r")
            try:
                PythonCAD.Generic.imageio.load_image(_image, _imagefile)
            finally:
                _imagefile.close()
        except (IOError, OSError), e:
            sys.stderr.write("Can't open '%s': %s\n" % (_fname, e))
            return False
        except StandardError, e:
            sys.stderr.write("Error: %s\n" % str(e))
            return False
        self.setImage(_image)
        return True

#
# End of Cocoa methods
#            
    
#
# Beginning of PythonCad specific methods
#    
    def addChildLayer(self, layer):
        """Add a new Layer as a Child of the entered layer.
#
#addChildLayer(l)
#
#This method covers the image::addChildLayer() method.
#        """
        new_layer = PythonCAD.Generic.layer.Layer("NewChildLayer")
        try:
            self.getImage().addChildLayer(new_layer, layer)
        except: #should do this better
            return
        self.updateChangeCount_(NSChangeDone)
#        _nc = NSNotificationCenter.defaultCenter()
#        _nc.postNotificationName_object_(Globals.LayerAddedNotification, self)

    def delLayer(self, layer):
        """Remove a Layer from the drawing.

delLayer(l)

This method covers the image::delLayer() method.
        """
        try:
            self.getImage().delLayer(layer)
        except: #should do this better
            return
        self.updateChangeCount_(NSChangeDone)
#        _nc = NSNotificationCenter.defaultCenter()
#        _nc.postNotificationName_object_(Globals.LayerDeletedNotification, self)
         
        
    def getDA(self):
        """ Return the CADView for the Image

getDA()
        """
        return self.CADView

    da = property(getDA, None, None, "DrawingArea for a ImageDocument.")
    
    def getOutlineView(self):
        """ Return the NSOutlineView holding layers for the Image
        
getOutlineView()
        """
        return self.layerOutlineView

    outline_view = property(getOutlineView, None, None, "OutlineView for a ImageDocument.")
    
    def getEntry(self):
        """ Returns the NSTextField for command entry
        
getEntry()
        """
        return self.entryField

    entry = property(getEntry, None, None, "Entry box for a ImageDocument.")
    
    def getSplitView(self):
        """ Returns the NSSplitView containing the OutlineView & DA

getSplitView()
        """
        return self.splitView
        
    def getOption(self, key):
        """Cover method to return the value of an option set in the drawing.

getOption(key)

Return the value of the option associated with the string "key".
If there is no option found for that key, return None.
        """
        return self.getImage().getOption(key)


    def setOption(self, key, value):
        """Cover method to set values of an option in the drawing

setOption(key, value)

The "key" must be a string, and "value" can be anything.
Using the same key twice will result on the second value
overwriting the first.
        """
        self.getImage().setOption(key, value)
        

    def getImage(self):
        """ Returns Generic PythonCad Image object associated with this document
        
getImage()
        """
        return self.__image
    
    image = property(getImage, None, None, "Generic Image object of ImageDocument")
    
    def setImage(self, image):
        """ Sets Generic PythondCad Image object associated with this document
        
setImage(image)
        """
        _curImg = self.getImage()
        if isinstance(_curImg, PythonCAD.Generic.image.Image): 
            PythonCAD.Generic.image.Image.close(_curImg)
        self.__image = image
        _m = self.getMessenger()
        image.connect("added_object", _m.added_object_event)
        image.connect("deleted_object", _m.deleted_object_event)
        image.connect("added_child", _m.added_child_event)
        image.connect("removed_child", _m.removed_child_event)
        
        
    def getMessenger(self):
        """  Returns PythonCad to NSNotificationCenter translation object for
        this document

getMessenger()
        """
        return self.__messenger
        
    def setMessenger(self, m):
        """ Sets PythongCad to NSNotificationCenter translation object for this document
        
setMessenger()
        """
        if not isinstance(m, CocoaMessenger):
            raise TypeError, "Invalid messenger object!"
        self.__messenger = m
        
    def getPrompt(self):
        """Return the current prompt string.

getPrompt()        
        """
        return self.promptField.stringValue()
    
    def setPrompt(self, prompt):
        """Set the current prompt string.

setPrompt(prompt)
        """
        if not isinstance(prompt, types.StringTypes):
            raise TypeError, "Invalid prompt: " + `prompt`
        self.promptField.setStringValue_(prompt)

    prompt = property(getPrompt, setPrompt, None, "Prompt string.")
        
    
    def setTool(self, tool):
        """Replace the tool in the image with a new Tool.

setTool(tool)

The argument "tool" should be an instance of a Tool object.
        """
        if not isinstance(tool, PythonCAD.Generic.tools.Tool):
            raise TypeError, "Invalid tool: " + str(tool)
        self.__tool = tool
        _nc = NSNotificationCenter.defaultCenter()
        _nc.postNotificationName_object_userInfo_(Globals.ToolChangedNotification, self, {"tool" : tool})

    def getTool(self):
        """Return the current Tool used in the drawing.

getTool()        
        """
        return self.__tool
    
    tool = property(getTool, None, None, "Tool for adding/modifying entities.")


    def getPoint(self):
        """Get the current point where the tool is located.

getPoint()

This function returns a tuple with two floats

(x,y)

x: x-coordinate
y: y-coordinate
        """
        return (self.__point_x, self.__point_y)

    def setPoint(self, x, y):
        """Store the point where the tool currently is located at.

setPoint(x,y)

The x and y arguments should be floats.
        """
        _x = x
        _y = y
        try:
            if not isinstance(_x, float):
                _x = float(x)
            if not isinstance(_y, float):
                _y = float(y)
            _str = "%.4f, %.4f" % (_x, _y)
            self.locationField.setStringValue_(_str)
            self.__point_x = _x
            self.__point_y = _y
        except: # need better error handling
            pass
            
    point = property(getPoint, setPoint, None, "Current tool location.")
    
    def reset(self):
        """Set the image to an initial drawing state.

reset()        
        """
        self.__tool = None
        self.setPrompt("Enter command:")
        self.getEntry().setStringValue_("")
        _da = self.getDA()
        _da.setTempObject()
        _da.setNeedsDisplay_(True)


    def changeColor_(self, sender):
        """ Records new colors chosen from color picker.
        
changeColor_(colorPicker)
        """
        _nscolor = sender.color().colorUsingColorSpaceName_("NSCalibratedRGBColorSpace")
        (_r, _g, _b, _a) = _nscolor.getRed_green_blue_alpha_()
        _ir = int(_r*255)
        _ig = int(_g*255)
        _ib = int(_b*255)
        _color = PythonCAD.Generic.color.get_color(_ir, _ig, _ib)
        self.setOption('LINE_COLOR', _color)