1from Cocoa import * 2import objc 3 4import PDFHandling 5import BitmapContext 6import Utilities 7 8# Initial defaults 9_dpi = 144 10_useQT = False 11 12def getURLToExport(suffix): 13 savePanel = NSSavePanel.savePanel() 14 15 initialFileName = "BasicDrawing.%s"%(suffix,) 16 17 if savePanel.runModalForDirectory_file_(None, initialFileName) == NSFileHandlingPanelOKButton: 18 return savePanel.URL() 19 20 return None 21 22 23class MyAppController (NSObject): 24 theView = objc.IBOutlet() 25 currentDPIMenuItem = objc.IBOutlet() 26 currentExportStyleMenuItem = objc.IBOutlet() 27 28 @objc.IBAction 29 def print_(self, sender): 30 self.theView.print_(sender) 31 32 def updateDPIMenu_(self, sender): 33 if self.currentDPIMenuItem is not sender: 34 # Uncheck the previous item. 35 if self.currentDPIMenuItem is not None: 36 self.currentDPIMenuItem.setState_(NSOffState) 37 # Update to the current item. 38 self.currentDPIMenuItem = sender 39 # Check new menu item. 40 self.currentDPIMenuItem.setState_(NSOnState) 41 42 def updateExportStyleMenu_(self, sender): 43 if self.currentExportStyleMenuItem is not sender: 44 # Uncheck the previous item. 45 if self.currentExportStyleMenuItem is not None: 46 self.currentExportStyleMenuItem.setState_(NSOffState) 47 # Update to the current item. 48 self.currentExportStyleMenuItem = sender 49 # Check new menu item. 50 self.currentExportStyleMenuItem.setState_(NSOnState) 51 52 @objc.IBAction 53 def setExportResolution_(self, sender): 54 global _dpi 55 _dpi = sender.tag() 56 self.updateDPIMenu_(sender) 57 58 @objc.IBAction 59 def setUseQT_(self, sender): 60 global _useQT 61 _useQT = True 62 self.updateExportStyleMenu_(sender) 63 64 @objc.IBAction 65 def setUseCGImageSource_(self, sender): 66 global _useQT 67 _useQT = False 68 self.updateExportStyleMenu_(sender) 69 70 def setupExportInfo_(self, exportInfoP): 71 # Use the printable version of the current command. This produces 72 # the best results for exporting. 73 exportInfoP.command = self.theView.currentPrintableCommand() 74 exportInfoP.fileType = ' ' # unused 75 exportInfoP.useQTForExport = _useQT 76 exportInfoP.dpi = _dpi 77 78 @objc.IBAction 79 def exportAsPDF_(self, sender): 80 url = getURLToExport("pdf") 81 if url is not None: 82 exportInfo = Utilities.ExportInfo() 83 self.setupExportInfo_(exportInfo) 84 PDFHandling.MakePDFDocument(url, exportInfo) 85 86 @objc.IBAction 87 def exportAsPNG_(self, sender): 88 url = getURLToExport("png") 89 if url is not None: 90 exportInfo = Utilities.ExportInfo() 91 self.setupExportInfo_(exportInfo) 92 BitmapContext.MakePNGDocument(url, exportInfo) 93 94 @objc.IBAction 95 def exportAsTIFF_(self, sender): 96 url = getURLToExport("tif") 97 if url is not None: 98 exportInfo = Utilities.ExportInfo() 99 self.setupExportInfo_(exportInfo) 100 BitmapContext.MakeTIFFDocument(url, exportInfo) 101 102 103 @objc.IBAction 104 def exportAsJPEG_(self, sender): 105 url = getURLToExport("jpg") 106 if url is not None: 107 exportInfo = Utilities.ExportInfo() 108 self.setupExportInfo_(exportInfo) 109 BitmapContext.MakeJPEGDocument(url, exportInfo) 110 111 def validateMenuItem_(self, menuItem): 112 if menuItem.tag == _dpi: 113 currentDPIMenuItem = menuItem 114 menuItem.setState_(True) 115 elif menuItem.action() == 'setUseQT:': 116 if _useQT: 117 self.currentDPIMenuItem = menuItem 118 menuItem.setState_(True) 119 else: 120 menuItem.setState_(False) 121 122 elif menuItem.action() == 'setUseCGImageSource:': 123 if _useQT: 124 currentDPIMenuItem = menuItem 125 menuItem.setState_(True) 126 else: 127 menuItem.setState_(False) 128 129 return True 130